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 );
35 # Allowed values for $mOutputType
36 define( "OT_HTML", 1 );
37 define( "OT_WIKI", 2 );
38 define( "OT_MSG", 3 );
40 # string parameter for extractTags which will cause it
41 # to strip HTML comments in addition to regular
42 # <XML>-style tags. This should not be anything we
43 # may want to use in wikisyntax
44 define( "STRIP_COMMENTS", "HTMLCommentStrip" );
46 # prefix for escaping, used in two functions at least
47 define( "UNIQ_PREFIX", "NaodW29");
54 # Cleared with clearState():
55 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
56 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
59 var $mOptions, $mTitle, $mOutputType;
62 $this->mTagHooks
= array();
66 function clearState() {
67 $this->mOutput
= new ParserOutput
;
68 $this->mAutonumber
= 0;
69 $this->mLastSection
= "";
70 $this->mDTopen
= false;
71 $this->mVariables
= false;
72 $this->mIncludeCount
= array();
73 $this->mStripState
= array();
74 $this->mArgStack
= array();
75 $this->mInPre
= false;
78 # First pass--just handle <nowiki> sections, pass the rest off
79 # to internalParse() which does all the real work.
81 # Returns a ParserOutput
83 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
85 $fname = "Parser::parse";
86 wfProfileIn( $fname );
92 $this->mOptions
= $options;
93 $this->mTitle
=& $title;
94 $this->mOutputType
= OT_HTML
;
97 $text = $this->strip( $text, $this->mStripState
);
98 $text = $this->internalParse( $text, $linestart );
99 $text = $this->unstrip( $text, $this->mStripState
);
100 # Clean up special characters, only run once, next-to-last before doBlockLevels
103 # french spaces, last one Guillemet-left
104 # only if there is something before the space
105 '/(.) (\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
106 # french spaces, Guillemet-right
107 "/(\\302\\253) /i"=>"\\1 ",
108 '/<hr *>/i' => '<hr />',
109 '/<br *>/i' => '<br />',
110 '/<center *>/i' => '<div class="center">',
111 '/<\\/center *>/i' => '</div>',
112 # Clean up spare ampersands; note that we probably ought to be
113 # more careful about named entities.
114 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
116 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
119 # french spaces, last one Guillemet-left
120 '/ (\\?|:|!|\\302\\273)/i' => ' \\1',
121 # french spaces, Guillemet-right
122 '/(\\302\\253) /i' => '\\1 ',
123 '/([^> ]+(0(1|3|9);)[^< ]*)/i' => '<span class="diacrit">\\1</span>',
124 '/<center *>/i' => '<div class="center">',
125 '/<\\/center *>/i' => '</div>'
127 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
130 $text = $this->doBlockLevels( $text, $linestart );
131 $text = $this->unstripNoWiki( $text, $this->mStripState
);
133 $text = $this->tidy($text);
135 $this->mOutput
->setText( $text );
136 wfProfileOut( $fname );
137 return $this->mOutput
;
140 /* static */ function getRandomString() {
141 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
144 # Replaces all occurrences of <$tag>content</$tag> in the text
145 # with a random marker and returns the new text. the output parameter
146 # $content will be an associative array filled with data on the form
147 # $unique_marker => content.
149 # If $content is already set, the additional entries will be appended
151 # If $tag is set to STRIP_COMMENTS, the function will extract
152 # <!-- HTML comments -->
154 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
155 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
162 while ( '' != $text ) {
163 if($tag==STRIP_COMMENTS
) {
164 $p = preg_split( '/<!--/i', $text, 2 );
166 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
169 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
172 if($tag==STRIP_COMMENTS
) {
173 $q = preg_split( '/-->/i', $p[1], 2 );
175 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
177 $marker = $rnd . sprintf('%08X', $n++
);
178 $content[$marker] = $q[0];
179 $stripped .= $marker;
186 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
187 # If $render is set, performs necessary rendering operations on plugins
188 # Returns the text, and fills an array with data needed in unstrip()
189 # If the $state is already a valid strip state, it adds to the state
191 # When $stripcomments is set, HTML comments <!-- like this -->
192 # will be stripped in addition to other tags. This is important
193 # for section editing, where these comments cause confusion when
194 # counting the sections in the wikisource
195 function strip( $text, &$state, $stripcomments = false ) {
196 $render = ($this->mOutputType
== OT_HTML
);
197 $nowiki_content = array();
198 $math_content = array();
199 $pre_content = array();
200 $comment_content = array();
201 $ext_content = array();
203 # Replace any instances of the placeholders
204 $uniq_prefix = UNIQ_PREFIX
;
205 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
209 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
210 foreach( $nowiki_content as $marker => $content ){
212 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
214 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
219 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
220 foreach( $math_content as $marker => $content ){
222 if( $this->mOptions
->getUseTeX() ) {
223 $math_content[$marker] = renderMath( $content );
225 $math_content[$marker] = "<math>$content<math>";
228 $math_content[$marker] = "<math>$content</math>";
233 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
234 foreach( $pre_content as $marker => $content ){
236 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
238 $pre_content[$marker] = "<pre>$content</pre>";
244 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
245 foreach( $comment_content as $marker => $content ){
246 $comment_content[$marker] = "<!--$content-->";
251 foreach ( $this->mTagHooks
as $tag => $callback ) {
252 $ext_contents[$tag] = array();
253 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
254 foreach( $ext_content[$tag] as $marker => $content ) {
256 $ext_content[$tag][$marker] = $callback( $content );
258 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
263 # Merge state with the pre-existing state, if there is one
265 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
266 $state['math'] = $state['math'] +
$math_content;
267 $state['pre'] = $state['pre'] +
$pre_content;
268 $state['comment'] = $state['comment'] +
$comment_content;
270 foreach( $ext_content as $tag => $array ) {
271 if ( array_key_exists( $tag, $state ) ) {
272 $state[$tag] = $state[$tag] +
$array;
277 'nowiki' => $nowiki_content,
278 'math' => $math_content,
279 'pre' => $pre_content,
280 'comment' => $comment_content,
286 # always call unstripNoWiki() after this one
287 function unstrip( $text, &$state ) {
288 # Must expand in reverse order, otherwise nested tags will be corrupted
289 $contentDict = end( $state );
290 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
291 if( key($state) != 'nowiki') {
292 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
293 $text = str_replace( key( $contentDict ), $content, $text );
300 # always call this after unstrip() to preserve the order
301 function unstripNoWiki( $text, &$state ) {
302 # Must expand in reverse order, otherwise nested tags will be corrupted
303 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
304 $text = str_replace( key( $state['nowiki'] ), $content, $text );
310 # Add an item to the strip state
311 # Returns the unique tag which must be inserted into the stripped text
312 # The tag will be replaced with the original text in unstrip()
314 function insertStripItem( $text, &$state ) {
315 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
323 $state['item'][$rnd] = $text;
328 # generate a list of subcategories and pages for a category
329 # depending on wfMsg("usenewcategorypage") it either calls the new
330 # or the old code. The new code will not work properly for some
331 # languages due to sorting issues, so they might want to turn it
333 function categoryMagic() {
334 $msg = wfMsg('usenewcategorypage');
335 if ( '0' == @$msg[0] )
337 return $this->oldCategoryMagic();
339 return $this->newCategoryMagic();
343 # This method generates the list of subcategories and pages for a category
344 function oldCategoryMagic () {
345 global $wgLang , $wgUser ;
346 $fname = 'Parser::oldCategoryMagic';
348 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
350 $cns = Namespace::getCategory() ;
351 if ( $this->mTitle
->getNamespace() != $cns ) return "" ; # This ain't a category page
353 $r = "<br style=\"clear:both;\"/>\n";
356 $sk =& $wgUser->getSkin() ;
358 $articles = array() ;
359 $children = array() ;
361 $id = $this->mTitle
->getArticleID() ;
364 $dbr =& wfGetDB( DB_READ
);
365 $cur = $dbr->tableName( 'cur' );
366 $categorylinks = $dbr->tableName( 'categorylinks' );
368 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
369 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
370 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
371 $res = $dbr->query( $sql, $fname ) ;
372 while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
374 # For all pages that link to this category
375 foreach ( $data AS $x )
377 $t = $wgLang->getNsText ( $x->cur_namespace
) ;
378 if ( $t != "" ) $t .= ":" ;
379 $t .= $x->cur_title
;
381 if ( $x->cur_namespace
== $cns ) {
382 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
384 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
387 $dbr->freeResult ( $res ) ;
389 # Showing subcategories
390 if ( count ( $children ) > 0 ) {
391 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
392 $r .= implode ( ', ' , $children ) ;
395 # Showing pages in this category
396 if ( count ( $articles ) > 0 ) {
397 $ti = $this->mTitle
->getText() ;
398 $h = wfMsg( 'category_header', $ti );
399 $r .= "<h2>{$h}</h2>\n" ;
400 $r .= implode ( ', ' , $articles ) ;
408 function newCategoryMagic () {
409 global $wgLang , $wgUser ;
410 if ( !$this->mOptions
->getUseCategoryMagic() ) return ; # Doesn't use categories at all
412 $cns = Namespace::getCategory() ;
413 if ( $this->mTitle
->getNamespace() != $cns ) return '' ; # This ain't a category page
415 $r = "<br style=\"clear:both;\"/>\n";
418 $sk =& $wgUser->getSkin() ;
420 $articles = array() ;
421 $articles_start_char = array();
422 $children = array() ;
423 $children_start_char = array();
425 $id = $this->mTitle
->getArticleID() ;
428 $dbr =& wfGetDB( DB_READ
);
429 $cur = $dbr->tableName( 'cur' );
430 $categorylinks = $dbr->tableName( 'categorylinks' );
432 $t = $dbr->strencode( $this->mTitle
->getDBKey() );
433 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
434 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
435 $res = $dbr->query ( $sql ) ;
436 while ( $x = $dbr->fetchObject ( $res ) )
438 $t = $ns = $wgLang->getNsText ( $x->cur_namespace
) ;
439 if ( $t != '' ) $t .= ':' ;
440 $t .= $x->cur_title
;
442 if ( $x->cur_namespace
== $cns ) {
443 $ctitle = str_replace( '_',' ',$x->cur_title
);
444 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
446 // If there's a link from Category:A to Category:B, the sortkey of the resulting
447 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
448 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
449 // else use sortkey...
450 if ( ($ns.":".$ctitle) == $x->cl_sortkey
) {
451 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title
) );
453 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
456 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
457 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey
) ) ;
460 $dbr->freeResult ( $res ) ;
462 $ti = $this->mTitle
->getText() ;
464 # Don't show subcategories section if there are none.
465 if ( count ( $children ) > 0 )
467 # Showing subcategories
468 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
469 . wfMsg( 'subcategorycount', count( $children ) );
470 if ( count ( $children ) > 6 ) {
472 // divide list into three equal chunks
473 $chunk = (int) (count ( $children ) / 3);
475 // get and display header
476 $r .= '<table width="100%"><tr valign="top">';
481 // loop through the chunks
482 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
484 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
488 // output all subcategories to category
489 for ($index = $startChunk ;
490 $index < $endChunk && $index < count($children);
493 // check for change of starting letter or begging of chunk
494 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
495 ||
($index == $startChunk) )
497 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
500 $r .= "<li>{$children[$index]}</li>";
506 $r .= '</tr></table>';
508 // for short lists of subcategories to category.
510 $r .= "<h3>{$children_start_char[0]}</h3>\n";
511 $r .= '<ul><li>'.$children[0].'</li>';
512 for ($index = 1; $index < count($children); $index++
)
514 if ($children_start_char[$index] != $children_start_char[$index - 1])
516 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
519 $r .= "<li>{$children[$index]}</li>";
523 } # END of if ( count($children) > 0 )
525 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
526 wfMsg( 'categoryarticlecount', count( $articles ) );
528 # Showing articles in this category
529 if ( count ( $articles ) > 6) {
530 $ti = $this->mTitle
->getText() ;
532 // divide list into three equal chunks
533 $chunk = (int) (count ( $articles ) / 3);
535 // get and display header
536 $r .= '<table width="100%"><tr valign="top">';
538 // loop through the chunks
539 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
541 $chunkIndex++
, $startChunk = $endChunk, $endChunk +
= $chunk +
1)
546 // output all articles in category
547 for ($index = $startChunk ;
548 $index < $endChunk && $index < count($articles);
551 // check for change of starting letter or begging of chunk
552 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
553 ||
($index == $startChunk) )
555 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
558 $r .= "<li>{$articles[$index]}</li>";
564 $r .= '</tr></table>';
565 } elseif ( count ( $articles ) > 0) {
566 // for short lists of articles in categories.
567 $ti = $this->mTitle
->getText() ;
569 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
570 $r .= '<ul><li>'.$articles[0].'</li>';
571 for ($index = 1; $index < count($articles); $index++
)
573 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
575 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
578 $r .= "<li>{$articles[$index]}</li>";
587 # Return allowed HTML attributes
588 function getHTMLattrs () {
589 $htmlattrs = array( # Allowed attributes--no scripting, etc.
590 'title', 'align', 'lang', 'dir', 'width', 'height',
591 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
592 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
593 /* FONT */ 'type', 'start', 'value', 'compact',
594 /* For various lists, mostly deprecated but safe */
595 'summary', 'width', 'border', 'frame', 'rules',
596 'cellspacing', 'cellpadding', 'valign', 'char',
597 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
598 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
599 'id', 'class', 'name', 'style' /* For CSS */
604 # Remove non approved attributes and javascript in css
605 function fixTagAttributes ( $t ) {
606 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
607 $htmlattrs = $this->getHTMLattrs() ;
609 # Strip non-approved attributes from the tag
611 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
612 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
614 # Strip javascript "expression" from stylesheets. Brute force approach:
615 # If anythin offensive is found, all attributes of the HTML tag are dropped
618 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
619 wfMungeToUtf8( $t ) ) )
627 # interface with html tidy, used if $wgUseTidy = true
628 function tidy ( $text ) {
629 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
630 global $wgInputEncoding, $wgOutputEncoding;
631 $fname = 'Parser::tidy';
632 wfProfileIn( $fname );
635 switch(strtoupper($wgOutputEncoding)) {
637 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
640 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
643 $wgTidyOpts .= ' -raw';
646 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
647 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
648 '<head><title>test</title></head><body>'.$text.'</body></html>';
649 $descriptorspec = array(
650 0 => array('pipe', 'r'),
651 1 => array('pipe', 'w'),
652 2 => array('file', '/dev/null', 'a')
654 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
655 if (is_resource($process)) {
656 fwrite($pipes[0], $wrappedtext);
658 while (!feof($pipes[1])) {
659 $cleansource .= fgets($pipes[1], 1024);
662 $return_value = proc_close($process);
665 wfProfileOut( $fname );
667 if( $cleansource == '' && $text != '') {
668 wfDebug( "Tidy error detected!\n" );
669 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
675 # parse the wiki syntax used to render tables
676 function doTableStuff ( $t ) {
677 $fname = 'Parser::doTableStuff';
678 wfProfileIn( $fname );
680 $t = explode ( "\n" , $t ) ;
681 $td = array () ; # Is currently a td tag open?
682 $ltd = array () ; # Was it TD or TH?
683 $tr = array () ; # Is currently a tr tag open?
684 $ltr = array () ; # tr attributes
685 foreach ( $t AS $k => $x )
688 $fc = substr ( $x , 0 , 1 ) ;
689 if ( '{|' == substr ( $x , 0 , 2 ) )
691 $t[$k] = "\n<table " . $this->fixTagAttributes ( substr ( $x , 2 ) ) . '>' ;
692 array_push ( $td , false ) ;
693 array_push ( $ltd , '' ) ;
694 array_push ( $tr , false ) ;
695 array_push ( $ltr , '' ) ;
697 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
698 else if ( '|}' == substr ( $x , 0 , 2 ) )
701 $l = array_pop ( $ltd ) ;
702 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
703 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
707 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
709 $x = substr ( $x , 1 ) ;
710 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
712 $l = array_pop ( $ltd ) ;
713 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
714 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
717 array_push ( $tr , false ) ;
718 array_push ( $td , false ) ;
719 array_push ( $ltd , '' ) ;
720 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
722 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) # Caption
724 if ( '|+' == substr ( $x , 0 , 2 ) )
727 $x = substr ( $x , 1 ) ;
729 $after = substr ( $x , 1 ) ;
730 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
731 $after = explode ( '||' , $after ) ;
733 foreach ( $after AS $theline )
738 $tra = array_pop ( $ltr ) ;
739 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
740 array_push ( $tr , true ) ;
741 array_push ( $ltr , '' ) ;
744 $l = array_pop ( $ltd ) ;
745 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
746 if ( $fc == '|' ) $l = 'td' ;
747 else if ( $fc == '!' ) $l = 'th' ;
748 else if ( $fc == '+' ) $l = 'caption' ;
750 array_push ( $ltd , $l ) ;
751 $y = explode ( '|' , $theline , 2 ) ;
752 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
753 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
755 array_push ( $td , true ) ;
760 # Closing open td, tr && table
761 while ( count ( $td ) > 0 )
763 if ( array_pop ( $td ) ) $t[] = '</td>' ;
764 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
768 $t = implode ( "\n" , $t ) ;
769 # $t = $this->removeHTMLtags( $t );
770 wfProfileOut( $fname );
774 # Parses the text and adds the result to the strip state
775 # Returns the strip tag
776 function stripParse( $text, $newline, $args )
778 $text = $this->strip( $text, $this->mStripState
);
779 $text = $this->internalParse( $text, (bool)$newline, $args, false );
780 return $newline.$this->insertStripItem( $text, $this->mStripState
);
783 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
784 $fname = 'Parser::internalParse';
785 wfProfileIn( $fname );
787 $text = $this->removeHTMLtags( $text );
788 $text = $this->replaceVariables( $text, $args );
790 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
792 $text = $this->doHeadings( $text );
793 if($this->mOptions
->getUseDynamicDates()) {
794 global $wgDateFormatter;
795 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
797 $text = $this->doAllQuotes( $text );
798 // $text = $this->doExponent( $text );
799 $text = $this->replaceExternalLinks( $text );
800 $text = $this->replaceInternalLinks ( $text );
801 $text = $this->replaceInternalLinks ( $text );
802 //$text = $this->doTokenizedParser ( $text );
803 $text = $this->doTableStuff ( $text ) ;
804 $text = $this->magicISBN( $text );
805 $text = $this->magicRFC( $text );
806 $text = $this->formatHeadings( $text, $isMain );
807 $sk =& $this->mOptions
->getSkin();
808 $text = $sk->transformContent( $text );
810 if ( !isset ( $this->categoryMagicDone
) ) {
811 $text .= $this->categoryMagic () ;
812 $this->categoryMagicDone
= true ;
815 wfProfileOut( $fname );
819 # Parse ^^ tokens and return html
820 /* private */ function doExponent ( $text )
822 $fname = 'Parser::doExponent';
823 wfProfileIn( $fname);
824 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
825 wfProfileOut( $fname);
829 # Parse headers and return html
830 /* private */ function doHeadings( $text ) {
831 $fname = 'Parser::doHeadings';
832 wfProfileIn( $fname );
833 for ( $i = 6; $i >= 1; --$i ) {
834 $h = substr( '======', 0, $i );
835 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
836 "<h{$i}>\\1</h{$i}>\\2", $text );
838 wfProfileOut( $fname );
842 /* private */ function doAllQuotes( $text ) {
843 $fname = 'Parser::doAllQuotes';
844 wfProfileIn( $fname );
846 $lines = explode( "\n", $text );
847 foreach ( $lines as $line ) {
848 $outtext .= $this->doQuotes ( '', $line, '' ) . "\n";
850 $outtext = substr($outtext, 0,-1);
851 wfProfileOut( $fname );
855 /* private */ function doQuotes( $pre, $text, $mode ) {
856 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
857 $m1_strong = ($m[1] == "") ?
"" : "<strong>{$m[1]}</strong>";
858 $m1_em = ($m[1] == "") ?
"" : "<em>{$m[1]}</em>";
859 if ( substr ($m[2], 0, 1) == '\'' ) {
860 $m[2] = substr ($m[2], 1);
862 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ?
'both' : 'emstrong' );
863 } else if ($mode == 'strong') {
864 return $m1_strong . $this->doQuotes ( '', $m[2], '' );
865 } else if (($mode == 'emstrong') ||
($mode == 'both')) {
866 return $this->doQuotes ( '', $pre.$m1_strong.$m[2], 'em' );
867 } else if ($mode == 'strongem') {
868 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( '', $m[2], 'em' );
870 return $m[1] . $this->doQuotes ( '', $m[2], 'strong' );
873 if ($mode == 'strong') {
874 return $this->doQuotes ( $m[1], $m[2], ($m[1] == '') ?
'both' : 'strongem' );
875 } else if ($mode == 'em') {
876 return $m1_em . $this->doQuotes ( '', $m[2], '' );
877 } else if ($mode == 'emstrong') {
878 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( '', $m[2], 'strong' );
879 } else if (($mode == 'strongem') ||
($mode == 'both')) {
880 return $this->doQuotes ( '', $pre.$m1_em.$m[2], 'strong' );
882 return $m[1] . $this->doQuotes ( '', $m[2], 'em' );
886 $text_strong = ($text == '') ?
'' : "<strong>{$text}</strong>";
887 $text_em = ($text == '') ?
'' : "<em>{$text}</em>";
890 } else if ($mode == 'em') {
891 return $pre . $text_em;
892 } else if ($mode == 'strong') {
893 return $pre . $text_strong;
894 } else if ($mode == 'strongem') {
895 return (($pre == '') && ($text == '')) ?
'' : "<strong>{$pre}{$text_em}</strong>";
897 return (($pre == '') && ($text == '')) ?
'' : "<em>{$pre}{$text_strong}</em>";
902 # Note: we have to do external links before the internal ones,
903 # and otherwise take great care in the order of things here, so
904 # that we don't end up interpreting some URLs twice.
906 /* private */ function replaceExternalLinks( $text ) {
907 $fname = 'Parser::replaceExternalLinks';
908 wfProfileIn( $fname );
909 $text = $this->subReplaceExternalLinks( $text, 'http', true );
910 $text = $this->subReplaceExternalLinks( $text, 'https', true );
911 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
912 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
913 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
914 $text = $this->subReplaceExternalLinks( $text, 'news', false );
915 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
916 wfProfileOut( $fname );
920 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
921 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
922 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
924 # this is the list of separators that should be ignored if they
925 # are the last character of an URL but that should be included
926 # if they occur within the URL, e.g. "go to www.foo.com, where .."
927 # in this case, the last comma should not become part of the URL,
928 # but in "www.foo.com/123,2342,32.htm" it should.
930 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
931 $images = 'gif|png|jpg|jpeg';
933 # PLEASE NOTE: The curly braces { } are not part of the regex,
934 # they are interpreted as part of the string (used to tell PHP
935 # that the content of the string should be inserted there).
936 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
937 "((?i){$images})([^{$uc}]|$)/";
939 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
940 $sk =& $this->mOptions
->getSkin();
942 if ( $autonumber and $this->mOptions
->getAllowExternalImages() ) { # Use img tags only for HTTP urls
943 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
944 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
946 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
947 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
948 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
950 $s = str_replace( $unique, $protocol, $s );
952 $a = explode( "[{$protocol}:", " " . $s );
953 $s = array_shift( $a );
954 $s = substr( $s, 1 );
956 # Regexp for URL in square brackets
957 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
958 # Regexp for URL with link text in square brackets
959 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
961 foreach ( $a as $line ) {
963 # CASE 1: Link in square brackets, e.g.
964 # some text [http://domain.tld/some.link] more text
965 if ( preg_match( $e1, $line, $m ) ) {
966 $link = "{$protocol}:{$m[1]}";
968 if ( $autonumber ) { $text = "[" . ++
$this->mAutonumber
. "]"; }
969 else { $text = wfEscapeHTML( $link ); }
972 # CASE 2: Link with link text and text directly following it, e.g.
973 # This is a collection of [http://domain.tld/some.link link]s
974 else if ( preg_match( $e2, $line, $m ) ) {
975 $link = "{$protocol}:{$m[1]}";
979 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
985 # CASE 3: Nothing matches, just output the source text
987 $s .= "[{$protocol}:" . $line;
991 if( $link == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
994 # Expand the URL for printable version
995 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
997 $la = $sk->getExternalLinkAttributes( $link, $text );
998 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1005 /* private */ function replaceInternalLinks( $s ) {
1006 global $wgLang, $wgLinkCache;
1007 global $wgNamespacesWithSubpages, $wgLanguageCode;
1008 static $fname = 'Parser::replaceInternalLinks' ;
1009 wfProfileIn( $fname );
1011 wfProfileIn( $fname.'-setup' );
1013 # the % is needed to support urlencoded titles as well
1014 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
1015 $sk =& $this->mOptions
->getSkin();
1017 $a = explode( '[[', ' ' . $s );
1018 $s = array_shift( $a );
1019 $s = substr( $s, 1 );
1021 # Match a link having the form [[namespace:link|alternate]]trail
1023 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1024 # Match the end of a line for a word that's not followed by whitespace,
1025 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1026 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1028 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1029 # Special and Media are pseudo-namespaces; no pages actually exist in them
1030 static $image = FALSE;
1031 static $special = FALSE;
1032 static $media = FALSE;
1033 static $category = FALSE;
1034 if ( !$image ) { $image = Namespace::getImage(); }
1035 if ( !$special ) { $special = Namespace::getSpecial(); }
1036 if ( !$media ) { $media = Namespace::getMedia(); }
1037 if ( !$category ) { $category = Namespace::getCategory(); }
1039 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
1041 if ( $useLinkPrefixExtension ) {
1042 if ( preg_match( $e2, $s, $m ) ) {
1043 $first_prefix = $m[2];
1046 $first_prefix = false;
1052 wfProfileOut( $fname.'-setup' );
1054 foreach ( $a as $line ) {
1055 wfProfileIn( $fname.'-prefixhandling' );
1056 if ( $useLinkPrefixExtension ) {
1057 if ( preg_match( $e2, $s, $m ) ) {
1065 $prefix = $first_prefix;
1066 $first_prefix = false;
1069 wfProfileOut( $fname.'-prefixhandling' );
1071 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1073 # fix up urlencoded title texts
1074 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1076 } else { # Invalid form; output directly
1077 $s .= $prefix . '[[' . $line ;
1081 /* Valid link forms:
1083 :Foobar -- override special treatment of prefix (images, language links)
1084 /Foobar -- convert to CurrentPage/Foobar
1085 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1087 $c = substr($m[1],0,1);
1088 $noforce = ($c != ':');
1089 if( $c == '/' ) { # subpage
1090 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1091 $m[1]=substr($m[1],1,strlen($m[1])-2);
1094 $noslash=substr($m[1],1);
1096 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) { # subpages allowed here
1097 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1100 } # this might be changed for ugliness reasons
1102 $link = $noslash; # no subpage allowed, use standard link
1104 } elseif( $noforce ) { # no subpage
1107 $link = substr( $m[1], 1 );
1109 $wasblank = ( '' == $text );
1113 $nt = Title
::newFromText( $link );
1115 $s .= $prefix . '[[' . $line;
1118 $ns = $nt->getNamespace();
1119 $iw = $nt->getInterWiki();
1121 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1122 array_push( $this->mOutput
->mLanguageLinks
, $nt->getPrefixedText() );
1123 $tmp = $prefix . $trail ;
1124 $s .= (trim($tmp) == '')?
'': $tmp;
1127 if ( $ns == $image ) {
1128 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1129 $wgLinkCache->addImageLinkObj( $nt );
1132 if ( $ns == $category ) {
1133 $t = $nt->getText() ;
1134 $nnt = Title
::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1136 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1137 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1138 $wgLinkCache->resume();
1140 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1141 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1142 $this->mOutput
->mCategoryLinks
[] = $t ;
1143 $s .= $prefix . $trail ;
1147 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1148 ( strpos( $link, '#' ) == FALSE ) ) {
1149 # Self-links are handled specially; generally de-link and change to bold.
1150 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1154 if( $ns == $media ) {
1155 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1156 $wgLinkCache->addImageLinkObj( $nt );
1158 } elseif( $ns == $special ) {
1159 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1162 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1164 wfProfileOut( $fname );
1168 # Some functions here used by doBlockLevels()
1170 /* private */ function closeParagraph() {
1172 if ( '' != $this->mLastSection
) {
1173 $result = '</' . $this->mLastSection
. ">\n";
1175 $this->mInPre
= false;
1176 $this->mLastSection
= '';
1179 # getCommon() returns the length of the longest common substring
1180 # of both arguments, starting at the beginning of both.
1182 /* private */ function getCommon( $st1, $st2 ) {
1183 $fl = strlen( $st1 );
1184 $shorter = strlen( $st2 );
1185 if ( $fl < $shorter ) { $shorter = $fl; }
1187 for ( $i = 0; $i < $shorter; ++
$i ) {
1188 if ( $st1{$i} != $st2{$i} ) { break; }
1192 # These next three functions open, continue, and close the list
1193 # element appropriate to the prefix character passed into them.
1195 /* private */ function openList( $char )
1197 $result = $this->closeParagraph();
1199 if ( '*' == $char ) { $result .= '<ul><li>'; }
1200 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1201 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1202 else if ( ';' == $char ) {
1203 $result .= '<dl><dt>';
1204 $this->mDTopen
= true;
1206 else { $result = '<!-- ERR 1 -->'; }
1211 /* private */ function nextItem( $char ) {
1212 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1213 else if ( ':' == $char ||
';' == $char ) {
1215 if ( $this->mDTopen
) { $close = '</dt>'; }
1216 if ( ';' == $char ) {
1217 $this->mDTopen
= true;
1218 return $close . '<dt>';
1220 $this->mDTopen
= false;
1221 return $close . '<dd>';
1224 return '<!-- ERR 2 -->';
1227 /* private */function closeList( $char ) {
1228 if ( '*' == $char ) { $text = '</li></ul>'; }
1229 else if ( '#' == $char ) { $text = '</li></ol>'; }
1230 else if ( ':' == $char ) {
1231 if ( $this->mDTopen
) {
1232 $this->mDTopen
= false;
1233 $text = '</dt></dl>';
1235 $text = '</dd></dl>';
1238 else { return '<!-- ERR 3 -->'; }
1242 /* private */ function doBlockLevels( $text, $linestart ) {
1243 $fname = 'Parser::doBlockLevels';
1244 wfProfileIn( $fname );
1246 # Parsing through the text line by line. The main thing
1247 # happening here is handling of block-level elements p, pre,
1248 # and making lists from lines starting with * # : etc.
1250 $textLines = explode( "\n", $text );
1252 $lastPrefix = $output = $lastLine = '';
1253 $this->mDTopen
= $inBlockElem = false;
1255 $paragraphStack = false;
1257 if ( !$linestart ) {
1258 $output .= array_shift( $textLines );
1260 foreach ( $textLines as $oLine ) {
1261 $lastPrefixLength = strlen( $lastPrefix );
1262 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1263 $preOpenMatch = preg_match("/<pre/i", $oLine );
1264 if (!$this->mInPre
) {
1265 $this->mInPre
= !empty($preOpenMatch);
1267 if ( !$this->mInPre
) {
1268 # Multiple prefixes may abut each other for nested lists.
1269 $prefixLength = strspn( $oLine, '*#:;' );
1270 $pref = substr( $oLine, 0, $prefixLength );
1273 $pref2 = str_replace( ';', ':', $pref );
1274 $t = substr( $oLine, $prefixLength );
1276 # Don't interpret any other prefixes in preformatted text
1278 $pref = $pref2 = '';
1283 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1284 # Same as the last item, so no need to deal with nesting or opening stuff
1285 $output .= $this->nextItem( substr( $pref, -1 ) );
1286 $paragraphStack = false;
1288 if ( ";" == substr( $pref, -1 ) ) {
1289 # The one nasty exception: definition lists work like this:
1290 # ; title : definition text
1291 # So we check for : in the remainder text to split up the
1292 # title and definition, without b0rking links.
1293 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1294 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1296 $output .= $term . $this->nextItem( ':' );
1300 } elseif( $prefixLength ||
$lastPrefixLength ) {
1301 # Either open or close a level...
1302 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1303 $paragraphStack = false;
1305 while( $commonPrefixLength < $lastPrefixLength ) {
1306 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1307 --$lastPrefixLength;
1309 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1310 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1312 while ( $prefixLength > $commonPrefixLength ) {
1313 $char = substr( $pref, $commonPrefixLength, 1 );
1314 $output .= $this->openList( $char );
1316 if ( ';' == $char ) {
1317 # FIXME: This is dupe of code above
1318 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1320 $output .= $term . $this->nextItem( ":" );
1324 ++
$commonPrefixLength;
1326 $lastPrefix = $pref2;
1328 if( 0 == $prefixLength ) {
1329 # No prefix (not in list)--go to paragraph mode
1330 $uniq_prefix = UNIQ_PREFIX
;
1331 // XXX: use a stack for nestable elements like span, table and div
1332 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1333 $closematch = preg_match(
1334 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1335 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1336 if ( $openmatch or $closematch ) {
1337 $paragraphStack = false;
1338 $output .= $this->closeParagraph();
1339 if($preOpenMatch and !$preCloseMatch) {
1340 $this->mInPre
= true;
1342 if ( $closematch ) {
1343 $inBlockElem = false;
1345 $inBlockElem = true;
1347 } else if ( !$inBlockElem && !$this->mInPre
) {
1348 if ( " " == $t{0} and trim($t) != '' ) {
1350 if ($this->mLastSection
!= 'pre') {
1351 $paragraphStack = false;
1352 $output .= $this->closeParagraph().'<pre>';
1353 $this->mLastSection
= 'pre';
1357 if ( '' == trim($t) ) {
1358 if ( $paragraphStack ) {
1359 $output .= $paragraphStack.'<br />';
1360 $paragraphStack = false;
1361 $this->mLastSection
= 'p';
1363 if ($this->mLastSection
!= 'p' ) {
1364 $output .= $this->closeParagraph();
1365 $this->mLastSection
= '';
1366 $paragraphStack = '<p>';
1368 $paragraphStack = '</p><p>';
1372 if ( $paragraphStack ) {
1373 $output .= $paragraphStack;
1374 $paragraphStack = false;
1375 $this->mLastSection
= 'p';
1376 } else if ($this->mLastSection
!= 'p') {
1377 $output .= $this->closeParagraph().'<p>';
1378 $this->mLastSection
= 'p';
1384 if ($paragraphStack === false) {
1388 while ( $prefixLength ) {
1389 $output .= $this->closeList( $pref2{$prefixLength-1} );
1392 if ( '' != $this->mLastSection
) {
1393 $output .= '</' . $this->mLastSection
. '>';
1394 $this->mLastSection
= '';
1397 wfProfileOut( $fname );
1401 # Return value of a magic variable (like PAGENAME)
1402 function getVariableValue( $index ) {
1403 global $wgLang, $wgSitename, $wgServer;
1406 case MAG_CURRENTMONTH
:
1408 case MAG_CURRENTMONTHNAME
:
1409 return $wgLang->getMonthName( date('n') );
1410 case MAG_CURRENTMONTHNAMEGEN
:
1411 return $wgLang->getMonthNameGen( date('n') );
1412 case MAG_CURRENTDAY
:
1415 return $this->mTitle
->getText();
1417 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1418 return $wgLang->getNsText($this->mTitle
->getNamespace()); // Patch by Dori
1419 case MAG_CURRENTDAYNAME
:
1420 return $wgLang->getWeekdayName( date('w')+
1 );
1421 case MAG_CURRENTYEAR
:
1423 case MAG_CURRENTTIME
:
1424 return $wgLang->time( wfTimestampNow(), false );
1425 case MAG_NUMBEROFARTICLES
:
1426 return wfNumberOfArticles();
1436 # initialise the magic variables (like CURRENTMONTHNAME)
1437 function initialiseVariables() {
1438 global $wgVariableIDs;
1439 $this->mVariables
= array();
1440 foreach ( $wgVariableIDs as $id ) {
1441 $mw =& MagicWord
::get( $id );
1442 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1446 /* private */ function replaceVariables( $text, $args = array() ) {
1447 global $wgLang, $wgScript, $wgArticlePath;
1449 $fname = 'Parser::replaceVariables';
1450 wfProfileIn( $fname );
1453 $titleChars = Title
::legalChars();
1454 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1456 # This function is called recursively. To keep track of arguments we need a stack:
1457 array_push( $this->mArgStack
, $args );
1459 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1460 $GLOBALS['wgCurParser'] =& $this;
1463 if ( $this->mOutputType
== OT_HTML
) {
1464 # Variable substitution
1465 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1467 # Argument substitution
1468 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1470 # Template substitution
1471 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1472 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1474 array_pop( $this->mArgStack
);
1476 wfProfileOut( $fname );
1480 function variableSubstitution( $matches ) {
1481 if ( !$this->mVariables
) {
1482 $this->initialiseVariables();
1484 if ( array_key_exists( $matches[1], $this->mVariables
) ) {
1485 $text = $this->mVariables
[$matches[1]];
1486 $this->mOutput
->mContainsOldMagic
= true;
1488 $text = $matches[0];
1493 function braceSubstitution( $matches ) {
1494 global $wgLinkCache, $wgLang;
1495 $fname = 'Parser::braceSubstitution';
1502 # $newline is an optional newline character before the braces
1503 # $part1 is the bit before the first |, and must contain only title characters
1504 # $args is a list of arguments, starting from index 0, not including $part1
1506 $newline = $matches[1];
1507 $part1 = $matches[2];
1508 # If the third subpattern matched anything, it will start with |
1509 if ( $matches[3] !== '' ) {
1510 $args = explode( '|', substr( $matches[3], 1 ) );
1514 $argc = count( $args );
1517 if ( strpos( $matches[0], '{{{' ) !== false ) {
1518 $text = $matches[0];
1525 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1526 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1527 if ( $this->mOutputType
!= OT_WIKI
) {
1528 # Invalid SUBST not replaced at PST time
1529 # Return without further processing
1530 $text = $matches[0];
1534 } elseif ( $this->mOutputType
== OT_WIKI
) {
1535 # SUBST not found in PST pass, do nothing
1536 $text = $matches[0];
1541 # MSG, MSGNW and INT
1544 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1545 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1548 # Remove obsolete MSG:
1549 $mwMsg =& MagicWord
::get( MAG_MSG
);
1550 $mwMsg->matchStartAndRemove( $part1 );
1553 # Check if it is an internal message
1554 $mwInt =& MagicWord
::get( MAG_INT
);
1555 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1556 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1557 $text = wfMsgReal( $part1, $args, true );
1565 # Check for NS: (namespace expansion)
1566 $mwNs = MagicWord
::get( MAG_NS
);
1567 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1568 if ( intval( $part1 ) ) {
1569 $text = $wgLang->getNsText( intval( $part1 ) );
1572 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1573 if ( !is_null( $index ) ) {
1574 $text = $wgLang->getNsText( $index );
1581 # LOCALURL and LOCALURLE
1583 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1584 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1586 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1587 $func = 'getLocalURL';
1588 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1589 $func = 'escapeLocalURL';
1594 if ( $func !== '' ) {
1595 $title = Title
::newFromText( $part1 );
1596 if ( !is_null( $title ) ) {
1598 $text = $title->$func( $args[0] );
1600 $text = $title->$func();
1607 # Internal variables
1608 if ( !$this->mVariables
) {
1609 $this->initialiseVariables();
1611 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1612 $text = $this->mVariables
[$part1];
1614 $this->mOutput
->mContainsOldMagic
= true;
1617 # Arguments input from the caller
1618 $inputArgs = end( $this->mArgStack );
1619 if ( !$found && array_key_exists( $part1, $inputArgs ) ) {
1620 $text = $inputArgs[$part1];
1624 # Load from database
1626 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1627 if ( !is_null( $title ) && !$title->isExternal() ) {
1628 # Check for excessive inclusion
1629 $dbk = $title->getPrefixedDBkey();
1630 if ( $this->incrementIncludeCount( $dbk ) ) {
1631 $article = new Article( $title );
1632 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1633 if ( $articleContent !== false ) {
1635 $text = $articleContent;
1640 # If the title is valid but undisplayable, make a link to it
1641 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1642 $text = '[[' . $title->getPrefixedText() . ']]';
1648 # Recursive parsing, escaping and link table handling
1649 # Only for HTML output
1650 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1651 $text = wfEscapeWikiText( $text );
1652 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1653 # Clean up argument array
1654 $assocArgs = array();
1656 foreach( $args as $arg ) {
1657 $eqpos = strpos( $arg, '=' );
1658 if ( $eqpos === false ) {
1659 $assocArgs[$index++
] = $arg;
1661 $name = trim( substr( $arg, 0, $eqpos ) );
1662 $value = trim( substr( $arg, $eqpos+
1 ) );
1663 if ( $value === false ) {
1666 if ( $name !== false ) {
1667 $assocArgs[$name] = $value;
1672 # Do not enter included links in link table
1673 if ( !is_null( $title ) ) {
1674 $wgLinkCache->suspend();
1677 # Run full parser on the included text
1678 $text = $this->stripParse( $text, $newline, $assocArgs );
1680 # Resume the link cache and register the inclusion as a link
1681 if ( !is_null( $title ) ) {
1682 $wgLinkCache->resume();
1683 $wgLinkCache->addLinkObj( $title );
1694 # Triple brace replacement -- used for template arguments
1695 function argSubstitution( $matches ) {
1696 $newline = $matches[1];
1697 $arg = trim( $matches[2] );
1698 $text = $matches[0];
1699 $inputArgs = end( $this->mArgStack
);
1701 if ( array_key_exists( $arg, $inputArgs ) ) {
1702 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1708 # Returns true if the function is allowed to include this entity
1709 function incrementIncludeCount( $dbk ) {
1710 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1711 $this->mIncludeCount
[$dbk] = 0;
1713 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1721 # Cleans up HTML, removes dangerous tags and attributes
1722 /* private */ function removeHTMLtags( $text ) {
1723 global $wgUseTidy, $wgUserHtml;
1724 $fname = 'Parser::removeHTMLtags';
1725 wfProfileIn( $fname );
1728 $htmlpairs = array( # Tags that must be closed
1729 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1730 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1731 'strike', 'strong', 'tt', 'var', 'div', 'center',
1732 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1733 'ruby', 'rt' , 'rb' , 'rp', 'p'
1735 $htmlsingle = array(
1736 'br', 'hr', 'li', 'dt', 'dd'
1738 $htmlnest = array( # Tags that can be nested--??
1739 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1740 'dl', 'font', 'big', 'small', 'sub', 'sup'
1742 $tabletags = array( # Can only appear inside table
1746 $htmlpairs = array();
1747 $htmlsingle = array();
1748 $htmlnest = array();
1749 $tabletags = array();
1752 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1753 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1755 $htmlattrs = $this->getHTMLattrs () ;
1757 # Remove HTML comments
1758 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1760 $bits = explode( '<', $text );
1761 $text = array_shift( $bits );
1763 $tagstack = array(); $tablestack = array();
1764 foreach ( $bits as $x ) {
1765 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1766 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1768 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1769 error_reporting( $prev );
1772 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1776 if ( ! in_array( $t, $htmlsingle ) &&
1777 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1778 @array_push
( $tagstack, $ot );
1781 if ( $t == 'table' ) {
1782 $tagstack = array_pop( $tablestack );
1787 # Keep track for later
1788 if ( in_array( $t, $tabletags ) &&
1789 ! in_array( 'table', $tagstack ) ) {
1791 } else if ( in_array( $t, $tagstack ) &&
1792 ! in_array ( $t , $htmlnest ) ) {
1794 } else if ( ! in_array( $t, $htmlsingle ) ) {
1795 if ( $t == 'table' ) {
1796 array_push( $tablestack, $tagstack );
1797 $tagstack = array();
1799 array_push( $tagstack, $t );
1801 # Strip non-approved attributes from the tag
1802 $newparams = $this->fixTagAttributes($params);
1806 $rest = str_replace( '>', '>', $rest );
1807 $text .= "<$slash$t $newparams$brace$rest";
1811 $text .= '<' . str_replace( '>', '>', $x);
1813 # Close off any remaining tags
1814 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1816 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1819 # this might be possible using tidy itself
1820 foreach ( $bits as $x ) {
1821 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1823 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1824 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1825 $newparams = $this->fixTagAttributes($params);
1826 $rest = str_replace( '>', '>', $rest );
1827 $text .= "<$slash$t $newparams$brace$rest";
1829 $text .= '<' . str_replace( '>', '>', $x);
1833 wfProfileOut( $fname );
1840 * This function accomplishes several tasks:
1841 * 1) Auto-number headings if that option is enabled
1842 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1843 * 3) Add a Table of contents on the top for users who have enabled the option
1844 * 4) Auto-anchor headings
1846 * It loops through all headlines, collects the necessary data, then splits up the
1847 * string and re-inserts the newly formatted headlines.
1851 /* private */ function formatHeadings( $text, $isMain=true ) {
1852 global $wgInputEncoding;
1854 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1855 $doShowToc = $this->mOptions
->getShowToc();
1856 $forceTocHere = false;
1857 if( !$this->mTitle
->userCanEdit() ) {
1859 $rightClickHack = 0;
1861 $showEditLink = $this->mOptions
->getEditSection();
1862 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1865 # Inhibit editsection links if requested in the page
1866 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1867 if( $esw->matchAndRemove( $text ) ) {
1870 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1872 $mw =& MagicWord
::get( MAG_NOTOC
);
1873 if( $mw->matchAndRemove( $text ) ) {
1877 # never add the TOC to the Main Page. This is an entry page that should not
1878 # be more than 1-2 screens large anyway
1879 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
1883 # Get all headlines for numbering them and adding funky stuff like [edit]
1884 # links - this is for later, but we need the number of headlines right now
1885 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1887 # if there are fewer than 4 headlines in the article, do not show TOC
1888 if( $numMatches < 4 ) {
1892 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1893 # override above conditions and always show TOC at that place
1894 $mw =& MagicWord
::get( MAG_TOC
);
1895 if ($mw->match( $text ) ) {
1897 $forceTocHere = true;
1899 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1900 # override above conditions and always show TOC above first header
1901 $mw =& MagicWord
::get( MAG_FORCETOC
);
1902 if ($mw->matchAndRemove( $text ) ) {
1909 # We need this to perform operations on the HTML
1910 $sk =& $this->mOptions
->getSkin();
1915 # Ugh .. the TOC should have neat indentation levels which can be
1916 # passed to the skin functions. These are determined here
1921 $sublevelCount = array();
1924 foreach( $matches[3] as $headline ) {
1927 $prevlevel = $level;
1929 $level = $matches[1][$headlineCount];
1930 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1931 # reset when we enter a new level
1932 $sublevelCount[$level] = 0;
1933 $toc .= $sk->tocIndent( $level - $prevlevel );
1934 $toclevel +
= $level - $prevlevel;
1936 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1937 # reset when we step back a level
1938 $sublevelCount[$level+
1]=0;
1939 $toc .= $sk->tocUnindent( $prevlevel - $level );
1940 $toclevel -= $prevlevel - $level;
1942 # count number of headlines for each level
1943 @$sublevelCount[$level]++
;
1944 if( $doNumberHeadings ||
$doShowToc ) {
1946 for( $i = 1; $i <= $level; $i++
) {
1947 if( !empty( $sublevelCount[$i] ) ) {
1951 $numbering .= $sublevelCount[$i];
1957 # The canonized header is a version of the header text safe to use for links
1958 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1959 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1960 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
1963 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1964 $tocline = trim( $canonized_headline );
1965 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
1966 $replacearray = array(
1970 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1971 $refer[$headlineCount] = $canonized_headline;
1973 # count how many in assoc. array so we can track dupes in anchors
1974 @$refers[$canonized_headline]++
;
1975 $refcount[$headlineCount]=$refers[$canonized_headline];
1977 # Prepend the number to the heading text
1979 if( $doNumberHeadings ||
$doShowToc ) {
1980 $tocline = $numbering . ' ' . $tocline;
1982 # Don't number the heading if it is the only one (looks silly)
1983 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1984 # the two are different if the line contains a link
1985 $headline=$numbering . ' ' . $headline;
1989 # Create the anchor for linking from the TOC to the section
1990 $anchor = $canonized_headline;
1991 if($refcount[$headlineCount] > 1 ) {
1992 $anchor .= '_' . $refcount[$headlineCount];
1995 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1997 if( $showEditLink ) {
1998 if ( empty( $head[$headlineCount] ) ) {
1999 $head[$headlineCount] = '';
2001 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
2004 # Add the edit section span
2005 if( $rightClickHack ) {
2006 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
2009 # give headline the correct <h#> tag
2010 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2016 $toclines = $headlineCount;
2017 $toc .= $sk->tocUnindent( $toclevel );
2018 $toc = $sk->tocTable( $toc );
2021 # split up and insert constructed headlines
2023 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2026 foreach( $blocks as $block ) {
2027 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2028 # This is the [edit] link that appears for the top block of text when
2029 # section editing is enabled
2031 # Disabled because it broke block formatting
2032 # For example, a bullet point in the top line
2033 # $full .= $sk->editSectionLink(0);
2036 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2037 # Top anchor now in skin
2041 if( !empty( $head[$i] ) ) {
2047 $mw =& MagicWord
::get( MAG_TOC
);
2048 return $mw->replace( $toc, $full );
2054 # Return an HTML link for the "ISBN 123456" text
2055 /* private */ function magicISBN( $text ) {
2057 $fname = 'Parser::magicISBN';
2058 wfProfileIn( $fname );
2060 $a = split( 'ISBN ', " $text" );
2061 if ( count ( $a ) < 2 ) {
2062 wfProfileOut( $fname );
2065 $text = substr( array_shift( $a ), 1);
2066 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2068 foreach ( $a as $x ) {
2069 $isbn = $blank = '' ;
2070 while ( ' ' == $x{0} ) {
2072 $x = substr( $x, 1 );
2074 while ( strstr( $valid, $x{0} ) != false ) {
2076 $x = substr( $x, 1 );
2078 $num = str_replace( '-', '', $isbn );
2079 $num = str_replace( ' ', '', $num );
2082 $text .= "ISBN $blank$x";
2084 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2085 $text .= '<a href="' .
2086 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2087 "\" class=\"internal\">ISBN $isbn</a>";
2091 wfProfileOut( $fname );
2095 # Return an HTML link for the "RFC 1234" text
2096 /* private */ function magicRFC( $text ) {
2099 $a = split( 'RFC ', ' '.$text );
2100 if ( count ( $a ) < 2 ) return $text;
2101 $text = substr( array_shift( $a ), 1);
2102 $valid = '0123456789';
2104 foreach ( $a as $x ) {
2105 $rfc = $blank = '' ;
2106 while ( ' ' == $x{0} ) {
2108 $x = substr( $x, 1 );
2110 while ( strstr( $valid, $x{0} ) != false ) {
2112 $x = substr( $x, 1 );
2116 $text .= "RFC $blank$x";
2118 $url = wfmsg( 'rfcurl' );
2119 $url = str_replace( '$1', $rfc, $url);
2120 $sk =& $this->mOptions
->getSkin();
2121 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2122 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2128 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2129 $this->mOptions
= $options;
2130 $this->mTitle
=& $title;
2131 $this->mOutputType
= OT_WIKI
;
2133 if ( $clearState ) {
2134 $this->clearState();
2137 $stripState = false;
2141 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2145 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2146 "/<br *?>/i" => "<br />",
2148 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2150 $text = $this->strip( $text, $stripState, false );
2151 $text = $this->pstPass2( $text, $user );
2152 $text = $this->unstrip( $text, $stripState );
2153 $text = $this->unstripNoWiki( $text, $stripState );
2157 /* private */ function pstPass2( $text, &$user ) {
2158 global $wgLang, $wgLocaltimezone, $wgCurParser;
2160 # Variable replacement
2161 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2162 $text = $this->replaceVariables( $text );
2166 $n = $user->getName();
2167 $k = $user->getOption( 'nickname' );
2168 if ( '' == $k ) { $k = $n; }
2169 if(isset($wgLocaltimezone)) {
2170 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2172 /* Note: this is an ugly timezone hack for the European wikis */
2173 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2174 ' (' . date( 'T' ) . ')';
2175 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2177 $text = preg_replace( '/~~~~~/', $d, $text );
2178 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2179 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2180 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2181 Namespace::getUser() ) . ":$n|$k]]", $text );
2183 # Context links: [[|name]] and [[name (context)|]]
2185 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2186 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2187 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2188 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2190 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2191 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2192 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2193 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2194 # [[ns:page (cont)|]]
2196 $t = $this->mTitle
->getText();
2197 if ( preg_match( $conpat, $t, $m ) ) {
2200 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2201 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2202 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2204 if ( '' == $context ) {
2205 $text = preg_replace( $p2, '[[\\1]]', $text );
2207 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2211 $mw =& MagicWord::get( MAG_SUBST );
2212 $wgCurParser = $this->fork();
2213 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2214 $this->merge( $wgCurParser );
2217 # Trim trailing whitespace
2218 # MAG_END (__END__) tag allows for trailing
2219 # whitespace to be deliberately included
2220 $text = rtrim( $text );
2221 $mw =& MagicWord
::get( MAG_END
);
2222 $mw->matchAndRemove( $text );
2227 # Set up some variables which are usually set up in parse()
2228 # so that an external function can call some class members with confidence
2229 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2230 $this->mTitle
=& $title;
2231 $this->mOptions
= $options;
2232 $this->mOutputType
= $outputType;
2233 if ( $clearState ) {
2234 $this->clearState();
2238 function transformMsg( $text, $options ) {
2240 static $executing = false;
2242 # Guard against infinite recursion
2248 $this->mTitle
= $wgTitle;
2249 $this->mOptions
= $options;
2250 $this->mOutputType
= OT_MSG
;
2251 $this->clearState();
2252 $text = $this->replaceVariables( $text );
2258 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2259 # Callback will be called with the text within
2260 # Transform and return the text within
2261 function setHook( $tag, $callback ) {
2262 $oldVal = @$this->mTagHooks
[$tag];
2263 $this->mTagHooks
[$tag] = $callback;
2270 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2271 var $mCacheTime; # Used in ParserCache
2273 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2274 $containsOldMagic = false )
2276 $this->mText
= $text;
2277 $this->mLanguageLinks
= $languageLinks;
2278 $this->mCategoryLinks
= $categoryLinks;
2279 $this->mContainsOldMagic
= $containsOldMagic;
2280 $this->mCacheTime
= "";
2283 function getText() { return $this->mText
; }
2284 function getLanguageLinks() { return $this->mLanguageLinks
; }
2285 function getCategoryLinks() { return $this->mCategoryLinks
; }
2286 function getCacheTime() { return $this->mCacheTime
; }
2287 function containsOldMagic() { return $this->mContainsOldMagic
; }
2288 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2289 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2290 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2291 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2292 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2294 function merge( $other ) {
2295 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2296 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2297 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2304 # All variables are private
2305 var $mUseTeX; # Use texvc to expand <math> tags
2306 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2307 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2308 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2309 var $mAllowExternalImages; # Allow external images inline
2310 var $mSkin; # Reference to the preferred skin
2311 var $mDateFormat; # Date format index
2312 var $mEditSection; # Create "edit section" links
2313 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2314 var $mNumberHeadings; # Automatically number headings
2315 var $mShowToc; # Show table of contents
2317 function getUseTeX() { return $this->mUseTeX
; }
2318 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2319 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2320 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2321 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2322 function getSkin() { return $this->mSkin
; }
2323 function getDateFormat() { return $this->mDateFormat
; }
2324 function getEditSection() { return $this->mEditSection
; }
2325 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2326 function getNumberHeadings() { return $this->mNumberHeadings
; }
2327 function getShowToc() { return $this->mShowToc
; }
2329 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2330 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2331 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2332 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2333 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2334 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2335 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2336 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2337 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2338 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2340 function setSkin( &$x ) { $this->mSkin
=& $x; }
2342 /* static */ function newFromUser( &$user ) {
2343 $popts = new ParserOptions
;
2344 $popts->initialiseFromUser( $user );
2348 function initialiseFromUser( &$userInput ) {
2349 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2351 if ( !$userInput ) {
2353 $user->setLoaded( true );
2355 $user =& $userInput;
2358 $this->mUseTeX
= $wgUseTeX;
2359 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2360 $this->mUseDynamicDates
= $wgUseDynamicDates;
2361 $this->mInterwikiMagic
= $wgInterwikiMagic;
2362 $this->mAllowExternalImages
= $wgAllowExternalImages;
2363 $this->mSkin
=& $user->getSkin();
2364 $this->mDateFormat
= $user->getOption( 'date' );
2365 $this->mEditSection
= $user->getOption( 'editsection' );
2366 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2367 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2368 $this->mShowToc
= $user->getOption( 'showtoc' );
2374 # Regex callbacks, used in Parser::replaceVariables
2375 function wfBraceSubstitution( $matches )
2377 global $wgCurParser;
2378 return $wgCurParser->braceSubstitution( $matches );
2381 function wfArgSubstitution( $matches )
2383 global $wgCurParser;
2384 return $wgCurParser->argSubstitution( $matches );
2387 function wfVariableSubstitution( $matches )
2389 global $wgCurParser;
2390 return $wgCurParser->variableSubstitution( $matches );