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
This commit is contained in:
Fomafix 2024-04-02 19:34:12 +00:00
parent 0b9631ba86
commit 2a31c0b399
4 changed files with 27 additions and 8 deletions

View file

@ -2,4 +2,17 @@
$cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php'; $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; return $cfg;

View file

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

View file

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

View file

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