| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Interwiki; |
| 4 |
|
| 5 |
use MediaWiki\MediaWikiServices; |
| 6 |
use MediaWiki\Permissions\Hook\UserGetAllRightsHook; |
| 7 |
use MediaWiki\WikiMap\WikiMap; |
| 8 |
|
| 9 |
class Hooks implements UserGetAllRightsHook { |
| 10 |
|
| 11 |
private static $shouldSkipIWCheck = false; |
| 12 |
|
| 13 |
private static $shouldSkipILCheck = false; |
| 14 |
|
| 15 |
public static function onExtensionFunctions() { |
| 16 |
global $wgInterwikiViewOnly; |
| 17 |
|
| 18 |
if ( !$wgInterwikiViewOnly ) { |
| 19 |
global $wgLogTypes; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
$wgLogTypes[] = 'interwiki'; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
global $wgInterwikiCentralDB, $wgInterwikiCentralInterlanguageDB; |
| 28 |
|
| 29 |
self::$shouldSkipIWCheck = ( |
| 30 |
$wgInterwikiCentralDB === null || |
| 31 |
$wgInterwikiCentralDB === WikiMap::getCurrentWikiId() |
| 32 |
); |
| 33 |
self::$shouldSkipILCheck = ( |
| 34 |
$wgInterwikiCentralInterlanguageDB === null || |
| 35 |
$wgInterwikiCentralInterlanguageDB === WikiMap::getCurrentWikiId() |
| 36 |
); |
| 37 |
if ( self::$shouldSkipIWCheck && self::$shouldSkipILCheck ) { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
MediaWikiServices::getInstance()->getHookContainer()->register( |
| 45 |
'InterwikiLoadPrefix', 'MediaWiki\Extension\Interwiki\Hooks::onInterwikiLoadPrefix' |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function onUserGetAllRights( &$rights ) { |
| 53 |
global $wgInterwikiViewOnly; |
| 54 |
if ( !$wgInterwikiViewOnly ) { |
| 55 |
|
| 56 |
$rights[] = 'interwiki'; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public static function onInterwikiLoadPrefix( $prefix, &$iwData ) { |
| 61 |
global $wgInterwikiCentralDB, $wgInterwikiCentralInterlanguageDB; |
| 62 |
|
| 63 |
$services = MediaWikiServices::getInstance(); |
| 64 |
$connectionProvider = $services->getConnectionProvider(); |
| 65 |
$isInterlanguageLink = $services->getLanguageNameUtils()->getLanguageName( $prefix ); |
| 66 |
if ( !$isInterlanguageLink && !self::$shouldSkipIWCheck ) { |
| 67 |
|
| 68 |
$lookup = $services->getInterwikiLookup(); |
| 69 |
foreach ( $lookup->getAllPrefixes( null ) as $id => $localPrefixInfo ) { |
| 70 |
if ( $prefix === $localPrefixInfo['iw_prefix'] ) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
$dbrCentralDB = $connectionProvider->getReplicaDatabase( $wgInterwikiCentralDB ?? false ); |
| 76 |
|
| 77 |
$res = $dbrCentralDB->newSelectQueryBuilder() |
| 78 |
->select( '*' ) |
| 79 |
->from( 'interwiki' ) |
| 80 |
->where( [ 'iw_prefix' => $prefix ] ) |
| 81 |
->caller( __METHOD__ ) |
| 82 |
->fetchRow(); |
| 83 |
if ( !$res ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
$iwData = (array)$res; |
| 88 |
|
| 89 |
return false; |
| 90 |
} elseif ( $isInterlanguageLink && !self::$shouldSkipILCheck ) { |
| 91 |
|
| 92 |
$dbrCentralLangDB = $connectionProvider->getReplicaDatabase( $wgInterwikiCentralInterlanguageDB ?? false ); |
| 93 |
|
| 94 |
$res = $dbrCentralLangDB->newSelectQueryBuilder() |
| 95 |
->select( '*' ) |
| 96 |
->from( 'interwiki' ) |
| 97 |
->where( [ 'iw_prefix' => $prefix ] ) |
| 98 |
->caller( __METHOD__ ) |
| 99 |
->fetchRow(); |
| 100 |
if ( !$res ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
$iwData = (array)$res; |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
} |
| 109 |
} |