Performance optimisation: Merge two for loops into one.
[lhc/web/wiklou.git] / includes / Parser.php
1 <?php
2
3 // require_once('Tokenizer.php');
4
5 # PHP Parser
6 #
7 # Processes wiki markup
8 #
9 # There are two main entry points into the Parser class: parse() and preSaveTransform().
10 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
11 #
12 # Globals used:
13 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
14 #
15 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
16 #
17 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
18 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
19 # $wgLocaltimezone
20 #
21 # * only within ParserOptions
22 #
23 #
24 #----------------------------------------
25 # Variable substitution O(N^2) attack
26 #-----------------------------------------
27 # Without countermeasures, it would be possible to attack the parser by saving a page
28 # filled with a large number of inclusions of large pages. The size of the generated
29 # page would be proportional to the square of the input size. Hence, we limit the number
30 # of inclusions of any given page, thus bringing any attack back to O(N).
31 #
32
33 define( "MAX_INCLUDE_REPEAT", 5 );
34 define( "MAX_INCLUDE_SIZE", 1000000 ); // 1 Million
35
36 # Allowed values for $mOutputType
37 define( "OT_HTML", 1 );
38 define( "OT_WIKI", 2 );
39 define( "OT_MSG", 3 );
40
41 # string parameter for extractTags which will cause it
42 # to strip HTML comments in addition to regular
43 # <XML>-style tags. This should not be anything we
44 # may want to use in wikisyntax
45 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
46
47 # prefix for escaping, used in two functions at least
48 define( "UNIQ_PREFIX", "NaodW29");
49
50 class Parser
51 {
52 # Persistent:
53 var $mTagHooks;
54
55 # Cleared with clearState():
56 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
57 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
58
59 # Temporary:
60 var $mOptions, $mTitle, $mOutputType,
61 $mTemplates, // cache of already loaded templates, avoids
62 // multiple SQL queries for the same string
63 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
64 // in this path. Used for loop detection.
65
66 function Parser() {
67 $this->mTemplates = array();
68 $this->mTemplatePath = array();
69 $this->mTagHooks = array();
70 $this->clearState();
71 }
72
73 function clearState() {
74 $this->mOutput = new ParserOutput;
75 $this->mAutonumber = 0;
76 $this->mLastSection = "";
77 $this->mDTopen = false;
78 $this->mVariables = false;
79 $this->mIncludeCount = array();
80 $this->mStripState = array();
81 $this->mArgStack = array();
82 $this->mInPre = false;
83 }
84
85 # First pass--just handle <nowiki> sections, pass the rest off
86 # to internalParse() which does all the real work.
87 #
88 # Returns a ParserOutput
89 #
90 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
91 global $wgUseTidy;
92 $fname = "Parser::parse";
93 wfProfileIn( $fname );
94
95 if ( $clearState ) {
96 $this->clearState();
97 }
98
99 $this->mOptions = $options;
100 $this->mTitle =& $title;
101 $this->mOutputType = OT_HTML;
102
103 $stripState = NULL;
104 $text = $this->strip( $text, $this->mStripState );
105 $text = $this->internalParse( $text, $linestart );
106 $text = $this->unstrip( $text, $this->mStripState );
107 # Clean up special characters, only run once, next-to-last before doBlockLevels
108 if(!$wgUseTidy) {
109 $fixtags = array(
110 # french spaces, last one Guillemet-left
111 # only if there is something before the space
112 '/(.) (\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
113 # french spaces, Guillemet-right
114 "/(\\302\\253) /i"=>"\\1&nbsp;",
115 '/<hr *>/i' => '<hr />',
116 '/<br *>/i' => '<br />',
117 '/<center *>/i' => '<div class="center">',
118 '/<\\/center *>/i' => '</div>',
119 # Clean up spare ampersands; note that we probably ought to be
120 # more careful about named entities.
121 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
122 );
123 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
124 } else {
125 $fixtags = array(
126 # french spaces, last one Guillemet-left
127 '/ (\\?|:|!|\\302\\273)/i' => '&nbsp;\\1',
128 # french spaces, Guillemet-right
129 '/(\\302\\253) /i' => '\\1&nbsp;',
130 '/([^> ]+(&#x30(1|3|9);)[^< ]*)/i' => '<span class="diacrit">\\1</span>',
131 '/<center *>/i' => '<div class="center">',
132 '/<\\/center *>/i' => '</div>'
133 );
134 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
135 }
136 # only once and last
137 $text = $this->doBlockLevels( $text, $linestart );
138 $text = $this->unstripNoWiki( $text, $this->mStripState );
139 if($wgUseTidy) {
140 $text = $this->tidy($text);
141 }
142 $this->mOutput->setText( $text );
143 wfProfileOut( $fname );
144 return $this->mOutput;
145 }
146
147 /* static */ function getRandomString() {
148 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
149 }
150
151 # Replaces all occurrences of <$tag>content</$tag> in the text
152 # with a random marker and returns the new text. the output parameter
153 # $content will be an associative array filled with data on the form
154 # $unique_marker => content.
155
156 # If $content is already set, the additional entries will be appended
157
158 # If $tag is set to STRIP_COMMENTS, the function will extract
159 # <!-- HTML comments -->
160
161 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
162 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
163 if ( !$content ) {
164 $content = array( );
165 }
166 $n = 1;
167 $stripped = '';
168
169 while ( '' != $text ) {
170 if($tag==STRIP_COMMENTS) {
171 $p = preg_split( '/<!--/i', $text, 2 );
172 } else {
173 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
174 }
175 $stripped .= $p[0];
176 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
177 $text = '';
178 } else {
179 if($tag==STRIP_COMMENTS) {
180 $q = preg_split( '/-->/i', $p[1], 2 );
181 } else {
182 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
183 }
184 $marker = $rnd . sprintf('%08X', $n++);
185 $content[$marker] = $q[0];
186 $stripped .= $marker;
187 $text = $q[1];
188 }
189 }
190 return $stripped;
191 }
192
193 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
194 # If $render is set, performs necessary rendering operations on plugins
195 # Returns the text, and fills an array with data needed in unstrip()
196 # If the $state is already a valid strip state, it adds to the state
197
198 # When $stripcomments is set, HTML comments <!-- like this -->
199 # will be stripped in addition to other tags. This is important
200 # for section editing, where these comments cause confusion when
201 # counting the sections in the wikisource
202 function strip( $text, &$state, $stripcomments = false ) {
203 $render = ($this->mOutputType == OT_HTML);
204 $html_content = array();
205 $nowiki_content = array();
206 $math_content = array();
207 $pre_content = array();
208 $comment_content = array();
209 $ext_content = array();
210
211 # Replace any instances of the placeholders
212 $uniq_prefix = UNIQ_PREFIX;
213 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
214
215 # html
216 global $wgRawHtml;
217 if( $wgRawHtml ) {
218 $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
219 foreach( $html_content as $marker => $content ) {
220 if ($render ) {
221 # Raw and unchecked for validity.
222 $html_content[$marker] = $content;
223 } else {
224 $html_content[$marker] = "<html>$content</html>";
225 }
226 }
227 }
228
229 # nowiki
230 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
231 foreach( $nowiki_content as $marker => $content ) {
232 if( $render ){
233 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
234 } else {
235 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
236 }
237 }
238
239 # math
240 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
241 foreach( $math_content as $marker => $content ){
242 if( $render ) {
243 if( $this->mOptions->getUseTeX() ) {
244 $math_content[$marker] = renderMath( $content );
245 } else {
246 $math_content[$marker] = "&lt;math&gt;$content&lt;math&gt;";
247 }
248 } else {
249 $math_content[$marker] = "<math>$content</math>";
250 }
251 }
252
253 # pre
254 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
255 foreach( $pre_content as $marker => $content ){
256 if( $render ){
257 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
258 } else {
259 $pre_content[$marker] = "<pre>$content</pre>";
260 }
261 }
262
263 # Comments
264 if($stripcomments) {
265 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
266 foreach( $comment_content as $marker => $content ){
267 $comment_content[$marker] = "<!--$content-->";
268 }
269 }
270
271 # Extensions
272 foreach ( $this->mTagHooks as $tag => $callback ) {
273 $ext_contents[$tag] = array();
274 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
275 foreach( $ext_content[$tag] as $marker => $content ) {
276 if ( $render ) {
277 $ext_content[$tag][$marker] = $callback( $content );
278 } else {
279 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
280 }
281 }
282 }
283
284 # Merge state with the pre-existing state, if there is one
285 if ( $state ) {
286 $state['html'] = $state['html'] + $html_content;
287 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
288 $state['math'] = $state['math'] + $math_content;
289 $state['pre'] = $state['pre'] + $pre_content;
290 $state['comment'] = $state['comment'] + $comment_content;
291
292 foreach( $ext_content as $tag => $array ) {
293 if ( array_key_exists( $tag, $state ) ) {
294 $state[$tag] = $state[$tag] + $array;
295 }
296 }
297 } else {
298 $state = array(
299 'html' => $html_content,
300 'nowiki' => $nowiki_content,
301 'math' => $math_content,
302 'pre' => $pre_content,
303 'comment' => $comment_content,
304 ) + $ext_content;
305 }
306 return $text;
307 }
308
309 # always call unstripNoWiki() after this one
310 function unstrip( $text, &$state ) {
311 # Must expand in reverse order, otherwise nested tags will be corrupted
312 $contentDict = end( $state );
313 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
314 if( key($state) != 'nowiki' && key($state) != 'html') {
315 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
316 $text = str_replace( key( $contentDict ), $content, $text );
317 }
318 }
319 }
320
321 return $text;
322 }
323 # always call this after unstrip() to preserve the order
324 function unstripNoWiki( $text, &$state ) {
325 # Must expand in reverse order, otherwise nested tags will be corrupted
326 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
327 $text = str_replace( key( $state['nowiki'] ), $content, $text );
328 }
329
330 global $wgRawHtml;
331 if ($wgRawHtml) {
332 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
333 $text = str_replace( key( $state['html'] ), $content, $text );
334 }
335 }
336
337 return $text;
338 }
339
340 # Add an item to the strip state
341 # Returns the unique tag which must be inserted into the stripped text
342 # The tag will be replaced with the original text in unstrip()
343
344 function insertStripItem( $text, &$state ) {
345 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
346 if ( !$state ) {
347 $state = array(
348 'html' => array(),
349 'nowiki' => array(),
350 'math' => array(),
351 'pre' => array()
352 );
353 }
354 $state['item'][$rnd] = $text;
355 return $rnd;
356 }
357
358 # categoryMagic
359 # generate a list of subcategories and pages for a category
360 # depending on wfMsg("usenewcategorypage") it either calls the new
361 # or the old code. The new code will not work properly for some
362 # languages due to sorting issues, so they might want to turn it
363 # off.
364 function categoryMagic() {
365 $msg = wfMsg('usenewcategorypage');
366 if ( '0' == @$msg[0] )
367 {
368 return $this->oldCategoryMagic();
369 } else {
370 return $this->newCategoryMagic();
371 }
372 }
373
374 # This method generates the list of subcategories and pages for a category
375 function oldCategoryMagic () {
376 global $wgLang , $wgUser ;
377 $fname = 'Parser::oldCategoryMagic';
378
379 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
380
381 $cns = Namespace::getCategory() ;
382 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
383
384 $r = "<br style=\"clear:both;\"/>\n";
385
386
387 $sk =& $wgUser->getSkin() ;
388
389 $articles = array() ;
390 $children = array() ;
391 $data = array () ;
392 $id = $this->mTitle->getArticleID() ;
393
394 # FIXME: add limits
395 $dbr =& wfGetDB( DB_SLAVE );
396 $cur = $dbr->tableName( 'cur' );
397 $categorylinks = $dbr->tableName( 'categorylinks' );
398
399 $t = $dbr->strencode( $this->mTitle->getDBKey() );
400 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
401 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
402 $res = $dbr->query( $sql, $fname ) ;
403 while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
404
405 # For all pages that link to this category
406 foreach ( $data AS $x )
407 {
408 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
409 if ( $t != "" ) $t .= ":" ;
410 $t .= $x->cur_title ;
411
412 if ( $x->cur_namespace == $cns ) {
413 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
414 } else {
415 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
416 }
417 }
418 $dbr->freeResult ( $res ) ;
419
420 # Showing subcategories
421 if ( count ( $children ) > 0 ) {
422 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
423 $r .= implode ( ', ' , $children ) ;
424 }
425
426 # Showing pages in this category
427 if ( count ( $articles ) > 0 ) {
428 $ti = $this->mTitle->getText() ;
429 $h = wfMsg( 'category_header', $ti );
430 $r .= "<h2>{$h}</h2>\n" ;
431 $r .= implode ( ', ' , $articles ) ;
432 }
433
434 return $r ;
435 }
436
437
438
439 function newCategoryMagic () {
440 global $wgLang , $wgUser ;
441 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
442
443 $cns = Namespace::getCategory() ;
444 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
445
446 $r = "<br style=\"clear:both;\"/>\n";
447
448
449 $sk =& $wgUser->getSkin() ;
450
451 $articles = array() ;
452 $articles_start_char = array();
453 $children = array() ;
454 $children_start_char = array();
455 $data = array () ;
456 $id = $this->mTitle->getArticleID() ;
457
458 # FIXME: add limits
459 $dbr =& wfGetDB( DB_SLAVE );
460 $cur = $dbr->tableName( 'cur' );
461 $categorylinks = $dbr->tableName( 'categorylinks' );
462
463 $t = $dbr->strencode( $this->mTitle->getDBKey() );
464 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
465 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
466 $res = $dbr->query ( $sql ) ;
467 while ( $x = $dbr->fetchObject ( $res ) )
468 {
469 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
470 if ( $t != '' ) $t .= ':' ;
471 $t .= $x->cur_title ;
472
473 if ( $x->cur_namespace == $cns ) {
474 $ctitle = str_replace( '_',' ',$x->cur_title );
475 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
476
477 // If there's a link from Category:A to Category:B, the sortkey of the resulting
478 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
479 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
480 // else use sortkey...
481 if ( ($ns.":".$ctitle) == $x->cl_sortkey ) {
482 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) );
483 } else {
484 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
485 }
486 } else {
487 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
488 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
489 }
490 }
491 $dbr->freeResult ( $res ) ;
492
493 $ti = $this->mTitle->getText() ;
494
495 # Don't show subcategories section if there are none.
496 if ( count ( $children ) > 0 )
497 {
498 # Showing subcategories
499 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
500 . wfMsg( 'subcategorycount', count( $children ) );
501 if ( count ( $children ) > 6 ) {
502
503 // divide list into three equal chunks
504 $chunk = (int) (count ( $children ) / 3);
505
506 // get and display header
507 $r .= '<table width="100%"><tr valign="top">';
508
509 $startChunk = 0;
510 $endChunk = $chunk;
511
512 // loop through the chunks
513 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
514 $chunkIndex < 3;
515 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
516 {
517
518 $r .= '<td><ul>';
519 // output all subcategories to category
520 for ($index = $startChunk ;
521 $index < $endChunk && $index < count($children);
522 $index++ )
523 {
524 // check for change of starting letter or begging of chunk
525 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
526 || ($index == $startChunk) )
527 {
528 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
529 }
530
531 $r .= "<li>{$children[$index]}</li>";
532 }
533 $r .= '</ul></td>';
534
535
536 }
537 $r .= '</tr></table>';
538 } else {
539 // for short lists of subcategories to category.
540
541 $r .= "<h3>{$children_start_char[0]}</h3>\n";
542 $r .= '<ul><li>'.$children[0].'</li>';
543 for ($index = 1; $index < count($children); $index++ )
544 {
545 if ($children_start_char[$index] != $children_start_char[$index - 1])
546 {
547 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
548 }
549
550 $r .= "<li>{$children[$index]}</li>";
551 }
552 $r .= '</ul>';
553 }
554 } # END of if ( count($children) > 0 )
555
556 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
557 wfMsg( 'categoryarticlecount', count( $articles ) );
558
559 # Showing articles in this category
560 if ( count ( $articles ) > 6) {
561 $ti = $this->mTitle->getText() ;
562
563 // divide list into three equal chunks
564 $chunk = (int) (count ( $articles ) / 3);
565
566 // get and display header
567 $r .= '<table width="100%"><tr valign="top">';
568
569 // loop through the chunks
570 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
571 $chunkIndex < 3;
572 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
573 {
574
575 $r .= '<td><ul>';
576
577 // output all articles in category
578 for ($index = $startChunk ;
579 $index < $endChunk && $index < count($articles);
580 $index++ )
581 {
582 // check for change of starting letter or begging of chunk
583 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
584 || ($index == $startChunk) )
585 {
586 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
587 }
588
589 $r .= "<li>{$articles[$index]}</li>";
590 }
591 $r .= '</ul></td>';
592
593
594 }
595 $r .= '</tr></table>';
596 } elseif ( count ( $articles ) > 0) {
597 // for short lists of articles in categories.
598 $ti = $this->mTitle->getText() ;
599
600 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
601 $r .= '<ul><li>'.$articles[0].'</li>';
602 for ($index = 1; $index < count($articles); $index++ )
603 {
604 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
605 {
606 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
607 }
608
609 $r .= "<li>{$articles[$index]}</li>";
610 }
611 $r .= '</ul>';
612 }
613
614
615 return $r ;
616 }
617
618 # Return allowed HTML attributes
619 function getHTMLattrs () {
620 $htmlattrs = array( # Allowed attributes--no scripting, etc.
621 'title', 'align', 'lang', 'dir', 'width', 'height',
622 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
623 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
624 /* FONT */ 'type', 'start', 'value', 'compact',
625 /* For various lists, mostly deprecated but safe */
626 'summary', 'width', 'border', 'frame', 'rules',
627 'cellspacing', 'cellpadding', 'valign', 'char',
628 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
629 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
630 'id', 'class', 'name', 'style' /* For CSS */
631 );
632 return $htmlattrs ;
633 }
634
635 # Remove non approved attributes and javascript in css
636 function fixTagAttributes ( $t ) {
637 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
638 $htmlattrs = $this->getHTMLattrs() ;
639
640 # Strip non-approved attributes from the tag
641 $t = preg_replace(
642 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
643 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
644 $t);
645
646 $t = str_replace ( "<></>" , "" , $t ) ; # This should fix bug 980557
647
648 # Strip javascript "expression" from stylesheets. Brute force approach:
649 # If anythin offensive is found, all attributes of the HTML tag are dropped
650
651 if( preg_match(
652 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
653 wfMungeToUtf8( $t ) ) )
654 {
655 $t='';
656 }
657
658 return trim ( $t ) ;
659 }
660
661 # interface with html tidy, used if $wgUseTidy = true
662 function tidy ( $text ) {
663 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
664 global $wgInputEncoding, $wgOutputEncoding;
665 $fname = 'Parser::tidy';
666 wfProfileIn( $fname );
667
668 $cleansource = '';
669 switch(strtoupper($wgOutputEncoding)) {
670 case 'ISO-8859-1':
671 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
672 break;
673 case 'UTF-8':
674 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
675 break;
676 default:
677 $wgTidyOpts .= ' -raw';
678 }
679
680 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
681 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
682 '<head><title>test</title></head><body>'.$text.'</body></html>';
683 $descriptorspec = array(
684 0 => array('pipe', 'r'),
685 1 => array('pipe', 'w'),
686 2 => array('file', '/dev/null', 'a')
687 );
688 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
689 if (is_resource($process)) {
690 fwrite($pipes[0], $wrappedtext);
691 fclose($pipes[0]);
692 while (!feof($pipes[1])) {
693 $cleansource .= fgets($pipes[1], 1024);
694 }
695 fclose($pipes[1]);
696 $return_value = proc_close($process);
697 }
698
699 wfProfileOut( $fname );
700
701 if( $cleansource == '' && $text != '') {
702 wfDebug( "Tidy error detected!\n" );
703 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
704 } else {
705 return $cleansource;
706 }
707 }
708
709 # parse the wiki syntax used to render tables
710 function doTableStuff ( $t ) {
711 $fname = 'Parser::doTableStuff';
712 wfProfileIn( $fname );
713
714 $t = explode ( "\n" , $t ) ;
715 $td = array () ; # Is currently a td tag open?
716 $ltd = array () ; # Was it TD or TH?
717 $tr = array () ; # Is currently a tr tag open?
718 $ltr = array () ; # tr attributes
719 $indent_level = 0; # indent level of the table
720 foreach ( $t AS $k => $x )
721 {
722 $x = trim ( $x ) ;
723 $fc = substr ( $x , 0 , 1 ) ;
724 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) )
725 {
726 $indent_level = strlen( $matches[1] );
727 $t[$k] = "\n" .
728 str_repeat( "<dl><dd>", $indent_level ) .
729 "<table " . $this->fixTagAttributes ( $matches[2] ) . '>' ;
730 array_push ( $td , false ) ;
731 array_push ( $ltd , '' ) ;
732 array_push ( $tr , false ) ;
733 array_push ( $ltr , '' ) ;
734 }
735 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
736 else if ( '|}' == substr ( $x , 0 , 2 ) )
737 {
738 $z = "</table>\n" ;
739 $l = array_pop ( $ltd ) ;
740 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
741 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
742 array_pop ( $ltr ) ;
743 $t[$k] = $z . str_repeat( "</dd></dl>", $indent_level );
744 }
745 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
746 {
747 $x = substr ( $x , 1 ) ;
748 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
749 $z = '' ;
750 $l = array_pop ( $ltd ) ;
751 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
752 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
753 array_pop ( $ltr ) ;
754 $t[$k] = $z ;
755 array_push ( $tr , false ) ;
756 array_push ( $td , false ) ;
757 array_push ( $ltd , '' ) ;
758 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
759 }
760 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
761 {
762 if ( '|+' == substr ( $x , 0 , 2 ) )
763 {
764 $fc = '+' ;
765 $x = substr ( $x , 1 ) ;
766 }
767 $after = substr ( $x , 1 ) ;
768 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
769 $after = explode ( '||' , $after ) ;
770 $t[$k] = '' ;
771 foreach ( $after AS $theline )
772 {
773 $z = '' ;
774 if ( $fc != '+' )
775 {
776 $tra = array_pop ( $ltr ) ;
777 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
778 array_push ( $tr , true ) ;
779 array_push ( $ltr , '' ) ;
780 }
781
782 $l = array_pop ( $ltd ) ;
783 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
784 if ( $fc == '|' ) $l = 'td' ;
785 else if ( $fc == '!' ) $l = 'th' ;
786 else if ( $fc == '+' ) $l = 'caption' ;
787 else $l = '' ;
788 array_push ( $ltd , $l ) ;
789 $y = explode ( '|' , $theline , 2 ) ;
790 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
791 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
792 $t[$k] .= $y ;
793 array_push ( $td , true ) ;
794 }
795 }
796 }
797
798 # Closing open td, tr && table
799 while ( count ( $td ) > 0 )
800 {
801 if ( array_pop ( $td ) ) $t[] = '</td>' ;
802 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
803 $t[] = '</table>' ;
804 }
805
806 $t = implode ( "\n" , $t ) ;
807 # $t = $this->removeHTMLtags( $t );
808 wfProfileOut( $fname );
809 return $t ;
810 }
811
812 # Parses the text and adds the result to the strip state
813 # Returns the strip tag
814 function stripParse( $text, $newline, $args )
815 {
816 $text = $this->strip( $text, $this->mStripState );
817 $text = $this->internalParse( $text, (bool)$newline, $args, false );
818 return $newline.$this->insertStripItem( $text, $this->mStripState );
819 }
820
821 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
822 $fname = 'Parser::internalParse';
823 wfProfileIn( $fname );
824
825 $text = $this->removeHTMLtags( $text );
826 $text = $this->replaceVariables( $text, $args );
827
828 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
829
830 $text = $this->doHeadings( $text );
831 if($this->mOptions->getUseDynamicDates()) {
832 global $wgDateFormatter;
833 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
834 }
835 $text = $this->doAllQuotes( $text );
836 $text = $this->replaceExternalLinks( $text );
837 $text = $this->doMagicLinks( $text );
838 $text = $this->replaceInternalLinks ( $text );
839 $text = $this->replaceInternalLinks ( $text );
840 $text = $this->doTableStuff( $text );
841 $text = $this->formatHeadings( $text, $isMain );
842 $sk =& $this->mOptions->getSkin();
843 $text = $sk->transformContent( $text );
844
845 if ( $isMain && !isset ( $this->categoryMagicDone ) ) {
846 $text .= $this->categoryMagic () ;
847 $this->categoryMagicDone = true ;
848 }
849
850 wfProfileOut( $fname );
851 return $text;
852 }
853
854 /* private */ function &doMagicLinks( &$text ) {
855 $text = $this->magicISBN( $text );
856 $text = $this->magicGEO( $text );
857 $text = $this->magicRFC( $text );
858 return $text;
859 }
860
861 # Parse ^^ tokens and return html
862 /* private */ function doExponent ( $text )
863 {
864 $fname = 'Parser::doExponent';
865 wfProfileIn( $fname);
866 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
867 wfProfileOut( $fname);
868 return $text;
869 }
870
871 # Parse headers and return html
872 /* private */ function doHeadings( $text ) {
873 $fname = 'Parser::doHeadings';
874 wfProfileIn( $fname );
875 for ( $i = 6; $i >= 1; --$i ) {
876 $h = substr( '======', 0, $i );
877 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
878 "<h{$i}>\\1</h{$i}>\\2", $text );
879 }
880 wfProfileOut( $fname );
881 return $text;
882 }
883
884 /* private */ function doAllQuotes( $text ) {
885 $fname = 'Parser::doAllQuotes';
886 wfProfileIn( $fname );
887 $outtext = '';
888 $lines = explode( "\n", $text );
889 foreach ( $lines as $line ) {
890 $outtext .= $this->doQuotes ( $line ) . "\n";
891 }
892 $outtext = substr($outtext, 0,-1);
893 wfProfileOut( $fname );
894 return $outtext;
895 }
896
897 /* private */ function doQuotes( $text ) {
898 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
899 if (count ($arr) == 1)
900 return $text;
901 else
902 {
903 # First, do some preliminary work. This may shift some apostrophes from
904 # being mark-up to being text. It also counts the number of occurrences
905 # of bold and italics mark-ups.
906 $i = 0;
907 $numbold = 0;
908 $numitalics = 0;
909 foreach ($arr as $r)
910 {
911 if (($i % 2) == 1)
912 {
913 # If there are ever four apostrophes, assume the first is supposed to
914 # be text, and the remaining three constitute mark-up for bold text.
915 if (strlen ($arr[$i]) == 4)
916 {
917 $arr[$i-1] .= "'";
918 $arr[$i] = "'''";
919 }
920 # If there are more than 5 apostrophes in a row, assume they're all
921 # text except for the last 5.
922 else if (strlen ($arr[$i]) > 5)
923 {
924 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
925 $arr[$i] = "'''''";
926 }
927 # Count the number of occurrences of bold and italics mark-ups.
928 if (strlen ($arr[$i]) == 2) $numitalics++; else
929 if (strlen ($arr[$i]) == 3) $numbold++; else
930 if (strlen ($arr[$i]) == 5) { $numitalics++; $numbold++; }
931 }
932 $i++;
933 }
934
935 # If there is an odd number of both bold and italics, it is likely
936 # that one of the bold ones was meant to be an apostrophe followed
937 # by italics. Which one we cannot know for certain, but it is more
938 # likely to be one that has a single-letter word before it.
939 if (($numbold % 2 == 1) && ($numitalics % 2 == 1))
940 {
941 $i = 0;
942 $firstsingleletterword = -1;
943 $firstmultiletterword = -1;
944 $firstspace = -1;
945 foreach ($arr as $r)
946 {
947 if (($i % 2 == 1) and (strlen ($r) == 3))
948 {
949 $x1 = substr ($arr[$i-1], -1);
950 $x2 = substr ($arr[$i-1], -2, 1);
951 if ($x1 == " ") {
952 if ($firstspace == -1) $firstspace = $i;
953 } else if ($x2 == " ") {
954 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
955 } else {
956 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
957 }
958 }
959 $i++;
960 }
961
962 # If there is a single-letter word, use it!
963 if ($firstsingleletterword > -1)
964 {
965 $arr [ $firstsingleletterword ] = "''";
966 $arr [ $firstsingleletterword-1 ] .= "'";
967 }
968 # If not, but there's a multi-letter word, use that one.
969 else if ($firstmultiletterword > -1)
970 {
971 $arr [ $firstmultiletterword ] = "''";
972 $arr [ $firstmultiletterword-1 ] .= "'";
973 }
974 # ... otherwise use the first one that has neither.
975 else
976 {
977 $arr [ $firstspace ] = "''";
978 $arr [ $firstspace-1 ] .= "'";
979 }
980 }
981
982 # Now let's actually convert our apostrophic mush to HTML!
983 $output = '';
984 $buffer = '';
985 $state = '';
986 $i = 0;
987 foreach ($arr as $r)
988 {
989 if (($i % 2) == 0)
990 {
991 if ($state == 'both')
992 $buffer .= $r;
993 else
994 $output .= $r;
995 }
996 else
997 {
998 if (strlen ($r) == 2)
999 {
1000 if ($state == 'em')
1001 { $output .= "</em>"; $state = ''; }
1002 else if ($state == 'strongem')
1003 { $output .= "</em>"; $state = 'strong'; }
1004 else if ($state == 'emstrong')
1005 { $output .= "</strong></em><strong>"; $state = 'strong'; }
1006 else if ($state == 'both')
1007 { $output .= "<strong><em>{$buffer}</em>"; $state = 'strong'; }
1008 else # $state can be 'strong' or ''
1009 { $output .= "<em>"; $state .= 'em'; }
1010 }
1011 else if (strlen ($r) == 3)
1012 {
1013 if ($state == 'strong')
1014 { $output .= "</strong>"; $state = ''; }
1015 else if ($state == 'strongem')
1016 { $output .= "</em></strong><em>"; $state = 'em'; }
1017 else if ($state == 'emstrong')
1018 { $output .= "</strong>"; $state = 'em'; }
1019 else if ($state == 'both')
1020 { $output .= "<em><strong>{$buffer}</strong>"; $state = 'em'; }
1021 else # $state can be 'em' or ''
1022 { $output .= "<strong>"; $state .= 'strong'; }
1023 }
1024 else if (strlen ($r) == 5)
1025 {
1026 if ($state == 'strong')
1027 { $output .= "</strong><em>"; $state = 'em'; }
1028 else if ($state == 'em')
1029 { $output .= "</em><strong>"; $state = 'strong'; }
1030 else if ($state == 'strongem')
1031 { $output .= "</em></strong>"; $state = ''; }
1032 else if ($state == 'emstrong')
1033 { $output .= "</strong></em>"; $state = ''; }
1034 else if ($state == 'both')
1035 { $output .= "<em><strong>{$buffer}</strong></em>"; $state = ''; }
1036 else # ($state == '')
1037 { $buffer = ''; $state = 'both'; }
1038 }
1039 }
1040 $i++;
1041 }
1042 return $output;
1043 }
1044 }
1045
1046 # Note: we have to do external links before the internal ones,
1047 # and otherwise take great care in the order of things here, so
1048 # that we don't end up interpreting some URLs twice.
1049
1050 /* private */ function replaceExternalLinks( $text ) {
1051 $fname = 'Parser::replaceExternalLinks';
1052 wfProfileIn( $fname );
1053 $text = $this->subReplaceExternalLinks( $text, 'http', true );
1054 $text = $this->subReplaceExternalLinks( $text, 'https', true );
1055 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
1056 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
1057 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
1058 $text = $this->subReplaceExternalLinks( $text, 'news', false );
1059 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
1060 wfProfileOut( $fname );
1061 return $text;
1062 }
1063
1064 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
1065 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
1066 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
1067
1068 # this is the list of separators that should be ignored if they
1069 # are the last character of an URL but that should be included
1070 # if they occur within the URL, e.g. "go to www.foo.com, where .."
1071 # in this case, the last comma should not become part of the URL,
1072 # but in "www.foo.com/123,2342,32.htm" it should.
1073 $sep = ",;\.:";
1074 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
1075 $images = 'gif|png|jpg|jpeg';
1076
1077 # PLEASE NOTE: The curly braces { } are not part of the regex,
1078 # they are interpreted as part of the string (used to tell PHP
1079 # that the content of the string should be inserted there).
1080 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
1081 "((?i){$images})([^{$uc}]|$)/";
1082
1083 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
1084 $sk =& $this->mOptions->getSkin();
1085
1086 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
1087 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
1088 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
1089 }
1090 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
1091 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
1092 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
1093 '</a>\\5', $s );
1094 $s = str_replace( $unique, $protocol, $s );
1095
1096 $a = explode( "[{$protocol}:", " " . $s );
1097 $s = array_shift( $a );
1098 $s = substr( $s, 1 );
1099
1100 # Regexp for URL in square brackets
1101 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
1102 # Regexp for URL with link text in square brackets
1103 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
1104
1105 foreach ( $a as $line ) {
1106
1107 # CASE 1: Link in square brackets, e.g.
1108 # some text [http://domain.tld/some.link] more text
1109 if ( preg_match( $e1, $line, $m ) ) {
1110 $link = "{$protocol}:{$m[1]}";
1111 $trail = $m[2];
1112 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
1113 else { $text = wfEscapeHTML( $link ); }
1114 }
1115
1116 # CASE 2: Link with link text and text directly following it, e.g.
1117 # This is a collection of [http://domain.tld/some.link link]s
1118 else if ( preg_match( $e2, $line, $m ) ) {
1119 $link = "{$protocol}:{$m[1]}";
1120 $text = $m[2];
1121 $dtrail = '';
1122 $trail = $m[3];
1123 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1124 $dtrail = $m2[1];
1125 $trail = $m2[2];
1126 }
1127 }
1128
1129 # CASE 3: Nothing matches, just output the source text
1130 else {
1131 $s .= "[{$protocol}:" . $line;
1132 continue;
1133 }
1134
1135 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1136 $paren = '';
1137 } else {
1138 # Expand the URL for printable version
1139 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1140 }
1141 $la = $sk->getExternalLinkAttributes( $link, $text );
1142 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1143
1144 }
1145 return $s;
1146 }
1147
1148
1149 /* private */ function replaceInternalLinks( $s ) {
1150 global $wgLang, $wgLinkCache;
1151 global $wgNamespacesWithSubpages, $wgLanguageCode;
1152 static $fname = 'Parser::replaceInternalLinks' ;
1153 wfProfileIn( $fname );
1154
1155 wfProfileIn( $fname.'-setup' );
1156 static $tc = FALSE;
1157 # the % is needed to support urlencoded titles as well
1158 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1159 $sk =& $this->mOptions->getSkin();
1160
1161 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1162 $isRedirect = $redirect->matchStart ( strtoupper ( substr ( $s , 0 , 10 ) ) ) ;
1163
1164 $a = explode( '[[', ' ' . $s );
1165 $s = array_shift( $a );
1166 $s = substr( $s, 1 );
1167
1168 # Match a link having the form [[namespace:link|alternate]]trail
1169 static $e1 = FALSE;
1170 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1171 # Match the end of a line for a word that's not followed by whitespace,
1172 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1173 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1174
1175 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1176 # Special and Media are pseudo-namespaces; no pages actually exist in them
1177 static $image = FALSE;
1178 static $special = FALSE;
1179 static $media = FALSE;
1180 static $category = FALSE;
1181 if ( !$image ) { $image = Namespace::getImage(); }
1182 if ( !$special ) { $special = Namespace::getSpecial(); }
1183 if ( !$media ) { $media = Namespace::getMedia(); }
1184 if ( !$category ) { $category = Namespace::getCategory(); }
1185
1186 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1187
1188 if ( $useLinkPrefixExtension ) {
1189 if ( preg_match( $e2, $s, $m ) ) {
1190 $first_prefix = $m[2];
1191 $s = $m[1];
1192 } else {
1193 $first_prefix = false;
1194 }
1195 } else {
1196 $prefix = '';
1197 }
1198
1199 wfProfileOut( $fname.'-setup' );
1200
1201 foreach ( $a as $line ) {
1202 wfProfileIn( $fname.'-prefixhandling' );
1203 if ( $useLinkPrefixExtension ) {
1204 if ( preg_match( $e2, $s, $m ) ) {
1205 $prefix = $m[2];
1206 $s = $m[1];
1207 } else {
1208 $prefix='';
1209 }
1210 # first link
1211 if($first_prefix) {
1212 $prefix = $first_prefix;
1213 $first_prefix = false;
1214 }
1215 }
1216 wfProfileOut( $fname.'-prefixhandling' );
1217
1218 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1219 $text = $m[2];
1220 # fix up urlencoded title texts
1221 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1222 $trail = $m[3];
1223 } else { # Invalid form; output directly
1224 $s .= $prefix . '[[' . $line ;
1225 continue;
1226 }
1227
1228 /* Valid link forms:
1229 Foobar -- normal
1230 :Foobar -- override special treatment of prefix (images, language links)
1231 /Foobar -- convert to CurrentPage/Foobar
1232 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1233 */
1234 $c = substr($m[1],0,1);
1235 $noforce = ($c != ':');
1236 if( $c == '/' ) { # subpage
1237 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1238 $m[1]=substr($m[1],1,strlen($m[1])-2);
1239 $noslash=$m[1];
1240 } else {
1241 $noslash=substr($m[1],1);
1242 }
1243 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1244 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1245 if( '' == $text ) {
1246 $text= $m[1];
1247 } # this might be changed for ugliness reasons
1248 } else {
1249 $link = $noslash; # no subpage allowed, use standard link
1250 }
1251 } elseif( $noforce ) { # no subpage
1252 $link = $m[1];
1253 } else {
1254 $link = substr( $m[1], 1 );
1255 }
1256 $wasblank = ( '' == $text );
1257 if( $wasblank )
1258 $text = $link;
1259
1260 $nt = Title::newFromText( $link );
1261 if( !$nt ) {
1262 $s .= $prefix . '[[' . $line;
1263 continue;
1264 }
1265 $ns = $nt->getNamespace();
1266 $iw = $nt->getInterWiki();
1267 if( $noforce ) {
1268 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1269 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1270 $tmp = $prefix . $trail ;
1271 $s .= (trim($tmp) == '')? '': $tmp;
1272 continue;
1273 }
1274 if ( $ns == $image ) {
1275 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1276 $wgLinkCache->addImageLinkObj( $nt );
1277 continue;
1278 }
1279 if ( $ns == $category && !$isRedirect ) {
1280 $t = $nt->getText() ;
1281 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1282
1283 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1284 $pPLC=$sk->postParseLinkColour();
1285 $sk->postParseLinkColour( false );
1286 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1287 $sk->postParseLinkColour( $pPLC );
1288 $wgLinkCache->resume();
1289
1290 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1291 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1292 $this->mOutput->mCategoryLinks[] = $t ;
1293 $s .= $prefix . $trail ;
1294 continue;
1295 }
1296 }
1297 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1298 ( strpos( $link, '#' ) == FALSE ) ) {
1299 # Self-links are handled specially; generally de-link and change to bold.
1300 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1301 continue;
1302 }
1303
1304 if( $ns == $media ) {
1305 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1306 $wgLinkCache->addImageLinkObj( $nt );
1307 continue;
1308 } elseif( $ns == $special ) {
1309 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1310 continue;
1311 }
1312 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1313 }
1314 wfProfileOut( $fname );
1315 return $s;
1316 }
1317
1318 # Some functions here used by doBlockLevels()
1319 #
1320 /* private */ function closeParagraph() {
1321 $result = '';
1322 if ( '' != $this->mLastSection ) {
1323 $result = '</' . $this->mLastSection . ">\n";
1324 }
1325 $this->mInPre = false;
1326 $this->mLastSection = '';
1327 return $result;
1328 }
1329 # getCommon() returns the length of the longest common substring
1330 # of both arguments, starting at the beginning of both.
1331 #
1332 /* private */ function getCommon( $st1, $st2 ) {
1333 $fl = strlen( $st1 );
1334 $shorter = strlen( $st2 );
1335 if ( $fl < $shorter ) { $shorter = $fl; }
1336
1337 for ( $i = 0; $i < $shorter; ++$i ) {
1338 if ( $st1{$i} != $st2{$i} ) { break; }
1339 }
1340 return $i;
1341 }
1342 # These next three functions open, continue, and close the list
1343 # element appropriate to the prefix character passed into them.
1344 #
1345 /* private */ function openList( $char )
1346 {
1347 $result = $this->closeParagraph();
1348
1349 if ( '*' == $char ) { $result .= '<ul><li>'; }
1350 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1351 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1352 else if ( ';' == $char ) {
1353 $result .= '<dl><dt>';
1354 $this->mDTopen = true;
1355 }
1356 else { $result = '<!-- ERR 1 -->'; }
1357
1358 return $result;
1359 }
1360
1361 /* private */ function nextItem( $char ) {
1362 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1363 else if ( ':' == $char || ';' == $char ) {
1364 $close = "</dd>";
1365 if ( $this->mDTopen ) { $close = '</dt>'; }
1366 if ( ';' == $char ) {
1367 $this->mDTopen = true;
1368 return $close . '<dt>';
1369 } else {
1370 $this->mDTopen = false;
1371 return $close . '<dd>';
1372 }
1373 }
1374 return '<!-- ERR 2 -->';
1375 }
1376
1377 /* private */function closeList( $char ) {
1378 if ( '*' == $char ) { $text = '</li></ul>'; }
1379 else if ( '#' == $char ) { $text = '</li></ol>'; }
1380 else if ( ':' == $char ) {
1381 if ( $this->mDTopen ) {
1382 $this->mDTopen = false;
1383 $text = '</dt></dl>';
1384 } else {
1385 $text = '</dd></dl>';
1386 }
1387 }
1388 else { return '<!-- ERR 3 -->'; }
1389 return $text."\n";
1390 }
1391
1392 /* private */ function doBlockLevels( $text, $linestart ) {
1393 $fname = 'Parser::doBlockLevels';
1394 wfProfileIn( $fname );
1395
1396 # Parsing through the text line by line. The main thing
1397 # happening here is handling of block-level elements p, pre,
1398 # and making lists from lines starting with * # : etc.
1399 #
1400 $textLines = explode( "\n", $text );
1401
1402 $lastPrefix = $output = $lastLine = '';
1403 $this->mDTopen = $inBlockElem = false;
1404 $prefixLength = 0;
1405 $paragraphStack = false;
1406
1407 if ( !$linestart ) {
1408 $output .= array_shift( $textLines );
1409 }
1410 foreach ( $textLines as $oLine ) {
1411 $lastPrefixLength = strlen( $lastPrefix );
1412 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1413 $preOpenMatch = preg_match("/<pre/i", $oLine );
1414 if ( !$this->mInPre ) {
1415 # Multiple prefixes may abut each other for nested lists.
1416 $prefixLength = strspn( $oLine, '*#:;' );
1417 $pref = substr( $oLine, 0, $prefixLength );
1418
1419 # eh?
1420 $pref2 = str_replace( ';', ':', $pref );
1421 $t = substr( $oLine, $prefixLength );
1422 $this->mInPre = !empty($preOpenMatch);
1423 } else {
1424 # Don't interpret any other prefixes in preformatted text
1425 $prefixLength = 0;
1426 $pref = $pref2 = '';
1427 $t = $oLine;
1428 }
1429
1430 # List generation
1431 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1432 # Same as the last item, so no need to deal with nesting or opening stuff
1433 $output .= $this->nextItem( substr( $pref, -1 ) );
1434 $paragraphStack = false;
1435
1436 if ( ";" == substr( $pref, -1 ) ) {
1437 # The one nasty exception: definition lists work like this:
1438 # ; title : definition text
1439 # So we check for : in the remainder text to split up the
1440 # title and definition, without b0rking links.
1441 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1442 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1443 $term = $match[1];
1444 $output .= $term . $this->nextItem( ':' );
1445 $t = $match[2];
1446 }
1447 }
1448 } elseif( $prefixLength || $lastPrefixLength ) {
1449 # Either open or close a level...
1450 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1451 $paragraphStack = false;
1452
1453 while( $commonPrefixLength < $lastPrefixLength ) {
1454 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1455 --$lastPrefixLength;
1456 }
1457 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1458 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1459 }
1460 while ( $prefixLength > $commonPrefixLength ) {
1461 $char = substr( $pref, $commonPrefixLength, 1 );
1462 $output .= $this->openList( $char );
1463
1464 if ( ';' == $char ) {
1465 # FIXME: This is dupe of code above
1466 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1467 $term = $match[1];
1468 $output .= $term . $this->nextItem( ":" );
1469 $t = $match[2];
1470 }
1471 }
1472 ++$commonPrefixLength;
1473 }
1474 $lastPrefix = $pref2;
1475 }
1476 if( 0 == $prefixLength ) {
1477 # No prefix (not in list)--go to paragraph mode
1478 $uniq_prefix = UNIQ_PREFIX;
1479 // XXX: use a stack for nestable elements like span, table and div
1480 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1481 $closematch = preg_match(
1482 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1483 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1484 if ( $openmatch or $closematch ) {
1485 $paragraphStack = false;
1486 $output .= $this->closeParagraph();
1487 if($preOpenMatch and !$preCloseMatch) {
1488 $this->mInPre = true;
1489 }
1490 if ( $closematch ) {
1491 $inBlockElem = false;
1492 } else {
1493 $inBlockElem = true;
1494 }
1495 } else if ( !$inBlockElem && !$this->mInPre ) {
1496 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1497 // pre
1498 if ($this->mLastSection != 'pre') {
1499 $paragraphStack = false;
1500 $output .= $this->closeParagraph().'<pre>';
1501 $this->mLastSection = 'pre';
1502 }
1503 } else {
1504 // paragraph
1505 if ( '' == trim($t) ) {
1506 if ( $paragraphStack ) {
1507 $output .= $paragraphStack.'<br />';
1508 $paragraphStack = false;
1509 $this->mLastSection = 'p';
1510 } else {
1511 if ($this->mLastSection != 'p' ) {
1512 $output .= $this->closeParagraph();
1513 $this->mLastSection = '';
1514 $paragraphStack = '<p>';
1515 } else {
1516 $paragraphStack = '</p><p>';
1517 }
1518 }
1519 } else {
1520 if ( $paragraphStack ) {
1521 $output .= $paragraphStack;
1522 $paragraphStack = false;
1523 $this->mLastSection = 'p';
1524 } else if ($this->mLastSection != 'p') {
1525 $output .= $this->closeParagraph().'<p>';
1526 $this->mLastSection = 'p';
1527 }
1528 }
1529 }
1530 }
1531 }
1532 if ($paragraphStack === false) {
1533 $output .= $t."\n";
1534 }
1535 }
1536 while ( $prefixLength ) {
1537 $output .= $this->closeList( $pref2{$prefixLength-1} );
1538 --$prefixLength;
1539 }
1540 if ( '' != $this->mLastSection ) {
1541 $output .= '</' . $this->mLastSection . '>';
1542 $this->mLastSection = '';
1543 }
1544
1545 wfProfileOut( $fname );
1546 return $output;
1547 }
1548
1549 # Return value of a magic variable (like PAGENAME)
1550 function getVariableValue( $index ) {
1551 global $wgLang, $wgSitename, $wgServer;
1552
1553 switch ( $index ) {
1554 case MAG_CURRENTMONTH:
1555 return $wgLang->formatNum( date( 'm' ) );
1556 case MAG_CURRENTMONTHNAME:
1557 return $wgLang->getMonthName( date('n') );
1558 case MAG_CURRENTMONTHNAMEGEN:
1559 return $wgLang->getMonthNameGen( date('n') );
1560 case MAG_CURRENTDAY:
1561 return $wgLang->formatNum( date('j') );
1562 case MAG_PAGENAME:
1563 return $this->mTitle->getText();
1564 case MAG_NAMESPACE:
1565 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1566 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1567 case MAG_CURRENTDAYNAME:
1568 return $wgLang->getWeekdayName( date('w')+1 );
1569 case MAG_CURRENTYEAR:
1570 return $wgLang->formatNum( date( 'Y' ) );
1571 case MAG_CURRENTTIME:
1572 return $wgLang->time( wfTimestampNow(), false );
1573 case MAG_NUMBEROFARTICLES:
1574 return $wgLang->formatNum( wfNumberOfArticles() );
1575 case MAG_SITENAME:
1576 return $wgSitename;
1577 case MAG_SERVER:
1578 return $wgServer;
1579 default:
1580 return NULL;
1581 }
1582 }
1583
1584 # initialise the magic variables (like CURRENTMONTHNAME)
1585 function initialiseVariables() {
1586 global $wgVariableIDs;
1587 $this->mVariables = array();
1588 foreach ( $wgVariableIDs as $id ) {
1589 $mw =& MagicWord::get( $id );
1590 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1591 }
1592 }
1593
1594 /* private */ function replaceVariables( $text, $args = array() ) {
1595 global $wgLang, $wgScript, $wgArticlePath;
1596
1597 # Prevent too big inclusions
1598 if(strlen($text)> MAX_INCLUDE_SIZE)
1599 return $text;
1600
1601 $fname = 'Parser::replaceVariables';
1602 wfProfileIn( $fname );
1603
1604 $bail = false;
1605 $titleChars = Title::legalChars();
1606 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1607
1608 # This function is called recursively. To keep track of arguments we need a stack:
1609 array_push( $this->mArgStack, $args );
1610
1611 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1612 $GLOBALS['wgCurParser'] =& $this;
1613
1614
1615 if ( $this->mOutputType == OT_HTML ) {
1616 # Variable substitution
1617 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1618
1619 # Argument substitution
1620 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1621 }
1622 # Template substitution
1623 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1624 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1625
1626 array_pop( $this->mArgStack );
1627
1628 wfProfileOut( $fname );
1629 return $text;
1630 }
1631
1632 function variableSubstitution( $matches ) {
1633 if ( !$this->mVariables ) {
1634 $this->initialiseVariables();
1635 }
1636 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1637 $text = $this->mVariables[$matches[1]];
1638 $this->mOutput->mContainsOldMagic = true;
1639 } else {
1640 $text = $matches[0];
1641 }
1642 return $text;
1643 }
1644
1645 # Split template arguments
1646 function getTemplateArgs( $argsString ) {
1647 if ( $argsString === '' ) {
1648 return array();
1649 }
1650
1651 $args = explode( '|', substr( $argsString, 1 ) );
1652
1653 # If any of the arguments contains a '[[' but no ']]', it needs to be
1654 # merged with the next arg because the '|' character between belongs
1655 # to the link syntax and not the template parameter syntax.
1656 $argc = count($args);
1657 $i = 0;
1658 for ( $i = 0; $i < $argc-1; $i++ ) {
1659 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1660 $args[$i] .= "|".$args[$i+1];
1661 array_splice($args, $i+1, 1);
1662 $i--;
1663 $argc--;
1664 }
1665 }
1666
1667 return $args;
1668 }
1669
1670 function braceSubstitution( $matches ) {
1671 global $wgLinkCache, $wgLang;
1672 $fname = 'Parser::braceSubstitution';
1673 $found = false;
1674 $nowiki = false;
1675 $noparse = false;
1676
1677 $title = NULL;
1678
1679 # $newline is an optional newline character before the braces
1680 # $part1 is the bit before the first |, and must contain only title characters
1681 # $args is a list of arguments, starting from index 0, not including $part1
1682
1683 $newline = $matches[1];
1684 $part1 = $matches[2];
1685 # If the third subpattern matched anything, it will start with |
1686
1687 $args = $this->getTemplateArgs($matches[3]);
1688 $argc = count( $args );
1689
1690 # {{{}}}
1691 if ( strpos( $matches[0], '{{{' ) !== false ) {
1692 $text = $matches[0];
1693 $found = true;
1694 $noparse = true;
1695 }
1696
1697 # SUBST
1698 if ( !$found ) {
1699 $mwSubst =& MagicWord::get( MAG_SUBST );
1700 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1701 if ( $this->mOutputType != OT_WIKI ) {
1702 # Invalid SUBST not replaced at PST time
1703 # Return without further processing
1704 $text = $matches[0];
1705 $found = true;
1706 $noparse= true;
1707 }
1708 } elseif ( $this->mOutputType == OT_WIKI ) {
1709 # SUBST not found in PST pass, do nothing
1710 $text = $matches[0];
1711 $found = true;
1712 }
1713 }
1714
1715 # MSG, MSGNW and INT
1716 if ( !$found ) {
1717 # Check for MSGNW:
1718 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1719 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1720 $nowiki = true;
1721 } else {
1722 # Remove obsolete MSG:
1723 $mwMsg =& MagicWord::get( MAG_MSG );
1724 $mwMsg->matchStartAndRemove( $part1 );
1725 }
1726
1727 # Check if it is an internal message
1728 $mwInt =& MagicWord::get( MAG_INT );
1729 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1730 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1731 $text = wfMsgReal( $part1, $args, true );
1732 $found = true;
1733 }
1734 }
1735 }
1736
1737 # NS
1738 if ( !$found ) {
1739 # Check for NS: (namespace expansion)
1740 $mwNs = MagicWord::get( MAG_NS );
1741 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1742 if ( intval( $part1 ) ) {
1743 $text = $wgLang->getNsText( intval( $part1 ) );
1744 $found = true;
1745 } else {
1746 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1747 if ( !is_null( $index ) ) {
1748 $text = $wgLang->getNsText( $index );
1749 $found = true;
1750 }
1751 }
1752 }
1753 }
1754
1755 # LOCALURL and LOCALURLE
1756 if ( !$found ) {
1757 $mwLocal = MagicWord::get( MAG_LOCALURL );
1758 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1759
1760 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1761 $func = 'getLocalURL';
1762 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1763 $func = 'escapeLocalURL';
1764 } else {
1765 $func = '';
1766 }
1767
1768 if ( $func !== '' ) {
1769 $title = Title::newFromText( $part1 );
1770 if ( !is_null( $title ) ) {
1771 if ( $argc > 0 ) {
1772 $text = $title->$func( $args[0] );
1773 } else {
1774 $text = $title->$func();
1775 }
1776 $found = true;
1777 }
1778 }
1779 }
1780
1781 # Internal variables
1782 if ( !$this->mVariables ) {
1783 $this->initialiseVariables();
1784 }
1785 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1786 $text = $this->mVariables[$part1];
1787 $found = true;
1788 $this->mOutput->mContainsOldMagic = true;
1789 }
1790
1791 # Template table test
1792
1793 # Did we encounter this template already? If yes, it is in the cache
1794 # and we need to check for loops.
1795 if ( isset( $this->mTemplates[$part1] ) ) {
1796 # Infinite loop test
1797 if ( isset( $this->mTemplatePath[$part1] ) ) {
1798 $noparse = true;
1799 $found = true;
1800 }
1801 # set $text to cached message.
1802 $text = $this->mTemplates[$part1];
1803 $found = true;
1804 }
1805
1806 # Load from database
1807 if ( !$found ) {
1808 $title = Title::newFromText( $part1, NS_TEMPLATE );
1809 if ( !is_null( $title ) && !$title->isExternal() ) {
1810 # Check for excessive inclusion
1811 $dbk = $title->getPrefixedDBkey();
1812 if ( $this->incrementIncludeCount( $dbk ) ) {
1813 # This should never be reached.
1814 $article = new Article( $title );
1815 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1816 if ( $articleContent !== false ) {
1817 $found = true;
1818 $text = $articleContent;
1819
1820 }
1821 }
1822
1823 # If the title is valid but undisplayable, make a link to it
1824 if ( $this->mOutputType == OT_HTML && !$found ) {
1825 $text = '[[' . $title->getPrefixedText() . ']]';
1826 $found = true;
1827 }
1828
1829 # Template cache array insertion
1830 $this->mTemplates[$part1] = $text;
1831 }
1832 }
1833
1834 # Recursive parsing, escaping and link table handling
1835 # Only for HTML output
1836 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1837 $text = wfEscapeWikiText( $text );
1838 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1839 # Clean up argument array
1840 $assocArgs = array();
1841 $index = 1;
1842 foreach( $args as $arg ) {
1843 $eqpos = strpos( $arg, '=' );
1844 if ( $eqpos === false ) {
1845 $assocArgs[$index++] = $arg;
1846 } else {
1847 $name = trim( substr( $arg, 0, $eqpos ) );
1848 $value = trim( substr( $arg, $eqpos+1 ) );
1849 if ( $value === false ) {
1850 $value = '';
1851 }
1852 if ( $name !== false ) {
1853 $assocArgs[$name] = $value;
1854 }
1855 }
1856 }
1857
1858 # Do not enter included links in link table
1859 if ( !is_null( $title ) ) {
1860 $wgLinkCache->suspend();
1861 }
1862
1863 # Add a new element to the templace recursion path
1864 $this->mTemplatePath[$part1] = 1;
1865
1866 # Run full parser on the included text
1867 $text = $this->internalParse( $text, $newline, $assocArgs );
1868 # I replaced the line below with the line above, as it former seems to cause several bugs
1869 #$text = $this->stripParse( $text, $newline, $assocArgs );
1870
1871 # Resume the link cache and register the inclusion as a link
1872 if ( !is_null( $title ) ) {
1873 $wgLinkCache->resume();
1874 $wgLinkCache->addLinkObj( $title );
1875 }
1876 }
1877 # Empties the template path
1878 $this->mTemplatePath = array();
1879
1880 if ( !$found ) {
1881 return $matches[0];
1882 } else {
1883 return $text;
1884 }
1885 }
1886
1887 # Triple brace replacement -- used for template arguments
1888 function argSubstitution( $matches ) {
1889 $newline = $matches[1];
1890 $arg = trim( $matches[2] );
1891 $text = $matches[0];
1892 $inputArgs = end( $this->mArgStack );
1893
1894 if ( array_key_exists( $arg, $inputArgs ) ) {
1895 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1896 }
1897
1898 return $text;
1899 }
1900
1901 # Returns true if the function is allowed to include this entity
1902 function incrementIncludeCount( $dbk ) {
1903 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1904 $this->mIncludeCount[$dbk] = 0;
1905 }
1906 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1907 return true;
1908 } else {
1909 return false;
1910 }
1911 }
1912
1913
1914 # Cleans up HTML, removes dangerous tags and attributes
1915 /* private */ function removeHTMLtags( $text ) {
1916 global $wgUseTidy, $wgUserHtml;
1917 $fname = 'Parser::removeHTMLtags';
1918 wfProfileIn( $fname );
1919
1920 if( $wgUserHtml ) {
1921 $htmlpairs = array( # Tags that must be closed
1922 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1923 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1924 'strike', 'strong', 'tt', 'var', 'div', 'center',
1925 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1926 'ruby', 'rt' , 'rb' , 'rp', 'p'
1927 );
1928 $htmlsingle = array(
1929 'br', 'hr', 'li', 'dt', 'dd'
1930 );
1931 $htmlnest = array( # Tags that can be nested--??
1932 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1933 'dl', 'font', 'big', 'small', 'sub', 'sup'
1934 );
1935 $tabletags = array( # Can only appear inside table
1936 'td', 'th', 'tr'
1937 );
1938 } else {
1939 $htmlpairs = array();
1940 $htmlsingle = array();
1941 $htmlnest = array();
1942 $tabletags = array();
1943 }
1944
1945 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1946 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1947
1948 $htmlattrs = $this->getHTMLattrs () ;
1949
1950 # Remove HTML comments
1951 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1952
1953 $bits = explode( '<', $text );
1954 $text = array_shift( $bits );
1955 if(!$wgUseTidy) {
1956 $tagstack = array(); $tablestack = array();
1957 foreach ( $bits as $x ) {
1958 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1959 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1960 $x, $regs );
1961 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1962 error_reporting( $prev );
1963
1964 $badtag = 0 ;
1965 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1966 # Check our stack
1967 if ( $slash ) {
1968 # Closing a tag...
1969 if ( ! in_array( $t, $htmlsingle ) &&
1970 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1971 @array_push( $tagstack, $ot );
1972 $badtag = 1;
1973 } else {
1974 if ( $t == 'table' ) {
1975 $tagstack = array_pop( $tablestack );
1976 }
1977 $newparams = '';
1978 }
1979 } else {
1980 # Keep track for later
1981 if ( in_array( $t, $tabletags ) &&
1982 ! in_array( 'table', $tagstack ) ) {
1983 $badtag = 1;
1984 } else if ( in_array( $t, $tagstack ) &&
1985 ! in_array ( $t , $htmlnest ) ) {
1986 $badtag = 1 ;
1987 } else if ( ! in_array( $t, $htmlsingle ) ) {
1988 if ( $t == 'table' ) {
1989 array_push( $tablestack, $tagstack );
1990 $tagstack = array();
1991 }
1992 array_push( $tagstack, $t );
1993 }
1994 # Strip non-approved attributes from the tag
1995 $newparams = $this->fixTagAttributes($params);
1996
1997 }
1998 if ( ! $badtag ) {
1999 $rest = str_replace( '>', '&gt;', $rest );
2000 $text .= "<$slash$t $newparams$brace$rest";
2001 continue;
2002 }
2003 }
2004 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2005 }
2006 # Close off any remaining tags
2007 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2008 $text .= "</$t>\n";
2009 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2010 }
2011 } else {
2012 # this might be possible using tidy itself
2013 foreach ( $bits as $x ) {
2014 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2015 $x, $regs );
2016 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2017 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2018 $newparams = $this->fixTagAttributes($params);
2019 $rest = str_replace( '>', '&gt;', $rest );
2020 $text .= "<$slash$t $newparams$brace$rest";
2021 } else {
2022 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2023 }
2024 }
2025 }
2026 wfProfileOut( $fname );
2027 return $text;
2028 }
2029
2030
2031 /*
2032 *
2033 * This function accomplishes several tasks:
2034 * 1) Auto-number headings if that option is enabled
2035 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2036 * 3) Add a Table of contents on the top for users who have enabled the option
2037 * 4) Auto-anchor headings
2038 *
2039 * It loops through all headlines, collects the necessary data, then splits up the
2040 * string and re-inserts the newly formatted headlines.
2041 *
2042 */
2043
2044 /* private */ function formatHeadings( $text, $isMain=true ) {
2045 global $wgInputEncoding, $wgMaxTocLevel;
2046
2047 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2048 $doShowToc = $this->mOptions->getShowToc();
2049 $forceTocHere = false;
2050 if( !$this->mTitle->userCanEdit() ) {
2051 $showEditLink = 0;
2052 $rightClickHack = 0;
2053 } else {
2054 $showEditLink = $this->mOptions->getEditSection();
2055 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2056 }
2057
2058 # Inhibit editsection links if requested in the page
2059 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2060 if( $esw->matchAndRemove( $text ) ) {
2061 $showEditLink = 0;
2062 }
2063 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2064 # do not add TOC
2065 $mw =& MagicWord::get( MAG_NOTOC );
2066 if( $mw->matchAndRemove( $text ) ) {
2067 $doShowToc = 0;
2068 }
2069
2070 # never add the TOC to the Main Page. This is an entry page that should not
2071 # be more than 1-2 screens large anyway
2072 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2073 $doShowToc = 0;
2074 }
2075
2076 # Get all headlines for numbering them and adding funky stuff like [edit]
2077 # links - this is for later, but we need the number of headlines right now
2078 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2079
2080 # if there are fewer than 4 headlines in the article, do not show TOC
2081 if( $numMatches < 4 ) {
2082 $doShowToc = 0;
2083 }
2084
2085 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2086 # override above conditions and always show TOC at that place
2087 $mw =& MagicWord::get( MAG_TOC );
2088 if ($mw->match( $text ) ) {
2089 $doShowToc = 1;
2090 $forceTocHere = true;
2091 } else {
2092 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2093 # override above conditions and always show TOC above first header
2094 $mw =& MagicWord::get( MAG_FORCETOC );
2095 if ($mw->matchAndRemove( $text ) ) {
2096 $doShowToc = 1;
2097 }
2098 }
2099
2100
2101
2102 # We need this to perform operations on the HTML
2103 $sk =& $this->mOptions->getSkin();
2104
2105 # headline counter
2106 $headlineCount = 0;
2107
2108 # Ugh .. the TOC should have neat indentation levels which can be
2109 # passed to the skin functions. These are determined here
2110 $toclevel = 0;
2111 $toc = '';
2112 $full = '';
2113 $head = array();
2114 $sublevelCount = array();
2115 $level = 0;
2116 $prevlevel = 0;
2117 foreach( $matches[3] as $headline ) {
2118 $numbering = '';
2119 if( $level ) {
2120 $prevlevel = $level;
2121 }
2122 $level = $matches[1][$headlineCount];
2123 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2124 # reset when we enter a new level
2125 $sublevelCount[$level] = 0;
2126 $toc .= $sk->tocIndent( $level - $prevlevel );
2127 $toclevel += $level - $prevlevel;
2128 }
2129 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2130 # reset when we step back a level
2131 $sublevelCount[$level+1]=0;
2132 $toc .= $sk->tocUnindent( $prevlevel - $level );
2133 $toclevel -= $prevlevel - $level;
2134 }
2135 # count number of headlines for each level
2136 @$sublevelCount[$level]++;
2137 if( $doNumberHeadings || $doShowToc ) {
2138 $dot = 0;
2139 for( $i = 1; $i <= $level; $i++ ) {
2140 if( !empty( $sublevelCount[$i] ) ) {
2141 if( $dot ) {
2142 $numbering .= '.';
2143 }
2144 $numbering .= $sublevelCount[$i];
2145 $dot = 1;
2146 }
2147 }
2148 }
2149
2150 # The canonized header is a version of the header text safe to use for links
2151 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2152 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2153 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2154
2155 # strip out HTML
2156 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2157 $tocline = trim( $canonized_headline );
2158 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2159 $replacearray = array(
2160 '%3A' => ':',
2161 '%' => '.'
2162 );
2163 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2164 $refer[$headlineCount] = $canonized_headline;
2165
2166 # count how many in assoc. array so we can track dupes in anchors
2167 @$refers[$canonized_headline]++;
2168 $refcount[$headlineCount]=$refers[$canonized_headline];
2169
2170 # Prepend the number to the heading text
2171
2172 if( $doNumberHeadings || $doShowToc ) {
2173 $tocline = $numbering . ' ' . $tocline;
2174
2175 # Don't number the heading if it is the only one (looks silly)
2176 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2177 # the two are different if the line contains a link
2178 $headline=$numbering . ' ' . $headline;
2179 }
2180 }
2181
2182 # Create the anchor for linking from the TOC to the section
2183 $anchor = $canonized_headline;
2184 if($refcount[$headlineCount] > 1 ) {
2185 $anchor .= '_' . $refcount[$headlineCount];
2186 }
2187 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2188 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2189 }
2190 if( $showEditLink ) {
2191 if ( empty( $head[$headlineCount] ) ) {
2192 $head[$headlineCount] = '';
2193 }
2194 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2195 }
2196
2197 # Add the edit section span
2198 if( $rightClickHack ) {
2199 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2200 }
2201
2202 # give headline the correct <h#> tag
2203 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2204
2205 $headlineCount++;
2206 }
2207
2208 if( $doShowToc ) {
2209 $toclines = $headlineCount;
2210 $toc .= $sk->tocUnindent( $toclevel );
2211 $toc = $sk->tocTable( $toc );
2212 }
2213
2214 # split up and insert constructed headlines
2215
2216 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2217 $i = 0;
2218
2219 foreach( $blocks as $block ) {
2220 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2221 # This is the [edit] link that appears for the top block of text when
2222 # section editing is enabled
2223
2224 # Disabled because it broke block formatting
2225 # For example, a bullet point in the top line
2226 # $full .= $sk->editSectionLink(0);
2227 }
2228 $full .= $block;
2229 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2230 # Top anchor now in skin
2231 $full = $full.$toc;
2232 }
2233
2234 if( !empty( $head[$i] ) ) {
2235 $full .= $head[$i];
2236 }
2237 $i++;
2238 }
2239 if($forceTocHere) {
2240 $mw =& MagicWord::get( MAG_TOC );
2241 return $mw->replace( $toc, $full );
2242 } else {
2243 return $full;
2244 }
2245 }
2246
2247 # Return an HTML link for the "ISBN 123456" text
2248 /* private */ function magicISBN( $text ) {
2249 global $wgLang;
2250 $fname = 'Parser::magicISBN';
2251 wfProfileIn( $fname );
2252
2253 $a = split( 'ISBN ', " $text" );
2254 if ( count ( $a ) < 2 ) {
2255 wfProfileOut( $fname );
2256 return $text;
2257 }
2258 $text = substr( array_shift( $a ), 1);
2259 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2260
2261 foreach ( $a as $x ) {
2262 $isbn = $blank = '' ;
2263 while ( ' ' == $x{0} ) {
2264 $blank .= ' ';
2265 $x = substr( $x, 1 );
2266 }
2267 while ( strstr( $valid, $x{0} ) != false ) {
2268 $isbn .= $x{0};
2269 $x = substr( $x, 1 );
2270 }
2271 $num = str_replace( '-', '', $isbn );
2272 $num = str_replace( ' ', '', $num );
2273
2274 if ( '' == $num ) {
2275 $text .= "ISBN $blank$x";
2276 } else {
2277 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2278 $text .= '<a href="' .
2279 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2280 "\" class=\"internal\">ISBN $isbn</a>";
2281 $text .= $x;
2282 }
2283 }
2284 wfProfileOut( $fname );
2285 return $text;
2286 }
2287
2288 # Return an HTML link for the "GEO ..." text
2289 /* private */ function magicGEO( $text ) {
2290 global $wgLang, $wgUseGeoMode;
2291 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2292 $fname = 'Parser::magicGEO';
2293 wfProfileIn( $fname );
2294
2295 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2296 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2297 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2298 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2299 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2300 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2301
2302 $a = split( 'GEO ', " $text" );
2303 if ( count ( $a ) < 2 ) {
2304 wfProfileOut( $fname );
2305 return $text;
2306 }
2307 $text = substr( array_shift( $a ), 1);
2308 $valid = '0123456789.+-:';
2309
2310 foreach ( $a as $x ) {
2311 $geo = $blank = '' ;
2312 while ( ' ' == $x{0} ) {
2313 $blank .= ' ';
2314 $x = substr( $x, 1 );
2315 }
2316 while ( strstr( $valid, $x{0} ) != false ) {
2317 $geo .= $x{0};
2318 $x = substr( $x, 1 );
2319 }
2320 $num = str_replace( '+', '', $geo );
2321 $num = str_replace( ' ', '', $num );
2322
2323 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2324 $text .= "GEO $blank$x";
2325 } else {
2326 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2327 $text .= '<a href="' .
2328 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2329 "\" class=\"internal\">GEO $geo</a>";
2330 $text .= $x;
2331 }
2332 }
2333 wfProfileOut( $fname );
2334 return $text;
2335 }
2336
2337 # Return an HTML link for the "RFC 1234" text
2338 /* private */ function magicRFC( $text ) {
2339 global $wgLang;
2340
2341 $a = split( 'RFC ', ' '.$text );
2342 if ( count ( $a ) < 2 ) return $text;
2343 $text = substr( array_shift( $a ), 1);
2344 $valid = '0123456789';
2345
2346 foreach ( $a as $x ) {
2347 $rfc = $blank = '' ;
2348 while ( ' ' == $x{0} ) {
2349 $blank .= ' ';
2350 $x = substr( $x, 1 );
2351 }
2352 while ( strstr( $valid, $x{0} ) != false ) {
2353 $rfc .= $x{0};
2354 $x = substr( $x, 1 );
2355 }
2356
2357 if ( '' == $rfc ) {
2358 $text .= "RFC $blank$x";
2359 } else {
2360 $url = wfmsg( 'rfcurl' );
2361 $url = str_replace( '$1', $rfc, $url);
2362 $sk =& $this->mOptions->getSkin();
2363 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2364 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2365 }
2366 }
2367 return $text;
2368 }
2369
2370 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2371 $this->mOptions = $options;
2372 $this->mTitle =& $title;
2373 $this->mOutputType = OT_WIKI;
2374
2375 if ( $clearState ) {
2376 $this->clearState();
2377 }
2378
2379 $stripState = false;
2380 $pairs = array(
2381 "\r\n" => "\n",
2382 );
2383 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2384 // now with regexes
2385 /*
2386 $pairs = array(
2387 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2388 "/<br *?>/i" => "<br />",
2389 );
2390 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2391 */
2392 $text = $this->strip( $text, $stripState, false );
2393 $text = $this->pstPass2( $text, $user );
2394 $text = $this->unstrip( $text, $stripState );
2395 $text = $this->unstripNoWiki( $text, $stripState );
2396 return $text;
2397 }
2398
2399 /* private */ function pstPass2( $text, &$user ) {
2400 global $wgLang, $wgLocaltimezone, $wgCurParser;
2401
2402 # Variable replacement
2403 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2404 $text = $this->replaceVariables( $text );
2405
2406 # Signatures
2407 #
2408 $n = $user->getName();
2409 $k = $user->getOption( 'nickname' );
2410 if ( '' == $k ) { $k = $n; }
2411 if(isset($wgLocaltimezone)) {
2412 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2413 }
2414 /* Note: this is an ugly timezone hack for the European wikis */
2415 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2416 ' (' . date( 'T' ) . ')';
2417 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2418
2419 $text = preg_replace( '/~~~~~/', $d, $text );
2420 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2421 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2422 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2423 Namespace::getUser() ) . ":$n|$k]]", $text );
2424
2425 # Context links: [[|name]] and [[name (context)|]]
2426 #
2427 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2428 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2429 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2430 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2431
2432 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2433 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2434 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2435 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2436 # [[ns:page (cont)|]]
2437 $context = "";
2438 $t = $this->mTitle->getText();
2439 if ( preg_match( $conpat, $t, $m ) ) {
2440 $context = $m[2];
2441 }
2442 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2443 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2444 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2445
2446 if ( '' == $context ) {
2447 $text = preg_replace( $p2, '[[\\1]]', $text );
2448 } else {
2449 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2450 }
2451
2452 /*
2453 $mw =& MagicWord::get( MAG_SUBST );
2454 $wgCurParser = $this->fork();
2455 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2456 $this->merge( $wgCurParser );
2457 */
2458
2459 # Trim trailing whitespace
2460 # MAG_END (__END__) tag allows for trailing
2461 # whitespace to be deliberately included
2462 $text = rtrim( $text );
2463 $mw =& MagicWord::get( MAG_END );
2464 $mw->matchAndRemove( $text );
2465
2466 return $text;
2467 }
2468
2469 # Set up some variables which are usually set up in parse()
2470 # so that an external function can call some class members with confidence
2471 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2472 $this->mTitle =& $title;
2473 $this->mOptions = $options;
2474 $this->mOutputType = $outputType;
2475 if ( $clearState ) {
2476 $this->clearState();
2477 }
2478 }
2479
2480 function transformMsg( $text, $options ) {
2481 global $wgTitle;
2482 static $executing = false;
2483
2484 # Guard against infinite recursion
2485 if ( $executing ) {
2486 return $text;
2487 }
2488 $executing = true;
2489
2490 $this->mTitle = $wgTitle;
2491 $this->mOptions = $options;
2492 $this->mOutputType = OT_MSG;
2493 $this->clearState();
2494 $text = $this->replaceVariables( $text );
2495
2496 $executing = false;
2497 return $text;
2498 }
2499
2500 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2501 # Callback will be called with the text within
2502 # Transform and return the text within
2503 function setHook( $tag, $callback ) {
2504 $oldVal = @$this->mTagHooks[$tag];
2505 $this->mTagHooks[$tag] = $callback;
2506 return $oldVal;
2507 }
2508 }
2509
2510 class ParserOutput
2511 {
2512 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2513 var $mCacheTime; # Used in ParserCache
2514
2515 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2516 $containsOldMagic = false )
2517 {
2518 $this->mText = $text;
2519 $this->mLanguageLinks = $languageLinks;
2520 $this->mCategoryLinks = $categoryLinks;
2521 $this->mContainsOldMagic = $containsOldMagic;
2522 $this->mCacheTime = "";
2523 }
2524
2525 function getText() { return $this->mText; }
2526 function getLanguageLinks() { return $this->mLanguageLinks; }
2527 function getCategoryLinks() { return $this->mCategoryLinks; }
2528 function getCacheTime() { return $this->mCacheTime; }
2529 function containsOldMagic() { return $this->mContainsOldMagic; }
2530 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2531 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2532 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2533 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2534 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2535
2536 function merge( $other ) {
2537 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2538 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2539 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2540 }
2541
2542 }
2543
2544 class ParserOptions
2545 {
2546 # All variables are private
2547 var $mUseTeX; # Use texvc to expand <math> tags
2548 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2549 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2550 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2551 var $mAllowExternalImages; # Allow external images inline
2552 var $mSkin; # Reference to the preferred skin
2553 var $mDateFormat; # Date format index
2554 var $mEditSection; # Create "edit section" links
2555 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2556 var $mNumberHeadings; # Automatically number headings
2557 var $mShowToc; # Show table of contents
2558
2559 function getUseTeX() { return $this->mUseTeX; }
2560 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2561 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2562 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2563 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2564 function getSkin() { return $this->mSkin; }
2565 function getDateFormat() { return $this->mDateFormat; }
2566 function getEditSection() { return $this->mEditSection; }
2567 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2568 function getNumberHeadings() { return $this->mNumberHeadings; }
2569 function getShowToc() { return $this->mShowToc; }
2570
2571 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2572 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2573 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2574 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2575 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2576 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2577 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2578 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2579 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2580 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2581
2582 function setSkin( &$x ) { $this->mSkin =& $x; }
2583
2584 /* static */ function newFromUser( &$user ) {
2585 $popts = new ParserOptions;
2586 $popts->initialiseFromUser( $user );
2587 return $popts;
2588 }
2589
2590 function initialiseFromUser( &$userInput ) {
2591 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2592
2593 if ( !$userInput ) {
2594 $user = new User;
2595 $user->setLoaded( true );
2596 } else {
2597 $user =& $userInput;
2598 }
2599
2600 $this->mUseTeX = $wgUseTeX;
2601 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2602 $this->mUseDynamicDates = $wgUseDynamicDates;
2603 $this->mInterwikiMagic = $wgInterwikiMagic;
2604 $this->mAllowExternalImages = $wgAllowExternalImages;
2605 $this->mSkin =& $user->getSkin();
2606 $this->mDateFormat = $user->getOption( 'date' );
2607 $this->mEditSection = $user->getOption( 'editsection' );
2608 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2609 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2610 $this->mShowToc = $user->getOption( 'showtoc' );
2611 }
2612
2613
2614 }
2615
2616 # Regex callbacks, used in Parser::replaceVariables
2617 function wfBraceSubstitution( $matches )
2618 {
2619 global $wgCurParser;
2620 return $wgCurParser->braceSubstitution( $matches );
2621 }
2622
2623 function wfArgSubstitution( $matches )
2624 {
2625 global $wgCurParser;
2626 return $wgCurParser->argSubstitution( $matches );
2627 }
2628
2629 function wfVariableSubstitution( $matches )
2630 {
2631 global $wgCurParser;
2632 return $wgCurParser->variableSubstitution( $matches );
2633 }
2634
2635 ?>