a0e8649dd4eb6303d1b4a3d9a97758d095089859
3 // require_once('Tokenizer.php');
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.
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*,
21 # * only within ParserOptions
24 #----------------------------------------
25 # Variable substitution O(N^2) attack
26 #-----------------------------------------
27 # Without countermeasures, it would be possible to attack the parser by saving a page
28 # filled with a large number of inclusions of large pages. The size of the generated
29 # page would be proportional to the square of the input size. Hence, we limit the number
30 # of inclusions of any given page, thus bringing any attack back to O(N).
33 define( "MAX_INCLUDE_REPEAT", 5 );
34 define( "MAX_INCLUDE_SIZE", 1000000 ); // 1 Million
36 # Allowed values for $mOutputType
37 define( "OT_HTML", 1 );
38 define( "OT_WIKI", 2 );
39 define( "OT_MSG", 3 );
41 # string parameter for extractTags which will cause it
42 # to strip HTML comments in addition to regular
43 # <XML>-style tags. This should not be anything we
44 # may want to use in wikisyntax
45 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
47 # prefix for escaping, used in two functions at least
48 define( "UNIQ_PREFIX", "NaodW29");
55 # Cleared with clearState():
56 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
57 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
60 var $mOptions, $mTitle, $mOutputType,
61 $mTemplates, // cache of already loaded templates, avoids
62 // multiple SQL queries for the same string
63 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
64 // in this path. Used for loop detection.
67 $this->mTemplates
= array();
68 $this->mTemplatePath
= array();
69 $this->mTagHooks
= array();
73 function clearState() {
74 $this->mOutput
= new ParserOutput
;
75 $this->mAutonumber
= 0;
76 $this->mLastSection
= "";
77 $this->mDTopen
= false;
78 $this->mVariables
= false;
79 $this->mIncludeCount
= array();
80 $this->mStripState
= array();
81 $this->mArgStack
= array();
82 $this->mInPre
= false;
85 # First pass--just handle <nowiki> sections, pass the rest off
86 # to internalParse() which does all the real work.
88 # Returns a ParserOutput
90 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
92 $fname = "Parser::parse";
93 wfProfileIn( $fname );
99 $this->mOptions
= $options;
100 $this->mTitle
=& $title;
101 $this->mOutputType
= OT_HTML
;
104 $text = $this->strip( $text, $this->mStripState
);
105 $text = $this->internalParse( $text, $linestart );
106 $text = $this->unstrip( $text, $this->mStripState
);
107 # Clean up special characters, only run once, next-to-last before doBlockLevels
110 # french spaces, last one Guillemet-left
111 # only if there is something before the space
112 '/(.) (\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
113 # french spaces, Guillemet-right
114 "/(\\302\\253) /i"=>"\\1 ",
115 '/<hr *>/i' => '<hr />',
116 '/<br *>/i' => '<br />',
117 '/<center *>/i' => '<div class="center">',
118 '/<\\/center *>/i' => '</div>',
119 # Clean up spare ampersands; note that we probably ought to be
120 # more careful about named entities.
121 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
123 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
126 # french spaces, last one Guillemet-left
127 '/ (\\?|:|!|\\302\\273)/i' => ' \\1',
128 # french spaces, Guillemet-right
129 '/(\\302\\253) /i' => '\\1 ',
130 '/([^> ]+(0(1|3|9);)[^< ]*)/i' => '<span class="diacrit">\\1</span>',
131 '/<center *>/i' => '<div class="center">',
132 '/<\\/center *>/i' => '</div>'
134 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
137 $text = $this->doBlockLevels( $text, $linestart );
138 $text = $this->unstripNoWiki( $text, $this->mStripState
);
140 $text = $this->tidy($text);
142 $this->mOutput
->setText( $text );
143 wfProfileOut( $fname );
144 return $this->mOutput
;
147 /* static */ function getRandomString() {
148 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
151 # Replaces all occurrences of <$tag>content</$tag> in the text
152 # with a random marker and returns the new text. the output parameter
153 # $content will be an associative array filled with data on the form
154 # $unique_marker => content.
156 # If $content is already set, the additional entries will be appended
158 # If $tag is set to STRIP_COMMENTS, the function will extract
159 # <!-- HTML comments -->
161 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
162 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
169 while ( '' != $text ) {
170 if($tag==STRIP_COMMENTS
) {
171 $p = preg_split( '/<!--/i', $text, 2 );
173 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
176 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
179 if($tag==STRIP_COMMENTS
) {
180 $q = preg_split( '/-->/i', $p[1], 2 );
182 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
184 $marker = $rnd . sprintf('%08X', $n++
);
185 $content[$marker] = $q[0];
186 $stripped .= $marker;
193 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
194 # If $render is set, performs necessary rendering operations on plugins
195 # Returns the text, and fills an array with data needed in unstrip()
196 # If the $state is already a valid strip state, it adds to the state
198 # When $stripcomments is set, HTML comments <!-- like this -->
199 # will be stripped in addition to other tags. This is important
200 # for section editing, where these comments cause confusion when
201 # counting the sections in the wikisource
202 function strip( $text, &$state, $stripcomments = false ) {
203 $render = ($this->mOutputType
== OT_HTML
);
204 $html_content = array();
205 $nowiki_content = array();
206 $math_content = array();
207 $pre_content = array();
208 $comment_content = array();
209 $ext_content = array();
211 # Replace any instances of the placeholders
212 $uniq_prefix = UNIQ_PREFIX
;
213 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
218 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
219 foreach( $html_content as $marker => $content ) {
221 # Raw and unchecked for validity.
222 $html_content[$marker] = $content;
224 $html_content[$marker] = "<html>$content</html>";
230 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
231 foreach( $nowiki_content as $marker => $content ) {
233 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
235 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
240 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
241 foreach( $math_content as $marker => $content ){
243 if( $this->mOptions
->getUseTeX() ) {
244 $math_content[$marker] = renderMath( $content );
246 $math_content[$marker] = "<math>$content<math>";
249 $math_content[$marker] = "<math>$content</math>";
254 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
255 foreach( $pre_content as $marker => $content ){
257 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
259 $pre_content[$marker] = "<pre>$content</pre>";
265 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
266 foreach( $comment_content as $marker => $content ){
267 $comment_content[$marker] = "<!--$content-->";
272 foreach ( $this->mTagHooks
as $tag => $callback ) {
273 $ext_contents[$tag] = array();
274 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
275 foreach( $ext_content[$tag] as $marker => $content ) {
277 $ext_content[$tag][$marker] = $callback( $content );
279 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
284 # Merge state with the pre-existing state, if there is one
286 $state['html'] = $state['html'] +
$html_content;
287 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
288 $state['math'] = $state['math'] +
$math_content;
289 $state['pre'] = $state['pre'] +
$pre_content;
290 $state['comment'] = $state['comment'] +
$comment_content;
292 foreach( $ext_content as $tag => $array ) {
293 if ( array_key_exists( $tag, $state ) ) {
294 $state[$tag] = $state[$tag] +
$array;
299 'html' => $html_content,
300 'nowiki' => $nowiki_content,
301 'math' => $math_content,
302 'pre' => $pre_content,
303 'comment' => $comment_content,
309 # always call unstripNoWiki() after this one
310 function unstrip( $text, &$state ) {
311 # Must expand in reverse order, otherwise nested tags will be corrupted
312 $contentDict = end( $state );
313 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
314 if( key($state) != 'nowiki' && key($state) != 'html') {
315 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
316 $text = str_replace( key( $contentDict ), $content, $text );
323 # always call this after unstrip() to preserve the order
324 function unstripNoWiki( $text, &$state ) {
325 # Must expand in reverse order, otherwise nested tags will be corrupted
326 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
327 $text = str_replace( key( $state['nowiki'] ), $content, $text );
332 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
333 $text = str_replace( key( $state['html'] ), $content, $text );
340 # Add an item to the strip state
341 # Returns the unique tag which must be inserted into the stripped text
342 # The tag will be replaced with the original text in unstrip()
344 function insertStripItem( $text, &$state ) {
345 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
354 $state['item'][$rnd] = $text;
359 # generate a list of subcategories and pages for a category
360 # depending on wfMsg("usenewcategorypage") it either calls the new
361 # or the old code. The new code will not work properly for some
362 # languages due to sorting issues, so they might want to turn it
364 function categoryMagic() {
365 $msg = wfMsg('usenewcategorypage');
366 if ( '0' == @$msg[0] )
368 return $this->oldCategoryMagic();
370 return $this->newCategoryMagic();
374 # This method generates the list of subcategories and pages for a category
375 function oldCategoryMagic () {
376 global $wgLang , $wgUser ;
377 $fname = 'Parser::oldCategoryMagic';
379 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
381 if ( $this->mTitle
->getNamespace() != NS_CATEGORY
) return "" ; # This ain't a category page
383 $r = "<br style=\"clear:both;\"/>\n";
386 $sk =& $wgUser->getSkin() ;
388 $articles = array() ;
389 $children = array() ;
391 $id = $this->mTitle
->getArticleID() ;
394 $dbr =& wfGetDB( DB_SLAVE
);
395 $cur = $dbr->tableName( 'cur' );
396 $categorylinks = $dbr->tableName( 'categorylinks' );
398 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
399 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
400 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
401 $res = $dbr->query( $sql, $fname ) ;
402 while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
404 # For all pages that link to this category
405 foreach ( $data AS $x )
407 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
408 if ( $t != "" ) $t .= ":" ;
409 $t .= $x->cur_title
;
411 if ( $x->cur_namespace
== NS_CATEGORY
) {
412 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
414 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
417 $dbr->freeResult ( $res ) ;
419 # Showing subcategories
420 if ( count ( $children ) > 0 ) {
421 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
422 $r .= implode ( ', ' , $children ) ;
425 # Showing pages in this category
426 if ( count ( $articles ) > 0 ) {
427 $ti = $this->mTitle
->getText() ;
428 $h = wfMsg( 'category_header', $ti );
429 $r .= "<h2>{$h}</h2>\n" ;
430 $r .= implode ( ', ' , $articles ) ;
438 function newCategoryMagic () {
439 global $wgLang , $wgUser ;
440 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
442 if ( $this->mTitle
->getNamespace() != NS_CATEGORY
) return '' ; # This ain't a category page
444 $r = "<br style=\"clear:both;\"/>\n";
447 $sk =& $wgUser->getSkin() ;
449 $articles = array() ;
450 $articles_start_char = array();
451 $children = array() ;
452 $children_start_char = array();
454 $id = $this->mTitle
->getArticleID() ;
457 $dbr =& wfGetDB( DB_SLAVE
);
458 $cur = $dbr->tableName( 'cur' );
459 $categorylinks = $dbr->tableName( 'categorylinks' );
461 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
462 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
463 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
464 $res = $dbr->query ( $sql ) ;
465 while ( $x = $dbr->fetchObject ( $res ) )
467 $t = $ns = $wgLang->getNsText ( $x->cur_namespace
) ;
468 if ( $t != '' ) $t .= ':' ;
469 $t .= $x->cur_title
;
471 if ( $x->cur_namespace
== NS_CATEGORY
) {
472 $ctitle = str_replace( '_',' ',$x->cur_title
);
473 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
475 // If there's a link from Category:A to Category:B, the sortkey of the resulting
476 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
477 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
478 // else use sortkey...
479 if ( ($ns.":".$ctitle) == $x->cl_sortkey
) {
480 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title
) );
482 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
485 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
486 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
489 $dbr->freeResult ( $res ) ;
491 $ti = $this->mTitle
->getText() ;
493 # Don't show subcategories section if there are none.
494 if ( count ( $children ) > 0 )
496 # Showing subcategories
497 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
498 . wfMsg( 'subcategorycount', count( $children ) );
499 if ( count ( $children ) > 6 ) {
501 // divide list into three equal chunks
502 $chunk = (int) (count ( $children ) / 3);
504 // get and display header
505 $r .= '<table width="100%"><tr valign="top">';
510 // loop through the chunks
511 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
513 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
517 // output all subcategories to category
518 for ($index = $startChunk ;
519 $index < $endChunk && $index < count($children);
522 // check for change of starting letter or begging of chunk
523 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
524 ||
($index == $startChunk) )
526 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
529 $r .= "<li>{$children[$index]}</li>";
535 $r .= '</tr></table>';
537 // for short lists of subcategories to category.
539 $r .= "<h3>{$children_start_char[0]}</h3>\n";
540 $r .= '<ul><li>'.$children[0].'</li>';
541 for ($index = 1; $index < count($children); $index++
)
543 if ($children_start_char[$index] != $children_start_char[$index - 1])
545 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
548 $r .= "<li>{$children[$index]}</li>";
552 } # END of if ( count($children) > 0 )
554 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
555 wfMsg( 'categoryarticlecount', count( $articles ) );
557 # Showing articles in this category
558 if ( count ( $articles ) > 6) {
559 $ti = $this->mTitle
->getText() ;
561 // divide list into three equal chunks
562 $chunk = (int) (count ( $articles ) / 3);
564 // get and display header
565 $r .= '<table width="100%"><tr valign="top">';
567 // loop through the chunks
568 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
570 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
575 // output all articles in category
576 for ($index = $startChunk ;
577 $index < $endChunk && $index < count($articles);
580 // check for change of starting letter or begging of chunk
581 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
582 ||
($index == $startChunk) )
584 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
587 $r .= "<li>{$articles[$index]}</li>";
593 $r .= '</tr></table>';
594 } elseif ( count ( $articles ) > 0) {
595 // for short lists of articles in categories.
596 $ti = $this->mTitle
->getText() ;
598 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
599 $r .= '<ul><li>'.$articles[0].'</li>';
600 for ($index = 1; $index < count($articles); $index++
)
602 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
604 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
607 $r .= "<li>{$articles[$index]}</li>";
616 # Return allowed HTML attributes
617 function getHTMLattrs () {
618 $htmlattrs = array( # Allowed attributes--no scripting, etc.
619 'title', 'align', 'lang', 'dir', 'width', 'height',
620 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
621 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
622 /* FONT */ 'type', 'start', 'value', 'compact',
623 /* For various lists, mostly deprecated but safe */
624 'summary', 'width', 'border', 'frame', 'rules',
625 'cellspacing', 'cellpadding', 'valign', 'char',
626 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
627 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
628 'id', 'class', 'name', 'style' /* For CSS */
633 # Remove non approved attributes and javascript in css
634 function fixTagAttributes ( $t ) {
635 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
636 $htmlattrs = $this->getHTMLattrs() ;
638 # Strip non-approved attributes from the tag
640 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
641 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
644 $t = str_replace ( "<></>" , "" , $t ) ; # This should fix bug 980557
646 # Strip javascript "expression" from stylesheets. Brute force approach:
647 # If anythin offensive is found, all attributes of the HTML tag are dropped
650 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
651 wfMungeToUtf8( $t ) ) )
659 # interface with html tidy, used if $wgUseTidy = true
660 function tidy ( $text ) {
661 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
662 global $wgInputEncoding, $wgOutputEncoding;
663 $fname = 'Parser::tidy';
664 wfProfileIn( $fname );
667 switch(strtoupper($wgOutputEncoding)) {
669 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
672 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
675 $wgTidyOpts .= ' -raw';
678 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
679 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
680 '<head><title>test</title></head><body>'.$text.'</body></html>';
681 $descriptorspec = array(
682 0 => array('pipe', 'r'),
683 1 => array('pipe', 'w'),
684 2 => array('file', '/dev/null', 'a')
686 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
687 if (is_resource($process)) {
688 fwrite($pipes[0], $wrappedtext);
690 while (!feof($pipes[1])) {
691 $cleansource .= fgets($pipes[1], 1024);
694 $return_value = proc_close($process);
697 wfProfileOut( $fname );
699 if( $cleansource == '' && $text != '') {
700 wfDebug( "Tidy error detected!\n" );
701 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
707 # parse the wiki syntax used to render tables
708 function doTableStuff ( $t ) {
709 $fname = 'Parser::doTableStuff';
710 wfProfileIn( $fname );
712 $t = explode ( "\n" , $t ) ;
713 $td = array () ; # Is currently a td tag open?
714 $ltd = array () ; # Was it TD or TH?
715 $tr = array () ; # Is currently a tr tag open?
716 $ltr = array () ; # tr attributes
717 $indent_level = 0; # indent level of the table
718 foreach ( $t AS $k => $x )
721 $fc = substr ( $x , 0 , 1 ) ;
722 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) )
724 $indent_level = strlen( $matches[1] );
726 str_repeat( "<dl><dd>", $indent_level ) .
727 "<table " . $this->fixTagAttributes ( $matches[2] ) . '>' ;
728 array_push ( $td , false ) ;
729 array_push ( $ltd , '' ) ;
730 array_push ( $tr , false ) ;
731 array_push ( $ltr , '' ) ;
733 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
734 else if ( '|}' == substr ( $x , 0 , 2 ) )
737 $l = array_pop ( $ltd ) ;
738 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
739 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
741 $t[$k] = $z . str_repeat( "</dd></dl>", $indent_level );
743 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
745 $x = substr ( $x , 1 ) ;
746 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
748 $l = array_pop ( $ltd ) ;
749 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
750 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
753 array_push ( $tr , false ) ;
754 array_push ( $td , false ) ;
755 array_push ( $ltd , '' ) ;
756 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
758 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) # Caption
760 if ( '|+' == substr ( $x , 0 , 2 ) )
763 $x = substr ( $x , 1 ) ;
765 $after = substr ( $x , 1 ) ;
766 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
767 $after = explode ( '||' , $after ) ;
769 foreach ( $after AS $theline )
774 $tra = array_pop ( $ltr ) ;
775 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
776 array_push ( $tr , true ) ;
777 array_push ( $ltr , '' ) ;
780 $l = array_pop ( $ltd ) ;
781 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
782 if ( $fc == '|' ) $l = 'td' ;
783 else if ( $fc == '!' ) $l = 'th' ;
784 else if ( $fc == '+' ) $l = 'caption' ;
786 array_push ( $ltd , $l ) ;
787 $y = explode ( '|' , $theline , 2 ) ;
788 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
789 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
791 array_push ( $td , true ) ;
796 # Closing open td, tr && table
797 while ( count ( $td ) > 0 )
799 if ( array_pop ( $td ) ) $t[] = '</td>' ;
800 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
804 $t = implode ( "\n" , $t ) ;
805 # $t = $this->removeHTMLtags( $t );
806 wfProfileOut( $fname );
810 # Parses the text and adds the result to the strip state
811 # Returns the strip tag
812 function stripParse( $text, $newline, $args )
814 $text = $this->strip( $text, $this->mStripState
);
815 $text = $this->internalParse( $text, (bool)$newline, $args, false );
816 return $newline.$this->insertStripItem( $text, $this->mStripState
);
819 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
820 $fname = 'Parser::internalParse';
821 wfProfileIn( $fname );
823 $text = $this->removeHTMLtags( $text );
824 $text = $this->replaceVariables( $text, $args );
826 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
828 $text = $this->doHeadings( $text );
829 if($this->mOptions
->getUseDynamicDates()) {
830 global $wgDateFormatter;
831 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
833 $text = $this->doAllQuotes( $text );
834 $text = $this->replaceExternalLinks( $text );
835 $text = $this->doMagicLinks( $text );
836 $text = $this->replaceInternalLinks ( $text );
837 $text = $this->replaceInternalLinks ( $text );
838 $text = $this->doTableStuff( $text );
839 $text = $this->formatHeadings( $text, $isMain );
840 $sk =& $this->mOptions
->getSkin();
841 $text = $sk->transformContent( $text );
843 if ( $isMain && !isset ( $this->categoryMagicDone
) ) {
844 $text .= $this->categoryMagic () ;
845 $this->categoryMagicDone
= true ;
848 wfProfileOut( $fname );
852 /* private */ function &doMagicLinks( &$text ) {
853 $text = $this->magicISBN( $text );
854 $text = $this->magicGEO( $text );
855 $text = $this->magicRFC( $text );
859 # Parse ^^ tokens and return html
860 /* private */ function doExponent ( $text )
862 $fname = 'Parser::doExponent';
863 wfProfileIn( $fname);
864 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
865 wfProfileOut( $fname);
869 # Parse headers and return html
870 /* private */ function doHeadings( $text ) {
871 $fname = 'Parser::doHeadings';
872 wfProfileIn( $fname );
873 for ( $i = 6; $i >= 1; --$i ) {
874 $h = substr( '======', 0, $i );
875 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
876 "<h{$i}>\\1</h{$i}>\\2", $text );
878 wfProfileOut( $fname );
882 /* private */ function doAllQuotes( $text ) {
883 $fname = 'Parser::doAllQuotes';
884 wfProfileIn( $fname );
886 $lines = explode( "\n", $text );
887 foreach ( $lines as $line ) {
888 $outtext .= $this->doQuotes ( $line ) . "\n";
890 $outtext = substr($outtext, 0,-1);
891 wfProfileOut( $fname );
895 /* private */ function doQuotes( $text ) {
896 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
897 if (count ($arr) == 1)
901 # First, do some preliminary work. This may shift some apostrophes from
902 # being mark-up to being text. It also counts the number of occurrences
903 # of bold and italics mark-ups.
911 # If there are ever four apostrophes, assume the first is supposed to
912 # be text, and the remaining three constitute mark-up for bold text.
913 if (strlen ($arr[$i]) == 4)
918 # If there are more than 5 apostrophes in a row, assume they're all
919 # text except for the last 5.
920 else if (strlen ($arr[$i]) > 5)
922 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
925 # Count the number of occurrences of bold and italics mark-ups.
926 # We are not counting sequences of five apostrophes.
927 if (strlen ($arr[$i]) == 2) $numitalics++
; else
928 if (strlen ($arr[$i]) == 3) $numbold++
;
933 # If there is an odd number of both bold and italics, it is likely
934 # that one of the bold ones was meant to be an apostrophe followed
935 # by italics. Which one we cannot know for certain, but it is more
936 # likely to be one that has a single-letter word before it.
937 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
940 $firstsingleletterword = -1;
941 $firstmultiletterword = -1;
945 if (($i %
2 == 1) and (strlen ($r) == 3))
947 $x1 = substr ($arr[$i-1], -1);
948 $x2 = substr ($arr[$i-1], -2, 1);
950 if ($firstspace == -1) $firstspace = $i;
951 } else if ($x2 == " ") {
952 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
954 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
960 # If there is a single-letter word, use it!
961 if ($firstsingleletterword > -1)
963 $arr [ $firstsingleletterword ] = "''";
964 $arr [ $firstsingleletterword-1 ] .= "'";
966 # If not, but there's a multi-letter word, use that one.
967 else if ($firstmultiletterword > -1)
969 $arr [ $firstmultiletterword ] = "''";
970 $arr [ $firstmultiletterword-1 ] .= "'";
972 # ... otherwise use the first one that has neither.
975 $arr [ $firstspace ] = "''";
976 $arr [ $firstspace-1 ] .= "'";
980 # Now let's actually convert our apostrophic mush to HTML!
989 if ($state == 'both')
996 if (strlen ($r) == 2)
999 { $output .= "</em>"; $state = ''; }
1000 else if ($state == 'strongem')
1001 { $output .= "</em>"; $state = 'strong'; }
1002 else if ($state == 'emstrong')
1003 { $output .= "</strong></em><strong>"; $state = 'strong'; }
1004 else if ($state == 'both')
1005 { $output .= "<strong><em>{$buffer}</em>"; $state = 'strong'; }
1006 else # $state can be 'strong' or ''
1007 { $output .= "<em>"; $state .= 'em'; }
1009 else if (strlen ($r) == 3)
1011 if ($state == 'strong')
1012 { $output .= "</strong>"; $state = ''; }
1013 else if ($state == 'strongem')
1014 { $output .= "</em></strong><em>"; $state = 'em'; }
1015 else if ($state == 'emstrong')
1016 { $output .= "</strong>"; $state = 'em'; }
1017 else if ($state == 'both')
1018 { $output .= "<em><strong>{$buffer}</strong>"; $state = 'em'; }
1019 else # $state can be 'em' or ''
1020 { $output .= "<strong>"; $state .= 'strong'; }
1022 else if (strlen ($r) == 5)
1024 if ($state == 'strong')
1025 { $output .= "</strong><em>"; $state = 'em'; }
1026 else if ($state == 'em')
1027 { $output .= "</em><strong>"; $state = 'strong'; }
1028 else if ($state == 'strongem')
1029 { $output .= "</em></strong>"; $state = ''; }
1030 else if ($state == 'emstrong')
1031 { $output .= "</strong></em>"; $state = ''; }
1032 else if ($state == 'both')
1033 { $output .= "<em><strong>{$buffer}</strong></em>"; $state = ''; }
1034 else # ($state == '')
1035 { $buffer = ''; $state = 'both'; }
1040 # Now close all remaining tags. Notice that the order is important.
1041 if ($state == 'strong' ||
$state == 'emstrong')
1042 $output .= "</strong>";
1043 if ($state == 'em' ||
$state == 'strongem' ||
$state == 'emstrong')
1045 if ($state == 'strongem')
1046 $output .= "</strong>";
1047 if ($state == 'both')
1048 $output .= "<strong><em>{$buffer}</em></strong>";
1053 # Note: we have to do external links before the internal ones,
1054 # and otherwise take great care in the order of things here, so
1055 # that we don't end up interpreting some URLs twice.
1057 /* private */ function replaceExternalLinks( $text ) {
1058 $fname = 'Parser::replaceExternalLinks';
1059 wfProfileIn( $fname );
1061 $text = $this->subReplaceExternalLinks( $text, 'http', true );
1062 $text = $this->subReplaceExternalLinks( $text, 'https', true );
1063 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
1064 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
1065 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
1066 $text = $this->subReplaceExternalLinks( $text, 'news', false );
1067 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
1068 wfProfileOut( $fname );
1072 # Replaces all external links with a given protocol
1073 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
1074 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
1076 # URL character class
1077 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
1079 # this is the list of separators that should be ignored if they
1080 # are the last character of an URL but that should be included
1081 # if they occur within the URL, e.g. "go to www.foo.com, where .."
1082 # in this case, the last comma should not become part of the URL,
1083 # but in "www.foo.com/123,2342,32.htm" it should.
1086 # File name character class, used for identifying images
1087 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
1088 # Recognised image extensions
1089 $images = 'gif|png|jpg|jpeg';
1091 # PLEASE NOTE: The curly braces { } are not part of the regex,
1092 # they are interpreted as part of the string (used to tell PHP
1093 # that the content of the string should be inserted there).
1095 # Regexp for matching image URLs
1096 $rxImageURL = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
1097 "((?i){$images})([^{$uc}]|$)/";
1099 # Regexp for matching non-delimited URLs
1100 $rxFreeURL = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
1101 $sk =& $this->mOptions
->getSkin();
1103 # Replace image URLs with <img> tags, but only for HTTP and HTTPS ($autonumber=true)
1104 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) {
1105 $s = preg_replace( $rxImageURL, '\\1' . $sk->makeImage( "{$unique}:\\3" .
1106 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
1110 $s = preg_replace( $rxFreeURL, '\\1' . "<a href=\"{$unique}:\\3\"" .
1111 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
1112 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
1114 $s = str_replace( $unique, $protocol, $s );
1116 # Search for external links with square brackets by splitting with explode()
1117 $a = explode( "[{$protocol}:", " " . $s );
1118 $s = array_shift( $a );
1119 $s = substr( $s, 1 );
1121 # Regexp for URL in square brackets
1122 $rxWithoutText = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
1123 # Regexp for URL with link text in square brackets
1124 $rxWithText = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
1126 # Now interpret each instance of [protocol:
1127 foreach ( $a as $line ) {
1129 # CASE 1: Link in square brackets, e.g.
1130 # some text [http://domain.tld/some.link] more text
1131 if ( preg_match( $rxWithoutText, $line, $m ) ) {
1132 $link = "{$protocol}:{$m[1]}";
1134 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
1135 else { $text = wfEscapeHTML( $link ); }
1138 # CASE 2: Link with link text and text directly following it, e.g.
1139 # This is a collection of [http://domain.tld/some.link link]s
1140 else if ( preg_match( $rxWithText, $line, $m ) ) {
1141 $link = "{$protocol}:{$m[1]}";
1145 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1151 # CASE 3: Nothing matches, just output the source text
1153 $s .= "[{$protocol}:" . $line;
1157 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1160 # Expand the URL for printable version
1161 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1163 $la = $sk->getExternalLinkAttributes( $link, $text );
1164 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1171 /* private */ function replaceInternalLinks( $s ) {
1172 global $wgLang, $wgLinkCache;
1173 global $wgNamespacesWithSubpages, $wgLanguageCode;
1174 static $fname = 'Parser::replaceInternalLinks' ;
1175 wfProfileIn( $fname );
1177 wfProfileIn( $fname.'-setup' );
1179 # the % is needed to support urlencoded titles as well
1180 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1181 $sk =& $this->mOptions
->getSkin();
1183 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
1185 $a = explode( '[[', ' ' . $s );
1186 $s = array_shift( $a );
1187 $s = substr( $s, 1 );
1189 # Match a link having the form [[namespace:link|alternate]]trail
1191 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1192 # Match the end of a line for a word that's not followed by whitespace,
1193 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1194 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1196 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1197 # Special and Media are pseudo-namespaces; no pages actually exist in them
1199 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1201 if ( $useLinkPrefixExtension ) {
1202 if ( preg_match( $e2, $s, $m ) ) {
1203 $first_prefix = $m[2];
1206 $first_prefix = false;
1212 wfProfileOut( $fname.'-setup' );
1214 foreach ( $a as $line ) {
1215 wfProfileIn( $fname.'-prefixhandling' );
1216 if ( $useLinkPrefixExtension ) {
1217 if ( preg_match( $e2, $s, $m ) ) {
1225 $prefix = $first_prefix;
1226 $first_prefix = false;
1229 wfProfileOut( $fname.'-prefixhandling' );
1231 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1233 # fix up urlencoded title texts
1234 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1236 } else { # Invalid form; output directly
1237 $s .= $prefix . '[[' . $line ;
1241 /* Valid link forms:
1243 :Foobar -- override special treatment of prefix (images, language links)
1244 /Foobar -- convert to CurrentPage/Foobar
1245 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1247 $c = substr($m[1],0,1);
1248 $noforce = ($c != ':');
1249 if( $c == '/' ) { # subpage
1250 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1251 $m[1]=substr($m[1],1,strlen($m[1])-2);
1254 $noslash=substr($m[1],1);
1256 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) { # subpages allowed here
1257 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1260 } # this might be changed for ugliness reasons
1262 $link = $noslash; # no subpage allowed, use standard link
1264 } elseif( $noforce ) { # no subpage
1267 $link = substr( $m[1], 1 );
1269 $wasblank = ( '' == $text );
1273 $nt = Title
::newFromText( $link );
1275 $s .= $prefix . '[[' . $line;
1278 $ns = $nt->getNamespace();
1279 $iw = $nt->getInterWiki();
1281 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1282 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1283 $tmp = $prefix . $trail ;
1284 $s .= (trim($tmp) == '')?
'': $tmp;
1287 if ( $ns == NS_IMAGE
) {
1288 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1289 $wgLinkCache->addImageLinkObj( $nt );
1292 if ( $ns == NS_CATEGORY
) {
1293 $t = $nt->getText() ;
1294 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).":".$t ) ;
1296 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1297 $pPLC=$sk->postParseLinkColour();
1298 $sk->postParseLinkColour( false );
1299 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1300 $sk->postParseLinkColour( $pPLC );
1301 $wgLinkCache->resume();
1303 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1304 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1305 $this->mOutput
->mCategoryLinks
[] = $t ;
1306 $s .= $prefix . $trail ;
1310 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1311 ( strpos( $link, '#' ) == FALSE ) ) {
1312 # Self-links are handled specially; generally de-link and change to bold.
1313 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1317 if( $ns == NS_MEDIA
) {
1318 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1319 $wgLinkCache->addImageLinkObj( $nt );
1321 } elseif( $ns == NS_SPECIAL
) {
1322 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1325 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1327 wfProfileOut( $fname );
1331 # Some functions here used by doBlockLevels()
1333 /* private */ function closeParagraph() {
1335 if ( '' != $this->mLastSection
) {
1336 $result = '</' . $this->mLastSection
. ">\n";
1338 $this->mInPre
= false;
1339 $this->mLastSection
= '';
1342 # getCommon() returns the length of the longest common substring
1343 # of both arguments, starting at the beginning of both.
1345 /* private */ function getCommon( $st1, $st2 ) {
1346 $fl = strlen( $st1 );
1347 $shorter = strlen( $st2 );
1348 if ( $fl < $shorter ) { $shorter = $fl; }
1350 for ( $i = 0; $i < $shorter; ++
$i ) {
1351 if ( $st1{$i} != $st2{$i} ) { break; }
1355 # These next three functions open, continue, and close the list
1356 # element appropriate to the prefix character passed into them.
1358 /* private */ function openList( $char )
1360 $result = $this->closeParagraph();
1362 if ( '*' == $char ) { $result .= '<ul><li>'; }
1363 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1364 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1365 else if ( ';' == $char ) {
1366 $result .= '<dl><dt>';
1367 $this->mDTopen
= true;
1369 else { $result = '<!-- ERR 1 -->'; }
1374 /* private */ function nextItem( $char ) {
1375 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1376 else if ( ':' == $char ||
';' == $char ) {
1378 if ( $this->mDTopen
) { $close = '</dt>'; }
1379 if ( ';' == $char ) {
1380 $this->mDTopen
= true;
1381 return $close . '<dt>';
1383 $this->mDTopen
= false;
1384 return $close . '<dd>';
1387 return '<!-- ERR 2 -->';
1390 /* private */function closeList( $char ) {
1391 if ( '*' == $char ) { $text = '</li></ul>'; }
1392 else if ( '#' == $char ) { $text = '</li></ol>'; }
1393 else if ( ':' == $char ) {
1394 if ( $this->mDTopen
) {
1395 $this->mDTopen
= false;
1396 $text = '</dt></dl>';
1398 $text = '</dd></dl>';
1401 else { return '<!-- ERR 3 -->'; }
1405 /* private */ function doBlockLevels( $text, $linestart ) {
1406 $fname = 'Parser::doBlockLevels';
1407 wfProfileIn( $fname );
1409 # Parsing through the text line by line. The main thing
1410 # happening here is handling of block-level elements p, pre,
1411 # and making lists from lines starting with * # : etc.
1413 $textLines = explode( "\n", $text );
1415 $lastPrefix = $output = $lastLine = '';
1416 $this->mDTopen
= $inBlockElem = false;
1418 $paragraphStack = false;
1420 if ( !$linestart ) {
1421 $output .= array_shift( $textLines );
1423 foreach ( $textLines as $oLine ) {
1424 $lastPrefixLength = strlen( $lastPrefix );
1425 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1426 $preOpenMatch = preg_match("/<pre/i", $oLine );
1427 if ( !$this->mInPre
) {
1428 # Multiple prefixes may abut each other for nested lists.
1429 $prefixLength = strspn( $oLine, '*#:;' );
1430 $pref = substr( $oLine, 0, $prefixLength );
1433 $pref2 = str_replace( ';', ':', $pref );
1434 $t = substr( $oLine, $prefixLength );
1435 $this->mInPre
= !empty($preOpenMatch);
1437 # Don't interpret any other prefixes in preformatted text
1439 $pref = $pref2 = '';
1444 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1445 # Same as the last item, so no need to deal with nesting or opening stuff
1446 $output .= $this->nextItem( substr( $pref, -1 ) );
1447 $paragraphStack = false;
1449 if ( ";" == substr( $pref, -1 ) ) {
1450 # The one nasty exception: definition lists work like this:
1451 # ; title : definition text
1452 # So we check for : in the remainder text to split up the
1453 # title and definition, without b0rking links.
1454 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1455 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1457 $output .= $term . $this->nextItem( ':' );
1461 } elseif( $prefixLength ||
$lastPrefixLength ) {
1462 # Either open or close a level...
1463 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1464 $paragraphStack = false;
1466 while( $commonPrefixLength < $lastPrefixLength ) {
1467 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1468 --$lastPrefixLength;
1470 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1471 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1473 while ( $prefixLength > $commonPrefixLength ) {
1474 $char = substr( $pref, $commonPrefixLength, 1 );
1475 $output .= $this->openList( $char );
1477 if ( ';' == $char ) {
1478 # FIXME: This is dupe of code above
1479 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1481 $output .= $term . $this->nextItem( ":" );
1485 ++
$commonPrefixLength;
1487 $lastPrefix = $pref2;
1489 if( 0 == $prefixLength ) {
1490 # No prefix (not in list)--go to paragraph mode
1491 $uniq_prefix = UNIQ_PREFIX
;
1492 // XXX: use a stack for nestable elements like span, table and div
1493 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1494 $closematch = preg_match(
1495 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1496 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1497 if ( $openmatch or $closematch ) {
1498 $paragraphStack = false;
1499 $output .= $this->closeParagraph();
1500 if($preOpenMatch and !$preCloseMatch) {
1501 $this->mInPre
= true;
1503 if ( $closematch ) {
1504 $inBlockElem = false;
1506 $inBlockElem = true;
1508 } else if ( !$inBlockElem && !$this->mInPre
) {
1509 if ( " " == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1511 if ($this->mLastSection
!= 'pre') {
1512 $paragraphStack = false;
1513 $output .= $this->closeParagraph().'<pre>';
1514 $this->mLastSection
= 'pre';
1518 if ( '' == trim($t) ) {
1519 if ( $paragraphStack ) {
1520 $output .= $paragraphStack.'<br />';
1521 $paragraphStack = false;
1522 $this->mLastSection
= 'p';
1524 if ($this->mLastSection
!= 'p' ) {
1525 $output .= $this->closeParagraph();
1526 $this->mLastSection
= '';
1527 $paragraphStack = '<p>';
1529 $paragraphStack = '</p><p>';
1533 if ( $paragraphStack ) {
1534 $output .= $paragraphStack;
1535 $paragraphStack = false;
1536 $this->mLastSection
= 'p';
1537 } else if ($this->mLastSection
!= 'p') {
1538 $output .= $this->closeParagraph().'<p>';
1539 $this->mLastSection
= 'p';
1545 if ($paragraphStack === false) {
1549 while ( $prefixLength ) {
1550 $output .= $this->closeList( $pref2{$prefixLength-1} );
1553 if ( '' != $this->mLastSection
) {
1554 $output .= '</' . $this->mLastSection
. '>';
1555 $this->mLastSection
= '';
1558 wfProfileOut( $fname );
1562 # Return value of a magic variable (like PAGENAME)
1563 function getVariableValue( $index ) {
1564 global $wgLang, $wgSitename, $wgServer;
1567 case MAG_CURRENTMONTH
:
1568 return $wgLang->formatNum( date( 'm' ) );
1569 case MAG_CURRENTMONTHNAME
:
1570 return $wgLang->getMonthName( date('n') );
1571 case MAG_CURRENTMONTHNAMEGEN
:
1572 return $wgLang->getMonthNameGen( date('n') );
1573 case MAG_CURRENTDAY
:
1574 return $wgLang->formatNum( date('j') );
1576 return $this->mTitle
->getText();
1578 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1579 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1580 case MAG_CURRENTDAYNAME
:
1581 return $wgLang->getWeekdayName( date('w')+
1 );
1582 case MAG_CURRENTYEAR
:
1583 return $wgLang->formatNum( date( 'Y' ) );
1584 case MAG_CURRENTTIME
:
1585 return $wgLang->time( wfTimestampNow(), false );
1586 case MAG_NUMBEROFARTICLES
:
1587 return $wgLang->formatNum( wfNumberOfArticles() );
1597 # initialise the magic variables (like CURRENTMONTHNAME)
1598 function initialiseVariables() {
1599 global $wgVariableIDs;
1600 $this->mVariables
= array();
1601 foreach ( $wgVariableIDs as $id ) {
1602 $mw =& MagicWord
::get( $id );
1603 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1607 /* private */ function replaceVariables( $text, $args = array() ) {
1608 global $wgLang, $wgScript, $wgArticlePath;
1610 # Prevent too big inclusions
1611 if(strlen($text)> MAX_INCLUDE_SIZE
)
1614 $fname = 'Parser::replaceVariables';
1615 wfProfileIn( $fname );
1618 $titleChars = Title
::legalChars();
1619 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1621 # This function is called recursively. To keep track of arguments we need a stack:
1622 array_push( $this->mArgStack
, $args );
1624 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1625 $GLOBALS['wgCurParser'] =& $this;
1628 if ( $this->mOutputType
== OT_HTML
) {
1629 # Variable substitution
1630 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1632 # Argument substitution
1633 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1635 # Template substitution
1636 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1637 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1639 array_pop( $this->mArgStack
);
1641 wfProfileOut( $fname );
1645 function variableSubstitution( $matches ) {
1646 if ( !$this->mVariables
) {
1647 $this->initialiseVariables();
1649 if ( array_key_exists( $matches[1], $this->mVariables
) ) {
1650 $text = $this->mVariables
[$matches[1]];
1651 $this->mOutput
->mContainsOldMagic
= true;
1653 $text = $matches[0];
1658 # Split template arguments
1659 function getTemplateArgs( $argsString ) {
1660 if ( $argsString === '' ) {
1664 $args = explode( '|', substr( $argsString, 1 ) );
1666 # If any of the arguments contains a '[[' but no ']]', it needs to be
1667 # merged with the next arg because the '|' character between belongs
1668 # to the link syntax and not the template parameter syntax.
1669 $argc = count($args);
1671 for ( $i = 0; $i < $argc-1; $i++
) {
1672 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1673 $args[$i] .= "|".$args[$i+
1];
1674 array_splice($args, $i+
1, 1);
1683 function braceSubstitution( $matches ) {
1684 global $wgLinkCache, $wgLang;
1685 $fname = 'Parser::braceSubstitution';
1692 # $newline is an optional newline character before the braces
1693 # $part1 is the bit before the first |, and must contain only title characters
1694 # $args is a list of arguments, starting from index 0, not including $part1
1696 $newline = $matches[1];
1697 $part1 = $matches[2];
1698 # If the third subpattern matched anything, it will start with |
1700 $args = $this->getTemplateArgs($matches[3]);
1701 $argc = count( $args );
1704 if ( strpos( $matches[0], '{{{' ) !== false ) {
1705 $text = $matches[0];
1712 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1713 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1714 if ( $this->mOutputType
!= OT_WIKI
) {
1715 # Invalid SUBST not replaced at PST time
1716 # Return without further processing
1717 $text = $matches[0];
1721 } elseif ( $this->mOutputType
== OT_WIKI
) {
1722 # SUBST not found in PST pass, do nothing
1723 $text = $matches[0];
1728 # MSG, MSGNW and INT
1731 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1732 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1735 # Remove obsolete MSG:
1736 $mwMsg =& MagicWord
::get( MAG_MSG
);
1737 $mwMsg->matchStartAndRemove( $part1 );
1740 # Check if it is an internal message
1741 $mwInt =& MagicWord
::get( MAG_INT
);
1742 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1743 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1744 $text = wfMsgReal( $part1, $args, true );
1752 # Check for NS: (namespace expansion)
1753 $mwNs = MagicWord
::get( MAG_NS
);
1754 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1755 if ( intval( $part1 ) ) {
1756 $text = $wgLang->getNsText( intval( $part1 ) );
1759 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1760 if ( !is_null( $index ) ) {
1761 $text = $wgLang->getNsText( $index );
1768 # LOCALURL and LOCALURLE
1770 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1771 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1773 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1774 $func = 'getLocalURL';
1775 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1776 $func = 'escapeLocalURL';
1781 if ( $func !== '' ) {
1782 $title = Title
::newFromText( $part1 );
1783 if ( !is_null( $title ) ) {
1785 $text = $title->$func( $args[0] );
1787 $text = $title->$func();
1794 # Internal variables
1795 if ( !$this->mVariables
) {
1796 $this->initialiseVariables();
1798 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1799 $text = $this->mVariables
[$part1];
1801 $this->mOutput
->mContainsOldMagic
= true;
1804 # Template table test
1806 # Did we encounter this template already? If yes, it is in the cache
1807 # and we need to check for loops.
1808 if ( isset( $this->mTemplates
[$part1] ) ) {
1809 # Infinite loop test
1810 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1814 # set $text to cached message.
1815 $text = $this->mTemplates
[$part1];
1819 # Load from database
1821 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1822 if ( !is_null( $title ) && !$title->isExternal() ) {
1823 # Check for excessive inclusion
1824 $dbk = $title->getPrefixedDBkey();
1825 if ( $this->incrementIncludeCount( $dbk ) ) {
1826 # This should never be reached.
1827 $article = new Article( $title );
1828 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1829 if ( $articleContent !== false ) {
1831 $text = $articleContent;
1836 # If the title is valid but undisplayable, make a link to it
1837 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1838 $text = '[[' . $title->getPrefixedText() . ']]';
1842 # Template cache array insertion
1843 $this->mTemplates
[$part1] = $text;
1847 # Recursive parsing, escaping and link table handling
1848 # Only for HTML output
1849 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1850 $text = wfEscapeWikiText( $text );
1851 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1852 # Clean up argument array
1853 $assocArgs = array();
1855 foreach( $args as $arg ) {
1856 $eqpos = strpos( $arg, '=' );
1857 if ( $eqpos === false ) {
1858 $assocArgs[$index++
] = $arg;
1860 $name = trim( substr( $arg, 0, $eqpos ) );
1861 $value = trim( substr( $arg, $eqpos+
1 ) );
1862 if ( $value === false ) {
1865 if ( $name !== false ) {
1866 $assocArgs[$name] = $value;
1871 # Do not enter included links in link table
1872 if ( !is_null( $title ) ) {
1873 $wgLinkCache->suspend();
1876 # Add a new element to the templace recursion path
1877 $this->mTemplatePath
[$part1] = 1;
1879 # Run full parser on the included text
1880 $text = $this->internalParse( $text, $newline, $assocArgs );
1881 # I replaced the line below with the line above, as it former seems to cause several bugs
1882 #$text = $this->stripParse( $text, $newline, $assocArgs );
1884 # Resume the link cache and register the inclusion as a link
1885 if ( !is_null( $title ) ) {
1886 $wgLinkCache->resume();
1887 $wgLinkCache->addLinkObj( $title );
1890 # Empties the template path
1891 $this->mTemplatePath
= array();
1900 # Triple brace replacement -- used for template arguments
1901 function argSubstitution( $matches ) {
1902 $newline = $matches[1];
1903 $arg = trim( $matches[2] );
1904 $text = $matches[0];
1905 $inputArgs = end( $this->mArgStack
);
1907 if ( array_key_exists( $arg, $inputArgs ) ) {
1908 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1914 # Returns true if the function is allowed to include this entity
1915 function incrementIncludeCount( $dbk ) {
1916 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1917 $this->mIncludeCount
[$dbk] = 0;
1919 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1927 # Cleans up HTML, removes dangerous tags and attributes
1928 /* private */ function removeHTMLtags( $text ) {
1929 global $wgUseTidy, $wgUserHtml;
1930 $fname = 'Parser::removeHTMLtags';
1931 wfProfileIn( $fname );
1934 $htmlpairs = array( # Tags that must be closed
1935 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1936 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1937 'strike', 'strong', 'tt', 'var', 'div', 'center',
1938 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1939 'ruby', 'rt' , 'rb' , 'rp', 'p'
1941 $htmlsingle = array(
1942 'br', 'hr', 'li', 'dt', 'dd'
1944 $htmlnest = array( # Tags that can be nested--??
1945 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1946 'dl', 'font', 'big', 'small', 'sub', 'sup'
1948 $tabletags = array( # Can only appear inside table
1952 $htmlpairs = array();
1953 $htmlsingle = array();
1954 $htmlnest = array();
1955 $tabletags = array();
1958 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1959 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1961 $htmlattrs = $this->getHTMLattrs () ;
1963 # Remove HTML comments
1964 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1966 $bits = explode( '<', $text );
1967 $text = array_shift( $bits );
1969 $tagstack = array(); $tablestack = array();
1970 foreach ( $bits as $x ) {
1971 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1972 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1974 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1975 error_reporting( $prev );
1978 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1982 if ( ! in_array( $t, $htmlsingle ) &&
1983 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1984 @array_push
( $tagstack, $ot );
1987 if ( $t == 'table' ) {
1988 $tagstack = array_pop( $tablestack );
1993 # Keep track for later
1994 if ( in_array( $t, $tabletags ) &&
1995 ! in_array( 'table', $tagstack ) ) {
1997 } else if ( in_array( $t, $tagstack ) &&
1998 ! in_array ( $t , $htmlnest ) ) {
2000 } else if ( ! in_array( $t, $htmlsingle ) ) {
2001 if ( $t == 'table' ) {
2002 array_push( $tablestack, $tagstack );
2003 $tagstack = array();
2005 array_push( $tagstack, $t );
2007 # Strip non-approved attributes from the tag
2008 $newparams = $this->fixTagAttributes($params);
2012 $rest = str_replace( '>', '>', $rest );
2013 $text .= "<$slash$t $newparams$brace$rest";
2017 $text .= '<' . str_replace( '>', '>', $x);
2019 # Close off any remaining tags
2020 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2022 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2025 # this might be possible using tidy itself
2026 foreach ( $bits as $x ) {
2027 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2029 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2030 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2031 $newparams = $this->fixTagAttributes($params);
2032 $rest = str_replace( '>', '>', $rest );
2033 $text .= "<$slash$t $newparams$brace$rest";
2035 $text .= '<' . str_replace( '>', '>', $x);
2039 wfProfileOut( $fname );
2046 * This function accomplishes several tasks:
2047 * 1) Auto-number headings if that option is enabled
2048 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2049 * 3) Add a Table of contents on the top for users who have enabled the option
2050 * 4) Auto-anchor headings
2052 * It loops through all headlines, collects the necessary data, then splits up the
2053 * string and re-inserts the newly formatted headlines.
2057 /* private */ function formatHeadings( $text, $isMain=true ) {
2058 global $wgInputEncoding, $wgMaxTocLevel;
2060 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2061 $doShowToc = $this->mOptions
->getShowToc();
2062 $forceTocHere = false;
2063 if( !$this->mTitle
->userCanEdit() ) {
2065 $rightClickHack = 0;
2067 $showEditLink = $this->mOptions
->getEditSection();
2068 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2071 # Inhibit editsection links if requested in the page
2072 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2073 if( $esw->matchAndRemove( $text ) ) {
2076 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2078 $mw =& MagicWord
::get( MAG_NOTOC
);
2079 if( $mw->matchAndRemove( $text ) ) {
2083 # never add the TOC to the Main Page. This is an entry page that should not
2084 # be more than 1-2 screens large anyway
2085 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2089 # Get all headlines for numbering them and adding funky stuff like [edit]
2090 # links - this is for later, but we need the number of headlines right now
2091 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2093 # if there are fewer than 4 headlines in the article, do not show TOC
2094 if( $numMatches < 4 ) {
2098 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2099 # override above conditions and always show TOC at that place
2100 $mw =& MagicWord
::get( MAG_TOC
);
2101 if ($mw->match( $text ) ) {
2103 $forceTocHere = true;
2105 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2106 # override above conditions and always show TOC above first header
2107 $mw =& MagicWord
::get( MAG_FORCETOC
);
2108 if ($mw->matchAndRemove( $text ) ) {
2115 # We need this to perform operations on the HTML
2116 $sk =& $this->mOptions
->getSkin();
2121 # Ugh .. the TOC should have neat indentation levels which can be
2122 # passed to the skin functions. These are determined here
2127 $sublevelCount = array();
2130 foreach( $matches[3] as $headline ) {
2133 $prevlevel = $level;
2135 $level = $matches[1][$headlineCount];
2136 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
2137 # reset when we enter a new level
2138 $sublevelCount[$level] = 0;
2139 $toc .= $sk->tocIndent( $level - $prevlevel );
2140 $toclevel +
= $level - $prevlevel;
2142 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
2143 # reset when we step back a level
2144 $sublevelCount[$level+
1]=0;
2145 $toc .= $sk->tocUnindent( $prevlevel - $level );
2146 $toclevel -= $prevlevel - $level;
2148 # count number of headlines for each level
2149 @$sublevelCount[$level]++
;
2150 if( $doNumberHeadings ||
$doShowToc ) {
2152 for( $i = 1; $i <= $level; $i++
) {
2153 if( !empty( $sublevelCount[$i] ) ) {
2157 $numbering .= $sublevelCount[$i];
2163 # The canonized header is a version of the header text safe to use for links
2164 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2165 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2166 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
2169 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2170 $tocline = trim( $canonized_headline );
2171 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2172 $replacearray = array(
2176 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2177 $refer[$headlineCount] = $canonized_headline;
2179 # count how many in assoc. array so we can track dupes in anchors
2180 @$refers[$canonized_headline]++
;
2181 $refcount[$headlineCount]=$refers[$canonized_headline];
2183 # Prepend the number to the heading text
2185 if( $doNumberHeadings ||
$doShowToc ) {
2186 $tocline = $numbering . ' ' . $tocline;
2188 # Don't number the heading if it is the only one (looks silly)
2189 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2190 # the two are different if the line contains a link
2191 $headline=$numbering . ' ' . $headline;
2195 # Create the anchor for linking from the TOC to the section
2196 $anchor = $canonized_headline;
2197 if($refcount[$headlineCount] > 1 ) {
2198 $anchor .= '_' . $refcount[$headlineCount];
2200 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2201 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2203 if( $showEditLink ) {
2204 if ( empty( $head[$headlineCount] ) ) {
2205 $head[$headlineCount] = '';
2207 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
2210 # Add the edit section span
2211 if( $rightClickHack ) {
2212 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
2215 # give headline the correct <h#> tag
2216 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2222 $toclines = $headlineCount;
2223 $toc .= $sk->tocUnindent( $toclevel );
2224 $toc = $sk->tocTable( $toc );
2227 # split up and insert constructed headlines
2229 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2232 foreach( $blocks as $block ) {
2233 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2234 # This is the [edit] link that appears for the top block of text when
2235 # section editing is enabled
2237 # Disabled because it broke block formatting
2238 # For example, a bullet point in the top line
2239 # $full .= $sk->editSectionLink(0);
2242 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2243 # Top anchor now in skin
2247 if( !empty( $head[$i] ) ) {
2253 $mw =& MagicWord
::get( MAG_TOC
);
2254 return $mw->replace( $toc, $full );
2260 # Return an HTML link for the "ISBN 123456" text
2261 /* private */ function magicISBN( $text ) {
2263 $fname = 'Parser::magicISBN';
2264 wfProfileIn( $fname );
2266 $a = split( 'ISBN ', " $text" );
2267 if ( count ( $a ) < 2 ) {
2268 wfProfileOut( $fname );
2271 $text = substr( array_shift( $a ), 1);
2272 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2274 foreach ( $a as $x ) {
2275 $isbn = $blank = '' ;
2276 while ( ' ' == $x{0} ) {
2278 $x = substr( $x, 1 );
2280 while ( strstr( $valid, $x{0} ) != false ) {
2282 $x = substr( $x, 1 );
2284 $num = str_replace( '-', '', $isbn );
2285 $num = str_replace( ' ', '', $num );
2288 $text .= "ISBN $blank$x";
2290 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2291 $text .= '<a href="' .
2292 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2293 "\" class=\"internal\">ISBN $isbn</a>";
2297 wfProfileOut( $fname );
2301 # Return an HTML link for the "GEO ..." text
2302 /* private */ function magicGEO( $text ) {
2303 global $wgLang, $wgUseGeoMode;
2304 if ( !isset ( $wgUseGeoMode ) ||
!$wgUseGeoMode ) return $text ;
2305 $fname = 'Parser::magicGEO';
2306 wfProfileIn( $fname );
2308 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2309 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2310 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2311 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2312 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2313 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2315 $a = split( 'GEO ', " $text" );
2316 if ( count ( $a ) < 2 ) {
2317 wfProfileOut( $fname );
2320 $text = substr( array_shift( $a ), 1);
2321 $valid = '0123456789.+-:';
2323 foreach ( $a as $x ) {
2324 $geo = $blank = '' ;
2325 while ( ' ' == $x{0} ) {
2327 $x = substr( $x, 1 );
2329 while ( strstr( $valid, $x{0} ) != false ) {
2331 $x = substr( $x, 1 );
2333 $num = str_replace( '+', '', $geo );
2334 $num = str_replace( ' ', '', $num );
2336 if ( '' == $num ||
count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2337 $text .= "GEO $blank$x";
2339 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2340 $text .= '<a href="' .
2341 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2342 "\" class=\"internal\">GEO $geo</a>";
2346 wfProfileOut( $fname );
2350 # Return an HTML link for the "RFC 1234" text
2351 /* private */ function magicRFC( $text ) {
2354 $a = split( 'RFC ', ' '.$text );
2355 if ( count ( $a ) < 2 ) return $text;
2356 $text = substr( array_shift( $a ), 1);
2357 $valid = '0123456789';
2359 foreach ( $a as $x ) {
2360 $rfc = $blank = '' ;
2361 while ( ' ' == $x{0} ) {
2363 $x = substr( $x, 1 );
2365 while ( strstr( $valid, $x{0} ) != false ) {
2367 $x = substr( $x, 1 );
2371 $text .= "RFC $blank$x";
2373 $url = wfmsg( 'rfcurl' );
2374 $url = str_replace( '$1', $rfc, $url);
2375 $sk =& $this->mOptions
->getSkin();
2376 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2377 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2383 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2384 $this->mOptions
= $options;
2385 $this->mTitle
=& $title;
2386 $this->mOutputType
= OT_WIKI
;
2388 if ( $clearState ) {
2389 $this->clearState();
2392 $stripState = false;
2396 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2400 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2401 "/<br *?>/i" => "<br />",
2403 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2405 $text = $this->strip( $text, $stripState, false );
2406 $text = $this->pstPass2( $text, $user );
2407 $text = $this->unstrip( $text, $stripState );
2408 $text = $this->unstripNoWiki( $text, $stripState );
2412 /* private */ function pstPass2( $text, &$user ) {
2413 global $wgLang, $wgLocaltimezone, $wgCurParser;
2415 # Variable replacement
2416 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2417 $text = $this->replaceVariables( $text );
2421 $n = $user->getName();
2422 $k = $user->getOption( 'nickname' );
2423 if ( '' == $k ) { $k = $n; }
2424 if(isset($wgLocaltimezone)) {
2425 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2427 /* Note: this is an ugly timezone hack for the European wikis */
2428 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2429 ' (' . date( 'T' ) . ')';
2430 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2432 $text = preg_replace( '/~~~~~/', $d, $text );
2433 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2434 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2436 # Context links: [[|name]] and [[name (context)|]]
2438 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2439 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2440 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2441 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2443 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2444 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2445 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2446 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2447 # [[ns:page (cont)|]]
2449 $t = $this->mTitle
->getText();
2450 if ( preg_match( $conpat, $t, $m ) ) {
2453 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2454 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2455 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2457 if ( '' == $context ) {
2458 $text = preg_replace( $p2, '[[\\1]]', $text );
2460 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2464 $mw =& MagicWord::get( MAG_SUBST );
2465 $wgCurParser = $this->fork();
2466 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2467 $this->merge( $wgCurParser );
2470 # Trim trailing whitespace
2471 # MAG_END (__END__) tag allows for trailing
2472 # whitespace to be deliberately included
2473 $text = rtrim( $text );
2474 $mw =& MagicWord
::get( MAG_END
);
2475 $mw->matchAndRemove( $text );
2480 # Set up some variables which are usually set up in parse()
2481 # so that an external function can call some class members with confidence
2482 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2483 $this->mTitle
=& $title;
2484 $this->mOptions
= $options;
2485 $this->mOutputType
= $outputType;
2486 if ( $clearState ) {
2487 $this->clearState();
2491 function transformMsg( $text, $options ) {
2493 static $executing = false;
2495 # Guard against infinite recursion
2501 $this->mTitle
= $wgTitle;
2502 $this->mOptions
= $options;
2503 $this->mOutputType
= OT_MSG
;
2504 $this->clearState();
2505 $text = $this->replaceVariables( $text );
2511 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2512 # Callback will be called with the text within
2513 # Transform and return the text within
2514 function setHook( $tag, $callback ) {
2515 $oldVal = @$this->mTagHooks
[$tag];
2516 $this->mTagHooks
[$tag] = $callback;
2523 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2524 var $mCacheTime; # Used in ParserCache
2526 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2527 $containsOldMagic = false )
2529 $this->mText
= $text;
2530 $this->mLanguageLinks
= $languageLinks;
2531 $this->mCategoryLinks
= $categoryLinks;
2532 $this->mContainsOldMagic
= $containsOldMagic;
2533 $this->mCacheTime
= "";
2536 function getText() { return $this->mText
; }
2537 function getLanguageLinks() { return $this->mLanguageLinks
; }
2538 function getCategoryLinks() { return $this->mCategoryLinks
; }
2539 function getCacheTime() { return $this->mCacheTime
; }
2540 function containsOldMagic() { return $this->mContainsOldMagic
; }
2541 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2542 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2543 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2544 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2545 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2547 function merge( $other ) {
2548 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2549 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2550 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2557 # All variables are private
2558 var $mUseTeX; # Use texvc to expand <math> tags
2559 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2560 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2561 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2562 var $mAllowExternalImages; # Allow external images inline
2563 var $mSkin; # Reference to the preferred skin
2564 var $mDateFormat; # Date format index
2565 var $mEditSection; # Create "edit section" links
2566 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2567 var $mNumberHeadings; # Automatically number headings
2568 var $mShowToc; # Show table of contents
2570 function getUseTeX() { return $this->mUseTeX
; }
2571 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2572 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2573 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2574 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2575 function getSkin() { return $this->mSkin
; }
2576 function getDateFormat() { return $this->mDateFormat
; }
2577 function getEditSection() { return $this->mEditSection
; }
2578 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2579 function getNumberHeadings() { return $this->mNumberHeadings
; }
2580 function getShowToc() { return $this->mShowToc
; }
2582 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2583 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2584 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2585 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2586 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2587 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2588 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2589 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2590 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2591 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2593 function setSkin( &$x ) { $this->mSkin
=& $x; }
2595 /* static */ function newFromUser( &$user ) {
2596 $popts = new ParserOptions
;
2597 $popts->initialiseFromUser( $user );
2601 function initialiseFromUser( &$userInput ) {
2602 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2604 if ( !$userInput ) {
2606 $user->setLoaded( true );
2608 $user =& $userInput;
2611 $this->mUseTeX
= $wgUseTeX;
2612 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2613 $this->mUseDynamicDates
= $wgUseDynamicDates;
2614 $this->mInterwikiMagic
= $wgInterwikiMagic;
2615 $this->mAllowExternalImages
= $wgAllowExternalImages;
2616 $this->mSkin
=& $user->getSkin();
2617 $this->mDateFormat
= $user->getOption( 'date' );
2618 $this->mEditSection
= $user->getOption( 'editsection' );
2619 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2620 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2621 $this->mShowToc
= $user->getOption( 'showtoc' );
2627 # Regex callbacks, used in Parser::replaceVariables
2628 function wfBraceSubstitution( $matches )
2630 global $wgCurParser;
2631 return $wgCurParser->braceSubstitution( $matches );
2634 function wfArgSubstitution( $matches )
2636 global $wgCurParser;
2637 return $wgCurParser->argSubstitution( $matches );
2640 function wfVariableSubstitution( $matches )
2642 global $wgCurParser;
2643 return $wgCurParser->variableSubstitution( $matches );