Use "optional_services" to inject service MobileContext

This change requires a soft dependency on extension MobileFrontend
in project integration/config in file zuul/parameter_functions.py.

Change-Id: I4e0c8185804a22c57f94dceae3c998e13afa3cfc
这个提交包含在:
Fomafix 2024-04-02 19:34:12 +00:00
父节点 0b9631ba86
当前提交 2a31c0b399
共有 4 个文件被更改,包括 27 次插入8 次删除

查看文件

@ -2,4 +2,17 @@
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
$cfg['directory_list'] = array_merge(
$cfg['directory_list'],
[
'../../extensions/MobileFrontend',
]
);
$cfg['exclude_analysis_directory_list'] = array_merge(
$cfg['exclude_analysis_directory_list'],
[
'../../extensions/MobileFrontend',
]
);
return $cfg;

查看文件

@ -428,6 +428,9 @@
"MainConfig",
"SpecialPageFactory",
"UserOptionsLookup"
],
"optional_services": [
"MobileFrontend.Context"
]
}
},

查看文件

@ -30,7 +30,6 @@ use MediaWiki\Config\Config;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\MakeGlobalVariablesScriptHook;
use MediaWiki\Hook\ThumbnailBeforeProduceHTMLHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use MediaWiki\Page\Hook\CategoryPageViewHook;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
@ -39,6 +38,7 @@ use MediaWiki\SpecialPage\SpecialPageFactory;
use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\User;
use MobileContext;
use Skin;
use ThumbnailImage;
@ -66,6 +66,7 @@ class Hooks implements
private Config $config;
private SpecialPageFactory $specialPageFactory;
private UserOptionsLookup $userOptionsLookup;
private ?MobileContext $mobileContext;
/**
* @param Config $config
@ -75,11 +76,13 @@ class Hooks implements
public function __construct(
Config $config,
SpecialPageFactory $specialPageFactory,
UserOptionsLookup $userOptionsLookup
UserOptionsLookup $userOptionsLookup,
?MobileContext $mobileContext
) {
$this->config = $config;
$this->specialPageFactory = $specialPageFactory;
$this->userOptionsLookup = $userOptionsLookup;
$this->mobileContext = $mobileContext;
}
/**
@ -113,13 +116,12 @@ class Hooks implements
* Could be on article pages or on Category pages
* @param OutputPage $out
*/
protected static function getModules( OutputPage $out ) {
protected function getModules( OutputPage $out ) {
// The MobileFrontend extension provides its own implementation of MultimediaViewer.
// See https://phabricator.wikimedia.org/T65504 and subtasks for more details.
// To avoid loading MMV twice, we check the environment we are running in.
$isMobileFrontendView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' )
->shouldDisplayMobileView();
$this->mobileContext && $this->mobileContext->shouldDisplayMobileView();
if ( !$isMobileFrontendView ) {
$out->addModules( [ 'mmv.head', 'mmv.bootstrap.autostart' ] );
}
@ -146,7 +148,7 @@ class Hooks implements
$fileRelatedSpecialPages );
if ( $pageHasThumbnails || $pageIsFilePage || $pageIsFileRelatedSpecialPage || $pageIsFlowPage ) {
self::getModules( $out );
$this->getModules( $out );
}
}
@ -160,7 +162,7 @@ class Hooks implements
$cat = Category::newFromTitle( $title );
if ( $cat->getFileCount() > 0 ) {
$out = $catPage->getContext()->getOutput();
self::getModules( $out );
$this->getModules( $out );
}
}

查看文件

@ -17,7 +17,8 @@ class HooksTest extends MediaWikiIntegrationTestCase {
return new Hooks(
$this->getServiceContainer()->getMainConfig(),
$this->getServiceContainer()->getSpecialPageFactory(),
$this->getServiceContainer()->getUserOptionsLookup()
$this->getServiceContainer()->getUserOptionsLookup(),
null
);
}