r95172 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95171‎ | r95172 | r95173 >
Date:19:08, 21 August 2011
Author:mkroetzsch
Status:deferred (Comments)
Tags:
Comment:
also support OutputPage/ParserOutput-agnostic script loading a la addScript() via SMWOutputs
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Outputs.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Outputs.php
@@ -27,8 +27,20 @@
2828 */
2929 class SMWOutputs {
3030
31 - /// Protected member for temporarily storing header items.
32 - protected static $mHeadItems = array();
 31+ /**
 32+ * Protected member for temporarily storing header items.
 33+ * Format $id => $headItem where $id is used only to avoid duplicate
 34+ * items in the time before they are forwarded to the output.
 35+ */
 36+ protected static $headItems = array();
 37+
 38+ /**
 39+ * Protected member for temporarily storing additional Javascript
 40+ * snippets. Format $id => $scriptText where $id is used only to
 41+ * avoid duplicate scripts in the time before they are forwarded
 42+ * to the output.
 43+ */
 44+ protected static $scripts = array();
3345
3446 /// Protected member for temporarily storing resource modules.
3547 protected static $resourceModules = array();
@@ -43,11 +55,30 @@
4456 public static function requireResource( $moduleName ) {
4557 self::$resourceModules[$moduleName] = $moduleName;
4658 }
 59+
 60+ /**
 61+ * Require the presence of header scripts, provided as strings with
 62+ * enclosing script tags. Note that the same could be achieved with
 63+ * requireHeadItems, but scripts use a special method "addScript" in
 64+ * MediaWiki OutputPage, hence we distinguish them.
 65+ *
 66+ * The id is used to avoid that the requirement for one script is
 67+ * recorded multiple times in SMWOutputs.
 68+ *
 69+ * @param string $id
 70+ * @param string $item
 71+ */
 72+ public static function requireScript( $id, $script ) {
 73+ self::$scripts[$id] = $script;
 74+ }
4775
4876 /**
4977 * Adds head items that are not Resource Loader modules. Should only
5078 * be used for custom head items such as RSS fedd links.
51 - *
 79+ *
 80+ * The id is used to avoid that the requirement for one script is
 81+ * recorded multiple times in SMWOutputs.
 82+ *
5283 * Support for calling this with the old constants SMW_HEADER_STYLE
5384 * and SMW_HEADER_TOOLTIP will vanish in SMW 1.7 at the latest.
5485 *
@@ -65,7 +96,7 @@
6697 break;
6798 }
6899 } else {
69 - self::$mHeadItems[$id] = $item;
 100+ self::$headItems[$id] = $item;
70101 }
71102 }
72103
@@ -85,9 +116,10 @@
86117 * @param ParserOutput $parserOutput
87118 */
88119 static public function requireFromParserOutput( ParserOutput $parserOutput ) {
89 - self::$mHeadItems = array_merge( (array)self::$mHeadItems, (array)$parserOutput->mHeadItems );
 120+ // Note: we do not attempt to recover which head items where scripts here.
 121+ self::$headItems = array_merge( (array)self::$headItems, (array)$parserOutput->headItems );
90122 if ( isset( $parserOutput->mModules ) ) {
91 - self::$mHeadItems = array_merge( (array)self::$resourceModules, (array)$parserOutput->mModules );
 123+ self::$headItems = array_merge( (array)self::$resourceModules, (array)$parserOutput->mModules );
92124 }
93125 }
94126
@@ -103,6 +135,7 @@
104136 * @param Parser $parser
105137 */
106138 static public function commitToParser( Parser $parser ) {
 139+ /// TODO find out and document when this b/c code can go away
107140 if ( method_exists( $parser, 'getOutput' ) ) {
108141 $po = $parser->getOutput();
109142 } else {
@@ -120,7 +153,10 @@
121154 * @param ParserOutput $parserOutput
122155 */
123156 static public function commitToParserOutput( ParserOutput $parserOutput ) {
124 - foreach ( self::$mHeadItems as $key => $item ) {
 157+ foreach ( self::$scripts as $key => $script ) {
 158+ $parserOutput->addHeadItem( $script . "\n", $key );
 159+ }
 160+ foreach ( self::$headItems as $key => $item ) {
125161 $parserOutput->addHeadItem( "\t\t" . $item . "\n", $key );
126162 }
127163
@@ -132,7 +168,7 @@
133169 }
134170
135171 self::$resourceModules = array();
136 - self::$mHeadItems = array();
 172+ self::$headItems = array();
137173 }
138174
139175 /**
@@ -146,7 +182,10 @@
147183 * @param OutputPage $output
148184 */
149185 static public function commitToOutputPage( OutputPage $output ) {
150 - foreach ( self::$mHeadItems as $key => $item ) {
 186+ foreach ( self::$scripts as $key => $script ) {
 187+ $parserOutput->addScript( $script );
 188+ }
 189+ foreach ( self::$headItems as $key => $item ) {
151190 $output->addHeadItem( $key, "\t\t" . $item . "\n" );
152191 }
153192
@@ -158,7 +197,7 @@
159198 }
160199
161200 self::$resourceModules = array();
162 - self::$mHeadItems = array();
 201+ self::$headItems = array();
163202 }
164203
165204 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r95193Followup r95172, use the getter instead of nonexistent member variablenikerabbit08:25, 22 August 2011
r95874fix for bug 30660, followup to r95172jeroendedauw15:09, 31 August 2011

Comments

#Comment by Raymond (talk | contribs)   08:20, 22 August 2011

Seen on translatewiki.net:

PHP Notice: Undefined property: ParserOutput::$headItems in /www/w/extensions/SemanticMediaWiki/includes/SMW_Outputs.php on line 120
#Comment by Jeroen De Dauw (talk | contribs)   15:13, 31 August 2011

Markus, can you have a look at r95874? That code was adding resource loader module names as head items for MW >= 1.17. Commenting it out does not seem to cause any issues, but I'm not sure what the original purpose of this was, so something might be broken now (esp on 1.16).

Status & tagging log