r76043 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76042‎ | r76043 | r76044 >
Date:21:32, 4 November 2010
Author:maxsem
Status:deferred
Tags:
Comment:
Moved list-loading functions to Gadget class
Modified paths:
  • /branches/Gadgets-work/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/Gadgets-work/Gadgets_body.php
@@ -18,81 +18,13 @@
1919 //update cache if MediaWiki:Gadgets-definition was edited
2020 $title = $article->mTitle;
2121 if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
22 - self::loadStructured( $text );
 22+ Gadget::loadStructuredList( $text );
2323 }
2424 return true;
2525 }
2626
27 - private static function load() {
28 - static $gadgets = null;
29 -
30 - if ( $gadgets !== null ) return $gadgets;
31 -
32 - $struct = self::loadStructured();
33 - if ( !$struct ) {
34 - $gadgets = $struct;
35 - return $gadgets;
36 - }
37 -
38 - $gadgets = array();
39 - foreach ( $struct as $section => $entries ) {
40 - $gadgets = array_merge( $gadgets, $entries );
41 - }
42 -
43 - return $gadgets;
44 - }
45 -
46 - public static function loadStructured( $forceNewText = null ) {
47 - global $wgMemc;
48 -
49 - static $gadgets = null;
50 - if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
51 -
52 - $key = wfMemcKey( 'gadgets-definition' );
53 -
54 - if ( $forceNewText === null ) {
55 - //cached?
56 - $gadgets = $wgMemc->get( $key );
57 - // TODO: isOutdated()
58 - if ( is_array($gadgets) && next( $gadgets ) instanceof Gadget ) return $gadgets;
59 -
60 - $g = wfMsgForContentNoTrans( "gadgets-definition" );
61 - if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
62 - $gadgets = false;
63 - return $gadgets;
64 - }
65 - } else {
66 - $g = $forceNewText;
67 - }
68 -
69 - $g = preg_replace( '/<!--.*-->/s', '', $g );
70 - $g = preg_split( '/(\r\n|\r|\n)+/', $g );
71 -
72 - $gadgets = array();
73 - $section = '';
74 -
75 - foreach ( $g as $line ) {
76 - if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
77 - $section = $m[1];
78 - }
79 - else {
80 - $gadget = Gadget::newFromDefinition( $line );
81 - if ( $gadget ) {
82 - $gadgets[$section][$gadget->getName()] = $gadget;
83 - }
84 - }
85 - }
86 -
87 - //cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
88 - $wgMemc->set( $key, $gadgets, 60*60*24 );
89 - $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
90 - wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
91 -
92 - return $gadgets;
93 - }
94 -
9527 public static function getPreferences( $user, &$preferences ) {
96 - $gadgets = self::loadStructured();
 28+ $gadgets = Gadget::loadStructuredList();
9729 if (!$gadgets) return true;
9830
9931 $options = array();
@@ -135,7 +67,7 @@
13668 }
13769
13870 public static function registerModules( &$resourceLoader ) {
139 - $gadgets = self::load();
 71+ $gadgets = Gadget::loadList();
14072 if ( !$gadgets ) {
14173 return true;
14274 }
@@ -164,7 +96,7 @@
16597 return true;
16698 }
16799
168 - $gadgets = self::Load();
 100+ $gadgets = Gadget::loadList();
169101 if ( !$gadgets ) return true;
170102
171103 $lb = new LinkBatch();
@@ -291,6 +223,74 @@
292224 }
293225 return $this->scripts;
294226 }
 227+
 228+ public static function loadList() {
 229+ static $gadgets = null;
 230+
 231+ if ( $gadgets !== null ) return $gadgets;
 232+
 233+ $struct = self::loadStructuredList();
 234+ if ( !$struct ) {
 235+ $gadgets = $struct;
 236+ return $gadgets;
 237+ }
 238+
 239+ $gadgets = array();
 240+ foreach ( $struct as $section => $entries ) {
 241+ $gadgets = array_merge( $gadgets, $entries );
 242+ }
 243+
 244+ return $gadgets;
 245+ }
 246+
 247+ public static function loadStructuredList( $forceNewText = null ) {
 248+ global $wgMemc;
 249+
 250+ static $gadgets = null;
 251+ if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
 252+
 253+ $key = wfMemcKey( 'gadgets-definition' );
 254+
 255+ if ( $forceNewText === null ) {
 256+ //cached?
 257+ $gadgets = $wgMemc->get( $key );
 258+ // TODO: isOutdated()
 259+ if ( is_array($gadgets) && next( $gadgets ) instanceof Gadget ) return $gadgets;
 260+
 261+ $g = wfMsgForContentNoTrans( "gadgets-definition" );
 262+ if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
 263+ $gadgets = false;
 264+ return $gadgets;
 265+ }
 266+ } else {
 267+ $g = $forceNewText;
 268+ }
 269+
 270+ $g = preg_replace( '/<!--.*-->/s', '', $g );
 271+ $g = preg_split( '/(\r\n|\r|\n)+/', $g );
 272+
 273+ $gadgets = array();
 274+ $section = '';
 275+
 276+ foreach ( $g as $line ) {
 277+ if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
 278+ $section = $m[1];
 279+ }
 280+ else {
 281+ $gadget = Gadget::newFromDefinition( $line );
 282+ if ( $gadget ) {
 283+ $gadgets[$section][$gadget->getName()] = $gadget;
 284+ }
 285+ }
 286+ }
 287+
 288+ //cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited
 289+ $wgMemc->set( $key, $gadgets, 60*60*24 );
 290+ $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
 291+ wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n");
 292+
 293+ return $gadgets;
 294+ }
295295 }
296296
297297 class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {

Status & tagging log