Conducting a conversation by means of comments
[mediawiki.git] / includes / Parser.php
blob8a24bf178a891fee9870cd0114391552d59b7d22
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 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
348 $cns = Namespace::getCategory() ;
349 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
351 $r = "<br style=\"clear:both;\"/>\n";
354 $sk =& $wgUser->getSkin() ;
356 $articles = array() ;
357 $children = array() ;
358 $data = array () ;
359 $id = $this->mTitle->getArticleID() ;
361 # FIXME: add limits
362 $t = wfStrencode( $this->mTitle->getDBKey() );
363 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
364 $res = wfQuery ( $sql, DB_READ ) ;
365 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
367 # For all pages that link to this category
368 foreach ( $data AS $x )
370 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
371 if ( $t != "" ) $t .= ":" ;
372 $t .= $x->cur_title ;
374 if ( $x->cur_namespace == $cns ) {
375 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
376 } else {
377 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
380 wfFreeResult ( $res ) ;
382 # Showing subcategories
383 if ( count ( $children ) > 0 ) {
384 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
385 $r .= implode ( ', ' , $children ) ;
388 # Showing pages in this category
389 if ( count ( $articles ) > 0 ) {
390 $ti = $this->mTitle->getText() ;
391 $h = wfMsg( 'category_header', $ti );
392 $r .= "<h2>{$h}</h2>\n" ;
393 $r .= implode ( ', ' , $articles ) ;
397 return $r ;
402 function newCategoryMagic () {
403 global $wgLang , $wgUser ;
404 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
406 $cns = Namespace::getCategory() ;
407 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
409 $r = "<br style=\"clear:both;\"/>\n";
412 $sk =& $wgUser->getSkin() ;
414 $articles = array() ;
415 $articles_start_char = array();
416 $children = array() ;
417 $children_start_char = array();
418 $data = array () ;
419 $id = $this->mTitle->getArticleID() ;
421 # FIXME: add limits
422 $t = wfStrencode( $this->mTitle->getDBKey() );
423 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM
424 cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY
425 cl_sortkey" ;
426 $res = wfQuery ( $sql, DB_READ ) ;
427 while ( $x = wfFetchObject ( $res ) )
429 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
430 if ( $t != '' ) $t .= ':' ;
431 $t .= $x->cur_title ;
433 if ( $x->cur_namespace == $cns ) {
434 $ctitle = str_replace( '_',' ',$x->cur_title );
435 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
437 // If there's a link from Category:A to Category:B, the sortkey of the resulting
438 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
439 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
440 // else use sortkey...
441 if ( ($ns.":".$ctitle) == $x->cl_sortkey ) {
442 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) );
443 } else {
444 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
446 } else {
447 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
448 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
451 wfFreeResult ( $res ) ;
453 $ti = $this->mTitle->getText() ;
455 # Don't show subcategories section if there are none.
456 if ( count ( $children ) > 0 )
458 # Showing subcategories
459 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
460 . wfMsg( 'subcategorycount', count( $children ) );
461 if ( count ( $children ) > 6 ) {
463 // divide list into three equal chunks
464 $chunk = (int) (count ( $children ) / 3);
466 // get and display header
467 $r .= '<table width="100%"><tr valign="top">';
469 $startChunk = 0;
470 $endChunk = $chunk;
472 // loop through the chunks
473 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
474 $chunkIndex < 3;
475 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
478 $r .= '<td><ul>';
479 // output all subcategories to category
480 for ($index = $startChunk ;
481 $index < $endChunk && $index < count($children);
482 $index++ )
484 // check for change of starting letter or begging of chunk
485 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
486 || ($index == $startChunk) )
488 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
491 $r .= "<li>{$children[$index]}</li>";
493 $r .= '</ul></td>';
497 $r .= '</tr></table>';
498 } else {
499 // for short lists of subcategories to category.
501 $r .= "<h3>{$children_start_char[0]}</h3>\n";
502 $r .= '<ul><li>'.$children[0].'</li>';
503 for ($index = 1; $index < count($children); $index++ )
505 if ($children_start_char[$index] != $children_start_char[$index - 1])
507 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
510 $r .= "<li>{$children[$index]}</li>";
512 $r .= '</ul>';
514 } # END of if ( count($children) > 0 )
516 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
517 wfMsg( 'categoryarticlecount', count( $articles ) );
519 # Showing articles in this category
520 if ( count ( $articles ) > 6) {
521 $ti = $this->mTitle->getText() ;
523 // divide list into three equal chunks
524 $chunk = (int) (count ( $articles ) / 3);
526 // get and display header
527 $r .= '<table width="100%"><tr valign="top">';
529 // loop through the chunks
530 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
531 $chunkIndex < 3;
532 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
535 $r .= '<td><ul>';
537 // output all articles in category
538 for ($index = $startChunk ;
539 $index < $endChunk && $index < count($articles);
540 $index++ )
542 // check for change of starting letter or begging of chunk
543 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
544 || ($index == $startChunk) )
546 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
549 $r .= "<li>{$articles[$index]}</li>";
551 $r .= '</ul></td>';
555 $r .= '</tr></table>';
556 } elseif ( count ( $articles ) > 0) {
557 // for short lists of articles in categories.
558 $ti = $this->mTitle->getText() ;
560 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
561 $r .= '<ul><li>'.$articles[0].'</li>';
562 for ($index = 1; $index < count($articles); $index++ )
564 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
566 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
569 $r .= "<li>{$articles[$index]}</li>";
571 $r .= '</ul>';
575 return $r ;
578 # Return allowed HTML attributes
579 function getHTMLattrs () {
580 $htmlattrs = array( # Allowed attributes--no scripting, etc.
581 'title', 'align', 'lang', 'dir', 'width', 'height',
582 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
583 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
584 /* FONT */ 'type', 'start', 'value', 'compact',
585 /* For various lists, mostly deprecated but safe */
586 'summary', 'width', 'border', 'frame', 'rules',
587 'cellspacing', 'cellpadding', 'valign', 'char',
588 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
589 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
590 'id', 'class', 'name', 'style' /* For CSS */
592 return $htmlattrs ;
595 # Remove non approved attributes and javascript in css
596 function fixTagAttributes ( $t ) {
597 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
598 $htmlattrs = $this->getHTMLattrs() ;
600 # Strip non-approved attributes from the tag
601 $t = preg_replace(
602 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
603 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
604 $t);
605 # Strip javascript "expression" from stylesheets. Brute force approach:
606 # If anythin offensive is found, all attributes of the HTML tag are dropped
608 if( preg_match(
609 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
610 wfMungeToUtf8( $t ) ) )
612 $t='';
615 return trim ( $t ) ;
618 # interface with html tidy, used if $wgUseTidy = true
619 function tidy ( $text ) {
620 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
621 global $wgInputEncoding, $wgOutputEncoding;
622 $fname = 'Parser::tidy';
623 wfProfileIn( $fname );
625 $cleansource = '';
626 switch(strtoupper($wgOutputEncoding)) {
627 case 'ISO-8859-1':
628 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
629 break;
630 case 'UTF-8':
631 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
632 break;
633 default:
634 $wgTidyOpts .= ' -raw';
637 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
638 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
639 '<head><title>test</title></head><body>'.$text.'</body></html>';
640 $descriptorspec = array(
641 0 => array('pipe', 'r'),
642 1 => array('pipe', 'w'),
643 2 => array('file', '/dev/null', 'a')
645 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
646 if (is_resource($process)) {
647 fwrite($pipes[0], $wrappedtext);
648 fclose($pipes[0]);
649 while (!feof($pipes[1])) {
650 $cleansource .= fgets($pipes[1], 1024);
652 fclose($pipes[1]);
653 $return_value = proc_close($process);
656 wfProfileOut( $fname );
658 if( $cleansource == '' && $text != '') {
659 wfDebug( "Tidy error detected!\n" );
660 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
661 } else {
662 return $cleansource;
666 # parse the wiki syntax used to render tables
667 function doTableStuff ( $t ) {
668 $t = explode ( "\n" , $t ) ;
669 $td = array () ; # Is currently a td tag open?
670 $ltd = array () ; # Was it TD or TH?
671 $tr = array () ; # Is currently a tr tag open?
672 $ltr = array () ; # tr attributes
673 foreach ( $t AS $k => $x )
675 $x = trim ( $x ) ;
676 $fc = substr ( $x , 0 , 1 ) ;
677 if ( '{|' == substr ( $x , 0 , 2 ) )
679 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . '>' ;
680 array_push ( $td , false ) ;
681 array_push ( $ltd , '' ) ;
682 array_push ( $tr , false ) ;
683 array_push ( $ltr , '' ) ;
685 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
686 else if ( '|}' == substr ( $x , 0 , 2 ) )
688 $z = "</table>\n" ;
689 $l = array_pop ( $ltd ) ;
690 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
691 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
692 array_pop ( $ltr ) ;
693 $t[$k] = $z ;
695 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
697 $z = trim ( substr ( $x , 2 ) ) ;
698 $t[$k] = "<caption>{$z}</caption>\n" ;
700 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
702 $x = substr ( $x , 1 ) ;
703 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
704 $z = '' ;
705 $l = array_pop ( $ltd ) ;
706 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
707 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
708 array_pop ( $ltr ) ;
709 $t[$k] = $z ;
710 array_push ( $tr , false ) ;
711 array_push ( $td , false ) ;
712 array_push ( $ltd , '' ) ;
713 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
715 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
717 if ( '|+' == substr ( $x , 0 , 2 ) )
719 $fc = '+' ;
720 $x = substr ( $x , 1 ) ;
722 $after = substr ( $x , 1 ) ;
723 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
724 $after = explode ( '||' , $after ) ;
725 $t[$k] = '' ;
726 foreach ( $after AS $theline )
728 $z = '' ;
729 if ( $fc != '+' )
731 $tra = array_pop ( $ltr ) ;
732 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
733 array_push ( $tr , true ) ;
734 array_push ( $ltr , '' ) ;
737 $l = array_pop ( $ltd ) ;
738 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
739 if ( $fc == '|' ) $l = 'td' ;
740 else if ( $fc == '!' ) $l = 'th' ;
741 else if ( $fc == '+' ) $l = 'caption' ;
742 else $l = '' ;
743 array_push ( $ltd , $l ) ;
744 $y = explode ( '|' , $theline , 2 ) ;
745 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
746 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
747 $t[$k] .= $y ;
748 array_push ( $td , true ) ;
753 # Closing open td, tr && table
754 while ( count ( $td ) > 0 )
756 if ( array_pop ( $td ) ) $t[] = '</td>' ;
757 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
758 $t[] = '</table>' ;
761 $t = implode ( "\n" , $t ) ;
762 # $t = $this->removeHTMLtags( $t );
763 return $t ;
766 # Parses the text and adds the result to the strip state
767 # Returns the strip tag
768 function stripParse( $text, $newline, $args )
770 $text = $this->strip( $text, $this->mStripState );
771 $text = $this->internalParse( $text, (bool)$newline, $args, false );
772 return $newline.$this->insertStripItem( $text, $this->mStripState );
775 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
776 $fname = 'Parser::internalParse';
777 wfProfileIn( $fname );
779 $text = $this->removeHTMLtags( $text );
780 $text = $this->replaceVariables( $text, $args );
782 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
784 $text = $this->doHeadings( $text );
785 if($this->mOptions->getUseDynamicDates()) {
786 global $wgDateFormatter;
787 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
789 $text = $this->doAllQuotes( $text );
790 // $text = $this->doExponent( $text );
791 $text = $this->replaceExternalLinks( $text );
792 $text = $this->replaceInternalLinks ( $text );
793 $text = $this->replaceInternalLinks ( $text );
794 //$text = $this->doTokenizedParser ( $text );
795 $text = $this->doTableStuff ( $text ) ;
796 $text = $this->magicISBN( $text );
797 $text = $this->magicRFC( $text );
798 $text = $this->formatHeadings( $text, $isMain );
799 $sk =& $this->mOptions->getSkin();
800 $text = $sk->transformContent( $text );
802 if ( !isset ( $this->categoryMagicDone ) ) {
803 $text .= $this->categoryMagic () ;
804 $this->categoryMagicDone = true ;
807 wfProfileOut( $fname );
808 return $text;
811 # Parse ^^ tokens and return html
812 /* private */ function doExponent ( $text )
814 $fname = 'Parser::doExponent';
815 wfProfileIn( $fname);
816 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
817 wfProfileOut( $fname);
818 return $text;
821 # Parse headers and return html
822 /* private */ function doHeadings( $text ) {
823 $fname = 'Parser::doHeadings';
824 wfProfileIn( $fname );
825 for ( $i = 6; $i >= 1; --$i ) {
826 $h = substr( '======', 0, $i );
827 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
828 "<h{$i}>\\1</h{$i}>\\2", $text );
830 wfProfileOut( $fname );
831 return $text;
834 /* private */ function doAllQuotes( $text ) {
835 $fname = 'Parser::doAllQuotes';
836 wfProfileIn( $fname );
837 $outtext = '';
838 $lines = explode( "\n", $text );
839 foreach ( $lines as $line ) {
840 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
842 $outtext = substr($outtext, 0,-1);
843 wfProfileOut( $fname );
844 return $outtext;
847 /* private */ function doQuotes( $pre, $text, $mode ) {
848 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
849 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
850 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
851 if ( substr ($m[2], 0, 1) == '\'' ) {
852 $m[2] = substr ($m[2], 1);
853 if ($mode == 'em') {
854 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'emstrong' );
855 } else if ($mode == 'strong') {
856 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
857 } else if (($mode == 'emstrong') || ($mode == 'both')) {
858 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
859 } else if ($mode == 'strongem') {
860 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
861 } else {
862 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
864 } else {
865 if ($mode == 'strong') {
866 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'strongem' );
867 } else if ($mode == 'em') {
868 return $m1_em . $this->doQuotes ( '', $m[2], '' );
869 } else if ($mode == 'emstrong') {
870 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
871 } else if (($mode == 'strongem') || ($mode == 'both')) {
872 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
873 } else {
874 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
877 } else {
878 $text_strong = ($text == '') ? '' : "<strong>{$text}</strong>";
879 $text_em = ($text == '') ? '' : "<em>{$text}</em>";
880 if ($mode == '') {
881 return $pre . $text;
882 } else if ($mode == 'em') {
883 return $pre . $text_em;
884 } else if ($mode == 'strong') {
885 return $pre . $text_strong;
886 } else if ($mode == 'strongem') {
887 return (($pre == '') && ($text == '')) ? '' : "<strong>{$pre}{$text_em}</strong>";
888 } else {
889 return (($pre == '') && ($text == '')) ? '' : "<em>{$pre}{$text_strong}</em>";
894 # Note: we have to do external links before the internal ones,
895 # and otherwise take great care in the order of things here, so
896 # that we don't end up interpreting some URLs twice.
898 /* private */ function replaceExternalLinks( $text ) {
899 $fname = 'Parser::replaceExternalLinks';
900 wfProfileIn( $fname );
901 $text = $this->subReplaceExternalLinks( $text, 'http', true );
902 $text = $this->subReplaceExternalLinks( $text, 'https', true );
903 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
904 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
905 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
906 $text = $this->subReplaceExternalLinks( $text, 'news', false );
907 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
908 wfProfileOut( $fname );
909 return $text;
912 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
913 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
914 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
916 # this is the list of separators that should be ignored if they
917 # are the last character of an URL but that should be included
918 # if they occur within the URL, e.g. "go to www.foo.com, where .."
919 # in this case, the last comma should not become part of the URL,
920 # but in "www.foo.com/123,2342,32.htm" it should.
921 $sep = ",;\.:";
922 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
923 $images = 'gif|png|jpg|jpeg';
925 # PLEASE NOTE: The curly braces { } are not part of the regex,
926 # they are interpreted as part of the string (used to tell PHP
927 # that the content of the string should be inserted there).
928 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
929 "((?i){$images})([^{$uc}]|$)/";
931 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
932 $sk =& $this->mOptions->getSkin();
934 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
935 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
936 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
938 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
939 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
940 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
941 '</a>\\5', $s );
942 $s = str_replace( $unique, $protocol, $s );
944 $a = explode( "[{$protocol}:", " " . $s );
945 $s = array_shift( $a );
946 $s = substr( $s, 1 );
948 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
949 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
951 foreach ( $a as $line ) {
952 if ( preg_match( $e1, $line, $m ) ) {
953 $link = "{$protocol}:{$m[1]}";
954 $trail = $m[2];
955 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
956 else { $text = wfEscapeHTML( $link ); }
957 } else if ( preg_match( $e2, $line, $m ) ) {
958 $link = "{$protocol}:{$m[1]}";
959 $text = $m[2];
960 $trail = $m[3];
961 } else {
962 $s .= "[{$protocol}:" . $line;
963 continue;
965 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
966 $paren = '';
967 } else {
968 # Expand the URL for printable version
969 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
971 $la = $sk->getExternalLinkAttributes( $link, $text );
972 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
975 return $s;
979 /* private */ function replaceInternalLinks( $s ) {
980 global $wgLang, $wgLinkCache;
981 global $wgNamespacesWithSubpages, $wgLanguageCode;
982 static $fname = 'Parser::replaceInternalLinks' ;
983 wfProfileIn( $fname );
985 wfProfileIn( $fname.'-setup' );
986 static $tc = FALSE;
987 # the % is needed to support urlencoded titles as well
988 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
989 $sk =& $this->mOptions->getSkin();
991 $a = explode( '[[', ' ' . $s );
992 $s = array_shift( $a );
993 $s = substr( $s, 1 );
995 # Match a link having the form [[namespace:link|alternate]]trail
996 static $e1 = FALSE;
997 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
998 # Match the end of a line for a word that's not followed by whitespace,
999 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1000 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1002 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1003 # Special and Media are pseudo-namespaces; no pages actually exist in them
1004 static $image = FALSE;
1005 static $special = FALSE;
1006 static $media = FALSE;
1007 static $category = FALSE;
1008 if ( !$image ) { $image = Namespace::getImage(); }
1009 if ( !$special ) { $special = Namespace::getSpecial(); }
1010 if ( !$media ) { $media = Namespace::getMedia(); }
1011 if ( !$category ) { $category = Namespace::getCategory(); }
1013 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1015 if ( $useLinkPrefixExtension ) {
1016 if ( preg_match( $e2, $s, $m ) ) {
1017 $first_prefix = $m[2];
1018 $s = $m[1];
1019 } else {
1020 $first_prefix = false;
1022 } else {
1023 $prefix = '';
1026 wfProfileOut( $fname.'-setup' );
1028 foreach ( $a as $line ) {
1029 wfProfileIn( $fname.'-prefixhandling' );
1030 if ( $useLinkPrefixExtension ) {
1031 if ( preg_match( $e2, $s, $m ) ) {
1032 $prefix = $m[2];
1033 $s = $m[1];
1034 } else {
1035 $prefix='';
1037 # first link
1038 if($first_prefix) {
1039 $prefix = $first_prefix;
1040 $first_prefix = false;
1043 wfProfileOut( $fname.'-prefixhandling' );
1045 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1046 $text = $m[2];
1047 # fix up urlencoded title texts
1048 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1049 $trail = $m[3];
1050 } else { # Invalid form; output directly
1051 $s .= $prefix . '[[' . $line ;
1052 continue;
1055 /* Valid link forms:
1056 Foobar -- normal
1057 :Foobar -- override special treatment of prefix (images, language links)
1058 /Foobar -- convert to CurrentPage/Foobar
1059 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1061 $c = substr($m[1],0,1);
1062 $noforce = ($c != ':');
1063 if( $c == '/' ) { # subpage
1064 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1065 $m[1]=substr($m[1],1,strlen($m[1])-2);
1066 $noslash=$m[1];
1067 } else {
1068 $noslash=substr($m[1],1);
1070 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1071 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1072 if( '' == $text ) {
1073 $text= $m[1];
1074 } # this might be changed for ugliness reasons
1075 } else {
1076 $link = $noslash; # no subpage allowed, use standard link
1078 } elseif( $noforce ) { # no subpage
1079 $link = $m[1];
1080 } else {
1081 $link = substr( $m[1], 1 );
1083 $wasblank = ( '' == $text );
1084 if( $wasblank )
1085 $text = $link;
1087 $nt = Title::newFromText( $link );
1088 if( !$nt ) {
1089 $s .= $prefix . '[[' . $line;
1090 continue;
1092 $ns = $nt->getNamespace();
1093 $iw = $nt->getInterWiki();
1094 if( $noforce ) {
1095 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1096 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
1097 $tmp = $prefix . $trail ;
1098 $s .= (trim($tmp) == '')? '': $tmp;
1099 continue;
1101 if ( $ns == $image ) {
1102 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1103 $wgLinkCache->addImageLinkObj( $nt );
1104 continue;
1106 if ( $ns == $category ) {
1107 $t = $nt->getText() ;
1108 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1110 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1111 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1112 $wgLinkCache->resume();
1114 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1115 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1116 $this->mOutput->mCategoryLinks[] = $t ;
1117 $s .= $prefix . $trail ;
1118 continue;
1121 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1122 ( strpos( $link, '#' ) == FALSE ) ) {
1123 # Self-links are handled specially; generally de-link and change to bold.
1124 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1125 continue;
1128 if( $ns == $media ) {
1129 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1130 $wgLinkCache->addImageLinkObj( $nt );
1131 continue;
1132 } elseif( $ns == $special ) {
1133 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1134 continue;
1136 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1138 wfProfileOut( $fname );
1139 return $s;
1142 # Some functions here used by doBlockLevels()
1144 /* private */ function closeParagraph() {
1145 $result = '';
1146 if ( '' != $this->mLastSection ) {
1147 $result = '</' . $this->mLastSection . ">\n";
1149 $this->mInPre = false;
1150 $this->mLastSection = '';
1151 return $result;
1153 # getCommon() returns the length of the longest common substring
1154 # of both arguments, starting at the beginning of both.
1156 /* private */ function getCommon( $st1, $st2 ) {
1157 $fl = strlen( $st1 );
1158 $shorter = strlen( $st2 );
1159 if ( $fl < $shorter ) { $shorter = $fl; }
1161 for ( $i = 0; $i < $shorter; ++$i ) {
1162 if ( $st1{$i} != $st2{$i} ) { break; }
1164 return $i;
1166 # These next three functions open, continue, and close the list
1167 # element appropriate to the prefix character passed into them.
1169 /* private */ function openList( $char )
1171 $result = $this->closeParagraph();
1173 if ( '*' == $char ) { $result .= '<ul><li>'; }
1174 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1175 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1176 else if ( ';' == $char ) {
1177 $result .= '<dl><dt>';
1178 $this->mDTopen = true;
1180 else { $result = '<!-- ERR 1 -->'; }
1182 return $result;
1185 /* private */ function nextItem( $char ) {
1186 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1187 else if ( ':' == $char || ';' == $char ) {
1188 $close = "</dd>";
1189 if ( $this->mDTopen ) { $close = '</dt>'; }
1190 if ( ';' == $char ) {
1191 $this->mDTopen = true;
1192 return $close . '<dt>';
1193 } else {
1194 $this->mDTopen = false;
1195 return $close . '<dd>';
1198 return '<!-- ERR 2 -->';
1201 /* private */function closeList( $char ) {
1202 if ( '*' == $char ) { $text = '</li></ul>'; }
1203 else if ( '#' == $char ) { $text = '</li></ol>'; }
1204 else if ( ':' == $char ) {
1205 if ( $this->mDTopen ) {
1206 $this->mDTopen = false;
1207 $text = '</dt></dl>';
1208 } else {
1209 $text = '</dd></dl>';
1212 else { return '<!-- ERR 3 -->'; }
1213 return $text."\n";
1216 /* private */ function doBlockLevels( $text, $linestart ) {
1217 $fname = 'Parser::doBlockLevels';
1218 wfProfileIn( $fname );
1220 # Parsing through the text line by line. The main thing
1221 # happening here is handling of block-level elements p, pre,
1222 # and making lists from lines starting with * # : etc.
1224 $textLines = explode( "\n", $text );
1226 $lastPrefix = $output = $lastLine = '';
1227 $this->mDTopen = $inBlockElem = false;
1228 $prefixLength = 0;
1229 $paragraphStack = false;
1231 if ( !$linestart ) {
1232 $output .= array_shift( $textLines );
1234 foreach ( $textLines as $oLine ) {
1235 $lastPrefixLength = strlen( $lastPrefix );
1236 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1237 $preOpenMatch = preg_match("/<pre/i", $oLine );
1238 if (!$this->mInPre) {
1239 $this->mInPre = !empty($preOpenMatch);
1241 if ( !$this->mInPre ) {
1242 # Multiple prefixes may abut each other for nested lists.
1243 $prefixLength = strspn( $oLine, '*#:;' );
1244 $pref = substr( $oLine, 0, $prefixLength );
1246 # eh?
1247 $pref2 = str_replace( ';', ':', $pref );
1248 $t = substr( $oLine, $prefixLength );
1249 } else {
1250 # Don't interpret any other prefixes in preformatted text
1251 $prefixLength = 0;
1252 $pref = $pref2 = '';
1253 $t = $oLine;
1256 # List generation
1257 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1258 # Same as the last item, so no need to deal with nesting or opening stuff
1259 $output .= $this->nextItem( substr( $pref, -1 ) );
1260 $paragraphStack = false;
1262 if ( ";" == substr( $pref, -1 ) ) {
1263 # The one nasty exception: definition lists work like this:
1264 # ; title : definition text
1265 # So we check for : in the remainder text to split up the
1266 # title and definition, without b0rking links.
1267 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1268 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1269 $term = $match[1];
1270 $output .= $term . $this->nextItem( ':' );
1271 $t = $match[2];
1274 } elseif( $prefixLength || $lastPrefixLength ) {
1275 # Either open or close a level...
1276 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1277 $paragraphStack = false;
1279 while( $commonPrefixLength < $lastPrefixLength ) {
1280 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1281 --$lastPrefixLength;
1283 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1284 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1286 while ( $prefixLength > $commonPrefixLength ) {
1287 $char = substr( $pref, $commonPrefixLength, 1 );
1288 $output .= $this->openList( $char );
1290 if ( ';' == $char ) {
1291 # FIXME: This is dupe of code above
1292 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1293 $term = $match[1];
1294 $output .= $term . $this->nextItem( ":" );
1295 $t = $match[2];
1298 ++$commonPrefixLength;
1300 $lastPrefix = $pref2;
1302 if( 0 == $prefixLength ) {
1303 # No prefix (not in list)--go to paragraph mode
1304 $uniq_prefix = UNIQ_PREFIX;
1305 // XXX: use a stack for nestable elements like span, table and div
1306 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1307 $closematch = preg_match(
1308 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1309 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1310 if ( $openmatch or $closematch ) {
1311 $paragraphStack = false;
1312 $output .= $this->closeParagraph();
1313 if($preOpenMatch and !$preCloseMatch) {
1314 $this->mInPre = true;
1316 if ( $closematch ) {
1317 $inBlockElem = false;
1318 } else {
1319 $inBlockElem = true;
1321 } else if ( !$inBlockElem && !$this->mInPre ) {
1322 if ( " " == $t{0} and trim($t) != '' ) {
1323 // pre
1324 if ($this->mLastSection != 'pre') {
1325 $paragraphStack = false;
1326 $output .= $this->closeParagraph().'<pre>';
1327 $this->mLastSection = 'pre';
1329 } else {
1330 // paragraph
1331 if ( '' == trim($t) ) {
1332 if ( $paragraphStack ) {
1333 $output .= $paragraphStack.'<br />';
1334 $paragraphStack = false;
1335 $this->mLastSection = 'p';
1336 } else {
1337 if ($this->mLastSection != 'p' ) {
1338 $output .= $this->closeParagraph();
1339 $this->mLastSection = '';
1340 $paragraphStack = '<p>';
1341 } else {
1342 $paragraphStack = '</p><p>';
1345 } else {
1346 if ( $paragraphStack ) {
1347 $output .= $paragraphStack;
1348 $paragraphStack = false;
1349 $this->mLastSection = 'p';
1350 } else if ($this->mLastSection != 'p') {
1351 $output .= $this->closeParagraph().'<p>';
1352 $this->mLastSection = 'p';
1358 if ($paragraphStack === false) {
1359 $output .= $t."\n";
1362 while ( $prefixLength ) {
1363 $output .= $this->closeList( $pref2{$prefixLength-1} );
1364 --$prefixLength;
1366 if ( '' != $this->mLastSection ) {
1367 $output .= '</' . $this->mLastSection . '>';
1368 $this->mLastSection = '';
1371 wfProfileOut( $fname );
1372 return $output;
1375 # Return value of a magic variable (like PAGENAME)
1376 function getVariableValue( $index ) {
1377 global $wgLang, $wgSitename, $wgServer;
1379 switch ( $index ) {
1380 case MAG_CURRENTMONTH:
1381 return date( 'm' );
1382 case MAG_CURRENTMONTHNAME:
1383 return $wgLang->getMonthName( date('n') );
1384 case MAG_CURRENTMONTHNAMEGEN:
1385 return $wgLang->getMonthNameGen( date('n') );
1386 case MAG_CURRENTDAY:
1387 return date('j');
1388 case MAG_PAGENAME:
1389 return $this->mTitle->getText();
1390 case MAG_NAMESPACE:
1391 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1392 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1393 case MAG_CURRENTDAYNAME:
1394 return $wgLang->getWeekdayName( date('w')+1 );
1395 case MAG_CURRENTYEAR:
1396 return date( 'Y' );
1397 case MAG_CURRENTTIME:
1398 return $wgLang->time( wfTimestampNow(), false );
1399 case MAG_NUMBEROFARTICLES:
1400 return wfNumberOfArticles();
1401 case MAG_SITENAME:
1402 return $wgSitename;
1403 case MAG_SERVER:
1404 return $wgServer;
1405 default:
1406 return NULL;
1410 # initialise the magic variables (like CURRENTMONTHNAME)
1411 function initialiseVariables() {
1412 global $wgVariableIDs;
1413 $this->mVariables = array();
1414 foreach ( $wgVariableIDs as $id ) {
1415 $mw =& MagicWord::get( $id );
1416 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1420 /* private */ function replaceVariables( $text, $args = array() ) {
1421 global $wgLang, $wgScript, $wgArticlePath;
1423 $fname = 'Parser::replaceVariables';
1424 wfProfileIn( $fname );
1426 $bail = false;
1427 if ( !$this->mVariables ) {
1428 $this->initialiseVariables();
1430 $titleChars = Title::legalChars();
1431 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1433 # This function is called recursively. To keep track of arguments we need a stack:
1434 array_push( $this->mArgStack, $args );
1436 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1437 $GLOBALS['wgCurParser'] =& $this;
1440 if ( $this->mOutputType == OT_HTML ) {
1441 # Variable substitution
1442 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1444 # Argument substitution
1445 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1447 # Template substitution
1448 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1449 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1451 array_pop( $this->mArgStack );
1453 wfProfileOut( $fname );
1454 return $text;
1457 function variableSubstitution( $matches ) {
1458 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1459 $text = $this->mVariables[$matches[1]];
1460 $this->mOutput->mContainsOldMagic = true;
1461 } else {
1462 $text = $matches[0];
1464 return $text;
1467 function braceSubstitution( $matches ) {
1468 global $wgLinkCache, $wgLang;
1469 $fname = 'Parser::braceSubstitution';
1470 $found = false;
1471 $nowiki = false;
1472 $noparse = false;
1474 $title = NULL;
1476 # $newline is an optional newline character before the braces
1477 # $part1 is the bit before the first |, and must contain only title characters
1478 # $args is a list of arguments, starting from index 0, not including $part1
1480 $newline = $matches[1];
1481 $part1 = $matches[2];
1482 # If the third subpattern matched anything, it will start with |
1483 if ( $matches[3] !== '' ) {
1484 $args = explode( '|', substr( $matches[3], 1 ) );
1485 } else {
1486 $args = array();
1488 $argc = count( $args );
1490 # {{{}}}
1491 if ( strpos( $matches[0], '{{{' ) !== false ) {
1492 $text = $matches[0];
1493 $found = true;
1494 $noparse = true;
1497 # SUBST
1498 if ( !$found ) {
1499 $mwSubst =& MagicWord::get( MAG_SUBST );
1500 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1501 if ( $this->mOutputType != OT_WIKI ) {
1502 # Invalid SUBST not replaced at PST time
1503 # Return without further processing
1504 $text = $matches[0];
1505 $found = true;
1506 $noparse= true;
1508 } elseif ( $this->mOutputType == OT_WIKI ) {
1509 # SUBST not found in PST pass, do nothing
1510 $text = $matches[0];
1511 $found = true;
1515 # MSG, MSGNW and INT
1516 if ( !$found ) {
1517 # Check for MSGNW:
1518 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1519 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1520 $nowiki = true;
1521 } else {
1522 # Remove obsolete MSG:
1523 $mwMsg =& MagicWord::get( MAG_MSG );
1524 $mwMsg->matchStartAndRemove( $part1 );
1527 # Check if it is an internal message
1528 $mwInt =& MagicWord::get( MAG_INT );
1529 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1530 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1531 $text = wfMsgReal( $part1, $args, true );
1532 $found = true;
1537 # NS
1538 if ( !$found ) {
1539 # Check for NS: (namespace expansion)
1540 $mwNs = MagicWord::get( MAG_NS );
1541 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1542 if ( intval( $part1 ) ) {
1543 $text = $wgLang->getNsText( intval( $part1 ) );
1544 $found = true;
1545 } else {
1546 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1547 if ( !is_null( $index ) ) {
1548 $text = $wgLang->getNsText( $index );
1549 $found = true;
1555 # LOCALURL and LOCALURLE
1556 if ( !$found ) {
1557 $mwLocal = MagicWord::get( MAG_LOCALURL );
1558 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1560 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1561 $func = 'getLocalURL';
1562 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1563 $func = 'escapeLocalURL';
1564 } else {
1565 $func = '';
1568 if ( $func !== '' ) {
1569 $title = Title::newFromText( $part1 );
1570 if ( !is_null( $title ) ) {
1571 if ( $argc > 0 ) {
1572 $text = $title->$func( $args[0] );
1573 } else {
1574 $text = $title->$func();
1576 $found = true;
1581 # Internal variables
1582 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1583 $text = $this->mVariables[$part1];
1584 $found = true;
1585 $this->mOutput->mContainsOldMagic = true;
1588 # Arguments input from the caller
1589 $inputArgs = end( $this->mArgStack );
1590 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1591 $text = $inputArgs[$part1];
1592 $found = true;
1595 # Load from database
1596 if ( !$found ) {
1597 $title = Title::newFromText( $part1, NS_TEMPLATE );
1598 if ( !is_null( $title ) && !$title->isExternal() ) {
1599 # Check for excessive inclusion
1600 $dbk = $title->getPrefixedDBkey();
1601 if ( $this->incrementIncludeCount( $dbk ) ) {
1602 $article = new Article( $title );
1603 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1604 if ( $articleContent !== false ) {
1605 $found = true;
1606 $text = $articleContent;
1611 # If the title is valid but undisplayable, make a link to it
1612 if ( $this->mOutputType == OT_HTML && !$found ) {
1613 $text = '[[' . $title->getPrefixedText() . ']]';
1614 $found = true;
1619 # Recursive parsing, escaping and link table handling
1620 # Only for HTML output
1621 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1622 $text = wfEscapeWikiText( $text );
1623 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1624 # Clean up argument array
1625 $assocArgs = array();
1626 $index = 1;
1627 foreach( $args as $arg ) {
1628 $eqpos = strpos( $arg, '=' );
1629 if ( $eqpos === false ) {
1630 $assocArgs[$index++] = $arg;
1631 } else {
1632 $name = trim( substr( $arg, 0, $eqpos ) );
1633 $value = trim( substr( $arg, $eqpos+1 ) );
1634 if ( $value === false ) {
1635 $value = '';
1637 if ( $name !== false ) {
1638 $assocArgs[$name] = $value;
1643 # Do not enter included links in link table
1644 if ( !is_null( $title ) ) {
1645 $wgLinkCache->suspend();
1648 # Run full parser on the included text
1649 $text = $this->stripParse( $text, $newline, $assocArgs );
1651 # Resume the link cache and register the inclusion as a link
1652 if ( !is_null( $title ) ) {
1653 $wgLinkCache->resume();
1654 $wgLinkCache->addLinkObj( $title );
1658 if ( !$found ) {
1659 return $matches[0];
1660 } else {
1661 return $text;
1665 # Triple brace replacement -- used for template arguments
1666 function argSubstitution( $matches ) {
1667 $newline = $matches[1];
1668 $arg = trim( $matches[2] );
1669 $text = $matches[0];
1670 $inputArgs = end( $this->mArgStack );
1672 if ( array_key_exists( $arg, $inputArgs ) ) {
1673 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1676 return $text;
1679 # Returns true if the function is allowed to include this entity
1680 function incrementIncludeCount( $dbk ) {
1681 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1682 $this->mIncludeCount[$dbk] = 0;
1684 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1685 return true;
1686 } else {
1687 return false;
1692 # Cleans up HTML, removes dangerous tags and attributes
1693 /* private */ function removeHTMLtags( $text ) {
1694 global $wgUseTidy, $wgUserHtml;
1695 $fname = 'Parser::removeHTMLtags';
1696 wfProfileIn( $fname );
1698 if( $wgUserHtml ) {
1699 $htmlpairs = array( # Tags that must be closed
1700 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1701 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1702 'strike', 'strong', 'tt', 'var', 'div', 'center',
1703 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1704 'ruby', 'rt' , 'rb' , 'rp', 'p'
1706 $htmlsingle = array(
1707 'br', 'hr', 'li', 'dt', 'dd'
1709 $htmlnest = array( # Tags that can be nested--??
1710 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1711 'dl', 'font', 'big', 'small', 'sub', 'sup'
1713 $tabletags = array( # Can only appear inside table
1714 'td', 'th', 'tr'
1716 } else {
1717 $htmlpairs = array();
1718 $htmlsingle = array();
1719 $htmlnest = array();
1720 $tabletags = array();
1723 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1724 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1726 $htmlattrs = $this->getHTMLattrs () ;
1728 # Remove HTML comments
1729 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1731 $bits = explode( '<', $text );
1732 $text = array_shift( $bits );
1733 if(!$wgUseTidy) {
1734 $tagstack = array(); $tablestack = array();
1735 foreach ( $bits as $x ) {
1736 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1737 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1738 $x, $regs );
1739 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1740 error_reporting( $prev );
1742 $badtag = 0 ;
1743 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1744 # Check our stack
1745 if ( $slash ) {
1746 # Closing a tag...
1747 if ( ! in_array( $t, $htmlsingle ) &&
1748 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1749 @array_push( $tagstack, $ot );
1750 $badtag = 1;
1751 } else {
1752 if ( $t == 'table' ) {
1753 $tagstack = array_pop( $tablestack );
1755 $newparams = '';
1757 } else {
1758 # Keep track for later
1759 if ( in_array( $t, $tabletags ) &&
1760 ! in_array( 'table', $tagstack ) ) {
1761 $badtag = 1;
1762 } else if ( in_array( $t, $tagstack ) &&
1763 ! in_array ( $t , $htmlnest ) ) {
1764 $badtag = 1 ;
1765 } else if ( ! in_array( $t, $htmlsingle ) ) {
1766 if ( $t == 'table' ) {
1767 array_push( $tablestack, $tagstack );
1768 $tagstack = array();
1770 array_push( $tagstack, $t );
1772 # Strip non-approved attributes from the tag
1773 $newparams = $this->fixTagAttributes($params);
1776 if ( ! $badtag ) {
1777 $rest = str_replace( '>', '&gt;', $rest );
1778 $text .= "<$slash$t $newparams$brace$rest";
1779 continue;
1782 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1784 # Close off any remaining tags
1785 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1786 $text .= "</$t>\n";
1787 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1789 } else {
1790 # this might be possible using tidy itself
1791 foreach ( $bits as $x ) {
1792 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1793 $x, $regs );
1794 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1795 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1796 $newparams = $this->fixTagAttributes($params);
1797 $rest = str_replace( '>', '&gt;', $rest );
1798 $text .= "<$slash$t $newparams$brace$rest";
1799 } else {
1800 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1804 wfProfileOut( $fname );
1805 return $text;
1811 * This function accomplishes several tasks:
1812 * 1) Auto-number headings if that option is enabled
1813 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1814 * 3) Add a Table of contents on the top for users who have enabled the option
1815 * 4) Auto-anchor headings
1817 * It loops through all headlines, collects the necessary data, then splits up the
1818 * string and re-inserts the newly formatted headlines.
1822 /* private */ function formatHeadings( $text, $isMain=true ) {
1823 global $wgInputEncoding;
1825 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1826 $doShowToc = $this->mOptions->getShowToc();
1827 $forceTocHere = false;
1828 if( !$this->mTitle->userCanEdit() ) {
1829 $showEditLink = 0;
1830 $rightClickHack = 0;
1831 } else {
1832 $showEditLink = $this->mOptions->getEditSection();
1833 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1836 # Inhibit editsection links if requested in the page
1837 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1838 if( $esw->matchAndRemove( $text ) ) {
1839 $showEditLink = 0;
1841 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1842 # do not add TOC
1843 $mw =& MagicWord::get( MAG_NOTOC );
1844 if( $mw->matchAndRemove( $text ) ) {
1845 $doShowToc = 0;
1848 # never add the TOC to the Main Page. This is an entry page that should not
1849 # be more than 1-2 screens large anyway
1850 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
1851 $doShowToc = 0;
1854 # Get all headlines for numbering them and adding funky stuff like [edit]
1855 # links - this is for later, but we need the number of headlines right now
1856 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1858 # if there are fewer than 4 headlines in the article, do not show TOC
1859 if( $numMatches < 4 ) {
1860 $doShowToc = 0;
1863 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1864 # override above conditions and always show TOC at that place
1865 $mw =& MagicWord::get( MAG_TOC );
1866 if ($mw->match( $text ) ) {
1867 $doShowToc = 1;
1868 $forceTocHere = true;
1869 } else {
1870 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1871 # override above conditions and always show TOC above first header
1872 $mw =& MagicWord::get( MAG_FORCETOC );
1873 if ($mw->matchAndRemove( $text ) ) {
1874 $doShowToc = 1;
1880 # We need this to perform operations on the HTML
1881 $sk =& $this->mOptions->getSkin();
1883 # headline counter
1884 $headlineCount = 0;
1886 # Ugh .. the TOC should have neat indentation levels which can be
1887 # passed to the skin functions. These are determined here
1888 $toclevel = 0;
1889 $toc = '';
1890 $full = '';
1891 $head = array();
1892 $sublevelCount = array();
1893 $level = 0;
1894 $prevlevel = 0;
1895 foreach( $matches[3] as $headline ) {
1896 $numbering = '';
1897 if( $level ) {
1898 $prevlevel = $level;
1900 $level = $matches[1][$headlineCount];
1901 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1902 # reset when we enter a new level
1903 $sublevelCount[$level] = 0;
1904 $toc .= $sk->tocIndent( $level - $prevlevel );
1905 $toclevel += $level - $prevlevel;
1907 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1908 # reset when we step back a level
1909 $sublevelCount[$level+1]=0;
1910 $toc .= $sk->tocUnindent( $prevlevel - $level );
1911 $toclevel -= $prevlevel - $level;
1913 # count number of headlines for each level
1914 @$sublevelCount[$level]++;
1915 if( $doNumberHeadings || $doShowToc ) {
1916 $dot = 0;
1917 for( $i = 1; $i <= $level; $i++ ) {
1918 if( !empty( $sublevelCount[$i] ) ) {
1919 if( $dot ) {
1920 $numbering .= '.';
1922 $numbering .= $sublevelCount[$i];
1923 $dot = 1;
1928 # The canonized header is a version of the header text safe to use for links
1929 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1930 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
1931 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
1933 # strip out HTML
1934 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1935 $tocline = trim( $canonized_headline );
1936 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
1937 $replacearray = array(
1938 '%3A' => ':',
1939 '%' => '.'
1941 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1942 $refer[$headlineCount] = $canonized_headline;
1944 # count how many in assoc. array so we can track dupes in anchors
1945 @$refers[$canonized_headline]++;
1946 $refcount[$headlineCount]=$refers[$canonized_headline];
1948 # Prepend the number to the heading text
1950 if( $doNumberHeadings || $doShowToc ) {
1951 $tocline = $numbering . ' ' . $tocline;
1953 # Don't number the heading if it is the only one (looks silly)
1954 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1955 # the two are different if the line contains a link
1956 $headline=$numbering . ' ' . $headline;
1960 # Create the anchor for linking from the TOC to the section
1961 $anchor = $canonized_headline;
1962 if($refcount[$headlineCount] > 1 ) {
1963 $anchor .= '_' . $refcount[$headlineCount];
1965 if( $doShowToc ) {
1966 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1968 if( $showEditLink ) {
1969 if ( empty( $head[$headlineCount] ) ) {
1970 $head[$headlineCount] = '';
1972 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1975 # Add the edit section span
1976 if( $rightClickHack ) {
1977 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1980 # give headline the correct <h#> tag
1981 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1983 $headlineCount++;
1986 if( $doShowToc ) {
1987 $toclines = $headlineCount;
1988 $toc .= $sk->tocUnindent( $toclevel );
1989 $toc = $sk->tocTable( $toc );
1992 # split up and insert constructed headlines
1994 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
1995 $i = 0;
1997 foreach( $blocks as $block ) {
1998 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
1999 # This is the [edit] link that appears for the top block of text when
2000 # section editing is enabled
2002 # Disabled because it broke block formatting
2003 # For example, a bullet point in the top line
2004 # $full .= $sk->editSectionLink(0);
2006 $full .= $block;
2007 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2008 # Top anchor now in skin
2009 $full = $full.$toc;
2012 if( !empty( $head[$i] ) ) {
2013 $full .= $head[$i];
2015 $i++;
2017 if($forceTocHere) {
2018 $mw =& MagicWord::get( MAG_TOC );
2019 return $mw->replace( $toc, $full );
2020 } else {
2021 return $full;
2025 # Return an HTML link for the "ISBN 123456" text
2026 /* private */ function magicISBN( $text ) {
2027 global $wgLang;
2029 $a = split( 'ISBN ', " $text" );
2030 if ( count ( $a ) < 2 ) return $text;
2031 $text = substr( array_shift( $a ), 1);
2032 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2034 foreach ( $a as $x ) {
2035 $isbn = $blank = '' ;
2036 while ( ' ' == $x{0} ) {
2037 $blank .= ' ';
2038 $x = substr( $x, 1 );
2040 while ( strstr( $valid, $x{0} ) != false ) {
2041 $isbn .= $x{0};
2042 $x = substr( $x, 1 );
2044 $num = str_replace( '-', '', $isbn );
2045 $num = str_replace( ' ', '', $num );
2047 if ( '' == $num ) {
2048 $text .= "ISBN $blank$x";
2049 } else {
2050 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2051 $text .= '<a href="' .
2052 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2053 "\" class=\"internal\">ISBN $isbn</a>";
2054 $text .= $x;
2057 return $text;
2060 # Return an HTML link for the "RFC 1234" text
2061 /* private */ function magicRFC( $text ) {
2062 global $wgLang;
2064 $a = split( 'RFC ', ' '.$text );
2065 if ( count ( $a ) < 2 ) return $text;
2066 $text = substr( array_shift( $a ), 1);
2067 $valid = '0123456789';
2069 foreach ( $a as $x ) {
2070 $rfc = $blank = '' ;
2071 while ( ' ' == $x{0} ) {
2072 $blank .= ' ';
2073 $x = substr( $x, 1 );
2075 while ( strstr( $valid, $x{0} ) != false ) {
2076 $rfc .= $x{0};
2077 $x = substr( $x, 1 );
2080 if ( '' == $rfc ) {
2081 $text .= "RFC $blank$x";
2082 } else {
2083 $url = wfmsg( 'rfcurl' );
2084 $url = str_replace( '$1', $rfc, $url);
2085 $sk =& $this->mOptions->getSkin();
2086 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2087 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2090 return $text;
2093 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2094 $this->mOptions = $options;
2095 $this->mTitle =& $title;
2096 $this->mOutputType = OT_WIKI;
2098 if ( $clearState ) {
2099 $this->clearState();
2102 $stripState = false;
2103 $pairs = array(
2104 "\r\n" => "\n",
2106 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2107 // now with regexes
2109 $pairs = array(
2110 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2111 "/<br *?>/i" => "<br />",
2113 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2115 $text = $this->strip( $text, $stripState, false );
2116 $text = $this->pstPass2( $text, $user );
2117 $text = $this->unstrip( $text, $stripState );
2118 $text = $this->unstripNoWiki( $text, $stripState );
2119 return $text;
2122 /* private */ function pstPass2( $text, &$user ) {
2123 global $wgLang, $wgLocaltimezone, $wgCurParser;
2125 # Variable replacement
2126 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2127 $text = $this->replaceVariables( $text );
2129 # Signatures
2131 $n = $user->getName();
2132 $k = $user->getOption( 'nickname' );
2133 if ( '' == $k ) { $k = $n; }
2134 if(isset($wgLocaltimezone)) {
2135 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2137 /* Note: this is an ugly timezone hack for the European wikis */
2138 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2139 ' (' . date( 'T' ) . ')';
2140 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2142 $text = preg_replace( '/~~~~~/', $d, $text );
2143 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2144 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2145 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2146 Namespace::getUser() ) . ":$n|$k]]", $text );
2148 # Context links: [[|name]] and [[name (context)|]]
2150 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2151 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2152 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2153 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2155 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2156 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2157 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2158 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2159 # [[ns:page (cont)|]]
2160 $context = "";
2161 $t = $this->mTitle->getText();
2162 if ( preg_match( $conpat, $t, $m ) ) {
2163 $context = $m[2];
2165 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2166 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2167 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2169 if ( '' == $context ) {
2170 $text = preg_replace( $p2, '[[\\1]]', $text );
2171 } else {
2172 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2176 $mw =& MagicWord::get( MAG_SUBST );
2177 $wgCurParser = $this->fork();
2178 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2179 $this->merge( $wgCurParser );
2182 # Trim trailing whitespace
2183 # MAG_END (__END__) tag allows for trailing
2184 # whitespace to be deliberately included
2185 $text = rtrim( $text );
2186 $mw =& MagicWord::get( MAG_END );
2187 $mw->matchAndRemove( $text );
2189 return $text;
2192 # Set up some variables which are usually set up in parse()
2193 # so that an external function can call some class members with confidence
2194 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2195 $this->mTitle =& $title;
2196 $this->mOptions = $options;
2197 $this->mOutputType = $outputType;
2198 if ( $clearState ) {
2199 $this->clearState();
2203 function transformMsg( $text, $options ) {
2204 global $wgTitle;
2205 static $executing = false;
2207 # Guard against infinite recursion
2208 if ( $executing ) {
2209 return $text;
2211 $executing = true;
2213 $this->mTitle = $wgTitle;
2214 $this->mOptions = $options;
2215 $this->mOutputType = OT_MSG;
2216 $this->clearState();
2217 $text = $this->replaceVariables( $text );
2219 $executing = false;
2220 return $text;
2223 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2224 # Callback will be called with the text within
2225 # Transform and return the text within
2226 function setHook( $tag, $callback ) {
2227 $oldVal = @$this->mTagHooks[$tag];
2228 $this->mTagHooks[$tag] = $callback;
2229 return $oldVal;
2233 class ParserOutput
2235 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2236 var $mCacheTime; # Used in ParserCache
2238 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2239 $containsOldMagic = false )
2241 $this->mText = $text;
2242 $this->mLanguageLinks = $languageLinks;
2243 $this->mCategoryLinks = $categoryLinks;
2244 $this->mContainsOldMagic = $containsOldMagic;
2245 $this->mCacheTime = "";
2248 function getText() { return $this->mText; }
2249 function getLanguageLinks() { return $this->mLanguageLinks; }
2250 function getCategoryLinks() { return $this->mCategoryLinks; }
2251 function getCacheTime() { return $this->mCacheTime; }
2252 function containsOldMagic() { return $this->mContainsOldMagic; }
2253 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2254 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2255 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2256 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2257 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2259 function merge( $other ) {
2260 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2261 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2262 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2267 class ParserOptions
2269 # All variables are private
2270 var $mUseTeX; # Use texvc to expand <math> tags
2271 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2272 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2273 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2274 var $mAllowExternalImages; # Allow external images inline
2275 var $mSkin; # Reference to the preferred skin
2276 var $mDateFormat; # Date format index
2277 var $mEditSection; # Create "edit section" links
2278 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2279 var $mNumberHeadings; # Automatically number headings
2280 var $mShowToc; # Show table of contents
2282 function getUseTeX() { return $this->mUseTeX; }
2283 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2284 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2285 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2286 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2287 function getSkin() { return $this->mSkin; }
2288 function getDateFormat() { return $this->mDateFormat; }
2289 function getEditSection() { return $this->mEditSection; }
2290 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2291 function getNumberHeadings() { return $this->mNumberHeadings; }
2292 function getShowToc() { return $this->mShowToc; }
2294 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2295 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2296 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2297 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2298 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2299 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2300 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2301 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2302 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2303 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2305 function setSkin( &$x ) { $this->mSkin =& $x; }
2307 /* static */ function newFromUser( &$user ) {
2308 $popts = new ParserOptions;
2309 $popts->initialiseFromUser( $user );
2310 return $popts;
2313 function initialiseFromUser( &$userInput ) {
2314 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2316 if ( !$userInput ) {
2317 $user = new User;
2318 $user->setLoaded( true );
2319 } else {
2320 $user =& $userInput;
2323 $this->mUseTeX = $wgUseTeX;
2324 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2325 $this->mUseDynamicDates = $wgUseDynamicDates;
2326 $this->mInterwikiMagic = $wgInterwikiMagic;
2327 $this->mAllowExternalImages = $wgAllowExternalImages;
2328 $this->mSkin =& $user->getSkin();
2329 $this->mDateFormat = $user->getOption( 'date' );
2330 $this->mEditSection = $user->getOption( 'editsection' );
2331 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2332 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2333 $this->mShowToc = $user->getOption( 'showtoc' );
2339 # Regex callbacks, used in Parser::replaceVariables
2340 function wfBraceSubstitution( $matches )
2342 global $wgCurParser;
2343 return $wgCurParser->braceSubstitution( $matches );
2346 function wfArgSubstitution( $matches )
2348 global $wgCurParser;
2349 return $wgCurParser->argSubstitution( $matches );
2352 function wfVariableSubstitution( $matches )
2354 global $wgCurParser;
2355 return $wgCurParser->variableSubstitution( $matches );