mediawiki-extensions-WikiEd.../WikiEditor.hooks.php

330 lines
8 KiB
PHP
Raw Normal View History

<?php
/**
* Hooks for WikiEditor extension
2011-09-13 08:56:32 +00:00
*
* @file
* @ingroup Extensions
*/
class WikiEditorHooks {
2011-09-13 08:56:32 +00:00
/* Protected Static Members */
2011-09-13 08:56:32 +00:00
protected static $features = array(
2011-09-13 08:56:32 +00:00
/* Beta Features */
2011-09-13 08:56:32 +00:00
'toolbar' => array(
'preferences' => array(
// Ideally this key would be 'wikieditor-toolbar'
'usebetatoolbar' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-preference',
'section' => 'editing/beta',
),
),
'requirements' => array(
'usebetatoolbar' => true,
),
'modules' => array(
'ext.wikiEditor.toolbar',
),
),
'dialogs' => array(
'preferences' => array(
// Ideally this key would be 'wikieditor-toolbar-dialogs'
'usebetatoolbar-cgd' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-dialogs-preference',
'section' => 'editing/beta',
),
),
'requirements' => array(
'usebetatoolbar-cgd' => true,
'usebetatoolbar' => true,
),
'modules' => array(
'ext.wikiEditor.dialogs',
),
),
'hidesig' => array(
'preferences' => array(
'wikieditor-toolbar-hidesig' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-toolbar-hidesig',
'section' => 'editing/beta',
),
),
'requirements' => array(
'wikieditor-toolbar-hidesig' => true,
'usebetatoolbar' => true,
),
'modules' => array(
'ext.wikiEditor.toolbar.hideSig',
),
),
2011-09-13 08:56:32 +00:00
/* Labs Features */
2011-09-13 08:56:32 +00:00
'templateEditor' => array(
'preferences' => array(
'wikieditor-template-editor' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-template-editor-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'wikieditor-template-editor' => true,
),
'modules' => array(
'ext.wikiEditor.templateEditor',
),
),
'templates' => array(
'preferences' => array(
'wikieditor-templates' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-templates-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'wikieditor-templates' => true,
),
'modules' => array(
'ext.wikiEditor.templates',
),
),
'preview' => array(
'preferences' => array(
'wikieditor-preview' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-preview-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'wikieditor-preview' => true,
),
'modules' => array(
'ext.wikiEditor.preview',
),
),
'previewDialog' => array(
'preferences' => array(
'wikieditor-previewDialog' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-previewDialog-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'wikieditor-previewDialog' => true,
),
'modules' => array(
'ext.wikiEditor.previewDialog',
),
),
'publish' => array(
'preferences' => array(
'wikieditor-publish' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-publish-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'wikieditor-publish' => true,
),
'modules' => array(
'ext.wikiEditor.publish',
),
),
'toc' => array(
'preferences' => array(
// Ideally this key would be 'wikieditor-toc'
'usenavigabletoc' => array(
'type' => 'toggle',
'label-message' => 'wikieditor-toc-preference',
'section' => 'editing/labs',
),
),
'requirements' => array(
'usenavigabletoc' => true,
),
'modules' => array(
'ext.wikiEditor.toc',
),
),
);
2011-09-13 08:56:32 +00:00
/* Static Methods */
2011-09-13 08:56:32 +00:00
/**
* 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 $name string Name of the feature, should be a key of $features
* @return bool
*/
public static function isEnabled( $name ) {
global $wgWikiEditorFeatures, $wgUser;
2011-09-13 08:56:32 +00:00
// 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;
}
2011-09-13 08:56:32 +00:00
// Features controlled by $wgWikiEditorFeatures with both global and user set to false are awlways disabled
return false;
}
2011-09-13 08:56:32 +00:00
/**
* EditPage::showEditForm:initial hook
2011-09-13 08:56:32 +00:00
*
* Adds the modules to the edit form
2011-09-13 08:56:32 +00:00
*
* @param $toolbar array list of toolbar items
* @return bool
*/
public static function editPageShowEditFormInitial( &$toolbar ) {
global $wgOut;
2011-09-13 08:56:32 +00:00
// Add modules for enabled features
foreach ( self::$features as $name => $feature ) {
if ( isset( $feature['modules'] ) && self::isEnabled( $name ) ) {
$wgOut->addModules( $feature['modules'] );
}
}
return true;
}
2011-09-13 08:56:32 +00:00
/**
* EditPageBeforeEditToolbar hook
*
* Disable the old toolbar if the new one is enabled
*
* @param $toolbar html
* @return bool
*/
public static function EditPageBeforeEditToolbar( &$toolbar ) {
if ( self::isEnabled( 'toolbar' ) ) {
$toolbar = Html::rawElement(
'div', array(
'class' => 'wikiEditor-oldToolbar',
'style' => 'display:none;'
),
$toolbar
);
}
return true;
}
/**
* GetPreferences hook
2011-09-13 08:56:32 +00:00
*
* Adds WikiEditor-releated items to the preferences
2011-09-13 08:56:32 +00:00
*
* @param $user User current user
* @param $defaultPreferences array list of default user preference controls
* @return bool
*/
public static function getPreferences( $user, &$defaultPreferences ) {
global $wgWikiEditorFeatures;
2011-09-13 08:56:32 +00:00
foreach ( self::$features as $name => $feature ) {
if (
isset( $feature['preferences'] ) &&
( !isset( $wgWikiEditorFeatures[$name] ) || $wgWikiEditorFeatures[$name]['user'] )
) {
2010-10-01 19:14:33 +00:00
foreach ( $feature['preferences'] as $key => $options ) {
$defaultPreferences[$key] = $options;
}
}
}
return true;
}
2011-09-13 08:56:32 +00:00
/**
* MakeGlobalVariablesScript hook
2011-09-13 08:56:32 +00:00
*
* Adds enabled/disabled switches for WikiEditor modules
* @param $vars array
* @return bool
*/
public static function resourceLoaderGetConfigVars( &$vars ) {
global $wgWikiEditorFeatures;
2011-09-13 08:56:32 +00:00
$configurations = array();
foreach ( self::$features as $name => $feature ) {
if (
isset( $feature['configurations'] ) &&
( !isset( $wgWikiEditorFeatures[$name] ) || self::isEnabled( $name ) )
) {
foreach ( $feature['configurations'] as $configuration ) {
global $$configuration;
$configurations[$configuration] = $$configuration;
}
}
}
if ( count( $configurations ) ) {
$vars = array_merge( $vars, $configurations );
}
//expose magic words for use by the wikieditor toolbar
WikiEditorHooks::getMagicWords( $vars );
return true;
}
2011-09-13 08:56:32 +00:00
/**
* @param $vars array
* @return bool
*/
public static function makeGlobalVariablesScript( &$vars ) {
// Build and export old-style wgWikiEditorEnabledModules object for back compat
$enabledModules = array();
foreach ( self::$features as $name => $feature ) {
$enabledModules[$name] = self::isEnabled( $name );
}
2011-09-13 08:56:32 +00:00
$vars['wgWikiEditorEnabledModules'] = $enabledModules;
return true;
}
/**
* Expose useful magic words which are used by the wikieditor toolbar
* @param $vars array
* @return bool
*/
private static function getMagicWords( &$vars ){
$requiredMagicWords = array(
'redirect',
'img_right',
'img_left',
'img_none',
'img_center',
'img_thumbnail',
'img_framed',
'img_frameless',
);
foreach ( $requiredMagicWords as $name ) {
$magicWords[$name] = MagicWord::get( $name )->getSynonym( 0 );
}
$vars['wgWikiEditorMagicWords'] = $magicWords;
}
}