Rework extensions setup (v 3.1.0)

* Cleanup the init process of this extension (PHP & JS setup)
* Make it useable for other extensions like MobileFrontend.
* Call new hook CodeMirrorGetAdditionalResources instead of CodeMirrorGetExtensionMode

Bug: T91796
Change-Id: I9763c40835c2edddafb0dcbacdf53a86f663b8cd
This commit is contained in:
Florian Schmidt 2015-03-16 11:00:43 +06:00 committed by Florianschmidtwelzow
parent dd933b665a
commit 3253edba8b
4 changed files with 445 additions and 350 deletions

View file

@ -2,72 +2,180 @@
class CodeMirrorHooks {
static $globalVariableScript = array();
/** @var null|array Cached version of global variables, if available, otherwise null */
private static $globalVariableScript = null;
/** @var null|boolean Saves, if CodeMirror should be loaded on this page or not */
private static $isEnabled = null;
/** @var array values passed from other extensions for use in self::getGlobalVariables() */
private static $extModes = array();
/**
* ResourceLoaderRegisterModules hook handler to conditionally register CodeMirror modules
*
* @global Parser $wgParser
* @global Language $wgContLang
* @param EditPage $editPage
* @param OutputPage $output
* @return boolean
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderRegisterModules
*
* @param ResourceLoader &$resourceLoader The ResourceLoader object
* @return bool Always true
*/
public static function onEditPageShowEditFormInitial( EditPage $editPage, OutputPage $output ) {
global $wgParser, $wgContLang;
public static function onResourceLoaderRegisterModules( ResourceLoader $rl ) {
global $wgCodeMirrorResourceTemplate;
self::$globalVariableScript['ExtMode'] = array(
self::$extModes = array(
'tag' => array(
'pre' => 'mw-tag-pre',
'nowiki' => 'mw-tag-nowiki',
),
'func' => array(),
'data' => array()
'data' => array(),
);
$extResources = array(
'scripts' => array(),
'styles' => array(),
'messages' => array(),
'dependencies' => array( 'ext.CodeMirror.lib' => true ),
);
\wfRunHooks( 'CodeMirrorGetExtensionMode', array( &self::$globalVariableScript['ExtMode'], &$module, &$output ) );
if ( false === isset( $wgParser->mFunctionSynonyms ) ) {
// Check if WikiEditor is installed and add it as a dependency
// FIXME: Is there no better solution doing it?
$resourceModules = $rl->getConfig()->get( 'ResourceModules' );
if ( isset( $resourceModules['ext.wikiEditor'] ) ) {
$extResources['dependencies']['ext.wikiEditor'] = true;
}
// enable other extensions to add additional resources and modes
Hooks::run( 'CodeMirrorGetAdditionalResources', array( &$extResources, &self::$extModes ) );
// Prepare array of resources for ResourceLoader
$codeMirror = array(
'scripts' => array_keys( $extResources['scripts'] ),
'styles' => array_keys( $extResources['styles'] ),
'messages' => array_keys( $extResources['messages'] ),
'dependencies' => array_keys( $extResources['dependencies'] ),
'group' => 'ext.CodeMirror',
) + $wgCodeMirrorResourceTemplate;
$rl->register( array( 'ext.CodeMirror.other' => $codeMirror ) );
return true;
}
/**
* Checks, if CodeMirror should be loaded on this page or not.
*
* @param IContextSource $context The current ContextSource object
* @return boolean
*/
private static function isCodeMirrorEnabled( IContextSource $context ) {
// Check, if we already checked, if page action is editing, if not, do it now
if ( is_null( self::$isEnabled ) ) {
// edit can be 'edit' and 'submit'
self::$isEnabled = in_array(
Action::getActionName( $context ),
array( 'edit', 'submit' )
);
}
return self::$isEnabled;
}
/**
* Returns an array of variables for CodeMirror to work (tags and so on)
*
* @param IContextSource $context The current ContextSource object
* @return array
*/
public static function getGlobalVariables( IContextSource $context ) {
global $wgParser;
// if we already created these variable array, return it
if ( !self::$globalVariableScript ) {
$contObj = $context->getLanguage();
if ( !isset( $wgParser->mFunctionSynonyms ) ) {
$wgParser->initialiseVariables();
$wgParser->firstCallInit();
}
self::$globalVariableScript['Tags'] = array_fill_keys( $wgParser->getTags(), true );
$mw = $wgContLang->getMagicWords();
self::$globalVariableScript['DoubleUnderscore'] = array( array(), array() );
// initialize global vars
$globalVariableScript = array(
'ExtModes' => self::$extModes,
'Tags' => array_fill_keys( $wgParser->getTags(), true ),
'DoubleUnderscore' => array( array(), array() ),
'FunctionSynonyms' => $wgParser->mFunctionSynonyms,
'UrlProtocols' => $wgParser->mUrlProtocols,
'LinkTrailCharacters' => $contObj->linkTrail(),
);
$mw = $contObj->getMagicWords();
foreach ( MagicWord::getDoubleUnderscoreArray()->names as $name ) {
if ( isset( $mw[$name] ) ) {
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
foreach ( $mw[$name] as $n ) {
self::$globalVariableScript['DoubleUnderscore'][$caseSensitive][ $caseSensitive ? $n : $wgContLang->lc( $n ) ] = $name;
$globalVariableScript['DoubleUnderscore'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
}
} else {
self::$globalVariableScript['DoubleUnderscore'][0][] = $name;
$globalVariableScript['DoubleUnderscore'][0][] = $name;
}
}
self::$globalVariableScript['FunctionSynonyms'] = $wgParser->mFunctionSynonyms;
foreach ( MagicWord::getVariableIDs() as $name ) {
if ( isset( $mw[$name] ) ) {
$caseSensitive = array_shift( $mw[$name] ) == 0 ? 0 : 1;
foreach ( $mw[$name] as $n ) {
self::$globalVariableScript['FunctionSynonyms'][$caseSensitive][ $caseSensitive ? $n : $wgContLang->lc( $n ) ] = $name;
$globalVariableScript['FunctionSynonyms'][$caseSensitive][ $caseSensitive ? $n : $contObj->lc( $n ) ] = $name;
}
}
}
self::$globalVariableScript['UrlProtocols'] = $wgParser->mUrlProtocols;// wfUrlProtocolsWithoutProtRel();
// self::$globalVariableScript['LinkTrailCharacters'] = $wgContLang->linkTrail();
$output->addModules( 'ext.CodeMirror.init' );
return true;
}
public static function onMakeGlobalVariablesScript( array &$vars ) {
foreach ( self::$globalVariableScript as $key=> $value ) {
$vars["extCodeMirror$key"] = $value;
// prefix all variables and save it into class variable
foreach ( $globalVariableScript as $key=> $value ) {
self::$globalVariableScript["extCodeMirror$key"] = $value;
}
}
public static function getPreferences( $user, &$defaultPreferences ) {
return self::$globalVariableScript;
}
/**
* MakeGlobalVariablesScript hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
*
* @param ResourceLoader &$resourceLoader The ResourceLoader object
* @return bool Always true
*/
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
$context = $out->getContext();
// add CodeMirror vars only for edit pages
if ( self::isCodeMirrorEnabled( $context ) ) {
$vars += self::getGlobalVariables( $context );
}
}
/**
* BeforePageDisplay hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
*
* @param ResourceLoader &$resourceLoader The ResourceLoader object
* @return bool Always true
*/
public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
if ( self::isCodeMirrorEnabled( $out->getContext() ) ) {
$out->addModules( 'ext.CodeMirror.init' );
}
}
/**
* GetPreferences hook handler
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
*
* @param ResourceLoader &$resourceLoader The ResourceLoader object
* @return bool Always true
*/
public static function onGetPreferences( User $user, &$defaultPreferences ) {
$defaultPreferences['usecodemirror'] = array(
'type' => 'api',
'default' => '1',

View file

@ -15,7 +15,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}
const EXT_CODEMIRROR_VERSION = '3.0.1';
const EXT_CODEMIRROR_VERSION = '3.1.0';
// Register this extension on Special:Version
$wgExtensionCredits['parserhook'][] = array(
@ -23,7 +23,7 @@ $wgExtensionCredits['parserhook'][] = array(
'name' => 'CodeMirror',
'version' => EXT_CODEMIRROR_VERSION,
'url' => 'https://www.mediawiki.org/wiki/Extension:CodeMirror',
'author' => '[https://www.mediawiki.org/wiki/User:Pastakhov Pavel Astakhov]',
'author' => array( '[https://www.mediawiki.org/wiki/User:Pastakhov Pavel Astakhov]', 'Florian Schmidt' ),
'descriptionmsg' => 'codemirror-desc'
);
@ -33,60 +33,41 @@ $wgExtensionMessagesFiles['CodeMirror'] = __DIR__ . '/CodeMirror.i18n.php';
$wgAutoloadClasses['CodeMirrorHooks'] = __DIR__ . '/CodeMirror.hooks.php';
$wgHooks['EditPage::showEditForm:initial'][] = 'CodeMirrorHooks::onEditPageShowEditFormInitial';
//$wgHooks['EditPage::showReadOnlyForm:initial'][] = 'CodeMirrorHooks::onEditPageShowEditFormInitial';
$wgHooks['MakeGlobalVariablesScript'][] = 'CodeMirrorHooks::onMakeGlobalVariablesScript';
$wgHooks['GetPreferences'][] = 'CodeMirrorHooks::getPreferences';
$wgHooks['BeforePageDisplay'][] = 'CodeMirrorHooks::onBeforePageDisplay';
$wgHooks['GetPreferences'][] = 'CodeMirrorHooks::onGetPreferences';
$wgHooks['ResourceLoaderRegisterModules'][] = 'CodeMirrorHooks::onResourceLoaderRegisterModules';
$wgHooks['ResourceLoaderRegisterModules'][] = function () {
global $wgResourceModules, $wgCodeMirrorResources;
if ( isset($wgResourceModules['ext.wikiEditor']) ) {
$wgCodeMirrorResources['dependencies']['ext.wikiEditor'] = true;
}
if ( isset( $wgCodeMirrorResources['scripts'] ) ) {
$wgResourceModules['ext.CodeMirror.other']['scripts'] = array_keys( $wgCodeMirrorResources['scripts'] );
}
if ( isset( $wgCodeMirrorResources['styles'] ) ) {
$wgResourceModules['ext.CodeMirror.other']['styles'] = array_keys( $wgCodeMirrorResources['styles'] );
}
if ( isset( $wgCodeMirrorResources['messages'] ) ) {
$wgResourceModules['ext.CodeMirror.other']['messages'] = array_keys( $wgCodeMirrorResources['messages'] );
}
if ( isset( $wgCodeMirrorResources['dependencies'] ) ) {
$wgResourceModules['ext.CodeMirror.other']['dependencies'] = array_keys( $wgCodeMirrorResources['dependencies'] );
}
};
$tpl = array(
$wgCodeMirrorResourceTemplate = array(
'localBasePath' => __DIR__ . '/resources',
'remoteExtPath' => 'CodeMirror/resources',
);
$wgResourceModules['ext.CodeMirror.init'] = array(
'group' => 'ext.CodeMirror',
'scripts' => 'ext.CodeMirror.js',
'dependencies' => array( 'ext.CodeMirror.lib', 'ext.CodeMirror.other' ),
) + $tpl;
$wgResourceModules['ext.CodeMirror.lib'] = array(
$wgResourceModules['ext.CodeMirror.init'] = $wgCodeMirrorResourceTemplate + array(
'dependencies' => array(
'ext.CodeMirror.lib',
'ext.CodeMirror.other',
'mediawiki.api',
'jquery.textSelection',
'user.options',
),
'scripts' => array(
'ext.CodeMirror.js'
),
'group' => 'ext.CodeMirror',
);
$wgResourceModules['ext.CodeMirror.lib'] = $wgCodeMirrorResourceTemplate + array(
'scripts' => array(
'lib/codemirror/lib/codemirror.js',
'lib/codemirror/addon/selection/active-line.js',
'mode/mediawiki/mediawiki.js',
//'mode/mediawiki/matchMW.js',
),
'styles' => array(
'lib/codemirror/lib/codemirror.css',
'lib/codemirror/addon/lint/lint.css',
'mode/mediawiki/mediawiki.css',
),
) + $tpl;
$wgResourceModules['ext.CodeMirror.other'] = array(
'group' => 'ext.CodeMirror',
) + $tpl;
if ( false === isset( $wgCodeMirrorResources ) ) {
$wgCodeMirrorResources = array();
}
$wgCodeMirrorResources['dependencies']['ext.CodeMirror.lib'] = true;
'targets' => array( 'mobile', 'desktop' ),
);

View file

@ -1,11 +1,13 @@
/* global CodeMirror, mw, $ */
var initExtCodeMirror = function() {
var origTextSelection = $.fn.textSelection, codeMirror = false;
// Replace jquery.textSelection.js
var cmTextSelection = function ( command, options ) {
if ( ! codeMirror ) {
/* global CodeMirror, mediaWiki */
( function ( mw, $ ) {
// codeMirror needs a special textselection jQuery function to work, save the current one to restore when
// CodeMirror get's disabled.
var origTextSelection = $.fn.textSelection,
codeMirror = mw.user.options.get( 'usecodemirror' ) === '1' || mw.user.options.get( 'usecodemirror' ) === 1,
api = new mw.Api(),
// function for a textselection function for CodeMirror
cmTextSelection = function ( command, options ) {
if ( !codeMirror ) {
return origTextSelection( command, options );
}
var fn, retval;
@ -197,25 +199,11 @@ var initExtCodeMirror = function() {
codeMirror.focus();
return retval;
};
function setCodeEditorPreference( prefValue ) {
var api = new mw.Api();
api.postWithToken( 'options', {
action: 'options',
optionname: 'usecodemirror',
optionvalue: prefValue ? 1 : 0
} ).fail( function ( code, result ) {
mw.log.error( 'Failed to set code editor preference: ' + code + '\n' + result.error );
} );
}
if ( mw.user.options.get( 'usecodemirror' ) === '1' || mw.user.options.get( 'usecodemirror' ) === 1 ) {
codeMirror = true;
}
function addCodeMirrorToWikiEditor() {
(function waitWikiEditor() {
},
/**
* Adds the CodeMirror button to WikiEditor
*/
addCodeMirrorToWikiEditor = function () {
if ( $( '#wikiEditor-section-main' ).length > 0 ) {
$( '#wpTextbox1' ).wikiEditor(
'addToToolbar',
@ -223,40 +211,116 @@ var initExtCodeMirror = function() {
'section': 'main',
'groups': {
'codemirror':{
// 'label': 'CodeMirror',
'tools': {
'CodeMirror': {
label: 'CodeMirror',
type: 'button',
icon: mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/cm-' + (codeMirror ? 'on.png' : 'off.png'),
action: {type: 'callback', execute: function( context ){ switchCodeMirror( context ); } }
// },
// 'Undo':{
// label: 'Undo',
// type: 'button',
// icon: mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/undo.png',
// action: {type: 'callback', execute: function(context){alert(context);} }
// },
// 'Redo':{
// label: 'Redo',
// type: 'button',
// icon: mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/redo.png',
// action: {type: 'callback', execute: function(context){alert(context);} }
// FIXME: There should be a better way?
icon: mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/cm-' + ( codeMirror ? 'on.png' : 'off.png' ),
action: {
type: 'callback',
execute: function( context ) {
switchCodeMirror( context );
}
}
}
}
}
}
}
);
} else {
setTimeout( waitWikiEditor, 500 );
}
})();
};
/**
* Save CodeMirror enabled pref.
*
* @param {Boolean} prefValue True, if CodeMirror should be enabled by default, otherwise false.
*/
function setCodeEditorPreference( prefValue ) {
api.postWithToken( 'options', {
action: 'options',
optionname: 'usecodemirror',
optionvalue: prefValue ? 1 : 0
} ).fail( function ( code, result ) {
// FIXME: Should this throw an user visible error message?
mw.log.warn( 'Failed to set code editor preference: ' + code + '\n' + result.error );
} );
}
if ( $( '#wpTextbox1' ).wikiEditor && mw.user.options.get( 'usebetatoolbar' ) === 1 && mw.user.options.get( 'showtoolbar' ) === 1 ) {
addCodeMirrorToWikiEditor();
/**
* Enables or disables CodeMirror
*
* @param {undefined} context Doc needed
*/
function switchCodeMirror( context ) {
var $img, $src;
if ( context !== false ) {
$img = context.modules.toolbar.$toolbar.find( 'img.tool[rel=CodeMirror]' );
} else {
$img = $( '#CodeMirrorButton' );
}
if ( codeMirror ) {
setCodeEditorPreference( false );
codeMirror.save();
codeMirror.toTextArea();
codeMirror = false;
$.fn.textSelection = origTextSelection;
$src = mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/' + ( context ? 'cm-off.png' : 'old-cm-off.png' );
$img.attr( 'src', $src );
} else {
enableCodeMirror();
$src = mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/' + ( context ? 'cm-on.png' : 'old-cm-on.png' );
$img.attr( 'src', $src );
setCodeEditorPreference( true );
}
}
/**
* Replaces the default textarea with CodeMirror
*/
function enableCodeMirror() {
var textbox1 = $( '#wpTextbox1' );
if ( textbox1[0].style.display === 'none' ) {
return;
}
codeMirror = CodeMirror.fromTextArea( textbox1[0], {
mwextFunctionSynonyms: mw.config.get( 'extCodeMirrorFunctionSynonyms' ),
mwextTags: mw.config.get( 'extCodeMirrorTags' ),
mwextDoubleUnderscore: mw.config.get( 'extCodeMirrorDoubleUnderscore' ),
mwextUrlProtocols: mw.config.get( 'extCodeMirrorUrlProtocols' ),
mwextModes: mw.config.get( 'extCodeMirrorExtModes' ),
styleActiveLine: true,
lineWrapping: true,
// select mediawiki as text input mode
mode: 'text/mediawiki'
} );
// Our best friend, IE, needs some special css
if ( window.navigator.userAgent.indexOf('Trident/') > -1 ) {
$( '.CodeMirror' ).addClass( 'CodeMirrorIE' );
}
// set the hight of the textarea
codeMirror.setSize( null, textbox1.height() );
// Overwrite default textselection of WikiEditor to work with CodeMirror, too
$.fn.textSelection = cmTextSelection;
}
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
// This function shouldn't be called without user.options is loaded, but it's not guaranteed
mw.loader.using( 'user.options', function () {
// This can be the string "0" if the user disabled the preference - Bug T54542#555387
if ( mw.user.options.get( 'usebetatoolbar' ) === 1 || mw.user.options.get( 'usebetatoolbar' ) === '1' ) {
// load wikiEditor's toolbar (if not already) and add our button
$.when(
mw.loader.using( 'ext.wikiEditor.toolbar' ), $.ready
).then( addCodeMirrorToWikiEditor );
} else {
// If WikiEditor isn't enabled, add CodeMirror button to the default wiki editor toolbar
var $image = $( '<img>' ).attr( {
width: 23,
height: 22,
@ -272,69 +336,11 @@ var initExtCodeMirror = function() {
$( '#toolbar' ).append( $image );
}
function switchCodeMirror( context ) {
//alert( 'switchCodeMirror: ' + codeMirror );
var $img, $src;
if ( context !== false ) {
$img = context.modules.toolbar.$toolbar.find( 'img.tool[rel=CodeMirror]' );
} else {
$img = $( '#CodeMirrorButton' );
}
if ( codeMirror ) {
setCodeEditorPreference( false );
codeMirror.save();
codeMirror.toTextArea();
codeMirror = false;
$.fn.textSelection = origTextSelection;
$src = mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/' + (context ? 'cm-off.png' : 'old-cm-off.png' );
$img.attr( 'src', $src );
} else {
enableCodeMirror();
$src = mw.config.get( 'wgExtensionAssetsPath' ) + '/CodeMirror/resources/images/' + (context ? 'cm-on.png' : 'old-cm-on.png' );
$img.attr( 'src', $src );
setCodeEditorPreference( true );
}
}
function enableCodeMirror() {
var textbox1 = $( '#wpTextbox1' );
if ( textbox1[0].style.display === 'none' ) {
return;
}
codeMirror = CodeMirror.fromTextArea( textbox1[0], {
mwextFunctionSynonyms: mw.config.get( 'extCodeMirrorFunctionSynonyms' ),
mwextTags: mw.config.get( 'extCodeMirrorTags' ),
mwextDoubleUnderscore: mw.config.get( 'extCodeMirrorDoubleUnderscore' ),
mwextUrlProtocols: mw.config.get( 'extCodeMirrorUrlProtocols' ),
mwextMode: mw.config.get( 'extCodeMirrorExtMode' ),
//matchMW: true,
styleActiveLine: true,
//gutters: ['CodeMirror-mediawiki-gutter'],
//lint: true,
lineWrapping: true,
//indentUnit: 4,
//indentWithTabs: true,
//matchBrackets: true,
//autoCloseBrackets: true,
mode: 'text/mediawiki'
} );
if ( window.navigator.userAgent.indexOf('Trident/') > -1 ) { //IE specific code goes here
$( '.CodeMirror' ).addClass( 'CodeMirrorIE' );
}
codeMirror.setSize( null, textbox1.height() );
$.fn.textSelection = cmTextSelection;
}
// enable CodeMirror
if ( codeMirror ) {
enableCodeMirror();
}
};
$.when(
$.ready,
mw.loader.using( ['user.options', 'jquery.textSelection', 'mediawiki.api'] )
).done( initExtCodeMirror() );
}( mediaWiki, jQuery ) );

View file

@ -433,8 +433,8 @@ CodeMirror.defineMode( 'mediawiki', function( config/*, parserConfig */ ) {
}
if ( stream.eat( '>' ) ) {
state.extName = name;
if ( name in config.mwextMode.tag ) {
state.extMode = CodeMirror.getMode( config, config.mwextMode.tag[name] );
if ( name in config.mwextModes.tag ) {
state.extMode = CodeMirror.getMode( config, config.mwextModes.tag[name] );
state.extState = CodeMirror.startState( state.extMode );
}
state.tokenize = eatExtTagArea( name );