mediawiki-extensions-Visual.../VisualEditor.hooks.php
Roan Kattouw ced2a8aa59 Move edit tab generation into PHP and make it more configurable
* Generate the edit tabs and the section edit links in PHP, with a
  fallback in JS for cases where we don't have them yet due to
  caching. But only change things if VE is enabled, and have the JS
  correct the state if the wrong cached HTML comes through.
* Make the order of the tabs/links and the messages to use as captions
  configurable
* Make the edit tabs and section edit links always be present in the
  page (regardless of namespace, user prefs, etc.) but be hidden and
  have JS unhide them (using html.ve-available) if appropriate
* Add appendix messages so we can do a superscript "beta" even in places
  where we can't use HTML in the message

VisualEditor.php:
* Add new hook registrations
* Remove edit link caption messages from the init init module because
  they're now added dynamically in VisualEditor.hooks.php
* Add a noscript CSS module so we can hide some things in JS-less
  environments
* Remove $wgVisualEditorTabLayout and replace it with
  $wgVisualEditorPosition
* Add config vars for link captions, with null causing us to use
  the default caption
* Add config vars for link caption appendices. Too many config vars
  but we'll clean that up later

VisualEditor.hooks.php:
* Dynamically add tab messages to the init init module
* Remove unused globals in onBeforePageDisplay()
* Add noscript CSS module
* Add a SkinTemplateNavigation hook that changes and reorders the edit
  tabs as appropriate
* Add a DoEditSectionLink hook that overwrites the edit section links
* Export the new config variables to JS

VisualEditor.i18n.php:
* Add beta appendix message
* Add a message for the default VE edit section link

ve.init.mw.ViewPageTarget.init.css:
* Remove the animation on the edit section links
* Darken the color of the brackets and the pipe from #ccc to #555
* Style the beta message to be superscript-like (but not real <sup> to
  avoid moving the baseline)

ve.init.mw.ViewPageTarget.noscript.css:
* Hide the VE edit tab, the pipe and the VE edit section link initally
  unless and until JS unhides

ve.init.mw.ViewPageTarget.init.js:
* Toggle .ve-not-available / .ve-available
* Edit tabs
** Only generate the the edit tabs if they're not already there from PHP
** Rewrite the edit tab generation to mirror what's being done in PHP
* Section edit links
** Same as for edit tabs
** Also add mw-visualeditor-expanded to pad the brackets

ve.init.mw.ViewPageTarget.js:
* #ca-ve-edit is now always the VE tab (and #ca-edit always the
  edit source tab) so update the .selected behavior accordingly

Change-Id: Idcb15faea7fabe5fe7578b1508079969b27d2469
2013-08-01 21:08:13 -07:00

416 lines
14 KiB
PHP

<?php
/**
* VisualEditor extension hooks
*
* @file
* @ingroup Extensions
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
class VisualEditorHooks {
/** List of skins VisualEditor integration supports */
protected static $supportedSkins = array( 'vector', 'apex', 'monobook' );
public static function onSetup() {
global $wgVisualEditorEnableEventLogging, $wgResourceModules,
$wgVisualEditorResourceTemplate, $wgVisualEditorTabMessages;
// This prevents VisualEditor from being run in environments that don't
// have the dependent code in core; this should be updated as a part of
// when additional dependencies are created and pushed into MediaWiki's
// core. The most direct effect of this is to avoid confusing any third
// parties who attempt to install VisualEditor onto non-alpha wikis, as
// this should have no impact on deploying to Wikimedia's wiki cluster.
// Is fine for release tarballs because 1.22wmf11 < 1.22alpha < 1.22.0.
wfUseMW( '1.22wmf11' );
// Add tab messages to the init init module
foreach ( $wgVisualEditorTabMessages as $msg ) {
if ( $msg !== null ) {
$wgResourceModules['ext.visualEditor.viewPageTarget.init']['messages'][] = $msg;
}
}
if ( $wgVisualEditorEnableEventLogging ) {
if ( class_exists( 'ResourceLoaderSchemaModule' ) ) {
// EventLogging schema module for logging edit events.
// See <http://meta.wikimedia.org/wiki/Schema:Edit>
$wgResourceModules['schema.Edit'] = array(
'class' => 'ResourceLoaderSchemaModule',
'schema' => 'Edit',
'revision' => 5570274,
);
} else {
wfWarn( 'VisualEditor is configured to use EventLogging, but the extension is ' .
' not available. Disabling wgVisualEditorEnableEventLogging.' );
$wgVisualEditorEnableEventLogging = false;
}
}
// Only load jquery.ULS if ULS Extension isn't already installed:
if ( !class_exists( 'UniversalLanguageSelectorHooks' ) ) {
$wgResourceModules['jquery.uls'] = $wgVisualEditorResourceTemplate + array(
'scripts' => array(
'jquery.uls/src/jquery.uls.core.js',
'jquery.uls/src/jquery.uls.lcd.js',
'jquery.uls/src/jquery.uls.languagefilter.js',
'jquery.uls/src/jquery.uls.regionfilter.js',
),
'styles' => array(
'jquery.uls/css/jquery.uls.css',
'jquery.uls/css/jquery.uls.lcd.css',
),
'dependencies' => array(
'jquery.uls.grid',
'jquery.uls.data',
'jquery.uls.compact',
),
);
$wgResourceModules['jquery.uls.data'] = $wgVisualEditorResourceTemplate + array(
'scripts' => array(
'jquery.uls/src/jquery.uls.data.js',
'jquery.uls/src/jquery.uls.data.utils.js',
),
'position' => 'top',
);
$wgResourceModules['jquery.uls.grid'] = $wgVisualEditorResourceTemplate + array(
'styles' => 'jquery.uls/css/jquery.uls.grid.css',
'position' => 'top',
);
$wgResourceModules['jquery.uls.compact'] = $wgVisualEditorResourceTemplate + array(
'styles' => 'jquery.uls/css/jquery.uls.compact.css',
'position' => 'top',
);
}
}
/**
* Adds VisualEditor JS to the output.
*
* This is attached to the MediaWiki 'BeforePageDisplay' hook.
*
* @param $output OutputPage
* @param $skin Skin
*/
public static function onBeforePageDisplay( &$output, &$skin ) {
global $wgVisualEditorEnableEventLogging;
if ( $wgVisualEditorEnableEventLogging ) {
$output->addModules( array( 'schema.Edit' ) );
}
$output->addModules( array( 'ext.visualEditor.viewPageTarget.init' ) );
$output->addModuleStyles( array( 'ext.visualEditor.viewPageTarget.noscript' ) );
return true;
}
/**
* Changes the Edit tab and adds the VisualEditor tab.
*
* This is attached to the MediaWiki 'SkinTemplateNavigation' hook.
*
* @param SkinTemplate $skin
* @param array $links Navigation links
* @return boolean
*/
public static function onSkinTemplateNavigation( &$skin, &$links ) {
// Only do this if the user has VE enabled
if (
!$skin->getUser()->getOption( 'visualeditor-enable' ) ||
$skin->getUser()->getOption( 'visualeditor-betatempdisable' )
) {
return true;
}
global $wgVisualEditorTabMessages, $wgVisualEditorTabPosition;
if ( !isset( $links['views']['edit'] ) ) {
// There's no edit link, nothing to do
return true;
}
$title = $skin->getRelevantTitle();
// Rebuild the $links['views'] array and inject the VisualEditor tab before or after
// the edit tab as appropriate. We have to rebuild the array because PHP doesn't allow
// us to splice into the middle of an associative array.
$newViews = array();
foreach ( $links['views'] as $action => $data ) {
if ( $action === 'edit' ) {
// Build the VisualEditor tab
$existing = $title->exists() || (
$title->getNamespace() == NS_MEDIAWIKI &&
$title->getDefaultMessageText() !== false
);
$veParams = $skin->editUrlOptions();
unset( $veParams['action'] ); // Remove action=edit
$veParams['veaction'] = 'edit'; // Set veaction=edit
$veTabMessage = $wgVisualEditorTabMessages[$existing ? 'edit' : 'create'];
$veTabText = $veTabMessage === null ? $data['text'] :
wfMessage( $veTabMessage )->setContext( $skin->getContext() )->text();
$veTab = array(
'href' => $title->getLocalURL( $veParams ),
'text' => $veTabText,
'primary' => true,
'class' => '',
);
// Alter the edit tab
$editTab = $data;
$editTabMessage = $wgVisualEditorTabMessages[$existing ? 'editsource' : 'createsource'];
if ( $editTabMessage !== null ) {
$editTab['text'] = wfMessage( $editTabMessage )->setContext( $skin->getContext() )->text();
}
// Inject the VE tab before or after the edit tab
if ( $wgVisualEditorTabPosition === 'before' ) {
$newViews['ve-edit'] = $veTab;
$newViews['edit'] = $editTab;
} else {
$newViews['edit'] = $editTab;
$newViews['ve-edit'] = $veTab;
}
} else {
// Just pass through
$newViews[$action] = $data;
}
}
$links['views'] = $newViews;
return true;
}
/**
* Changes the section edit links to add a VE edit link.
*
* This is attached to the MediaWiki 'DoEditSectionLink' hook.
*
* @param $skin Skin
* @param $title Title
* @param $section string
* @param $tooltip string
* @param $result string HTML
* @param $lang Language
* @returns bool true
*/
public static function onDoEditSectionLink( $skin, $title, $section, $tooltip, &$result, $lang ) {
// Only do this if the user has VE enabled
if (
!$skin->getUser()->getOption( 'visualeditor-enable' ) ||
$skin->getUser()->getOption( 'visualeditor-betatempdisable' )
) {
return;
}
global $wgVisualEditorTabMessages, $wgVisualEditorTabPosition;
$veEditSection = $wgVisualEditorTabMessages['editsection'] !== null ?
$wgVisualEditorTabMessages['editsection'] : 'editsection';
$sourceEditSection = $wgVisualEditorTabMessages['editsectionsource'] !== null ?
$wgVisualEditorTabMessages['editsectionsource'] : 'editsection';
// Code mostly duplicated from Skin::doEditSectionLink() :(
$attribs = array();
if ( !is_null( $tooltip ) ) {
# Bug 25462: undo double-escaping.
$tooltip = Sanitizer::decodeCharReferences( $tooltip );
$attribs['title'] = wfMessage( 'editsectionhint' )->rawParams( $tooltip )
->inLanguage( $lang )->text();
}
$veLink = Linker::link( $title, wfMessage( $veEditSection )->inLanguage( $lang )->text(),
$attribs + array( 'class' => 'mw-editsection-visualeditor' ),
array( 'veaction' => 'edit', 'section' => $section ),
array( 'noclasses', 'known' )
);
$sourceLink = Linker::link( $title, wfMessage( $sourceEditSection )->inLanguage( $lang )->text(),
$attribs,
array( 'action' => 'edit', 'section' => $section ),
array( 'noclasses', 'known' )
);
$veFirst = $wgVisualEditorTabPosition === 'before';
$result = '<span class="mw-editsection">'
. '<span class="mw-editsection-bracket">[</span>'
. ( $veFirst ? $veLink : $sourceLink )
. '<span class="mw-editsection-divider">'
. wfMessage( 'pipe-separator' )->inLanguage( $lang )->text()
. '</span>'
. ( $veFirst ? $sourceLink : $veLink )
. '<span class="mw-editsection-bracket">]</span>'
. '</span>';
return true;
}
public static function onGetPreferences( $user, &$preferences ) {
$preferences['visualeditor-enable'] = array(
'type' => 'toggle',
'label-message' => 'visualeditor-preference-enable',
'section' => 'editing/beta'
);
$preferences['visualeditor-betatempdisable'] = array(
'type' => 'toggle',
'label-message' => 'visualeditor-preference-betatempdisable',
'section' => 'editing/beta'
);
return true;
}
public static function onListDefinedTags( &$tags ) {
$tags[] = 'visualeditor';
$tags[] = 'visualeditor-needcheck';
return true;
}
/**
* Adds extra variables to the page config.
*/
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
global $wgStylePath, $wgContLang;
$vars['wgVisualEditor'] = array(
'isPageWatched' => $out->getUser()->isWatched( $out->getTitle() ),
// Same as in Linker.php
'magnifyClipIconURL' => $wgStylePath .
'/common/images/magnify-clip' .
( $wgContLang->isRTL() ? '-rtl' : '' ) . '.png',
'pageLanguageCode' => $out->getTitle()->getPageLanguage()->getHtmlCode(),
'pageLanguageDir' => $out->getTitle()->getPageLanguage()->getDir(),
);
return true;
}
/**
* Adds extra variables to the global config
*/
public static function onResourceLoaderGetConfigVars( array &$vars ) {
global $wgDefaultUserOptions,
$wgVisualEditorDisableForAnons,
$wgVisualEditorEnableEventLogging,
$wgVisualEditorEnableExperimentalCode,
$wgVisualEditorNamespaces,
$wgVisualEditorPluginModules,
$wgVisualEditorTabPosition,
$wgVisualEditorTabMessages;
$vars['wgVisualEditorConfig'] = array(
'disableForAnons' => $wgVisualEditorDisableForAnons,
'enableEventLogging' => $wgVisualEditorEnableEventLogging,
'enableExperimentalCode' => $wgVisualEditorEnableExperimentalCode,
'namespaces' => $wgVisualEditorNamespaces,
'pluginModules' => $wgVisualEditorPluginModules,
'defaultUserOptions' => array(
'enable' => $wgDefaultUserOptions['visualeditor-enable'],
'betatempdisable' => $wgDefaultUserOptions['visualeditor-betatempdisable'],
),
'skins' => self::$supportedSkins,
'tabPosition' => $wgVisualEditorTabPosition,
'tabMessages' => $wgVisualEditorTabMessages,
);
return true;
}
public static function onResourceLoaderTestModules(
array &$testModules,
ResourceLoader &$resourceLoader
) {
$testModules['qunit']['ext.visualEditor.test'] = array(
'scripts' => array(
// MW config preload
've-mw/test/mw-preload.js',
// QUnit plugin
've/test/ve.qunit.js',
// UnicodeJS Tests
'unicodejs/test/unicodejs.test.js',
'unicodejs/test/unicodejs.graphemebreak.test.js',
'unicodejs/test/unicodejs.wordbreak.test.js',
// VisualEditor Tests
've/test/ve.test.utils.js',
've/test/ve.test.js',
've/test/ve.Document.test.js',
've/test/ve.Element.test.js',
've/test/ve.Node.test.js',
've/test/ve.BranchNode.test.js',
've/test/ve.LeafNode.test.js',
've/test/ve.Factory.test.js',
// VisualEditor DataModel Tests
've/test/dm/ve.dm.example.js',
've/test/dm/ve.dm.AnnotationSet.test.js',
've/test/dm/ve.dm.NodeFactory.test.js',
've/test/dm/ve.dm.Node.test.js',
've/test/dm/ve.dm.Converter.test.js',
've/test/dm/ve.dm.BranchNode.test.js',
've/test/dm/ve.dm.LeafNode.test.js',
've/test/dm/ve.dm.LinearData.test.js',
've/test/dm/nodes/ve.dm.TextNode.test.js',
've-mw/test/dm/nodes/ve.dm.MWTransclusionNode.test.js',
've/test/dm/ve.dm.Document.test.js',
've/test/dm/ve.dm.DocumentSynchronizer.test.js',
've/test/dm/ve.dm.IndexValueStore.test.js',
've/test/dm/ve.dm.InternalList.test.js',
've-mw/test/dm/ve.dm.InternalList.test.js',
've/test/dm/ve.dm.Transaction.test.js',
've/test/dm/ve.dm.TransactionProcessor.test.js',
've/test/dm/ve.dm.Surface.test.js',
've/test/dm/ve.dm.SurfaceFragment.test.js',
've-mw/test/dm/ve.dm.SurfaceFragment.test.js',
've/test/dm/ve.dm.ModelRegistry.test.js',
've/test/dm/ve.dm.MetaList.test.js',
've/test/dm/ve.dm.Model.test.js',
've/test/dm/lineardata/ve.dm.ElementLinearData.test.js',
've/test/dm/lineardata/ve.dm.MetaLinearData.test.js',
've-mw/test/dm/ve.dm.mwExample.js',
've-mw/test/dm/ve.dm.MWConverter.test.js',
// VisualEditor ContentEditable Tests
've/test/ce/ve.ce.test.js',
've/test/ce/ve.ce.Document.test.js',
've-mw/test/ce/ve.ce.Document.test.js',
've/test/ce/ve.ce.NodeFactory.test.js',
've/test/ce/ve.ce.Node.test.js',
've/test/ce/ve.ce.BranchNode.test.js',
've/test/ce/ve.ce.ContentBranchNode.test.js',
've-mw/test/ce/ve.ce.ContentBranchNode.test.js',
've/test/ce/ve.ce.LeafNode.test.js',
've/test/ce/nodes/ve.ce.TextNode.test.js',
// VisualEditor Actions Tests
've/test/ui/actions/ve.ui.FormatAction.test.js',
've-mw/test/ui/actions/ve.ui.FormatAction.test.js',
've/test/ui/actions/ve.ui.IndentationAction.test.js',
've/test/ui/actions/ve.ui.ListAction.test.js',
// VisualEditor initialization Tests
've/test/init/ve.init.Platform.test.js',
've-mw/test/init/targets/ve.init.mw.ViewPageTarget.test.js',
),
'dependencies' => array(
'unicodejs.wordbreak',
'ext.visualEditor.standalone',
'ext.visualEditor.core',
'ext.visualEditor.experimental',
'ext.visualEditor.viewPageTarget.init',
'ext.visualEditor.viewPageTarget',
),
'localBasePath' => dirname( __FILE__ ) . '/modules',
'remoteExtPath' => 'VisualEditor/modules',
);
return true;
}
/**
* Sets user preference to enable the VisualEditor account if their new
* account's userID is even, if $wgVisualEditorEnableSplitTest is true.
*
* Added per bug 49604; to be removed once no longer needed.
*/
public static function onAddNewAccount( $user, $byEmail ) {
global $wgVisualEditorEnableSplitTest;
if ( $wgVisualEditorEnableSplitTest &&
$user->isLoggedin() &&
( ( $user->getId() % 2 ) === 0 ) ) {
$user->setOption( 'visualeditor-enable', 1 );
$user->saveSettings();
}
return true;
}
}