aboutsummaryrefslogtreecommitdiffstats
path: root/includes/dao/WikiAwareEntityTrait.php
blob: 8f438af06fc678b368b0f09ccd212e3c8b7d4ae4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\DAO;

use MediaWiki\Page\PageIdentity;
use MediaWiki\User\UserIdentity;
use Wikimedia\Assert\Assert;
use Wikimedia\Assert\PreconditionException;

/**
 * Helper trait for {@link WikiAwareEntity implementations}
 * @package MediaWiki\DAO
 */
trait WikiAwareEntityTrait {

	/**
	 * Get the ID of the wiki this entity belongs to.
	 *
	 * @since 1.36
	 *
	 * @see RevisionRecord::getWikiId()
	 * @see UserIdentity::getWikiId()
	 * @see PageIdentity::getWikiId()
	 * @see Block::getWikiId()
	 *
	 * @return string|false The wiki's logical name or WikiAwareEntity::LOCAL for the local wiki
	 */
	abstract public function getWikiId();

	/**
	 * Throws if $wikiId is not the same as this entity wiki.
	 *
	 * @param string|false $wikiId The wiki ID expected by the caller.
	 *
	 * @throws PreconditionException
	 */
	public function assertWiki( $wikiId ) {
		if ( $wikiId !== $this->getWikiId() ) {
			$expected = $this->wikiIdToString( $wikiId );
			$actual = $this->wikiIdToString( $this->getWikiId() );
			throw new PreconditionException(
				"Expected " . __CLASS__ . " to belong to $expected, but it belongs to $actual"
			);
		}
	}

	/**
	 * Emits a deprecation warning $since version if $wikiId is not the same as this wiki.
	 *
	 * @param string|false $wikiId
	 * @param string $since
	 */
	protected function deprecateInvalidCrossWiki( $wikiId, string $since ) {
		if ( $wikiId !== $this->getWikiId() ) {
			$expected = $this->wikiIdToString( $wikiId );
			$actual = $this->wikiIdToString( $this->getWikiId() );
			wfDeprecatedMsg(
				'Deprecated cross-wiki access to ' . __CLASS__ . '. ' .
				"Expected: {$expected}, Actual: {$actual}. " .
				"Pass expected \$wikiId.",
				$since
			);
		}
	}

	/**
	 * Asserts correct $wikiId parameter was passed.
	 *
	 * @param string|false $wikiId
	 */
	protected function assertWikiIdParam( $wikiId ) {
		Assert::parameterType( [ 'string', 'false' ], $wikiId, '$wikiId' );
	}

	/**
	 * Convert $wikiId to a string for logging.
	 *
	 * @param string|false $wikiId
	 * @return string
	 */
	private function wikiIdToString( $wikiId ): string {
		return $wikiId === WikiAwareEntity::LOCAL ? 'the local wiki' : "'{$wikiId}'";
	}
}