r105681 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105680‎ | r105681 | r105682 >
Date:17:26, 9 December 2011
Author:danwe
Status:deferred
Tags:
Comment:
Moved files of include classes into 'includes' folder.
Modified paths:
  • /trunk/extensions/ParserFun/PFun_Caller.php (deleted) (history)
  • /trunk/extensions/ParserFun/PFun_Parse.php (deleted) (history)
  • /trunk/extensions/ParserFun/ParserFun.php (modified) (history)
  • /trunk/extensions/ParserFun/includes (added) (history)
  • /trunk/extensions/ParserFun/includes/PFun_Caller.php (added) (history)
  • /trunk/extensions/ParserFun/includes/PFun_Parse.php (added) (history)

Diff [purge]

Index: trunk/extensions/ParserFun/PFun_Caller.php
@@ -1,267 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class for the 'CALLER' variable-style parser function.
6 - *
7 - * @since 0.2
8 - *
9 - * @file PFun_Caller.php
10 - * @ingroup ParserFun
11 - *
12 - * @author Daniel Werner
13 - */
14 -class ParserFunCaller extends ParserHook {
15 -
16 - public function __construct() {
17 - // make this a parser function extension (no tag extension) only:
18 - parent::__construct( false, true, ParserHook::FH_NO_HASH );
19 - }
20 -
21 - /**
22 - * No LSB in pre-5.3 PHP, to be refactored later
23 - */
24 - public static function staticMagic( array &$magicWords, $langCode ) {
25 - $instance = new self;
26 - return $instance->magic( $magicWords, $langCode );
27 - }
28 -
29 - /**
30 - * No LSB in pre-5.3 PHP, to be refactored later
31 - */
32 - public static function staticInit( Parser &$parser ) {
33 - global $egParserFunEnabledFunctions;
34 - if( in_array( ExtParserFun::MAG_CALLER, $egParserFunEnabledFunctions ) ) {
35 - // only register function if not disabled by configuration
36 - $instance = new self;
37 - $instance->init( $parser );
38 - }
39 - return true;
40 - }
41 -
42 - /**
43 - * Gets the name of the parser hook.
44 - * @see ParserHook::getName
45 - *
46 - * @return string
47 - */
48 - protected function getName() {
49 - return 'CALLER';
50 - }
51 -
52 - /**
53 - * Returns an array containing the parameter info.
54 - * @see ParserHook::getParameterInfo
55 - *
56 - * @return array
57 - */
58 - protected function getParameterInfo( $type ) {
59 - $params = array();
60 -
61 - # what to get, index (any number) or certain mode (count, list)
62 - # since 0.2
63 - $params['mode'] = new Parameter( 'mode', Parameter::TYPE_STRING );
64 - $params['mode']->addAliases( 'index' );
65 -
66 - # where in the stack to start returning.
67 - # negative value will return that many elements from the top-level caller
68 - # since 0.2
69 - $params['offset'] = new Parameter( 'offset', Parameter::TYPE_INTEGER );
70 - $params['offset']->setDefault( false, false );
71 -
72 - # max return, if negative stop that many elements from end
73 - # since 0.2
74 - $params['limit'] = new Parameter( 'limit', Parameter::TYPE_INTEGER );
75 - $params['limit']->setDefault( false, false );
76 - $params['limit']->addAliases( 'len', 'length' );
77 -
78 - # whether to link the page names
79 - # since 0.2
80 - $params['linked'] = new Parameter( 'linked', Parameter::TYPE_BOOLEAN );
81 - $params['linked']->setDefault( false );
82 -
83 - # separator between list items
84 - # since 0.2
85 - $params['sep'] = new Parameter( 'sep', Parameter::TYPE_STRING );
86 - $params['sep']->setDefault( ', ', false );
87 -
88 - return $params;
89 - }
90 -
91 - /**
92 - * Returns the list of default parameters.
93 - * @see ParserHook::getDefaultParameters
94 - *
95 - * @return array
96 - */
97 - protected function getDefaultParameters( $type ) {
98 - return array(
99 - array( 'mode' ),
100 - );
101 - }
102 -
103 - /**
104 - * Renders and returns the output.
105 - * @see ParserHook::renderTag
106 - *
107 - * @param array $parameters
108 - *
109 - * @return string
110 - */
111 - public function render( array $parameters ) {
112 -
113 - $mode = $parameters['mode'];
114 - $linked = $parameters['linked'];
115 - $limit = $parameters['limit'];
116 - $offset = $parameters['offset'];
117 -
118 - if( is_numeric( $mode ) ) {
119 - // get specific caller
120 - /*
121 - * do not just set $offset to $mode, $mode to 'list' and $limit to 1 here since handling
122 - * for negative offset is different in 'list' mode. Here non-existant negative index will
123 - * return '', in 'list' mode it will jump to the 0 element.
124 - */
125 - $index = (int)$mode;
126 - $frame = self::getFrameStackItem( $this->frame, $index );
127 -
128 - if( $frame === null ) {
129 - return '';
130 - }
131 - return self::createSiteList(
132 - array( $frame ),
133 - $linked
134 - );
135 - }
136 -
137 - $mode = strtolower( $mode );
138 - switch( $mode ) {
139 - case 'level':
140 - // synnonym for 'count'
141 - $mode = 'count';
142 - case 'count':
143 - case '':
144 - /*
145 - * '{{CALLER:}}', perhaps with additional parameters, but not in list mode except limit
146 - * is set. Otherwise tread it similar to '{{CALLER}}' variable but with parameters.
147 - */
148 - if( $mode !== 'count' && $limit === false ) {
149 - $limit = 1;
150 - }
151 - if( $offset === false ) {
152 - // '{{CALLER}}' equals '{{CALLER:1}}', not 0, in count mode ignore current page.
153 - $offset = 1;
154 - }
155 - // no-break, evaluate parameters in 'list' mode but count only
156 - case 'list':
157 - $stack = self::getFrameStack( $this->frame );
158 - $offset = ( $offset === false ) ? 0 : $offset;
159 - $limit = ( $limit === false ) ? null : $limit; // Validator can't have null as default...
160 -
161 - $stack = array_slice( $stack, $offset, $limit );
162 -
163 - if( $mode === 'count' ) {
164 - // in 'count' mode, return the level
165 - return count( $stack );
166 - } else {
167 - // normal list mode
168 - return self::createSiteList(
169 - $stack,
170 - $linked,
171 - $parameters['sep']
172 - );
173 - }
174 - }
175 -
176 - /*
177 - * No valid operation mode or index given to first parameter!
178 - * Return error message
179 - */
180 - $error = new ValidationError( wfMsgForContent( 'parserfun-invalid-caller-mode' ) );
181 - return $this->renderFatalError( $error );
182 - }
183 -
184 - /**
185 - * Returns a certain parent caller from a given frame by index. 0 returns the given frame, 1 would return
186 - * the frame of the site which was calling the given frame and so on.
187 - *
188 - * @param PPFrame $frame
189 - * @param int $index can be negative to return the element from the top-level caller. -1 would return
190 - * the same as {{FULLPAGENAME}} would be. If the index doesn't exist, null will be returned.
191 - *
192 - * @return PPFrame|null
193 - */
194 - static function getFrameStackItem( PPFrame $frame, $index ) {
195 - // get the whole stack or just till some certain index
196 - $stack = self::getFrameStack( $frame, $index );
197 -
198 - if( $index >= 0 ) {
199 - if( array_key_exists( $index, $stack ) ) {
200 - return $stack[ $index ];
201 - }
202 - } else {
203 - // negative index, return from top-level
204 - $index = count( $stack ) + $index;
205 - if( $index >= 0 ) {
206 - return $stack[ $index ];
207 - }
208 - }
209 -
210 - // index doesn't exist!
211 - return null;
212 - }
213 -
214 - /**
215 - * Gets all parent frames from a frame and returns them as array with the given frame as first element.
216 - *
217 - * @param PPFrame $frame
218 - * @param int $limit how many parent frames should be returned as maximum (in addition to given frame).
219 - * Limit below 0 means no limit. 0 will just return an array with the given frame.
220 - *
221 - * @return PPFrame[]
222 - */
223 - static function getFrameStack( PPFrame $frame, $limit = -1 ) {
224 - $frames = array();
225 - if( $limit >= 0 ) {
226 - $limit++; // given $frame doesn't count so this will be returned if 0 is given
227 - }
228 -
229 - while( $frame !== null && $limit !== 0 ) {
230 - $frames[] = $frame;
231 - $limit--;
232 -
233 - if( $frame instanceof PPTemplateFrame_DOM ) {
234 - $frame = $frame->parent;
235 - } else {
236 - // frame is no template, so this is the top-level frame (page being rendered)
237 - $frame = null;
238 - }
239 - };
240 -
241 - return $frames;
242 - }
243 -
244 - /**
245 - * Create a list with page titles from a given array of frames, optionally linked output.
246 - * The output ist un-parsed wiki markup, no HTML.
247 - *
248 - * @param array $frames the titles represented as frames
249 - * @param bool $link whether or not to link the pages in the list
250 - * @param string $sep glue between the pages
251 - *
252 - * @return string
253 - */
254 - protected static function createSiteList( $frames, $link = false, $sep = ', ' ) {
255 - $out = array();
256 - foreach( $frames as $frame ) {
257 - $text = $frame->title->getPrefixedText();
258 - if( $link ) {
259 - $out[] = "[[:{$text}]]";
260 - } else {
261 - $text = wfEscapeWikiText( $text );
262 - $out[] = $text;
263 - }
264 - }
265 - return implode( $sep, $out );
266 - }
267 -
268 -}
Index: trunk/extensions/ParserFun/PFun_Parse.php
@@ -1,173 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class for the '#parse' parser function.
6 - *
7 - * @since 0.1
8 - *
9 - * @file PFun_Parse.php
10 - * @ingroup ParserFun
11 - *
12 - * @author Daniel Werner
13 - */
14 -class ParserFunParse extends ParserHook {
15 -
16 - /**
17 - * Whether or not the input text should be parsed by Parser::braceSubstitution()
18 - * after the function has returned its value (this is possible by returning function
19 - * result as array with 'noparse' set to false).
20 - * This is always set to 'true' for new MW versions which support object style parser
21 - * function arguments sinc we call the parsing process manually in this case.
22 - *
23 - * @since 0.1
24 - *
25 - * @var boolean
26 - */
27 - protected $postParse_fallback;
28 -
29 - public function __construct() {
30 - // make this a parser function extension (no tag extension) only:
31 - parent::__construct( false, true );
32 - }
33 -
34 - /**
35 - * No LSB in pre-5.3 PHP, to be refactored later
36 - */
37 - public static function staticMagic( array &$magicWords, $langCode ) {
38 - $instance = new self;
39 - return $instance->magic( $magicWords, $langCode );
40 - }
41 -
42 - /**
43 - * No LSB in pre-5.3 PHP, to be refactored later
44 - */
45 - public static function staticInit( Parser &$parser ) {
46 - global $egParserFunEnabledFunctions;
47 - if( in_array( 'parse', $egParserFunEnabledFunctions ) ) {
48 - // only register function if not disabled by configuration
49 - $instance = new self;
50 - $instance->init( $parser );
51 - }
52 - return true;
53 - }
54 -
55 - /**
56 - * Gets the name of the parser hook.
57 - * @see ParserHook::getName
58 - *
59 - * @return string
60 - */
61 - protected function getName() {
62 - return 'parse';
63 - }
64 -
65 - /**
66 - * Returns an array containing the parameter info.
67 - * @see ParserHook::getParameterInfo
68 - *
69 - * @return array
70 - */
71 - protected function getParameterInfo( $type ) {
72 - $params = array();
73 -
74 - # input text.
75 - # since 0.1
76 - $params['text'] = new Parameter( 'text' );
77 -
78 - # if 'true', this will prevent parsing. Usful if something should be unstripped only.
79 - # since 0.1
80 - $params['parse'] = new Parameter( 'parse', Parameter::TYPE_BOOLEAN );
81 - $params['parse']->setDefault( true );
82 -
83 - # Whether the input text should be unstripped first.
84 - # since 0.1
85 - $params['unstrip'] = new Parameter( 'unstrip', Parameter::TYPE_STRING );
86 - $params['unstrip']->addCriteria( new CriterionInArray( 'none', 'nowiki', 'general', 'all' ) );
87 - $params['unstrip']->setDefault( 'none' );
88 -
89 - /**
90 - * @ToDo: Perhaps a 'context' parameter would be quite interesting.
91 - */
92 -
93 - return $params;
94 - }
95 -
96 - /**
97 - * Returns the list of default parameters.
98 - * @see ParserHook::getDefaultParameters
99 - *
100 - * @return array
101 - */
102 - protected function getDefaultParameters( $type ) {
103 - return array(
104 - array( 'text', Validator::PARAM_UNNAMED ),
105 - );
106 - }
107 -
108 - /**
109 - * Returns the parser function options.
110 - * @see ParserHook::getFunctionOptions
111 - *
112 - * @return array
113 - */
114 - protected function getFunctionOptions() {
115 - return array(
116 - 'noparse' => !$this->postParse_fallback,
117 - 'isHTML' => false
118 - );
119 - }
120 -
121 - /**
122 - * Renders and returns the output.
123 - * @see ParserHook::renderTag
124 - *
125 - * @param array $parameters
126 - *
127 - * @return string
128 - */
129 - public function render( array $parameters ) {
130 - $text = $parameters['text'];
131 -
132 - // current parsers StripState object
133 - $stripState = $this->parser->mStripState;
134 -
135 - switch( $parameters['unstrip'] ) {
136 -
137 - // case 'none': <do nothing>
138 -
139 - case 'nowiki':
140 - $text = $stripState->unstripNoWiki( $text );
141 - break;
142 -
143 - case 'general':
144 - $text = $stripState->unstripGeneral( $text );
145 - break;
146 -
147 - case 'all':
148 - $text = $stripState->unstripBoth( $text );
149 - break;
150 - }
151 -
152 - // parse if $frame is set (new MW versions) and parsing is not disabled for this one:
153 - if( $this->frame !== null && $parameters['parse'] === true ) {
154 -
155 - // we don't need the fallback since $frame is given:
156 - $this->postParse_fallback = false;
157 -
158 - /**
159 - * Doing the parsing here allows to parse <noinclude> / <includeonly> acording to the context
160 - * of where the function is defined and called. IF we use the parser function 'noparse' return
161 - * value, it would always be parsed like a page view meaning <includeonly> content would appear.
162 - */
163 - $text = $this->parser->preprocessToDom( $text, $this->frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
164 - $text = trim( $this->frame->expand( $text ) );
165 - }
166 - else {
167 - // fallback for old MW versions or in case the 'parse' #parse parameter is set to false
168 - $this->postParse_fallback = $parameters['parse'];
169 - }
170 -
171 - return $text;
172 - }
173 -
174 -}
Index: trunk/extensions/ParserFun/includes/PFun_Caller.php
@@ -0,0 +1,267 @@
 2+<?php
 3+
 4+/**
 5+ * Class for the 'CALLER' variable-style parser function.
 6+ *
 7+ * @since 0.2
 8+ *
 9+ * @file PFun_Caller.php
 10+ * @ingroup ParserFun
 11+ *
 12+ * @author Daniel Werner
 13+ */
 14+class ParserFunCaller extends ParserHook {
 15+
 16+ public function __construct() {
 17+ // make this a parser function extension (no tag extension) only:
 18+ parent::__construct( false, true, ParserHook::FH_NO_HASH );
 19+ }
 20+
 21+ /**
 22+ * No LSB in pre-5.3 PHP, to be refactored later
 23+ */
 24+ public static function staticMagic( array &$magicWords, $langCode ) {
 25+ $instance = new self;
 26+ return $instance->magic( $magicWords, $langCode );
 27+ }
 28+
 29+ /**
 30+ * No LSB in pre-5.3 PHP, to be refactored later
 31+ */
 32+ public static function staticInit( Parser &$parser ) {
 33+ global $egParserFunEnabledFunctions;
 34+ if( in_array( ExtParserFun::MAG_CALLER, $egParserFunEnabledFunctions ) ) {
 35+ // only register function if not disabled by configuration
 36+ $instance = new self;
 37+ $instance->init( $parser );
 38+ }
 39+ return true;
 40+ }
 41+
 42+ /**
 43+ * Gets the name of the parser hook.
 44+ * @see ParserHook::getName
 45+ *
 46+ * @return string
 47+ */
 48+ protected function getName() {
 49+ return 'CALLER';
 50+ }
 51+
 52+ /**
 53+ * Returns an array containing the parameter info.
 54+ * @see ParserHook::getParameterInfo
 55+ *
 56+ * @return array
 57+ */
 58+ protected function getParameterInfo( $type ) {
 59+ $params = array();
 60+
 61+ # what to get, index (any number) or certain mode (count, list)
 62+ # since 0.2
 63+ $params['mode'] = new Parameter( 'mode', Parameter::TYPE_STRING );
 64+ $params['mode']->addAliases( 'index' );
 65+
 66+ # where in the stack to start returning.
 67+ # negative value will return that many elements from the top-level caller
 68+ # since 0.2
 69+ $params['offset'] = new Parameter( 'offset', Parameter::TYPE_INTEGER );
 70+ $params['offset']->setDefault( false, false );
 71+
 72+ # max return, if negative stop that many elements from end
 73+ # since 0.2
 74+ $params['limit'] = new Parameter( 'limit', Parameter::TYPE_INTEGER );
 75+ $params['limit']->setDefault( false, false );
 76+ $params['limit']->addAliases( 'len', 'length' );
 77+
 78+ # whether to link the page names
 79+ # since 0.2
 80+ $params['linked'] = new Parameter( 'linked', Parameter::TYPE_BOOLEAN );
 81+ $params['linked']->setDefault( false );
 82+
 83+ # separator between list items
 84+ # since 0.2
 85+ $params['sep'] = new Parameter( 'sep', Parameter::TYPE_STRING );
 86+ $params['sep']->setDefault( ', ', false );
 87+
 88+ return $params;
 89+ }
 90+
 91+ /**
 92+ * Returns the list of default parameters.
 93+ * @see ParserHook::getDefaultParameters
 94+ *
 95+ * @return array
 96+ */
 97+ protected function getDefaultParameters( $type ) {
 98+ return array(
 99+ array( 'mode' ),
 100+ );
 101+ }
 102+
 103+ /**
 104+ * Renders and returns the output.
 105+ * @see ParserHook::renderTag
 106+ *
 107+ * @param array $parameters
 108+ *
 109+ * @return string
 110+ */
 111+ public function render( array $parameters ) {
 112+
 113+ $mode = $parameters['mode'];
 114+ $linked = $parameters['linked'];
 115+ $limit = $parameters['limit'];
 116+ $offset = $parameters['offset'];
 117+
 118+ if( is_numeric( $mode ) ) {
 119+ // get specific caller
 120+ /*
 121+ * do not just set $offset to $mode, $mode to 'list' and $limit to 1 here since handling
 122+ * for negative offset is different in 'list' mode. Here non-existant negative index will
 123+ * return '', in 'list' mode it will jump to the 0 element.
 124+ */
 125+ $index = (int)$mode;
 126+ $frame = self::getFrameStackItem( $this->frame, $index );
 127+
 128+ if( $frame === null ) {
 129+ return '';
 130+ }
 131+ return self::createSiteList(
 132+ array( $frame ),
 133+ $linked
 134+ );
 135+ }
 136+
 137+ $mode = strtolower( $mode );
 138+ switch( $mode ) {
 139+ case 'level':
 140+ // synnonym for 'count'
 141+ $mode = 'count';
 142+ case 'count':
 143+ case '':
 144+ /*
 145+ * '{{CALLER:}}', perhaps with additional parameters, but not in list mode except limit
 146+ * is set. Otherwise tread it similar to '{{CALLER}}' variable but with parameters.
 147+ */
 148+ if( $mode !== 'count' && $limit === false ) {
 149+ $limit = 1;
 150+ }
 151+ if( $offset === false ) {
 152+ // '{{CALLER}}' equals '{{CALLER:1}}', not 0, in count mode ignore current page.
 153+ $offset = 1;
 154+ }
 155+ // no-break, evaluate parameters in 'list' mode but count only
 156+ case 'list':
 157+ $stack = self::getFrameStack( $this->frame );
 158+ $offset = ( $offset === false ) ? 0 : $offset;
 159+ $limit = ( $limit === false ) ? null : $limit; // Validator can't have null as default...
 160+
 161+ $stack = array_slice( $stack, $offset, $limit );
 162+
 163+ if( $mode === 'count' ) {
 164+ // in 'count' mode, return the level
 165+ return count( $stack );
 166+ } else {
 167+ // normal list mode
 168+ return self::createSiteList(
 169+ $stack,
 170+ $linked,
 171+ $parameters['sep']
 172+ );
 173+ }
 174+ }
 175+
 176+ /*
 177+ * No valid operation mode or index given to first parameter!
 178+ * Return error message
 179+ */
 180+ $error = new ValidationError( wfMsgForContent( 'parserfun-invalid-caller-mode' ) );
 181+ return $this->renderFatalError( $error );
 182+ }
 183+
 184+ /**
 185+ * Returns a certain parent caller from a given frame by index. 0 returns the given frame, 1 would return
 186+ * the frame of the site which was calling the given frame and so on.
 187+ *
 188+ * @param PPFrame $frame
 189+ * @param int $index can be negative to return the element from the top-level caller. -1 would return
 190+ * the same as {{FULLPAGENAME}} would be. If the index doesn't exist, null will be returned.
 191+ *
 192+ * @return PPFrame|null
 193+ */
 194+ static function getFrameStackItem( PPFrame $frame, $index ) {
 195+ // get the whole stack or just till some certain index
 196+ $stack = self::getFrameStack( $frame, $index );
 197+
 198+ if( $index >= 0 ) {
 199+ if( array_key_exists( $index, $stack ) ) {
 200+ return $stack[ $index ];
 201+ }
 202+ } else {
 203+ // negative index, return from top-level
 204+ $index = count( $stack ) + $index;
 205+ if( $index >= 0 ) {
 206+ return $stack[ $index ];
 207+ }
 208+ }
 209+
 210+ // index doesn't exist!
 211+ return null;
 212+ }
 213+
 214+ /**
 215+ * Gets all parent frames from a frame and returns them as array with the given frame as first element.
 216+ *
 217+ * @param PPFrame $frame
 218+ * @param int $limit how many parent frames should be returned as maximum (in addition to given frame).
 219+ * Limit below 0 means no limit. 0 will just return an array with the given frame.
 220+ *
 221+ * @return PPFrame[]
 222+ */
 223+ static function getFrameStack( PPFrame $frame, $limit = -1 ) {
 224+ $frames = array();
 225+ if( $limit >= 0 ) {
 226+ $limit++; // given $frame doesn't count so this will be returned if 0 is given
 227+ }
 228+
 229+ while( $frame !== null && $limit !== 0 ) {
 230+ $frames[] = $frame;
 231+ $limit--;
 232+
 233+ if( $frame instanceof PPTemplateFrame_DOM ) {
 234+ $frame = $frame->parent;
 235+ } else {
 236+ // frame is no template, so this is the top-level frame (page being rendered)
 237+ $frame = null;
 238+ }
 239+ };
 240+
 241+ return $frames;
 242+ }
 243+
 244+ /**
 245+ * Create a list with page titles from a given array of frames, optionally linked output.
 246+ * The output ist un-parsed wiki markup, no HTML.
 247+ *
 248+ * @param array $frames the titles represented as frames
 249+ * @param bool $link whether or not to link the pages in the list
 250+ * @param string $sep glue between the pages
 251+ *
 252+ * @return string
 253+ */
 254+ protected static function createSiteList( $frames, $link = false, $sep = ', ' ) {
 255+ $out = array();
 256+ foreach( $frames as $frame ) {
 257+ $text = $frame->title->getPrefixedText();
 258+ if( $link ) {
 259+ $out[] = "[[:{$text}]]";
 260+ } else {
 261+ $text = wfEscapeWikiText( $text );
 262+ $out[] = $text;
 263+ }
 264+ }
 265+ return implode( $sep, $out );
 266+ }
 267+
 268+}
Property changes on: trunk/extensions/ParserFun/includes/PFun_Caller.php
___________________________________________________________________
Added: svn:eol-style
1269 + native
Index: trunk/extensions/ParserFun/includes/PFun_Parse.php
@@ -0,0 +1,173 @@
 2+<?php
 3+
 4+/**
 5+ * Class for the '#parse' parser function.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file PFun_Parse.php
 10+ * @ingroup ParserFun
 11+ *
 12+ * @author Daniel Werner
 13+ */
 14+class ParserFunParse extends ParserHook {
 15+
 16+ /**
 17+ * Whether or not the input text should be parsed by Parser::braceSubstitution()
 18+ * after the function has returned its value (this is possible by returning function
 19+ * result as array with 'noparse' set to false).
 20+ * This is always set to 'true' for new MW versions which support object style parser
 21+ * function arguments sinc we call the parsing process manually in this case.
 22+ *
 23+ * @since 0.1
 24+ *
 25+ * @var boolean
 26+ */
 27+ protected $postParse_fallback;
 28+
 29+ public function __construct() {
 30+ // make this a parser function extension (no tag extension) only:
 31+ parent::__construct( false, true );
 32+ }
 33+
 34+ /**
 35+ * No LSB in pre-5.3 PHP, to be refactored later
 36+ */
 37+ public static function staticMagic( array &$magicWords, $langCode ) {
 38+ $instance = new self;
 39+ return $instance->magic( $magicWords, $langCode );
 40+ }
 41+
 42+ /**
 43+ * No LSB in pre-5.3 PHP, to be refactored later
 44+ */
 45+ public static function staticInit( Parser &$parser ) {
 46+ global $egParserFunEnabledFunctions;
 47+ if( in_array( 'parse', $egParserFunEnabledFunctions ) ) {
 48+ // only register function if not disabled by configuration
 49+ $instance = new self;
 50+ $instance->init( $parser );
 51+ }
 52+ return true;
 53+ }
 54+
 55+ /**
 56+ * Gets the name of the parser hook.
 57+ * @see ParserHook::getName
 58+ *
 59+ * @return string
 60+ */
 61+ protected function getName() {
 62+ return 'parse';
 63+ }
 64+
 65+ /**
 66+ * Returns an array containing the parameter info.
 67+ * @see ParserHook::getParameterInfo
 68+ *
 69+ * @return array
 70+ */
 71+ protected function getParameterInfo( $type ) {
 72+ $params = array();
 73+
 74+ # input text.
 75+ # since 0.1
 76+ $params['text'] = new Parameter( 'text' );
 77+
 78+ # if 'true', this will prevent parsing. Usful if something should be unstripped only.
 79+ # since 0.1
 80+ $params['parse'] = new Parameter( 'parse', Parameter::TYPE_BOOLEAN );
 81+ $params['parse']->setDefault( true );
 82+
 83+ # Whether the input text should be unstripped first.
 84+ # since 0.1
 85+ $params['unstrip'] = new Parameter( 'unstrip', Parameter::TYPE_STRING );
 86+ $params['unstrip']->addCriteria( new CriterionInArray( 'none', 'nowiki', 'general', 'all' ) );
 87+ $params['unstrip']->setDefault( 'none' );
 88+
 89+ /**
 90+ * @ToDo: Perhaps a 'context' parameter would be quite interesting.
 91+ */
 92+
 93+ return $params;
 94+ }
 95+
 96+ /**
 97+ * Returns the list of default parameters.
 98+ * @see ParserHook::getDefaultParameters
 99+ *
 100+ * @return array
 101+ */
 102+ protected function getDefaultParameters( $type ) {
 103+ return array(
 104+ array( 'text', Validator::PARAM_UNNAMED ),
 105+ );
 106+ }
 107+
 108+ /**
 109+ * Returns the parser function options.
 110+ * @see ParserHook::getFunctionOptions
 111+ *
 112+ * @return array
 113+ */
 114+ protected function getFunctionOptions() {
 115+ return array(
 116+ 'noparse' => !$this->postParse_fallback,
 117+ 'isHTML' => false
 118+ );
 119+ }
 120+
 121+ /**
 122+ * Renders and returns the output.
 123+ * @see ParserHook::renderTag
 124+ *
 125+ * @param array $parameters
 126+ *
 127+ * @return string
 128+ */
 129+ public function render( array $parameters ) {
 130+ $text = $parameters['text'];
 131+
 132+ // current parsers StripState object
 133+ $stripState = $this->parser->mStripState;
 134+
 135+ switch( $parameters['unstrip'] ) {
 136+
 137+ // case 'none': <do nothing>
 138+
 139+ case 'nowiki':
 140+ $text = $stripState->unstripNoWiki( $text );
 141+ break;
 142+
 143+ case 'general':
 144+ $text = $stripState->unstripGeneral( $text );
 145+ break;
 146+
 147+ case 'all':
 148+ $text = $stripState->unstripBoth( $text );
 149+ break;
 150+ }
 151+
 152+ // parse if $frame is set (new MW versions) and parsing is not disabled for this one:
 153+ if( $this->frame !== null && $parameters['parse'] === true ) {
 154+
 155+ // we don't need the fallback since $frame is given:
 156+ $this->postParse_fallback = false;
 157+
 158+ /**
 159+ * Doing the parsing here allows to parse <noinclude> / <includeonly> acording to the context
 160+ * of where the function is defined and called. IF we use the parser function 'noparse' return
 161+ * value, it would always be parsed like a page view meaning <includeonly> content would appear.
 162+ */
 163+ $text = $this->parser->preprocessToDom( $text, $this->frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
 164+ $text = trim( $this->frame->expand( $text ) );
 165+ }
 166+ else {
 167+ // fallback for old MW versions or in case the 'parse' #parse parameter is set to false
 168+ $this->postParse_fallback = $parameters['parse'];
 169+ }
 170+
 171+ return $text;
 172+ }
 173+
 174+}
Property changes on: trunk/extensions/ParserFun/includes/PFun_Parse.php
___________________________________________________________________
Added: svn:eol-style
1175 + native
Index: trunk/extensions/ParserFun/ParserFun.php
@@ -60,8 +60,8 @@
6161
6262
6363 // 'parse' and 'CALLER' parser function initializations:
64 -$wgAutoloadClasses['ParserFunParse' ] = ExtParserFun::getDir() . '/PFun_Parse.php';
65 -$wgAutoloadClasses['ParserFunCaller'] = ExtParserFun::getDir() . '/PFun_Caller.php';
 64+$wgAutoloadClasses['ParserFunParse' ] = ExtParserFun::getDir() . '/includes/PFun_Parse.php';
 65+$wgAutoloadClasses['ParserFunCaller'] = ExtParserFun::getDir() . '/includes/PFun_Caller.php';
6666
6767 $wgHooks['ParserFirstCallInit'][] = 'ParserFunParse::staticInit';
6868 $wgHooks['LanguageGetMagic' ][] = 'ParserFunParse::staticMagic';

Status & tagging log