Radically simplify this extension now there's only one config option

On or off. Pick your apples.

Change-Id: I7677f0b597c33f236192aea1aaa347a36216fbb7
This commit is contained in:
James D. Forrester 2017-11-22 11:59:00 -08:00 committed by Catrope
parent 73a84dd453
commit be36244c70
6 changed files with 35 additions and 120 deletions

18
README
View file

@ -1,16 +1,16 @@
# WikiEditor provides enhancements to the MediaWiki edit page
WikiEditor provides enhancements to the MediaWiki edit page
# This extension requires MediaWiki 1.28 or higher.
# Example LocalSettings.php additions
For installation, once the code is copied into your extensions directory, you can load it for your
wiki by adding to LocalSettings.php the line:
wfLoadExtension( 'WikiEditor' );
wfLoadExtensions( "WikiEditor" );
# Before configuring this extension, see WikiEditor.php and become familiar with the initial state and structure of the
# $wgWikiEditorFeatures configuration variable. Essentially it's an array of arrays, keyed by feature name, each
# containing global and user keys with boolean values. "global" indicates that it should be turned on for everyone
# always, while user indicates that users should be allowed to turn it on or off in their user preferences.
By default, when installed this extension will be available to all users, and logged-in users can
disable it from their preferences. If you wish all users to have it, and be unable to disable it,
add it to $wgHiddenPrefs in your LocalSettings.php:
# To enable a preference by default but still allow users to disable it in preferences, use something like...
$wgHiddenPrefs[] = 'usebetatoolbar';
$wgDefaultUserOptions['usebetatoolbar'] = 1;
More can be found on the extension's page: https://www.mediawiki.org/wiki/Extension:WikiEditor

View file

@ -11,71 +11,8 @@ class WikiEditorHooks {
// EventLogging.
private static $statsId = false;
/* Protected Static Members */
protected static $features = [
/* Toolbar Features */
// 'toolbar' is the main wikieditor feature, including toolbars and dialogs.
// The legacy name preserves user preferences for disabling the feature.
'toolbar' => [
'preferences' => [
// Ideally this key would be 'wikieditor-toolbar'
'usebetatoolbar' => [
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-preference',
'section' => 'editing/editor',
],
],
'requirements' => [
'usebetatoolbar' => true,
],
'modules' => [
'ext.wikiEditor',
],
'stylemodules' => [
'ext.wikiEditor.styles',
],
],
];
/* Static Methods */
/**
* Checks if a certain option is enabled
*
* This method is public to allow other extensions that use WikiEditor to use the
* same configuration as WikiEditor itself
*
* @param string $name Name of the feature, should be a key of $features
* @return bool
*/
public static function isEnabled( $name ) {
global $wgWikiEditorFeatures, $wgUser;
// Features with global set to true are always enabled
if ( !isset( $wgWikiEditorFeatures[$name] ) || $wgWikiEditorFeatures[$name]['global'] ) {
return true;
}
// Features with user preference control can have any number of preferences
// to be specific values to be enabled
if ( $wgWikiEditorFeatures[$name]['user'] ) {
if ( isset( self::$features[$name]['requirements'] ) ) {
foreach ( self::$features[$name]['requirements'] as $requirement => $value ) {
// Important! We really do want fuzzy evaluation here
if ( $wgUser->getOption( $requirement ) != $value ) {
return false;
}
}
}
return true;
}
// Features controlled by $wgWikiEditorFeatures with both global and user
// set to false are always disabled
return false;
}
/**
* Log stuff to EventLogging's Schema:Edit - see https://meta.wikimedia.org/wiki/Schema:Edit
* If you don't have EventLogging installed, does nothing.
@ -135,21 +72,16 @@ class WikiEditorHooks {
return true;
}
// Add modules for enabled features
foreach ( self::$features as $name => $feature ) {
if ( !self::isEnabled( $name ) ) {
continue;
}
if ( isset( $feature['stylemodules'] ) ) {
$outputPage->addModuleStyles( $feature['stylemodules'] );
}
if ( isset( $feature['modules'] ) ) {
$outputPage->addModules( $feature['modules'] );
}
}
$article = $editPage->getArticle();
$request = $article->getContext()->getRequest();
// Add modules if enabled
$user = $article->getContext()->getUser();
if ( $user->getOption( 'usebetatoolbar' ) ) {
$outputPage->addModuleStyles( 'ext.wikiEditor.styles' );
$outputPage->addModules( 'ext.wikiEditor' );
}
// Don't run this if the request was posted - we don't want to log 'init' when the
// user just pressed 'Show preview' or 'Show changes', or switched from VE keeping
// changes.
@ -220,7 +152,8 @@ class WikiEditorHooks {
* @return bool
*/
public static function EditPageBeforeEditToolbar( &$toolbar ) {
if ( self::isEnabled( 'toolbar' ) ) {
global $wgUser;
if ( $wgUser->getOption( 'usebetatoolbar' ) ) {
$toolbar = Html::rawElement(
'div', [
'class' => 'wikiEditor-oldToolbar'
@ -244,18 +177,13 @@ class WikiEditorHooks {
* @return bool
*/
public static function getPreferences( $user, &$defaultPreferences ) {
global $wgWikiEditorFeatures;
// Ideally this key would be 'wikieditor-toolbar'
$defaultPreferences['usebetatoolbar'] = [
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-preference',
'section' => 'editing/editor',
];
foreach ( self::$features as $name => $feature ) {
if (
isset( $feature['preferences'] ) &&
( !isset( $wgWikiEditorFeatures[$name] ) || $wgWikiEditorFeatures[$name]['user'] )
) {
foreach ( $feature['preferences'] as $key => $options ) {
$defaultPreferences[$key] = $options;
}
}
}
return true;
}
@ -301,12 +229,7 @@ class WikiEditorHooks {
*/
public static function makeGlobalVariablesScript( &$vars ) {
// Build and export old-style wgWikiEditorEnabledModules object for back compat
$enabledModules = [];
foreach ( self::$features as $name => $feature ) {
$enabledModules[$name] = self::isEnabled( $name );
}
$vars['wgWikiEditorEnabledModules'] = $enabledModules;
$vars['wgWikiEditorEnabledModules'] = [];
return true;
}

View file

@ -364,14 +364,8 @@
"localBasePath": "modules",
"remoteExtPath": "WikiEditor/modules"
},
"config": {
"WikiEditorFeatures": {
"toolbar": {
"global": false,
"user": true
},
"_merge_strategy": "array_plus_2d"
}
"DefaultUserOptions": {
"usebetatoolbar": true
},
"AutoloadClasses": {
"WikiEditorHooks": "WikiEditorHooks.php"

View file

@ -5,7 +5,7 @@
]
},
"wikieditor": "Advanced wikitext editing interface",
"wikieditor-desc": "Provides an extendable wikitext editing interface and many feature-providing modules",
"wikieditor-desc": "Provides an advanced, extensible wikitext editing interface",
"wikieditor-wikitext-tab": "Wikitext",
"wikieditor-loading": "Loading...",
"wikieditor-toolbar": "Editing toolbar",

View file

@ -28,8 +28,8 @@
"Quiddity"
]
},
"wikieditor": "An extension to allow for advanced editing features.\nhttps://www.mediawiki.org/wiki/Extension:WikiEditor",
"wikieditor-desc": "{{desc|name=Wiki Editor|url=https://www.mediawiki.org/wiki/Extension:WikiEditor}}\nI guess that \"feature-providing modules\" means the same as \"modules providing features\".",
"wikieditor": "An extension to provide an advanced, extensible wikitext editing interface.\nhttps://www.mediawiki.org/wiki/Extension:WikiEditor",
"wikieditor-desc": "{{desc|name=WikiEditor|url=https://www.mediawiki.org/wiki/Extension:WikiEditor}}",
"wikieditor-wikitext-tab": "Caption of the tab containing the edit box\n{{Identical|Wikitext}}",
"wikieditor-loading": "Explanatory text for the temporary cover placed over the wikieditor while it's being assembled.\n{{Identical|Loading}}",
"wikieditor-toolbar": "A customizable toolbar for the WikiEditor.\nFor more information, see https://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar_customization",

View file

@ -104,18 +104,16 @@
* is essentially to blacklist rather than whitelist are debatable, but at this point we've decided it's the more
* "open-web" way to go.
*
* @param {Object} module Module object, defaults to $.wikiEditor
* @return {boolean}
*/
isSupported: function ( module ) {
isSupported: function () {
// Fallback to the wikiEditor browser map if no special map is provided in the module
var mod = module && 'browsers' in module ? module : $.wikiEditor;
// Check for and make use of cached value and early opportunities to bail
if ( typeof mod.supported === 'undefined' ) {
if ( typeof $.wikiEditor.supported === 'undefined' ) {
// Run a browser support test and then cache and return the result
mod.supported = $.client.test( mod.browsers );
$.wikiEditor.supported = $.client.test( $.wikiEditor.browsers );
}
return mod.supported;
return $.wikiEditor.supported;
},
/**