MediaWiki  master
WikiAwareEntityTrait.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\DAO;
22 
25 use Wikimedia\Assert\Assert;
26 use Wikimedia\Assert\PreconditionException;
27 
33 
45  abstract public function getWikiId();
46 
54  public function assertWiki( $wikiId ) {
55  if ( $wikiId !== $this->getWikiId() ) {
56  $expected = $this->wikiIdToString( $wikiId );
57  $actual = $this->wikiIdToString( $this->getWikiId() );
58  throw new PreconditionException(
59  "Expected " . __CLASS__ . " to belong to $expected, but it belongs to $actual"
60  );
61  }
62  }
63 
70  protected function deprecateInvalidCrossWiki( $wikiId, string $since ) {
71  if ( $wikiId !== $this->getWikiId() ) {
72  $expected = $this->wikiIdToString( $wikiId );
73  $actual = $this->wikiIdToString( $this->getWikiId() );
75  'Deprecated cross-wiki access to ' . __CLASS__ . '. ' .
76  "Expected: {$expected}, Actual: {$actual}. " .
77  "Pass expected \$wikiId.",
78  $since
79  );
80  }
81  }
82 
88  protected function assertWikiIdParam( $wikiId ) {
89  Assert::parameterType( 'string|boolean', $wikiId, '$wikiId' );
90  Assert::parameter( $wikiId !== true, '$wikiId', 'must be false or a string' );
91  }
92 
99  private function wikiIdToString( $wikiId ): string {
100  return $wikiId === WikiAwareEntity::LOCAL ? 'the local wiki' : "'{$wikiId}'";
101  }
102 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\DAO\WikiAwareEntityTrait
trait WikiAwareEntityTrait
Definition: WikiAwareEntityTrait.php:32
MediaWiki\DAO\deprecateInvalidCrossWiki
deprecateInvalidCrossWiki( $wikiId, string $since)
Emits a deprecation warning $since version if $wikiId is not the same as this wiki.
Definition: WikiAwareEntityTrait.php:70
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\DAO\wikiIdToString
wikiIdToString( $wikiId)
Convert $wikiId to a string for logging.
Definition: WikiAwareEntityTrait.php:99
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1028
MediaWiki\DAO\assertWiki
assertWiki( $wikiId)
Throws if $wikiId is not the same as this entity wiki.
Definition: WikiAwareEntityTrait.php:54
MediaWiki\DAO
Definition: WikiAwareEntity.php:3
MediaWiki\DAO\assertWikiIdParam
assertWikiIdParam( $wikiId)
Asserts correct $wikiId parameter was passed.
Definition: WikiAwareEntityTrait.php:88