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 The page view.
* @param Skin &$skin The skin that's going to build the UI.
* @return boolean Always true.
*/
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;
}
public static function onDiffViewHeader(
DifferenceEngine $diff,
Revision $oldRev,
Revision $newRev
) {
$config = ConfigFactory::getDefaultInstance()->makeConfig( 'visualeditor' );
$output = RequestContext::getMain()->getOutput();
if (
!$config->get( 'VisualEditorEnableDiffPage' ) &&
$output->getRequest()->getVal( 'visualdiff' ) === null
) {
return;
}
$output->addModuleStyles( [
'ext.visualEditor.diffPage.init.styles',
'oojs-ui.styles.icons-alerts',
'oojs-ui.styles.icons-editing-advanced'
] );
$output->addModules( 'ext.visualEditor.diffPage.init' );
$output->enableOOUI();
$output->addHtml(
'
' .
// Will be replaced by a ButtonSelectWidget in JS
new OOUI\ButtonGroupWidget( [
'items' => [
new \OOUI\ButtonWidget( [
'data' => 'visual',
'icon' => 'eye',
'disabled' => true,
'label' => $output->msg( 'visualeditor-savedialog-review-visual' )->plain()
] ),
new \OOUI\ButtonWidget( [
'data' => 'source',
'icon' => 'wikiText',
'active' => true,
'label' => $output->msg( 'visualeditor-savedialog-review-wikitext' )->plain()
] )
]
] ) .
'
'
);
}
/**
* 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
* @return boolean True if the User Agent is blacklisted
*/
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 The article being viewed.
* @param User $user The user-specific settings.
* @return boolean 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 (