Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.07% |
110 / 305 |
|
15.38% |
6 / 39 |
CRAP | |
0.00% |
0 / 1 |
| MobileFrontendHooks | |
36.07% |
110 / 305 |
|
15.38% |
6 / 39 |
4685.57 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultMobileSkin | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| onRequestContextCreateSkin | |
68.18% |
15 / 22 |
|
0.00% |
0 / 1 |
8.58 | |||
| onSkinAddFooterLinks | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| onBeforeDisplayNoArticleText | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| onOutputPageBeforeHTML | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 | |||
| onOutputPageBodyAttributes | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| onBeforePageRedirect | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| onApiBeforeMain | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| onResourceLoaderBeforeResponse | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| onMediaWikiPerformAction | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
5.93 | |||
| onResourceLoaderSiteStylesModulePages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| onResourceLoaderSiteModulePages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| onGetCacheVaryCookies | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| onListDefinedTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onChangeTagsListActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addDefinedTags | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| onRecentChange_save | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onManualLogEntryBeforePublish | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onTaggableObjectCreation | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| onAbuseFilterGenerateUserVars | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| onAbuseFilterBuilder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSpecialPageBeforeExecute | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| shouldCollapseSectionsByDefault | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| onBeforePageDisplay | |
89.47% |
68 / 76 |
|
0.00% |
0 / 1 |
23.62 | |||
| interimTogglingSupportForParsoid | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterBuildFeedLinks | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| onGetPreferences | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| setTagline | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| findTagline | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| onOutputPageParserOutput | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| onArticleParserOptions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| shouldUseParsoid | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
2.86 | |||
| onHTMLFileCache__useFileCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onLoginFormValidErrorMessages | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| onMakeGlobalVariablesScript | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| hasEditNoticesFeatureConflict | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| onTitleSquidURLs | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| onAPIQuerySiteInfoGeneralInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Actions\ActionEntryPoint; |
| 4 | use MediaWiki\Api\ApiMain; |
| 5 | use MediaWiki\Api\ApiQuerySiteinfo; |
| 6 | use MediaWiki\Api\Hook\APIQuerySiteInfoGeneralInfoHook; |
| 7 | use MediaWiki\Cache\Hook\HTMLFileCache__useFileCacheHook; |
| 8 | use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook; |
| 9 | use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook; |
| 10 | use MediaWiki\ChangeTags\Taggable; |
| 11 | use MediaWiki\Config\Config; |
| 12 | use MediaWiki\Context\Hook\RequestContextCreateSkinHook; |
| 13 | use MediaWiki\Context\IContextSource; |
| 14 | use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 15 | use MediaWiki\Extension\Gadgets\GadgetRepo; |
| 16 | use MediaWiki\Hook\ApiBeforeMainHook; |
| 17 | use MediaWiki\Hook\MediaWikiPerformActionHook; |
| 18 | use MediaWiki\Hook\TitleSquidURLsHook; |
| 19 | use MediaWiki\HookContainer\HookContainer; |
| 20 | use MediaWiki\Html\Html; |
| 21 | use MediaWiki\Logging\Hook\ManualLogEntryBeforePublishHook; |
| 22 | use MediaWiki\Logging\ManualLogEntry; |
| 23 | use MediaWiki\MainConfigNames; |
| 24 | use MediaWiki\MediaWikiServices; |
| 25 | use MediaWiki\Output\Hook\AfterBuildFeedLinksHook; |
| 26 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 27 | use MediaWiki\Output\Hook\BeforePageRedirectHook; |
| 28 | use MediaWiki\Output\Hook\GetCacheVaryCookiesHook; |
| 29 | use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook; |
| 30 | use MediaWiki\Output\Hook\OutputPageBeforeHTMLHook; |
| 31 | use MediaWiki\Output\Hook\OutputPageBodyAttributesHook; |
| 32 | use MediaWiki\Output\Hook\OutputPageParserOutputHook; |
| 33 | use MediaWiki\Output\OutputPage; |
| 34 | use MediaWiki\Page\Article; |
| 35 | use MediaWiki\Page\Hook\ArticleParserOptionsHook; |
| 36 | use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook; |
| 37 | use MediaWiki\Parser\ParserOptions; |
| 38 | use MediaWiki\Parser\ParserOutput; |
| 39 | use MediaWiki\Preferences\Hook\GetPreferencesHook; |
| 40 | use MediaWiki\RecentChanges\Hook\RecentChange_saveHook; |
| 41 | use MediaWiki\RecentChanges\RecentChange; |
| 42 | use MediaWiki\Registration\ExtensionRegistry; |
| 43 | use MediaWiki\Request\WebRequest; |
| 44 | use MediaWiki\ResourceLoader as RL; |
| 45 | use MediaWiki\ResourceLoader\Hook\ResourceLoaderBeforeResponseHook; |
| 46 | use MediaWiki\ResourceLoader\Hook\ResourceLoaderSiteModulePagesHook; |
| 47 | use MediaWiki\ResourceLoader\Hook\ResourceLoaderSiteStylesModulePagesHook; |
| 48 | use MediaWiki\ResourceLoader\ResourceLoader; |
| 49 | use MediaWiki\Skin\Hook\SkinAddFooterLinksHook; |
| 50 | use MediaWiki\Skin\Skin; |
| 51 | use MediaWiki\Skin\SkinException; |
| 52 | use MediaWiki\Skin\SkinFactory; |
| 53 | use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook; |
| 54 | use MediaWiki\SpecialPage\SpecialPage; |
| 55 | use MediaWiki\Specials\Hook\LoginFormValidErrorMessagesHook; |
| 56 | use MediaWiki\Title\Title; |
| 57 | use MediaWiki\User\Options\UserOptionsLookup; |
| 58 | use MediaWiki\User\User; |
| 59 | use MediaWiki\Utils\UrlUtils; |
| 60 | use MobileFrontend\Api\ApiParseExtender; |
| 61 | use MobileFrontend\ContentProviders\DefaultContentProvider; |
| 62 | use MobileFrontend\Features\FeaturesManager; |
| 63 | use MobileFrontend\Hooks\HookRunner; |
| 64 | use MobileFrontend\Transforms\MakeSectionsTransform; |
| 65 | |
| 66 | /** |
| 67 | * Hook handlers for MobileFrontend extension |
| 68 | * |
| 69 | * If your hook changes the behaviour of the Minerva skin, you are in the wrong place. |
| 70 | * Any changes relating to Minerva should go into Minerva.hooks.php |
| 71 | */ |
| 72 | class MobileFrontendHooks implements |
| 73 | APIQuerySiteInfoGeneralInfoHook, |
| 74 | ApiBeforeMainHook, |
| 75 | RequestContextCreateSkinHook, |
| 76 | BeforeDisplayNoArticleTextHook, |
| 77 | OutputPageBeforeHTMLHook, |
| 78 | OutputPageBodyAttributesHook, |
| 79 | ResourceLoaderBeforeResponseHook, |
| 80 | ResourceLoaderSiteStylesModulePagesHook, |
| 81 | ResourceLoaderSiteModulePagesHook, |
| 82 | SkinAddFooterLinksHook, |
| 83 | BeforePageRedirectHook, |
| 84 | MediaWikiPerformActionHook, |
| 85 | GetCacheVaryCookiesHook, |
| 86 | ListDefinedTagsHook, |
| 87 | ChangeTagsListActiveHook, |
| 88 | RecentChange_saveHook, |
| 89 | SpecialPageBeforeExecuteHook, |
| 90 | BeforePageDisplayHook, |
| 91 | GetPreferencesHook, |
| 92 | OutputPageParserOutputHook, |
| 93 | ArticleParserOptionsHook, |
| 94 | HTMLFileCache__useFileCacheHook, |
| 95 | LoginFormValidErrorMessagesHook, |
| 96 | AfterBuildFeedLinksHook, |
| 97 | MakeGlobalVariablesScriptHook, |
| 98 | TitleSquidURLsHook, |
| 99 | ManualLogEntryBeforePublishHook |
| 100 | { |
| 101 | public const MOBILE_PREFERENCES_EDITOR = 'mobile-editor'; |
| 102 | public const MOBILE_PREFERENCES_FONTSIZE = 'mf-font-size'; |
| 103 | public const MOBILE_PREFERENCES_EXPAND_SECTIONS = 'mf-expand-sections'; |
| 104 | // This should always be kept in sync with Codex `@min-width-breakpoint-tablet` |
| 105 | // in mediawiki.skin.variables.less and Browser in mobile.startup module. |
| 106 | private const DEVICE_WIDTH_TABLET = '640px'; |
| 107 | |
| 108 | private readonly MobileFrontendSkinHooks $skinHooks; |
| 109 | |
| 110 | public function __construct( |
| 111 | private readonly HookContainer $hookContainer, |
| 112 | private readonly Config $config, |
| 113 | private readonly SkinFactory $skinFactory, |
| 114 | UrlUtils $urlUtils, |
| 115 | private readonly UserOptionsLookup $userOptionsLookup, |
| 116 | private readonly MobileContext $mobileContext, |
| 117 | private readonly FeaturesManager $featuresManager, |
| 118 | private readonly ?GadgetRepo $gadgetRepo, |
| 119 | ) { |
| 120 | $this->skinHooks = new MobileFrontendSkinHooks( $urlUtils ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Obtain the default mobile skin |
| 125 | * |
| 126 | * @throws SkinException If a factory function isn't registered for the skin name |
| 127 | * @return Skin |
| 128 | */ |
| 129 | protected function getDefaultMobileSkin(): Skin { |
| 130 | $defaultSkin = $this->config->get( 'DefaultMobileSkin' ) ?: |
| 131 | $this->config->get( MainConfigNames::DefaultSkin ); |
| 132 | return $this->skinFactory->makeSkin( Skin::normalizeKey( $defaultSkin ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * RequestContextCreateSkin hook handler |
| 137 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/RequestContextCreateSkin |
| 138 | * |
| 139 | * @param IContextSource $context The RequestContext object the skin is being created for. |
| 140 | * @param Skin|null|string &$skin A variable reference you may set a Skin instance or string |
| 141 | * key on to override the skin that will be used for the context. |
| 142 | * @return bool |
| 143 | */ |
| 144 | public function onRequestContextCreateSkin( $context, &$skin ) { |
| 145 | $mobileContext = $this->mobileContext; |
| 146 | |
| 147 | // If mobileaction is set, save toggling cookie and do a redirect |
| 148 | $mobileContext->doToggling(); |
| 149 | |
| 150 | if ( !$mobileContext->shouldDisplayMobileView() ) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | // Handle any X-Analytics header values in the request by adding them |
| 155 | // as log items. X-Analytics header values are serialized key=value |
| 156 | // pairs, separated by ';', used for analytics purposes. |
| 157 | $xanalytics = $mobileContext->getRequest()->getHeader( 'X-Analytics' ); |
| 158 | if ( $xanalytics ) { |
| 159 | $xanalytics_arr = explode( ';', $xanalytics ); |
| 160 | if ( count( $xanalytics_arr ) > 1 ) { |
| 161 | foreach ( $xanalytics_arr as $xanalytics_item ) { |
| 162 | $mobileContext->addAnalyticsLogItemFromXAnalytics( $xanalytics_item ); |
| 163 | } |
| 164 | } else { |
| 165 | $mobileContext->addAnalyticsLogItemFromXAnalytics( $xanalytics ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // log whether user is using AMC mode |
| 170 | $mobileContext->logMobileMode(); |
| 171 | |
| 172 | // Allow overriding of skin by useskin e.g. useskin=vector&useformat=mobile or by |
| 173 | // setting the mobileskin preferences (api only currently) |
| 174 | $userSkin = $context->getRequest()->getRawVal( 'useskin' ) ?? |
| 175 | $this->userOptionsLookup->getOption( |
| 176 | $context->getUser(), 'mobileskin' |
| 177 | ); |
| 178 | if ( $userSkin && Skin::normalizeKey( $userSkin ) === $userSkin ) { |
| 179 | $skin = $this->skinFactory->makeSkin( $userSkin ); |
| 180 | } else { |
| 181 | $skin = $this->getDefaultMobileSkin(); |
| 182 | } |
| 183 | |
| 184 | $hookRunner = new HookRunner( $this->hookContainer ); |
| 185 | $hookRunner->onRequestContextCreateSkinMobile( $mobileContext, $skin ); |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Update the footer |
| 192 | * @param Skin $skin |
| 193 | * @param string $key the current key for the current group (row) of footer links. |
| 194 | * e.g. `info` or `places`. |
| 195 | * @param array &$footerLinks an empty array that can be populated with new links. |
| 196 | * keys should be strings and will be used for generating the ID of the footer item |
| 197 | * and value should be an HTML string. |
| 198 | */ |
| 199 | public function onSkinAddFooterLinks( Skin $skin, string $key, array &$footerLinks ) { |
| 200 | $context = $this->mobileContext; |
| 201 | if ( $key === 'places' ) { |
| 202 | if ( $context->shouldDisplayMobileView() ) { |
| 203 | $terms = $this->skinHooks->getTermsLink( $skin ); |
| 204 | if ( $terms ) { |
| 205 | $footerLinks['terms-use'] = $terms; |
| 206 | } |
| 207 | $footerLinks['desktop-toggle'] = $this->skinHooks->getDesktopViewLink( $skin, $context ); |
| 208 | } else { |
| 209 | // If desktop site append a mobile view link |
| 210 | $footerLinks['mobileview'] = |
| 211 | $this->skinHooks->getMobileViewLink( $skin, $context ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * BeforeDisplayNoArticleText hook handler |
| 218 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforeDisplayNoArticleText |
| 219 | * |
| 220 | * @param Article $article The (empty) article |
| 221 | * @return bool This hook can abort |
| 222 | */ |
| 223 | public function onBeforeDisplayNoArticleText( $article ) { |
| 224 | $displayMobileView = $this->mobileContext->shouldDisplayMobileView(); |
| 225 | |
| 226 | $title = $article->getTitle(); |
| 227 | |
| 228 | // if the page is a userpage |
| 229 | // @todo: Upstream to core (T248347). |
| 230 | if ( $displayMobileView && |
| 231 | $title->inNamespaces( NS_USER ) && |
| 232 | !$title->isSubpage() |
| 233 | ) { |
| 234 | $out = $article->getContext()->getOutput(); |
| 235 | $userpagetext = ExtMobileFrontend::blankUserPageHTML( $out, $title ); |
| 236 | if ( $userpagetext ) { |
| 237 | // Replace the default message with ours |
| 238 | $out->addHTML( $userpagetext ); |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * OutputPageBeforeHTML hook handler |
| 248 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageBeforeHTML |
| 249 | * |
| 250 | * Applies MobileFormatter to mobile viewed content |
| 251 | * |
| 252 | * @param OutputPage $out the OutputPage object to which wikitext is added |
| 253 | * @param string &$text the HTML to be wrapped inside the #mw-content-text element |
| 254 | */ |
| 255 | public function onOutputPageBeforeHTML( $out, &$text ) { |
| 256 | // This hook can be executed more than once per page view if the page content is composed from |
| 257 | // multiple sources! Anything that doesn't depend on $text should use onBeforePageDisplay. |
| 258 | |
| 259 | $context = $this->mobileContext; |
| 260 | $title = $out->getTitle(); |
| 261 | $displayMobileView = $context->shouldDisplayMobileView(); |
| 262 | |
| 263 | if ( !$title ) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | $options = $this->config->get( 'MFMobileFormatterOptions' ); |
| 268 | $excludeNamespaces = $options['excludeNamespaces'] ?? []; |
| 269 | // Perform a few extra changes if we are in mobile mode |
| 270 | $namespaceAllowed = !$title->inNamespaces( $excludeNamespaces ); |
| 271 | |
| 272 | $provider = new DefaultContentProvider( $text ); |
| 273 | $originalProviderClass = DefaultContentProvider::class; |
| 274 | ( new HookRunner( $this->hookContainer ) )->onMobileFrontendContentProvider( |
| 275 | $provider, $out |
| 276 | ); |
| 277 | |
| 278 | $isParse = ApiParseExtender::isParseAction( |
| 279 | $context->getRequest()->getText( 'action' ) |
| 280 | ); |
| 281 | |
| 282 | if ( get_class( $provider ) === $originalProviderClass ) { |
| 283 | // This line is important to avoid the default content provider running unnecessarily |
| 284 | // on desktop views. |
| 285 | $useContentProvider = $displayMobileView; |
| 286 | $runMobileFormatter = $displayMobileView && ( |
| 287 | // T245160 - don't run the mobile formatter on old revisions. |
| 288 | // Note if not the default content provider we ignore this requirement. |
| 289 | $title->getLatestRevID() > 0 || |
| 290 | // Always allow the formatter in ApiParse |
| 291 | $isParse |
| 292 | ); |
| 293 | } else { |
| 294 | // When a custom content provider is enabled, always use it. |
| 295 | $useContentProvider = true; |
| 296 | $runMobileFormatter = $displayMobileView; |
| 297 | } |
| 298 | |
| 299 | if ( $namespaceAllowed && $useContentProvider ) { |
| 300 | $text = ExtMobileFrontend::domParseWithContentProvider( |
| 301 | $provider, $out, $runMobileFormatter |
| 302 | ); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Modifies the `<body>` element's attributes. |
| 308 | * |
| 309 | * By default, the `class` attribute is set to the output's "bodyClassName" |
| 310 | * property. |
| 311 | * |
| 312 | * @param OutputPage $out |
| 313 | * @param Skin $skin |
| 314 | * @param string[] &$bodyAttrs |
| 315 | */ |
| 316 | public function onOutputPageBodyAttributes( $out, $skin, &$bodyAttrs ): void { |
| 317 | /** @var \MobileFrontend\Amc\UserMode $userMode */ |
| 318 | $userMode = MediaWikiServices::getInstance()->getService( 'MobileFrontend.AMC.UserMode' ); |
| 319 | $isMobile = $this->mobileContext->shouldDisplayMobileView(); |
| 320 | |
| 321 | // FIXME: This can be removed when existing references have been updated. |
| 322 | if ( $isMobile && !$userMode->isEnabled() ) { |
| 323 | $bodyAttrs['class'] .= ' mw-mf-amc-disabled'; |
| 324 | } |
| 325 | |
| 326 | if ( $isMobile ) { |
| 327 | // Add a class to the body so that TemplateStyles (which can only |
| 328 | // access html and body) and gadgets have something to check for. |
| 329 | // @stable added in 1.38 |
| 330 | $bodyAttrs['class'] .= ' mw-mf'; |
| 331 | |
| 332 | // Add a class to the body to indicate whether collapsible sections |
| 333 | // are enabled on this namespace page. |
| 334 | // @stable added in 1.46 |
| 335 | $title = $out->getTitle(); |
| 336 | if ( $title ) { |
| 337 | $namespace = $title->getNamespace(); |
| 338 | $namespacesWithoutCollapsibleSections = $this->config->get( 'MFNamespacesWithoutCollapsibleSections' ); |
| 339 | $isCollapsibleSections = !in_array( $namespace, $namespacesWithoutCollapsibleSections ); |
| 340 | |
| 341 | if ( $isCollapsibleSections ) { |
| 342 | $bodyAttrs['class'] .= ' mf-collapsible-sections'; |
| 343 | } else { |
| 344 | $bodyAttrs['class'] .= ' mf-collapsible-sections-disabled'; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * BeforePageRedirect hook handler |
| 352 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageRedirect |
| 353 | * |
| 354 | * Ensures URLs are handled properly for select special pages. |
| 355 | * @param OutputPage $out |
| 356 | * @param string &$redirect URL string, modifiable |
| 357 | * @param string &$code HTTP code (eg '301' or '302'), modifiable |
| 358 | */ |
| 359 | public function onBeforePageRedirect( $out, &$redirect, &$code ) { |
| 360 | $shouldDisplayMobileView = $this->mobileContext->shouldDisplayMobileView(); |
| 361 | if ( !$shouldDisplayMobileView ) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // T45123: force mobile URLs only for local redirects |
| 366 | if ( $this->mobileContext->isLocalUrl( $redirect ) ) { |
| 367 | $redirect = $this->mobileContext->getMobileUrl( $redirect ); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * This hook is called early on api.php requests. |
| 373 | * |
| 374 | * @param ApiMain &$main |
| 375 | * @return void |
| 376 | */ |
| 377 | public function onApiBeforeMain( &$main ) { |
| 378 | // T390929: api.php varies on MobileContext::shouldDisplayMobileView(), |
| 379 | // based on skin involvement (e.g. api.php?action=parse&useskin), |
| 380 | // and various extension hooks (e.g. URLs returned by query modules). |
| 381 | // |
| 382 | // NOTE: This OutputPage object is discarded and replaced for ApiHelp responses, |
| 383 | // such as the /w/api.php landing page. This is fine because those are personalised |
| 384 | // and uncachable (Cache-Control: private). |
| 385 | $mobileHeader = $this->config->get( 'MFMobileHeader' ); |
| 386 | if ( $mobileHeader ) { |
| 387 | $main->getOutput()->addVaryHeader( $mobileHeader ); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * This hook is called early on load.php requests. |
| 393 | * |
| 394 | * @param RL\Context $context |
| 395 | * @param string[] &$extraHeaders |
| 396 | */ |
| 397 | public function onResourceLoaderBeforeResponse( RL\Context $context, array &$extraHeaders ): void { |
| 398 | // T390929: load.php varies when $wgMFCustomSiteModules is enabled, |
| 399 | // through the onResourceLoaderSiteStylesModulePages and onResourceLoaderSiteModulePages |
| 400 | // hooks in this file. |
| 401 | global $wgMFMobileHeader, $wgMFCustomSiteModules; |
| 402 | if ( $wgMFMobileHeader |
| 403 | && $wgMFCustomSiteModules |
| 404 | && array_intersect( $context->getModules(), [ 'startup', 'site', 'site.styles' ] ) |
| 405 | ) { |
| 406 | $extraHeaders[] = "Vary: $wgMFMobileHeader"; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * This hook is called early on index.php requests. |
| 412 | * |
| 413 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/MediaWikiPerformActionHook |
| 414 | * |
| 415 | * |
| 416 | * @param OutputPage $output Context output |
| 417 | * @param Article $article Article on which the action will be performed |
| 418 | * @param Title $title Title on which the action will be performed |
| 419 | * @param User $user Context user |
| 420 | * @param WebRequest $request Context request |
| 421 | * @param ActionEntryPoint $entryPoint |
| 422 | * @return void |
| 423 | */ |
| 424 | public function onMediaWikiPerformAction( $output, $article, $title, $user, |
| 425 | $request, $entryPoint |
| 426 | ) { |
| 427 | // T390929: index.php varies on MobileContext::shouldDisplayMobileView, |
| 428 | // especially in onRequestContextCreateSkin and onBeforePageRedirect. |
| 429 | $mobileHeader = $this->config->get( 'MFMobileHeader' ); |
| 430 | if ( $mobileHeader ) { |
| 431 | $output->addVaryHeader( $mobileHeader ); |
| 432 | } |
| 433 | |
| 434 | // Set Diff page to diff-only mode for mobile view |
| 435 | // this code should only apply to mobile view. |
| 436 | if ( $this->mobileContext->shouldDisplayMobileView() ) { |
| 437 | // Default to diff-only mode on mobile diff pages if not specified. |
| 438 | if ( $request->getCheck( 'diff' ) && !$request->getCheck( 'diffonly' ) ) { |
| 439 | $request->setVal( 'diffonly', 'true' ); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * ResourceLoaderSiteStylesModulePages hook handler |
| 446 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderSiteStylesModulePages |
| 447 | * |
| 448 | * @param string $skin |
| 449 | * @param array &$pages to sort modules from. |
| 450 | */ |
| 451 | public function onResourceLoaderSiteStylesModulePages( $skin, array &$pages ): void { |
| 452 | $ctx = $this->mobileContext; |
| 453 | // Use Mobile.css instead of MediaWiki:Common.css on mobile views. |
| 454 | if ( $ctx->shouldDisplayMobileView() && $this->config->get( 'MFCustomSiteModules' ) ) { |
| 455 | unset( $pages['MediaWiki:Common.css'] ); |
| 456 | unset( $pages['MediaWiki:Print.css'] ); |
| 457 | if ( $this->config->get( 'MFSiteStylesRenderBlocking' ) ) { |
| 458 | $pages['MediaWiki:Mobile.css'] = [ 'type' => 'style' ]; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * ResourceLoaderSiteModulePages hook handler |
| 465 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderSiteModulePages |
| 466 | * |
| 467 | * @param string $skin |
| 468 | * @param array &$pages to sort modules from. |
| 469 | */ |
| 470 | public function onResourceLoaderSiteModulePages( $skin, array &$pages ): void { |
| 471 | $ctx = $this->mobileContext; |
| 472 | // Use Mobile.js instead of MediaWiki:Common.js and MediaWiki:<skinname.js> on mobile views. |
| 473 | if ( $ctx->shouldDisplayMobileView() && $this->config->get( 'MFCustomSiteModules' ) ) { |
| 474 | unset( $pages['MediaWiki:Common.js'] ); |
| 475 | $pages['MediaWiki:Mobile.js'] = [ 'type' => 'script' ]; |
| 476 | if ( !$this->config->get( 'MFSiteStylesRenderBlocking' ) ) { |
| 477 | $pages['MediaWiki:Mobile.css'] = [ 'type' => 'style' ]; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * GetCacheVaryCookies hook handler |
| 484 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetCacheVaryCookies |
| 485 | * |
| 486 | * @param OutputPage $out |
| 487 | * @param array &$cookies array of cookies name, add a value to it |
| 488 | * if you want to add a cookie that have to vary cache options |
| 489 | */ |
| 490 | public function onGetCacheVaryCookies( $out, &$cookies ) { |
| 491 | // Enables mobile cookies on wikis w/o mobile domain |
| 492 | $cookies[] = MobileContext::USEFORMAT_COOKIE_NAME; |
| 493 | // Don't redirect to mobile if user had explicitly opted out of it |
| 494 | $cookies[] = MobileContext::STOP_MOBILE_REDIRECT_COOKIE_NAME; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * ListDefinedTags hook handler |
| 499 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ListDefinedTags |
| 500 | * |
| 501 | * @param array &$tags The list of tags. Add your extension's tags to this array. |
| 502 | */ |
| 503 | public function onListDefinedTags( &$tags ) { |
| 504 | $this->addDefinedTags( $tags ); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * ChangeTagsListActive hook handler |
| 509 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ChangeTagsListActive |
| 510 | * |
| 511 | * @param array &$tags The list of tags. Add your extension's tags to this array. |
| 512 | */ |
| 513 | public function onChangeTagsListActive( &$tags ) { |
| 514 | $this->addDefinedTags( $tags ); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @param array &$tags |
| 519 | */ |
| 520 | public function addDefinedTags( &$tags ) { |
| 521 | $tags[] = 'mobile edit'; |
| 522 | $tags[] = 'mobile web edit'; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * RecentChange_save hook handler that tags mobile changes |
| 527 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/RecentChange_save |
| 528 | * |
| 529 | * @param RecentChange $recentChange |
| 530 | */ |
| 531 | public function onRecentChange_save( $recentChange ) { |
| 532 | self::onTaggableObjectCreation( $recentChange ); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * ManualLogEntryBeforePublish hook handler that tags actions logged when user uses mobile mode |
| 537 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ManualLogEntryBeforePublish |
| 538 | * |
| 539 | * @param ManualLogEntry $logEntry |
| 540 | */ |
| 541 | public function onManualLogEntryBeforePublish( $logEntry ): void { |
| 542 | self::onTaggableObjectCreation( $logEntry ); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @param Taggable $taggable Object to tag |
| 547 | */ |
| 548 | public static function onTaggableObjectCreation( Taggable $taggable ) { |
| 549 | /** @var MobileContext $context */ |
| 550 | $context = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' ); |
| 551 | $userAgent = $context->getRequest()->getHeader( "User-agent" ); |
| 552 | if ( $context->shouldDisplayMobileView() ) { |
| 553 | $taggable->addTags( [ 'mobile edit' ] ); |
| 554 | // Tag as mobile web edit specifically, if it isn't coming from the apps |
| 555 | if ( strpos( $userAgent, 'WikipediaApp/' ) !== 0 ) { |
| 556 | $taggable->addTags( [ 'mobile web edit' ] ); |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * AbuseFilter-generateUserVars hook handler that adds a user_mobile variable. |
| 563 | * Altering the variables generated for a specific user |
| 564 | * |
| 565 | * @see hooks.txt in AbuseFilter extension |
| 566 | * @param VariableHolder $vars object to add vars to |
| 567 | * @param User $user |
| 568 | * @param RecentChange|null $rc If the variables should be generated for an RC entry, this |
| 569 | * is the entry. Null if it's for the current action being filtered. |
| 570 | */ |
| 571 | public static function onAbuseFilterGenerateUserVars( $vars, $user, ?RecentChange $rc = null ) { |
| 572 | $services = MediaWikiServices::getInstance(); |
| 573 | |
| 574 | if ( !$rc ) { |
| 575 | /** @var MobileContext $context */ |
| 576 | $context = $services->getService( 'MobileFrontend.Context' ); |
| 577 | $vars->setVar( 'user_mobile', $context->shouldDisplayMobileView() ); |
| 578 | } else { |
| 579 | |
| 580 | $dbr = $services->getConnectionProvider()->getReplicaDatabase(); |
| 581 | |
| 582 | $tags = $services->getChangeTagsStore()->getTags( $dbr, $rc->getAttribute( 'rc_id' ) ); |
| 583 | $val = (bool)array_intersect( $tags, [ 'mobile edit', 'mobile web edit' ] ); |
| 584 | $vars->setVar( 'user_mobile', $val ); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * AbuseFilter-builder hook handler that adds user_mobile variable to list |
| 590 | * of valid vars |
| 591 | * |
| 592 | * @param array &$builder Array in AbuseFilter::getBuilderValues to add to. |
| 593 | */ |
| 594 | public static function onAbuseFilterBuilder( &$builder ) { |
| 595 | $builder['vars']['user_mobile'] = 'user-mobile'; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Invocation of hook SpecialPageBeforeExecute |
| 600 | * |
| 601 | * We use this hook to ensure that login/account creation pages |
| 602 | * are redirected to HTTPS if they are not accessed via HTTPS and |
| 603 | * $wgSecureLogin == true - but only when using the |
| 604 | * mobile site. |
| 605 | * |
| 606 | * @param SpecialPage $special |
| 607 | * @param string $subpage subpage name |
| 608 | */ |
| 609 | public function onSpecialPageBeforeExecute( $special, $subpage ) { |
| 610 | $isMobileView = $this->mobileContext->shouldDisplayMobileView(); |
| 611 | $name = $special->getName(); |
| 612 | |
| 613 | if ( $isMobileView ) { |
| 614 | $out = $special->getOutput(); |
| 615 | // FIXME: mobile.special.styles should be replaced with mediawiki.special module |
| 616 | $out->addModuleStyles( |
| 617 | [ 'mobile.special.styles' ] |
| 618 | ); |
| 619 | // FIXME: Should be moved to MediaWiki core module. |
| 620 | if ( $name === 'Userlogin' || $name === 'CreateAccount' ) { |
| 621 | $out->addModules( 'mobile.special.userlogin.scripts' ); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Check whether the sections should be expanded by default or collapsed. |
| 628 | * |
| 629 | * @internal |
| 630 | * @param int $ns |
| 631 | * @param Wikimedia\Parsoid\Core\SectionMetadata[] $sections |
| 632 | * @return bool |
| 633 | */ |
| 634 | private static function shouldCollapseSectionsByDefault( $ns, $sections = [] ): bool { |
| 635 | $numSections = $sections ? count( |
| 636 | array_filter( $sections, static function ( $item ) { |
| 637 | return $item->tocLevel === 1; |
| 638 | } ) |
| 639 | ) : 0; |
| 640 | |
| 641 | // Only enable section collapsing/expanding on pages with more than |
| 642 | // one section (T435668) |
| 643 | // The namespace check is necessary to ensure that Special:MobileOptions |
| 644 | // has the class, since it relies on this for rendering the setting |
| 645 | return $numSections > 1 || $ns === NS_SPECIAL; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * BeforePageDisplay hook handler |
| 650 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay |
| 651 | * |
| 652 | * @param OutputPage $out |
| 653 | * @param Skin $skin Skin object that will be used to generate the page, added in 1.13. |
| 654 | */ |
| 655 | public function onBeforePageDisplay( $out, $skin ): void { |
| 656 | $context = $this->mobileContext; |
| 657 | $mfEnableXAnalyticsLogging = $this->config->get( 'MFEnableXAnalyticsLogging' ); |
| 658 | $mfNoIndexPages = $this->config->get( 'MFNoindexPages' ); |
| 659 | $isCanonicalLinkHandledByCore = $this->config->get( MainConfigNames::EnableCanonicalServerLink ); |
| 660 | $hasMobileUrl = $context->hasMobileDomain(); |
| 661 | $displayMobileView = $context->shouldDisplayMobileView(); |
| 662 | |
| 663 | $title = $skin->getTitle(); |
| 664 | |
| 665 | // an canonical/alternate link is only useful, if the mobile and desktop URL are different |
| 666 | // and $wgMFNoindexPages needs to be true |
| 667 | if ( $hasMobileUrl && $mfNoIndexPages ) { |
| 668 | $link = false; |
| 669 | |
| 670 | if ( !$displayMobileView ) { |
| 671 | // add alternate link to desktop sites - bug T91183 |
| 672 | $desktopUrl = $title->getFullURL(); |
| 673 | $link = [ |
| 674 | 'rel' => 'alternate', |
| 675 | 'media' => 'only screen and (max-width: ' . self::DEVICE_WIDTH_TABLET . ')', |
| 676 | 'href' => $context->getMobileUrl( $desktopUrl ), |
| 677 | ]; |
| 678 | } elseif ( !$isCanonicalLinkHandledByCore ) { |
| 679 | $link = [ |
| 680 | 'rel' => 'canonical', |
| 681 | 'href' => $title->getFullURL(), |
| 682 | ]; |
| 683 | } |
| 684 | |
| 685 | if ( $link ) { |
| 686 | $out->addLink( $link ); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // set the vary header to User-Agent, if mobile frontend auto detects, if the mobile |
| 691 | // view should be delivered and the same url is used for desktop and mobile devices |
| 692 | // Bug: T123189 |
| 693 | if ( |
| 694 | $this->config->get( 'MFVaryOnUA' ) && |
| 695 | $this->config->get( 'MFAutodetectMobileView' ) && |
| 696 | !$hasMobileUrl |
| 697 | ) { |
| 698 | $out->addVaryHeader( 'User-Agent' ); |
| 699 | } |
| 700 | |
| 701 | // Set X-Analytics HTTP response header if necessary |
| 702 | if ( $displayMobileView ) { |
| 703 | $analyticsHeader = ( $mfEnableXAnalyticsLogging ? $context->getXAnalyticsHeader() : false ); |
| 704 | if ( $analyticsHeader ) { |
| 705 | $resp = $out->getRequest()->response(); |
| 706 | $resp->header( $analyticsHeader ); |
| 707 | } |
| 708 | |
| 709 | // in mobile view: always add vary header |
| 710 | $out->addVaryHeader( 'Cookie' ); |
| 711 | |
| 712 | $out->addLink( |
| 713 | [ |
| 714 | 'rel' => 'manifest', |
| 715 | 'href' => wfAppendQuery( |
| 716 | wfScript( 'api' ), |
| 717 | [ 'action' => 'webapp-manifest' ] |
| 718 | ) |
| 719 | ] |
| 720 | ); |
| 721 | |
| 722 | $out->addModules( [ 'mobile.init' ] ); |
| 723 | $out->addModuleStyles( [ 'mobile.init.styles' ] ); |
| 724 | |
| 725 | $fontSize = $this->userOptionsLookup->getOption( |
| 726 | $context->getUser(), self::MOBILE_PREFERENCES_FONTSIZE |
| 727 | ) ?? 'small'; |
| 728 | $expandSections = $this->userOptionsLookup->getOption( |
| 729 | $context->getUser(), self::MOBILE_PREFERENCES_EXPAND_SECTIONS |
| 730 | ) ? '1' : '0'; |
| 731 | |
| 732 | /** @var \MobileFrontend\Amc\UserMode $userMode */ |
| 733 | $userMode = MediaWikiServices::getInstance()->getService( 'MobileFrontend.AMC.UserMode' ); |
| 734 | $amc = !$userMode->isEnabled() ? '0' : '1'; |
| 735 | $context->getOutput()->addHtmlClasses( [ |
| 736 | 'mf-font-size-clientpref-' . $fontSize, |
| 737 | 'mw-mf-amc-clientpref-' . $amc |
| 738 | ] ); |
| 739 | $tocData = $out->getTOCData(); |
| 740 | if ( self::shouldCollapseSectionsByDefault( |
| 741 | $title->getNamespace(), |
| 742 | $tocData ? $tocData->getSections() : [] ) |
| 743 | ) { |
| 744 | $out->addHtmlClasses( |
| 745 | 'mf-expand-sections-clientpref-' . $expandSections |
| 746 | ); |
| 747 | } else { |
| 748 | // given mf-expand-sections-clientpref-0 is omitted |
| 749 | // avoid a flash of unstyled content. |
| 750 | $out->addHtmlClasses( 'collapsible-headings-expanded' ); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // T204691 |
| 755 | $theme = $this->config->get( 'MFManifestThemeColor' ); |
| 756 | if ( $theme && $displayMobileView ) { |
| 757 | $out->addMeta( 'theme-color', $theme ); |
| 758 | } |
| 759 | |
| 760 | if ( $displayMobileView ) { |
| 761 | // Adds inline script to allow opening of sections while JS is still loading |
| 762 | if ( $this->shouldUseParsoid( $out->getTitle() ) ) { |
| 763 | // Append so this runs after any copy embedded in HTML from MobileFrontendContentProvider. |
| 764 | $out->addHTML( self::interimTogglingSupportForParsoid() ); |
| 765 | } else { |
| 766 | $out->prependHTML( MakeSectionsTransform::interimTogglingSupport() ); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // For Account Creation CTA experiment. See T425372. |
| 771 | // Add a JS config var rather than loading the ext.testKitchen module on the client side to |
| 772 | // avoid introducing async behavior to the CtaDrawer. |
| 773 | if ( ExtensionRegistry::getInstance()->isLoaded( 'TestKitchen' ) ) { |
| 774 | $experimentManager = MediaWikiServices::getInstance()->getService( 'TestKitchen.ExperimentManager' ); |
| 775 | $experiment = $experimentManager->getExperiment( 'account-creation-reading-list-cta' ); |
| 776 | if ( $experiment && $experiment->isAssignedGroup( 'control' ) ) { |
| 777 | $out->addJsConfigVars( [ 'wgMFInAccountCreationCTAControl' => true ] ); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | private static function interimTogglingSupportForParsoid(): string { |
| 783 | $js = file_get_contents( __DIR__ . '/../resources/inline/interim-toggling-parsoid.js' ); |
| 784 | return Html::inlineScript( |
| 785 | ResourceLoader::filter( 'minify-js', $js ) |
| 786 | ); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * AfterBuildFeedLinks hook handler. Remove all feed links in mobile view. |
| 791 | * |
| 792 | * @param array &$tags Added feed links |
| 793 | */ |
| 794 | public function onAfterBuildFeedLinks( &$tags ) { |
| 795 | if ( $this->mobileContext->shouldDisplayMobileView() ) { |
| 796 | $tags = []; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * GetPreferences hook handler |
| 802 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences |
| 803 | * |
| 804 | * @param User $user User whose preferences are being modified |
| 805 | * @param array &$preferences Preferences description array, to be fed to an HTMLForm object |
| 806 | */ |
| 807 | public function onGetPreferences( $user, &$preferences ) { |
| 808 | $definition = [ |
| 809 | 'type' => 'api', |
| 810 | 'default' => '', |
| 811 | ]; |
| 812 | $preferences[self::MOBILE_PREFERENCES_EDITOR] = $definition; |
| 813 | $preferences[self::MOBILE_PREFERENCES_FONTSIZE] = $definition; |
| 814 | $preferences[self::MOBILE_PREFERENCES_EXPAND_SECTIONS] = $definition; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * Sets a tagline for a given page that can be displayed by the skin. |
| 819 | * |
| 820 | * @param OutputPage $outputPage |
| 821 | * @param string $desc |
| 822 | */ |
| 823 | private static function setTagline( OutputPage $outputPage, $desc ) { |
| 824 | $outputPage->setProperty( 'wgMFDescription', $desc ); |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Finds the wikidata tagline associated with the page |
| 829 | * |
| 830 | * @param ParserOutput $po |
| 831 | * @param callable $fallbackWikibaseDescriptionFunc A fallback to provide Wikibase description. |
| 832 | * Function takes wikibase_item as a first and only argument |
| 833 | * @return ?string the tagline as a string, or else null if none is found |
| 834 | */ |
| 835 | public static function findTagline( ParserOutput $po, $fallbackWikibaseDescriptionFunc ) { |
| 836 | $desc = $po->getPageProperty( 'wikibase-shortdesc' ); |
| 837 | $item = $po->getPageProperty( 'wikibase_item' ); |
| 838 | if ( $desc === null && $item && $fallbackWikibaseDescriptionFunc ) { |
| 839 | return $fallbackWikibaseDescriptionFunc( $item ); |
| 840 | } |
| 841 | return $desc; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * OutputPageParserOutput hook handler |
| 846 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput |
| 847 | * |
| 848 | * @param OutputPage $outputPage the OutputPage object to which wikitext is added |
| 849 | * @param ParserOutput $parserOutput |
| 850 | */ |
| 851 | public function onOutputPageParserOutput( $outputPage, $parserOutput ): void { |
| 852 | $title = $outputPage->getTitle(); |
| 853 | $descriptionsEnabled = !$title->isMainPage() && |
| 854 | $title->getNamespace() === NS_MAIN && |
| 855 | $this->featuresManager->isFeatureAvailableForCurrentUser( |
| 856 | 'MFEnableWikidataDescriptions' |
| 857 | ) && $this->mobileContext->shouldShowWikibaseDescriptions( 'tagline', $this->config ); |
| 858 | |
| 859 | // Only set the tagline if the feature has been enabled and the article is in the main namespace |
| 860 | if ( $this->mobileContext->shouldDisplayMobileView() && $descriptionsEnabled ) { |
| 861 | $desc = self::findTagline( $parserOutput, ExtMobileFrontend::getWikibaseDescription( ... ) ); |
| 862 | if ( $desc ) { |
| 863 | self::setTagline( $outputPage, $desc ); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * ArticleParserOptions hook handler |
| 870 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleParserOptions |
| 871 | * |
| 872 | * @param Article $article |
| 873 | * @param ParserOptions $parserOptions |
| 874 | */ |
| 875 | public function onArticleParserOptions( Article $article, ParserOptions $parserOptions ) { |
| 876 | // set the collapsible sections parser flag so that section content is wrapped in a div for easier targeting |
| 877 | // only if we're in mobile view and parsoid is enabled |
| 878 | if ( $this->mobileContext->shouldDisplayMobileView() && $this->shouldUseParsoid( $article->getTitle() ) ) { |
| 879 | $title = $article->getTitle(); |
| 880 | $namespace = $title->getNamespace(); |
| 881 | $namespacesWithoutCollapsibleSections = $this->config->get( 'MFNamespacesWithoutCollapsibleSections' ); |
| 882 | |
| 883 | // Don't enable collapsible sections for certain namespaces |
| 884 | if ( !in_array( $namespace, $namespacesWithoutCollapsibleSections ) ) { |
| 885 | $parserOptions->setCollapsibleSections(); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | private function shouldUseParsoid( Title $title ): bool { |
| 891 | // while the parser is actively being migrated, we rely on the ParserMigration extension for using Parsoid |
| 892 | if ( ExtensionRegistry::getInstance()->isLoaded( 'ParserMigration' ) ) { |
| 893 | $context = $this->mobileContext; |
| 894 | $oracle = MediaWikiServices::getInstance()->getService( 'ParserMigration.Oracle' ); |
| 895 | |
| 896 | return $oracle->shouldUseParsoid( $context->getUser(), $context->getRequest(), $title ); |
| 897 | |
| 898 | } |
| 899 | return false; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * HTMLFileCache::useFileCache hook handler |
| 904 | * Disables file caching for mobile pageviews |
| 905 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/HTMLFileCache::useFileCache |
| 906 | * |
| 907 | * @param IContextSource $context |
| 908 | * @return bool |
| 909 | */ |
| 910 | public function onHTMLFileCache__useFileCache( $context ) { |
| 911 | return !$this->mobileContext->shouldDisplayMobileView(); |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * LoginFormValidErrorMessages hook handler to promote MF specific error message be valid. |
| 916 | * |
| 917 | * @param array &$messages Array of already added messages |
| 918 | */ |
| 919 | public function onLoginFormValidErrorMessages( array &$messages ) { |
| 920 | $messages = array_merge( $messages, |
| 921 | [ |
| 922 | // watchstart sign up CTA |
| 923 | 'mobile-frontend-watchlist-signup-action', |
| 924 | // Watchlist and watchstar sign in CTA |
| 925 | 'mobile-frontend-watchlist-purpose', |
| 926 | 'mobile-frontend-donate-image-login-action', |
| 927 | ] |
| 928 | ); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Handler for MakeGlobalVariablesScript hook. |
| 933 | * For values that depend on the current page, user or request state. |
| 934 | * |
| 935 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript |
| 936 | * @param array &$vars Variables to be added into the output |
| 937 | * @param OutputPage $out OutputPage instance calling the hook |
| 938 | */ |
| 939 | public function onMakeGlobalVariablesScript( &$vars, $out ): void { |
| 940 | $services = MediaWikiServices::getInstance(); |
| 941 | /** @var \MobileFrontend\Amc\UserMode $userMode */ |
| 942 | $userMode = $services->getService( 'MobileFrontend.AMC.UserMode' ); |
| 943 | |
| 944 | // If the device is a mobile, Remove the category entry. |
| 945 | $context = $this->mobileContext; |
| 946 | if ( $context->shouldDisplayMobileView() ) { |
| 947 | unset( $vars['wgCategories'] ); |
| 948 | $vars['wgMFMode'] = 'stable'; |
| 949 | $vars['wgMFAmc'] = $userMode->isEnabled(); |
| 950 | $vars['wgMFEditNoticesFeatureConflict'] = $this->hasEditNoticesFeatureConflict( |
| 951 | $this->config, $context->getUser() |
| 952 | ); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Check if a conflicting edit notices gadget is enabled for the current user |
| 958 | * |
| 959 | * @param Config $config |
| 960 | * @param User $user |
| 961 | * @return bool |
| 962 | */ |
| 963 | private function hasEditNoticesFeatureConflict( Config $config, User $user ): bool { |
| 964 | $gadgetName = $config->get( 'MFEditNoticesConflictingGadgetName' ); |
| 965 | if ( !$gadgetName ) { |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | if ( $this->gadgetRepo ) { |
| 970 | $match = array_search( $gadgetName, $this->gadgetRepo->getGadgetIds(), true ); |
| 971 | if ( $match !== false ) { |
| 972 | try { |
| 973 | return $this->gadgetRepo->getGadget( $gadgetName ) |
| 974 | ->isEnabled( $user ); |
| 975 | } catch ( \InvalidArgumentException ) { |
| 976 | return false; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Handler for TitleSquidURLs hook to add copies of the cache purge |
| 985 | * URLs which are transformed according to the wgMobileUrlCallback, so |
| 986 | * that both mobile and non-mobile URL variants get purged. |
| 987 | * |
| 988 | * @see * https://www.mediawiki.org/wiki/Manual:Hooks/TitleSquidURLs |
| 989 | * @param Title $title the article title |
| 990 | * @param array &$urls the set of URLs to purge |
| 991 | */ |
| 992 | public function onTitleSquidURLs( $title, &$urls ) { |
| 993 | foreach ( $urls as $url ) { |
| 994 | $newUrl = $this->mobileContext->getMobileUrl( $url ); |
| 995 | if ( $newUrl !== false && $newUrl !== $url ) { |
| 996 | $urls[] = $newUrl; |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Add the base mobile site URL to the siteinfo API output. |
| 1003 | * @param ApiQuerySiteinfo $module |
| 1004 | * @param array &$result Api result array |
| 1005 | */ |
| 1006 | public function onAPIQuerySiteInfoGeneralInfo( $module, &$result ) { |
| 1007 | global $wgCanonicalServer; |
| 1008 | $result['mobileserver'] = $this->mobileContext->getMobileUrl( $wgCanonicalServer ); |
| 1009 | } |
| 1010 | } |