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 if (strlen ($arr[$i]) == 2) $numitalics++
; else
927 if (strlen ($arr[$i]) == 3) $numbold++
; else
928 if (strlen ($arr[$i]) == 5) { $numitalics++
; $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'; }
1044 # Note: we have to do external links before the internal ones,
1045 # and otherwise take great care in the order of things here, so
1046 # that we don't end up interpreting some URLs twice.
1048 /* private */ function replaceExternalLinks( $text ) {
1049 $fname = 'Parser::replaceExternalLinks';
1050 wfProfileIn( $fname );
1051 $text = $this->subReplaceExternalLinks( $text, 'http', true );
1052 $text = $this->subReplaceExternalLinks( $text, 'https', true );
1053 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
1054 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
1055 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
1056 $text = $this->subReplaceExternalLinks( $text, 'news', false );
1057 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
1058 wfProfileOut( $fname );
1062 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
1063 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
1064 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
1066 # this is the list of separators that should be ignored if they
1067 # are the last character of an URL but that should be included
1068 # if they occur within the URL, e.g. "go to www.foo.com, where .."
1069 # in this case, the last comma should not become part of the URL,
1070 # but in "www.foo.com/123,2342,32.htm" it should.
1072 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
1073 $images = 'gif|png|jpg|jpeg';
1075 # PLEASE NOTE: The curly braces { } are not part of the regex,
1076 # they are interpreted as part of the string (used to tell PHP
1077 # that the content of the string should be inserted there).
1078 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
1079 "((?i){$images})([^{$uc}]|$)/";
1081 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
1082 $sk =& $this->mOptions
->getSkin();
1084 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
1085 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
1086 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
1088 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
1089 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
1090 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
1092 $s = str_replace( $unique, $protocol, $s );
1094 $a = explode( "[{$protocol}:", " " . $s );
1095 $s = array_shift( $a );
1096 $s = substr( $s, 1 );
1098 # Regexp for URL in square brackets
1099 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
1100 # Regexp for URL with link text in square brackets
1101 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
1103 foreach ( $a as $line ) {
1105 # CASE 1: Link in square brackets, e.g.
1106 # some text [http://domain.tld/some.link] more text
1107 if ( preg_match( $e1, $line, $m ) ) {
1108 $link = "{$protocol}:{$m[1]}";
1110 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
1111 else { $text = wfEscapeHTML( $link ); }
1114 # CASE 2: Link with link text and text directly following it, e.g.
1115 # This is a collection of [http://domain.tld/some.link link]s
1116 else if ( preg_match( $e2, $line, $m ) ) {
1117 $link = "{$protocol}:{$m[1]}";
1121 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1127 # CASE 3: Nothing matches, just output the source text
1129 $s .= "[{$protocol}:" . $line;
1133 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1136 # Expand the URL for printable version
1137 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1139 $la = $sk->getExternalLinkAttributes( $link, $text );
1140 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1147 /* private */ function replaceInternalLinks( $s ) {
1148 global $wgLang, $wgLinkCache;
1149 global $wgNamespacesWithSubpages, $wgLanguageCode;
1150 static $fname = 'Parser::replaceInternalLinks' ;
1151 wfProfileIn( $fname );
1153 wfProfileIn( $fname.'-setup' );
1155 # the % is needed to support urlencoded titles as well
1156 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1157 $sk =& $this->mOptions
->getSkin();
1159 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
1161 $a = explode( '[[', ' ' . $s );
1162 $s = array_shift( $a );
1163 $s = substr( $s, 1 );
1165 # Match a link having the form [[namespace:link|alternate]]trail
1167 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1168 # Match the end of a line for a word that's not followed by whitespace,
1169 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1170 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1172 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1173 # Special and Media are pseudo-namespaces; no pages actually exist in them
1175 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1177 if ( $useLinkPrefixExtension ) {
1178 if ( preg_match( $e2, $s, $m ) ) {
1179 $first_prefix = $m[2];
1182 $first_prefix = false;
1188 wfProfileOut( $fname.'-setup' );
1190 foreach ( $a as $line ) {
1191 wfProfileIn( $fname.'-prefixhandling' );
1192 if ( $useLinkPrefixExtension ) {
1193 if ( preg_match( $e2, $s, $m ) ) {
1201 $prefix = $first_prefix;
1202 $first_prefix = false;
1205 wfProfileOut( $fname.'-prefixhandling' );
1207 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1209 # fix up urlencoded title texts
1210 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1212 } else { # Invalid form; output directly
1213 $s .= $prefix . '[[' . $line ;
1217 /* Valid link forms:
1219 :Foobar -- override special treatment of prefix (images, language links)
1220 /Foobar -- convert to CurrentPage/Foobar
1221 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1223 $c = substr($m[1],0,1);
1224 $noforce = ($c != ':');
1225 if( $c == '/' ) { # subpage
1226 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1227 $m[1]=substr($m[1],1,strlen($m[1])-2);
1230 $noslash=substr($m[1],1);
1232 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) { # subpages allowed here
1233 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1236 } # this might be changed for ugliness reasons
1238 $link = $noslash; # no subpage allowed, use standard link
1240 } elseif( $noforce ) { # no subpage
1243 $link = substr( $m[1], 1 );
1245 $wasblank = ( '' == $text );
1249 $nt = Title
::newFromText( $link );
1251 $s .= $prefix . '[[' . $line;
1254 $ns = $nt->getNamespace();
1255 $iw = $nt->getInterWiki();
1257 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1258 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1259 $tmp = $prefix . $trail ;
1260 $s .= (trim($tmp) == '')?
'': $tmp;
1263 if ( $ns == NS_IMAGE
) {
1264 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1265 $wgLinkCache->addImageLinkObj( $nt );
1268 if ( $ns == NS_CATEGORY
) {
1269 $t = $nt->getText() ;
1270 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).":".$t ) ;
1272 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1273 $pPLC=$sk->postParseLinkColour();
1274 $sk->postParseLinkColour( false );
1275 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1276 $sk->postParseLinkColour( $pPLC );
1277 $wgLinkCache->resume();
1279 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1280 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1281 $this->mOutput
->mCategoryLinks
[] = $t ;
1282 $s .= $prefix . $trail ;
1286 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1287 ( strpos( $link, '#' ) == FALSE ) ) {
1288 # Self-links are handled specially; generally de-link and change to bold.
1289 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1293 if( $ns == NS_MEDIA
) {
1294 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1295 $wgLinkCache->addImageLinkObj( $nt );
1297 } elseif( $ns == NS_SPECIAL
) {
1298 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1301 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1303 wfProfileOut( $fname );
1307 # Some functions here used by doBlockLevels()
1309 /* private */ function closeParagraph() {
1311 if ( '' != $this->mLastSection
) {
1312 $result = '</' . $this->mLastSection
. ">\n";
1314 $this->mInPre
= false;
1315 $this->mLastSection
= '';
1318 # getCommon() returns the length of the longest common substring
1319 # of both arguments, starting at the beginning of both.
1321 /* private */ function getCommon( $st1, $st2 ) {
1322 $fl = strlen( $st1 );
1323 $shorter = strlen( $st2 );
1324 if ( $fl < $shorter ) { $shorter = $fl; }
1326 for ( $i = 0; $i < $shorter; ++
$i ) {
1327 if ( $st1{$i} != $st2{$i} ) { break; }
1331 # These next three functions open, continue, and close the list
1332 # element appropriate to the prefix character passed into them.
1334 /* private */ function openList( $char )
1336 $result = $this->closeParagraph();
1338 if ( '*' == $char ) { $result .= '<ul><li>'; }
1339 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1340 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1341 else if ( ';' == $char ) {
1342 $result .= '<dl><dt>';
1343 $this->mDTopen
= true;
1345 else { $result = '<!-- ERR 1 -->'; }
1350 /* private */ function nextItem( $char ) {
1351 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1352 else if ( ':' == $char ||
';' == $char ) {
1354 if ( $this->mDTopen
) { $close = '</dt>'; }
1355 if ( ';' == $char ) {
1356 $this->mDTopen
= true;
1357 return $close . '<dt>';
1359 $this->mDTopen
= false;
1360 return $close . '<dd>';
1363 return '<!-- ERR 2 -->';
1366 /* private */function closeList( $char ) {
1367 if ( '*' == $char ) { $text = '</li></ul>'; }
1368 else if ( '#' == $char ) { $text = '</li></ol>'; }
1369 else if ( ':' == $char ) {
1370 if ( $this->mDTopen
) {
1371 $this->mDTopen
= false;
1372 $text = '</dt></dl>';
1374 $text = '</dd></dl>';
1377 else { return '<!-- ERR 3 -->'; }
1381 /* private */ function doBlockLevels( $text, $linestart ) {
1382 $fname = 'Parser::doBlockLevels';
1383 wfProfileIn( $fname );
1385 # Parsing through the text line by line. The main thing
1386 # happening here is handling of block-level elements p, pre,
1387 # and making lists from lines starting with * # : etc.
1389 $textLines = explode( "\n", $text );
1391 $lastPrefix = $output = $lastLine = '';
1392 $this->mDTopen
= $inBlockElem = false;
1394 $paragraphStack = false;
1396 if ( !$linestart ) {
1397 $output .= array_shift( $textLines );
1399 foreach ( $textLines as $oLine ) {
1400 $lastPrefixLength = strlen( $lastPrefix );
1401 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1402 $preOpenMatch = preg_match("/<pre/i", $oLine );
1403 if ( !$this->mInPre
) {
1404 # Multiple prefixes may abut each other for nested lists.
1405 $prefixLength = strspn( $oLine, '*#:;' );
1406 $pref = substr( $oLine, 0, $prefixLength );
1409 $pref2 = str_replace( ';', ':', $pref );
1410 $t = substr( $oLine, $prefixLength );
1411 $this->mInPre
= !empty($preOpenMatch);
1413 # Don't interpret any other prefixes in preformatted text
1415 $pref = $pref2 = '';
1420 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1421 # Same as the last item, so no need to deal with nesting or opening stuff
1422 $output .= $this->nextItem( substr( $pref, -1 ) );
1423 $paragraphStack = false;
1425 if ( ";" == substr( $pref, -1 ) ) {
1426 # The one nasty exception: definition lists work like this:
1427 # ; title : definition text
1428 # So we check for : in the remainder text to split up the
1429 # title and definition, without b0rking links.
1430 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1431 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1433 $output .= $term . $this->nextItem( ':' );
1437 } elseif( $prefixLength ||
$lastPrefixLength ) {
1438 # Either open or close a level...
1439 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1440 $paragraphStack = false;
1442 while( $commonPrefixLength < $lastPrefixLength ) {
1443 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1444 --$lastPrefixLength;
1446 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1447 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1449 while ( $prefixLength > $commonPrefixLength ) {
1450 $char = substr( $pref, $commonPrefixLength, 1 );
1451 $output .= $this->openList( $char );
1453 if ( ';' == $char ) {
1454 # FIXME: This is dupe of code above
1455 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1457 $output .= $term . $this->nextItem( ":" );
1461 ++
$commonPrefixLength;
1463 $lastPrefix = $pref2;
1465 if( 0 == $prefixLength ) {
1466 # No prefix (not in list)--go to paragraph mode
1467 $uniq_prefix = UNIQ_PREFIX
;
1468 // XXX: use a stack for nestable elements like span, table and div
1469 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1470 $closematch = preg_match(
1471 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1472 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1473 if ( $openmatch or $closematch ) {
1474 $paragraphStack = false;
1475 $output .= $this->closeParagraph();
1476 if($preOpenMatch and !$preCloseMatch) {
1477 $this->mInPre
= true;
1479 if ( $closematch ) {
1480 $inBlockElem = false;
1482 $inBlockElem = true;
1484 } else if ( !$inBlockElem && !$this->mInPre
) {
1485 if ( " " == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1487 if ($this->mLastSection
!= 'pre') {
1488 $paragraphStack = false;
1489 $output .= $this->closeParagraph().'<pre>';
1490 $this->mLastSection
= 'pre';
1494 if ( '' == trim($t) ) {
1495 if ( $paragraphStack ) {
1496 $output .= $paragraphStack.'<br />';
1497 $paragraphStack = false;
1498 $this->mLastSection
= 'p';
1500 if ($this->mLastSection
!= 'p' ) {
1501 $output .= $this->closeParagraph();
1502 $this->mLastSection
= '';
1503 $paragraphStack = '<p>';
1505 $paragraphStack = '</p><p>';
1509 if ( $paragraphStack ) {
1510 $output .= $paragraphStack;
1511 $paragraphStack = false;
1512 $this->mLastSection
= 'p';
1513 } else if ($this->mLastSection
!= 'p') {
1514 $output .= $this->closeParagraph().'<p>';
1515 $this->mLastSection
= 'p';
1521 if ($paragraphStack === false) {
1525 while ( $prefixLength ) {
1526 $output .= $this->closeList( $pref2{$prefixLength-1} );
1529 if ( '' != $this->mLastSection
) {
1530 $output .= '</' . $this->mLastSection
. '>';
1531 $this->mLastSection
= '';
1534 wfProfileOut( $fname );
1538 # Return value of a magic variable (like PAGENAME)
1539 function getVariableValue( $index ) {
1540 global $wgLang, $wgSitename, $wgServer;
1543 case MAG_CURRENTMONTH
:
1544 return $wgLang->formatNum( date( 'm' ) );
1545 case MAG_CURRENTMONTHNAME
:
1546 return $wgLang->getMonthName( date('n') );
1547 case MAG_CURRENTMONTHNAMEGEN
:
1548 return $wgLang->getMonthNameGen( date('n') );
1549 case MAG_CURRENTDAY
:
1550 return $wgLang->formatNum( date('j') );
1552 return $this->mTitle
->getText();
1554 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1555 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1556 case MAG_CURRENTDAYNAME
:
1557 return $wgLang->getWeekdayName( date('w')+
1 );
1558 case MAG_CURRENTYEAR
:
1559 return $wgLang->formatNum( date( 'Y' ) );
1560 case MAG_CURRENTTIME
:
1561 return $wgLang->time( wfTimestampNow(), false );
1562 case MAG_NUMBEROFARTICLES
:
1563 return $wgLang->formatNum( wfNumberOfArticles() );
1573 # initialise the magic variables (like CURRENTMONTHNAME)
1574 function initialiseVariables() {
1575 global $wgVariableIDs;
1576 $this->mVariables
= array();
1577 foreach ( $wgVariableIDs as $id ) {
1578 $mw =& MagicWord
::get( $id );
1579 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1583 /* private */ function replaceVariables( $text, $args = array() ) {
1584 global $wgLang, $wgScript, $wgArticlePath;
1586 # Prevent too big inclusions
1587 if(strlen($text)> MAX_INCLUDE_SIZE
)
1590 $fname = 'Parser::replaceVariables';
1591 wfProfileIn( $fname );
1594 $titleChars = Title
::legalChars();
1595 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1597 # This function is called recursively. To keep track of arguments we need a stack:
1598 array_push( $this->mArgStack
, $args );
1600 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1601 $GLOBALS['wgCurParser'] =& $this;
1604 if ( $this->mOutputType
== OT_HTML
) {
1605 # Variable substitution
1606 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1608 # Argument substitution
1609 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1611 # Template substitution
1612 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1613 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1615 array_pop( $this->mArgStack
);
1617 wfProfileOut( $fname );
1621 function variableSubstitution( $matches ) {
1622 if ( !$this->mVariables
) {
1623 $this->initialiseVariables();
1625 if ( array_key_exists( $matches[1], $this->mVariables
) ) {
1626 $text = $this->mVariables
[$matches[1]];
1627 $this->mOutput
->mContainsOldMagic
= true;
1629 $text = $matches[0];
1634 # Split template arguments
1635 function getTemplateArgs( $argsString ) {
1636 if ( $argsString === '' ) {
1640 $args = explode( '|', substr( $argsString, 1 ) );
1642 # If any of the arguments contains a '[[' but no ']]', it needs to be
1643 # merged with the next arg because the '|' character between belongs
1644 # to the link syntax and not the template parameter syntax.
1645 $argc = count($args);
1647 for ( $i = 0; $i < $argc-1; $i++
) {
1648 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1649 $args[$i] .= "|".$args[$i+
1];
1650 array_splice($args, $i+
1, 1);
1659 function braceSubstitution( $matches ) {
1660 global $wgLinkCache, $wgLang;
1661 $fname = 'Parser::braceSubstitution';
1668 # $newline is an optional newline character before the braces
1669 # $part1 is the bit before the first |, and must contain only title characters
1670 # $args is a list of arguments, starting from index 0, not including $part1
1672 $newline = $matches[1];
1673 $part1 = $matches[2];
1674 # If the third subpattern matched anything, it will start with |
1676 $args = $this->getTemplateArgs($matches[3]);
1677 $argc = count( $args );
1680 if ( strpos( $matches[0], '{{{' ) !== false ) {
1681 $text = $matches[0];
1688 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1689 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1690 if ( $this->mOutputType
!= OT_WIKI
) {
1691 # Invalid SUBST not replaced at PST time
1692 # Return without further processing
1693 $text = $matches[0];
1697 } elseif ( $this->mOutputType
== OT_WIKI
) {
1698 # SUBST not found in PST pass, do nothing
1699 $text = $matches[0];
1704 # MSG, MSGNW and INT
1707 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1708 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1711 # Remove obsolete MSG:
1712 $mwMsg =& MagicWord
::get( MAG_MSG
);
1713 $mwMsg->matchStartAndRemove( $part1 );
1716 # Check if it is an internal message
1717 $mwInt =& MagicWord
::get( MAG_INT
);
1718 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1719 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1720 $text = wfMsgReal( $part1, $args, true );
1728 # Check for NS: (namespace expansion)
1729 $mwNs = MagicWord
::get( MAG_NS
);
1730 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1731 if ( intval( $part1 ) ) {
1732 $text = $wgLang->getNsText( intval( $part1 ) );
1735 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1736 if ( !is_null( $index ) ) {
1737 $text = $wgLang->getNsText( $index );
1744 # LOCALURL and LOCALURLE
1746 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1747 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1749 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1750 $func = 'getLocalURL';
1751 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1752 $func = 'escapeLocalURL';
1757 if ( $func !== '' ) {
1758 $title = Title
::newFromText( $part1 );
1759 if ( !is_null( $title ) ) {
1761 $text = $title->$func( $args[0] );
1763 $text = $title->$func();
1770 # Internal variables
1771 if ( !$this->mVariables
) {
1772 $this->initialiseVariables();
1774 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1775 $text = $this->mVariables
[$part1];
1777 $this->mOutput
->mContainsOldMagic
= true;
1780 # Template table test
1782 # Did we encounter this template already? If yes, it is in the cache
1783 # and we need to check for loops.
1784 if ( isset( $this->mTemplates
[$part1] ) ) {
1785 # Infinite loop test
1786 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1790 # set $text to cached message.
1791 $text = $this->mTemplates
[$part1];
1795 # Load from database
1797 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1798 if ( !is_null( $title ) && !$title->isExternal() ) {
1799 # Check for excessive inclusion
1800 $dbk = $title->getPrefixedDBkey();
1801 if ( $this->incrementIncludeCount( $dbk ) ) {
1802 # This should never be reached.
1803 $article = new Article( $title );
1804 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1805 if ( $articleContent !== false ) {
1807 $text = $articleContent;
1812 # If the title is valid but undisplayable, make a link to it
1813 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1814 $text = '[[' . $title->getPrefixedText() . ']]';
1818 # Template cache array insertion
1819 $this->mTemplates
[$part1] = $text;
1823 # Recursive parsing, escaping and link table handling
1824 # Only for HTML output
1825 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1826 $text = wfEscapeWikiText( $text );
1827 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1828 # Clean up argument array
1829 $assocArgs = array();
1831 foreach( $args as $arg ) {
1832 $eqpos = strpos( $arg, '=' );
1833 if ( $eqpos === false ) {
1834 $assocArgs[$index++
] = $arg;
1836 $name = trim( substr( $arg, 0, $eqpos ) );
1837 $value = trim( substr( $arg, $eqpos+
1 ) );
1838 if ( $value === false ) {
1841 if ( $name !== false ) {
1842 $assocArgs[$name] = $value;
1847 # Do not enter included links in link table
1848 if ( !is_null( $title ) ) {
1849 $wgLinkCache->suspend();
1852 # Add a new element to the templace recursion path
1853 $this->mTemplatePath
[$part1] = 1;
1855 # Run full parser on the included text
1856 $text = $this->internalParse( $text, $newline, $assocArgs );
1857 # I replaced the line below with the line above, as it former seems to cause several bugs
1858 #$text = $this->stripParse( $text, $newline, $assocArgs );
1860 # Resume the link cache and register the inclusion as a link
1861 if ( !is_null( $title ) ) {
1862 $wgLinkCache->resume();
1863 $wgLinkCache->addLinkObj( $title );
1866 # Empties the template path
1867 $this->mTemplatePath
= array();
1876 # Triple brace replacement -- used for template arguments
1877 function argSubstitution( $matches ) {
1878 $newline = $matches[1];
1879 $arg = trim( $matches[2] );
1880 $text = $matches[0];
1881 $inputArgs = end( $this->mArgStack
);
1883 if ( array_key_exists( $arg, $inputArgs ) ) {
1884 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1890 # Returns true if the function is allowed to include this entity
1891 function incrementIncludeCount( $dbk ) {
1892 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1893 $this->mIncludeCount
[$dbk] = 0;
1895 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1903 # Cleans up HTML, removes dangerous tags and attributes
1904 /* private */ function removeHTMLtags( $text ) {
1905 global $wgUseTidy, $wgUserHtml;
1906 $fname = 'Parser::removeHTMLtags';
1907 wfProfileIn( $fname );
1910 $htmlpairs = array( # Tags that must be closed
1911 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1912 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1913 'strike', 'strong', 'tt', 'var', 'div', 'center',
1914 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1915 'ruby', 'rt' , 'rb' , 'rp', 'p'
1917 $htmlsingle = array(
1918 'br', 'hr', 'li', 'dt', 'dd'
1920 $htmlnest = array( # Tags that can be nested--??
1921 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1922 'dl', 'font', 'big', 'small', 'sub', 'sup'
1924 $tabletags = array( # Can only appear inside table
1928 $htmlpairs = array();
1929 $htmlsingle = array();
1930 $htmlnest = array();
1931 $tabletags = array();
1934 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1935 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1937 $htmlattrs = $this->getHTMLattrs () ;
1939 # Remove HTML comments
1940 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1942 $bits = explode( '<', $text );
1943 $text = array_shift( $bits );
1945 $tagstack = array(); $tablestack = array();
1946 foreach ( $bits as $x ) {
1947 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1948 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1950 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1951 error_reporting( $prev );
1954 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1958 if ( ! in_array( $t, $htmlsingle ) &&
1959 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1960 @array_push
( $tagstack, $ot );
1963 if ( $t == 'table' ) {
1964 $tagstack = array_pop( $tablestack );
1969 # Keep track for later
1970 if ( in_array( $t, $tabletags ) &&
1971 ! in_array( 'table', $tagstack ) ) {
1973 } else if ( in_array( $t, $tagstack ) &&
1974 ! in_array ( $t , $htmlnest ) ) {
1976 } else if ( ! in_array( $t, $htmlsingle ) ) {
1977 if ( $t == 'table' ) {
1978 array_push( $tablestack, $tagstack );
1979 $tagstack = array();
1981 array_push( $tagstack, $t );
1983 # Strip non-approved attributes from the tag
1984 $newparams = $this->fixTagAttributes($params);
1988 $rest = str_replace( '>', '>', $rest );
1989 $text .= "<$slash$t $newparams$brace$rest";
1993 $text .= '<' . str_replace( '>', '>', $x);
1995 # Close off any remaining tags
1996 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1998 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2001 # this might be possible using tidy itself
2002 foreach ( $bits as $x ) {
2003 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2005 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2006 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2007 $newparams = $this->fixTagAttributes($params);
2008 $rest = str_replace( '>', '>', $rest );
2009 $text .= "<$slash$t $newparams$brace$rest";
2011 $text .= '<' . str_replace( '>', '>', $x);
2015 wfProfileOut( $fname );
2022 * This function accomplishes several tasks:
2023 * 1) Auto-number headings if that option is enabled
2024 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2025 * 3) Add a Table of contents on the top for users who have enabled the option
2026 * 4) Auto-anchor headings
2028 * It loops through all headlines, collects the necessary data, then splits up the
2029 * string and re-inserts the newly formatted headlines.
2033 /* private */ function formatHeadings( $text, $isMain=true ) {
2034 global $wgInputEncoding, $wgMaxTocLevel;
2036 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
2037 $doShowToc = $this->mOptions
->getShowToc();
2038 $forceTocHere = false;
2039 if( !$this->mTitle
->userCanEdit() ) {
2041 $rightClickHack = 0;
2043 $showEditLink = $this->mOptions
->getEditSection();
2044 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
2047 # Inhibit editsection links if requested in the page
2048 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
2049 if( $esw->matchAndRemove( $text ) ) {
2052 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2054 $mw =& MagicWord
::get( MAG_NOTOC
);
2055 if( $mw->matchAndRemove( $text ) ) {
2059 # never add the TOC to the Main Page. This is an entry page that should not
2060 # be more than 1-2 screens large anyway
2061 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
2065 # Get all headlines for numbering them and adding funky stuff like [edit]
2066 # links - this is for later, but we need the number of headlines right now
2067 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2069 # if there are fewer than 4 headlines in the article, do not show TOC
2070 if( $numMatches < 4 ) {
2074 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2075 # override above conditions and always show TOC at that place
2076 $mw =& MagicWord
::get( MAG_TOC
);
2077 if ($mw->match( $text ) ) {
2079 $forceTocHere = true;
2081 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2082 # override above conditions and always show TOC above first header
2083 $mw =& MagicWord
::get( MAG_FORCETOC
);
2084 if ($mw->matchAndRemove( $text ) ) {
2091 # We need this to perform operations on the HTML
2092 $sk =& $this->mOptions
->getSkin();
2097 # Ugh .. the TOC should have neat indentation levels which can be
2098 # passed to the skin functions. These are determined here
2103 $sublevelCount = array();
2106 foreach( $matches[3] as $headline ) {
2109 $prevlevel = $level;
2111 $level = $matches[1][$headlineCount];
2112 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
2113 # reset when we enter a new level
2114 $sublevelCount[$level] = 0;
2115 $toc .= $sk->tocIndent( $level - $prevlevel );
2116 $toclevel +
= $level - $prevlevel;
2118 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
2119 # reset when we step back a level
2120 $sublevelCount[$level+
1]=0;
2121 $toc .= $sk->tocUnindent( $prevlevel - $level );
2122 $toclevel -= $prevlevel - $level;
2124 # count number of headlines for each level
2125 @$sublevelCount[$level]++
;
2126 if( $doNumberHeadings ||
$doShowToc ) {
2128 for( $i = 1; $i <= $level; $i++
) {
2129 if( !empty( $sublevelCount[$i] ) ) {
2133 $numbering .= $sublevelCount[$i];
2139 # The canonized header is a version of the header text safe to use for links
2140 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2141 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
2142 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
2145 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2146 $tocline = trim( $canonized_headline );
2147 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
2148 $replacearray = array(
2152 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2153 $refer[$headlineCount] = $canonized_headline;
2155 # count how many in assoc. array so we can track dupes in anchors
2156 @$refers[$canonized_headline]++
;
2157 $refcount[$headlineCount]=$refers[$canonized_headline];
2159 # Prepend the number to the heading text
2161 if( $doNumberHeadings ||
$doShowToc ) {
2162 $tocline = $numbering . ' ' . $tocline;
2164 # Don't number the heading if it is the only one (looks silly)
2165 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2166 # the two are different if the line contains a link
2167 $headline=$numbering . ' ' . $headline;
2171 # Create the anchor for linking from the TOC to the section
2172 $anchor = $canonized_headline;
2173 if($refcount[$headlineCount] > 1 ) {
2174 $anchor .= '_' . $refcount[$headlineCount];
2176 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
2177 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2179 if( $showEditLink ) {
2180 if ( empty( $head[$headlineCount] ) ) {
2181 $head[$headlineCount] = '';
2183 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
2186 # Add the edit section span
2187 if( $rightClickHack ) {
2188 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
2191 # give headline the correct <h#> tag
2192 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2198 $toclines = $headlineCount;
2199 $toc .= $sk->tocUnindent( $toclevel );
2200 $toc = $sk->tocTable( $toc );
2203 # split up and insert constructed headlines
2205 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2208 foreach( $blocks as $block ) {
2209 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2210 # This is the [edit] link that appears for the top block of text when
2211 # section editing is enabled
2213 # Disabled because it broke block formatting
2214 # For example, a bullet point in the top line
2215 # $full .= $sk->editSectionLink(0);
2218 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2219 # Top anchor now in skin
2223 if( !empty( $head[$i] ) ) {
2229 $mw =& MagicWord
::get( MAG_TOC
);
2230 return $mw->replace( $toc, $full );
2236 # Return an HTML link for the "ISBN 123456" text
2237 /* private */ function magicISBN( $text ) {
2239 $fname = 'Parser::magicISBN';
2240 wfProfileIn( $fname );
2242 $a = split( 'ISBN ', " $text" );
2243 if ( count ( $a ) < 2 ) {
2244 wfProfileOut( $fname );
2247 $text = substr( array_shift( $a ), 1);
2248 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2250 foreach ( $a as $x ) {
2251 $isbn = $blank = '' ;
2252 while ( ' ' == $x{0} ) {
2254 $x = substr( $x, 1 );
2256 while ( strstr( $valid, $x{0} ) != false ) {
2258 $x = substr( $x, 1 );
2260 $num = str_replace( '-', '', $isbn );
2261 $num = str_replace( ' ', '', $num );
2264 $text .= "ISBN $blank$x";
2266 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2267 $text .= '<a href="' .
2268 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2269 "\" class=\"internal\">ISBN $isbn</a>";
2273 wfProfileOut( $fname );
2277 # Return an HTML link for the "GEO ..." text
2278 /* private */ function magicGEO( $text ) {
2279 global $wgLang, $wgUseGeoMode;
2280 if ( !isset ( $wgUseGeoMode ) ||
!$wgUseGeoMode ) return $text ;
2281 $fname = 'Parser::magicGEO';
2282 wfProfileIn( $fname );
2284 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2285 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2286 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2287 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2288 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2289 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2291 $a = split( 'GEO ', " $text" );
2292 if ( count ( $a ) < 2 ) {
2293 wfProfileOut( $fname );
2296 $text = substr( array_shift( $a ), 1);
2297 $valid = '0123456789.+-:';
2299 foreach ( $a as $x ) {
2300 $geo = $blank = '' ;
2301 while ( ' ' == $x{0} ) {
2303 $x = substr( $x, 1 );
2305 while ( strstr( $valid, $x{0} ) != false ) {
2307 $x = substr( $x, 1 );
2309 $num = str_replace( '+', '', $geo );
2310 $num = str_replace( ' ', '', $num );
2312 if ( '' == $num ||
count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2313 $text .= "GEO $blank$x";
2315 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2316 $text .= '<a href="' .
2317 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2318 "\" class=\"internal\">GEO $geo</a>";
2322 wfProfileOut( $fname );
2326 # Return an HTML link for the "RFC 1234" text
2327 /* private */ function magicRFC( $text ) {
2330 $a = split( 'RFC ', ' '.$text );
2331 if ( count ( $a ) < 2 ) return $text;
2332 $text = substr( array_shift( $a ), 1);
2333 $valid = '0123456789';
2335 foreach ( $a as $x ) {
2336 $rfc = $blank = '' ;
2337 while ( ' ' == $x{0} ) {
2339 $x = substr( $x, 1 );
2341 while ( strstr( $valid, $x{0} ) != false ) {
2343 $x = substr( $x, 1 );
2347 $text .= "RFC $blank$x";
2349 $url = wfmsg( 'rfcurl' );
2350 $url = str_replace( '$1', $rfc, $url);
2351 $sk =& $this->mOptions
->getSkin();
2352 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2353 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2359 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2360 $this->mOptions
= $options;
2361 $this->mTitle
=& $title;
2362 $this->mOutputType
= OT_WIKI
;
2364 if ( $clearState ) {
2365 $this->clearState();
2368 $stripState = false;
2372 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2376 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2377 "/<br *?>/i" => "<br />",
2379 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2381 $text = $this->strip( $text, $stripState, false );
2382 $text = $this->pstPass2( $text, $user );
2383 $text = $this->unstrip( $text, $stripState );
2384 $text = $this->unstripNoWiki( $text, $stripState );
2388 /* private */ function pstPass2( $text, &$user ) {
2389 global $wgLang, $wgLocaltimezone, $wgCurParser;
2391 # Variable replacement
2392 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2393 $text = $this->replaceVariables( $text );
2397 $n = $user->getName();
2398 $k = $user->getOption( 'nickname' );
2399 if ( '' == $k ) { $k = $n; }
2400 if(isset($wgLocaltimezone)) {
2401 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2403 /* Note: this is an ugly timezone hack for the European wikis */
2404 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2405 ' (' . date( 'T' ) . ')';
2406 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2408 $text = preg_replace( '/~~~~~/', $d, $text );
2409 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2410 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2412 # Context links: [[|name]] and [[name (context)|]]
2414 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2415 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2416 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2417 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2419 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2420 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2421 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2422 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2423 # [[ns:page (cont)|]]
2425 $t = $this->mTitle
->getText();
2426 if ( preg_match( $conpat, $t, $m ) ) {
2429 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2430 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2431 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2433 if ( '' == $context ) {
2434 $text = preg_replace( $p2, '[[\\1]]', $text );
2436 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2440 $mw =& MagicWord::get( MAG_SUBST );
2441 $wgCurParser = $this->fork();
2442 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2443 $this->merge( $wgCurParser );
2446 # Trim trailing whitespace
2447 # MAG_END (__END__) tag allows for trailing
2448 # whitespace to be deliberately included
2449 $text = rtrim( $text );
2450 $mw =& MagicWord
::get( MAG_END
);
2451 $mw->matchAndRemove( $text );
2456 # Set up some variables which are usually set up in parse()
2457 # so that an external function can call some class members with confidence
2458 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2459 $this->mTitle
=& $title;
2460 $this->mOptions
= $options;
2461 $this->mOutputType
= $outputType;
2462 if ( $clearState ) {
2463 $this->clearState();
2467 function transformMsg( $text, $options ) {
2469 static $executing = false;
2471 # Guard against infinite recursion
2477 $this->mTitle
= $wgTitle;
2478 $this->mOptions
= $options;
2479 $this->mOutputType
= OT_MSG
;
2480 $this->clearState();
2481 $text = $this->replaceVariables( $text );
2487 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2488 # Callback will be called with the text within
2489 # Transform and return the text within
2490 function setHook( $tag, $callback ) {
2491 $oldVal = @$this->mTagHooks
[$tag];
2492 $this->mTagHooks
[$tag] = $callback;
2499 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2500 var $mCacheTime; # Used in ParserCache
2502 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2503 $containsOldMagic = false )
2505 $this->mText
= $text;
2506 $this->mLanguageLinks
= $languageLinks;
2507 $this->mCategoryLinks
= $categoryLinks;
2508 $this->mContainsOldMagic
= $containsOldMagic;
2509 $this->mCacheTime
= "";
2512 function getText() { return $this->mText
; }
2513 function getLanguageLinks() { return $this->mLanguageLinks
; }
2514 function getCategoryLinks() { return $this->mCategoryLinks
; }
2515 function getCacheTime() { return $this->mCacheTime
; }
2516 function containsOldMagic() { return $this->mContainsOldMagic
; }
2517 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2518 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2519 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2520 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2521 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2523 function merge( $other ) {
2524 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2525 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2526 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2533 # All variables are private
2534 var $mUseTeX; # Use texvc to expand <math> tags
2535 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2536 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2537 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2538 var $mAllowExternalImages; # Allow external images inline
2539 var $mSkin; # Reference to the preferred skin
2540 var $mDateFormat; # Date format index
2541 var $mEditSection; # Create "edit section" links
2542 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2543 var $mNumberHeadings; # Automatically number headings
2544 var $mShowToc; # Show table of contents
2546 function getUseTeX() { return $this->mUseTeX
; }
2547 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2548 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2549 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2550 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2551 function getSkin() { return $this->mSkin
; }
2552 function getDateFormat() { return $this->mDateFormat
; }
2553 function getEditSection() { return $this->mEditSection
; }
2554 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2555 function getNumberHeadings() { return $this->mNumberHeadings
; }
2556 function getShowToc() { return $this->mShowToc
; }
2558 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2559 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2560 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2561 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2562 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2563 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2564 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2565 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2566 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2567 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2569 function setSkin( &$x ) { $this->mSkin
=& $x; }
2571 /* static */ function newFromUser( &$user ) {
2572 $popts = new ParserOptions
;
2573 $popts->initialiseFromUser( $user );
2577 function initialiseFromUser( &$userInput ) {
2578 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2580 if ( !$userInput ) {
2582 $user->setLoaded( true );
2584 $user =& $userInput;
2587 $this->mUseTeX
= $wgUseTeX;
2588 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2589 $this->mUseDynamicDates
= $wgUseDynamicDates;
2590 $this->mInterwikiMagic
= $wgInterwikiMagic;
2591 $this->mAllowExternalImages
= $wgAllowExternalImages;
2592 $this->mSkin
=& $user->getSkin();
2593 $this->mDateFormat
= $user->getOption( 'date' );
2594 $this->mEditSection
= $user->getOption( 'editsection' );
2595 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2596 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2597 $this->mShowToc
= $user->getOption( 'showtoc' );
2603 # Regex callbacks, used in Parser::replaceVariables
2604 function wfBraceSubstitution( $matches )
2606 global $wgCurParser;
2607 return $wgCurParser->braceSubstitution( $matches );
2610 function wfArgSubstitution( $matches )
2612 global $wgCurParser;
2613 return $wgCurParser->argSubstitution( $matches );
2616 function wfVariableSubstitution( $matches )
2618 global $wgCurParser;
2619 return $wgCurParser->variableSubstitution( $matches );