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;
|
2022-12-27 16:18:50 +00:00
|
|
|
$isInterlanguageLink = MediaWikiServices::getInstance()->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
|
2018-06-07 18:39:17 +00:00
|
|
|
$lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
|
|
|
|
foreach ( $lookup->getAllPrefixes( null ) as $id => $localPrefixInfo ) {
|
2015-05-03 02:24:46 +00:00
|
|
|
if ( $prefix === $localPrefixInfo['iw_prefix'] ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 19:24:47 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument
|
2017-09-24 05:27:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA, [], $wgInterwikiCentralDB );
|
2015-05-03 02:24:46 +00:00
|
|
|
$res = $dbr->selectRow(
|
|
|
|
'interwiki',
|
|
|
|
'*',
|
2016-03-07 14:41:22 +00:00
|
|
|
[ 'iw_prefix' => $prefix ],
|
2015-05-03 02:24:46 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
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!
|
2021-09-08 19:24:47 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument
|
2019-04-05 23:47:04 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA, [], $wgInterwikiCentralInterlanguageDB );
|
|
|
|
$res = $dbr->selectRow(
|
|
|
|
'interwiki',
|
|
|
|
'*',
|
|
|
|
[ 'iw_prefix' => $prefix ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|