Remove trailing space which breaks XML output
[mediawiki.git] / includes / Parser.php
blob57600b3229175501d3c3509cab97ddbd68070181
1 <?php
3 // require_once('Tokenizer.php');
5 # PHP Parser
7 # Processes wiki markup
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.
12 # Globals used:
13 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
15 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
17 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
18 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
19 # $wgLocaltimezone
21 # * only within ParserOptions
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).
33 define( "MAX_INCLUDE_REPEAT", 5 );
34 define( "MAX_INCLUDE_SIZE", 1000000 ); // 1 Million
36 # Allowed values for $mOutputType
37 define( "OT_HTML", 1 );
38 define( "OT_WIKI", 2 );
39 define( "OT_MSG", 3 );
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" );
47 # prefix for escaping, used in two functions at least
48 define( "UNIQ_PREFIX", "NaodW29");
50 class Parser
52 # Persistent:
53 var $mTagHooks;
55 # Cleared with clearState():
56 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
57 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
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.
66 function Parser() {
67 $this->mTemplates = array();
68 $this->mTemplatePath = array();
69 $this->mTagHooks = array();
70 $this->clearState();
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;
85 # First pass--just handle <nowiki> sections, pass the rest off
86 # to internalParse() which does all the real work.
88 # Returns a ParserOutput
90 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
91 global $wgUseTidy;
92 $fname = "Parser::parse";
93 wfProfileIn( $fname );
95 if ( $clearState ) {
96 $this->clearState();
99 $this->mOptions = $options;
100 $this->mTitle =& $title;
101 $this->mOutputType = OT_HTML;
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;'
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>'
134 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
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);
142 $this->mOutput->setText( $text );
143 wfProfileOut( $fname );
144 return $this->mOutput;
147 /* static */ function getRandomString() {
148 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
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.
156 # If $content is already set, the additional entries will be appended
158 # If $tag is set to STRIP_COMMENTS, the function will extract
159 # <!-- HTML comments -->
161 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
162 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
163 if ( !$content ) {
164 $content = array( );
166 $n = 1;
167 $stripped = '';
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 );
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 );
184 $marker = $rnd . sprintf('%08X', $n++);
185 $content[$marker] = $q[0];
186 $stripped .= $marker;
187 $text = $q[1];
190 return $stripped;
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
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();
211 # Replace any instances of the placeholders
212 $uniq_prefix = UNIQ_PREFIX;
213 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
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>";
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>";
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;";
248 } else {
249 $math_content[$marker] = "<math>$content</math>";
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>";
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-->";
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>";
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;
292 foreach( $ext_content as $tag => $array ) {
293 if ( array_key_exists( $tag, $state ) ) {
294 $state[$tag] = $state[$tag] + $array;
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;
306 return $text;
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 );
321 return $text;
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 );
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 );
337 return $text;
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()
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()
354 $state['item'][$rnd] = $text;
355 return $rnd;
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] )
368 return $this->oldCategoryMagic();
369 } else {
370 return $this->newCategoryMagic();
374 # This method generates the list of subcategories and pages for a category
375 function oldCategoryMagic () {
376 global $wgLang , $wgUser ;
377 $fname = 'Parser::oldCategoryMagic';
379 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
381 $cns = Namespace::getCategory() ;
382 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
384 $r = "<br style=\"clear:both;\"/>\n";
387 $sk =& $wgUser->getSkin() ;
389 $articles = array() ;
390 $children = array() ;
391 $data = array () ;
392 $id = $this->mTitle->getArticleID() ;
394 # FIXME: add limits
395 $dbr =& wfGetDB( DB_SLAVE );
396 $cur = $dbr->tableName( 'cur' );
397 $categorylinks = $dbr->tableName( 'categorylinks' );
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 ;
405 # For all pages that link to this category
406 foreach ( $data AS $x )
408 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
409 if ( $t != "" ) $t .= ":" ;
410 $t .= $x->cur_title ;
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
418 $dbr->freeResult ( $res ) ;
420 # Showing subcategories
421 if ( count ( $children ) > 0 ) {
422 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
423 $r .= implode ( ', ' , $children ) ;
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 ) ;
434 return $r ;
439 function newCategoryMagic () {
440 global $wgLang , $wgUser ;
441 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
443 $cns = Namespace::getCategory() ;
444 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
446 $r = "<br style=\"clear:both;\"/>\n";
449 $sk =& $wgUser->getSkin() ;
451 $articles = array() ;
452 $articles_start_char = array();
453 $children = array() ;
454 $children_start_char = array();
455 $data = array () ;
456 $id = $this->mTitle->getArticleID() ;
458 # FIXME: add limits
459 $dbr =& wfGetDB( DB_SLAVE );
460 $cur = $dbr->tableName( 'cur' );
461 $categorylinks = $dbr->tableName( 'categorylinks' );
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 ) )
469 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
470 if ( $t != '' ) $t .= ':' ;
471 $t .= $x->cur_title ;
473 if ( $x->cur_namespace == $cns ) {
474 $ctitle = str_replace( '_',' ',$x->cur_title );
475 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
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 ) ) ;
486 } else {
487 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
488 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
491 $dbr->freeResult ( $res ) ;
493 $ti = $this->mTitle->getText() ;
495 # Don't show subcategories section if there are none.
496 if ( count ( $children ) > 0 )
498 # Showing subcategories
499 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
500 . wfMsg( 'subcategorycount', count( $children ) );
501 if ( count ( $children ) > 6 ) {
503 // divide list into three equal chunks
504 $chunk = (int) (count ( $children ) / 3);
506 // get and display header
507 $r .= '<table width="100%"><tr valign="top">';
509 $startChunk = 0;
510 $endChunk = $chunk;
512 // loop through the chunks
513 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
514 $chunkIndex < 3;
515 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
518 $r .= '<td><ul>';
519 // output all subcategories to category
520 for ($index = $startChunk ;
521 $index < $endChunk && $index < count($children);
522 $index++ )
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) )
528 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
531 $r .= "<li>{$children[$index]}</li>";
533 $r .= '</ul></td>';
537 $r .= '</tr></table>';
538 } else {
539 // for short lists of subcategories to category.
541 $r .= "<h3>{$children_start_char[0]}</h3>\n";
542 $r .= '<ul><li>'.$children[0].'</li>';
543 for ($index = 1; $index < count($children); $index++ )
545 if ($children_start_char[$index] != $children_start_char[$index - 1])
547 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
550 $r .= "<li>{$children[$index]}</li>";
552 $r .= '</ul>';
554 } # END of if ( count($children) > 0 )
556 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
557 wfMsg( 'categoryarticlecount', count( $articles ) );
559 # Showing articles in this category
560 if ( count ( $articles ) > 6) {
561 $ti = $this->mTitle->getText() ;
563 // divide list into three equal chunks
564 $chunk = (int) (count ( $articles ) / 3);
566 // get and display header
567 $r .= '<table width="100%"><tr valign="top">';
569 // loop through the chunks
570 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
571 $chunkIndex < 3;
572 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
575 $r .= '<td><ul>';
577 // output all articles in category
578 for ($index = $startChunk ;
579 $index < $endChunk && $index < count($articles);
580 $index++ )
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) )
586 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
589 $r .= "<li>{$articles[$index]}</li>";
591 $r .= '</ul></td>';
595 $r .= '</tr></table>';
596 } elseif ( count ( $articles ) > 0) {
597 // for short lists of articles in categories.
598 $ti = $this->mTitle->getText() ;
600 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
601 $r .= '<ul><li>'.$articles[0].'</li>';
602 for ($index = 1; $index < count($articles); $index++ )
604 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
606 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
609 $r .= "<li>{$articles[$index]}</li>";
611 $r .= '</ul>';
615 return $r ;
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 */
632 return $htmlattrs ;
635 # Remove non approved attributes and javascript in css
636 function fixTagAttributes ( $t ) {
637 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
638 $htmlattrs = $this->getHTMLattrs() ;
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 # Strip javascript "expression" from stylesheets. Brute force approach:
646 # If anythin offensive is found, all attributes of the HTML tag are dropped
648 if( preg_match(
649 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
650 wfMungeToUtf8( $t ) ) )
652 $t='';
655 return trim ( $t ) ;
658 # interface with html tidy, used if $wgUseTidy = true
659 function tidy ( $text ) {
660 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
661 global $wgInputEncoding, $wgOutputEncoding;
662 $fname = 'Parser::tidy';
663 wfProfileIn( $fname );
665 $cleansource = '';
666 switch(strtoupper($wgOutputEncoding)) {
667 case 'ISO-8859-1':
668 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
669 break;
670 case 'UTF-8':
671 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
672 break;
673 default:
674 $wgTidyOpts .= ' -raw';
677 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
678 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
679 '<head><title>test</title></head><body>'.$text.'</body></html>';
680 $descriptorspec = array(
681 0 => array('pipe', 'r'),
682 1 => array('pipe', 'w'),
683 2 => array('file', '/dev/null', 'a')
685 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
686 if (is_resource($process)) {
687 fwrite($pipes[0], $wrappedtext);
688 fclose($pipes[0]);
689 while (!feof($pipes[1])) {
690 $cleansource .= fgets($pipes[1], 1024);
692 fclose($pipes[1]);
693 $return_value = proc_close($process);
696 wfProfileOut( $fname );
698 if( $cleansource == '' && $text != '') {
699 wfDebug( "Tidy error detected!\n" );
700 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
701 } else {
702 return $cleansource;
706 # parse the wiki syntax used to render tables
707 function doTableStuff ( $t ) {
708 $fname = 'Parser::doTableStuff';
709 wfProfileIn( $fname );
711 $t = explode ( "\n" , $t ) ;
712 $td = array () ; # Is currently a td tag open?
713 $ltd = array () ; # Was it TD or TH?
714 $tr = array () ; # Is currently a tr tag open?
715 $ltr = array () ; # tr attributes
716 foreach ( $t AS $k => $x )
718 $x = trim ( $x ) ;
719 $fc = substr ( $x , 0 , 1 ) ;
720 if ( '{|' == substr ( $x , 0 , 2 ) )
722 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 2 ) ) . '>' ;
723 array_push ( $td , false ) ;
724 array_push ( $ltd , '' ) ;
725 array_push ( $tr , false ) ;
726 array_push ( $ltr , '' ) ;
728 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
729 else if ( '|}' == substr ( $x , 0 , 2 ) )
731 $z = "</table>\n" ;
732 $l = array_pop ( $ltd ) ;
733 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
734 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
735 array_pop ( $ltr ) ;
736 $t[$k] = $z ;
738 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
740 $x = substr ( $x , 1 ) ;
741 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
742 $z = '' ;
743 $l = array_pop ( $ltd ) ;
744 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
745 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
746 array_pop ( $ltr ) ;
747 $t[$k] = $z ;
748 array_push ( $tr , false ) ;
749 array_push ( $td , false ) ;
750 array_push ( $ltd , '' ) ;
751 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
753 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
755 if ( '|+' == substr ( $x , 0 , 2 ) )
757 $fc = '+' ;
758 $x = substr ( $x , 1 ) ;
760 $after = substr ( $x , 1 ) ;
761 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
762 $after = explode ( '||' , $after ) ;
763 $t[$k] = '' ;
764 foreach ( $after AS $theline )
766 $z = '' ;
767 if ( $fc != '+' )
769 $tra = array_pop ( $ltr ) ;
770 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
771 array_push ( $tr , true ) ;
772 array_push ( $ltr , '' ) ;
775 $l = array_pop ( $ltd ) ;
776 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
777 if ( $fc == '|' ) $l = 'td' ;
778 else if ( $fc == '!' ) $l = 'th' ;
779 else if ( $fc == '+' ) $l = 'caption' ;
780 else $l = '' ;
781 array_push ( $ltd , $l ) ;
782 $y = explode ( '|' , $theline , 2 ) ;
783 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
784 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
785 $t[$k] .= $y ;
786 array_push ( $td , true ) ;
791 # Closing open td, tr && table
792 while ( count ( $td ) > 0 )
794 if ( array_pop ( $td ) ) $t[] = '</td>' ;
795 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
796 $t[] = '</table>' ;
799 $t = implode ( "\n" , $t ) ;
800 # $t = $this->removeHTMLtags( $t );
801 wfProfileOut( $fname );
802 return $t ;
805 # Parses the text and adds the result to the strip state
806 # Returns the strip tag
807 function stripParse( $text, $newline, $args )
809 $text = $this->strip( $text, $this->mStripState );
810 $text = $this->internalParse( $text, (bool)$newline, $args, false );
811 return $newline.$this->insertStripItem( $text, $this->mStripState );
814 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
815 $fname = 'Parser::internalParse';
816 wfProfileIn( $fname );
818 $text = $this->removeHTMLtags( $text );
819 $text = $this->replaceVariables( $text, $args );
821 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
823 $text = $this->doHeadings( $text );
824 if($this->mOptions->getUseDynamicDates()) {
825 global $wgDateFormatter;
826 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
828 $text = $this->doAllQuotes( $text );
829 // $text = $this->doExponent( $text );
830 $text = $this->replaceExternalLinks( $text );
831 $text = $this->replaceInternalLinks ( $text );
832 $text = $this->replaceInternalLinks ( $text );
833 //$text = $this->doTokenizedParser ( $text );
834 $text = $this->doTableStuff ( $text ) ;
835 $text = $this->magicISBN( $text );
836 $text = $this->magicGEO( $text );
837 $text = $this->magicRFC( $text );
838 $text = $this->formatHeadings( $text, $isMain );
839 $sk =& $this->mOptions->getSkin();
840 $text = $sk->transformContent( $text );
842 if ( $isMain && !isset ( $this->categoryMagicDone ) ) {
843 $text .= $this->categoryMagic () ;
844 $this->categoryMagicDone = true ;
847 wfProfileOut( $fname );
848 return $text;
851 # Parse ^^ tokens and return html
852 /* private */ function doExponent ( $text )
854 $fname = 'Parser::doExponent';
855 wfProfileIn( $fname);
856 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
857 wfProfileOut( $fname);
858 return $text;
861 # Parse headers and return html
862 /* private */ function doHeadings( $text ) {
863 $fname = 'Parser::doHeadings';
864 wfProfileIn( $fname );
865 for ( $i = 6; $i >= 1; --$i ) {
866 $h = substr( '======', 0, $i );
867 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
868 "<h{$i}>\\1</h{$i}>\\2", $text );
870 wfProfileOut( $fname );
871 return $text;
874 /* private */ function doAllQuotes( $text ) {
875 $fname = 'Parser::doAllQuotes';
876 wfProfileIn( $fname );
877 $outtext = '';
878 $lines = explode( "\n", $text );
879 foreach ( $lines as $line ) {
880 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
882 $outtext = substr($outtext, 0,-1);
883 wfProfileOut( $fname );
884 return $outtext;
887 /* private */ function doQuotes( $pre, $text, $mode ) {
888 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
889 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
890 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
891 if ( substr ($m[2], 0, 1) == '\'' ) {
892 $m[2] = substr ($m[2], 1);
893 if ($mode == 'em') {
894 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'emstrong' );
895 } else if ($mode == 'strong') {
896 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
897 } else if (($mode == 'emstrong') || ($mode == 'both')) {
898 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
899 } else if ($mode == 'strongem') {
900 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
901 } else {
902 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
904 } else {
905 if ($mode == 'strong') {
906 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'strongem' );
907 } else if ($mode == 'em') {
908 return $m1_em . $this->doQuotes ( '', $m[2], '' );
909 } else if ($mode == 'emstrong') {
910 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
911 } else if (($mode == 'strongem') || ($mode == 'both')) {
912 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
913 } else {
914 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
917 } else {
918 $text_strong = ($text == '') ? '' : "<strong>{$text}</strong>";
919 $text_em = ($text == '') ? '' : "<em>{$text}</em>";
920 if ($mode == '') {
921 return $pre . $text;
922 } else if ($mode == 'em') {
923 return $pre . $text_em;
924 } else if ($mode == 'strong') {
925 return $pre . $text_strong;
926 } else if ($mode == 'strongem') {
927 return (($pre == '') && ($text == '')) ? '' : "<strong>{$pre}{$text_em}</strong>";
928 } else {
929 return (($pre == '') && ($text == '')) ? '' : "<em>{$pre}{$text_strong}</em>";
934 # Note: we have to do external links before the internal ones,
935 # and otherwise take great care in the order of things here, so
936 # that we don't end up interpreting some URLs twice.
938 /* private */ function replaceExternalLinks( $text ) {
939 $fname = 'Parser::replaceExternalLinks';
940 wfProfileIn( $fname );
941 $text = $this->subReplaceExternalLinks( $text, 'http', true );
942 $text = $this->subReplaceExternalLinks( $text, 'https', true );
943 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
944 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
945 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
946 $text = $this->subReplaceExternalLinks( $text, 'news', false );
947 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
948 wfProfileOut( $fname );
949 return $text;
952 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
953 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
954 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
956 # this is the list of separators that should be ignored if they
957 # are the last character of an URL but that should be included
958 # if they occur within the URL, e.g. "go to www.foo.com, where .."
959 # in this case, the last comma should not become part of the URL,
960 # but in "www.foo.com/123,2342,32.htm" it should.
961 $sep = ",;\.:";
962 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
963 $images = 'gif|png|jpg|jpeg';
965 # PLEASE NOTE: The curly braces { } are not part of the regex,
966 # they are interpreted as part of the string (used to tell PHP
967 # that the content of the string should be inserted there).
968 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
969 "((?i){$images})([^{$uc}]|$)/";
971 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
972 $sk =& $this->mOptions->getSkin();
974 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
975 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
976 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
978 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
979 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
980 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
981 '</a>\\5', $s );
982 $s = str_replace( $unique, $protocol, $s );
984 $a = explode( "[{$protocol}:", " " . $s );
985 $s = array_shift( $a );
986 $s = substr( $s, 1 );
988 # Regexp for URL in square brackets
989 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
990 # Regexp for URL with link text in square brackets
991 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
993 foreach ( $a as $line ) {
995 # CASE 1: Link in square brackets, e.g.
996 # some text [http://domain.tld/some.link] more text
997 if ( preg_match( $e1, $line, $m ) ) {
998 $link = "{$protocol}:{$m[1]}";
999 $trail = $m[2];
1000 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
1001 else { $text = wfEscapeHTML( $link ); }
1004 # CASE 2: Link with link text and text directly following it, e.g.
1005 # This is a collection of [http://domain.tld/some.link link]s
1006 else if ( preg_match( $e2, $line, $m ) ) {
1007 $link = "{$protocol}:{$m[1]}";
1008 $text = $m[2];
1009 $dtrail = '';
1010 $trail = $m[3];
1011 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1012 $dtrail = $m2[1];
1013 $trail = $m2[2];
1017 # CASE 3: Nothing matches, just output the source text
1018 else {
1019 $s .= "[{$protocol}:" . $line;
1020 continue;
1023 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1024 $paren = '';
1025 } else {
1026 # Expand the URL for printable version
1027 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1029 $la = $sk->getExternalLinkAttributes( $link, $text );
1030 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1033 return $s;
1037 /* private */ function replaceInternalLinks( $s ) {
1038 global $wgLang, $wgLinkCache;
1039 global $wgNamespacesWithSubpages, $wgLanguageCode;
1040 static $fname = 'Parser::replaceInternalLinks' ;
1041 wfProfileIn( $fname );
1043 wfProfileIn( $fname.'-setup' );
1044 static $tc = FALSE;
1045 # the % is needed to support urlencoded titles as well
1046 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1047 $sk =& $this->mOptions->getSkin();
1049 $a = explode( '[[', ' ' . $s );
1050 $s = array_shift( $a );
1051 $s = substr( $s, 1 );
1053 # Match a link having the form [[namespace:link|alternate]]trail
1054 static $e1 = FALSE;
1055 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1056 # Match the end of a line for a word that's not followed by whitespace,
1057 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1058 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1060 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1061 # Special and Media are pseudo-namespaces; no pages actually exist in them
1062 static $image = FALSE;
1063 static $special = FALSE;
1064 static $media = FALSE;
1065 static $category = FALSE;
1066 if ( !$image ) { $image = Namespace::getImage(); }
1067 if ( !$special ) { $special = Namespace::getSpecial(); }
1068 if ( !$media ) { $media = Namespace::getMedia(); }
1069 if ( !$category ) { $category = Namespace::getCategory(); }
1071 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1073 if ( $useLinkPrefixExtension ) {
1074 if ( preg_match( $e2, $s, $m ) ) {
1075 $first_prefix = $m[2];
1076 $s = $m[1];
1077 } else {
1078 $first_prefix = false;
1080 } else {
1081 $prefix = '';
1084 wfProfileOut( $fname.'-setup' );
1086 foreach ( $a as $line ) {
1087 wfProfileIn( $fname.'-prefixhandling' );
1088 if ( $useLinkPrefixExtension ) {
1089 if ( preg_match( $e2, $s, $m ) ) {
1090 $prefix = $m[2];
1091 $s = $m[1];
1092 } else {
1093 $prefix='';
1095 # first link
1096 if($first_prefix) {
1097 $prefix = $first_prefix;
1098 $first_prefix = false;
1101 wfProfileOut( $fname.'-prefixhandling' );
1103 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1104 $text = $m[2];
1105 # fix up urlencoded title texts
1106 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1107 $trail = $m[3];
1108 } else { # Invalid form; output directly
1109 $s .= $prefix . '[[' . $line ;
1110 continue;
1113 /* Valid link forms:
1114 Foobar -- normal
1115 :Foobar -- override special treatment of prefix (images, language links)
1116 /Foobar -- convert to CurrentPage/Foobar
1117 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1119 $c = substr($m[1],0,1);
1120 $noforce = ($c != ':');
1121 if( $c == '/' ) { # subpage
1122 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1123 $m[1]=substr($m[1],1,strlen($m[1])-2);
1124 $noslash=$m[1];
1125 } else {
1126 $noslash=substr($m[1],1);
1128 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1129 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1130 if( '' == $text ) {
1131 $text= $m[1];
1132 } # this might be changed for ugliness reasons
1133 } else {
1134 $link = $noslash; # no subpage allowed, use standard link
1136 } elseif( $noforce ) { # no subpage
1137 $link = $m[1];
1138 } else {
1139 $link = substr( $m[1], 1 );
1141 $wasblank = ( '' == $text );
1142 if( $wasblank )
1143 $text = $link;
1145 $nt = Title::newFromText( $link );
1146 if( !$nt ) {
1147 $s .= $prefix . '[[' . $line;
1148 continue;
1150 $ns = $nt->getNamespace();
1151 $iw = $nt->getInterWiki();
1152 if( $noforce ) {
1153 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1154 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1155 $tmp = $prefix . $trail ;
1156 $s .= (trim($tmp) == '')? '': $tmp;
1157 continue;
1159 if ( $ns == $image ) {
1160 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1161 $wgLinkCache->addImageLinkObj( $nt );
1162 continue;
1164 if ( $ns == $category ) {
1165 $t = $nt->getText() ;
1166 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1168 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1169 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1170 $wgLinkCache->resume();
1172 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1173 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1174 $this->mOutput->mCategoryLinks[] = $t ;
1175 $s .= $prefix . $trail ;
1176 continue;
1179 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1180 ( strpos( $link, '#' ) == FALSE ) ) {
1181 # Self-links are handled specially; generally de-link and change to bold.
1182 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1183 continue;
1186 if( $ns == $media ) {
1187 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1188 $wgLinkCache->addImageLinkObj( $nt );
1189 continue;
1190 } elseif( $ns == $special ) {
1191 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1192 continue;
1194 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1196 wfProfileOut( $fname );
1197 return $s;
1200 # Some functions here used by doBlockLevels()
1202 /* private */ function closeParagraph() {
1203 $result = '';
1204 if ( '' != $this->mLastSection ) {
1205 $result = '</' . $this->mLastSection . ">\n";
1207 $this->mInPre = false;
1208 $this->mLastSection = '';
1209 return $result;
1211 # getCommon() returns the length of the longest common substring
1212 # of both arguments, starting at the beginning of both.
1214 /* private */ function getCommon( $st1, $st2 ) {
1215 $fl = strlen( $st1 );
1216 $shorter = strlen( $st2 );
1217 if ( $fl < $shorter ) { $shorter = $fl; }
1219 for ( $i = 0; $i < $shorter; ++$i ) {
1220 if ( $st1{$i} != $st2{$i} ) { break; }
1222 return $i;
1224 # These next three functions open, continue, and close the list
1225 # element appropriate to the prefix character passed into them.
1227 /* private */ function openList( $char )
1229 $result = $this->closeParagraph();
1231 if ( '*' == $char ) { $result .= '<ul><li>'; }
1232 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1233 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1234 else if ( ';' == $char ) {
1235 $result .= '<dl><dt>';
1236 $this->mDTopen = true;
1238 else { $result = '<!-- ERR 1 -->'; }
1240 return $result;
1243 /* private */ function nextItem( $char ) {
1244 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1245 else if ( ':' == $char || ';' == $char ) {
1246 $close = "</dd>";
1247 if ( $this->mDTopen ) { $close = '</dt>'; }
1248 if ( ';' == $char ) {
1249 $this->mDTopen = true;
1250 return $close . '<dt>';
1251 } else {
1252 $this->mDTopen = false;
1253 return $close . '<dd>';
1256 return '<!-- ERR 2 -->';
1259 /* private */function closeList( $char ) {
1260 if ( '*' == $char ) { $text = '</li></ul>'; }
1261 else if ( '#' == $char ) { $text = '</li></ol>'; }
1262 else if ( ':' == $char ) {
1263 if ( $this->mDTopen ) {
1264 $this->mDTopen = false;
1265 $text = '</dt></dl>';
1266 } else {
1267 $text = '</dd></dl>';
1270 else { return '<!-- ERR 3 -->'; }
1271 return $text."\n";
1274 /* private */ function doBlockLevels( $text, $linestart ) {
1275 $fname = 'Parser::doBlockLevels';
1276 wfProfileIn( $fname );
1278 # Parsing through the text line by line. The main thing
1279 # happening here is handling of block-level elements p, pre,
1280 # and making lists from lines starting with * # : etc.
1282 $textLines = explode( "\n", $text );
1284 $lastPrefix = $output = $lastLine = '';
1285 $this->mDTopen = $inBlockElem = false;
1286 $prefixLength = 0;
1287 $paragraphStack = false;
1289 if ( !$linestart ) {
1290 $output .= array_shift( $textLines );
1292 foreach ( $textLines as $oLine ) {
1293 $lastPrefixLength = strlen( $lastPrefix );
1294 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1295 $preOpenMatch = preg_match("/<pre/i", $oLine );
1296 if ( !$this->mInPre ) {
1297 # Multiple prefixes may abut each other for nested lists.
1298 $prefixLength = strspn( $oLine, '*#:;' );
1299 $pref = substr( $oLine, 0, $prefixLength );
1301 # eh?
1302 $pref2 = str_replace( ';', ':', $pref );
1303 $t = substr( $oLine, $prefixLength );
1304 $this->mInPre = !empty($preOpenMatch);
1305 } else {
1306 # Don't interpret any other prefixes in preformatted text
1307 $prefixLength = 0;
1308 $pref = $pref2 = '';
1309 $t = $oLine;
1312 # List generation
1313 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1314 # Same as the last item, so no need to deal with nesting or opening stuff
1315 $output .= $this->nextItem( substr( $pref, -1 ) );
1316 $paragraphStack = false;
1318 if ( ";" == substr( $pref, -1 ) ) {
1319 # The one nasty exception: definition lists work like this:
1320 # ; title : definition text
1321 # So we check for : in the remainder text to split up the
1322 # title and definition, without b0rking links.
1323 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1324 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1325 $term = $match[1];
1326 $output .= $term . $this->nextItem( ':' );
1327 $t = $match[2];
1330 } elseif( $prefixLength || $lastPrefixLength ) {
1331 # Either open or close a level...
1332 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1333 $paragraphStack = false;
1335 while( $commonPrefixLength < $lastPrefixLength ) {
1336 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1337 --$lastPrefixLength;
1339 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1340 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1342 while ( $prefixLength > $commonPrefixLength ) {
1343 $char = substr( $pref, $commonPrefixLength, 1 );
1344 $output .= $this->openList( $char );
1346 if ( ';' == $char ) {
1347 # FIXME: This is dupe of code above
1348 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1349 $term = $match[1];
1350 $output .= $term . $this->nextItem( ":" );
1351 $t = $match[2];
1354 ++$commonPrefixLength;
1356 $lastPrefix = $pref2;
1358 if( 0 == $prefixLength ) {
1359 # No prefix (not in list)--go to paragraph mode
1360 $uniq_prefix = UNIQ_PREFIX;
1361 // XXX: use a stack for nestable elements like span, table and div
1362 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1363 $closematch = preg_match(
1364 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1365 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1366 if ( $openmatch or $closematch ) {
1367 $paragraphStack = false;
1368 $output .= $this->closeParagraph();
1369 if($preOpenMatch and !$preCloseMatch) {
1370 $this->mInPre = true;
1372 if ( $closematch ) {
1373 $inBlockElem = false;
1374 } else {
1375 $inBlockElem = true;
1377 } else if ( !$inBlockElem && !$this->mInPre ) {
1378 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1379 // pre
1380 if ($this->mLastSection != 'pre') {
1381 $paragraphStack = false;
1382 $output .= $this->closeParagraph().'<pre>';
1383 $this->mLastSection = 'pre';
1385 } else {
1386 // paragraph
1387 if ( '' == trim($t) ) {
1388 if ( $paragraphStack ) {
1389 $output .= $paragraphStack.'<br />';
1390 $paragraphStack = false;
1391 $this->mLastSection = 'p';
1392 } else {
1393 if ($this->mLastSection != 'p' ) {
1394 $output .= $this->closeParagraph();
1395 $this->mLastSection = '';
1396 $paragraphStack = '<p>';
1397 } else {
1398 $paragraphStack = '</p><p>';
1401 } else {
1402 if ( $paragraphStack ) {
1403 $output .= $paragraphStack;
1404 $paragraphStack = false;
1405 $this->mLastSection = 'p';
1406 } else if ($this->mLastSection != 'p') {
1407 $output .= $this->closeParagraph().'<p>';
1408 $this->mLastSection = 'p';
1414 if ($paragraphStack === false) {
1415 $output .= $t."\n";
1418 while ( $prefixLength ) {
1419 $output .= $this->closeList( $pref2{$prefixLength-1} );
1420 --$prefixLength;
1422 if ( '' != $this->mLastSection ) {
1423 $output .= '</' . $this->mLastSection . '>';
1424 $this->mLastSection = '';
1427 wfProfileOut( $fname );
1428 return $output;
1431 # Return value of a magic variable (like PAGENAME)
1432 function getVariableValue( $index ) {
1433 global $wgLang, $wgSitename, $wgServer;
1435 switch ( $index ) {
1436 case MAG_CURRENTMONTH:
1437 return $wgLang->formatNum( date( 'm' ) );
1438 case MAG_CURRENTMONTHNAME:
1439 return $wgLang->getMonthName( date('n') );
1440 case MAG_CURRENTMONTHNAMEGEN:
1441 return $wgLang->getMonthNameGen( date('n') );
1442 case MAG_CURRENTDAY:
1443 return $wgLang->formatNum( date('j') );
1444 case MAG_PAGENAME:
1445 return $this->mTitle->getText();
1446 case MAG_NAMESPACE:
1447 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1448 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1449 case MAG_CURRENTDAYNAME:
1450 return $wgLang->getWeekdayName( date('w')+1 );
1451 case MAG_CURRENTYEAR:
1452 return $wgLang->formatNum( date( 'Y' ) );
1453 case MAG_CURRENTTIME:
1454 return $wgLang->time( wfTimestampNow(), false );
1455 case MAG_NUMBEROFARTICLES:
1456 return $wgLang->formatNum( wfNumberOfArticles() );
1457 case MAG_SITENAME:
1458 return $wgSitename;
1459 case MAG_SERVER:
1460 return $wgServer;
1461 default:
1462 return NULL;
1466 # initialise the magic variables (like CURRENTMONTHNAME)
1467 function initialiseVariables() {
1468 global $wgVariableIDs;
1469 $this->mVariables = array();
1470 foreach ( $wgVariableIDs as $id ) {
1471 $mw =& MagicWord::get( $id );
1472 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1476 /* private */ function replaceVariables( $text, $args = array() ) {
1477 global $wgLang, $wgScript, $wgArticlePath;
1479 # Prevent too big inclusions
1480 if(strlen($text)> MAX_INCLUDE_SIZE)
1481 return $text;
1483 $fname = 'Parser::replaceVariables';
1484 wfProfileIn( $fname );
1486 $bail = false;
1487 $titleChars = Title::legalChars();
1488 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1490 # This function is called recursively. To keep track of arguments we need a stack:
1491 array_push( $this->mArgStack, $args );
1493 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1494 $GLOBALS['wgCurParser'] =& $this;
1497 if ( $this->mOutputType == OT_HTML ) {
1498 # Variable substitution
1499 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1501 # Argument substitution
1502 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1504 # Template substitution
1505 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1506 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1508 array_pop( $this->mArgStack );
1510 wfProfileOut( $fname );
1511 return $text;
1514 function variableSubstitution( $matches ) {
1515 if ( !$this->mVariables ) {
1516 $this->initialiseVariables();
1518 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1519 $text = $this->mVariables[$matches[1]];
1520 $this->mOutput->mContainsOldMagic = true;
1521 } else {
1522 $text = $matches[0];
1524 return $text;
1527 # Split template arguments
1528 function getTemplateArgs( $argsString ) {
1529 if ( $argsString === '' ) {
1530 return array();
1533 $args = explode( '|', substr( $argsString, 1 ) );
1535 # If any of the arguments contains a '[[' but no ']]', it needs to be
1536 # merged with the next arg because the '|' character between belongs
1537 # to the link syntax and not the template parameter syntax.
1538 $argc = count($args);
1539 $i = 0;
1540 for ( $i = 0; $i < $argc-1; $i++ ) {
1541 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1542 $args[$i] .= "|".$args[$i+1];
1543 array_splice($args, $i+1, 1);
1544 $i--;
1545 $argc--;
1549 return $args;
1552 function braceSubstitution( $matches ) {
1553 global $wgLinkCache, $wgLang;
1554 $fname = 'Parser::braceSubstitution';
1555 $found = false;
1556 $nowiki = false;
1557 $noparse = false;
1559 $title = NULL;
1561 # $newline is an optional newline character before the braces
1562 # $part1 is the bit before the first |, and must contain only title characters
1563 # $args is a list of arguments, starting from index 0, not including $part1
1565 $newline = $matches[1];
1566 $part1 = $matches[2];
1567 # If the third subpattern matched anything, it will start with |
1569 $args = $this->getTemplateArgs($matches[3]);
1570 $argc = count( $args );
1572 # {{{}}}
1573 if ( strpos( $matches[0], '{{{' ) !== false ) {
1574 $text = $matches[0];
1575 $found = true;
1576 $noparse = true;
1579 # SUBST
1580 if ( !$found ) {
1581 $mwSubst =& MagicWord::get( MAG_SUBST );
1582 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1583 if ( $this->mOutputType != OT_WIKI ) {
1584 # Invalid SUBST not replaced at PST time
1585 # Return without further processing
1586 $text = $matches[0];
1587 $found = true;
1588 $noparse= true;
1590 } elseif ( $this->mOutputType == OT_WIKI ) {
1591 # SUBST not found in PST pass, do nothing
1592 $text = $matches[0];
1593 $found = true;
1597 # MSG, MSGNW and INT
1598 if ( !$found ) {
1599 # Check for MSGNW:
1600 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1601 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1602 $nowiki = true;
1603 } else {
1604 # Remove obsolete MSG:
1605 $mwMsg =& MagicWord::get( MAG_MSG );
1606 $mwMsg->matchStartAndRemove( $part1 );
1609 # Check if it is an internal message
1610 $mwInt =& MagicWord::get( MAG_INT );
1611 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1612 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1613 $text = wfMsgReal( $part1, $args, true );
1614 $found = true;
1619 # NS
1620 if ( !$found ) {
1621 # Check for NS: (namespace expansion)
1622 $mwNs = MagicWord::get( MAG_NS );
1623 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1624 if ( intval( $part1 ) ) {
1625 $text = $wgLang->getNsText( intval( $part1 ) );
1626 $found = true;
1627 } else {
1628 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1629 if ( !is_null( $index ) ) {
1630 $text = $wgLang->getNsText( $index );
1631 $found = true;
1637 # LOCALURL and LOCALURLE
1638 if ( !$found ) {
1639 $mwLocal = MagicWord::get( MAG_LOCALURL );
1640 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1642 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1643 $func = 'getLocalURL';
1644 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1645 $func = 'escapeLocalURL';
1646 } else {
1647 $func = '';
1650 if ( $func !== '' ) {
1651 $title = Title::newFromText( $part1 );
1652 if ( !is_null( $title ) ) {
1653 if ( $argc > 0 ) {
1654 $text = $title->$func( $args[0] );
1655 } else {
1656 $text = $title->$func();
1658 $found = true;
1663 # Internal variables
1664 if ( !$this->mVariables ) {
1665 $this->initialiseVariables();
1667 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1668 $text = $this->mVariables[$part1];
1669 $found = true;
1670 $this->mOutput->mContainsOldMagic = true;
1673 # Template table test
1675 # Did we encounter this template already? If yes, it is in the cache
1676 # and we need to check for loops.
1677 if ( isset( $this->mTemplates[$part1] ) ) {
1678 # Infinite loop test
1679 if ( isset( $this->mTemplatePath[$part1] ) ) {
1680 $noparse = true;
1681 $found = true;
1683 # set $text to cached message.
1684 $text = $this->mTemplates[$part1];
1685 $found = true;
1688 # Load from database
1689 if ( !$found ) {
1690 $title = Title::newFromText( $part1, NS_TEMPLATE );
1691 if ( !is_null( $title ) && !$title->isExternal() ) {
1692 # Check for excessive inclusion
1693 $dbk = $title->getPrefixedDBkey();
1694 if ( $this->incrementIncludeCount( $dbk ) ) {
1695 # This should never be reached.
1696 $article = new Article( $title );
1697 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1698 if ( $articleContent !== false ) {
1699 $found = true;
1700 $text = $articleContent;
1705 # If the title is valid but undisplayable, make a link to it
1706 if ( $this->mOutputType == OT_HTML && !$found ) {
1707 $text = '[[' . $title->getPrefixedText() . ']]';
1708 $found = true;
1711 # Template cache array insertion
1712 $this->mTemplates[$part1] = $text;
1716 # Recursive parsing, escaping and link table handling
1717 # Only for HTML output
1718 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1719 $text = wfEscapeWikiText( $text );
1720 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1721 # Clean up argument array
1722 $assocArgs = array();
1723 $index = 1;
1724 foreach( $args as $arg ) {
1725 $eqpos = strpos( $arg, '=' );
1726 if ( $eqpos === false ) {
1727 $assocArgs[$index++] = $arg;
1728 } else {
1729 $name = trim( substr( $arg, 0, $eqpos ) );
1730 $value = trim( substr( $arg, $eqpos+1 ) );
1731 if ( $value === false ) {
1732 $value = '';
1734 if ( $name !== false ) {
1735 $assocArgs[$name] = $value;
1740 # Do not enter included links in link table
1741 if ( !is_null( $title ) ) {
1742 $wgLinkCache->suspend();
1745 # Add a new element to the templace recursion path
1746 $this->mTemplatePath[$part1] = 1;
1748 # Run full parser on the included text
1749 $text = $this->stripParse( $text, $newline, $assocArgs );
1751 # Resume the link cache and register the inclusion as a link
1752 if ( !is_null( $title ) ) {
1753 $wgLinkCache->resume();
1754 $wgLinkCache->addLinkObj( $title );
1757 # Empties the template path
1758 $this->mTemplatePath = array();
1760 if ( !$found ) {
1761 return $matches[0];
1762 } else {
1763 return $text;
1767 # Triple brace replacement -- used for template arguments
1768 function argSubstitution( $matches ) {
1769 $newline = $matches[1];
1770 $arg = trim( $matches[2] );
1771 $text = $matches[0];
1772 $inputArgs = end( $this->mArgStack );
1774 if ( array_key_exists( $arg, $inputArgs ) ) {
1775 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1778 return $text;
1781 # Returns true if the function is allowed to include this entity
1782 function incrementIncludeCount( $dbk ) {
1783 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1784 $this->mIncludeCount[$dbk] = 0;
1786 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1787 return true;
1788 } else {
1789 return false;
1794 # Cleans up HTML, removes dangerous tags and attributes
1795 /* private */ function removeHTMLtags( $text ) {
1796 global $wgUseTidy, $wgUserHtml;
1797 $fname = 'Parser::removeHTMLtags';
1798 wfProfileIn( $fname );
1800 if( $wgUserHtml ) {
1801 $htmlpairs = array( # Tags that must be closed
1802 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1803 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1804 'strike', 'strong', 'tt', 'var', 'div', 'center',
1805 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1806 'ruby', 'rt' , 'rb' , 'rp', 'p'
1808 $htmlsingle = array(
1809 'br', 'hr', 'li', 'dt', 'dd'
1811 $htmlnest = array( # Tags that can be nested--??
1812 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1813 'dl', 'font', 'big', 'small', 'sub', 'sup'
1815 $tabletags = array( # Can only appear inside table
1816 'td', 'th', 'tr'
1818 } else {
1819 $htmlpairs = array();
1820 $htmlsingle = array();
1821 $htmlnest = array();
1822 $tabletags = array();
1825 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1826 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1828 $htmlattrs = $this->getHTMLattrs () ;
1830 # Remove HTML comments
1831 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1833 $bits = explode( '<', $text );
1834 $text = array_shift( $bits );
1835 if(!$wgUseTidy) {
1836 $tagstack = array(); $tablestack = array();
1837 foreach ( $bits as $x ) {
1838 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1839 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1840 $x, $regs );
1841 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1842 error_reporting( $prev );
1844 $badtag = 0 ;
1845 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1846 # Check our stack
1847 if ( $slash ) {
1848 # Closing a tag...
1849 if ( ! in_array( $t, $htmlsingle ) &&
1850 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1851 @array_push( $tagstack, $ot );
1852 $badtag = 1;
1853 } else {
1854 if ( $t == 'table' ) {
1855 $tagstack = array_pop( $tablestack );
1857 $newparams = '';
1859 } else {
1860 # Keep track for later
1861 if ( in_array( $t, $tabletags ) &&
1862 ! in_array( 'table', $tagstack ) ) {
1863 $badtag = 1;
1864 } else if ( in_array( $t, $tagstack ) &&
1865 ! in_array ( $t , $htmlnest ) ) {
1866 $badtag = 1 ;
1867 } else if ( ! in_array( $t, $htmlsingle ) ) {
1868 if ( $t == 'table' ) {
1869 array_push( $tablestack, $tagstack );
1870 $tagstack = array();
1872 array_push( $tagstack, $t );
1874 # Strip non-approved attributes from the tag
1875 $newparams = $this->fixTagAttributes($params);
1878 if ( ! $badtag ) {
1879 $rest = str_replace( '>', '&gt;', $rest );
1880 $text .= "<$slash$t $newparams$brace$rest";
1881 continue;
1884 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1886 # Close off any remaining tags
1887 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1888 $text .= "</$t>\n";
1889 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1891 } else {
1892 # this might be possible using tidy itself
1893 foreach ( $bits as $x ) {
1894 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1895 $x, $regs );
1896 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1897 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1898 $newparams = $this->fixTagAttributes($params);
1899 $rest = str_replace( '>', '&gt;', $rest );
1900 $text .= "<$slash$t $newparams$brace$rest";
1901 } else {
1902 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1906 wfProfileOut( $fname );
1907 return $text;
1913 * This function accomplishes several tasks:
1914 * 1) Auto-number headings if that option is enabled
1915 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1916 * 3) Add a Table of contents on the top for users who have enabled the option
1917 * 4) Auto-anchor headings
1919 * It loops through all headlines, collects the necessary data, then splits up the
1920 * string and re-inserts the newly formatted headlines.
1924 /* private */ function formatHeadings( $text, $isMain=true ) {
1925 global $wgInputEncoding, $wgMaxTocLevel;
1927 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1928 $doShowToc = $this->mOptions->getShowToc();
1929 $forceTocHere = false;
1930 if( !$this->mTitle->userCanEdit() ) {
1931 $showEditLink = 0;
1932 $rightClickHack = 0;
1933 } else {
1934 $showEditLink = $this->mOptions->getEditSection();
1935 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1938 # Inhibit editsection links if requested in the page
1939 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1940 if( $esw->matchAndRemove( $text ) ) {
1941 $showEditLink = 0;
1943 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1944 # do not add TOC
1945 $mw =& MagicWord::get( MAG_NOTOC );
1946 if( $mw->matchAndRemove( $text ) ) {
1947 $doShowToc = 0;
1950 # never add the TOC to the Main Page. This is an entry page that should not
1951 # be more than 1-2 screens large anyway
1952 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
1953 $doShowToc = 0;
1956 # Get all headlines for numbering them and adding funky stuff like [edit]
1957 # links - this is for later, but we need the number of headlines right now
1958 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1960 # if there are fewer than 4 headlines in the article, do not show TOC
1961 if( $numMatches < 4 ) {
1962 $doShowToc = 0;
1965 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1966 # override above conditions and always show TOC at that place
1967 $mw =& MagicWord::get( MAG_TOC );
1968 if ($mw->match( $text ) ) {
1969 $doShowToc = 1;
1970 $forceTocHere = true;
1971 } else {
1972 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1973 # override above conditions and always show TOC above first header
1974 $mw =& MagicWord::get( MAG_FORCETOC );
1975 if ($mw->matchAndRemove( $text ) ) {
1976 $doShowToc = 1;
1982 # We need this to perform operations on the HTML
1983 $sk =& $this->mOptions->getSkin();
1985 # headline counter
1986 $headlineCount = 0;
1988 # Ugh .. the TOC should have neat indentation levels which can be
1989 # passed to the skin functions. These are determined here
1990 $toclevel = 0;
1991 $toc = '';
1992 $full = '';
1993 $head = array();
1994 $sublevelCount = array();
1995 $level = 0;
1996 $prevlevel = 0;
1997 foreach( $matches[3] as $headline ) {
1998 $numbering = '';
1999 if( $level ) {
2000 $prevlevel = $level;
2002 $level = $matches[1][$headlineCount];
2003 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2004 # reset when we enter a new level
2005 $sublevelCount[$level] = 0;
2006 $toc .= $sk->tocIndent( $level - $prevlevel );
2007 $toclevel += $level - $prevlevel;
2009 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2010 # reset when we step back a level
2011 $sublevelCount[$level+1]=0;
2012 $toc .= $sk->tocUnindent( $prevlevel - $level );
2013 $toclevel -= $prevlevel - $level;
2015 # count number of headlines for each level
2016 @$sublevelCount[$level]++;
2017 if( $doNumberHeadings || $doShowToc ) {
2018 $dot = 0;
2019 for( $i = 1; $i <= $level; $i++ ) {
2020 if( !empty( $sublevelCount[$i] ) ) {
2021 if( $dot ) {
2022 $numbering .= '.';
2024 $numbering .= $sublevelCount[$i];
2025 $dot = 1;
2030 # The canonized header is a version of the header text safe to use for links
2031 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2032 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2033 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2035 # strip out HTML
2036 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2037 $tocline = trim( $canonized_headline );
2038 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2039 $replacearray = array(
2040 '%3A' => ':',
2041 '%' => '.'
2043 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2044 $refer[$headlineCount] = $canonized_headline;
2046 # count how many in assoc. array so we can track dupes in anchors
2047 @$refers[$canonized_headline]++;
2048 $refcount[$headlineCount]=$refers[$canonized_headline];
2050 # Prepend the number to the heading text
2052 if( $doNumberHeadings || $doShowToc ) {
2053 $tocline = $numbering . ' ' . $tocline;
2055 # Don't number the heading if it is the only one (looks silly)
2056 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2057 # the two are different if the line contains a link
2058 $headline=$numbering . ' ' . $headline;
2062 # Create the anchor for linking from the TOC to the section
2063 $anchor = $canonized_headline;
2064 if($refcount[$headlineCount] > 1 ) {
2065 $anchor .= '_' . $refcount[$headlineCount];
2067 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2068 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2070 if( $showEditLink ) {
2071 if ( empty( $head[$headlineCount] ) ) {
2072 $head[$headlineCount] = '';
2074 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2077 # Add the edit section span
2078 if( $rightClickHack ) {
2079 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2082 # give headline the correct <h#> tag
2083 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2085 $headlineCount++;
2088 if( $doShowToc ) {
2089 $toclines = $headlineCount;
2090 $toc .= $sk->tocUnindent( $toclevel );
2091 $toc = $sk->tocTable( $toc );
2094 # split up and insert constructed headlines
2096 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2097 $i = 0;
2099 foreach( $blocks as $block ) {
2100 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2101 # This is the [edit] link that appears for the top block of text when
2102 # section editing is enabled
2104 # Disabled because it broke block formatting
2105 # For example, a bullet point in the top line
2106 # $full .= $sk->editSectionLink(0);
2108 $full .= $block;
2109 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2110 # Top anchor now in skin
2111 $full = $full.$toc;
2114 if( !empty( $head[$i] ) ) {
2115 $full .= $head[$i];
2117 $i++;
2119 if($forceTocHere) {
2120 $mw =& MagicWord::get( MAG_TOC );
2121 return $mw->replace( $toc, $full );
2122 } else {
2123 return $full;
2127 # Return an HTML link for the "ISBN 123456" text
2128 /* private */ function magicISBN( $text ) {
2129 global $wgLang;
2130 $fname = 'Parser::magicISBN';
2131 wfProfileIn( $fname );
2133 $a = split( 'ISBN ', " $text" );
2134 if ( count ( $a ) < 2 ) {
2135 wfProfileOut( $fname );
2136 return $text;
2138 $text = substr( array_shift( $a ), 1);
2139 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2141 foreach ( $a as $x ) {
2142 $isbn = $blank = '' ;
2143 while ( ' ' == $x{0} ) {
2144 $blank .= ' ';
2145 $x = substr( $x, 1 );
2147 while ( strstr( $valid, $x{0} ) != false ) {
2148 $isbn .= $x{0};
2149 $x = substr( $x, 1 );
2151 $num = str_replace( '-', '', $isbn );
2152 $num = str_replace( ' ', '', $num );
2154 if ( '' == $num ) {
2155 $text .= "ISBN $blank$x";
2156 } else {
2157 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2158 $text .= '<a href="' .
2159 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2160 "\" class=\"internal\">ISBN $isbn</a>";
2161 $text .= $x;
2164 wfProfileOut( $fname );
2165 return $text;
2168 # Return an HTML link for the "GEO ..." text
2169 /* private */ function magicGEO( $text ) {
2170 global $wgLang, $wgUseGeoMode;
2171 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2172 $fname = 'Parser::magicGEO';
2173 wfProfileIn( $fname );
2175 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2176 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2177 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2178 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2179 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2180 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2182 $a = split( 'GEO ', " $text" );
2183 if ( count ( $a ) < 2 ) {
2184 wfProfileOut( $fname );
2185 return $text;
2187 $text = substr( array_shift( $a ), 1);
2188 $valid = '0123456789.+-:';
2190 foreach ( $a as $x ) {
2191 $geo = $blank = '' ;
2192 while ( ' ' == $x{0} ) {
2193 $blank .= ' ';
2194 $x = substr( $x, 1 );
2196 while ( strstr( $valid, $x{0} ) != false ) {
2197 $geo .= $x{0};
2198 $x = substr( $x, 1 );
2200 $num = str_replace( '+', '', $geo );
2201 $num = str_replace( ' ', '', $num );
2203 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2204 $text .= "GEO $blank$x";
2205 } else {
2206 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2207 $text .= '<a href="' .
2208 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2209 "\" class=\"internal\">GEO $geo</a>";
2210 $text .= $x;
2213 wfProfileOut( $fname );
2214 return $text;
2217 # Return an HTML link for the "RFC 1234" text
2218 /* private */ function magicRFC( $text ) {
2219 global $wgLang;
2221 $a = split( 'RFC ', ' '.$text );
2222 if ( count ( $a ) < 2 ) return $text;
2223 $text = substr( array_shift( $a ), 1);
2224 $valid = '0123456789';
2226 foreach ( $a as $x ) {
2227 $rfc = $blank = '' ;
2228 while ( ' ' == $x{0} ) {
2229 $blank .= ' ';
2230 $x = substr( $x, 1 );
2232 while ( strstr( $valid, $x{0} ) != false ) {
2233 $rfc .= $x{0};
2234 $x = substr( $x, 1 );
2237 if ( '' == $rfc ) {
2238 $text .= "RFC $blank$x";
2239 } else {
2240 $url = wfmsg( 'rfcurl' );
2241 $url = str_replace( '$1', $rfc, $url);
2242 $sk =& $this->mOptions->getSkin();
2243 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2244 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2247 return $text;
2250 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2251 $this->mOptions = $options;
2252 $this->mTitle =& $title;
2253 $this->mOutputType = OT_WIKI;
2255 if ( $clearState ) {
2256 $this->clearState();
2259 $stripState = false;
2260 $pairs = array(
2261 "\r\n" => "\n",
2263 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2264 // now with regexes
2266 $pairs = array(
2267 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2268 "/<br *?>/i" => "<br />",
2270 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2272 $text = $this->strip( $text, $stripState, false );
2273 $text = $this->pstPass2( $text, $user );
2274 $text = $this->unstrip( $text, $stripState );
2275 $text = $this->unstripNoWiki( $text, $stripState );
2276 return $text;
2279 /* private */ function pstPass2( $text, &$user ) {
2280 global $wgLang, $wgLocaltimezone, $wgCurParser;
2282 # Variable replacement
2283 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2284 $text = $this->replaceVariables( $text );
2286 # Signatures
2288 $n = $user->getName();
2289 $k = $user->getOption( 'nickname' );
2290 if ( '' == $k ) { $k = $n; }
2291 if(isset($wgLocaltimezone)) {
2292 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2294 /* Note: this is an ugly timezone hack for the European wikis */
2295 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2296 ' (' . date( 'T' ) . ')';
2297 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2299 $text = preg_replace( '/~~~~~/', $d, $text );
2300 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2301 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2302 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2303 Namespace::getUser() ) . ":$n|$k]]", $text );
2305 # Context links: [[|name]] and [[name (context)|]]
2307 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2308 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2309 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2310 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2312 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2313 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2314 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2315 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2316 # [[ns:page (cont)|]]
2317 $context = "";
2318 $t = $this->mTitle->getText();
2319 if ( preg_match( $conpat, $t, $m ) ) {
2320 $context = $m[2];
2322 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2323 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2324 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2326 if ( '' == $context ) {
2327 $text = preg_replace( $p2, '[[\\1]]', $text );
2328 } else {
2329 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2333 $mw =& MagicWord::get( MAG_SUBST );
2334 $wgCurParser = $this->fork();
2335 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2336 $this->merge( $wgCurParser );
2339 # Trim trailing whitespace
2340 # MAG_END (__END__) tag allows for trailing
2341 # whitespace to be deliberately included
2342 $text = rtrim( $text );
2343 $mw =& MagicWord::get( MAG_END );
2344 $mw->matchAndRemove( $text );
2346 return $text;
2349 # Set up some variables which are usually set up in parse()
2350 # so that an external function can call some class members with confidence
2351 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2352 $this->mTitle =& $title;
2353 $this->mOptions = $options;
2354 $this->mOutputType = $outputType;
2355 if ( $clearState ) {
2356 $this->clearState();
2360 function transformMsg( $text, $options ) {
2361 global $wgTitle;
2362 static $executing = false;
2364 # Guard against infinite recursion
2365 if ( $executing ) {
2366 return $text;
2368 $executing = true;
2370 $this->mTitle = $wgTitle;
2371 $this->mOptions = $options;
2372 $this->mOutputType = OT_MSG;
2373 $this->clearState();
2374 $text = $this->replaceVariables( $text );
2376 $executing = false;
2377 return $text;
2380 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2381 # Callback will be called with the text within
2382 # Transform and return the text within
2383 function setHook( $tag, $callback ) {
2384 $oldVal = @$this->mTagHooks[$tag];
2385 $this->mTagHooks[$tag] = $callback;
2386 return $oldVal;
2390 class ParserOutput
2392 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2393 var $mCacheTime; # Used in ParserCache
2395 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2396 $containsOldMagic = false )
2398 $this->mText = $text;
2399 $this->mLanguageLinks = $languageLinks;
2400 $this->mCategoryLinks = $categoryLinks;
2401 $this->mContainsOldMagic = $containsOldMagic;
2402 $this->mCacheTime = "";
2405 function getText() { return $this->mText; }
2406 function getLanguageLinks() { return $this->mLanguageLinks; }
2407 function getCategoryLinks() { return $this->mCategoryLinks; }
2408 function getCacheTime() { return $this->mCacheTime; }
2409 function containsOldMagic() { return $this->mContainsOldMagic; }
2410 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2411 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2412 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2413 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2414 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2416 function merge( $other ) {
2417 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2418 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2419 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2424 class ParserOptions
2426 # All variables are private
2427 var $mUseTeX; # Use texvc to expand <math> tags
2428 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2429 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2430 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2431 var $mAllowExternalImages; # Allow external images inline
2432 var $mSkin; # Reference to the preferred skin
2433 var $mDateFormat; # Date format index
2434 var $mEditSection; # Create "edit section" links
2435 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2436 var $mNumberHeadings; # Automatically number headings
2437 var $mShowToc; # Show table of contents
2439 function getUseTeX() { return $this->mUseTeX; }
2440 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2441 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2442 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2443 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2444 function getSkin() { return $this->mSkin; }
2445 function getDateFormat() { return $this->mDateFormat; }
2446 function getEditSection() { return $this->mEditSection; }
2447 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2448 function getNumberHeadings() { return $this->mNumberHeadings; }
2449 function getShowToc() { return $this->mShowToc; }
2451 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2452 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2453 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2454 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2455 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2456 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2457 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2458 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2459 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2460 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2462 function setSkin( &$x ) { $this->mSkin =& $x; }
2464 /* static */ function newFromUser( &$user ) {
2465 $popts = new ParserOptions;
2466 $popts->initialiseFromUser( $user );
2467 return $popts;
2470 function initialiseFromUser( &$userInput ) {
2471 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2473 if ( !$userInput ) {
2474 $user = new User;
2475 $user->setLoaded( true );
2476 } else {
2477 $user =& $userInput;
2480 $this->mUseTeX = $wgUseTeX;
2481 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2482 $this->mUseDynamicDates = $wgUseDynamicDates;
2483 $this->mInterwikiMagic = $wgInterwikiMagic;
2484 $this->mAllowExternalImages = $wgAllowExternalImages;
2485 $this->mSkin =& $user->getSkin();
2486 $this->mDateFormat = $user->getOption( 'date' );
2487 $this->mEditSection = $user->getOption( 'editsection' );
2488 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2489 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2490 $this->mShowToc = $user->getOption( 'showtoc' );
2496 # Regex callbacks, used in Parser::replaceVariables
2497 function wfBraceSubstitution( $matches )
2499 global $wgCurParser;
2500 return $wgCurParser->braceSubstitution( $matches );
2503 function wfArgSubstitution( $matches )
2505 global $wgCurParser;
2506 return $wgCurParser->argSubstitution( $matches );
2509 function wfVariableSubstitution( $matches )
2511 global $wgCurParser;
2512 return $wgCurParser->variableSubstitution( $matches );