Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| MobileFrontendEditorHooks | |
0.00% |
0 / 57 |
|
0.00% |
0 / 5 |
380 | |
0.00% |
0 / 1 |
| getContentLanguageMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getResourceLoaderMFConfigVars | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| onMakeGlobalVariablesScript | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| onCustomEditor | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |||
| isSupportedEditRequest | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Config\Config; |
| 4 | use MediaWiki\Context\IContextSource; |
| 5 | use MediaWiki\Hook\CustomEditorHook; |
| 6 | use MediaWiki\MediaWikiServices; |
| 7 | use MediaWiki\Output\Hook\MakeGlobalVariablesScriptHook; |
| 8 | use MediaWiki\Output\OutputPage; |
| 9 | use MediaWiki\Page\Article; |
| 10 | use MediaWiki\Registration\ExtensionRegistry; |
| 11 | use MediaWiki\ResourceLoader\Context; |
| 12 | use MediaWiki\User\User; |
| 13 | |
| 14 | class MobileFrontendEditorHooks implements |
| 15 | CustomEditorHook, |
| 16 | MakeGlobalVariablesScriptHook |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * Return messages in content language, for use in a ResourceLoader module. |
| 21 | * |
| 22 | * @param Context $context |
| 23 | * @param Config $config |
| 24 | * @param array $messagesKeys |
| 25 | * @return array |
| 26 | */ |
| 27 | public static function getContentLanguageMessages( |
| 28 | Context $context, Config $config, array $messagesKeys = [] |
| 29 | ): array { |
| 30 | return array_combine( |
| 31 | $messagesKeys, |
| 32 | array_map( static function ( $key ) { |
| 33 | return wfMessage( $key )->inContentLanguage()->text(); |
| 34 | }, $messagesKeys ) |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Generate config for usage inside MobileFrontend |
| 40 | * This should be used for variables which: |
| 41 | * - vary with the html |
| 42 | * - should work cross skin including anonymous users. |
| 43 | * |
| 44 | * @return array |
| 45 | */ |
| 46 | public static function getResourceLoaderMFConfigVars() { |
| 47 | $config = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Config' ); |
| 48 | |
| 49 | return [ |
| 50 | 'wgMFDefaultEditor' => $config->get( 'MFDefaultEditor' ), |
| 51 | 'wgMFFallbackEditor' => $config->get( 'MFFallbackEditor' ), |
| 52 | 'wgMFEnableVEWikitextEditor' => $config->get( 'MFEnableVEWikitextEditor' ), |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handler for MakeGlobalVariablesScript hook. |
| 58 | * For values that depend on the current page, user or request state. |
| 59 | * |
| 60 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript |
| 61 | * @param array &$vars Variables to be added into the output |
| 62 | * @param OutputPage $out OutputPage instance calling the hook |
| 63 | */ |
| 64 | public function onMakeGlobalVariablesScript( &$vars, $out ): void { |
| 65 | /** @var MobileContext $mobileContext */ |
| 66 | $mobileContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' ); |
| 67 | $config = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Config' ); |
| 68 | |
| 69 | if ( $mobileContext->shouldDisplayMobileView() ) { |
| 70 | // mobile.init |
| 71 | $vars['wgMFIsSupportedEditRequest'] = self::isSupportedEditRequest( $out->getContext() ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Decide whether to bother showing the wikitext editor at all. |
| 77 | * If not, we expect the editor initialisation JS to activate. |
| 78 | * |
| 79 | * @param Article $article The article being viewed. |
| 80 | * @param User $user The user-specific settings. |
| 81 | * @return bool Whether to show the wikitext editor or not. |
| 82 | */ |
| 83 | public function onCustomEditor( $article, $user ) { |
| 84 | $req = $article->getContext()->getRequest(); |
| 85 | $title = $article->getTitle(); |
| 86 | if ( |
| 87 | !$req->getVal( 'mfnoscript' ) && |
| 88 | self::isSupportedEditRequest( $article->getContext() ) |
| 89 | ) { |
| 90 | $params = $req->getValues(); |
| 91 | $params['mfnoscript'] = '1'; |
| 92 | $url = wfScript() . '?' . wfArrayToCgi( $params ); |
| 93 | $escapedUrl = htmlspecialchars( $url ); |
| 94 | |
| 95 | $out = $article->getContext()->getOutput(); |
| 96 | $titleMsg = $title->exists() ? 'editing' : 'creating'; |
| 97 | $out->setPageTitleMsg( wfMessage( $titleMsg, $title->getPrefixedText() ) ); |
| 98 | |
| 99 | $msgParams = []; |
| 100 | if ( $title->inNamespace( NS_FILE ) && !$title->exists() ) { |
| 101 | // Is a new file page (enable upload image only) T60311 |
| 102 | $msg = 'mobile-frontend-editor-uploadenable'; |
| 103 | } else { |
| 104 | $msg = 'mobile-frontend-editor-toload'; |
| 105 | $urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); |
| 106 | $msgParams[] = $urlUtils->expand( $url, PROTO_CURRENT ); |
| 107 | } |
| 108 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Only null for invalid URL, shouldn't happen |
| 109 | $out->showPendingTakeover( $url, $msg, ...$msgParams ); |
| 110 | |
| 111 | $out->setRevisionId( $req->getInt( 'oldid', $article->getRevIdFetched() ) ); |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Whether the custom editor override should occur |
| 119 | * |
| 120 | * @param IContextSource $context |
| 121 | * @return bool Whether the frontend JS should try to display an editor |
| 122 | */ |
| 123 | protected static function isSupportedEditRequest( IContextSource $context ) { |
| 124 | /** @var MobileContext $mobileContext */ |
| 125 | $mobileContext = MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' ); |
| 126 | if ( !$mobileContext->shouldDisplayMobileView() ) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | $extensionRegistry = ExtensionRegistry::getInstance(); |
| 131 | $editorAvailableSkins = $extensionRegistry->getAttribute( 'MobileFrontendEditorAvailableSkins' ); |
| 132 | if ( !in_array( $context->getSkin()->getSkinName(), $editorAvailableSkins ) ) { |
| 133 | // Mobile editor commonly doesn't work well with other skins than Minerva (it looks horribly |
| 134 | // broken without some styles that are only defined by Minerva). So we only enable it for the |
| 135 | // skin that wants it. |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | $req = $context->getRequest(); |
| 140 | $title = $context->getTitle(); |
| 141 | |
| 142 | // Various things fall back to WikiEditor |
| 143 | if ( $req->getRawVal( 'action' ) === 'submit' ) { |
| 144 | // Don't try to take over if the form has already been submitted |
| 145 | return false; |
| 146 | } |
| 147 | if ( $title->inNamespace( NS_SPECIAL ) ) { |
| 148 | return false; |
| 149 | } |
| 150 | if ( !$title->hasContentModel( CONTENT_MODEL_WIKITEXT ) ) { |
| 151 | // Only load the wikitext editor on wikitext. Otherwise we'll rely on the fallback behaviour |
| 152 | // (You can test this on MediaWiki:Common.css) ?action=edit url (T173800) |
| 153 | return false; |
| 154 | } |
| 155 | if ( $req->getCheck( 'undo' ) || $req->getCheck( 'undoafter' ) ) { |
| 156 | // Undo needs to show a diff above the editor |
| 157 | return false; |
| 158 | } |
| 159 | if ( $req->getRawVal( 'section' ) === 'new' ) { |
| 160 | // New sections need a title field |
| 161 | return false; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | } |