| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\DAO; |
| 22 | |
| 23 | use MediaWiki\Page\PageIdentity; |
| 24 | use MediaWiki\User\UserIdentity; |
| 25 | use Wikimedia\Assert\Assert; |
| 26 | use Wikimedia\Assert\PreconditionException; |
| 27 | |
| 28 | /** |
| 29 | * Helper trait for {@link WikiAwareEntity implementations} |
| 30 | * @package MediaWiki\DAO |
| 31 | */ |
| 32 | trait WikiAwareEntityTrait { |
| 33 | |
| 34 | /** |
| 35 | * Get the ID of the wiki this entity belongs to. |
| 36 | * |
| 37 | * @since 1.36 |
| 38 | * |
| 39 | * @see RevisionRecord::getWikiId() |
| 40 | * @see UserIdentity::getWikiId() |
| 41 | * @see PageIdentity::getWikiId() |
| 42 | * @see Block::getWikiId() |
| 43 | * |
| 44 | * @return string|false The wiki's logical name or WikiAwareEntity::LOCAL for the local wiki |
| 45 | */ |
| 46 | abstract public function getWikiId(); |
| 47 | |
| 48 | /** |
| 49 | * Throws if $wikiId is not the same as this entity wiki. |
| 50 | * |
| 51 | * @param string|false $wikiId The wiki ID expected by the caller. |
| 52 | * |
| 53 | * @throws PreconditionException |
| 54 | */ |
| 55 | public function assertWiki( $wikiId ) { |
| 56 | if ( $wikiId !== $this->getWikiId() ) { |
| 57 | $expected = $this->wikiIdToString( $wikiId ); |
| 58 | $actual = $this->wikiIdToString( $this->getWikiId() ); |
| 59 | throw new PreconditionException( |
| 60 | "Expected " . __CLASS__ . " to belong to $expected, but it belongs to $actual" |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Emits a deprecation warning $since version if $wikiId is not the same as this wiki. |
| 67 | * |
| 68 | * @param string|false $wikiId |
| 69 | * @param string $since |
| 70 | */ |
| 71 | protected function deprecateInvalidCrossWiki( $wikiId, string $since ) { |
| 72 | if ( $wikiId !== $this->getWikiId() ) { |
| 73 | $expected = $this->wikiIdToString( $wikiId ); |
| 74 | $actual = $this->wikiIdToString( $this->getWikiId() ); |
| 75 | wfDeprecatedMsg( |
| 76 | 'Deprecated cross-wiki access to ' . __CLASS__ . '. ' . |
| 77 | "Expected: {$expected}, Actual: {$actual}. " . |
| 78 | "Pass expected \$wikiId.", |
| 79 | $since |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Asserts correct $wikiId parameter was passed. |
| 86 | * |
| 87 | * @param string|false $wikiId |
| 88 | */ |
| 89 | protected function assertWikiIdParam( $wikiId ) { |
| 90 | Assert::parameterType( [ 'string', 'false' ], $wikiId, '$wikiId' ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Convert $wikiId to a string for logging. |
| 95 | * |
| 96 | * @param string|false $wikiId |
| 97 | * @return string |
| 98 | */ |
| 99 | private function wikiIdToString( $wikiId ): string { |
| 100 | return $wikiId === WikiAwareEntity::LOCAL ? 'the local wiki' : "'{$wikiId}'"; |
| 101 | } |
| 102 | } |