r92908 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92907‎ | r92908 | r92909 >
Date:22:43, 22 July 2011
Author:catrope
Status:deferred
Tags:
Comment:
RL2: Remove the Gadget and ResourceLoaderGadgetModule classes, they're gonna need to be completely rewritten and I want to put them in separate files as well. I know that this fatally breaks the extension, but what the heck, this is a branch
Modified paths:
  • /branches/RL2/extensions/Gadgets/Gadgets.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/Gadgets_body.php
@@ -194,386 +194,3 @@
195195 return true;
196196 }
197197 }
198 -
199 -/**
200 - * Wrapper for one gadget.
201 - */
202 -class Gadget {
203 - /**
204 - * Increment this when changing class structure
205 - */
206 - const GADGET_CLASS_VERSION = 5;
207 -
208 - private $version = self::GADGET_CLASS_VERSION,
209 - $scripts = array(),
210 - $styles = array(),
211 - $dependencies = array(),
212 - $name,
213 - $definition,
214 - $resourceLoaded = false,
215 - $requiredRights = array(),
216 - $onByDefault = false,
217 - $category;
218 -
219 - /**
220 - * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
221 - * @param $definition String: Gadget definition
222 - * @return Mixed: Instance of Gadget class or false if $definition is invalid
223 - */
224 - public static function newFromDefinition( $definition ) {
225 - $m = array();
226 - if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) {
227 - return false;
228 - }
229 - //NOTE: the gadget name is used as part of the name of a form field,
230 - // and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata
231 - // Also, title-normalization applies.
232 - $gadget = new Gadget();
233 - $gadget->name = trim( str_replace(' ', '_', $m[1] ) );
234 - $gadget->definition = $definition;
235 - $options = trim( $m[2], ' []' );
236 - foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
237 - $arr = preg_split( '/\s*=\s*/', $option, 2 );
238 - $option = $arr[0];
239 - if ( isset( $arr[1] ) ) {
240 - $params = explode( ',', $arr[1] );
241 - $params = array_map( 'trim', $params );
242 - } else {
243 - $params = array();
244 - }
245 - switch ( $option ) {
246 - case 'ResourceLoader':
247 - $gadget->resourceLoaded = true;
248 - break;
249 - case 'dependencies':
250 - $gadget->dependencies = $params;
251 - break;
252 - case 'rights':
253 - $gadget->requiredRights = $params;
254 - break;
255 - case 'default':
256 - $gadget->onByDefault = true;
257 - break;
258 - }
259 - }
260 - foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
261 - $page = "Gadget-$page";
262 - if ( preg_match( '/\.js/', $page ) ) {
263 - $gadget->scripts[] = $page;
264 - } elseif ( preg_match( '/\.css/', $page ) ) {
265 - $gadget->styles[] = $page;
266 - }
267 - }
268 - return $gadget;
269 - }
270 -
271 - /**
272 - * @return String: Gadget name
273 - */
274 - public function getName() {
275 - return $this->name;
276 - }
277 -
278 - /**
279 - * @return String: Gadget description parsed into HTML
280 - */
281 - public function getDescription() {
282 - return wfMessage( "gadget-{$this->getName()}" )->parse();
283 - }
284 -
285 - /**
286 - * @return String: Wikitext of gadget description
287 - */
288 - public function getRawDescription() {
289 - return wfMessage( "gadget-{$this->getName()}" )->plain();
290 - }
291 -
292 - /**
293 - * @return String: Name of category (aka section) our gadget belongs to. Empty string if none.
294 - */
295 - public function getCategory() {
296 - return $this->category;
297 - }
298 -
299 - /**
300 - * @return String: Name of ResourceLoader module for this gadget
301 - */
302 - public function getModuleName() {
303 - return "ext.gadget.{$this->name}";
304 - }
305 -
306 - /**
307 - * Checks whether this is an instance of an older version of this class deserialized from cache
308 - * @return Boolean
309 - */
310 - public function isOutdated() {
311 - return $this->version != self::GADGET_CLASS_VERSION;
312 - }
313 -
314 - /**
315 - * Checks whether this gadget is enabled for given user
316 - *
317 - * @param $user User: user to check against
318 - * @return Boolean
319 - */
320 - public function isEnabled( $user ) {
321 - return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
322 - }
323 -
324 - /**
325 - * Checks whether given user has permissions to use this gadget
326 - *
327 - * @param $user User: user to check against
328 - * @return Boolean
329 - */
330 - public function isAllowed( $user ) {
331 - return count( array_intersect( $this->requiredRights, $user->getRights() ) ) == count( $this->requiredRights );
332 - }
333 -
334 - /**
335 - * @return Boolean: Whether this gadget is on by default for everyone (but can be disabled in preferences)
336 - */
337 - public function isOnByDefault() {
338 - return $this->onByDefault;
339 - }
340 -
341 - /**
342 - * @return Boolean: Whether all of this gadget's JS components support ResourceLoader
343 - */
344 - public function supportsResourceLoader() {
345 - return $this->resourceLoaded;
346 - }
347 -
348 - /**
349 - * @return Boolean: Whether this gadget has resources that can be loaded via ResourceLoader
350 - */
351 - public function hasModule() {
352 - return count( $this->styles )
353 - + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
354 - > 0;
355 - }
356 -
357 - /**
358 - * @return String: Definition for this gadget from MediaWiki:gadgets-definition
359 - */
360 - public function getDefinition() {
361 - return $this->definition;
362 - }
363 -
364 - /**
365 - * @return Array: Array of pages with JS not prefixed with namespace
366 - */
367 - public function getScripts() {
368 - return $this->scripts;
369 - }
370 -
371 - /**
372 - * @return Array: Array of pages with CSS not prefixed with namespace
373 - */
374 - public function getStyles() {
375 - return $this->styles;
376 - }
377 -
378 - /**
379 - * @return Array: Array of all of this gadget's resources
380 - */
381 - public function getScriptsAndStyles() {
382 - return array_merge( $this->scripts, $this->styles );
383 - }
384 -
385 - /**
386 - * Returns module for ResourceLoader, see getModuleName() for its name.
387 - * If our gadget has no scripts or styles suitable for RL, false will be returned.
388 - * @return Mixed: GadgetResourceLoaderModule or false
389 - */
390 - public function getModule() {
391 - $pages = array();
392 - foreach( $this->styles as $style ) {
393 - $pages['MediaWiki:' . $style] = array( 'type' => 'style' );
394 - }
395 - if ( $this->supportsResourceLoader() ) {
396 - foreach ( $this->scripts as $script ) {
397 - $pages['MediaWiki:' . $script] = array( 'type' => 'script' );
398 - }
399 - }
400 - if ( !count( $pages ) ) {
401 - return null;
402 - }
403 - return new GadgetResourceLoaderModule( $pages, $this->dependencies );
404 - }
405 -
406 - /**
407 - * Returns list of scripts that don't support ResourceLoader
408 - * @return Array
409 - */
410 - public function getLegacyScripts() {
411 - if ( $this->supportsResourceLoader() ) {
412 - return array();
413 - }
414 - return $this->scripts;
415 - }
416 -
417 - /**
418 - * Returns names of resources this gadget depends on
419 - * @return Array
420 - */
421 - public function getDependencies() {
422 - return $this->dependencies;
423 - }
424 -
425 - /**
426 - * Returns array of permissions required by this gadget
427 - * @return Array
428 - */
429 - public function getRequiredRights() {
430 - return $this->requiredRights;
431 - }
432 -
433 - /**
434 - * Loads and returns a list of all gadgets
435 - * @return Mixed: Array of gadgets or false
436 - */
437 - public static function loadList() {
438 - static $gadgets = null;
439 -
440 - if ( $gadgets !== null ) return $gadgets;
441 -
442 - wfProfileIn( __METHOD__ );
443 - $struct = self::loadStructuredList();
444 - if ( !$struct ) {
445 - $gadgets = $struct;
446 - wfProfileOut( __METHOD__ );
447 - return $gadgets;
448 - }
449 -
450 - $gadgets = array();
451 - foreach ( $struct as $section => $entries ) {
452 - $gadgets = array_merge( $gadgets, $entries );
453 - }
454 - wfProfileOut( __METHOD__ );
455 -
456 - return $gadgets;
457 - }
458 -
459 - /**
460 - * Checks whether gadget list from cache can be used.
461 - * @return Boolean
462 - */
463 - private static function isValidList( $gadgets ) {
464 - if ( !is_array( $gadgets ) ) return false;
465 - // Check if we have 1) array of gadgets 2) the gadgets are up to date
466 - // One check is enough
467 - foreach ( $gadgets as $section => $list ) {
468 - foreach ( $list as $g ) {
469 - if ( !( $g instanceof Gadget ) || $g->isOutdated() ) {
470 - return false;
471 - } else {
472 - return true;
473 - }
474 - }
475 - }
476 - return true; // empty array
477 - }
478 -
479 - /**
480 - * Loads list of gadgets and returns it as associative array of sections with gadgets
481 - * e.g. array( 'sectionnname1' => array( $gadget1, $gadget2),
482 - * 'sectionnname2' => array( $gadget3 ) );
483 - * @param $forceNewText String: New text of MediaWiki:gadgets-sdefinition. If specified, will
484 - * force a purge of cache and recreation of the gadget list.
485 - * @return Mixed: Array or false
486 - */
487 - public static function loadStructuredList( $forceNewText = null ) {
488 - global $wgMemc;
489 -
490 - static $gadgets = null;
491 - if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
492 -
493 - wfProfileIn( __METHOD__ );
494 - $key = wfMemcKey( 'gadgets-definition', self::GADGET_CLASS_VERSION );
495 -
496 - if ( $forceNewText === null ) {
497 - //cached?
498 - $gadgets = $wgMemc->get( $key );
499 - if ( self::isValidList( $gadgets ) ) {
500 - wfProfileOut( __METHOD__ );
501 - return $gadgets;
502 - }
503 -
504 - $g = wfMsgForContentNoTrans( "gadgets-definition" );
505 - if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
506 - $gadgets = false;
507 - wfProfileOut( __METHOD__ );
508 - return $gadgets;
509 - }
510 - } else {
511 - $g = $forceNewText;
512 - }
513 -
514 - $g = preg_replace( '/<!--.*-->/s', '', $g );
515 - $g = preg_split( '/(\r\n|\r|\n)+/', $g );
516 -
517 - $gadgets = array();
518 - $section = '';
519 -
520 - foreach ( $g as $line ) {
521 - $m = array();
522 - if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
523 - $section = $m[1];
524 - }
525 - else {
526 - $gadget = self::newFromDefinition( $line );
527 - if ( $gadget ) {
528 - $gadgets[$section][$gadget->getName()] = $gadget;
529 - $gadget->category = $section;
530 - }
531 - }
532 - }
533 -
534 - //cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
535 - $wgMemc->set( $key, $gadgets, 60*60*24 );
536 - $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
537 - wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
538 - wfProfileOut( __METHOD__ );
539 -
540 - return $gadgets;
541 - }
542 -}
543 -
544 -/**
545 - * Class representing a list of resources for one gadget
546 - */
547 -class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
548 - private $pages, $dependencies;
549 -
550 - /**
551 - * Creates an instance of this class
552 - * @param $pages Array: Associative array of pages in ResourceLoaderWikiModule-compatible
553 - * format, for example:
554 - * array(
555 - * 'MediaWiki:Gadget-foo.js' => array( 'type' => 'script' ),
556 - * 'MediaWiki:Gadget-foo.css' => array( 'type' => 'style' ),
557 - * )
558 - * @param $dependencies Array: Names of resources this module depends on
559 - */
560 - public function __construct( $pages, $dependencies ) {
561 - $this->pages = $pages;
562 - $this->dependencies = $dependencies;
563 - }
564 -
565 - /**
566 - * Overrides the abstract function from ResourceLoaderWikiModule class
567 - * @return Array: $pages passed to __construct()
568 - */
569 - protected function getPages( ResourceLoaderContext $context ) {
570 - return $this->pages;
571 - }
572 -
573 - /**
574 - * Overrides ResourceLoaderModule::getDependencies()
575 - * @return Array: Names of resources this module depends on
576 - */
577 - public function getDependencies() {
578 - return $this->dependencies;
579 - }
580 -}
Index: branches/RL2/extensions/Gadgets/Gadgets.php
@@ -56,9 +56,7 @@
5757
5858 $wgAutoloadClasses['ApiQueryGadgetCategories'] = $dir . 'ApiQueryGadgetCategories.php';
5959 $wgAutoloadClasses['ApiQueryGadgets'] = $dir . 'ApiQueryGadgets.php';
60 -$wgAutoloadClasses['Gadget'] = $dir . 'Gadgets_body.php';
6160 $wgAutoloadClasses['GadgetHooks'] = $dir . 'Gadgets_body.php';
62 -$wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php';
6361 $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
6462
6563 $wgSpecialPages['Gadgets'] = 'SpecialGadgets';

Status & tagging log