Revert r37263 for now:

* (bug 12211) Show some gadgets only for admins
	* (bug 13742) Allow for gadgets to be turned on by default

I'm a bit leery of the 'on by default' entirely at the moment. :)

A few comments:
* The readme examples don't seem to clearly show the option format
* Why are numeric constants being used as indexes to the option array? Strings are easier to work with and debug.
* There's a lot of stuff like this which feels very ugly:
	if( isset( $gadget->options[Gadget::RIGHTS] ) && !empty( $gadget->options[Gadget::RIGHTS] ) ) {
  Since it's all hard-coded anyway, why not just do something nice and clear like this?
	if( !empty( $gadget->rights ) ) {
* And this:
    if( wfGadgetAllowed( $gadget->options ) ) {
  to:
    if( $gadget->isAllowed() ) {
This commit is contained in:
Brion Vibber 2008-07-07 18:50:47 +00:00
parent 90d18f24e2
commit 740da4c34d
4 changed files with 28 additions and 132 deletions

View file

@ -32,8 +32,6 @@ Local administrators can edit available gadgets using [[MediaWiki:Gadgets-defini
'gadgets-pagetext' => "Below is a list of special gadgets users can enable on their preferences page, as defined by [[MediaWiki:Gadgets-definition]].
This overview provides easy access to the system message pages that define each gadget's description and code.",
'gadgets-uses' => 'Uses',
'gadgets-rights' => 'Requires the following rights',
'gadgets-default' => 'Enabled by default',
);
/** Amharic (አማርኛ)
@ -899,6 +897,7 @@ Esta visão geral proporciona um acesso fácil para as mensagens de sistema que
* @author Illusion
* @author Siebrand
* @author Александр Сигачёв
* @author Ahonc
*/
$messages['ru'] = array(
'gadgets-desc' => 'Позволяет участникам выбирать в [[Special:Preferences|настройках]] CSS- и JavaScript-гаджеты, которые они хотят подключить',
@ -1164,4 +1163,3 @@ $messages['zh-hant'] = array(
'gadgets-uses' => '使用',
);

View file

@ -38,23 +38,6 @@ $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php';
$wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
$wgSpecialPages['Gadgets'] = 'SpecialGadgets';
class Gadget {
const CACHE_VERSION = 1;
/**
* This gadget option contains rights the user must have to use this gadget
*/
const RIGHTS = 666;
/**
* This gadget should be enabled by default
*/
const ON_BY_DEFAULT = 667;
var $name = '';
var $modules = array();
var $options = array();
}
function wfGadgetsArticleSaveComplete( &$article, &$wgUser, &$text ) {
//update cache if MediaWiki:Gadgets-definition was edited
$title = $article->mTitle;
@ -94,10 +77,7 @@ function wfLoadGadgetsStructured( $forceNewText = NULL ) {
if ( $forceNewText === NULL ) {
//cached?
$gadgets = $wgMemc->get( $key );
if ( is_array( $gadgets ) && isset( $gadgets['version'] ) && $gadgets['version'] == Gadget::CACHE_VERSION ) {
unset( $gadgets['version'] );
return $gadgets;
}
if ( is_array($gadgets) ) return $gadgets;
$g = wfMsgForContentNoTrans( "gadgets-definition" );
if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
@ -117,48 +97,29 @@ function wfLoadGadgetsStructured( $forceNewText = NULL ) {
foreach ( $g as $line ) {
if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
$section = $m[1];
} else if ( preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\s*((\|[^|[]*)+)\s*(|\[(.*)\])\s*$/', $line, $m ) ) {
$gadget = new Gadget();
}
else if ( preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\s*((\|[^|]*)+)\s*$/', $line, $m ) ) {
//NOTE: the gadget name is used as part of the name of a form field,
// and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata
// Also, title-normalization applies.
$gadget->name = str_replace(' ', '_', $m[1] );
$name = str_replace(' ', '_', $m[1] );
$gadget->modules = preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY );
$code = preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY );
if( !empty( $m[5] ) && $gadget->modules )
$gadget->options = wfProcessGadgetOptions( $m[5] );
if ( $gadget->modules ) {
$gadgets[$section][$gadget->name] = $gadget;
if ( $code ) {
$gadgets[$section][$name] = $code;
}
}
}
//cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
$gadgets['version'] = Gadget::CACHE_VERSION;
$wgMemc->set( $key, $gadgets, 60*60*24 );
$source = $forceNewText !== NULL ? 'input text' : 'MediaWiki:Gadgets-definition';
wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
unset( $gadgets['version'] );
return $gadgets;
}
function wfProcessGadgetOptions( $options ) {
$out = array();
foreach( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
if( preg_match( '/^rights\s*:\s*([\w\s,]+)$/', $option, $m ) ) {
$out[Gadget::RIGHTS] = preg_split( '/\s*,\s*/', $m[1], -1, PREG_SPLIT_NO_EMPTY );
} elseif( preg_match( '/^default$/', $option ) ) {
$out[Gadget::ON_BY_DEFAULT] = true;
}
}
return $out;
}
function wfGadgetsInitPreferencesForm( &$prefs, &$request ) {
$gadgets = wfLoadGadgets();
if ( !$gadgets ) return true;
@ -171,18 +132,6 @@ function wfGadgetsInitPreferencesForm( &$prefs, &$request ) {
return true;
}
function wfGadgetAllowed( $options ) {
global $wgUser;
if( isset( $options[Gadget::RIGHTS] ) )
foreach( $options[Gadget::RIGHTS] as $right ) {
if( !in_array( $right, $wgUser->getRights() ) )
return FALSE;
}
return TRUE;
}
function wfGadgetsResetPreferences( &$prefs, &$user ) {
$gadgets = wfLoadGadgets();
if ( !$gadgets ) return true;
@ -196,8 +145,6 @@ function wfGadgetsResetPreferences( &$prefs, &$user ) {
}
function wfGadgetsRenderPreferencesForm( &$prefs, &$out ) {
global $wgUser;
$gadgets = wfLoadGadgetsStructured();
if ( !$gadgets ) return true;
@ -210,39 +157,22 @@ function wfGadgetsRenderPreferencesForm( &$prefs, &$out ) {
$msgOpt = array( 'parseinline' );
foreach ( $gadgets as $section => $entries ) {
$first = true;
foreach ( $entries as $gname => $gadget ) {
$tname = "gadget-$gname";
$ttext = wfMsgExt( $tname, $msgOpt );
if( ( ( $prefs->mToggles[$tname] != '' ) && ($prefs->mToggles[$tname] == 1) )
|| ( ( $prefs->mToggles[$tname] === '' ) && isset( $gadget->options[Gadget::ON_BY_DEFAULT] ) ) )
$checked = ' checked="checked"';
else
$checked = '';
$disabled = '';
if( wfGadgetAllowed( $gadget->options ) ) {
// draw section heading
if ($first && $section !== false && $section !== '' ) {
$stext = wfMsgExt( "gadget-section-$section", $msgOpt );
$out->addHtml( "\n<h2 id=\"".htmlspecialchars("gadget-section-$section")."\">" . $stext . "</h2>\n" );
$first = false;
if ( $section !== false && $section !== '' ) {
$ttext = wfMsgExt( "gadget-section-$section", $msgOpt );
$out->addHtml( "\n<h2 id=\"".htmlspecialchars("gadget-section-$section")."\">" . $ttext . "</h2>\n" );
}
if( isset( $gadget->options[Gadget::ON_BY_DEFAULT] ) )
$extra = ' (' . wfMsg( 'gadgets-default' ) . ')';
else
$extra = '';
foreach ( $entries as $gname => $code ) {
$tname = "gadget-$gname";
$ttext = wfMsgExt( $tname, $msgOpt );
$checked = @$prefs->mToggles[$tname] == 1 ? ' checked="checked"' : '';
$disabled = '';
# NOTE: No label for checkmarks as this causes the checks to toggle
# when clicking a link in the describing text.
$out->addHtml( "<div class='toggle'><input type='checkbox' value='1' " .
"id=\"$tname\" name=\"wpOp$tname\"$checked$disabled />" .
" <span class='toggletext'>$ttext</span>$extra</div>\n" );
}
" <span class='toggletext'>$ttext</span></div>\n" );
}
}
@ -266,13 +196,10 @@ function wfGadgetsBeforePageDisplay( &$out ) {
$done = array();
foreach ( $gadgets as $gname => $gadget ) {
if( wfGadgetAllowed( $gadget->options ) ) {
foreach ( $gadgets as $gname => $code ) {
$tname = "gadget-$gname";
$default = isset( $gadget->options[Gadget::ON_BY_DEFAULT] );
if ( $wgUser->getOption( $tname, $default ) ) {
wfApplyGadgetCode( $gadget->modules, $out, $done );
}
if ( $wgUser->getOption( $tname ) ) {
wfApplyGadgetCode( $code, $out, $done );
}
}

22
README
View file

@ -33,7 +33,7 @@ messages, for easy editing.
Each line in MediaWiki:Gadgets-definition that start with one or more "*"
(asterisk) characters defines a gadget; it must have the following form:
* mygadget|mygadget.js|mygadget.css[option1|option2]
* mygadget|mygadget.js|mygadget.css
That is, each line consists of fields separated by a "|" (pipe) character.
The first field ("mygadget" in the example) is the gadgets internal name,
@ -65,26 +65,6 @@ section's name - for example:
This would define a new section, with the title defined on the page
MediaWiki:Gadget-section-editing-gadgets
== Options ==
Options are optionally appended to the end of the gadget declaration.
They are enclosed in square brackets ("[...]") and are separated by pipe
character ("|").
Currently, there are two options: rights required to use the gadget, and
whether the gadget should be enaled for everyone by default.
The rights option looks like "[rights:delete,userrights]". Note that it
requires specific rights, not groups the user belongs to. This option is
case-sensitive, but ignores whitespace, i.e. "[ rights: foo , bar ]" is a
valid declaration, but not "[Rights:Foo]".
The "default" option, if present, makes this gadget enabled for every
eligible user who has not specifically opted out of it by unchecking it in
their preferences. The option looks like that: "[default]".
Options may be combined in any number or order, i.e. "[rights:foo, bar]",
"[default]", "[rights:boz|default]" and "[default|rights:quux]" are all valid
declarations.
== Caveats ==

View file

@ -59,7 +59,7 @@ class SpecialGadgets extends SpecialPage {
$wgOut->addHTML( "\n<h2>$ttext &nbsp; &nbsp; [$lnk]</h2>\n" );
}
foreach ( $entries as $gname => $gadget ) {
foreach ( $entries as $gname => $code ) {
$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$gname" );
if ( !$t ) continue;
@ -73,19 +73,10 @@ class SpecialGadgets extends SpecialPage {
$wgOut->addHTML( "<li>" );
$wgOut->addHTML( "$ttext &nbsp; &nbsp; [$lnk]<br />" );
if( isset( $gadget->options[Gadget::RIGHTS] ) && !empty( $gadget->options[Gadget::RIGHTS] ) ) {
$wgOut->addHTML( wfMsgHTML( 'gadgets-rights' ) . ": " );
$wgOut->addHTML( htmlspecialchars( implode( ', ', $gadget->options[Gadget::RIGHTS] ) ) . '<br />' );
}
if( isset( $gadget->options[Gadget::ON_BY_DEFAULT] ) )
$wgOut->addHTML( wfMsgHTML( "gadgets-default" ) . "<br />" );
$wgOut->addHTML( wfMsgHTML("gadgets-uses") . ": " );
$first = true;
foreach ( $gadget->modules as $codePage ) {
foreach ( $code as $codePage ) {
$t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$codePage" );
if ( !$t ) continue;