r79579 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79578‎ | r79579 | r79580 >
Date:13:54, 4 January 2011
Author:catrope
Status:ok
Tags:
Comment:
Stylize LabeledSectionTransclusion
Modified paths:
  • /trunk/extensions/LabeledSectionTransclusion/compat.php (modified) (history)
  • /trunk/extensions/LabeledSectionTransclusion/lst.php (modified) (history)
  • /trunk/extensions/LabeledSectionTransclusion/lsth.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LabeledSectionTransclusion/lsth.php
@@ -2,7 +2,7 @@
33 if ( ! defined( 'MEDIAWIKI' ) )
44 die();
55
6 -/**#@+
 6+/**#@+
77 *
88 * A parser extension that further extends labeled section transclusion,
99 * adding a function, #lsth for transcluding marked sections of text,
@@ -23,15 +23,15 @@
2424 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
2525 */
2626
27 -##
 27+# #
2828 # Standard initialisation code
29 -##
 29+# #
3030
31 -$wgExtensionFunctions[]="wfLabeledSectionTransclusionHeading";
 31+$wgExtensionFunctions[] = "wfLabeledSectionTransclusionHeading";
3232 $wgHooks['LanguageGetMagic'][] = 'wfLabeledSectionTransclusionHeadingMagic';
3333 $wgParserTestFiles[] = dirname( __FILE__ ) . "/lsthParserTests.txt";
3434
35 -function wfLabeledSectionTransclusionHeading()
 35+function wfLabeledSectionTransclusionHeading()
3636 {
3737 global $wgParser;
3838 $wgParser->setFunctionHook( 'lsth', 'wfLstIncludeHeading' );
@@ -43,66 +43,66 @@
4444 return true;
4545 }
4646
47 -///section inclusion - include all matching sections
48 -function wfLstIncludeHeading($parser, $page='', $sec='', $to='')
 47+/// section inclusion - include all matching sections
 48+function wfLstIncludeHeading( $parser, $page = '', $sec = '', $to = '' )
4949 {
50 - if (LabeledSectionTransclusion::getTemplateText_($parser, $page, $title, $text) == false)
 50+ if ( LabeledSectionTransclusion::getTemplateText_( $parser, $page, $title, $text ) == false )
5151 return $text;
5252
53 - //Generate a regex to match the === classical heading section(s) === we're
54 - //interested in.
55 - if ($sec == '') {
 53+ // Generate a regex to match the === classical heading section(s) === we're
 54+ // interested in.
 55+ if ( $sec == '' ) {
5656 $begin_off = 0;
5757 $head_len = 6;
5858 } else {
59 - $pat = '^(={1,6})\s*' . preg_quote($sec, '/') . '\s*\1\s*($)' ;
60 - if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE) ) {
 59+ $pat = '^(={1,6})\s*' . preg_quote( $sec, '/' ) . '\s*\1\s*($)' ;
 60+ if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE ) ) {
6161 $begin_off = $m[2][1];
62 - $head_len = strlen($m[1][0]);
63 - //wfDebug( "LSTH: offset is $begin_off" );
 62+ $head_len = strlen( $m[1][0] );
 63+ // wfDebug( "LSTH: offset is $begin_off" );
6464 } else {
65 - //wfDebug( "LSTH: match failed: '$pat'" );
 65+ // wfDebug( "LSTH: match failed: '$pat'" );
6666 return '';
6767 }
68 -
 68+
6969 }
7070
71 - if ($to != '') {
72 - //if $to is supplied, try and match it. If we don't match, just
73 - //ignore it.
74 - $pat = '^(={1,6})\s*' . preg_quote($to, '/') . '\s*\1\s*$';
75 - if (preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off))
76 - $end_off = $m[0][1]-1;
 71+ if ( $to != '' ) {
 72+ // if $to is supplied, try and match it. If we don't match, just
 73+ // ignore it.
 74+ $pat = '^(={1,6})\s*' . preg_quote( $to, '/' ) . '\s*\1\s*$';
 75+ if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off ) )
 76+ $end_off = $m[0][1] -1;
7777 }
7878
7979
80 - if (! isset($end_off)) {
81 - $pat = '^(={1,'.$head_len.'})(?!=).*?\1\s*$';
82 - if (preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off))
83 - $end_off = $m[0][1]-1;
84 - else
85 - wfDebug("LSTH: fail end match: '$pat'");
 80+ if ( ! isset( $end_off ) ) {
 81+ $pat = '^(={1,' . $head_len . '})(?!=).*?\1\s*$';
 82+ if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off ) )
 83+ $end_off = $m[0][1] -1;
 84+ else
 85+ wfDebug( "LSTH: fail end match: '$pat'" );
8686
87 - //wfDebug("LSTH:head len is $head_len, pat is $pat, head is '.$m[1][0]'";
88 - }
 87+ // wfDebug("LSTH:head len is $head_len, pat is $pat, head is '.$m[1][0]'";
 88+ }
8989
90 - $nhead = LabeledSectionTransclusion::countHeadings_($text, $begin_off);
 90+ $nhead = LabeledSectionTransclusion::countHeadings_( $text, $begin_off );
9191 wfDebug( "LSTH: head offset = $nhead" );
9292
93 - if (isset($end_off))
94 - $result = substr($text, $begin_off, $end_off - $begin_off);
 93+ if ( isset( $end_off ) )
 94+ $result = substr( $text, $begin_off, $end_off - $begin_off );
9595 else
96 - $result = substr($text, $begin_off);
97 -
 96+ $result = substr( $text, $begin_off );
9897
99 - if (method_exists($parser,'getPreprocessor'))
 98+
 99+ if ( method_exists( $parser, 'getPreprocessor' ) )
100100 {
101101 $frame = $parser->getPreprocessor()->newFrame();
102102 $dom = $parser->preprocessToDom( $result );
103103 $result = $frame->expand( $dom );
104104 }
105 -
106105
107 - return LabeledSectionTransclusion::parse_($parser,$title,$result, "#lsth:${page}|${sec}", $nhead);
 106+
 107+ return LabeledSectionTransclusion::parse_( $parser, $title, $result, "#lsth:${page}|${sec}", $nhead );
108108 }
109109
Index: trunk/extensions/LabeledSectionTransclusion/lst.php
@@ -2,7 +2,7 @@
33 if ( ! defined( 'MEDIAWIKI' ) )
44 die();
55 /**#@+
6 - * A parser extension that adds two functions, #lst and #lstx, and the
 6+ * A parser extension that adds two functions, #lst and #lstx, and the
77 * <section> tag, for transcluding marked sections of text.
88 *
99 * @file
@@ -32,7 +32,7 @@
3333 'descriptionmsg' => 'lst-desc',
3434 );
3535 $wgParserTestFiles[] = dirname( __FILE__ ) . "/lstParserTests.txt";
36 -$wgExtensionMessagesFiles['LabeledSectionTransclusion'] = dirname(__FILE__) . '/lst.i18n.php';
 36+$wgExtensionMessagesFiles['LabeledSectionTransclusion'] = dirname( __FILE__ ) . '/lst.i18n.php';
3737
3838 // Local settings variable
3939 // Must be set now to avoid injection via register_globals
@@ -52,7 +52,7 @@
5353 }
5454 }
5555
56 - /// Add the magic words - possibly with more readable aliases
 56+ // / Add the magic words - possibly with more readable aliases
5757 static function setupMagic( &$magicWords, $langCode ) {
5858 global $wgParser, $wgLstLocal;
5959
@@ -60,21 +60,21 @@
6161 case 'de':
6262 $include = 'Abschnitt';
6363 $exclude = 'Abschnitt-x';
64 - $wgLstLocal = array( 'section' => 'Abschnitt', 'begin' => 'Anfang', 'end' => 'Ende') ;
 64+ $wgLstLocal = array( 'section' => 'Abschnitt', 'begin' => 'Anfang', 'end' => 'Ende' ) ;
6565 break;
6666 case 'he':
6767 $include = 'קטע';
6868 $exclude = 'בלי קטע';
69 - $wgLstLocal = array( 'section' => 'קטע', 'begin' => 'התחלה', 'end' => 'סוף') ;
 69+ $wgLstLocal = array( 'section' => 'קטע', 'begin' => 'התחלה', 'end' => 'סוף' ) ;
7070 break;
7171 case 'pt':
7272 $include = 'trecho';
7373 $exclude = 'trecho-x';
74 - $wgLstLocal = array( 'section' => 'trecho', 'begin' => 'começo', 'end' => 'fim');
 74+ $wgLstLocal = array( 'section' => 'trecho', 'begin' => 'começo', 'end' => 'fim' );
7575 break;
7676 }
77 -
78 - if( isset( $include ) ) {
 77+
 78+ if ( isset( $include ) ) {
7979 $magicWords['lst'] = array( 0, 'lst', 'section', $include );
8080 $magicWords['lstx'] = array( 0, 'lstx', 'section-x', $exclude );
8181 $wgParser->setHook( $include, array( __CLASS__, 'noop' ) );
@@ -82,7 +82,7 @@
8383 $magicWords['lst'] = array( 0, 'lst', 'section' );
8484 $magicWords['lstx'] = array( 0, 'lstx', 'section-x' );
8585 }
86 -
 86+
8787 return true;
8888 }
8989
@@ -91,28 +91,28 @@
9292 # at a low level. This is the general transclusion functionality
9393 ##############################################################
9494
95 - ///Register what we're working on in the parser, so we don't fall into a trap.
96 - static function open_($parser, $part1)
 95+ /// Register what we're working on in the parser, so we don't fall into a trap.
 96+ static function open_( $parser, $part1 )
9797 {
9898 // Infinite loop test
9999 if ( isset( $parser->mTemplatePath[$part1] ) ) {
100 - wfDebug( __METHOD__.": template loop broken at '$part1'\n" );
 100+ wfDebug( __METHOD__ . ": template loop broken at '$part1'\n" );
101101 return false;
102102 } else {
103103 $parser->mTemplatePath[$part1] = 1;
104104 return true;
105105 }
106 -
 106+
107107 }
108108
109 - ///Finish processing the function.
110 - static function close_($parser, $part1)
 109+ /// Finish processing the function.
 110+ static function close_( $parser, $part1 )
111111 {
112112 // Infinite loop test
113113 if ( isset( $parser->mTemplatePath[$part1] ) ) {
114114 unset( $parser->mTemplatePath[$part1] );
115115 } else {
116 - wfDebug( __METHOD__.": close unopened template loop at '$part1'\n" );
 116+ wfDebug( __METHOD__ . ": close unopened template loop at '$part1'\n" );
117117 }
118118 }
119119
@@ -128,21 +128,21 @@
129129 * @todo handle mixed-case </section>
130130 * @private
131131 */
132 - static function parse_($parser, $title, $text, $part1, $skiphead=0)
 132+ static function parse_( $parser, $title, $text, $part1, $skiphead = 0 )
133133 {
134134 // if someone tries something like<section begin=blah>lst only</section>
135135 // text, may as well do the right thing.
136 - $text = str_replace('</section>', '', $text);
 136+ $text = str_replace( '</section>', '', $text );
137137
138 - if (self::open_($parser, $part1)) {
139 - //Try to get edit sections correct by munging around the parser's guts.
140 - return array($text, 'title'=>$title, 'replaceHeadings'=>true,
141 - 'headingOffset'=>$skiphead, 'noparse'=>false, 'noargs'=>false);
 138+ if ( self::open_( $parser, $part1 ) ) {
 139+ // Try to get edit sections correct by munging around the parser's guts.
 140+ return array( $text, 'title' => $title, 'replaceHeadings' => true,
 141+ 'headingOffset' => $skiphead, 'noparse' => false, 'noargs' => false );
142142 } else {
143 - return "[[" . $title->getPrefixedText() . "]]".
 143+ return "[[" . $title->getPrefixedText() . "]]" .
144144 "<!-- WARNING: LST loop detected -->";
145145 }
146 -
 146+
147147 }
148148
149149 ##############################################################
@@ -158,7 +158,7 @@
159159 * @param Parser $parser
160160 * @return string HTML output
161161 */
162 - static function noop( $in, $assocArgs=array(), $parser=null ) {
 162+ static function noop( $in, $assocArgs = array(), $parser = null ) {
163163 return '';
164164 }
165165
@@ -171,7 +171,7 @@
172172 * @return string regex
173173 * @private
174174 */
175 - static function getPattern_($sec, $to)
 175+ static function getPattern_( $sec, $to )
176176 {
177177 global $wgLstLocal;
178178
@@ -182,15 +182,15 @@
183183 $endAttr = self::getAttrPattern_( $to, 'end' );
184184 }
185185
186 - $to_sec = ($to == '')?$sec : $to;
187 - $sec = preg_quote($sec, '/');
188 - $to_sec = preg_quote($to_sec, '/');
189 - if (isset($wgLstLocal)){
 186+ $to_sec = ( $to == '' ) ? $sec : $to;
 187+ $sec = preg_quote( $sec, '/' );
 188+ $to_sec = preg_quote( $to_sec, '/' );
 189+ if ( isset( $wgLstLocal ) ) {
190190 $section_re = "(?i:section|$wgLstLocal[section])";
191191 } else {
192192 $section_re = "(?i:section)";
193193 }
194 -
 194+
195195 return "/<$section_re$beginAttr\/?>(.*?)\n?<$section_re$endAttr\/?>/s";
196196 }
197197
@@ -201,9 +201,9 @@
202202 */
203203 static function getAttrPattern_( $sec, $type ) {
204204 global $wgLstLocal;
205 - $sec = preg_quote($sec, '/');
206 - $ws = "(?:\s+[^>]*)?"; //was like $ws="\s*"
207 - if (isset($wgLstLocal)){
 205+ $sec = preg_quote( $sec, '/' );
 206+ $ws = "(?:\s+[^>]*)?"; // was like $ws="\s*"
 207+ if ( isset( $wgLstLocal ) ) {
208208 if ( $type == 'begin' ) {
209209 $attrName = "(?i:begin|{$wgLstLocal['begin']})";
210210 } else {
@@ -230,16 +230,16 @@
231231 * @return int Number of matches
232232 * @private
233233 */
234 - static function countHeadings_($text,$limit)
 234+ static function countHeadings_( $text, $limit )
235235 {
236236 $pat = '^(={1,6}).+\1\s*$()';
237 -
238 - //return preg_match_all( "/$pat/im", substr($text,0,$limit), $m);
239237
 238+ // return preg_match_all( "/$pat/im", substr($text,0,$limit), $m);
 239+
240240 $count = 0;
241241 $offset = 0;
242 - while (preg_match("/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $offset)) {
243 - if ($m[2][1] > $limit)
 242+ while ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $offset ) ) {
 243+ if ( $m[2][1] > $limit )
244244 break;
245245
246246 $count++;
@@ -260,23 +260,23 @@
261261 * @return string bool true if returning text, false if target not found
262262 * @private
263263 */
264 - static function getTemplateText_($parser, $page, &$title, &$text)
 264+ static function getTemplateText_( $parser, $page, &$title, &$text )
265265 {
266 - $title = Title::newFromText($page);
267 -
268 - if (is_null($title) ) {
 266+ $title = Title::newFromText( $page );
 267+
 268+ if ( is_null( $title ) ) {
269269 $text = '';
270270 return true;
271271 } else {
272 - if (method_exists($parser, 'fetchTemplateAndTitle')) {
273 - list($text,$title) = $parser->fetchTemplateAndTitle($title);
 272+ if ( method_exists( $parser, 'fetchTemplateAndTitle' ) ) {
 273+ list( $text, $title ) = $parser->fetchTemplateAndTitle( $title );
274274 } else {
275 - $text = $parser->fetchTemplate($title);
 275+ $text = $parser->fetchTemplate( $title );
276276 }
277277 }
278 -
279 - //if article doesn't exist, return a red link.
280 - if ($text == false) {
 278+
 279+ // if article doesn't exist, return a red link.
 280+ if ( $text == false ) {
281281 $text = "[[" . $title->getPrefixedText() . "]]";
282282 return false;
283283 } else {
@@ -294,25 +294,25 @@
295295 * @param string $to Optional named section to end at
296296 * @return mixed wikitext output
297297 */
298 - function pfuncInclude($parser, $page='', $sec='', $to='')
 298+ function pfuncInclude( $parser, $page = '', $sec = '', $to = '' )
299299 {
300 - if (self::getTemplateText_($parser, $page, $title, $text) == false)
 300+ if ( self::getTemplateText_( $parser, $page, $title, $text ) == false )
301301 return $text;
302 - $pat = self::getPattern_($sec,$to);
 302+ $pat = self::getPattern_( $sec, $to );
303303
304 - if(preg_match_all( $pat, $text, $m, PREG_OFFSET_CAPTURE)) {
305 - $headings = self::countHeadings_($text, $m[0][0][1]);
 304+ if ( preg_match_all( $pat, $text, $m, PREG_OFFSET_CAPTURE ) ) {
 305+ $headings = self::countHeadings_( $text, $m[0][0][1] );
306306 } else {
307307 $headings = 0;
308308 }
309 -
 309+
310310 $text = '';
311 - foreach ($m[1] as $piece) {
 311+ foreach ( $m[1] as $piece ) {
312312 $text .= $piece[0];
313313 }
314314
315 - //wfDebug(__METHOD__.": skip $headings headings");
316 - return self::parse_($parser,$title,$text, "#lst:${page}|${sec}", $headings);
 315+ // wfDebug(__METHOD__.": skip $headings headings");
 316+ return self::parse_( $parser, $title, $text, "#lst:${page}|${sec}", $headings );
317317 }
318318
319319 /**
@@ -328,14 +328,14 @@
329329 return '';
330330 }
331331 if ( !$frame->loopCheck( $title ) ) {
332 - return "[[" . $title->getPrefixedText() . "]]".
 332+ return "[[" . $title->getPrefixedText() . "]]" .
333333 "<!-- WARNING: LST loop detected -->";
334334 }
335335
336336 list( $root, $finalTitle ) = $parser->getTemplateDom( $title );
337337
338338 // if article doesn't exist, return a red link.
339 - if ($root === false) {
 339+ if ( $root === false ) {
340340 return "[[" . $title->getPrefixedText() . "]]";
341341 }
342342
@@ -368,13 +368,13 @@
369369 return compact( 'dom', 'root', 'newFrame', 'repl', 'beginRegex', 'endRegex' );
370370 }
371371
372 - /**
 372+ /**
373373 * Returns true if the given extension name is "section"
374374 */
375375 static function isSection( $name ) {
376376 global $wgLstLocal;
377377 $name = strtolower( $name );
378 - return $name == 'section'
 378+ return $name == 'section'
379379 || ( isset( $wgLstLocal['section'] ) && strtolower( $wgLstLocal['section'] ) == $name );
380380 }
381381
@@ -459,13 +459,13 @@
460460 * @param string $to Optional named section to end at
461461 * @return mixed wikitext output
462462 */
463 - static function pfuncExclude($parser, $page='', $sec='', $repl='',$to='')
 463+ static function pfuncExclude( $parser, $page = '', $sec = '', $repl = '', $to = '' )
464464 {
465 - if (self::getTemplateText_($parser, $page, $title, $text) == false)
 465+ if ( self::getTemplateText_( $parser, $page, $title, $text ) == false )
466466 return $text;
467 - $pat = self::getPattern_($sec,$to);
468 - $text = preg_replace( $pat, $repl, $text);
469 - return self::parse_($parser,$title,$text, "#lstx:$page|$sec");
 467+ $pat = self::getPattern_( $sec, $to );
 468+ $text = preg_replace( $pat, $repl, $text );
 469+ return self::parse_( $parser, $title, $text, "#lstx:$page|$sec" );
470470 }
471471
472472 /**
Index: trunk/extensions/LabeledSectionTransclusion/compat.php
@@ -9,20 +9,20 @@
1010
1111 **/
1212
13 -///Fetch the page to be transcluded from the database.
14 -function wfLst_fetch_($parser, $page, $ns = NS_MAIN)
 13+/// Fetch the page to be transcluded from the database.
 14+function wfLst_fetch_( $parser, $page, $ns = NS_MAIN )
1515 {
16 - $title = Title::newFromText($page,$ns);
 16+ $title = Title::newFromText( $page, $ns );
1717 if ( !is_null( $title ) ) {
18 - $text = $parser->fetchTemplate($title);
 18+ $text = $parser->fetchTemplate( $title );
1919 }
2020 return $text;
2121 }
2222
2323
24 -function wfLstIncludeHeading2($parser, $page='', $sec='', $to='')
 24+function wfLstIncludeHeading2( $parser, $page = '', $sec = '', $to = '' )
2525 {
26 - return wfLstIncludeHeading($parser, $page, $sec, $to);
 26+ return wfLstIncludeHeading( $parser, $page, $sec, $to );
2727 }
2828
2929

Status & tagging log