r85902 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85901‎ | r85902 | r85903 >
Date:18:09, 12 April 2011
Author:maxsem
Status:ok (Comments)
Tags:
Comment:
Bug 13742 - Allow for gadgets to be turned on by default.
Because I'm lazy to do everything twice, README now points to on-wiki docs for definition format.
Modified paths:
  • /trunk/extensions/Gadgets/Gadgets.i18n.php (modified) (history)
  • /trunk/extensions/Gadgets/Gadgets_body.php (modified) (history)
  • /trunk/extensions/Gadgets/README (modified) (history)
  • /trunk/extensions/Gadgets/SpecialGadgets.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Gadgets/Gadgets_body.php
@@ -40,12 +40,16 @@
4141 if (!$gadgets) return true;
4242
4343 $options = array();
 44+ $default = array();
4445 foreach( $gadgets as $section => $thisSection ) {
4546 $available = array();
4647 foreach( $thisSection as $gadget ) {
4748 if ( $gadget->isAllowed( $user ) ) {
4849 $gname = $gadget->getName();
4950 $available[wfMsgExt( "gadget-$gname", 'parseinline' )] = $gname;
 51+ if ( $gadget->isEnabled( $user ) ) {
 52+ $default[] = $gname;
 53+ }
5054 }
5155 }
5256 if ( $section !== '' ) {
@@ -77,6 +81,7 @@
7882 'section' => 'gadgets',
7983 'label' => ' ',
8084 'prefix' => 'gadget-',
 85+ 'default' => $default,
8186 );
8287
8388 return true;
@@ -107,8 +112,6 @@
108113 public static function beforePageDisplay( $out ) {
109114 global $wgUser;
110115
111 - if ( !$wgUser->isLoggedIn() ) return true;
112 -
113116 wfProfileIn( __METHOD__ );
114117
115118 $gadgets = Gadget::loadList();
@@ -187,7 +190,7 @@
188191 /**
189192 * Increment this when changing class structure
190193 */
191 - const GADGET_CLASS_VERSION = 3;
 194+ const GADGET_CLASS_VERSION = 4;
192195
193196 private $version = self::GADGET_CLASS_VERSION,
194197 $scripts = array(),
@@ -196,7 +199,8 @@
197200 $name,
198201 $definition,
199202 $resourceLoaded = false,
200 - $requiredRights = array();
 203+ $requiredRights = array(),
 204+ $onByDefault = false;
201205
202206 /**
203207 * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
@@ -234,6 +238,9 @@
235239 case 'rights':
236240 $gadget->requiredRights = $params;
237241 break;
 242+ case 'default':
 243+ $gadget->onByDefault = true;
 244+ break;
238245 }
239246 }
240247 foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
@@ -276,7 +283,7 @@
277284 * @return Boolean
278285 */
279286 public function isEnabled( $user ) {
280 - return (bool)$user->getOption( "gadget-{$this->name}" );
 287+ return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
281288 }
282289
283290 /**
@@ -290,6 +297,13 @@
291298 }
292299
293300 /**
 301+ * @return Boolean: Whether this gadget is on by default for everyone (but can be disabled in preferences)
 302+ */
 303+ public function isOnByDefault() {
 304+ return $this->onByDefault;
 305+ }
 306+
 307+ /**
294308 * @return Boolean: Whether all of this gadget's JS components support ResourceLoader
295309 */
296310 public function supportsResourceLoader() {
Index: trunk/extensions/Gadgets/Gadgets.i18n.php
@@ -34,6 +34,7 @@
3535 This overview provides easy access to the system message pages that define each gadget's description and code.",
3636 'gadgets-uses' => 'Uses',
3737 'gadgets-required-rights' => 'Requires the {{PLURAL:$2|$1 right|following rights: $1}}.',
 38+ 'gadgets-default' => 'Enabled for everyone by default.',
3839 'gadgets-export' => 'Export',
3940 'gadgets-export-title' => 'Gadget export',
4041 'gadgets-not-found' => 'Gadget "$1" not found.',
Index: trunk/extensions/Gadgets/SpecialGadgets.php
@@ -116,9 +116,13 @@
117117 $wgOut->addHTML( $wgLang->commaList( $lnk ) );
118118 $rights = $gadget->getRequiredRights();
119119 if ( count( $rights ) ) {
120 - $wgOut->addHTML( '<br />' );
121 - $wgOut->addWikiMsg( 'gadgets-required-rights', $wgLang->commaList( $rights ), count( $rights ) );
 120+ $wgOut->addHTML( '<br />' .
 121+ wfMessage( 'gadgets-required-rights', $wgLang->commaList( $rights ), count( $rights ) )->parse()
 122+ );
122123 }
 124+ if ( $gadget->isOnByDefault() ) {
 125+ $wgOut->addHTML( '<br />' . wfMessage( 'gadgets-default' )->parse() );
 126+ }
123127
124128 $wgOut->addHTML( Xml::closeElement( 'li' ) . "\n" );
125129 }
Index: trunk/extensions/Gadgets/README
@@ -28,69 +28,8 @@
2929 require_once( "$IP/extensions/Gadgets/Gadgets.php" );
3030
3131 == Usage ==
32 -The list of available gadgets is defined on MediaWiki:Gadgets-definition.
33 -Gadgets defined there show up in the "Gadgets" section of
34 -Special:Preferences, so users can pick the gadgets they would like to use.
35 -An overview of the gadgets defined by MediaWiki:Gadgets-definition is also
36 -shown on Special:Gadgets, along with links to the respective system
37 -messages, for easy editing.
 32+See http://www.mediawiki.org/wiki/Extension:Gadgets#Usage
3833
39 -Each line in MediaWiki:Gadgets-definition that start with one or more "*"
40 -(asterisk) characters defines a gadget; it must have the following form:
41 -
42 - * mygadget|mygadget.js|mygadget.css
43 -
44 -or
45 -
46 - * mygadget[ResourceLoader]|mygadget.js|mygadget.css
47 -
48 -That is, each line consists of fields separated by a "|" (pipe) character.
49 -The first field ("mygadget" in the example) is the gadgets internal name,
50 -and references a system message (MediaWiki:Gadget-mygadget in the example)
51 -that contains a short description of the gadget, using wiki syntax.
52 -Note that the internal name must start with an ASCII letter, must not be
53 -longer than 25 bytes, and must contain only ASCII letters and numbers,
54 -hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
55 -(spaces are also allowed but converted to underscores ("_"), like for
56 -page titles).
57 -
58 -If the gadget name is followed by [ResourceLoader], its JavaScript will
59 -be loaded concatenated, minified and gzipped by ResourceLoader, thus
60 -improving load times. However, older gadgets may be incompatible with
61 -ResourceLoader, so all JS is by default loaded the old way, using separate
62 -<script> tags.
63 -
64 -The remaining fields on the line refer to the JavaScript or CSS code that
65 -makes up the gadget, contained in system messages
66 -(MediaWiki:Gadget-mygadget.js and MediaWiki:Gadget-mygadget.css in the
67 -example); the names of those messages must end with ".js" or ".css",
68 -respectively. A gadget can use any number of code messages, specifically,
69 -common code can be put into a code message used by several gadgets, in
70 -addition to their own specific code, e.g:
71 -
72 - * frobinator|commonStuff.js|frob.js|frob.css|pretty.css
73 - * l33t|commonStuff.js|tools.js|l33t.js
74 -
75 -Gadget definitions can contain whitespace between its elements, e.g.
76 -the following definitions are equivalent:
77 -
78 - *mygadget[ResourceLoader]|mygadget.js|mygadget.css
79 -
80 -and
81 -
82 - * mygadget [ ResourceLoader ] | mygadget.js | mygadget.css
83 -
84 -The list of gadgets in MediaWiki:Gadgets-definition can be broken into
85 -sections using lines that start and end with two or more "=" (equals)
86 -characters, enclosing the name of a system message that defines the
87 -section's name - for example:
88 -
89 - == editing-gadgets ==
90 -
91 -This would define a new section, with the title defined on the page
92 -MediaWiki:Gadget-section-editing-gadgets
93 -
94 -
9534 == Caveats ==
9635
9736 * Gadgets do not apply to Special:Preferences, Special:UserLogin and

Sign-offs

UserFlagDate
Krinkleinspected19:00, 12 April 2011

Comments

#Comment by Happy-melon (talk | contribs)   18:25, 12 April 2011

How does this interact with the privileges option? AFAICT it overrides it? IMO, a script defined with [rights=user|default] should be on by default for users and off-and-disabled for anons.

#Comment by MaxSem (talk | contribs)   18:45, 12 April 2011

The key here is if ( $gadget->isEnabled( $wgUser ) && $gadget->isAllowed( $wgUser ) ). It behaves exactly as you want it to:)

#Comment by Happy-melon (talk | contribs)   19:51, 12 April 2011

\o/

#Comment by He7d3r (talk | contribs)   22:11, 13 April 2011

Great! :)

Status & tagging log