3
3
use MediaWiki \Linker \LinkTarget ;
4
4
use MediaWiki \Revision \RevisionRecord ;
5
5
6
+ /**
7
+ * Registers and defines parser functions for UsingData.
8
+ */
6
9
class UsingDataHooks {
10
+ /**
11
+ * @var UsingDataHooks Singleton instance
12
+ */
7
13
public static $ instance = null ;
8
14
15
+ /**
16
+ * @var UsingDataPPFrameDOM[] Data frames for each page
17
+ */
9
18
private $ dataFrames = [];
10
19
20
+ /**
21
+ * @var bool Whether we are currently searching for data
22
+ */
11
23
private $ searchingForData = false ;
12
24
25
+ /**
26
+ * @var array|null Arguments to override
27
+ */
13
28
private static $ phTitle = null ;
14
29
15
30
public static function onParserFirstCallInit ( Parser &$ parser ) {
@@ -19,7 +34,11 @@ public static function onParserFirstCallInit( Parser &$parser ) {
19
34
$ parser ->setFunctionHook ( 'using ' , [ self ::$ instance , 'usingParserFunction ' ], SFH_OBJECT_ARGS );
20
35
$ parser ->setFunctionHook ( 'usingarg ' , [ self ::$ instance , 'usingArgParserFunction ' ], SFH_OBJECT_ARGS );
21
36
$ parser ->setFunctionHook ( 'data ' , [ self ::$ instance , 'dataParserFunction ' ], SFH_OBJECT_ARGS );
22
- $ parser ->setFunctionHook ( 'ancestorname ' , [ __CLASS__ , 'ancestorNameFunction ' ], SFH_OBJECT_ARGS | SFH_NO_HASH );
37
+ $ parser ->setFunctionHook (
38
+ 'ancestorname ' ,
39
+ [ __CLASS__ , 'ancestorNameFunction ' ],
40
+ SFH_OBJECT_ARGS | SFH_NO_HASH
41
+ );
23
42
$ parser ->setHook ( 'using ' , [ self ::$ instance , 'usingTag ' ] );
24
43
25
44
return true ;
@@ -35,7 +54,8 @@ public static function onGetMagicVariableIDs( &$magicWords ) {
35
54
$ magicWords [] = 'selfname ' ;
36
55
}
37
56
38
- /* Returns a UsingData frame for a given page
57
+ /**
58
+ * Returns a UsingData frame for a given page
39
59
*/
40
60
private function getDataFrame ( $ sourcePage , $ title , &$ parser , $ frame ) {
41
61
global $ wgHooks ;
@@ -71,7 +91,9 @@ private function getDataFrame( $sourcePage, $title, &$parser, $frame ) {
71
91
return $ this ->dataFrames [$ sourcePage ];
72
92
}
73
93
74
- /* Returns the page title of the $depth ancestor of $frame; empty string if invalid */
94
+ /**
95
+ * Returns the page title of the $depth ancestor of $frame; empty string if invalid
96
+ */
75
97
private static function ancestorNameHandler ( $ frame , $ depth ) {
76
98
while ( $ depth -- && $ frame != null ) {
77
99
$ frame = $ frame ->parent ?? null ;
@@ -83,10 +105,15 @@ private static function ancestorNameHandler( $frame, $depth ) {
83
105
/* Handles {{ANCESTORNAME:depth}} */
84
106
public static function ancestorNameFunction ( &$ parser , $ frame , $ args ) {
85
107
$ arg = $ frame ->expand ( $ args [0 ] );
86
- return [ self ::ancestorNameHandler ( $ frame , max ( 0 , is_numeric ( $ arg ) ? intval ( $ arg ) : 1 ) ), 'noparse ' => true ];
108
+ return [
109
+ self ::ancestorNameHandler ( $ frame , max ( 0 , is_numeric ( $ arg ) ? intval ( $ arg ) : 1 ) ),
110
+ 'noparse ' => true
111
+ ];
87
112
}
88
113
89
- /* Handles {{PARENTNAME}}, {{SELFNAME}}, {{ANCESTORNAME}} */
114
+ /**
115
+ * Handles {{PARENTNAME}}, {{SELFNAME}}, {{ANCESTORNAME}}
116
+ */
90
117
public static function ancestorNameVar ( &$ parser , &$ varCache , &$ index , &$ ret , &$ frame ) {
91
118
if ( $ index == 'parentname ' ) {
92
119
$ ret = self ::ancestorNameHandler ( $ frame , 1 );
@@ -97,7 +124,8 @@ public static function ancestorNameVar( &$parser, &$varCache, &$index, &$ret, &$
97
124
return true ;
98
125
}
99
126
100
- /* Parses common elements of #using syntax.
127
+ /**
128
+ * Parses common elements of #using syntax.
101
129
*/
102
130
private function usingParse ( &$ parser , $ frame , $ args ) {
103
131
if ( $ this ->searchingForData ) {
@@ -132,7 +160,8 @@ private function usingParse( &$parser, $frame, $args ) {
132
160
return [ $ this ->getDataFrame ( $ sourcePage , $ title , $ parser , $ frame ), $ sourceHash , $ namedArgs , $ one , $ two ];
133
161
}
134
162
135
- /* {{#using:Page#Hash|Template|Default|...}} parses Template using #data from Page's Hash fragment; or Default
163
+ /**
164
+ * {{#using:Page#Hash|Template|Default|...}} parses Template using #data from Page's Hash fragment; or Default
136
165
* if no data from Page can be found. Named arguments override those in the #data tag.
137
166
*/
138
167
public function usingParserFunction ( &$ parser , $ frame , $ args ) {
@@ -149,7 +178,9 @@ public function usingParserFunction( &$parser, $frame, $args ) {
149
178
return $ dframe ->expandUsing ( $ frame , $ title , $ dom , $ namedArgs , $ fragment );
150
179
}
151
180
152
- /* {{#usingarg:Page#Hash|Arg|Default}} returns the value of Arg data field on Page's Hash fragment, Default if undefined.
181
+ /**
182
+ * {{#usingarg:Page#Hash|Arg|Default}} returns the value of Arg data field on Page's Hash fragment, Default if
183
+ * undefined.
153
184
*/
154
185
public function usingArgParserFunction ( &$ parser , $ frame , $ args ) {
155
186
$ parse = $ this ->usingParse ( $ parser , $ frame , $ args );
@@ -167,12 +198,13 @@ public function usingArgParserFunction( &$parser, $frame, $args ) {
167
198
return $ ret !== false ? $ ret : $ frame ->expand ( $ defaultValue );
168
199
}
169
200
170
- /* <using page="Page#Hash" default="Default">...</using>
201
+ /**
202
+ * <using page="Page#Hash" default="Default">...</using>
171
203
* expands ... using the data from Page's Hash fragment; Default if undefined.
172
204
* This tag relies on $parser->replaceVariables($text, $frame), which may prove fragile across MW versions.
173
- * Should it break, $parser->recursiveTagParse($text, $frame), in combination with either modifying the markerType, or using
174
- * insertStripItem directly, is a viable short-term alternative -- but one that call certain hooks prematurely,
175
- * potentially causing other extensions to misbehave slightly.
205
+ * Should it break, $parser->recursiveTagParse($text, $frame), in combination with either modifying the markerType,
206
+ * or using insertStripItem directly, is a viable short-term alternative -- but one that call certain hooks
207
+ * prematurely, potentially causing other extensions to misbehave slightly.
176
208
*/
177
209
public function usingTag ( $ text , array $ args , Parser $ parser , PPFrame $ frame ) {
178
210
if ( $ this ->searchingForData ) {
@@ -206,7 +238,7 @@ public function usingTag( $text, array $args, Parser $parser, PPFrame $frame ) {
206
238
];
207
239
}
208
240
209
- /* {{#data:Template#Hash|...}} specifies data-transcludable arguments for the page; may not be transcluded. */
241
+ /** {{#data:Template#Hash|...}} specifies data-transcludable arguments for the page; may not be transcluded. */
210
242
public function dataParserFunction ( Parser &$ parser , PPFrame $ frame , $ args ) {
211
243
$ templateTitle = trim ( $ frame ->expand ( $ args [0 ] ) );
212
244
$ hostPage = $ frame ->title ->getPrefixedText ();
@@ -253,7 +285,8 @@ public function dataParserFunction( Parser &$parser, PPFrame $frame, $args ) {
253
285
return $ cframe ->expand ( $ dom );
254
286
}
255
287
256
- /* Returns template text for transclusion.
288
+ /**
289
+ * Returns template text for transclusion.
257
290
*/
258
291
private function fetchTemplate ( $ parser , $ template ) {
259
292
global $ wgNonincludableNamespaces ;
0 commit comments