Fixed syntax error, added missing comma
[mediawiki.git] / includes / Parser.php
blobbcaf49f2740b13cd80d56a4169b39ebc216f4f50
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 );
35 # Allowed values for $mOutputType
36 define( "OT_HTML", 1 );
37 define( "OT_WIKI", 2 );
38 define( "OT_MSG", 3 );
40 # string parameter for extractTags which will cause it
41 # to strip HTML comments in addition to regular
42 # <XML>-style tags. This should not be anything we
43 # may want to use in wikisyntax
44 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
46 # prefix for escaping, used in two functions at least
47 define( "UNIQ_PREFIX", "NaodW29");
49 class Parser
51 # Persistent:
52 var $mTagHooks;
54 # Cleared with clearState():
55 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
56 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
58 # Temporary:
59 var $mOptions, $mTitle, $mOutputType;
61 function Parser() {
62 $this->mTagHooks = array();
63 $this->clearState();
66 function clearState() {
67 $this->mOutput = new ParserOutput;
68 $this->mAutonumber = 0;
69 $this->mLastSection = "";
70 $this->mDTopen = false;
71 $this->mVariables = false;
72 $this->mIncludeCount = array();
73 $this->mStripState = array();
74 $this->mArgStack = array();
75 $this->mInPre = false;
78 # First pass--just handle <nowiki> sections, pass the rest off
79 # to internalParse() which does all the real work.
81 # Returns a ParserOutput
83 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
84 global $wgUseTidy;
85 $fname = "Parser::parse";
86 wfProfileIn( $fname );
88 if ( $clearState ) {
89 $this->clearState();
92 $this->mOptions = $options;
93 $this->mTitle =& $title;
94 $this->mOutputType = OT_HTML;
96 $stripState = NULL;
97 $text = $this->strip( $text, $this->mStripState );
98 $text = $this->internalParse( $text, $linestart );
99 $text = $this->unstrip( $text, $this->mStripState );
100 # Clean up special characters, only run once, next-to-last before doBlockLevels
101 if(!$wgUseTidy) {
102 $fixtags = array(
103 # french spaces, last one Guillemet-left
104 # only if there is something before the space
105 '/(.) (\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
106 # french spaces, Guillemet-right
107 "/(\\302\\253) /i"=>"\\1&nbsp;",
108 '/<hr *>/i' => '<hr />',
109 '/<br *>/i' => '<br />',
110 '/<center *>/i' => '<div class="center">',
111 '/<\\/center *>/i' => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
117 } else {
118 $fixtags = array(
119 # french spaces, last one Guillemet-left
120 '/ (\\?|:|!|\\302\\273)/i' => '&nbsp;\\1',
121 # french spaces, Guillemet-right
122 '/(\\302\\253) /i' => '\\1&nbsp;',
123 '/([^> ]+(&#x30(1|3|9);)[^< ]*)/i' => '<span class="diacrit">\\1</span>',
124 '/<center *>/i' => '<div class="center">',
125 '/<\\/center *>/i' => '</div>'
127 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
129 # only once and last
130 $text = $this->doBlockLevels( $text, $linestart );
131 $text = $this->unstripNoWiki( $text, $this->mStripState );
132 if($wgUseTidy) {
133 $text = $this->tidy($text);
135 $this->mOutput->setText( $text );
136 wfProfileOut( $fname );
137 return $this->mOutput;
140 /* static */ function getRandomString() {
141 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
144 # Replaces all occurrences of <$tag>content</$tag> in the text
145 # with a random marker and returns the new text. the output parameter
146 # $content will be an associative array filled with data on the form
147 # $unique_marker => content.
149 # If $content is already set, the additional entries will be appended
151 # If $tag is set to STRIP_COMMENTS, the function will extract
152 # <!-- HTML comments -->
154 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
155 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
156 if ( !$content ) {
157 $content = array( );
159 $n = 1;
160 $stripped = '';
162 while ( '' != $text ) {
163 if($tag==STRIP_COMMENTS) {
164 $p = preg_split( '/<!--/i', $text, 2 );
165 } else {
166 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
168 $stripped .= $p[0];
169 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
170 $text = '';
171 } else {
172 if($tag==STRIP_COMMENTS) {
173 $q = preg_split( '/-->/i', $p[1], 2 );
174 } else {
175 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
177 $marker = $rnd . sprintf('%08X', $n++);
178 $content[$marker] = $q[0];
179 $stripped .= $marker;
180 $text = $q[1];
183 return $stripped;
186 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
187 # If $render is set, performs necessary rendering operations on plugins
188 # Returns the text, and fills an array with data needed in unstrip()
189 # If the $state is already a valid strip state, it adds to the state
191 # When $stripcomments is set, HTML comments <!-- like this -->
192 # will be stripped in addition to other tags. This is important
193 # for section editing, where these comments cause confusion when
194 # counting the sections in the wikisource
195 function strip( $text, &$state, $stripcomments = false ) {
196 $render = ($this->mOutputType == OT_HTML);
197 $nowiki_content = array();
198 $math_content = array();
199 $pre_content = array();
200 $comment_content = array();
201 $ext_content = array();
203 # Replace any instances of the placeholders
204 $uniq_prefix = UNIQ_PREFIX;
205 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
208 # nowiki
209 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
210 foreach( $nowiki_content as $marker => $content ){
211 if( $render ){
212 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
213 } else {
214 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
218 # math
219 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
220 foreach( $math_content as $marker => $content ){
221 if( $render ) {
222 if( $this->mOptions->getUseTeX() ) {
223 $math_content[$marker] = renderMath( $content );
224 } else {
225 $math_content[$marker] = "&lt;math&gt;$content&lt;math&gt;";
227 } else {
228 $math_content[$marker] = "<math>$content</math>";
232 # pre
233 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
234 foreach( $pre_content as $marker => $content ){
235 if( $render ){
236 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
237 } else {
238 $pre_content[$marker] = "<pre>$content</pre>";
242 # Comments
243 if($stripcomments) {
244 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
245 foreach( $comment_content as $marker => $content ){
246 $comment_content[$marker] = "<!--$content-->";
250 # Extensions
251 foreach ( $this->mTagHooks as $tag => $callback ) {
252 $ext_contents[$tag] = array();
253 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
254 foreach( $ext_content[$tag] as $marker => $content ) {
255 if ( $render ) {
256 $ext_content[$tag][$marker] = $callback( $content );
257 } else {
258 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
263 # Merge state with the pre-existing state, if there is one
264 if ( $state ) {
265 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
266 $state['math'] = $state['math'] + $math_content;
267 $state['pre'] = $state['pre'] + $pre_content;
268 $state['comment'] = $state['comment'] + $comment_content;
270 foreach( $ext_content as $tag => $array ) {
271 if ( array_key_exists( $tag, $state ) ) {
272 $state[$tag] = $state[$tag] + $array;
275 } else {
276 $state = array(
277 'nowiki' => $nowiki_content,
278 'math' => $math_content,
279 'pre' => $pre_content,
280 'comment' => $comment_content,
281 ) + $ext_content;
283 return $text;
286 # always call unstripNoWiki() after this one
287 function unstrip( $text, &$state ) {
288 # Must expand in reverse order, otherwise nested tags will be corrupted
289 $contentDict = end( $state );
290 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
291 if( key($state) != 'nowiki') {
292 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
293 $text = str_replace( key( $contentDict ), $content, $text );
298 return $text;
300 # always call this after unstrip() to preserve the order
301 function unstripNoWiki( $text, &$state ) {
302 # Must expand in reverse order, otherwise nested tags will be corrupted
303 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
304 $text = str_replace( key( $state['nowiki'] ), $content, $text );
307 return $text;
310 # Add an item to the strip state
311 # Returns the unique tag which must be inserted into the stripped text
312 # The tag will be replaced with the original text in unstrip()
314 function insertStripItem( $text, &$state ) {
315 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
316 if ( !$state ) {
317 $state = array(
318 'nowiki' => array(),
319 'math' => array(),
320 'pre' => array()
323 $state['item'][$rnd] = $text;
324 return $rnd;
327 # categoryMagic
328 # generate a list of subcategories and pages for a category
329 # depending on wfMsg("usenewcategorypage") it either calls the new
330 # or the old code. The new code will not work properly for some
331 # languages due to sorting issues, so they might want to turn it
332 # off.
333 function categoryMagic() {
334 $msg = wfMsg('usenewcategorypage');
335 if ( '0' == @$msg[0] )
337 return $this->oldCategoryMagic();
338 } else {
339 return $this->newCategoryMagic();
343 # This method generates the list of subcategories and pages for a category
344 function oldCategoryMagic () {
345 global $wgLang , $wgUser ;
346 $fname = 'Parser::oldCategoryMagic';
348 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
350 $cns = Namespace::getCategory() ;
351 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
353 $r = "<br style=\"clear:both;\"/>\n";
356 $sk =& $wgUser->getSkin() ;
358 $articles = array() ;
359 $children = array() ;
360 $data = array () ;
361 $id = $this->mTitle->getArticleID() ;
363 # FIXME: add limits
364 $dbr =& wfGetDB( DB_SLAVE );
365 $cur = $dbr->tableName( 'cur' );
366 $categorylinks = $dbr->tableName( 'categorylinks' );
368 $t = $dbr->strencode( $this->mTitle->getDBKey() );
369 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
370 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
371 $res = $dbr->query( $sql, $fname ) ;
372 while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
374 # For all pages that link to this category
375 foreach ( $data AS $x )
377 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
378 if ( $t != "" ) $t .= ":" ;
379 $t .= $x->cur_title ;
381 if ( $x->cur_namespace == $cns ) {
382 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
383 } else {
384 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
387 $dbr->freeResult ( $res ) ;
389 # Showing subcategories
390 if ( count ( $children ) > 0 ) {
391 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
392 $r .= implode ( ', ' , $children ) ;
395 # Showing pages in this category
396 if ( count ( $articles ) > 0 ) {
397 $ti = $this->mTitle->getText() ;
398 $h = wfMsg( 'category_header', $ti );
399 $r .= "<h2>{$h}</h2>\n" ;
400 $r .= implode ( ', ' , $articles ) ;
403 return $r ;
408 function newCategoryMagic () {
409 global $wgLang , $wgUser ;
410 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
412 $cns = Namespace::getCategory() ;
413 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
415 $r = "<br style=\"clear:both;\"/>\n";
418 $sk =& $wgUser->getSkin() ;
420 $articles = array() ;
421 $articles_start_char = array();
422 $children = array() ;
423 $children_start_char = array();
424 $data = array () ;
425 $id = $this->mTitle->getArticleID() ;
427 # FIXME: add limits
428 $dbr =& wfGetDB( DB_SLAVE );
429 $cur = $dbr->tableName( 'cur' );
430 $categorylinks = $dbr->tableName( 'categorylinks' );
432 $t = $dbr->strencode( $this->mTitle->getDBKey() );
433 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
434 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
435 $res = $dbr->query ( $sql ) ;
436 while ( $x = $dbr->fetchObject ( $res ) )
438 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
439 if ( $t != '' ) $t .= ':' ;
440 $t .= $x->cur_title ;
442 if ( $x->cur_namespace == $cns ) {
443 $ctitle = str_replace( '_',' ',$x->cur_title );
444 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
446 // If there's a link from Category:A to Category:B, the sortkey of the resulting
447 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
448 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
449 // else use sortkey...
450 if ( ($ns.":".$ctitle) == $x->cl_sortkey ) {
451 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) );
452 } else {
453 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
455 } else {
456 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
457 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
460 $dbr->freeResult ( $res ) ;
462 $ti = $this->mTitle->getText() ;
464 # Don't show subcategories section if there are none.
465 if ( count ( $children ) > 0 )
467 # Showing subcategories
468 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
469 . wfMsg( 'subcategorycount', count( $children ) );
470 if ( count ( $children ) > 6 ) {
472 // divide list into three equal chunks
473 $chunk = (int) (count ( $children ) / 3);
475 // get and display header
476 $r .= '<table width="100%"><tr valign="top">';
478 $startChunk = 0;
479 $endChunk = $chunk;
481 // loop through the chunks
482 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
483 $chunkIndex < 3;
484 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
487 $r .= '<td><ul>';
488 // output all subcategories to category
489 for ($index = $startChunk ;
490 $index < $endChunk && $index < count($children);
491 $index++ )
493 // check for change of starting letter or begging of chunk
494 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
495 || ($index == $startChunk) )
497 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
500 $r .= "<li>{$children[$index]}</li>";
502 $r .= '</ul></td>';
506 $r .= '</tr></table>';
507 } else {
508 // for short lists of subcategories to category.
510 $r .= "<h3>{$children_start_char[0]}</h3>\n";
511 $r .= '<ul><li>'.$children[0].'</li>';
512 for ($index = 1; $index < count($children); $index++ )
514 if ($children_start_char[$index] != $children_start_char[$index - 1])
516 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
519 $r .= "<li>{$children[$index]}</li>";
521 $r .= '</ul>';
523 } # END of if ( count($children) > 0 )
525 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
526 wfMsg( 'categoryarticlecount', count( $articles ) );
528 # Showing articles in this category
529 if ( count ( $articles ) > 6) {
530 $ti = $this->mTitle->getText() ;
532 // divide list into three equal chunks
533 $chunk = (int) (count ( $articles ) / 3);
535 // get and display header
536 $r .= '<table width="100%"><tr valign="top">';
538 // loop through the chunks
539 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
540 $chunkIndex < 3;
541 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
544 $r .= '<td><ul>';
546 // output all articles in category
547 for ($index = $startChunk ;
548 $index < $endChunk && $index < count($articles);
549 $index++ )
551 // check for change of starting letter or begging of chunk
552 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
553 || ($index == $startChunk) )
555 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
558 $r .= "<li>{$articles[$index]}</li>";
560 $r .= '</ul></td>';
564 $r .= '</tr></table>';
565 } elseif ( count ( $articles ) > 0) {
566 // for short lists of articles in categories.
567 $ti = $this->mTitle->getText() ;
569 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
570 $r .= '<ul><li>'.$articles[0].'</li>';
571 for ($index = 1; $index < count($articles); $index++ )
573 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
575 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
578 $r .= "<li>{$articles[$index]}</li>";
580 $r .= '</ul>';
584 return $r ;
587 # Return allowed HTML attributes
588 function getHTMLattrs () {
589 $htmlattrs = array( # Allowed attributes--no scripting, etc.
590 'title', 'align', 'lang', 'dir', 'width', 'height',
591 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
592 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
593 /* FONT */ 'type', 'start', 'value', 'compact',
594 /* For various lists, mostly deprecated but safe */
595 'summary', 'width', 'border', 'frame', 'rules',
596 'cellspacing', 'cellpadding', 'valign', 'char',
597 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
598 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
599 'id', 'class', 'name', 'style' /* For CSS */
601 return $htmlattrs ;
604 # Remove non approved attributes and javascript in css
605 function fixTagAttributes ( $t ) {
606 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
607 $htmlattrs = $this->getHTMLattrs() ;
609 # Strip non-approved attributes from the tag
610 $t = preg_replace(
611 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
612 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
613 $t);
614 # Strip javascript "expression" from stylesheets. Brute force approach:
615 # If anythin offensive is found, all attributes of the HTML tag are dropped
617 if( preg_match(
618 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
619 wfMungeToUtf8( $t ) ) )
621 $t='';
624 return trim ( $t ) ;
627 # interface with html tidy, used if $wgUseTidy = true
628 function tidy ( $text ) {
629 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
630 global $wgInputEncoding, $wgOutputEncoding;
631 $fname = 'Parser::tidy';
632 wfProfileIn( $fname );
634 $cleansource = '';
635 switch(strtoupper($wgOutputEncoding)) {
636 case 'ISO-8859-1':
637 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
638 break;
639 case 'UTF-8':
640 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
641 break;
642 default:
643 $wgTidyOpts .= ' -raw';
646 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
647 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
648 '<head><title>test</title></head><body>'.$text.'</body></html>';
649 $descriptorspec = array(
650 0 => array('pipe', 'r'),
651 1 => array('pipe', 'w'),
652 2 => array('file', '/dev/null', 'a')
654 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
655 if (is_resource($process)) {
656 fwrite($pipes[0], $wrappedtext);
657 fclose($pipes[0]);
658 while (!feof($pipes[1])) {
659 $cleansource .= fgets($pipes[1], 1024);
661 fclose($pipes[1]);
662 $return_value = proc_close($process);
665 wfProfileOut( $fname );
667 if( $cleansource == '' && $text != '') {
668 wfDebug( "Tidy error detected!\n" );
669 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
670 } else {
671 return $cleansource;
675 # parse the wiki syntax used to render tables
676 function doTableStuff ( $t ) {
677 $fname = 'Parser::doTableStuff';
678 wfProfileIn( $fname );
680 $t = explode ( "\n" , $t ) ;
681 $td = array () ; # Is currently a td tag open?
682 $ltd = array () ; # Was it TD or TH?
683 $tr = array () ; # Is currently a tr tag open?
684 $ltr = array () ; # tr attributes
685 foreach ( $t AS $k => $x )
687 $x = trim ( $x ) ;
688 $fc = substr ( $x , 0 , 1 ) ;
689 if ( '{|' == substr ( $x , 0 , 2 ) )
691 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 2 ) ) . '>' ;
692 array_push ( $td , false ) ;
693 array_push ( $ltd , '' ) ;
694 array_push ( $tr , false ) ;
695 array_push ( $ltr , '' ) ;
697 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
698 else if ( '|}' == substr ( $x , 0 , 2 ) )
700 $z = "</table>\n" ;
701 $l = array_pop ( $ltd ) ;
702 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
703 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
704 array_pop ( $ltr ) ;
705 $t[$k] = $z ;
707 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
709 $x = substr ( $x , 1 ) ;
710 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
711 $z = '' ;
712 $l = array_pop ( $ltd ) ;
713 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
714 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
715 array_pop ( $ltr ) ;
716 $t[$k] = $z ;
717 array_push ( $tr , false ) ;
718 array_push ( $td , false ) ;
719 array_push ( $ltd , '' ) ;
720 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
722 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
724 if ( '|+' == substr ( $x , 0 , 2 ) )
726 $fc = '+' ;
727 $x = substr ( $x , 1 ) ;
729 $after = substr ( $x , 1 ) ;
730 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
731 $after = explode ( '||' , $after ) ;
732 $t[$k] = '' ;
733 foreach ( $after AS $theline )
735 $z = '' ;
736 if ( $fc != '+' )
738 $tra = array_pop ( $ltr ) ;
739 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
740 array_push ( $tr , true ) ;
741 array_push ( $ltr , '' ) ;
744 $l = array_pop ( $ltd ) ;
745 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
746 if ( $fc == '|' ) $l = 'td' ;
747 else if ( $fc == '!' ) $l = 'th' ;
748 else if ( $fc == '+' ) $l = 'caption' ;
749 else $l = '' ;
750 array_push ( $ltd , $l ) ;
751 $y = explode ( '|' , $theline , 2 ) ;
752 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
753 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
754 $t[$k] .= $y ;
755 array_push ( $td , true ) ;
760 # Closing open td, tr && table
761 while ( count ( $td ) > 0 )
763 if ( array_pop ( $td ) ) $t[] = '</td>' ;
764 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
765 $t[] = '</table>' ;
768 $t = implode ( "\n" , $t ) ;
769 # $t = $this->removeHTMLtags( $t );
770 wfProfileOut( $fname );
771 return $t ;
774 # Parses the text and adds the result to the strip state
775 # Returns the strip tag
776 function stripParse( $text, $newline, $args )
778 $text = $this->strip( $text, $this->mStripState );
779 $text = $this->internalParse( $text, (bool)$newline, $args, false );
780 return $newline.$this->insertStripItem( $text, $this->mStripState );
783 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
784 $fname = 'Parser::internalParse';
785 wfProfileIn( $fname );
787 $text = $this->removeHTMLtags( $text );
788 $text = $this->replaceVariables( $text, $args );
790 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
792 $text = $this->doHeadings( $text );
793 if($this->mOptions->getUseDynamicDates()) {
794 global $wgDateFormatter;
795 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
797 $text = $this->doAllQuotes( $text );
798 // $text = $this->doExponent( $text );
799 $text = $this->replaceExternalLinks( $text );
800 $text = $this->replaceInternalLinks ( $text );
801 $text = $this->replaceInternalLinks ( $text );
802 //$text = $this->doTokenizedParser ( $text );
803 $text = $this->doTableStuff ( $text ) ;
804 $text = $this->magicISBN( $text );
805 $text = $this->magicGEO( $text );
806 $text = $this->magicRFC( $text );
807 $text = $this->formatHeadings( $text, $isMain );
808 $sk =& $this->mOptions->getSkin();
809 $text = $sk->transformContent( $text );
811 if ( !isset ( $this->categoryMagicDone ) ) {
812 $text .= $this->categoryMagic () ;
813 $this->categoryMagicDone = true ;
816 wfProfileOut( $fname );
817 return $text;
820 # Parse ^^ tokens and return html
821 /* private */ function doExponent ( $text )
823 $fname = 'Parser::doExponent';
824 wfProfileIn( $fname);
825 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
826 wfProfileOut( $fname);
827 return $text;
830 # Parse headers and return html
831 /* private */ function doHeadings( $text ) {
832 $fname = 'Parser::doHeadings';
833 wfProfileIn( $fname );
834 for ( $i = 6; $i >= 1; --$i ) {
835 $h = substr( '======', 0, $i );
836 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
837 "<h{$i}>\\1</h{$i}>\\2", $text );
839 wfProfileOut( $fname );
840 return $text;
843 /* private */ function doAllQuotes( $text ) {
844 $fname = 'Parser::doAllQuotes';
845 wfProfileIn( $fname );
846 $outtext = '';
847 $lines = explode( "\n", $text );
848 foreach ( $lines as $line ) {
849 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
851 $outtext = substr($outtext, 0,-1);
852 wfProfileOut( $fname );
853 return $outtext;
856 /* private */ function doQuotes( $pre, $text, $mode ) {
857 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
858 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
859 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
860 if ( substr ($m[2], 0, 1) == '\'' ) {
861 $m[2] = substr ($m[2], 1);
862 if ($mode == 'em') {
863 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'emstrong' );
864 } else if ($mode == 'strong') {
865 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
866 } else if (($mode == 'emstrong') || ($mode == 'both')) {
867 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
868 } else if ($mode == 'strongem') {
869 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
870 } else {
871 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
873 } else {
874 if ($mode == 'strong') {
875 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'strongem' );
876 } else if ($mode == 'em') {
877 return $m1_em . $this->doQuotes ( '', $m[2], '' );
878 } else if ($mode == 'emstrong') {
879 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
880 } else if (($mode == 'strongem') || ($mode == 'both')) {
881 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
882 } else {
883 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
886 } else {
887 $text_strong = ($text == '') ? '' : "<strong>{$text}</strong>";
888 $text_em = ($text == '') ? '' : "<em>{$text}</em>";
889 if ($mode == '') {
890 return $pre . $text;
891 } else if ($mode == 'em') {
892 return $pre . $text_em;
893 } else if ($mode == 'strong') {
894 return $pre . $text_strong;
895 } else if ($mode == 'strongem') {
896 return (($pre == '') && ($text == '')) ? '' : "<strong>{$pre}{$text_em}</strong>";
897 } else {
898 return (($pre == '') && ($text == '')) ? '' : "<em>{$pre}{$text_strong}</em>";
903 # Note: we have to do external links before the internal ones,
904 # and otherwise take great care in the order of things here, so
905 # that we don't end up interpreting some URLs twice.
907 /* private */ function replaceExternalLinks( $text ) {
908 $fname = 'Parser::replaceExternalLinks';
909 wfProfileIn( $fname );
910 $text = $this->subReplaceExternalLinks( $text, 'http', true );
911 $text = $this->subReplaceExternalLinks( $text, 'https', true );
912 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
913 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
914 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
915 $text = $this->subReplaceExternalLinks( $text, 'news', false );
916 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
917 wfProfileOut( $fname );
918 return $text;
921 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
922 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
923 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
925 # this is the list of separators that should be ignored if they
926 # are the last character of an URL but that should be included
927 # if they occur within the URL, e.g. "go to www.foo.com, where .."
928 # in this case, the last comma should not become part of the URL,
929 # but in "www.foo.com/123,2342,32.htm" it should.
930 $sep = ",;\.:";
931 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
932 $images = 'gif|png|jpg|jpeg';
934 # PLEASE NOTE: The curly braces { } are not part of the regex,
935 # they are interpreted as part of the string (used to tell PHP
936 # that the content of the string should be inserted there).
937 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
938 "((?i){$images})([^{$uc}]|$)/";
940 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
941 $sk =& $this->mOptions->getSkin();
943 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
944 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
945 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
947 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
948 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
949 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
950 '</a>\\5', $s );
951 $s = str_replace( $unique, $protocol, $s );
953 $a = explode( "[{$protocol}:", " " . $s );
954 $s = array_shift( $a );
955 $s = substr( $s, 1 );
957 # Regexp for URL in square brackets
958 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
959 # Regexp for URL with link text in square brackets
960 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
962 foreach ( $a as $line ) {
964 # CASE 1: Link in square brackets, e.g.
965 # some text [http://domain.tld/some.link] more text
966 if ( preg_match( $e1, $line, $m ) ) {
967 $link = "{$protocol}:{$m[1]}";
968 $trail = $m[2];
969 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
970 else { $text = wfEscapeHTML( $link ); }
973 # CASE 2: Link with link text and text directly following it, e.g.
974 # This is a collection of [http://domain.tld/some.link link]s
975 else if ( preg_match( $e2, $line, $m ) ) {
976 $link = "{$protocol}:{$m[1]}";
977 $text = $m[2];
978 $dtrail = '';
979 $trail = $m[3];
980 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
981 $dtrail = $m2[1];
982 $trail = $m2[2];
986 # CASE 3: Nothing matches, just output the source text
987 else {
988 $s .= "[{$protocol}:" . $line;
989 continue;
992 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
993 $paren = '';
994 } else {
995 # Expand the URL for printable version
996 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
998 $la = $sk->getExternalLinkAttributes( $link, $text );
999 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1002 return $s;
1006 /* private */ function replaceInternalLinks( $s ) {
1007 global $wgLang, $wgLinkCache;
1008 global $wgNamespacesWithSubpages, $wgLanguageCode;
1009 static $fname = 'Parser::replaceInternalLinks' ;
1010 wfProfileIn( $fname );
1012 wfProfileIn( $fname.'-setup' );
1013 static $tc = FALSE;
1014 # the % is needed to support urlencoded titles as well
1015 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1016 $sk =& $this->mOptions->getSkin();
1018 $a = explode( '[[', ' ' . $s );
1019 $s = array_shift( $a );
1020 $s = substr( $s, 1 );
1022 # Match a link having the form [[namespace:link|alternate]]trail
1023 static $e1 = FALSE;
1024 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1025 # Match the end of a line for a word that's not followed by whitespace,
1026 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1027 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1029 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1030 # Special and Media are pseudo-namespaces; no pages actually exist in them
1031 static $image = FALSE;
1032 static $special = FALSE;
1033 static $media = FALSE;
1034 static $category = FALSE;
1035 if ( !$image ) { $image = Namespace::getImage(); }
1036 if ( !$special ) { $special = Namespace::getSpecial(); }
1037 if ( !$media ) { $media = Namespace::getMedia(); }
1038 if ( !$category ) { $category = Namespace::getCategory(); }
1040 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1042 if ( $useLinkPrefixExtension ) {
1043 if ( preg_match( $e2, $s, $m ) ) {
1044 $first_prefix = $m[2];
1045 $s = $m[1];
1046 } else {
1047 $first_prefix = false;
1049 } else {
1050 $prefix = '';
1053 wfProfileOut( $fname.'-setup' );
1055 foreach ( $a as $line ) {
1056 wfProfileIn( $fname.'-prefixhandling' );
1057 if ( $useLinkPrefixExtension ) {
1058 if ( preg_match( $e2, $s, $m ) ) {
1059 $prefix = $m[2];
1060 $s = $m[1];
1061 } else {
1062 $prefix='';
1064 # first link
1065 if($first_prefix) {
1066 $prefix = $first_prefix;
1067 $first_prefix = false;
1070 wfProfileOut( $fname.'-prefixhandling' );
1072 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1073 $text = $m[2];
1074 # fix up urlencoded title texts
1075 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1076 $trail = $m[3];
1077 } else { # Invalid form; output directly
1078 $s .= $prefix . '[[' . $line ;
1079 continue;
1082 /* Valid link forms:
1083 Foobar -- normal
1084 :Foobar -- override special treatment of prefix (images, language links)
1085 /Foobar -- convert to CurrentPage/Foobar
1086 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1088 $c = substr($m[1],0,1);
1089 $noforce = ($c != ':');
1090 if( $c == '/' ) { # subpage
1091 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1092 $m[1]=substr($m[1],1,strlen($m[1])-2);
1093 $noslash=$m[1];
1094 } else {
1095 $noslash=substr($m[1],1);
1097 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1098 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1099 if( '' == $text ) {
1100 $text= $m[1];
1101 } # this might be changed for ugliness reasons
1102 } else {
1103 $link = $noslash; # no subpage allowed, use standard link
1105 } elseif( $noforce ) { # no subpage
1106 $link = $m[1];
1107 } else {
1108 $link = substr( $m[1], 1 );
1110 $wasblank = ( '' == $text );
1111 if( $wasblank )
1112 $text = $link;
1114 $nt = Title::newFromText( $link );
1115 if( !$nt ) {
1116 $s .= $prefix . '[[' . $line;
1117 continue;
1119 $ns = $nt->getNamespace();
1120 $iw = $nt->getInterWiki();
1121 if( $noforce ) {
1122 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1123 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
1124 $tmp = $prefix . $trail ;
1125 $s .= (trim($tmp) == '')? '': $tmp;
1126 continue;
1128 if ( $ns == $image ) {
1129 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1130 $wgLinkCache->addImageLinkObj( $nt );
1131 continue;
1133 if ( $ns == $category ) {
1134 $t = $nt->getText() ;
1135 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1137 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1138 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1139 $wgLinkCache->resume();
1141 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1142 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1143 $this->mOutput->mCategoryLinks[] = $t ;
1144 $s .= $prefix . $trail ;
1145 continue;
1148 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1149 ( strpos( $link, '#' ) == FALSE ) ) {
1150 # Self-links are handled specially; generally de-link and change to bold.
1151 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1152 continue;
1155 if( $ns == $media ) {
1156 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1157 $wgLinkCache->addImageLinkObj( $nt );
1158 continue;
1159 } elseif( $ns == $special ) {
1160 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1161 continue;
1163 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1165 wfProfileOut( $fname );
1166 return $s;
1169 # Some functions here used by doBlockLevels()
1171 /* private */ function closeParagraph() {
1172 $result = '';
1173 if ( '' != $this->mLastSection ) {
1174 $result = '</' . $this->mLastSection . ">\n";
1176 $this->mInPre = false;
1177 $this->mLastSection = '';
1178 return $result;
1180 # getCommon() returns the length of the longest common substring
1181 # of both arguments, starting at the beginning of both.
1183 /* private */ function getCommon( $st1, $st2 ) {
1184 $fl = strlen( $st1 );
1185 $shorter = strlen( $st2 );
1186 if ( $fl < $shorter ) { $shorter = $fl; }
1188 for ( $i = 0; $i < $shorter; ++$i ) {
1189 if ( $st1{$i} != $st2{$i} ) { break; }
1191 return $i;
1193 # These next three functions open, continue, and close the list
1194 # element appropriate to the prefix character passed into them.
1196 /* private */ function openList( $char )
1198 $result = $this->closeParagraph();
1200 if ( '*' == $char ) { $result .= '<ul><li>'; }
1201 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1202 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1203 else if ( ';' == $char ) {
1204 $result .= '<dl><dt>';
1205 $this->mDTopen = true;
1207 else { $result = '<!-- ERR 1 -->'; }
1209 return $result;
1212 /* private */ function nextItem( $char ) {
1213 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1214 else if ( ':' == $char || ';' == $char ) {
1215 $close = "</dd>";
1216 if ( $this->mDTopen ) { $close = '</dt>'; }
1217 if ( ';' == $char ) {
1218 $this->mDTopen = true;
1219 return $close . '<dt>';
1220 } else {
1221 $this->mDTopen = false;
1222 return $close . '<dd>';
1225 return '<!-- ERR 2 -->';
1228 /* private */function closeList( $char ) {
1229 if ( '*' == $char ) { $text = '</li></ul>'; }
1230 else if ( '#' == $char ) { $text = '</li></ol>'; }
1231 else if ( ':' == $char ) {
1232 if ( $this->mDTopen ) {
1233 $this->mDTopen = false;
1234 $text = '</dt></dl>';
1235 } else {
1236 $text = '</dd></dl>';
1239 else { return '<!-- ERR 3 -->'; }
1240 return $text."\n";
1243 /* private */ function doBlockLevels( $text, $linestart ) {
1244 $fname = 'Parser::doBlockLevels';
1245 wfProfileIn( $fname );
1247 # Parsing through the text line by line. The main thing
1248 # happening here is handling of block-level elements p, pre,
1249 # and making lists from lines starting with * # : etc.
1251 $textLines = explode( "\n", $text );
1253 $lastPrefix = $output = $lastLine = '';
1254 $this->mDTopen = $inBlockElem = false;
1255 $prefixLength = 0;
1256 $paragraphStack = false;
1258 if ( !$linestart ) {
1259 $output .= array_shift( $textLines );
1261 foreach ( $textLines as $oLine ) {
1262 $lastPrefixLength = strlen( $lastPrefix );
1263 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1264 $preOpenMatch = preg_match("/<pre/i", $oLine );
1265 if ( !$this->mInPre ) {
1266 # Multiple prefixes may abut each other for nested lists.
1267 $prefixLength = strspn( $oLine, '*#:;' );
1268 $pref = substr( $oLine, 0, $prefixLength );
1270 # eh?
1271 $pref2 = str_replace( ';', ':', $pref );
1272 $t = substr( $oLine, $prefixLength );
1273 $this->mInPre = !empty($preOpenMatch);
1274 } else {
1275 # Don't interpret any other prefixes in preformatted text
1276 $prefixLength = 0;
1277 $pref = $pref2 = '';
1278 $t = $oLine;
1281 # List generation
1282 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1283 # Same as the last item, so no need to deal with nesting or opening stuff
1284 $output .= $this->nextItem( substr( $pref, -1 ) );
1285 $paragraphStack = false;
1287 if ( ";" == substr( $pref, -1 ) ) {
1288 # The one nasty exception: definition lists work like this:
1289 # ; title : definition text
1290 # So we check for : in the remainder text to split up the
1291 # title and definition, without b0rking links.
1292 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1293 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1294 $term = $match[1];
1295 $output .= $term . $this->nextItem( ':' );
1296 $t = $match[2];
1299 } elseif( $prefixLength || $lastPrefixLength ) {
1300 # Either open or close a level...
1301 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1302 $paragraphStack = false;
1304 while( $commonPrefixLength < $lastPrefixLength ) {
1305 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1306 --$lastPrefixLength;
1308 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1309 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1311 while ( $prefixLength > $commonPrefixLength ) {
1312 $char = substr( $pref, $commonPrefixLength, 1 );
1313 $output .= $this->openList( $char );
1315 if ( ';' == $char ) {
1316 # FIXME: This is dupe of code above
1317 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1318 $term = $match[1];
1319 $output .= $term . $this->nextItem( ":" );
1320 $t = $match[2];
1323 ++$commonPrefixLength;
1325 $lastPrefix = $pref2;
1327 if( 0 == $prefixLength ) {
1328 # No prefix (not in list)--go to paragraph mode
1329 $uniq_prefix = UNIQ_PREFIX;
1330 // XXX: use a stack for nestable elements like span, table and div
1331 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1332 $closematch = preg_match(
1333 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1334 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1335 if ( $openmatch or $closematch ) {
1336 $paragraphStack = false;
1337 $output .= $this->closeParagraph();
1338 if($preOpenMatch and !$preCloseMatch) {
1339 $this->mInPre = true;
1341 if ( $closematch ) {
1342 $inBlockElem = false;
1343 } else {
1344 $inBlockElem = true;
1346 } else if ( !$inBlockElem && !$this->mInPre ) {
1347 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1348 // pre
1349 if ($this->mLastSection != 'pre') {
1350 $paragraphStack = false;
1351 $output .= $this->closeParagraph().'<pre>';
1352 $this->mLastSection = 'pre';
1354 } else {
1355 // paragraph
1356 if ( '' == trim($t) ) {
1357 if ( $paragraphStack ) {
1358 $output .= $paragraphStack.'<br />';
1359 $paragraphStack = false;
1360 $this->mLastSection = 'p';
1361 } else {
1362 if ($this->mLastSection != 'p' ) {
1363 $output .= $this->closeParagraph();
1364 $this->mLastSection = '';
1365 $paragraphStack = '<p>';
1366 } else {
1367 $paragraphStack = '</p><p>';
1370 } else {
1371 if ( $paragraphStack ) {
1372 $output .= $paragraphStack;
1373 $paragraphStack = false;
1374 $this->mLastSection = 'p';
1375 } else if ($this->mLastSection != 'p') {
1376 $output .= $this->closeParagraph().'<p>';
1377 $this->mLastSection = 'p';
1383 if ($paragraphStack === false) {
1384 $output .= $t."\n";
1387 while ( $prefixLength ) {
1388 $output .= $this->closeList( $pref2{$prefixLength-1} );
1389 --$prefixLength;
1391 if ( '' != $this->mLastSection ) {
1392 $output .= '</' . $this->mLastSection . '>';
1393 $this->mLastSection = '';
1396 wfProfileOut( $fname );
1397 return $output;
1400 # Return value of a magic variable (like PAGENAME)
1401 function getVariableValue( $index ) {
1402 global $wgLang, $wgSitename, $wgServer;
1404 switch ( $index ) {
1405 case MAG_CURRENTMONTH:
1406 return date( 'm' );
1407 case MAG_CURRENTMONTHNAME:
1408 return $wgLang->getMonthName( date('n') );
1409 case MAG_CURRENTMONTHNAMEGEN:
1410 return $wgLang->getMonthNameGen( date('n') );
1411 case MAG_CURRENTDAY:
1412 return date('j');
1413 case MAG_PAGENAME:
1414 return $this->mTitle->getText();
1415 case MAG_NAMESPACE:
1416 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1417 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1418 case MAG_CURRENTDAYNAME:
1419 return $wgLang->getWeekdayName( date('w')+1 );
1420 case MAG_CURRENTYEAR:
1421 return date( 'Y' );
1422 case MAG_CURRENTTIME:
1423 return $wgLang->time( wfTimestampNow(), false );
1424 case MAG_NUMBEROFARTICLES:
1425 return wfNumberOfArticles();
1426 case MAG_SITENAME:
1427 return $wgSitename;
1428 case MAG_SERVER:
1429 return $wgServer;
1430 default:
1431 return NULL;
1435 # initialise the magic variables (like CURRENTMONTHNAME)
1436 function initialiseVariables() {
1437 global $wgVariableIDs;
1438 $this->mVariables = array();
1439 foreach ( $wgVariableIDs as $id ) {
1440 $mw =& MagicWord::get( $id );
1441 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1445 /* private */ function replaceVariables( $text, $args = array() ) {
1446 global $wgLang, $wgScript, $wgArticlePath;
1448 $fname = 'Parser::replaceVariables';
1449 wfProfileIn( $fname );
1451 $bail = false;
1452 $titleChars = Title::legalChars();
1453 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1455 # This function is called recursively. To keep track of arguments we need a stack:
1456 array_push( $this->mArgStack, $args );
1458 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1459 $GLOBALS['wgCurParser'] =& $this;
1462 if ( $this->mOutputType == OT_HTML ) {
1463 # Variable substitution
1464 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1466 # Argument substitution
1467 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1469 # Template substitution
1470 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1471 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1473 array_pop( $this->mArgStack );
1475 wfProfileOut( $fname );
1476 return $text;
1479 function variableSubstitution( $matches ) {
1480 if ( !$this->mVariables ) {
1481 $this->initialiseVariables();
1483 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1484 $text = $this->mVariables[$matches[1]];
1485 $this->mOutput->mContainsOldMagic = true;
1486 } else {
1487 $text = $matches[0];
1489 return $text;
1492 function braceSubstitution( $matches ) {
1493 global $wgLinkCache, $wgLang;
1494 $fname = 'Parser::braceSubstitution';
1495 $found = false;
1496 $nowiki = false;
1497 $noparse = false;
1499 $title = NULL;
1501 # $newline is an optional newline character before the braces
1502 # $part1 is the bit before the first |, and must contain only title characters
1503 # $args is a list of arguments, starting from index 0, not including $part1
1505 $newline = $matches[1];
1506 $part1 = $matches[2];
1507 # If the third subpattern matched anything, it will start with |
1508 if ( $matches[3] !== '' ) {
1509 $args = explode( '|', substr( $matches[3], 1 ) );
1510 } else {
1511 $args = array();
1513 $argc = count( $args );
1515 # {{{}}}
1516 if ( strpos( $matches[0], '{{{' ) !== false ) {
1517 $text = $matches[0];
1518 $found = true;
1519 $noparse = true;
1522 # SUBST
1523 if ( !$found ) {
1524 $mwSubst =& MagicWord::get( MAG_SUBST );
1525 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1526 if ( $this->mOutputType != OT_WIKI ) {
1527 # Invalid SUBST not replaced at PST time
1528 # Return without further processing
1529 $text = $matches[0];
1530 $found = true;
1531 $noparse= true;
1533 } elseif ( $this->mOutputType == OT_WIKI ) {
1534 # SUBST not found in PST pass, do nothing
1535 $text = $matches[0];
1536 $found = true;
1540 # MSG, MSGNW and INT
1541 if ( !$found ) {
1542 # Check for MSGNW:
1543 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1544 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1545 $nowiki = true;
1546 } else {
1547 # Remove obsolete MSG:
1548 $mwMsg =& MagicWord::get( MAG_MSG );
1549 $mwMsg->matchStartAndRemove( $part1 );
1552 # Check if it is an internal message
1553 $mwInt =& MagicWord::get( MAG_INT );
1554 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1555 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1556 $text = wfMsgReal( $part1, $args, true );
1557 $found = true;
1562 # NS
1563 if ( !$found ) {
1564 # Check for NS: (namespace expansion)
1565 $mwNs = MagicWord::get( MAG_NS );
1566 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1567 if ( intval( $part1 ) ) {
1568 $text = $wgLang->getNsText( intval( $part1 ) );
1569 $found = true;
1570 } else {
1571 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1572 if ( !is_null( $index ) ) {
1573 $text = $wgLang->getNsText( $index );
1574 $found = true;
1580 # LOCALURL and LOCALURLE
1581 if ( !$found ) {
1582 $mwLocal = MagicWord::get( MAG_LOCALURL );
1583 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1585 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1586 $func = 'getLocalURL';
1587 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1588 $func = 'escapeLocalURL';
1589 } else {
1590 $func = '';
1593 if ( $func !== '' ) {
1594 $title = Title::newFromText( $part1 );
1595 if ( !is_null( $title ) ) {
1596 if ( $argc > 0 ) {
1597 $text = $title->$func( $args[0] );
1598 } else {
1599 $text = $title->$func();
1601 $found = true;
1606 # Internal variables
1607 if ( !$this->mVariables ) {
1608 $this->initialiseVariables();
1610 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1611 $text = $this->mVariables[$part1];
1612 $found = true;
1613 $this->mOutput->mContainsOldMagic = true;
1616 # Arguments input from the caller
1617 $inputArgs = end( $this->mArgStack );
1618 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1619 $text = $inputArgs[$part1];
1620 $found = true;
1623 # Load from database
1624 if ( !$found ) {
1625 $title = Title::newFromText( $part1, NS_TEMPLATE );
1626 if ( !is_null( $title ) && !$title->isExternal() ) {
1627 # Check for excessive inclusion
1628 $dbk = $title->getPrefixedDBkey();
1629 if ( $this->incrementIncludeCount( $dbk ) ) {
1630 $article = new Article( $title );
1631 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1632 if ( $articleContent !== false ) {
1633 $found = true;
1634 $text = $articleContent;
1639 # If the title is valid but undisplayable, make a link to it
1640 if ( $this->mOutputType == OT_HTML && !$found ) {
1641 $text = '[[' . $title->getPrefixedText() . ']]';
1642 $found = true;
1647 # Recursive parsing, escaping and link table handling
1648 # Only for HTML output
1649 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1650 $text = wfEscapeWikiText( $text );
1651 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1652 # Clean up argument array
1653 $assocArgs = array();
1654 $index = 1;
1655 foreach( $args as $arg ) {
1656 $eqpos = strpos( $arg, '=' );
1657 if ( $eqpos === false ) {
1658 $assocArgs[$index++] = $arg;
1659 } else {
1660 $name = trim( substr( $arg, 0, $eqpos ) );
1661 $value = trim( substr( $arg, $eqpos+1 ) );
1662 if ( $value === false ) {
1663 $value = '';
1665 if ( $name !== false ) {
1666 $assocArgs[$name] = $value;
1671 # Do not enter included links in link table
1672 if ( !is_null( $title ) ) {
1673 $wgLinkCache->suspend();
1676 # Run full parser on the included text
1677 $text = $this->stripParse( $text, $newline, $assocArgs );
1679 # Resume the link cache and register the inclusion as a link
1680 if ( !is_null( $title ) ) {
1681 $wgLinkCache->resume();
1682 $wgLinkCache->addLinkObj( $title );
1686 if ( !$found ) {
1687 return $matches[0];
1688 } else {
1689 return $text;
1693 # Triple brace replacement -- used for template arguments
1694 function argSubstitution( $matches ) {
1695 $newline = $matches[1];
1696 $arg = trim( $matches[2] );
1697 $text = $matches[0];
1698 $inputArgs = end( $this->mArgStack );
1700 if ( array_key_exists( $arg, $inputArgs ) ) {
1701 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1704 return $text;
1707 # Returns true if the function is allowed to include this entity
1708 function incrementIncludeCount( $dbk ) {
1709 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1710 $this->mIncludeCount[$dbk] = 0;
1712 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1713 return true;
1714 } else {
1715 return false;
1720 # Cleans up HTML, removes dangerous tags and attributes
1721 /* private */ function removeHTMLtags( $text ) {
1722 global $wgUseTidy, $wgUserHtml;
1723 $fname = 'Parser::removeHTMLtags';
1724 wfProfileIn( $fname );
1726 if( $wgUserHtml ) {
1727 $htmlpairs = array( # Tags that must be closed
1728 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1729 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1730 'strike', 'strong', 'tt', 'var', 'div', 'center',
1731 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1732 'ruby', 'rt' , 'rb' , 'rp', 'p'
1734 $htmlsingle = array(
1735 'br', 'hr', 'li', 'dt', 'dd'
1737 $htmlnest = array( # Tags that can be nested--??
1738 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1739 'dl', 'font', 'big', 'small', 'sub', 'sup'
1741 $tabletags = array( # Can only appear inside table
1742 'td', 'th', 'tr'
1744 } else {
1745 $htmlpairs = array();
1746 $htmlsingle = array();
1747 $htmlnest = array();
1748 $tabletags = array();
1751 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1752 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1754 $htmlattrs = $this->getHTMLattrs () ;
1756 # Remove HTML comments
1757 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1759 $bits = explode( '<', $text );
1760 $text = array_shift( $bits );
1761 if(!$wgUseTidy) {
1762 $tagstack = array(); $tablestack = array();
1763 foreach ( $bits as $x ) {
1764 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1765 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1766 $x, $regs );
1767 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1768 error_reporting( $prev );
1770 $badtag = 0 ;
1771 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1772 # Check our stack
1773 if ( $slash ) {
1774 # Closing a tag...
1775 if ( ! in_array( $t, $htmlsingle ) &&
1776 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1777 @array_push( $tagstack, $ot );
1778 $badtag = 1;
1779 } else {
1780 if ( $t == 'table' ) {
1781 $tagstack = array_pop( $tablestack );
1783 $newparams = '';
1785 } else {
1786 # Keep track for later
1787 if ( in_array( $t, $tabletags ) &&
1788 ! in_array( 'table', $tagstack ) ) {
1789 $badtag = 1;
1790 } else if ( in_array( $t, $tagstack ) &&
1791 ! in_array ( $t , $htmlnest ) ) {
1792 $badtag = 1 ;
1793 } else if ( ! in_array( $t, $htmlsingle ) ) {
1794 if ( $t == 'table' ) {
1795 array_push( $tablestack, $tagstack );
1796 $tagstack = array();
1798 array_push( $tagstack, $t );
1800 # Strip non-approved attributes from the tag
1801 $newparams = $this->fixTagAttributes($params);
1804 if ( ! $badtag ) {
1805 $rest = str_replace( '>', '&gt;', $rest );
1806 $text .= "<$slash$t $newparams$brace$rest";
1807 continue;
1810 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1812 # Close off any remaining tags
1813 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1814 $text .= "</$t>\n";
1815 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1817 } else {
1818 # this might be possible using tidy itself
1819 foreach ( $bits as $x ) {
1820 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1821 $x, $regs );
1822 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1823 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1824 $newparams = $this->fixTagAttributes($params);
1825 $rest = str_replace( '>', '&gt;', $rest );
1826 $text .= "<$slash$t $newparams$brace$rest";
1827 } else {
1828 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1832 wfProfileOut( $fname );
1833 return $text;
1839 * This function accomplishes several tasks:
1840 * 1) Auto-number headings if that option is enabled
1841 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1842 * 3) Add a Table of contents on the top for users who have enabled the option
1843 * 4) Auto-anchor headings
1845 * It loops through all headlines, collects the necessary data, then splits up the
1846 * string and re-inserts the newly formatted headlines.
1850 /* private */ function formatHeadings( $text, $isMain=true ) {
1851 global $wgInputEncoding, $wgMaxTocLevel;
1853 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1854 $doShowToc = $this->mOptions->getShowToc();
1855 $forceTocHere = false;
1856 if( !$this->mTitle->userCanEdit() ) {
1857 $showEditLink = 0;
1858 $rightClickHack = 0;
1859 } else {
1860 $showEditLink = $this->mOptions->getEditSection();
1861 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1864 # Inhibit editsection links if requested in the page
1865 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1866 if( $esw->matchAndRemove( $text ) ) {
1867 $showEditLink = 0;
1869 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1870 # do not add TOC
1871 $mw =& MagicWord::get( MAG_NOTOC );
1872 if( $mw->matchAndRemove( $text ) ) {
1873 $doShowToc = 0;
1876 # never add the TOC to the Main Page. This is an entry page that should not
1877 # be more than 1-2 screens large anyway
1878 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
1879 $doShowToc = 0;
1882 # Get all headlines for numbering them and adding funky stuff like [edit]
1883 # links - this is for later, but we need the number of headlines right now
1884 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1886 # if there are fewer than 4 headlines in the article, do not show TOC
1887 if( $numMatches < 4 ) {
1888 $doShowToc = 0;
1891 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1892 # override above conditions and always show TOC at that place
1893 $mw =& MagicWord::get( MAG_TOC );
1894 if ($mw->match( $text ) ) {
1895 $doShowToc = 1;
1896 $forceTocHere = true;
1897 } else {
1898 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1899 # override above conditions and always show TOC above first header
1900 $mw =& MagicWord::get( MAG_FORCETOC );
1901 if ($mw->matchAndRemove( $text ) ) {
1902 $doShowToc = 1;
1908 # We need this to perform operations on the HTML
1909 $sk =& $this->mOptions->getSkin();
1911 # headline counter
1912 $headlineCount = 0;
1914 # Ugh .. the TOC should have neat indentation levels which can be
1915 # passed to the skin functions. These are determined here
1916 $toclevel = 0;
1917 $toc = '';
1918 $full = '';
1919 $head = array();
1920 $sublevelCount = array();
1921 $level = 0;
1922 $prevlevel = 0;
1923 foreach( $matches[3] as $headline ) {
1924 $numbering = '';
1925 if( $level ) {
1926 $prevlevel = $level;
1928 $level = $matches[1][$headlineCount];
1929 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1930 # reset when we enter a new level
1931 $sublevelCount[$level] = 0;
1932 $toc .= $sk->tocIndent( $level - $prevlevel );
1933 $toclevel += $level - $prevlevel;
1935 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1936 # reset when we step back a level
1937 $sublevelCount[$level+1]=0;
1938 $toc .= $sk->tocUnindent( $prevlevel - $level );
1939 $toclevel -= $prevlevel - $level;
1941 # count number of headlines for each level
1942 @$sublevelCount[$level]++;
1943 if( $doNumberHeadings || $doShowToc ) {
1944 $dot = 0;
1945 for( $i = 1; $i <= $level; $i++ ) {
1946 if( !empty( $sublevelCount[$i] ) ) {
1947 if( $dot ) {
1948 $numbering .= '.';
1950 $numbering .= $sublevelCount[$i];
1951 $dot = 1;
1956 # The canonized header is a version of the header text safe to use for links
1957 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1958 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
1959 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
1961 # strip out HTML
1962 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1963 $tocline = trim( $canonized_headline );
1964 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
1965 $replacearray = array(
1966 '%3A' => ':',
1967 '%' => '.'
1969 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1970 $refer[$headlineCount] = $canonized_headline;
1972 # count how many in assoc. array so we can track dupes in anchors
1973 @$refers[$canonized_headline]++;
1974 $refcount[$headlineCount]=$refers[$canonized_headline];
1976 # Prepend the number to the heading text
1978 if( $doNumberHeadings || $doShowToc ) {
1979 $tocline = $numbering . ' ' . $tocline;
1981 # Don't number the heading if it is the only one (looks silly)
1982 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1983 # the two are different if the line contains a link
1984 $headline=$numbering . ' ' . $headline;
1988 # Create the anchor for linking from the TOC to the section
1989 $anchor = $canonized_headline;
1990 if($refcount[$headlineCount] > 1 ) {
1991 $anchor .= '_' . $refcount[$headlineCount];
1993 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
1994 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1996 if( $showEditLink ) {
1997 if ( empty( $head[$headlineCount] ) ) {
1998 $head[$headlineCount] = '';
2000 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2003 # Add the edit section span
2004 if( $rightClickHack ) {
2005 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2008 # give headline the correct <h#> tag
2009 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2011 $headlineCount++;
2014 if( $doShowToc ) {
2015 $toclines = $headlineCount;
2016 $toc .= $sk->tocUnindent( $toclevel );
2017 $toc = $sk->tocTable( $toc );
2020 # split up and insert constructed headlines
2022 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2023 $i = 0;
2025 foreach( $blocks as $block ) {
2026 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2027 # This is the [edit] link that appears for the top block of text when
2028 # section editing is enabled
2030 # Disabled because it broke block formatting
2031 # For example, a bullet point in the top line
2032 # $full .= $sk->editSectionLink(0);
2034 $full .= $block;
2035 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2036 # Top anchor now in skin
2037 $full = $full.$toc;
2040 if( !empty( $head[$i] ) ) {
2041 $full .= $head[$i];
2043 $i++;
2045 if($forceTocHere) {
2046 $mw =& MagicWord::get( MAG_TOC );
2047 return $mw->replace( $toc, $full );
2048 } else {
2049 return $full;
2053 # Return an HTML link for the "ISBN 123456" text
2054 /* private */ function magicISBN( $text ) {
2055 global $wgLang;
2056 $fname = 'Parser::magicISBN';
2057 wfProfileIn( $fname );
2059 $a = split( 'ISBN ', " $text" );
2060 if ( count ( $a ) < 2 ) {
2061 wfProfileOut( $fname );
2062 return $text;
2064 $text = substr( array_shift( $a ), 1);
2065 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2067 foreach ( $a as $x ) {
2068 $isbn = $blank = '' ;
2069 while ( ' ' == $x{0} ) {
2070 $blank .= ' ';
2071 $x = substr( $x, 1 );
2073 while ( strstr( $valid, $x{0} ) != false ) {
2074 $isbn .= $x{0};
2075 $x = substr( $x, 1 );
2077 $num = str_replace( '-', '', $isbn );
2078 $num = str_replace( ' ', '', $num );
2080 if ( '' == $num ) {
2081 $text .= "ISBN $blank$x";
2082 } else {
2083 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2084 $text .= '<a href="' .
2085 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2086 "\" class=\"internal\">ISBN $isbn</a>";
2087 $text .= $x;
2090 wfProfileOut( $fname );
2091 return $text;
2094 # Return an HTML link for the "GEO ..." text
2095 /* private */ function magicGEO( $text ) {
2096 global $wgLang, $wgUseGeoMode;
2097 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2098 $fname = 'Parser::magicGEO';
2099 wfProfileIn( $fname );
2101 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2102 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2103 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2104 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2105 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2106 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2108 $a = split( 'GEO ', " $text" );
2109 if ( count ( $a ) < 2 ) {
2110 wfProfileOut( $fname );
2111 return $text;
2113 $text = substr( array_shift( $a ), 1);
2114 $valid = '0123456789.+-:';
2116 foreach ( $a as $x ) {
2117 $geo = $blank = '' ;
2118 while ( ' ' == $x{0} ) {
2119 $blank .= ' ';
2120 $x = substr( $x, 1 );
2122 while ( strstr( $valid, $x{0} ) != false ) {
2123 $geo .= $x{0};
2124 $x = substr( $x, 1 );
2126 $num = str_replace( '+', '', $geo );
2127 $num = str_replace( ' ', '', $num );
2129 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2130 $text .= "GEO $blank$x";
2131 } else {
2132 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2133 $text .= '<a href="' .
2134 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2135 "\" class=\"internal\">GEO $geo</a>";
2136 $text .= $x;
2139 wfProfileOut( $fname );
2140 return $text;
2143 # Return an HTML link for the "RFC 1234" text
2144 /* private */ function magicRFC( $text ) {
2145 global $wgLang;
2147 $a = split( 'RFC ', ' '.$text );
2148 if ( count ( $a ) < 2 ) return $text;
2149 $text = substr( array_shift( $a ), 1);
2150 $valid = '0123456789';
2152 foreach ( $a as $x ) {
2153 $rfc = $blank = '' ;
2154 while ( ' ' == $x{0} ) {
2155 $blank .= ' ';
2156 $x = substr( $x, 1 );
2158 while ( strstr( $valid, $x{0} ) != false ) {
2159 $rfc .= $x{0};
2160 $x = substr( $x, 1 );
2163 if ( '' == $rfc ) {
2164 $text .= "RFC $blank$x";
2165 } else {
2166 $url = wfmsg( 'rfcurl' );
2167 $url = str_replace( '$1', $rfc, $url);
2168 $sk =& $this->mOptions->getSkin();
2169 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2170 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2173 return $text;
2176 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2177 $this->mOptions = $options;
2178 $this->mTitle =& $title;
2179 $this->mOutputType = OT_WIKI;
2181 if ( $clearState ) {
2182 $this->clearState();
2185 $stripState = false;
2186 $pairs = array(
2187 "\r\n" => "\n",
2189 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2190 // now with regexes
2192 $pairs = array(
2193 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2194 "/<br *?>/i" => "<br />",
2196 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2198 $text = $this->strip( $text, $stripState, false );
2199 $text = $this->pstPass2( $text, $user );
2200 $text = $this->unstrip( $text, $stripState );
2201 $text = $this->unstripNoWiki( $text, $stripState );
2202 return $text;
2205 /* private */ function pstPass2( $text, &$user ) {
2206 global $wgLang, $wgLocaltimezone, $wgCurParser;
2208 # Variable replacement
2209 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2210 $text = $this->replaceVariables( $text );
2212 # Signatures
2214 $n = $user->getName();
2215 $k = $user->getOption( 'nickname' );
2216 if ( '' == $k ) { $k = $n; }
2217 if(isset($wgLocaltimezone)) {
2218 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2220 /* Note: this is an ugly timezone hack for the European wikis */
2221 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2222 ' (' . date( 'T' ) . ')';
2223 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2225 $text = preg_replace( '/~~~~~/', $d, $text );
2226 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2227 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2228 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2229 Namespace::getUser() ) . ":$n|$k]]", $text );
2231 # Context links: [[|name]] and [[name (context)|]]
2233 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2234 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2235 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2236 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2238 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2239 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2240 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2241 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2242 # [[ns:page (cont)|]]
2243 $context = "";
2244 $t = $this->mTitle->getText();
2245 if ( preg_match( $conpat, $t, $m ) ) {
2246 $context = $m[2];
2248 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2249 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2250 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2252 if ( '' == $context ) {
2253 $text = preg_replace( $p2, '[[\\1]]', $text );
2254 } else {
2255 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2259 $mw =& MagicWord::get( MAG_SUBST );
2260 $wgCurParser = $this->fork();
2261 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2262 $this->merge( $wgCurParser );
2265 # Trim trailing whitespace
2266 # MAG_END (__END__) tag allows for trailing
2267 # whitespace to be deliberately included
2268 $text = rtrim( $text );
2269 $mw =& MagicWord::get( MAG_END );
2270 $mw->matchAndRemove( $text );
2272 return $text;
2275 # Set up some variables which are usually set up in parse()
2276 # so that an external function can call some class members with confidence
2277 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2278 $this->mTitle =& $title;
2279 $this->mOptions = $options;
2280 $this->mOutputType = $outputType;
2281 if ( $clearState ) {
2282 $this->clearState();
2286 function transformMsg( $text, $options ) {
2287 global $wgTitle;
2288 static $executing = false;
2290 # Guard against infinite recursion
2291 if ( $executing ) {
2292 return $text;
2294 $executing = true;
2296 $this->mTitle = $wgTitle;
2297 $this->mOptions = $options;
2298 $this->mOutputType = OT_MSG;
2299 $this->clearState();
2300 $text = $this->replaceVariables( $text );
2302 $executing = false;
2303 return $text;
2306 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2307 # Callback will be called with the text within
2308 # Transform and return the text within
2309 function setHook( $tag, $callback ) {
2310 $oldVal = @$this->mTagHooks[$tag];
2311 $this->mTagHooks[$tag] = $callback;
2312 return $oldVal;
2316 class ParserOutput
2318 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2319 var $mCacheTime; # Used in ParserCache
2321 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2322 $containsOldMagic = false )
2324 $this->mText = $text;
2325 $this->mLanguageLinks = $languageLinks;
2326 $this->mCategoryLinks = $categoryLinks;
2327 $this->mContainsOldMagic = $containsOldMagic;
2328 $this->mCacheTime = "";
2331 function getText() { return $this->mText; }
2332 function getLanguageLinks() { return $this->mLanguageLinks; }
2333 function getCategoryLinks() { return $this->mCategoryLinks; }
2334 function getCacheTime() { return $this->mCacheTime; }
2335 function containsOldMagic() { return $this->mContainsOldMagic; }
2336 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2337 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2338 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2339 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2340 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2342 function merge( $other ) {
2343 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2344 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2345 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2350 class ParserOptions
2352 # All variables are private
2353 var $mUseTeX; # Use texvc to expand <math> tags
2354 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2355 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2356 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2357 var $mAllowExternalImages; # Allow external images inline
2358 var $mSkin; # Reference to the preferred skin
2359 var $mDateFormat; # Date format index
2360 var $mEditSection; # Create "edit section" links
2361 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2362 var $mNumberHeadings; # Automatically number headings
2363 var $mShowToc; # Show table of contents
2365 function getUseTeX() { return $this->mUseTeX; }
2366 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2367 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2368 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2369 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2370 function getSkin() { return $this->mSkin; }
2371 function getDateFormat() { return $this->mDateFormat; }
2372 function getEditSection() { return $this->mEditSection; }
2373 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2374 function getNumberHeadings() { return $this->mNumberHeadings; }
2375 function getShowToc() { return $this->mShowToc; }
2377 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2378 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2379 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2380 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2381 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2382 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2383 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2384 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2385 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2386 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2388 function setSkin( &$x ) { $this->mSkin =& $x; }
2390 /* static */ function newFromUser( &$user ) {
2391 $popts = new ParserOptions;
2392 $popts->initialiseFromUser( $user );
2393 return $popts;
2396 function initialiseFromUser( &$userInput ) {
2397 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2399 if ( !$userInput ) {
2400 $user = new User;
2401 $user->setLoaded( true );
2402 } else {
2403 $user =& $userInput;
2406 $this->mUseTeX = $wgUseTeX;
2407 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2408 $this->mUseDynamicDates = $wgUseDynamicDates;
2409 $this->mInterwikiMagic = $wgInterwikiMagic;
2410 $this->mAllowExternalImages = $wgAllowExternalImages;
2411 $this->mSkin =& $user->getSkin();
2412 $this->mDateFormat = $user->getOption( 'date' );
2413 $this->mEditSection = $user->getOption( 'editsection' );
2414 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2415 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2416 $this->mShowToc = $user->getOption( 'showtoc' );
2422 # Regex callbacks, used in Parser::replaceVariables
2423 function wfBraceSubstitution( $matches )
2425 global $wgCurParser;
2426 return $wgCurParser->braceSubstitution( $matches );
2429 function wfArgSubstitution( $matches )
2431 global $wgCurParser;
2432 return $wgCurParser->argSubstitution( $matches );
2435 function wfVariableSubstitution( $matches )
2437 global $wgCurParser;
2438 return $wgCurParser->variableSubstitution( $matches );