makeConfig( 'visualeditor' ); $class = $name === 'visualeditor' ? 'ApiVisualEditor' : 'ApiVisualEditorEdit'; return new $class( $main, $name, $config ); } /** * Adds VisualEditor JS to the output. * * This is attached to the MediaWiki 'BeforePageDisplay' hook. * * @param OutputPage $output * @param Skin $skin * @return boolean */ public static function onBeforePageDisplay( OutputPage &$output, Skin &$skin ) { $output->addModules( [ 'ext.visualEditor.desktopArticleTarget.init', 'ext.visualEditor.targetLoader' ] ); $output->addModuleStyles( [ 'ext.visualEditor.desktopArticleTarget.noscript' ] ); // add scroll offset js variable to output $veConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'visualeditor' ); $skinsToolbarScrollOffset = $veConfig->get( 'VisualEditorSkinToolbarScrollOffset' ); $toolbarScrollOffset = 0; $skinName = $skin->getSkinName(); if ( isset( $skinsToolbarScrollOffset[$skinName] ) ) { $toolbarScrollOffset = $skinsToolbarScrollOffset[$skinName]; } $output->addJsConfigVars( 'wgVisualEditorToolbarScrollOffset', $toolbarScrollOffset ); $output->addJsConfigVars( 'wgVisualEditorUnsupportedEditParams', self::$unsupportedEditParams ); $output->addJsConfigVars( 'wgEditSubmitButtonLabelPublish', $veConfig->get( 'EditSubmitButtonLabelPublish' ) ); return true; } /** * Detect incompatibile browsers which we can't expect to load VE * * @param WebRequest $req The web request to check the details of * @param Config $config VE config object * @param boolean * @return boolean */ private static function isUABlacklisted( WebRequest $req, $config ) { if ( $req->getVal( 'vewhitelist' ) ) { return false; } $blacklist = $config->get( 'VisualEditorBrowserBlacklist' ); $ua = strtolower( $req->getHeader( 'User-Agent' ) ); foreach ( $blacklist as $uaSubstr => $rules ) { if ( !strpos( $ua, $uaSubstr . '/' ) ) { continue; } if ( !is_array( $rules ) ) { return true; } $matches = []; $ret = preg_match( '/' . $uaSubstr . '\/([0-9\.]*) ?/i', $ua, $matches ); if ( $ret !== 1 ) { continue; } $version = $matches[1]; foreach ( $rules as $rule ) { list( $op, $matchVersion ) = $rule; if ( ( $op === '<' && $version < $matchVersion ) || ( $op === '>' && $version > $matchVersion ) || ( $op === '<=' && $version <= $matchVersion ) || ( $op === '>=' && $version >= $matchVersion ) ) { return true; } } } return false; } private static function isSupportedEditPage( Title $title, User $user, WebRequest $req ) { if ( $req->getVal( 'action' ) !== 'edit' || !$title->quickUserCan( 'edit' ) ) { return false; } foreach ( self::$unsupportedEditParams as $param ) { if ( $req->getVal( $param ) !== null ) { return false; } } if ( $req->getVal( 'wteswitched' ) ) { return self::isVisualAvailable( $title ); } switch ( self::getPreferredEditor( $user, $req ) ) { case 'visualeditor': return self::isVisualAvailable( $title ) || self::isWikitextAvailable( $title, $user ); case 'wikitext': return self::isWikitextAvailable( $title, $user ); } return false; } private static function isVisualAvailable( $title ) { $veConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'visualeditor' ); return ApiVisualEditor::isAllowedNamespace( $veConfig, $title->getNamespace() ) && ApiVisualEditor::isAllowedContentType( $veConfig, $title->getContentModel() ); } private static function isWikitextAvailable( $title, $user ) { $veConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'visualeditor' ); return $veConfig->get( 'VisualEditorEnableWikitext' ) && $user->getOption( 'visualeditor-newwikitext' ) && $title->getContentModel() === 'wikitext'; } /** * Decide whether to bother showing the wikitext editor at all. * If not, we expect the VE initialisation JS to activate. * @param $article Article * @param $user User * @return bool Whether to show the wikitext editor or not. */ public static function onCustomEditor( Article $article, User $user ) { $req = $article->getContext()->getRequest(); $veConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'visualeditor' ); if ( !$user->getOption( 'visualeditor-enable' ) || $user->getOption( 'visualeditor-betatempdisable' ) || $user->getOption( 'visualeditor-autodisable' ) || ( $veConfig->get( 'VisualEditorDisableForAnons' ) && $user->isAnon() ) || self::isUABlacklisted( $req, $veConfig ) ) { return true; } $title = $article->getTitle(); if ( $req->getVal( 'venoscript' ) ) { $req->response()->setCookie( 'VEE', 'wikitext', 0, [ 'prefix' => '' ] ); $user->setOption( 'visualeditor-editor', 'wikitext' ); if ( !wfReadOnly() && !$user->isAnon() ) { DeferredUpdates::addCallableUpdate( function () use ( $user ) { $user->saveSettings(); } ); } return true; } if ( self::isSupportedEditPage( $title, $user, $req ) ) { $params = $req->getValues(); $params['venoscript'] = '1'; $url = wfScript() . '?' . wfArrayToCgi( $params ); $escapedUrl = htmlspecialchars( $url ); $out = $article->getContext()->getOutput(); $titleMsg = $title->exists() ? 'editing' : 'creating'; $out->setPageTitle( wfMessage( $titleMsg, $title->getPrefixedText() ) ); $out->addWikiMsg( 'visualeditor-toload', wfExpandUrl( $url ) ); // Redirect if the user has no JS (