2015-05-03 02:24:46 +00:00
|
|
|
<?php
|
|
|
|
|
2021-03-14 05:06:15 +00:00
|
|
|
namespace MediaWiki\Extension\Interwiki;
|
|
|
|
|
2018-06-07 18:39:17 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-08-15 10:43:27 +00:00
|
|
|
use MediaWiki\Permissions\Hook\UserGetAllRightsHook;
|
2023-04-25 09:54:05 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2018-06-07 18:39:17 +00:00
|
|
|
|
2023-08-15 10:43:27 +00:00
|
|
|
class Hooks implements UserGetAllRightsHook {
|
2020-12-22 13:26:41 +00:00
|
|
|
/** @var bool */
|
|
|
|
private static $shouldSkipIWCheck = false;
|
|
|
|
/** @var bool */
|
|
|
|
private static $shouldSkipILCheck = false;
|
|
|
|
|
2015-05-03 02:24:46 +00:00
|
|
|
public static function onExtensionFunctions() {
|
|
|
|
global $wgInterwikiViewOnly;
|
|
|
|
|
2018-07-25 23:48:09 +00:00
|
|
|
if ( !$wgInterwikiViewOnly ) {
|
|
|
|
global $wgLogTypes;
|
2015-05-03 02:24:46 +00:00
|
|
|
|
|
|
|
// Set up the new log type - interwiki actions are logged to this new log
|
2018-07-25 23:48:09 +00:00
|
|
|
// TODO: Move this out of an extension function once T200385 is implemented.
|
2015-05-03 02:24:46 +00:00
|
|
|
$wgLogTypes[] = 'interwiki';
|
|
|
|
}
|
2020-12-22 13:26:41 +00:00
|
|
|
// Register the (deprecated) InterwikiLoadPrefix hook only if one
|
|
|
|
// of the wgInterwiki*CentralDB globals is set.
|
|
|
|
global $wgInterwikiCentralDB, $wgInterwikiCentralInterlanguageDB;
|
|
|
|
|
|
|
|
self::$shouldSkipIWCheck = (
|
|
|
|
$wgInterwikiCentralDB === null ||
|
2021-12-22 08:22:25 +00:00
|
|
|
$wgInterwikiCentralDB === WikiMap::getCurrentWikiId()
|
2020-12-22 13:26:41 +00:00
|
|
|
);
|
|
|
|
self::$shouldSkipILCheck = (
|
|
|
|
$wgInterwikiCentralInterlanguageDB === null ||
|
2021-12-22 08:22:25 +00:00
|
|
|
$wgInterwikiCentralInterlanguageDB === WikiMap::getCurrentWikiId()
|
2020-12-22 13:26:41 +00:00
|
|
|
);
|
|
|
|
if ( self::$shouldSkipIWCheck && self::$shouldSkipILCheck ) {
|
|
|
|
// Bail out early if _neither_ $wgInterwiki*CentralDB
|
|
|
|
// global is set; if one or both are set, we gotta register
|
|
|
|
// the InterwikiLoadPrefix hook.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This will trigger a deprecation warning in MW 1.36+
|
2023-05-06 21:05:11 +00:00
|
|
|
MediaWikiServices::getInstance()->getHookContainer()->register(
|
2021-05-24 20:47:17 +00:00
|
|
|
'InterwikiLoadPrefix', 'MediaWiki\Extension\Interwiki\Hooks::onInterwikiLoadPrefix'
|
2020-12-22 13:26:41 +00:00
|
|
|
);
|
2018-07-25 23:48:09 +00:00
|
|
|
}
|
2015-05-03 02:24:46 +00:00
|
|
|
|
2018-07-25 23:48:09 +00:00
|
|
|
/**
|
|
|
|
* @param array &$rights
|
|
|
|
*/
|
2023-08-15 10:43:27 +00:00
|
|
|
public function onUserGetAllRights( &$rights ) {
|
2018-07-25 23:48:09 +00:00
|
|
|
global $wgInterwikiViewOnly;
|
|
|
|
if ( !$wgInterwikiViewOnly ) {
|
|
|
|
// New user right, required to modify the interwiki table through Special:Interwiki
|
|
|
|
$rights[] = 'interwiki';
|
|
|
|
}
|
2015-05-03 02:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function onInterwikiLoadPrefix( $prefix, &$iwData ) {
|
2019-04-05 23:47:04 +00:00
|
|
|
global $wgInterwikiCentralDB, $wgInterwikiCentralInterlanguageDB;
|
2024-03-21 16:36:24 +00:00
|
|
|
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$connectionProvider = $services->getConnectionProvider();
|
|
|
|
$isInterlanguageLink = $services->getLanguageNameUtils()->getLanguageName( $prefix );
|
2020-12-22 13:26:41 +00:00
|
|
|
if ( !$isInterlanguageLink && !self::$shouldSkipIWCheck ) {
|
2015-05-03 02:24:46 +00:00
|
|
|
// Check if prefix exists locally and skip
|
2024-03-21 16:36:24 +00:00
|
|
|
$lookup = $services->getInterwikiLookup();
|
2018-06-07 18:39:17 +00:00
|
|
|
foreach ( $lookup->getAllPrefixes( null ) as $id => $localPrefixInfo ) {
|
2015-05-03 02:24:46 +00:00
|
|
|
if ( $prefix === $localPrefixInfo['iw_prefix'] ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 16:36:24 +00:00
|
|
|
|
|
|
|
$dbrCentralDB = $connectionProvider->getReplicaDatabase( $wgInterwikiCentralDB ?? false );
|
|
|
|
|
2024-04-26 19:44:06 +00:00
|
|
|
$res = $dbrCentralDB->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'interwiki' )
|
|
|
|
->where( [ 'iw_prefix' => $prefix ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRow();
|
2015-05-03 02:24:46 +00:00
|
|
|
if ( !$res ) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-01 00:15:51 +00:00
|
|
|
// Explicitly make this an array since it's expected to be one
|
2015-05-03 02:24:46 +00:00
|
|
|
$iwData = (array)$res;
|
|
|
|
// At this point, we can safely return false because we know that we have something
|
|
|
|
return false;
|
2020-12-22 13:26:41 +00:00
|
|
|
} elseif ( $isInterlanguageLink && !self::$shouldSkipILCheck ) {
|
2019-04-05 23:47:04 +00:00
|
|
|
// Global interlanguage link? Whoo!
|
2024-03-21 16:36:24 +00:00
|
|
|
$dbrCentralLangDB = $connectionProvider->getReplicaDatabase( $wgInterwikiCentralInterlanguageDB ?? false );
|
|
|
|
|
2024-04-26 19:44:06 +00:00
|
|
|
$res = $dbrCentralLangDB->newSelectQueryBuilder()
|
|
|
|
->select( '*' )
|
|
|
|
->from( 'interwiki' )
|
|
|
|
->where( [ 'iw_prefix' => $prefix ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchRow();
|
2019-04-05 23:47:04 +00:00
|
|
|
if ( !$res ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-01 00:15:51 +00:00
|
|
|
// Explicitly make this an array since it's expected to be one
|
2019-04-05 23:47:04 +00:00
|
|
|
$iwData = (array)$res;
|
|
|
|
// At this point, we can safely return false because we know that we have something
|
|
|
|
return false;
|
2015-05-03 02:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|