Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Outputs.php |
— | — | @@ -27,8 +27,20 @@ |
28 | 28 | */ |
29 | 29 | class SMWOutputs { |
30 | 30 | |
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(); |
33 | 45 | |
34 | 46 | /// Protected member for temporarily storing resource modules. |
35 | 47 | protected static $resourceModules = array(); |
— | — | @@ -43,11 +55,30 @@ |
44 | 56 | public static function requireResource( $moduleName ) { |
45 | 57 | self::$resourceModules[$moduleName] = $moduleName; |
46 | 58 | } |
| 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 | + } |
47 | 75 | |
48 | 76 | /** |
49 | 77 | * Adds head items that are not Resource Loader modules. Should only |
50 | 78 | * 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 | + * |
52 | 83 | * Support for calling this with the old constants SMW_HEADER_STYLE |
53 | 84 | * and SMW_HEADER_TOOLTIP will vanish in SMW 1.7 at the latest. |
54 | 85 | * |
— | — | @@ -65,7 +96,7 @@ |
66 | 97 | break; |
67 | 98 | } |
68 | 99 | } else { |
69 | | - self::$mHeadItems[$id] = $item; |
| 100 | + self::$headItems[$id] = $item; |
70 | 101 | } |
71 | 102 | } |
72 | 103 | |
— | — | @@ -85,9 +116,10 @@ |
86 | 117 | * @param ParserOutput $parserOutput |
87 | 118 | */ |
88 | 119 | 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 ); |
90 | 122 | 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 ); |
92 | 124 | } |
93 | 125 | } |
94 | 126 | |
— | — | @@ -103,6 +135,7 @@ |
104 | 136 | * @param Parser $parser |
105 | 137 | */ |
106 | 138 | static public function commitToParser( Parser $parser ) { |
| 139 | + /// TODO find out and document when this b/c code can go away |
107 | 140 | if ( method_exists( $parser, 'getOutput' ) ) { |
108 | 141 | $po = $parser->getOutput(); |
109 | 142 | } else { |
— | — | @@ -120,7 +153,10 @@ |
121 | 154 | * @param ParserOutput $parserOutput |
122 | 155 | */ |
123 | 156 | 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 ) { |
125 | 161 | $parserOutput->addHeadItem( "\t\t" . $item . "\n", $key ); |
126 | 162 | } |
127 | 163 | |
— | — | @@ -132,7 +168,7 @@ |
133 | 169 | } |
134 | 170 | |
135 | 171 | self::$resourceModules = array(); |
136 | | - self::$mHeadItems = array(); |
| 172 | + self::$headItems = array(); |
137 | 173 | } |
138 | 174 | |
139 | 175 | /** |
— | — | @@ -146,7 +182,10 @@ |
147 | 183 | * @param OutputPage $output |
148 | 184 | */ |
149 | 185 | 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 ) { |
151 | 190 | $output->addHeadItem( $key, "\t\t" . $item . "\n" ); |
152 | 191 | } |
153 | 192 | |
— | — | @@ -158,7 +197,7 @@ |
159 | 198 | } |
160 | 199 | |
161 | 200 | self::$resourceModules = array(); |
162 | | - self::$mHeadItems = array(); |
| 201 | + self::$headItems = array(); |
163 | 202 | } |
164 | 203 | |
165 | 204 | /** |