diff --git a/Gadgets.i18n.php b/Gadgets.i18n.php index 78061082..e396a26c 100644 --- a/Gadgets.i18n.php +++ b/Gadgets.i18n.php @@ -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' => '使用', ); - diff --git a/Gadgets.php b/Gadgets.php index 4366e704..1bedf0d1 100644 --- a/Gadgets.php +++ b/Gadgets.php @@ -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 ); + $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; + if ( $section !== false && $section !== '' ) { + $ttext = wfMsgExt( "gadget-section-$section", $msgOpt ); + $out->addHtml( "\n

" . $ttext . "

\n" ); + } - foreach ( $entries as $gname => $gadget ) { + foreach ( $entries as $gname => $code ) { $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 = ''; - + $checked = @$prefs->mToggles[$tname] == 1 ? ' checked="checked"' : ''; $disabled = ''; - if( wfGadgetAllowed( $gadget->options ) ) { - // draw section heading - if ($first && $section !== false && $section !== '' ) { - $stext = wfMsgExt( "gadget-section-$section", $msgOpt ); - $out->addHtml( "\n

" . $stext . "

\n" ); - $first = false; - } - - if( isset( $gadget->options[Gadget::ON_BY_DEFAULT] ) ) - $extra = ' (' . wfMsg( 'gadgets-default' ) . ')'; - else - $extra = ''; - - # NOTE: No label for checkmarks as this causes the checks to toggle - # when clicking a link in the describing text. - $out->addHtml( "
" . - " $ttext$extra
\n" ); - } + # NOTE: No label for checkmarks as this causes the checks to toggle + # when clicking a link in the describing text. + $out->addHtml( "
" . + " $ttext
\n" ); } } @@ -266,13 +196,10 @@ function wfGadgetsBeforePageDisplay( &$out ) { $done = array(); - foreach ( $gadgets as $gname => $gadget ) { - if( wfGadgetAllowed( $gadget->options ) ) { - $tname = "gadget-$gname"; - $default = isset( $gadget->options[Gadget::ON_BY_DEFAULT] ); - if ( $wgUser->getOption( $tname, $default ) ) { - wfApplyGadgetCode( $gadget->modules, $out, $done ); - } + foreach ( $gadgets as $gname => $code ) { + $tname = "gadget-$gname"; + if ( $wgUser->getOption( $tname ) ) { + wfApplyGadgetCode( $code, $out, $done ); } } diff --git a/README b/README index b06e3ba1..de688277 100644 --- a/README +++ b/README @@ -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 == diff --git a/SpecialGadgets.php b/SpecialGadgets.php index 7a605787..2993b1ad 100644 --- a/SpecialGadgets.php +++ b/SpecialGadgets.php @@ -59,33 +59,24 @@ class SpecialGadgets extends SpecialPage { $wgOut->addHTML( "\n

$ttext     [$lnk]

\n" ); } - foreach ( $entries as $gname => $gadget ) { + foreach ( $entries as $gname => $code ) { $t = Title::makeTitleSafe( NS_MEDIAWIKI, "Gadget-$gname" ); if ( !$t ) continue; $lnk = $skin->makeLinkObj( $t, wfMsgHTML("edit"), 'action=edit' ); $ttext = wfMsgExt( "gadget-$gname", $msgOpt ); - + if( !$listOpen ) { $listOpen = true; $wgOut->addHTML( '