Fix sorting of subcategories without a sortkey. Use Category name as sortkey, not...
[mediawiki.git] / includes / Parser.php
blob71d537db2ecc4a9dea4a05b4181ddbd407f69c0a
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 /* private */ $wgParserHooks = array();
51 class Parser
53 # Cleared with clearState():
54 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
55 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
57 # Temporary:
58 var $mOptions, $mTitle, $mOutputType;
60 function Parser()
62 $this->clearState();
65 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 )
85 global $wgUseTidy;
86 $fname = "Parser::parse";
87 wfProfileIn( $fname );
89 if ( $clearState ) {
90 $this->clearState();
93 $this->mOptions = $options;
94 $this->mTitle =& $title;
95 $this->mOutputType = OT_HTML;
97 $stripState = NULL;
98 $text = $this->strip( $text, $this->mStripState );
99 $text = $this->internalParse( $text, $linestart );
100 $text = $this->unstrip( $text, $this->mStripState );
101 # Clean up special characters, only run once, next-to-last before doBlockLevels
102 if(!$wgUseTidy) {
103 $fixtags = array(
104 # french spaces, last one Guillemet-left
105 # only if there is something before the space
106 '/(.) (\\?|:|!|\\302\\273)/i' => '\\1&nbsp;\\2',
107 # french spaces, Guillemet-right
108 "/(\\302\\253) /i"=>"\\1&nbsp;",
109 '/<hr *>/i' => '<hr />',
110 '/<br *>/i' => '<br />',
111 '/<center *>/i' => '<div class="center">',
112 '/<\\/center *>/i' => '</div>',
113 # Clean up spare ampersands; note that we probably ought to be
114 # more careful about named entities.
115 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
117 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
118 } else {
119 $fixtags = array(
120 # french spaces, last one Guillemet-left
121 '/ (\\?|:|!|\\302\\273)/i' => '&nbsp;\\1',
122 # french spaces, Guillemet-right
123 '/(\\302\\253) /i' => '\\1&nbsp;',
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()
142 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
145 # Replaces all occurrences of <$tag>content</$tag> in the text
146 # with a random marker and returns the new text. the output parameter
147 # $content will be an associative array filled with data on the form
148 # $unique_marker => content.
150 # If $content is already set, the additional entries will be appended
152 # If $tag is set to STRIP_COMMENTS, the function will extract
153 # <!-- HTML comments -->
155 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
156 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
157 if ( !$content ) {
158 $content = array( );
160 $n = 1;
161 $stripped = '';
163 while ( '' != $text ) {
164 if($tag==STRIP_COMMENTS) {
165 $p = preg_split( '/<!--/i', $text, 2 );
166 } else {
167 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
169 $stripped .= $p[0];
170 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
171 $text = '';
172 } else {
173 if($tag==STRIP_COMMENTS) {
174 $q = preg_split( '/-->/i', $p[1], 2 );
175 } else {
176 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
178 $marker = $rnd . sprintf('%08X', $n++);
179 $content[$marker] = $q[0];
180 $stripped .= $marker;
181 $text = $q[1];
184 return $stripped;
187 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
188 # If $render is set, performs necessary rendering operations on plugins
189 # Returns the text, and fills an array with data needed in unstrip()
190 # If the $state is already a valid strip state, it adds to the state
192 # When $stripcomments is set, HTML comments <!-- like this -->
193 # will be stripped in addition to other tags. This is important
194 # for section editing, where these comments cause confusion when
195 # counting the sections in the wikisource
196 function strip( $text, &$state, $stripcomments = false )
198 global $wgParserHooks;
200 $render = ($this->mOutputType == OT_HTML);
201 $nowiki_content = array();
202 $math_content = array();
203 $pre_content = array();
204 $comment_content = array();
205 $ext_content = array();
207 # Replace any instances of the placeholders
208 $uniq_prefix = UNIQ_PREFIX;
209 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
212 # nowiki
213 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
214 foreach( $nowiki_content as $marker => $content ){
215 if( $render ){
216 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
217 } else {
218 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
222 # math
223 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
224 foreach( $math_content as $marker => $content ){
225 if( $render ) {
226 if( $this->mOptions->getUseTeX() ) {
227 $math_content[$marker] = renderMath( $content );
228 } else {
229 $math_content[$marker] = "&lt;math&gt;$content&lt;math&gt;";
231 } else {
232 $math_content[$marker] = "<math>$content</math>";
236 # pre
237 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
238 foreach( $pre_content as $marker => $content ){
239 if( $render ){
240 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
241 } else {
242 $pre_content[$marker] = "<pre>$content</pre>";
246 # Comments
247 if($stripcomments) {
248 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
249 foreach( $comment_content as $marker => $content ){
250 $comment_content[$marker] = "<!--$content-->";
254 # Extensions
255 foreach ( $wgParserHooks as $tag => $callback ) {
256 $ext_contents[$tag] = array();
257 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
258 foreach( $ext_content[$tag] as $marker => $content ) {
259 if ( $render ) {
260 $ext_content[$tag][$marker] = $callback( $content );
261 } else {
262 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
267 # Merge state with the pre-existing state, if there is one
268 if ( $state ) {
269 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
270 $state['math'] = $state['math'] + $math_content;
271 $state['pre'] = $state['pre'] + $pre_content;
272 $state['comment'] = $state['comment'] + $comment_content;
274 foreach( $ext_content as $tag => $array ) {
275 if ( array_key_exists( $tag, $state ) ) {
276 $state[$tag] = $state[$tag] + $array;
279 } else {
280 $state = array(
281 'nowiki' => $nowiki_content,
282 'math' => $math_content,
283 'pre' => $pre_content,
284 'comment' => $comment_content,
285 ) + $ext_content;
287 return $text;
290 # always call unstripNoWiki() after this one
291 function unstrip( $text, &$state )
293 # Must expand in reverse order, otherwise nested tags will be corrupted
294 $contentDict = end( $state );
295 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
296 if( key($state) != 'nowiki') {
297 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
298 $text = str_replace( key( $contentDict ), $content, $text );
303 return $text;
305 # always call this after unstrip() to preserve the order
306 function unstripNoWiki( $text, &$state )
308 # Must expand in reverse order, otherwise nested tags will be corrupted
309 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
310 $text = str_replace( key( $state['nowiki'] ), $content, $text );
313 return $text;
316 # Add an item to the strip state
317 # Returns the unique tag which must be inserted into the stripped text
318 # The tag will be replaced with the original text in unstrip()
320 function insertStripItem( $text, &$state )
322 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
323 if ( !$state ) {
324 $state = array(
325 'nowiki' => array(),
326 'math' => array(),
327 'pre' => array()
330 $state['item'][$rnd] = $text;
331 return $rnd;
334 # categoryMagic
335 # generate a list of subcategories and pages for a category
336 # depending on wfMsg("usenewcategorypage") it either calls the new
337 # or the old code. The new code will not work properly for some
338 # languages due to sorting issues, so they might want to turn it
339 # off.
340 function categoryMagic()
342 $msg = wfMsg('usenewcategorypage');
343 if ( '0' == @$msg[0] )
345 return $this->oldCategoryMagic();
346 } else {
347 return $this->newCategoryMagic();
351 # This method generates the list of subcategories and pages for a category
352 function oldCategoryMagic ()
354 global $wgLang , $wgUser ;
355 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
357 $cns = Namespace::getCategory() ;
358 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
360 $r = "<br style=\"clear:both;\"/>\n";
363 $sk =& $wgUser->getSkin() ;
365 $articles = array() ;
366 $children = array() ;
367 $data = array () ;
368 $id = $this->mTitle->getArticleID() ;
370 # FIXME: add limits
371 $t = wfStrencode( $this->mTitle->getDBKey() );
372 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
373 $res = wfQuery ( $sql, DB_READ ) ;
374 while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
376 # For all pages that link to this category
377 foreach ( $data AS $x )
379 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
380 if ( $t != "" ) $t .= ":" ;
381 $t .= $x->cur_title ;
383 if ( $x->cur_namespace == $cns ) {
384 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
385 } else {
386 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
389 wfFreeResult ( $res ) ;
391 # Showing subcategories
392 if ( count ( $children ) > 0 ) {
393 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
394 $r .= implode ( ', ' , $children ) ;
397 # Showing pages in this category
398 if ( count ( $articles ) > 0 ) {
399 $ti = $this->mTitle->getText() ;
400 $h = wfMsg( 'category_header', $ti );
401 $r .= "<h2>{$h}</h2>\n" ;
402 $r .= implode ( ', ' , $articles ) ;
406 return $r ;
411 function newCategoryMagic ()
413 global $wgLang , $wgUser ;
414 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
416 $cns = Namespace::getCategory() ;
417 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
419 $r = "<br style=\"clear:both;\"/>\n";
422 $sk =& $wgUser->getSkin() ;
424 $articles = array() ;
425 $articles_start_char = array();
426 $children = array() ;
427 $children_start_char = array();
428 $data = array () ;
429 $id = $this->mTitle->getArticleID() ;
431 # FIXME: add limits
432 $t = wfStrencode( $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
435 cl_sortkey" ;
436 $res = wfQuery ( $sql, DB_READ ) ;
437 while ( $x = wfFetchObject ( $res ) )
439 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
440 if ( $t != '' ) $t .= ':' ;
441 $t .= $x->cur_title ;
443 if ( $x->cur_namespace == $cns ) {
444 $ctitle = str_replace( '_',' ',$x->cur_title );
445 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
447 // If there's a link from Category:A to Category:B, the sortkey of the resulting
448 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
449 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
450 // else use sortkey...
451 if ( ($ns.":".$ctitle) == $x->cl_sortkey ) {
452 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) );
453 } else {
454 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
456 } else {
457 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
458 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
461 wfFreeResult ( $res ) ;
463 $ti = $this->mTitle->getText() ;
465 # Don't show subcategories section if there are none.
466 if ( count ( $children ) > 0 )
468 # Showing subcategories
469 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
470 . wfMsg( 'subcategorycount', count( $children ) );
471 if ( count ( $children ) > 6 ) {
473 // divide list into three equal chunks
474 $chunk = (int) (count ( $children ) / 3);
476 // get and display header
477 $r .= '<table width="100%"><tr valign="top">';
479 $startChunk = 0;
480 $endChunk = $chunk;
482 // loop through the chunks
483 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
484 $chunkIndex < 3;
485 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
488 $r .= '<td><ul>';
489 // output all subcategories to category
490 for ($index = $startChunk ;
491 $index < $endChunk && $index < count($children);
492 $index++ )
494 // check for change of starting letter or begging of chunk
495 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
496 || ($index == $startChunk) )
498 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
501 $r .= "<li>{$children[$index]}</li>";
503 $r .= '</ul></td>';
507 $r .= '</tr></table>';
508 } else {
509 // for short lists of subcategories to category.
511 $r .= "<h3>{$children_start_char[0]}</h3>\n";
512 $r .= '<ul><li>'.$children[0].'</li>';
513 for ($index = 1; $index < count($children); $index++ )
515 if ($children_start_char[$index] != $children_start_char[$index - 1])
517 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
520 $r .= "<li>{$children[$index]}</li>";
522 $r .= '</ul>';
524 } # END of if ( count($children) > 0 )
526 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
527 wfMsg( 'categoryarticlecount', count( $articles ) );
529 # Showing articles in this category
530 if ( count ( $articles ) > 6) {
531 $ti = $this->mTitle->getText() ;
533 // divide list into three equal chunks
534 $chunk = (int) (count ( $articles ) / 3);
536 // get and display header
537 $r .= '<table width="100%"><tr valign="top">';
539 // loop through the chunks
540 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
541 $chunkIndex < 3;
542 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
545 $r .= '<td><ul>';
547 // output all articles in category
548 for ($index = $startChunk ;
549 $index < $endChunk && $index < count($articles);
550 $index++ )
552 // check for change of starting letter or begging of chunk
553 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
554 || ($index == $startChunk) )
556 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
559 $r .= "<li>{$articles[$index]}</li>";
561 $r .= '</ul></td>';
565 $r .= '</tr></table>';
566 } elseif ( count ( $articles ) > 0) {
567 // for short lists of articles in categories.
568 $ti = $this->mTitle->getText() ;
570 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
571 $r .= '<ul><li>'.$articles[0].'</li>';
572 for ($index = 1; $index < count($articles); $index++ )
574 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
576 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
579 $r .= "<li>{$articles[$index]}</li>";
581 $r .= '</ul>';
585 return $r ;
588 # Return allowed HTML attributes
589 function getHTMLattrs ()
591 $htmlattrs = array( # Allowed attributes--no scripting, etc.
592 'title', 'align', 'lang', 'dir', 'width', 'height',
593 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
594 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
595 /* FONT */ 'type', 'start', 'value', 'compact',
596 /* For various lists, mostly deprecated but safe */
597 'summary', 'width', 'border', 'frame', 'rules',
598 'cellspacing', 'cellpadding', 'valign', 'char',
599 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
600 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
601 'id', 'class', 'name', 'style' /* For CSS */
603 return $htmlattrs ;
606 # Remove non approved attributes and javascript in css
607 function fixTagAttributes ( $t )
609 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
610 $htmlattrs = $this->getHTMLattrs() ;
612 # Strip non-approved attributes from the tag
613 $t = preg_replace(
614 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
615 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
616 $t);
617 # Strip javascript "expression" from stylesheets. Brute force approach:
618 # If anythin offensive is found, all attributes of the HTML tag are dropped
620 if( preg_match(
621 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
622 wfMungeToUtf8( $t ) ) )
624 $t='';
627 return trim ( $t ) ;
630 # interface with html tidy, used if $wgUseTidy = true
631 function tidy ( $text ) {
632 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
633 global $wgInputEncoding, $wgOutputEncoding;
634 $fname = 'Parser::tidy';
635 wfProfileIn( $fname );
637 $cleansource = '';
638 switch(strtoupper($wgOutputEncoding)) {
639 case 'ISO-8859-1':
640 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
641 break;
642 case 'UTF-8':
643 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
644 break;
645 default:
646 $wgTidyOpts .= ' -raw';
649 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
650 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
651 '<head><title>test</title></head><body>'.$text.'</body></html>';
652 $descriptorspec = array(
653 0 => array('pipe', 'r'),
654 1 => array('pipe', 'w'),
655 2 => array('file', '/dev/null', 'a')
657 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
658 if (is_resource($process)) {
659 fwrite($pipes[0], $wrappedtext);
660 fclose($pipes[0]);
661 while (!feof($pipes[1])) {
662 $cleansource .= fgets($pipes[1], 1024);
664 fclose($pipes[1]);
665 $return_value = proc_close($process);
668 wfProfileOut( $fname );
670 if( $cleansource == '' && $text != '') {
671 wfDebug( "Tidy error detected!\n" );
672 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
673 } else {
674 return $cleansource;
678 # parse the wiki syntax used to render tables
679 function doTableStuff ( $t )
681 $t = explode ( "\n" , $t ) ;
682 $td = array () ; # Is currently a td tag open?
683 $ltd = array () ; # Was it TD or TH?
684 $tr = array () ; # Is currently a tr tag open?
685 $ltr = array () ; # tr attributes
686 foreach ( $t AS $k => $x )
688 $x = trim ( $x ) ;
689 $fc = substr ( $x , 0 , 1 ) ;
690 if ( '{|' == substr ( $x , 0 , 2 ) )
692 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 3 ) ) . '>' ;
693 array_push ( $td , false ) ;
694 array_push ( $ltd , '' ) ;
695 array_push ( $tr , false ) ;
696 array_push ( $ltr , '' ) ;
698 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
699 else if ( '|}' == substr ( $x , 0 , 2 ) )
701 $z = "</table>\n" ;
702 $l = array_pop ( $ltd ) ;
703 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
704 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
705 array_pop ( $ltr ) ;
706 $t[$k] = $z ;
708 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
710 $z = trim ( substr ( $x , 2 ) ) ;
711 $t[$k] = "<caption>{$z}</caption>\n" ;
713 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
715 $x = substr ( $x , 1 ) ;
716 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
717 $z = '' ;
718 $l = array_pop ( $ltd ) ;
719 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
720 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
721 array_pop ( $ltr ) ;
722 $t[$k] = $z ;
723 array_push ( $tr , false ) ;
724 array_push ( $td , false ) ;
725 array_push ( $ltd , '' ) ;
726 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
728 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
730 if ( '|+' == substr ( $x , 0 , 2 ) )
732 $fc = '+' ;
733 $x = substr ( $x , 1 ) ;
735 $after = substr ( $x , 1 ) ;
736 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
737 $after = explode ( '||' , $after ) ;
738 $t[$k] = '' ;
739 foreach ( $after AS $theline )
741 $z = '' ;
742 if ( $fc != '+' )
744 $tra = array_pop ( $ltr ) ;
745 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
746 array_push ( $tr , true ) ;
747 array_push ( $ltr , '' ) ;
750 $l = array_pop ( $ltd ) ;
751 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
752 if ( $fc == '|' ) $l = 'td' ;
753 else if ( $fc == '!' ) $l = 'th' ;
754 else if ( $fc == '+' ) $l = 'caption' ;
755 else $l = '' ;
756 array_push ( $ltd , $l ) ;
757 $y = explode ( '|' , $theline , 2 ) ;
758 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
759 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
760 $t[$k] .= $y ;
761 array_push ( $td , true ) ;
766 # Closing open td, tr && table
767 while ( count ( $td ) > 0 )
769 if ( array_pop ( $td ) ) $t[] = '</td>' ;
770 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
771 $t[] = '</table>' ;
774 $t = implode ( "\n" , $t ) ;
775 # $t = $this->removeHTMLtags( $t );
776 return $t ;
779 # Parses the text and adds the result to the strip state
780 # Returns the strip tag
781 function stripParse( $text, $newline, $args )
783 $text = $this->strip( $text, $this->mStripState );
784 $text = $this->internalParse( $text, (bool)$newline, $args, false );
785 return $newline.$this->insertStripItem( $text, $this->mStripState );
788 function internalParse( $text, $linestart, $args = array(), $isMain=true )
790 $fname = 'Parser::internalParse';
791 wfProfileIn( $fname );
793 $text = $this->removeHTMLtags( $text );
794 $text = $this->replaceVariables( $text, $args );
796 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
798 $text = $this->doHeadings( $text );
799 if($this->mOptions->getUseDynamicDates()) {
800 global $wgDateFormatter;
801 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
803 $text = $this->doAllQuotes( $text );
804 $text = $this->replaceExternalLinks( $text );
805 $text = $this->replaceInternalLinks ( $text );
806 $text = $this->replaceInternalLinks ( $text );
807 //$text = $this->doTokenizedParser ( $text );
808 $text = $this->doTableStuff ( $text ) ;
809 $text = $this->magicISBN( $text );
810 $text = $this->magicRFC( $text );
811 $text = $this->formatHeadings( $text, $isMain );
812 $sk =& $this->mOptions->getSkin();
813 $text = $sk->transformContent( $text );
815 if ( !isset ( $this->categoryMagicDone ) ) {
816 $text .= $this->categoryMagic () ;
817 $this->categoryMagicDone = true ;
820 wfProfileOut( $fname );
821 return $text;
824 # Parse headers and return html
825 /* private */ function doHeadings( $text )
827 $fname = 'Parser::doHeadings';
828 wfProfileIn( $fname );
829 for ( $i = 6; $i >= 1; --$i ) {
830 $h = substr( '======', 0, $i );
831 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
832 "<h{$i}>\\1</h{$i}>\\2", $text );
834 wfProfileOut( $fname );
835 return $text;
838 /* private */ function doAllQuotes( $text )
840 $fname = 'Parser::doAllQuotes';
841 wfProfileIn( $fname );
842 $outtext = '';
843 $lines = explode( "\n", $text );
844 foreach ( $lines as $line ) {
845 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
847 $outtext = substr($outtext, 0,-1);
848 wfProfileOut( $fname );
849 return $outtext;
852 /* private */ function doQuotes( $pre, $text, $mode )
854 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
855 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
856 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
857 if ( substr ($m[2], 0, 1) == '\'' ) {
858 $m[2] = substr ($m[2], 1);
859 if ($mode == 'em') {
860 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'emstrong' );
861 } else if ($mode == 'strong') {
862 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
863 } else if (($mode == 'emstrong') || ($mode == 'both')) {
864 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
865 } else if ($mode == 'strongem') {
866 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
867 } else {
868 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
870 } else {
871 if ($mode == 'strong') {
872 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ? 'both' : 'strongem' );
873 } else if ($mode == 'em') {
874 return $m1_em . $this->doQuotes ( '', $m[2], '' );
875 } else if ($mode == 'emstrong') {
876 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
877 } else if (($mode == 'strongem') || ($mode == 'both')) {
878 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
879 } else {
880 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
883 } else {
884 $text_strong = ($text == '') ? '' : "<strong>{$text}</strong>";
885 $text_em = ($text == '') ? '' : "<em>{$text}</em>";
886 if ($mode == '') {
887 return $pre . $text;
888 } else if ($mode == 'em') {
889 return $pre . $text_em;
890 } else if ($mode == 'strong') {
891 return $pre . $text_strong;
892 } else if ($mode == 'strongem') {
893 return (($pre == '') && ($text == '')) ? '' : "<strong>{$pre}{$text_em}</strong>";
894 } else {
895 return (($pre == '') && ($text == '')) ? '' : "<em>{$pre}{$text_strong}</em>";
900 # Note: we have to do external links before the internal ones,
901 # and otherwise take great care in the order of things here, so
902 # that we don't end up interpreting some URLs twice.
904 /* private */ function replaceExternalLinks( $text )
906 $fname = 'Parser::replaceExternalLinks';
907 wfProfileIn( $fname );
908 $text = $this->subReplaceExternalLinks( $text, 'http', true );
909 $text = $this->subReplaceExternalLinks( $text, 'https', true );
910 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
911 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
912 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
913 $text = $this->subReplaceExternalLinks( $text, 'news', false );
914 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
915 wfProfileOut( $fname );
916 return $text;
919 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
921 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
922 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
924 # this is the list of separators that should be ignored if they
925 # are the last character of an URL but that should be included
926 # if they occur within the URL, e.g. "go to www.foo.com, where .."
927 # in this case, the last comma should not become part of the URL,
928 # but in "www.foo.com/123,2342,32.htm" it should.
929 $sep = ",;\.:";
930 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
931 $images = 'gif|png|jpg|jpeg';
933 # PLEASE NOTE: The curly braces { } are not part of the regex,
934 # they are interpreted as part of the string (used to tell PHP
935 # that the content of the string should be inserted there).
936 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
937 "((?i){$images})([^{$uc}]|$)/";
939 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
940 $sk =& $this->mOptions->getSkin();
942 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
943 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
944 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
946 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
947 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
948 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
949 '</a>\\5', $s );
950 $s = str_replace( $unique, $protocol, $s );
952 $a = explode( "[{$protocol}:", " " . $s );
953 $s = array_shift( $a );
954 $s = substr( $s, 1 );
956 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
957 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
959 foreach ( $a as $line ) {
960 if ( preg_match( $e1, $line, $m ) ) {
961 $link = "{$protocol}:{$m[1]}";
962 $trail = $m[2];
963 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
964 else { $text = wfEscapeHTML( $link ); }
965 } else if ( preg_match( $e2, $line, $m ) ) {
966 $link = "{$protocol}:{$m[1]}";
967 $text = $m[2];
968 $trail = $m[3];
969 } else {
970 $s .= "[{$protocol}:" . $line;
971 continue;
973 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
974 $paren = '';
975 } else {
976 # Expand the URL for printable version
977 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
979 $la = $sk->getExternalLinkAttributes( $link, $text );
980 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
983 return $s;
987 /* private */ function replaceInternalLinks( $s )
989 global $wgLang, $wgLinkCache;
990 global $wgNamespacesWithSubpages, $wgLanguageCode;
991 static $fname = 'Parser::replaceInternalLinks' ;
992 wfProfileIn( $fname );
994 wfProfileIn( $fname.'-setup' );
995 static $tc = FALSE;
996 # the % is needed to support urlencoded titles as well
997 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
998 $sk =& $this->mOptions->getSkin();
1000 $a = explode( '[[', ' ' . $s );
1001 $s = array_shift( $a );
1002 $s = substr( $s, 1 );
1004 # Match a link having the form [[namespace:link|alternate]]trail
1005 static $e1 = FALSE;
1006 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1007 # Match the end of a line for a word that's not followed by whitespace,
1008 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1009 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1011 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1012 # Special and Media are pseudo-namespaces; no pages actually exist in them
1013 static $image = FALSE;
1014 static $special = FALSE;
1015 static $media = FALSE;
1016 static $category = FALSE;
1017 if ( !$image ) { $image = Namespace::getImage(); }
1018 if ( !$special ) { $special = Namespace::getSpecial(); }
1019 if ( !$media ) { $media = Namespace::getMedia(); }
1020 if ( !$category ) { $category = Namespace::getCategory(); }
1022 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1024 if ( $useLinkPrefixExtension ) {
1025 if ( preg_match( $e2, $s, $m ) ) {
1026 $first_prefix = $m[2];
1027 $s = $m[1];
1028 } else {
1029 $first_prefix = false;
1031 } else {
1032 $prefix = '';
1035 wfProfileOut( $fname.'-setup' );
1037 foreach ( $a as $line ) {
1038 wfProfileIn( $fname.'-prefixhandling' );
1039 if ( $useLinkPrefixExtension ) {
1040 if ( preg_match( $e2, $s, $m ) ) {
1041 $prefix = $m[2];
1042 $s = $m[1];
1043 } else {
1044 $prefix='';
1046 # first link
1047 if($first_prefix) {
1048 $prefix = $first_prefix;
1049 $first_prefix = false;
1052 wfProfileOut( $fname.'-prefixhandling' );
1054 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1055 $text = $m[2];
1056 # fix up urlencoded title texts
1057 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1058 $trail = $m[3];
1059 } else { # Invalid form; output directly
1060 $s .= $prefix . '[[' . $line ;
1061 continue;
1064 /* Valid link forms:
1065 Foobar -- normal
1066 :Foobar -- override special treatment of prefix (images, language links)
1067 /Foobar -- convert to CurrentPage/Foobar
1068 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1070 $c = substr($m[1],0,1);
1071 $noforce = ($c != ':');
1072 if( $c == '/' ) { # subpage
1073 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1074 $m[1]=substr($m[1],1,strlen($m[1])-2);
1075 $noslash=$m[1];
1076 } else {
1077 $noslash=substr($m[1],1);
1079 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1080 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1081 if( '' == $text ) {
1082 $text= $m[1];
1083 } # this might be changed for ugliness reasons
1084 } else {
1085 $link = $noslash; # no subpage allowed, use standard link
1087 } elseif( $noforce ) { # no subpage
1088 $link = $m[1];
1089 } else {
1090 $link = substr( $m[1], 1 );
1092 $wasblank = ( '' == $text );
1093 if( $wasblank )
1094 $text = $link;
1096 $nt = Title::newFromText( $link );
1097 if( !$nt ) {
1098 $s .= $prefix . '[[' . $line;
1099 continue;
1101 $ns = $nt->getNamespace();
1102 $iw = $nt->getInterWiki();
1103 if( $noforce ) {
1104 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1105 array_push( $this->mOutput->mLanguageLinks, $nt->getPrefixedText() );
1106 $tmp = $prefix . $trail ;
1107 $s .= (trim($tmp) == '')? '': $tmp;
1108 continue;
1110 if ( $ns == $image ) {
1111 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1112 $wgLinkCache->addImageLinkObj( $nt );
1113 continue;
1115 if ( $ns == $category ) {
1116 $t = $nt->getText() ;
1117 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1119 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1120 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1121 $wgLinkCache->resume();
1123 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1124 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1125 $this->mOutput->mCategoryLinks[] = $t ;
1126 $s .= $prefix . $trail ;
1127 continue;
1130 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1131 ( strpos( $link, '#' ) == FALSE ) ) {
1132 # Self-links are handled specially; generally de-link and change to bold.
1133 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1134 continue;
1137 if( $ns == $media ) {
1138 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1139 $wgLinkCache->addImageLinkObj( $nt );
1140 continue;
1141 } elseif( $ns == $special ) {
1142 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1143 continue;
1145 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1147 wfProfileOut( $fname );
1148 return $s;
1151 # Some functions here used by doBlockLevels()
1153 /* private */ function closeParagraph()
1155 $result = '';
1156 if ( '' != $this->mLastSection ) {
1157 $result = '</' . $this->mLastSection . ">\n";
1159 $this->mInPre = false;
1160 $this->mLastSection = '';
1161 return $result;
1163 # getCommon() returns the length of the longest common substring
1164 # of both arguments, starting at the beginning of both.
1166 /* private */ function getCommon( $st1, $st2 )
1168 $fl = strlen( $st1 );
1169 $shorter = strlen( $st2 );
1170 if ( $fl < $shorter ) { $shorter = $fl; }
1172 for ( $i = 0; $i < $shorter; ++$i ) {
1173 if ( $st1{$i} != $st2{$i} ) { break; }
1175 return $i;
1177 # These next three functions open, continue, and close the list
1178 # element appropriate to the prefix character passed into them.
1180 /* private */ function openList( $char )
1182 $result = $this->closeParagraph();
1184 if ( '*' == $char ) { $result .= '<ul><li>'; }
1185 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1186 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1187 else if ( ';' == $char ) {
1188 $result .= '<dl><dt>';
1189 $this->mDTopen = true;
1191 else { $result = '<!-- ERR 1 -->'; }
1193 return $result;
1196 /* private */ function nextItem( $char )
1198 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1199 else if ( ':' == $char || ';' == $char ) {
1200 $close = "</dd>";
1201 if ( $this->mDTopen ) { $close = '</dt>'; }
1202 if ( ';' == $char ) {
1203 $this->mDTopen = true;
1204 return $close . '<dt>';
1205 } else {
1206 $this->mDTopen = false;
1207 return $close . '<dd>';
1210 return '<!-- ERR 2 -->';
1213 /* private */function closeList( $char )
1215 if ( '*' == $char ) { $text = '</li></ul>'; }
1216 else if ( '#' == $char ) { $text = '</li></ol>'; }
1217 else if ( ':' == $char ) {
1218 if ( $this->mDTopen ) {
1219 $this->mDTopen = false;
1220 $text = '</dt></dl>';
1221 } else {
1222 $text = '</dd></dl>';
1225 else { return '<!-- ERR 3 -->'; }
1226 return $text."\n";
1229 /* private */ function doBlockLevels( $text, $linestart ) {
1230 $fname = 'Parser::doBlockLevels';
1231 wfProfileIn( $fname );
1233 # Parsing through the text line by line. The main thing
1234 # happening here is handling of block-level elements p, pre,
1235 # and making lists from lines starting with * # : etc.
1237 $textLines = explode( "\n", $text );
1239 $lastPrefix = $output = $lastLine = '';
1240 $this->mDTopen = $inBlockElem = false;
1241 $prefixLength = 0;
1242 $paragraphStack = false;
1244 if ( !$linestart ) {
1245 $output .= array_shift( $textLines );
1247 foreach ( $textLines as $oLine ) {
1248 $lastPrefixLength = strlen( $lastPrefix );
1249 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1250 $preOpenMatch = preg_match("/<pre/i", $oLine );
1251 if (!$this->mInPre) {
1252 $this->mInPre = !empty($preOpenMatch);
1254 if ( !$this->mInPre ) {
1255 # Multiple prefixes may abut each other for nested lists.
1256 $prefixLength = strspn( $oLine, '*#:;' );
1257 $pref = substr( $oLine, 0, $prefixLength );
1259 # eh?
1260 $pref2 = str_replace( ';', ':', $pref );
1261 $t = substr( $oLine, $prefixLength );
1262 } else {
1263 # Don't interpret any other prefixes in preformatted text
1264 $prefixLength = 0;
1265 $pref = $pref2 = '';
1266 $t = $oLine;
1269 # List generation
1270 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1271 # Same as the last item, so no need to deal with nesting or opening stuff
1272 $output .= $this->nextItem( substr( $pref, -1 ) );
1273 $paragraphStack = false;
1275 if ( ";" == substr( $pref, -1 ) ) {
1276 # The one nasty exception: definition lists work like this:
1277 # ; title : definition text
1278 # So we check for : in the remainder text to split up the
1279 # title and definition, without b0rking links.
1280 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1281 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1282 $term = $match[1];
1283 $output .= $term . $this->nextItem( ':' );
1284 $t = $match[2];
1287 } elseif( $prefixLength || $lastPrefixLength ) {
1288 # Either open or close a level...
1289 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1290 $paragraphStack = false;
1292 while( $commonPrefixLength < $lastPrefixLength ) {
1293 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1294 --$lastPrefixLength;
1296 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1297 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1299 while ( $prefixLength > $commonPrefixLength ) {
1300 $char = substr( $pref, $commonPrefixLength, 1 );
1301 $output .= $this->openList( $char );
1303 if ( ';' == $char ) {
1304 # FIXME: This is dupe of code above
1305 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1306 $term = $match[1];
1307 $output .= $term . $this->nextItem( ":" );
1308 $t = $match[2];
1311 ++$commonPrefixLength;
1313 $lastPrefix = $pref2;
1315 if( 0 == $prefixLength ) {
1316 # No prefix (not in list)--go to paragraph mode
1317 $uniq_prefix = UNIQ_PREFIX;
1318 // XXX: use a stack for nestable elements like span, table and div
1319 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1320 $closematch = preg_match(
1321 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1322 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1323 if ( $openmatch or $closematch ) {
1324 $paragraphStack = false;
1325 $output .= $this->closeParagraph();
1326 if($preOpenMatch and !$preCloseMatch) {
1327 $this->mInPre = true;
1329 if ( $closematch ) {
1330 $inBlockElem = false;
1331 } else {
1332 $inBlockElem = true;
1334 } else if ( !$inBlockElem && !$this->mInPre ) {
1335 if ( " " == $t{0} and trim($t) != '' ) {
1336 // pre
1337 if ($this->mLastSection != 'pre') {
1338 $paragraphStack = false;
1339 $output .= $this->closeParagraph().'<pre>';
1340 $this->mLastSection = 'pre';
1342 } else {
1343 // paragraph
1344 if ( '' == trim($t) ) {
1345 if ( $paragraphStack ) {
1346 $output .= $paragraphStack.'<br />';
1347 $paragraphStack = false;
1348 $this->mLastSection = 'p';
1349 } else {
1350 if ($this->mLastSection != 'p' ) {
1351 $output .= $this->closeParagraph();
1352 $this->mLastSection = '';
1353 $paragraphStack = '<p>';
1354 } else {
1355 $paragraphStack = '</p><p>';
1358 } else {
1359 if ( $paragraphStack ) {
1360 $output .= $paragraphStack;
1361 $paragraphStack = false;
1362 $this->mLastSection = 'p';
1363 } else if ($this->mLastSection != 'p') {
1364 $output .= $this->closeParagraph().'<p>';
1365 $this->mLastSection = 'p';
1371 if ($paragraphStack === false) {
1372 $output .= $t."\n";
1375 while ( $prefixLength ) {
1376 $output .= $this->closeList( $pref2{$prefixLength-1} );
1377 --$prefixLength;
1379 if ( '' != $this->mLastSection ) {
1380 $output .= '</' . $this->mLastSection . '>';
1381 $this->mLastSection = '';
1384 wfProfileOut( $fname );
1385 return $output;
1388 # Return value of a magic variable (like PAGENAME)
1389 function getVariableValue( $index ) {
1390 global $wgLang, $wgSitename, $wgServer;
1392 switch ( $index ) {
1393 case MAG_CURRENTMONTH:
1394 return date( 'm' );
1395 case MAG_CURRENTMONTHNAME:
1396 return $wgLang->getMonthName( date('n') );
1397 case MAG_CURRENTMONTHNAMEGEN:
1398 return $wgLang->getMonthNameGen( date('n') );
1399 case MAG_CURRENTDAY:
1400 return date('j');
1401 case MAG_PAGENAME:
1402 return $this->mTitle->getText();
1403 case MAG_NAMESPACE:
1404 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1405 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1406 case MAG_CURRENTDAYNAME:
1407 return $wgLang->getWeekdayName( date('w')+1 );
1408 case MAG_CURRENTYEAR:
1409 return date( 'Y' );
1410 case MAG_CURRENTTIME:
1411 return $wgLang->time( wfTimestampNow(), false );
1412 case MAG_NUMBEROFARTICLES:
1413 return wfNumberOfArticles();
1414 case MAG_SITENAME:
1415 return $wgSitename;
1416 case MAG_SERVER:
1417 return $wgServer;
1418 default:
1419 return NULL;
1423 # initialise the magic variables (like CURRENTMONTHNAME)
1424 function initialiseVariables()
1426 global $wgVariableIDs;
1427 $this->mVariables = array();
1428 foreach ( $wgVariableIDs as $id ) {
1429 $mw =& MagicWord::get( $id );
1430 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1434 /* private */ function replaceVariables( $text, $args = array() )
1436 global $wgLang, $wgScript, $wgArticlePath;
1438 $fname = 'Parser::replaceVariables';
1439 wfProfileIn( $fname );
1441 $bail = false;
1442 if ( !$this->mVariables ) {
1443 $this->initialiseVariables();
1445 $titleChars = Title::legalChars();
1446 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1448 # This function is called recursively. To keep track of arguments we need a stack:
1449 array_push( $this->mArgStack, $args );
1451 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1452 $GLOBALS['wgCurParser'] =& $this;
1455 if ( $this->mOutputType == OT_HTML ) {
1456 # Variable substitution
1457 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1459 # Argument substitution
1460 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1462 # Template substitution
1463 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1464 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1466 array_pop( $this->mArgStack );
1468 wfProfileOut( $fname );
1469 return $text;
1472 function variableSubstitution( $matches )
1474 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1475 $text = $this->mVariables[$matches[1]];
1476 $this->mOutput->mContainsOldMagic = true;
1477 } else {
1478 $text = $matches[0];
1480 return $text;
1483 function braceSubstitution( $matches )
1485 global $wgLinkCache, $wgLang;
1486 $fname = 'Parser::braceSubstitution';
1487 $found = false;
1488 $nowiki = false;
1489 $noparse = false;
1491 $title = NULL;
1493 # $newline is an optional newline character before the braces
1494 # $part1 is the bit before the first |, and must contain only title characters
1495 # $args is a list of arguments, starting from index 0, not including $part1
1497 $newline = $matches[1];
1498 $part1 = $matches[2];
1499 # If the third subpattern matched anything, it will start with |
1500 if ( $matches[3] !== '' ) {
1501 $args = explode( '|', substr( $matches[3], 1 ) );
1502 } else {
1503 $args = array();
1505 $argc = count( $args );
1507 # {{{}}}
1508 if ( strpos( $matches[0], '{{{' ) !== false ) {
1509 $text = $matches[0];
1510 $found = true;
1511 $noparse = true;
1514 # SUBST
1515 if ( !$found ) {
1516 $mwSubst =& MagicWord::get( MAG_SUBST );
1517 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1518 if ( $this->mOutputType != OT_WIKI ) {
1519 # Invalid SUBST not replaced at PST time
1520 # Return without further processing
1521 $text = $matches[0];
1522 $found = true;
1523 $noparse= true;
1525 } elseif ( $this->mOutputType == OT_WIKI ) {
1526 # SUBST not found in PST pass, do nothing
1527 $text = $matches[0];
1528 $found = true;
1532 # MSG, MSGNW and INT
1533 if ( !$found ) {
1534 # Check for MSGNW:
1535 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1536 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1537 $nowiki = true;
1538 } else {
1539 # Remove obsolete MSG:
1540 $mwMsg =& MagicWord::get( MAG_MSG );
1541 $mwMsg->matchStartAndRemove( $part1 );
1544 # Check if it is an internal message
1545 $mwInt =& MagicWord::get( MAG_INT );
1546 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1547 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1548 $text = wfMsgReal( $part1, $args, true );
1549 $found = true;
1554 # NS
1555 if ( !$found ) {
1556 # Check for NS: (namespace expansion)
1557 $mwNs = MagicWord::get( MAG_NS );
1558 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1559 if ( intval( $part1 ) ) {
1560 $text = $wgLang->getNsText( intval( $part1 ) );
1561 $found = true;
1562 } else {
1563 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1564 if ( !is_null( $index ) ) {
1565 $text = $wgLang->getNsText( $index );
1566 $found = true;
1572 # LOCALURL and LOCALURLE
1573 if ( !$found ) {
1574 $mwLocal = MagicWord::get( MAG_LOCALURL );
1575 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1577 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1578 $func = 'getLocalURL';
1579 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1580 $func = 'escapeLocalURL';
1581 } else {
1582 $func = '';
1585 if ( $func !== '' ) {
1586 $title = Title::newFromText( $part1 );
1587 if ( !is_null( $title ) ) {
1588 if ( $argc > 0 ) {
1589 $text = $title->$func( $args[0] );
1590 } else {
1591 $text = $title->$func();
1593 $found = true;
1598 # Internal variables
1599 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1600 $text = $this->mVariables[$part1];
1601 $found = true;
1602 $this->mOutput->mContainsOldMagic = true;
1605 # Arguments input from the caller
1606 $inputArgs = end( $this->mArgStack );
1607 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1608 $text = $inputArgs[$part1];
1609 $found = true;
1612 # Load from database
1613 if ( !$found ) {
1614 $title = Title::newFromText( $part1, NS_TEMPLATE );
1615 if ( !is_null( $title ) && !$title->isExternal() ) {
1616 # Check for excessive inclusion
1617 $dbk = $title->getPrefixedDBkey();
1618 if ( $this->incrementIncludeCount( $dbk ) ) {
1619 $article = new Article( $title );
1620 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1621 if ( $articleContent !== false ) {
1622 $found = true;
1623 $text = $articleContent;
1628 # If the title is valid but undisplayable, make a link to it
1629 if ( $this->mOutputType == OT_HTML && !$found ) {
1630 $text = '[[' . $title->getPrefixedText() . ']]';
1631 $found = true;
1636 # Recursive parsing, escaping and link table handling
1637 # Only for HTML output
1638 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1639 $text = wfEscapeWikiText( $text );
1640 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1641 # Clean up argument array
1642 $assocArgs = array();
1643 $index = 1;
1644 foreach( $args as $arg ) {
1645 $eqpos = strpos( $arg, '=' );
1646 if ( $eqpos === false ) {
1647 $assocArgs[$index++] = $arg;
1648 } else {
1649 $name = trim( substr( $arg, 0, $eqpos ) );
1650 $value = trim( substr( $arg, $eqpos+1 ) );
1651 if ( $value === false ) {
1652 $value = '';
1654 if ( $name !== false ) {
1655 $assocArgs[$name] = $value;
1660 # Do not enter included links in link table
1661 if ( !is_null( $title ) ) {
1662 $wgLinkCache->suspend();
1665 # Run full parser on the included text
1666 $text = $this->stripParse( $text, $newline, $assocArgs );
1668 # Resume the link cache and register the inclusion as a link
1669 if ( !is_null( $title ) ) {
1670 $wgLinkCache->resume();
1671 $wgLinkCache->addLinkObj( $title );
1675 if ( !$found ) {
1676 return $matches[0];
1677 } else {
1678 return $text;
1682 # Triple brace replacement -- used for template arguments
1683 function argSubstitution( $matches )
1685 $newline = $matches[1];
1686 $arg = trim( $matches[2] );
1687 $text = $matches[0];
1688 $inputArgs = end( $this->mArgStack );
1690 if ( array_key_exists( $arg, $inputArgs ) ) {
1691 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1694 return $text;
1697 # Returns true if the function is allowed to include this entity
1698 function incrementIncludeCount( $dbk )
1700 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1701 $this->mIncludeCount[$dbk] = 0;
1703 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1704 return true;
1705 } else {
1706 return false;
1711 # Cleans up HTML, removes dangerous tags and attributes
1712 /* private */ function removeHTMLtags( $text )
1714 global $wgUseTidy, $wgUserHtml;
1715 $fname = 'Parser::removeHTMLtags';
1716 wfProfileIn( $fname );
1718 if( $wgUserHtml ) {
1719 $htmlpairs = array( # Tags that must be closed
1720 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1721 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1722 'strike', 'strong', 'tt', 'var', 'div', 'center',
1723 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1724 'ruby', 'rt' , 'rb' , 'rp', 'p'
1726 $htmlsingle = array(
1727 'br', 'hr', 'li', 'dt', 'dd'
1729 $htmlnest = array( # Tags that can be nested--??
1730 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1731 'dl', 'font', 'big', 'small', 'sub', 'sup'
1733 $tabletags = array( # Can only appear inside table
1734 'td', 'th', 'tr'
1736 } else {
1737 $htmlpairs = array();
1738 $htmlsingle = array();
1739 $htmlnest = array();
1740 $tabletags = array();
1743 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1744 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1746 $htmlattrs = $this->getHTMLattrs () ;
1748 # Remove HTML comments
1749 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1751 $bits = explode( '<', $text );
1752 $text = array_shift( $bits );
1753 if(!$wgUseTidy) {
1754 $tagstack = array(); $tablestack = array();
1755 foreach ( $bits as $x ) {
1756 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1757 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1758 $x, $regs );
1759 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1760 error_reporting( $prev );
1762 $badtag = 0 ;
1763 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1764 # Check our stack
1765 if ( $slash ) {
1766 # Closing a tag...
1767 if ( ! in_array( $t, $htmlsingle ) &&
1768 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1769 @array_push( $tagstack, $ot );
1770 $badtag = 1;
1771 } else {
1772 if ( $t == 'table' ) {
1773 $tagstack = array_pop( $tablestack );
1775 $newparams = '';
1777 } else {
1778 # Keep track for later
1779 if ( in_array( $t, $tabletags ) &&
1780 ! in_array( 'table', $tagstack ) ) {
1781 $badtag = 1;
1782 } else if ( in_array( $t, $tagstack ) &&
1783 ! in_array ( $t , $htmlnest ) ) {
1784 $badtag = 1 ;
1785 } else if ( ! in_array( $t, $htmlsingle ) ) {
1786 if ( $t == 'table' ) {
1787 array_push( $tablestack, $tagstack );
1788 $tagstack = array();
1790 array_push( $tagstack, $t );
1792 # Strip non-approved attributes from the tag
1793 $newparams = $this->fixTagAttributes($params);
1796 if ( ! $badtag ) {
1797 $rest = str_replace( '>', '&gt;', $rest );
1798 $text .= "<$slash$t $newparams$brace$rest";
1799 continue;
1802 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1804 # Close off any remaining tags
1805 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1806 $text .= "</$t>\n";
1807 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1809 } else {
1810 # this might be possible using tidy itself
1811 foreach ( $bits as $x ) {
1812 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1813 $x, $regs );
1814 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1815 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1816 $newparams = $this->fixTagAttributes($params);
1817 $rest = str_replace( '>', '&gt;', $rest );
1818 $text .= "<$slash$t $newparams$brace$rest";
1819 } else {
1820 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
1824 wfProfileOut( $fname );
1825 return $text;
1831 * This function accomplishes several tasks:
1832 * 1) Auto-number headings if that option is enabled
1833 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1834 * 3) Add a Table of contents on the top for users who have enabled the option
1835 * 4) Auto-anchor headings
1837 * It loops through all headlines, collects the necessary data, then splits up the
1838 * string and re-inserts the newly formatted headlines.
1842 /* private */ function formatHeadings( $text, $isMain=true )
1844 global $wgInputEncoding;
1846 $doNumberHeadings = $this->mOptions->getNumberHeadings();
1847 $doShowToc = $this->mOptions->getShowToc();
1848 if( !$this->mTitle->userCanEdit() ) {
1849 $showEditLink = 0;
1850 $rightClickHack = 0;
1851 } else {
1852 $showEditLink = $this->mOptions->getEditSection();
1853 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
1856 # Inhibit editsection links if requested in the page
1857 $esw =& MagicWord::get( MAG_NOEDITSECTION );
1858 if( $esw->matchAndRemove( $text ) ) {
1859 $showEditLink = 0;
1861 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1862 # do not add TOC
1863 $mw =& MagicWord::get( MAG_NOTOC );
1864 if( $mw->matchAndRemove( $text ) ) {
1865 $doShowToc = 0;
1868 # never add the TOC to the Main Page. This is an entry page that should not
1869 # be more than 1-2 screens large anyway
1870 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
1871 $doShowToc = 0;
1874 # Get all headlines for numbering them and adding funky stuff like [edit]
1875 # links - this is for later, but we need the number of headlines right now
1876 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1878 # if there are fewer than 4 headlines in the article, do not show TOC
1879 if( $numMatches < 4 ) {
1880 $doShowToc = 0;
1883 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1884 # override above conditions and always show TOC
1885 $mw =& MagicWord::get( MAG_FORCETOC );
1886 if ($mw->matchAndRemove( $text ) ) {
1887 $doShowToc = 1;
1891 # We need this to perform operations on the HTML
1892 $sk =& $this->mOptions->getSkin();
1894 # headline counter
1895 $headlineCount = 0;
1897 # Ugh .. the TOC should have neat indentation levels which can be
1898 # passed to the skin functions. These are determined here
1899 $toclevel = 0;
1900 $toc = '';
1901 $full = '';
1902 $head = array();
1903 $sublevelCount = array();
1904 $level = 0;
1905 $prevlevel = 0;
1906 foreach( $matches[3] as $headline ) {
1907 $numbering = '';
1908 if( $level ) {
1909 $prevlevel = $level;
1911 $level = $matches[1][$headlineCount];
1912 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
1913 # reset when we enter a new level
1914 $sublevelCount[$level] = 0;
1915 $toc .= $sk->tocIndent( $level - $prevlevel );
1916 $toclevel += $level - $prevlevel;
1918 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
1919 # reset when we step back a level
1920 $sublevelCount[$level+1]=0;
1921 $toc .= $sk->tocUnindent( $prevlevel - $level );
1922 $toclevel -= $prevlevel - $level;
1924 # count number of headlines for each level
1925 @$sublevelCount[$level]++;
1926 if( $doNumberHeadings || $doShowToc ) {
1927 $dot = 0;
1928 for( $i = 1; $i <= $level; $i++ ) {
1929 if( !empty( $sublevelCount[$i] ) ) {
1930 if( $dot ) {
1931 $numbering .= '.';
1933 $numbering .= $sublevelCount[$i];
1934 $dot = 1;
1939 # The canonized header is a version of the header text safe to use for links
1940 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1941 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
1942 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
1944 # strip out HTML
1945 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1946 $tocline = trim( $canonized_headline );
1947 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
1948 $replacearray = array(
1949 '%3A' => ':',
1950 '%' => '.'
1952 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1953 $refer[$headlineCount] = $canonized_headline;
1955 # count how many in assoc. array so we can track dupes in anchors
1956 @$refers[$canonized_headline]++;
1957 $refcount[$headlineCount]=$refers[$canonized_headline];
1959 # Prepend the number to the heading text
1961 if( $doNumberHeadings || $doShowToc ) {
1962 $tocline = $numbering . ' ' . $tocline;
1964 # Don't number the heading if it is the only one (looks silly)
1965 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1966 # the two are different if the line contains a link
1967 $headline=$numbering . ' ' . $headline;
1971 # Create the anchor for linking from the TOC to the section
1972 $anchor = $canonized_headline;
1973 if($refcount[$headlineCount] > 1 ) {
1974 $anchor .= '_' . $refcount[$headlineCount];
1976 if( $doShowToc ) {
1977 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1979 if( $showEditLink ) {
1980 if ( empty( $head[$headlineCount] ) ) {
1981 $head[$headlineCount] = '';
1983 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
1986 # Add the edit section span
1987 if( $rightClickHack ) {
1988 $headline = $sk->editSectionScript($headlineCount+1,$headline);
1991 # give headline the correct <h#> tag
1992 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
1994 $headlineCount++;
1997 if( $doShowToc ) {
1998 $toclines = $headlineCount;
1999 $toc .= $sk->tocUnindent( $toclevel );
2000 $toc = $sk->tocTable( $toc );
2003 # split up and insert constructed headlines
2005 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2006 $i = 0;
2008 foreach( $blocks as $block ) {
2009 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2010 # This is the [edit] link that appears for the top block of text when
2011 # section editing is enabled
2013 # Disabled because it broke block formatting
2014 # For example, a bullet point in the top line
2015 # $full .= $sk->editSectionLink(0);
2017 $full .= $block;
2018 if( $doShowToc && !$i && $isMain) {
2019 # Top anchor now in skin
2020 $full = $full.$toc;
2023 if( !empty( $head[$i] ) ) {
2024 $full .= $head[$i];
2026 $i++;
2029 return $full;
2032 # Return an HTML link for the "ISBN 123456" text
2033 /* private */ function magicISBN( $text )
2035 global $wgLang;
2037 $a = split( 'ISBN ', " $text" );
2038 if ( count ( $a ) < 2 ) return $text;
2039 $text = substr( array_shift( $a ), 1);
2040 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2042 foreach ( $a as $x ) {
2043 $isbn = $blank = '' ;
2044 while ( ' ' == $x{0} ) {
2045 $blank .= ' ';
2046 $x = substr( $x, 1 );
2048 while ( strstr( $valid, $x{0} ) != false ) {
2049 $isbn .= $x{0};
2050 $x = substr( $x, 1 );
2052 $num = str_replace( '-', '', $isbn );
2053 $num = str_replace( ' ', '', $num );
2055 if ( '' == $num ) {
2056 $text .= "ISBN $blank$x";
2057 } else {
2058 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2059 $text .= '<a href="' .
2060 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2061 "\" class=\"internal\">ISBN $isbn</a>";
2062 $text .= $x;
2065 return $text;
2068 # Return an HTML link for the "RFC 1234" text
2069 /* private */ function magicRFC( $text )
2071 global $wgLang;
2073 $a = split( 'RFC ', ' '.$text );
2074 if ( count ( $a ) < 2 ) return $text;
2075 $text = substr( array_shift( $a ), 1);
2076 $valid = '0123456789';
2078 foreach ( $a as $x ) {
2079 $rfc = $blank = '' ;
2080 while ( ' ' == $x{0} ) {
2081 $blank .= ' ';
2082 $x = substr( $x, 1 );
2084 while ( strstr( $valid, $x{0} ) != false ) {
2085 $rfc .= $x{0};
2086 $x = substr( $x, 1 );
2089 if ( '' == $rfc ) {
2090 $text .= "RFC $blank$x";
2091 } else {
2092 $url = wfmsg( 'rfcurl' );
2093 $url = str_replace( '$1', $rfc, $url);
2094 $sk =& $this->mOptions->getSkin();
2095 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2096 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2099 return $text;
2102 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true )
2104 $this->mOptions = $options;
2105 $this->mTitle =& $title;
2106 $this->mOutputType = OT_WIKI;
2108 if ( $clearState ) {
2109 $this->clearState();
2112 $stripState = false;
2113 $pairs = array(
2114 "\r\n" => "\n",
2116 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2117 // now with regexes
2119 $pairs = array(
2120 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2121 "/<br *?>/i" => "<br />",
2123 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2125 $text = $this->strip( $text, $stripState, false );
2126 $text = $this->pstPass2( $text, $user );
2127 $text = $this->unstrip( $text, $stripState );
2128 $text = $this->unstripNoWiki( $text, $stripState );
2129 return $text;
2132 /* private */ function pstPass2( $text, &$user )
2134 global $wgLang, $wgLocaltimezone, $wgCurParser;
2136 # Variable replacement
2137 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2138 $text = $this->replaceVariables( $text );
2140 # Signatures
2142 $n = $user->getName();
2143 $k = $user->getOption( 'nickname' );
2144 if ( '' == $k ) { $k = $n; }
2145 if(isset($wgLocaltimezone)) {
2146 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2148 /* Note: this is an ugly timezone hack for the European wikis */
2149 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2150 ' (' . date( 'T' ) . ')';
2151 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2153 $text = preg_replace( '/~~~~~/', $d, $text );
2154 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2155 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2156 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2157 Namespace::getUser() ) . ":$n|$k]]", $text );
2159 # Context links: [[|name]] and [[name (context)|]]
2161 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2162 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2163 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2164 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2166 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2167 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2168 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2169 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2170 # [[ns:page (cont)|]]
2171 $context = "";
2172 $t = $this->mTitle->getText();
2173 if ( preg_match( $conpat, $t, $m ) ) {
2174 $context = $m[2];
2176 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2177 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2178 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2180 if ( '' == $context ) {
2181 $text = preg_replace( $p2, '[[\\1]]', $text );
2182 } else {
2183 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2187 $mw =& MagicWord::get( MAG_SUBST );
2188 $wgCurParser = $this->fork();
2189 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2190 $this->merge( $wgCurParser );
2193 # Trim trailing whitespace
2194 # MAG_END (__END__) tag allows for trailing
2195 # whitespace to be deliberately included
2196 $text = rtrim( $text );
2197 $mw =& MagicWord::get( MAG_END );
2198 $mw->matchAndRemove( $text );
2200 return $text;
2203 # Set up some variables which are usually set up in parse()
2204 # so that an external function can call some class members with confidence
2205 function startExternalParse( &$title, $options, $outputType, $clearState = true )
2207 $this->mTitle =& $title;
2208 $this->mOptions = $options;
2209 $this->mOutputType = $outputType;
2210 if ( $clearState ) {
2211 $this->clearState();
2215 function transformMsg( $text, $options ) {
2216 global $wgTitle;
2217 static $executing = false;
2219 # Guard against infinite recursion
2220 if ( $executing ) {
2221 return $text;
2223 $executing = true;
2225 $this->mTitle = $wgTitle;
2226 $this->mOptions = $options;
2227 $this->mOutputType = OT_MSG;
2228 $this->clearState();
2229 $text = $this->replaceVariables( $text );
2231 $executing = false;
2232 return $text;
2235 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2236 # Callback will be called with the text within
2237 # Transform and return the text within
2238 /* static */ function setHook( $tag, $callback ) {
2239 global $wgParserHooks;
2240 $oldVal = @$wgParserHooks[$tag];
2241 $wgParserHooks[$tag] = $callback;
2242 return $oldVal;
2246 class ParserOutput
2248 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2249 var $mCacheTime; # Used in ParserCache
2251 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2252 $containsOldMagic = false )
2254 $this->mText = $text;
2255 $this->mLanguageLinks = $languageLinks;
2256 $this->mCategoryLinks = $categoryLinks;
2257 $this->mContainsOldMagic = $containsOldMagic;
2258 $this->mCacheTime = "";
2261 function getText() { return $this->mText; }
2262 function getLanguageLinks() { return $this->mLanguageLinks; }
2263 function getCategoryLinks() { return $this->mCategoryLinks; }
2264 function getCacheTime() { return $this->mCacheTime; }
2265 function containsOldMagic() { return $this->mContainsOldMagic; }
2266 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2267 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2268 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2269 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2270 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2272 function merge( $other ) {
2273 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2274 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2275 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2280 class ParserOptions
2282 # All variables are private
2283 var $mUseTeX; # Use texvc to expand <math> tags
2284 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2285 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2286 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2287 var $mAllowExternalImages; # Allow external images inline
2288 var $mSkin; # Reference to the preferred skin
2289 var $mDateFormat; # Date format index
2290 var $mEditSection; # Create "edit section" links
2291 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2292 var $mNumberHeadings; # Automatically number headings
2293 var $mShowToc; # Show table of contents
2295 function getUseTeX() { return $this->mUseTeX; }
2296 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2297 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2298 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2299 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2300 function getSkin() { return $this->mSkin; }
2301 function getDateFormat() { return $this->mDateFormat; }
2302 function getEditSection() { return $this->mEditSection; }
2303 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2304 function getNumberHeadings() { return $this->mNumberHeadings; }
2305 function getShowToc() { return $this->mShowToc; }
2307 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2308 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2309 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2310 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2311 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2312 function setSkin( $x ) { return wfSetRef( $this->mSkin, $x ); }
2313 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2314 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2315 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2316 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2317 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2319 /* static */ function newFromUser( &$user )
2321 $popts = new ParserOptions;
2322 $popts->initialiseFromUser( $user );
2323 return $popts;
2326 function initialiseFromUser( &$userInput )
2328 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2330 if ( !$userInput ) {
2331 $user = new User;
2332 $user->setLoaded( true );
2333 } else {
2334 $user =& $userInput;
2337 $this->mUseTeX = $wgUseTeX;
2338 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2339 $this->mUseDynamicDates = $wgUseDynamicDates;
2340 $this->mInterwikiMagic = $wgInterwikiMagic;
2341 $this->mAllowExternalImages = $wgAllowExternalImages;
2342 $this->mSkin =& $user->getSkin();
2343 $this->mDateFormat = $user->getOption( 'date' );
2344 $this->mEditSection = $user->getOption( 'editsection' );
2345 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2346 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2347 $this->mShowToc = $user->getOption( 'showtoc' );
2353 # Regex callbacks, used in Parser::replaceVariables
2354 function wfBraceSubstitution( $matches )
2356 global $wgCurParser;
2357 return $wgCurParser->braceSubstitution( $matches );
2360 function wfArgSubstitution( $matches )
2362 global $wgCurParser;
2363 return $wgCurParser->argSubstitution( $matches );
2366 function wfVariableSubstitution( $matches )
2368 global $wgCurParser;
2369 return $wgCurParser->variableSubstitution( $matches );