Somewhat less hacky fix to the French l''''homme''' problem.
[mediawiki.git] / includes / Parser.php
blobd3fe58dd85008a09d49bbd8bb2b7b81c7805b32a
1 <?php
3 // require_once('Tokenizer.php');
5 # PHP Parser
7 # Processes wiki markup
9 # There are two main entry points into the Parser class: parse() and preSaveTransform().
10 # The parse() function produces HTML output, preSaveTransform() produces altered wiki markup.
12 # Globals used:
13 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
15 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
17 # settings: $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
18 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
19 # $wgLocaltimezone
21 # * only within ParserOptions
24 #----------------------------------------
25 # Variable substitution O(N^2) attack
26 #-----------------------------------------
27 # Without countermeasures, it would be possible to attack the parser by saving a page
28 # filled with a large number of inclusions of large pages. The size of the generated
29 # page would be proportional to the square of the input size. Hence, we limit the number
30 # of inclusions of any given page, thus bringing any attack back to O(N).
33 define( "MAX_INCLUDE_REPEAT", 5 );
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");
50 class Parser
52 # Persistent:
53 var $mTagHooks;
55 # Cleared with clearState():
56 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
57 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
59 # Temporary:
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.
66 function Parser() {
67 $this->mTemplates = array();
68 $this->mTemplatePath = array();
69 $this->mTagHooks = array();
70 $this->clearState();
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 ) {
91 global $wgUseTidy;
92 $fname = "Parser::parse";
93 wfProfileIn( $fname );
95 if ( $clearState ) {
96 $this->clearState();
99 $this->mOptions = $options;
100 $this->mTitle =& $title;
101 $this->mOutputType = OT_HTML;
103 $stripState = NULL;
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
108 if(!$wgUseTidy) {
109 $fixtags = array(
110 # french spaces, last one Guillemet-left
111 # only if there is something before the space
112 '/(.) (\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
113 # french spaces, Guillemet-right
114 "/(\\302\\253) /i"=>"\\1&nbsp;",
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]+;)/' => '&amp;'
123 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
124 } else {
125 $fixtags = array(
126 # french spaces, last one Guillemet-left
127 '/ (\\?|:|!|\\302\\273)/i' => '&nbsp;\\1',
128 # french spaces, Guillemet-right
129 '/(\\302\\253) /i' => '\\1&nbsp;',
130 '/([^> ]+(&#x30(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 );
136 # only once and last
137 $text = $this->doBlockLevels( $text, $linestart );
138 $text = $this->unstripNoWiki( $text, $this->mStripState );
139 if($wgUseTidy) {
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();
163 if ( !$content ) {
164 $content = array( );
166 $n = 1;
167 $stripped = '';
169 while ( '' != $text ) {
170 if($tag==STRIP_COMMENTS) {
171 $p = preg_split( '/<!--/i', $text, 2 );
172 } else {
173 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
175 $stripped .= $p[0];
176 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
177 $text = '';
178 } else {
179 if($tag==STRIP_COMMENTS) {
180 $q = preg_split( '/-->/i', $p[1], 2 );
181 } else {
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;
187 $text = $q[1];
190 return $stripped;
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 );
215 # html
216 global $wgRawHtml;
217 if( $wgRawHtml ) {
218 $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
219 foreach( $html_content as $marker => $content ) {
220 if ($render ) {
221 # Raw and unchecked for validity.
222 $html_content[$marker] = $content;
223 } else {
224 $html_content[$marker] = "<html>$content</html>";
229 # nowiki
230 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
231 foreach( $nowiki_content as $marker => $content ) {
232 if( $render ){
233 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
234 } else {
235 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
239 # math
240 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
241 foreach( $math_content as $marker => $content ){
242 if( $render ) {
243 if( $this->mOptions->getUseTeX() ) {
244 $math_content[$marker] = renderMath( $content );
245 } else {
246 $math_content[$marker] = "&lt;math&gt;$content&lt;math&gt;";
248 } else {
249 $math_content[$marker] = "<math>$content</math>";
253 # pre
254 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
255 foreach( $pre_content as $marker => $content ){
256 if( $render ){
257 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
258 } else {
259 $pre_content[$marker] = "<pre>$content</pre>";
263 # Comments
264 if($stripcomments) {
265 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
266 foreach( $comment_content as $marker => $content ){
267 $comment_content[$marker] = "<!--$content-->";
271 # Extensions
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 ) {
276 if ( $render ) {
277 $ext_content[$tag][$marker] = $callback( $content );
278 } else {
279 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
284 # Merge state with the pre-existing state, if there is one
285 if ( $state ) {
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;
297 } else {
298 $state = array(
299 'html' => $html_content,
300 'nowiki' => $nowiki_content,
301 'math' => $math_content,
302 'pre' => $pre_content,
303 'comment' => $comment_content,
304 ) + $ext_content;
306 return $text;
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 );
321 return $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 );
330 global $wgRawHtml;
331 if ($wgRawHtml) {
332 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
333 $text = str_replace( key( $state['html'] ), $content, $text );
337 return $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();
346 if ( !$state ) {
347 $state = array(
348 'html' => array(),
349 'nowiki' => array(),
350 'math' => array(),
351 'pre' => array()
354 $state['item'][$rnd] = $text;
355 return $rnd;
358 # categoryMagic
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
363 # off.
364 function categoryMagic() {
365 $msg = wfMsg('usenewcategorypage');
366 if ( '0' == @$msg[0] )
368 return $this->oldCategoryMagic();
369 } else {
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 $cns = Namespace::getCategory() ;
382 if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
384 $r = "<br style=\"clear:both;\"/>\n";
387 $sk =& $wgUser->getSkin() ;
389 $articles = array() ;
390 $children = array() ;
391 $data = array () ;
392 $id = $this->mTitle->getArticleID() ;
394 # FIXME: add limits
395 $dbr =& wfGetDB( DB_SLAVE );
396 $cur = $dbr->tableName( 'cur' );
397 $categorylinks = $dbr->tableName( 'categorylinks' );
399 $t = $dbr->strencode( $this->mTitle->getDBKey() );
400 $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " .
401 "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
402 $res = $dbr->query( $sql, $fname ) ;
403 while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
405 # For all pages that link to this category
406 foreach ( $data AS $x )
408 $t = $wgLang->getNsText ( $x->cur_namespace ) ;
409 if ( $t != "" ) $t .= ":" ;
410 $t .= $x->cur_title ;
412 if ( $x->cur_namespace == $cns ) {
413 array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
414 } else {
415 array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
418 $dbr->freeResult ( $res ) ;
420 # Showing subcategories
421 if ( count ( $children ) > 0 ) {
422 $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
423 $r .= implode ( ', ' , $children ) ;
426 # Showing pages in this category
427 if ( count ( $articles ) > 0 ) {
428 $ti = $this->mTitle->getText() ;
429 $h = wfMsg( 'category_header', $ti );
430 $r .= "<h2>{$h}</h2>\n" ;
431 $r .= implode ( ', ' , $articles ) ;
434 return $r ;
439 function newCategoryMagic () {
440 global $wgLang , $wgUser ;
441 if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
443 $cns = Namespace::getCategory() ;
444 if ( $this->mTitle->getNamespace() != $cns ) return '' ; # This ain't a category page
446 $r = "<br style=\"clear:both;\"/>\n";
449 $sk =& $wgUser->getSkin() ;
451 $articles = array() ;
452 $articles_start_char = array();
453 $children = array() ;
454 $children_start_char = array();
455 $data = array () ;
456 $id = $this->mTitle->getArticleID() ;
458 # FIXME: add limits
459 $dbr =& wfGetDB( DB_SLAVE );
460 $cur = $dbr->tableName( 'cur' );
461 $categorylinks = $dbr->tableName( 'categorylinks' );
463 $t = $dbr->strencode( $this->mTitle->getDBKey() );
464 $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " .
465 "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ;
466 $res = $dbr->query ( $sql ) ;
467 while ( $x = $dbr->fetchObject ( $res ) )
469 $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ;
470 if ( $t != '' ) $t .= ':' ;
471 $t .= $x->cur_title ;
473 if ( $x->cur_namespace == $cns ) {
474 $ctitle = str_replace( '_',' ',$x->cur_title );
475 array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
477 // If there's a link from Category:A to Category:B, the sortkey of the resulting
478 // entry in the categorylinks table is Category:A, not A, which it SHOULD be.
479 // Workaround: If sortkey == "Category:".$title, than use $title for sorting,
480 // else use sortkey...
481 if ( ($ns.":".$ctitle) == $x->cl_sortkey ) {
482 array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) );
483 } else {
484 array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
486 } else {
487 array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
488 array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ;
491 $dbr->freeResult ( $res ) ;
493 $ti = $this->mTitle->getText() ;
495 # Don't show subcategories section if there are none.
496 if ( count ( $children ) > 0 )
498 # Showing subcategories
499 $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n"
500 . wfMsg( 'subcategorycount', count( $children ) );
501 if ( count ( $children ) > 6 ) {
503 // divide list into three equal chunks
504 $chunk = (int) (count ( $children ) / 3);
506 // get and display header
507 $r .= '<table width="100%"><tr valign="top">';
509 $startChunk = 0;
510 $endChunk = $chunk;
512 // loop through the chunks
513 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
514 $chunkIndex < 3;
515 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
518 $r .= '<td><ul>';
519 // output all subcategories to category
520 for ($index = $startChunk ;
521 $index < $endChunk && $index < count($children);
522 $index++ )
524 // check for change of starting letter or begging of chunk
525 if ( ($children_start_char[$index] != $children_start_char[$index - 1])
526 || ($index == $startChunk) )
528 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
531 $r .= "<li>{$children[$index]}</li>";
533 $r .= '</ul></td>';
537 $r .= '</tr></table>';
538 } else {
539 // for short lists of subcategories to category.
541 $r .= "<h3>{$children_start_char[0]}</h3>\n";
542 $r .= '<ul><li>'.$children[0].'</li>';
543 for ($index = 1; $index < count($children); $index++ )
545 if ($children_start_char[$index] != $children_start_char[$index - 1])
547 $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
550 $r .= "<li>{$children[$index]}</li>";
552 $r .= '</ul>';
554 } # END of if ( count($children) > 0 )
556 $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n" .
557 wfMsg( 'categoryarticlecount', count( $articles ) );
559 # Showing articles in this category
560 if ( count ( $articles ) > 6) {
561 $ti = $this->mTitle->getText() ;
563 // divide list into three equal chunks
564 $chunk = (int) (count ( $articles ) / 3);
566 // get and display header
567 $r .= '<table width="100%"><tr valign="top">';
569 // loop through the chunks
570 for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
571 $chunkIndex < 3;
572 $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
575 $r .= '<td><ul>';
577 // output all articles in category
578 for ($index = $startChunk ;
579 $index < $endChunk && $index < count($articles);
580 $index++ )
582 // check for change of starting letter or begging of chunk
583 if ( ($articles_start_char[$index] != $articles_start_char[$index - 1])
584 || ($index == $startChunk) )
586 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
589 $r .= "<li>{$articles[$index]}</li>";
591 $r .= '</ul></td>';
595 $r .= '</tr></table>';
596 } elseif ( count ( $articles ) > 0) {
597 // for short lists of articles in categories.
598 $ti = $this->mTitle->getText() ;
600 $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
601 $r .= '<ul><li>'.$articles[0].'</li>';
602 for ($index = 1; $index < count($articles); $index++ )
604 if ($articles_start_char[$index] != $articles_start_char[$index - 1])
606 $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
609 $r .= "<li>{$articles[$index]}</li>";
611 $r .= '</ul>';
615 return $r ;
618 # Return allowed HTML attributes
619 function getHTMLattrs () {
620 $htmlattrs = array( # Allowed attributes--no scripting, etc.
621 'title', 'align', 'lang', 'dir', 'width', 'height',
622 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
623 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
624 /* FONT */ 'type', 'start', 'value', 'compact',
625 /* For various lists, mostly deprecated but safe */
626 'summary', 'width', 'border', 'frame', 'rules',
627 'cellspacing', 'cellpadding', 'valign', 'char',
628 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
629 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
630 'id', 'class', 'name', 'style' /* For CSS */
632 return $htmlattrs ;
635 # Remove non approved attributes and javascript in css
636 function fixTagAttributes ( $t ) {
637 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
638 $htmlattrs = $this->getHTMLattrs() ;
640 # Strip non-approved attributes from the tag
641 $t = preg_replace(
642 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
643 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
644 $t);
646 $t = str_replace ( "<></>" , "" , $t ) ; # This should fix bug 980557
648 # Strip javascript "expression" from stylesheets. Brute force approach:
649 # If anythin offensive is found, all attributes of the HTML tag are dropped
651 if( preg_match(
652 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
653 wfMungeToUtf8( $t ) ) )
655 $t='';
658 return trim ( $t ) ;
661 # interface with html tidy, used if $wgUseTidy = true
662 function tidy ( $text ) {
663 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
664 global $wgInputEncoding, $wgOutputEncoding;
665 $fname = 'Parser::tidy';
666 wfProfileIn( $fname );
668 $cleansource = '';
669 switch(strtoupper($wgOutputEncoding)) {
670 case 'ISO-8859-1':
671 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
672 break;
673 case 'UTF-8':
674 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
675 break;
676 default:
677 $wgTidyOpts .= ' -raw';
680 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
681 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
682 '<head><title>test</title></head><body>'.$text.'</body></html>';
683 $descriptorspec = array(
684 0 => array('pipe', 'r'),
685 1 => array('pipe', 'w'),
686 2 => array('file', '/dev/null', 'a')
688 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
689 if (is_resource($process)) {
690 fwrite($pipes[0], $wrappedtext);
691 fclose($pipes[0]);
692 while (!feof($pipes[1])) {
693 $cleansource .= fgets($pipes[1], 1024);
695 fclose($pipes[1]);
696 $return_value = proc_close($process);
699 wfProfileOut( $fname );
701 if( $cleansource == '' && $text != '') {
702 wfDebug( "Tidy error detected!\n" );
703 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
704 } else {
705 return $cleansource;
709 # parse the wiki syntax used to render tables
710 function doTableStuff ( $t ) {
711 $fname = 'Parser::doTableStuff';
712 wfProfileIn( $fname );
714 $t = explode ( "\n" , $t ) ;
715 $td = array () ; # Is currently a td tag open?
716 $ltd = array () ; # Was it TD or TH?
717 $tr = array () ; # Is currently a tr tag open?
718 $ltr = array () ; # tr attributes
719 $indent_level = 0; # indent level of the table
720 foreach ( $t AS $k => $x )
722 $x = trim ( $x ) ;
723 $fc = substr ( $x , 0 , 1 ) ;
724 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) )
726 $indent_level = strlen( $matches[1] );
727 $t[$k] = "\n" .
728 str_repeat( "<dl><dd>", $indent_level ) .
729 "<table " . $this->fixTagAttributes ( $matches[2] ) . '>' ;
730 array_push ( $td , false ) ;
731 array_push ( $ltd , '' ) ;
732 array_push ( $tr , false ) ;
733 array_push ( $ltr , '' ) ;
735 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
736 else if ( '|}' == substr ( $x , 0 , 2 ) )
738 $z = "</table>\n" ;
739 $l = array_pop ( $ltd ) ;
740 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
741 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
742 array_pop ( $ltr ) ;
743 $t[$k] = $z . str_repeat( "</dd></dl>", $indent_level );
745 else if ( '|-' == substr ( $x , 0 , 2 ) ) # Allows for |---------------
747 $x = substr ( $x , 1 ) ;
748 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
749 $z = '' ;
750 $l = array_pop ( $ltd ) ;
751 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
752 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
753 array_pop ( $ltr ) ;
754 $t[$k] = $z ;
755 array_push ( $tr , false ) ;
756 array_push ( $td , false ) ;
757 array_push ( $ltd , '' ) ;
758 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
760 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) # Caption
762 if ( '|+' == substr ( $x , 0 , 2 ) )
764 $fc = '+' ;
765 $x = substr ( $x , 1 ) ;
767 $after = substr ( $x , 1 ) ;
768 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
769 $after = explode ( '||' , $after ) ;
770 $t[$k] = '' ;
771 foreach ( $after AS $theline )
773 $z = '' ;
774 if ( $fc != '+' )
776 $tra = array_pop ( $ltr ) ;
777 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
778 array_push ( $tr , true ) ;
779 array_push ( $ltr , '' ) ;
782 $l = array_pop ( $ltd ) ;
783 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
784 if ( $fc == '|' ) $l = 'td' ;
785 else if ( $fc == '!' ) $l = 'th' ;
786 else if ( $fc == '+' ) $l = 'caption' ;
787 else $l = '' ;
788 array_push ( $ltd , $l ) ;
789 $y = explode ( '|' , $theline , 2 ) ;
790 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
791 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
792 $t[$k] .= $y ;
793 array_push ( $td , true ) ;
798 # Closing open td, tr && table
799 while ( count ( $td ) > 0 )
801 if ( array_pop ( $td ) ) $t[] = '</td>' ;
802 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
803 $t[] = '</table>' ;
806 $t = implode ( "\n" , $t ) ;
807 # $t = $this->removeHTMLtags( $t );
808 wfProfileOut( $fname );
809 return $t ;
812 # Parses the text and adds the result to the strip state
813 # Returns the strip tag
814 function stripParse( $text, $newline, $args )
816 $text = $this->strip( $text, $this->mStripState );
817 $text = $this->internalParse( $text, (bool)$newline, $args, false );
818 return $newline.$this->insertStripItem( $text, $this->mStripState );
821 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
822 $fname = 'Parser::internalParse';
823 wfProfileIn( $fname );
825 $text = $this->removeHTMLtags( $text );
826 $text = $this->replaceVariables( $text, $args );
828 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
830 $text = $this->doHeadings( $text );
831 if($this->mOptions->getUseDynamicDates()) {
832 global $wgDateFormatter;
833 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
835 $text = $this->doAllQuotes( $text );
836 $text = $this->replaceExternalLinks( $text );
837 $text = $this->doMagicLinks( $text );
838 $text = $this->replaceInternalLinks ( $text );
839 $text = $this->replaceInternalLinks ( $text );
840 $text = $this->doTableStuff( $text );
841 $text = $this->formatHeadings( $text, $isMain );
842 $sk =& $this->mOptions->getSkin();
843 $text = $sk->transformContent( $text );
845 if ( $isMain && !isset ( $this->categoryMagicDone ) ) {
846 $text .= $this->categoryMagic () ;
847 $this->categoryMagicDone = true ;
850 wfProfileOut( $fname );
851 return $text;
854 /* private */ function &doMagicLinks( &$text ) {
855 $text = $this->magicISBN( $text );
856 $text = $this->magicGEO( $text );
857 $text = $this->magicRFC( $text );
858 return $text;
861 # Parse ^^ tokens and return html
862 /* private */ function doExponent ( $text )
864 $fname = 'Parser::doExponent';
865 wfProfileIn( $fname);
866 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
867 wfProfileOut( $fname);
868 return $text;
871 # Parse headers and return html
872 /* private */ function doHeadings( $text ) {
873 $fname = 'Parser::doHeadings';
874 wfProfileIn( $fname );
875 for ( $i = 6; $i >= 1; --$i ) {
876 $h = substr( '======', 0, $i );
877 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
878 "<h{$i}>\\1</h{$i}>\\2", $text );
880 wfProfileOut( $fname );
881 return $text;
884 /* private */ function doAllQuotes( $text ) {
885 $fname = 'Parser::doAllQuotes';
886 wfProfileIn( $fname );
887 $outtext = '';
888 $lines = explode( "\n", $text );
889 foreach ( $lines as $line ) {
890 $outtext .= $this->doQuotes ( $line ) . "\n";
892 $outtext = substr($outtext, 0,-1);
893 wfProfileOut( $fname );
894 return $outtext;
897 /* private */ function doQuotes( $text ) {
898 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
899 if (count ($arr) == 1)
900 return $text;
901 else
903 $i = 0;
904 foreach ($arr as $r)
906 if (($i % 2) == 1)
908 # If there are ever four apostrophes, assume the first is supposed to
909 # be text, and the remaining three constitute mark-up for bold text.
910 if (strlen ($arr[$i]) == 4)
912 $arr[$i-1] .= "'";
913 $arr[$i] = "'''";
915 # If there are more than 5 apostrophes in a row, assume they're all
916 # text except for the last 5.
917 else if (strlen ($arr[$i]) > 5)
919 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
920 $arr[$i] = "'''''";
924 $i++;
927 # Now see if there's an odd or even number of "bold" and "italic"
928 # mark-up. There should normally be an even number of both.
929 $i = 0;
930 $numbold = 0;
931 $numitalics = 0;
932 foreach ($arr as $r)
934 if (($i % 2) == 1)
936 if (strlen ($r) == 2) $numitalics++; else
937 if (strlen ($r) == 3) $numbold++; else
938 if (strlen ($r) == 5) { $numitalics++; $numbold++; }
940 $i++;
943 # If there is an odd number of both bold and italics, it is likely
944 # that one of the bold ones was meant to be an apostrophe followed
945 # by italics. Which one we cannot know for certain, but it is more
946 # likely to be one that has a single-letter word before it.
947 if (($numbold % 2 == 1) && ($numitalics % 2 == 1))
949 $i = 0;
950 $firstsingleletterword = -1;
951 $firstmultiletterword = -1;
952 $firstspace = -1;
953 foreach ($arr as $r)
955 if (($i % 2 == 1) and (strlen ($r) == 3))
957 $x1 = substr ($arr[$i-1], -1);
958 $x2 = substr ($arr[$i-1], -2, 1);
959 if ($x1 == " ") {
960 if ($firstspace == -1) $firstspace = $i;
961 } else if ($x2 == " ") {
962 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
963 } else {
964 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
967 $i++;
970 # If there is a single-letter word, use it!
971 if ($firstsingleletterword > -1)
973 $arr [ $firstsingleletterword ] = "''";
974 $arr [ $firstsingleletterword-1 ] .= "'";
976 # If not, but there's a multi-letter word, use that one.
977 else if ($firstmultiletterword > -1)
979 $arr [ $firstmultiletterword ] = "''";
980 $arr [ $firstmultiletterword-1 ] .= "'";
982 # ... otherwise use the first one that has neither.
983 else
985 $arr [ $firstspace ] = "''";
986 $arr [ $firstspace-1 ] .= "'";
990 # Now let's actually convert our apostrophic mush to HTML!
991 $output = '';
992 $buffer = '';
993 $state = '';
994 $i = 0;
995 foreach ($arr as $r)
997 if (($i % 2) == 0)
999 if ($state == 'both')
1000 $buffer .= $r;
1001 else
1002 $output .= $r;
1004 else
1006 if (strlen ($r) == 2)
1008 if ($state == 'em')
1009 { $output .= "</em>"; $state = ''; }
1010 else if ($state == 'strongem')
1011 { $output .= "</em>"; $state = 'strong'; }
1012 else if ($state == 'emstrong')
1013 { $output .= "</strong></em><strong>"; $state = 'strong'; }
1014 else if ($state == 'both')
1015 { $output .= "<strong><em>{$buffer}</em>"; $state = 'strong'; }
1016 else # $state can be 'strong' or ''
1017 { $output .= "<em>"; $state .= 'em'; }
1019 else if (strlen ($r) == 3)
1021 if ($state == 'strong')
1022 { $output .= "</strong>"; $state = ''; }
1023 else if ($state == 'strongem')
1024 { $output .= "</em></strong><em>"; $state = 'em'; }
1025 else if ($state == 'emstrong')
1026 { $output .= "</strong>"; $state = 'em'; }
1027 else if ($state == 'both')
1028 { $output .= "<em><strong>{$buffer}</strong>"; $state = 'em'; }
1029 else # $state can be 'em' or ''
1030 { $output .= "<strong>"; $state .= 'strong'; }
1032 else if (strlen ($r) == 5)
1034 if ($state == 'strong')
1035 { $output .= "</strong><em>"; $state = 'em'; }
1036 else if ($state == 'em')
1037 { $output .= "</em><strong>"; $state = 'strong'; }
1038 else if ($state == 'strongem')
1039 { $output .= "</em></strong>"; $state = ''; }
1040 else if ($state == 'emstrong')
1041 { $output .= "</strong></em>"; $state = ''; }
1042 else if ($state == 'both')
1043 { $output .= "<em><strong>{$buffer}</strong></em>"; $state = ''; }
1044 else # ($state == '')
1045 { $buffer = ''; $state = 'both'; }
1048 $i++;
1050 return $output;
1054 # Note: we have to do external links before the internal ones,
1055 # and otherwise take great care in the order of things here, so
1056 # that we don't end up interpreting some URLs twice.
1058 /* private */ function replaceExternalLinks( $text ) {
1059 $fname = 'Parser::replaceExternalLinks';
1060 wfProfileIn( $fname );
1061 $text = $this->subReplaceExternalLinks( $text, 'http', true );
1062 $text = $this->subReplaceExternalLinks( $text, 'https', true );
1063 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
1064 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
1065 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
1066 $text = $this->subReplaceExternalLinks( $text, 'news', false );
1067 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
1068 wfProfileOut( $fname );
1069 return $text;
1072 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
1073 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
1074 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
1076 # this is the list of separators that should be ignored if they
1077 # are the last character of an URL but that should be included
1078 # if they occur within the URL, e.g. "go to www.foo.com, where .."
1079 # in this case, the last comma should not become part of the URL,
1080 # but in "www.foo.com/123,2342,32.htm" it should.
1081 $sep = ",;\.:";
1082 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
1083 $images = 'gif|png|jpg|jpeg';
1085 # PLEASE NOTE: The curly braces { } are not part of the regex,
1086 # they are interpreted as part of the string (used to tell PHP
1087 # that the content of the string should be inserted there).
1088 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
1089 "((?i){$images})([^{$uc}]|$)/";
1091 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
1092 $sk =& $this->mOptions->getSkin();
1094 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) { # Use img tags only for HTTP urls
1095 $s = preg_replace( $e1, '\\1' . $sk->makeImage( "{$unique}:\\3" .
1096 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
1098 $s = preg_replace( $e2, '\\1' . "<a href=\"{$unique}:\\3\"" .
1099 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
1100 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
1101 '</a>\\5', $s );
1102 $s = str_replace( $unique, $protocol, $s );
1104 $a = explode( "[{$protocol}:", " " . $s );
1105 $s = array_shift( $a );
1106 $s = substr( $s, 1 );
1108 # Regexp for URL in square brackets
1109 $e1 = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
1110 # Regexp for URL with link text in square brackets
1111 $e2 = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
1113 foreach ( $a as $line ) {
1115 # CASE 1: Link in square brackets, e.g.
1116 # some text [http://domain.tld/some.link] more text
1117 if ( preg_match( $e1, $line, $m ) ) {
1118 $link = "{$protocol}:{$m[1]}";
1119 $trail = $m[2];
1120 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
1121 else { $text = wfEscapeHTML( $link ); }
1124 # CASE 2: Link with link text and text directly following it, e.g.
1125 # This is a collection of [http://domain.tld/some.link link]s
1126 else if ( preg_match( $e2, $line, $m ) ) {
1127 $link = "{$protocol}:{$m[1]}";
1128 $text = $m[2];
1129 $dtrail = '';
1130 $trail = $m[3];
1131 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1132 $dtrail = $m2[1];
1133 $trail = $m2[2];
1137 # CASE 3: Nothing matches, just output the source text
1138 else {
1139 $s .= "[{$protocol}:" . $line;
1140 continue;
1143 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1144 $paren = '';
1145 } else {
1146 # Expand the URL for printable version
1147 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1149 $la = $sk->getExternalLinkAttributes( $link, $text );
1150 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1153 return $s;
1157 /* private */ function replaceInternalLinks( $s ) {
1158 global $wgLang, $wgLinkCache;
1159 global $wgNamespacesWithSubpages, $wgLanguageCode;
1160 static $fname = 'Parser::replaceInternalLinks' ;
1161 wfProfileIn( $fname );
1163 wfProfileIn( $fname.'-setup' );
1164 static $tc = FALSE;
1165 # the % is needed to support urlencoded titles as well
1166 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1167 $sk =& $this->mOptions->getSkin();
1169 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1170 $isRedirect = $redirect->matchStart ( strtoupper ( substr ( $s , 0 , 10 ) ) ) ;
1172 $a = explode( '[[', ' ' . $s );
1173 $s = array_shift( $a );
1174 $s = substr( $s, 1 );
1176 # Match a link having the form [[namespace:link|alternate]]trail
1177 static $e1 = FALSE;
1178 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1179 # Match the end of a line for a word that's not followed by whitespace,
1180 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1181 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1183 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1184 # Special and Media are pseudo-namespaces; no pages actually exist in them
1185 static $image = FALSE;
1186 static $special = FALSE;
1187 static $media = FALSE;
1188 static $category = FALSE;
1189 if ( !$image ) { $image = Namespace::getImage(); }
1190 if ( !$special ) { $special = Namespace::getSpecial(); }
1191 if ( !$media ) { $media = Namespace::getMedia(); }
1192 if ( !$category ) { $category = Namespace::getCategory(); }
1194 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1196 if ( $useLinkPrefixExtension ) {
1197 if ( preg_match( $e2, $s, $m ) ) {
1198 $first_prefix = $m[2];
1199 $s = $m[1];
1200 } else {
1201 $first_prefix = false;
1203 } else {
1204 $prefix = '';
1207 wfProfileOut( $fname.'-setup' );
1209 foreach ( $a as $line ) {
1210 wfProfileIn( $fname.'-prefixhandling' );
1211 if ( $useLinkPrefixExtension ) {
1212 if ( preg_match( $e2, $s, $m ) ) {
1213 $prefix = $m[2];
1214 $s = $m[1];
1215 } else {
1216 $prefix='';
1218 # first link
1219 if($first_prefix) {
1220 $prefix = $first_prefix;
1221 $first_prefix = false;
1224 wfProfileOut( $fname.'-prefixhandling' );
1226 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1227 $text = $m[2];
1228 # fix up urlencoded title texts
1229 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1230 $trail = $m[3];
1231 } else { # Invalid form; output directly
1232 $s .= $prefix . '[[' . $line ;
1233 continue;
1236 /* Valid link forms:
1237 Foobar -- normal
1238 :Foobar -- override special treatment of prefix (images, language links)
1239 /Foobar -- convert to CurrentPage/Foobar
1240 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1242 $c = substr($m[1],0,1);
1243 $noforce = ($c != ':');
1244 if( $c == '/' ) { # subpage
1245 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1246 $m[1]=substr($m[1],1,strlen($m[1])-2);
1247 $noslash=$m[1];
1248 } else {
1249 $noslash=substr($m[1],1);
1251 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1252 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1253 if( '' == $text ) {
1254 $text= $m[1];
1255 } # this might be changed for ugliness reasons
1256 } else {
1257 $link = $noslash; # no subpage allowed, use standard link
1259 } elseif( $noforce ) { # no subpage
1260 $link = $m[1];
1261 } else {
1262 $link = substr( $m[1], 1 );
1264 $wasblank = ( '' == $text );
1265 if( $wasblank )
1266 $text = $link;
1268 $nt = Title::newFromText( $link );
1269 if( !$nt ) {
1270 $s .= $prefix . '[[' . $line;
1271 continue;
1273 $ns = $nt->getNamespace();
1274 $iw = $nt->getInterWiki();
1275 if( $noforce ) {
1276 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1277 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1278 $tmp = $prefix . $trail ;
1279 $s .= (trim($tmp) == '')? '': $tmp;
1280 continue;
1282 if ( $ns == $image ) {
1283 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1284 $wgLinkCache->addImageLinkObj( $nt );
1285 continue;
1287 if ( $ns == $category && !$isRedirect ) {
1288 $t = $nt->getText() ;
1289 $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
1291 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1292 $pPLC=$sk->postParseLinkColour();
1293 $sk->postParseLinkColour( false );
1294 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1295 $sk->postParseLinkColour( $pPLC );
1296 $wgLinkCache->resume();
1298 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1299 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1300 $this->mOutput->mCategoryLinks[] = $t ;
1301 $s .= $prefix . $trail ;
1302 continue;
1305 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1306 ( strpos( $link, '#' ) == FALSE ) ) {
1307 # Self-links are handled specially; generally de-link and change to bold.
1308 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1309 continue;
1312 if( $ns == $media ) {
1313 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1314 $wgLinkCache->addImageLinkObj( $nt );
1315 continue;
1316 } elseif( $ns == $special ) {
1317 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1318 continue;
1320 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1322 wfProfileOut( $fname );
1323 return $s;
1326 # Some functions here used by doBlockLevels()
1328 /* private */ function closeParagraph() {
1329 $result = '';
1330 if ( '' != $this->mLastSection ) {
1331 $result = '</' . $this->mLastSection . ">\n";
1333 $this->mInPre = false;
1334 $this->mLastSection = '';
1335 return $result;
1337 # getCommon() returns the length of the longest common substring
1338 # of both arguments, starting at the beginning of both.
1340 /* private */ function getCommon( $st1, $st2 ) {
1341 $fl = strlen( $st1 );
1342 $shorter = strlen( $st2 );
1343 if ( $fl < $shorter ) { $shorter = $fl; }
1345 for ( $i = 0; $i < $shorter; ++$i ) {
1346 if ( $st1{$i} != $st2{$i} ) { break; }
1348 return $i;
1350 # These next three functions open, continue, and close the list
1351 # element appropriate to the prefix character passed into them.
1353 /* private */ function openList( $char )
1355 $result = $this->closeParagraph();
1357 if ( '*' == $char ) { $result .= '<ul><li>'; }
1358 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1359 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1360 else if ( ';' == $char ) {
1361 $result .= '<dl><dt>';
1362 $this->mDTopen = true;
1364 else { $result = '<!-- ERR 1 -->'; }
1366 return $result;
1369 /* private */ function nextItem( $char ) {
1370 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1371 else if ( ':' == $char || ';' == $char ) {
1372 $close = "</dd>";
1373 if ( $this->mDTopen ) { $close = '</dt>'; }
1374 if ( ';' == $char ) {
1375 $this->mDTopen = true;
1376 return $close . '<dt>';
1377 } else {
1378 $this->mDTopen = false;
1379 return $close . '<dd>';
1382 return '<!-- ERR 2 -->';
1385 /* private */function closeList( $char ) {
1386 if ( '*' == $char ) { $text = '</li></ul>'; }
1387 else if ( '#' == $char ) { $text = '</li></ol>'; }
1388 else if ( ':' == $char ) {
1389 if ( $this->mDTopen ) {
1390 $this->mDTopen = false;
1391 $text = '</dt></dl>';
1392 } else {
1393 $text = '</dd></dl>';
1396 else { return '<!-- ERR 3 -->'; }
1397 return $text."\n";
1400 /* private */ function doBlockLevels( $text, $linestart ) {
1401 $fname = 'Parser::doBlockLevels';
1402 wfProfileIn( $fname );
1404 # Parsing through the text line by line. The main thing
1405 # happening here is handling of block-level elements p, pre,
1406 # and making lists from lines starting with * # : etc.
1408 $textLines = explode( "\n", $text );
1410 $lastPrefix = $output = $lastLine = '';
1411 $this->mDTopen = $inBlockElem = false;
1412 $prefixLength = 0;
1413 $paragraphStack = false;
1415 if ( !$linestart ) {
1416 $output .= array_shift( $textLines );
1418 foreach ( $textLines as $oLine ) {
1419 $lastPrefixLength = strlen( $lastPrefix );
1420 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1421 $preOpenMatch = preg_match("/<pre/i", $oLine );
1422 if ( !$this->mInPre ) {
1423 # Multiple prefixes may abut each other for nested lists.
1424 $prefixLength = strspn( $oLine, '*#:;' );
1425 $pref = substr( $oLine, 0, $prefixLength );
1427 # eh?
1428 $pref2 = str_replace( ';', ':', $pref );
1429 $t = substr( $oLine, $prefixLength );
1430 $this->mInPre = !empty($preOpenMatch);
1431 } else {
1432 # Don't interpret any other prefixes in preformatted text
1433 $prefixLength = 0;
1434 $pref = $pref2 = '';
1435 $t = $oLine;
1438 # List generation
1439 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1440 # Same as the last item, so no need to deal with nesting or opening stuff
1441 $output .= $this->nextItem( substr( $pref, -1 ) );
1442 $paragraphStack = false;
1444 if ( ";" == substr( $pref, -1 ) ) {
1445 # The one nasty exception: definition lists work like this:
1446 # ; title : definition text
1447 # So we check for : in the remainder text to split up the
1448 # title and definition, without b0rking links.
1449 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1450 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1451 $term = $match[1];
1452 $output .= $term . $this->nextItem( ':' );
1453 $t = $match[2];
1456 } elseif( $prefixLength || $lastPrefixLength ) {
1457 # Either open or close a level...
1458 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1459 $paragraphStack = false;
1461 while( $commonPrefixLength < $lastPrefixLength ) {
1462 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1463 --$lastPrefixLength;
1465 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1466 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1468 while ( $prefixLength > $commonPrefixLength ) {
1469 $char = substr( $pref, $commonPrefixLength, 1 );
1470 $output .= $this->openList( $char );
1472 if ( ';' == $char ) {
1473 # FIXME: This is dupe of code above
1474 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1475 $term = $match[1];
1476 $output .= $term . $this->nextItem( ":" );
1477 $t = $match[2];
1480 ++$commonPrefixLength;
1482 $lastPrefix = $pref2;
1484 if( 0 == $prefixLength ) {
1485 # No prefix (not in list)--go to paragraph mode
1486 $uniq_prefix = UNIQ_PREFIX;
1487 // XXX: use a stack for nestable elements like span, table and div
1488 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1489 $closematch = preg_match(
1490 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1491 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1492 if ( $openmatch or $closematch ) {
1493 $paragraphStack = false;
1494 $output .= $this->closeParagraph();
1495 if($preOpenMatch and !$preCloseMatch) {
1496 $this->mInPre = true;
1498 if ( $closematch ) {
1499 $inBlockElem = false;
1500 } else {
1501 $inBlockElem = true;
1503 } else if ( !$inBlockElem && !$this->mInPre ) {
1504 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1505 // pre
1506 if ($this->mLastSection != 'pre') {
1507 $paragraphStack = false;
1508 $output .= $this->closeParagraph().'<pre>';
1509 $this->mLastSection = 'pre';
1511 } else {
1512 // paragraph
1513 if ( '' == trim($t) ) {
1514 if ( $paragraphStack ) {
1515 $output .= $paragraphStack.'<br />';
1516 $paragraphStack = false;
1517 $this->mLastSection = 'p';
1518 } else {
1519 if ($this->mLastSection != 'p' ) {
1520 $output .= $this->closeParagraph();
1521 $this->mLastSection = '';
1522 $paragraphStack = '<p>';
1523 } else {
1524 $paragraphStack = '</p><p>';
1527 } else {
1528 if ( $paragraphStack ) {
1529 $output .= $paragraphStack;
1530 $paragraphStack = false;
1531 $this->mLastSection = 'p';
1532 } else if ($this->mLastSection != 'p') {
1533 $output .= $this->closeParagraph().'<p>';
1534 $this->mLastSection = 'p';
1540 if ($paragraphStack === false) {
1541 $output .= $t."\n";
1544 while ( $prefixLength ) {
1545 $output .= $this->closeList( $pref2{$prefixLength-1} );
1546 --$prefixLength;
1548 if ( '' != $this->mLastSection ) {
1549 $output .= '</' . $this->mLastSection . '>';
1550 $this->mLastSection = '';
1553 wfProfileOut( $fname );
1554 return $output;
1557 # Return value of a magic variable (like PAGENAME)
1558 function getVariableValue( $index ) {
1559 global $wgLang, $wgSitename, $wgServer;
1561 switch ( $index ) {
1562 case MAG_CURRENTMONTH:
1563 return $wgLang->formatNum( date( 'm' ) );
1564 case MAG_CURRENTMONTHNAME:
1565 return $wgLang->getMonthName( date('n') );
1566 case MAG_CURRENTMONTHNAMEGEN:
1567 return $wgLang->getMonthNameGen( date('n') );
1568 case MAG_CURRENTDAY:
1569 return $wgLang->formatNum( date('j') );
1570 case MAG_PAGENAME:
1571 return $this->mTitle->getText();
1572 case MAG_NAMESPACE:
1573 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1574 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1575 case MAG_CURRENTDAYNAME:
1576 return $wgLang->getWeekdayName( date('w')+1 );
1577 case MAG_CURRENTYEAR:
1578 return $wgLang->formatNum( date( 'Y' ) );
1579 case MAG_CURRENTTIME:
1580 return $wgLang->time( wfTimestampNow(), false );
1581 case MAG_NUMBEROFARTICLES:
1582 return $wgLang->formatNum( wfNumberOfArticles() );
1583 case MAG_SITENAME:
1584 return $wgSitename;
1585 case MAG_SERVER:
1586 return $wgServer;
1587 default:
1588 return NULL;
1592 # initialise the magic variables (like CURRENTMONTHNAME)
1593 function initialiseVariables() {
1594 global $wgVariableIDs;
1595 $this->mVariables = array();
1596 foreach ( $wgVariableIDs as $id ) {
1597 $mw =& MagicWord::get( $id );
1598 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1602 /* private */ function replaceVariables( $text, $args = array() ) {
1603 global $wgLang, $wgScript, $wgArticlePath;
1605 # Prevent too big inclusions
1606 if(strlen($text)> MAX_INCLUDE_SIZE)
1607 return $text;
1609 $fname = 'Parser::replaceVariables';
1610 wfProfileIn( $fname );
1612 $bail = false;
1613 $titleChars = Title::legalChars();
1614 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1616 # This function is called recursively. To keep track of arguments we need a stack:
1617 array_push( $this->mArgStack, $args );
1619 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1620 $GLOBALS['wgCurParser'] =& $this;
1623 if ( $this->mOutputType == OT_HTML ) {
1624 # Variable substitution
1625 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1627 # Argument substitution
1628 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1630 # Template substitution
1631 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1632 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1634 array_pop( $this->mArgStack );
1636 wfProfileOut( $fname );
1637 return $text;
1640 function variableSubstitution( $matches ) {
1641 if ( !$this->mVariables ) {
1642 $this->initialiseVariables();
1644 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1645 $text = $this->mVariables[$matches[1]];
1646 $this->mOutput->mContainsOldMagic = true;
1647 } else {
1648 $text = $matches[0];
1650 return $text;
1653 # Split template arguments
1654 function getTemplateArgs( $argsString ) {
1655 if ( $argsString === '' ) {
1656 return array();
1659 $args = explode( '|', substr( $argsString, 1 ) );
1661 # If any of the arguments contains a '[[' but no ']]', it needs to be
1662 # merged with the next arg because the '|' character between belongs
1663 # to the link syntax and not the template parameter syntax.
1664 $argc = count($args);
1665 $i = 0;
1666 for ( $i = 0; $i < $argc-1; $i++ ) {
1667 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1668 $args[$i] .= "|".$args[$i+1];
1669 array_splice($args, $i+1, 1);
1670 $i--;
1671 $argc--;
1675 return $args;
1678 function braceSubstitution( $matches ) {
1679 global $wgLinkCache, $wgLang;
1680 $fname = 'Parser::braceSubstitution';
1681 $found = false;
1682 $nowiki = false;
1683 $noparse = false;
1685 $title = NULL;
1687 # $newline is an optional newline character before the braces
1688 # $part1 is the bit before the first |, and must contain only title characters
1689 # $args is a list of arguments, starting from index 0, not including $part1
1691 $newline = $matches[1];
1692 $part1 = $matches[2];
1693 # If the third subpattern matched anything, it will start with |
1695 $args = $this->getTemplateArgs($matches[3]);
1696 $argc = count( $args );
1698 # {{{}}}
1699 if ( strpos( $matches[0], '{{{' ) !== false ) {
1700 $text = $matches[0];
1701 $found = true;
1702 $noparse = true;
1705 # SUBST
1706 if ( !$found ) {
1707 $mwSubst =& MagicWord::get( MAG_SUBST );
1708 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1709 if ( $this->mOutputType != OT_WIKI ) {
1710 # Invalid SUBST not replaced at PST time
1711 # Return without further processing
1712 $text = $matches[0];
1713 $found = true;
1714 $noparse= true;
1716 } elseif ( $this->mOutputType == OT_WIKI ) {
1717 # SUBST not found in PST pass, do nothing
1718 $text = $matches[0];
1719 $found = true;
1723 # MSG, MSGNW and INT
1724 if ( !$found ) {
1725 # Check for MSGNW:
1726 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1727 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1728 $nowiki = true;
1729 } else {
1730 # Remove obsolete MSG:
1731 $mwMsg =& MagicWord::get( MAG_MSG );
1732 $mwMsg->matchStartAndRemove( $part1 );
1735 # Check if it is an internal message
1736 $mwInt =& MagicWord::get( MAG_INT );
1737 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1738 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1739 $text = wfMsgReal( $part1, $args, true );
1740 $found = true;
1745 # NS
1746 if ( !$found ) {
1747 # Check for NS: (namespace expansion)
1748 $mwNs = MagicWord::get( MAG_NS );
1749 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1750 if ( intval( $part1 ) ) {
1751 $text = $wgLang->getNsText( intval( $part1 ) );
1752 $found = true;
1753 } else {
1754 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1755 if ( !is_null( $index ) ) {
1756 $text = $wgLang->getNsText( $index );
1757 $found = true;
1763 # LOCALURL and LOCALURLE
1764 if ( !$found ) {
1765 $mwLocal = MagicWord::get( MAG_LOCALURL );
1766 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1768 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1769 $func = 'getLocalURL';
1770 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1771 $func = 'escapeLocalURL';
1772 } else {
1773 $func = '';
1776 if ( $func !== '' ) {
1777 $title = Title::newFromText( $part1 );
1778 if ( !is_null( $title ) ) {
1779 if ( $argc > 0 ) {
1780 $text = $title->$func( $args[0] );
1781 } else {
1782 $text = $title->$func();
1784 $found = true;
1789 # Internal variables
1790 if ( !$this->mVariables ) {
1791 $this->initialiseVariables();
1793 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1794 $text = $this->mVariables[$part1];
1795 $found = true;
1796 $this->mOutput->mContainsOldMagic = true;
1799 # Template table test
1801 # Did we encounter this template already? If yes, it is in the cache
1802 # and we need to check for loops.
1803 if ( isset( $this->mTemplates[$part1] ) ) {
1804 # Infinite loop test
1805 if ( isset( $this->mTemplatePath[$part1] ) ) {
1806 $noparse = true;
1807 $found = true;
1809 # set $text to cached message.
1810 $text = $this->mTemplates[$part1];
1811 $found = true;
1814 # Load from database
1815 if ( !$found ) {
1816 $title = Title::newFromText( $part1, NS_TEMPLATE );
1817 if ( !is_null( $title ) && !$title->isExternal() ) {
1818 # Check for excessive inclusion
1819 $dbk = $title->getPrefixedDBkey();
1820 if ( $this->incrementIncludeCount( $dbk ) ) {
1821 # This should never be reached.
1822 $article = new Article( $title );
1823 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1824 if ( $articleContent !== false ) {
1825 $found = true;
1826 $text = $articleContent;
1831 # If the title is valid but undisplayable, make a link to it
1832 if ( $this->mOutputType == OT_HTML && !$found ) {
1833 $text = '[[' . $title->getPrefixedText() . ']]';
1834 $found = true;
1837 # Template cache array insertion
1838 $this->mTemplates[$part1] = $text;
1842 # Recursive parsing, escaping and link table handling
1843 # Only for HTML output
1844 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1845 $text = wfEscapeWikiText( $text );
1846 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1847 # Clean up argument array
1848 $assocArgs = array();
1849 $index = 1;
1850 foreach( $args as $arg ) {
1851 $eqpos = strpos( $arg, '=' );
1852 if ( $eqpos === false ) {
1853 $assocArgs[$index++] = $arg;
1854 } else {
1855 $name = trim( substr( $arg, 0, $eqpos ) );
1856 $value = trim( substr( $arg, $eqpos+1 ) );
1857 if ( $value === false ) {
1858 $value = '';
1860 if ( $name !== false ) {
1861 $assocArgs[$name] = $value;
1866 # Do not enter included links in link table
1867 if ( !is_null( $title ) ) {
1868 $wgLinkCache->suspend();
1871 # Add a new element to the templace recursion path
1872 $this->mTemplatePath[$part1] = 1;
1874 # Run full parser on the included text
1875 $text = $this->internalParse( $text, $newline, $assocArgs );
1876 # I replaced the line below with the line above, as it former seems to cause several bugs
1877 #$text = $this->stripParse( $text, $newline, $assocArgs );
1879 # Resume the link cache and register the inclusion as a link
1880 if ( !is_null( $title ) ) {
1881 $wgLinkCache->resume();
1882 $wgLinkCache->addLinkObj( $title );
1885 # Empties the template path
1886 $this->mTemplatePath = array();
1888 if ( !$found ) {
1889 return $matches[0];
1890 } else {
1891 return $text;
1895 # Triple brace replacement -- used for template arguments
1896 function argSubstitution( $matches ) {
1897 $newline = $matches[1];
1898 $arg = trim( $matches[2] );
1899 $text = $matches[0];
1900 $inputArgs = end( $this->mArgStack );
1902 if ( array_key_exists( $arg, $inputArgs ) ) {
1903 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1906 return $text;
1909 # Returns true if the function is allowed to include this entity
1910 function incrementIncludeCount( $dbk ) {
1911 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1912 $this->mIncludeCount[$dbk] = 0;
1914 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1915 return true;
1916 } else {
1917 return false;
1922 # Cleans up HTML, removes dangerous tags and attributes
1923 /* private */ function removeHTMLtags( $text ) {
1924 global $wgUseTidy, $wgUserHtml;
1925 $fname = 'Parser::removeHTMLtags';
1926 wfProfileIn( $fname );
1928 if( $wgUserHtml ) {
1929 $htmlpairs = array( # Tags that must be closed
1930 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1931 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1932 'strike', 'strong', 'tt', 'var', 'div', 'center',
1933 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1934 'ruby', 'rt' , 'rb' , 'rp', 'p'
1936 $htmlsingle = array(
1937 'br', 'hr', 'li', 'dt', 'dd'
1939 $htmlnest = array( # Tags that can be nested--??
1940 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1941 'dl', 'font', 'big', 'small', 'sub', 'sup'
1943 $tabletags = array( # Can only appear inside table
1944 'td', 'th', 'tr'
1946 } else {
1947 $htmlpairs = array();
1948 $htmlsingle = array();
1949 $htmlnest = array();
1950 $tabletags = array();
1953 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1954 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1956 $htmlattrs = $this->getHTMLattrs () ;
1958 # Remove HTML comments
1959 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1961 $bits = explode( '<', $text );
1962 $text = array_shift( $bits );
1963 if(!$wgUseTidy) {
1964 $tagstack = array(); $tablestack = array();
1965 foreach ( $bits as $x ) {
1966 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1967 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1968 $x, $regs );
1969 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1970 error_reporting( $prev );
1972 $badtag = 0 ;
1973 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1974 # Check our stack
1975 if ( $slash ) {
1976 # Closing a tag...
1977 if ( ! in_array( $t, $htmlsingle ) &&
1978 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1979 @array_push( $tagstack, $ot );
1980 $badtag = 1;
1981 } else {
1982 if ( $t == 'table' ) {
1983 $tagstack = array_pop( $tablestack );
1985 $newparams = '';
1987 } else {
1988 # Keep track for later
1989 if ( in_array( $t, $tabletags ) &&
1990 ! in_array( 'table', $tagstack ) ) {
1991 $badtag = 1;
1992 } else if ( in_array( $t, $tagstack ) &&
1993 ! in_array ( $t , $htmlnest ) ) {
1994 $badtag = 1 ;
1995 } else if ( ! in_array( $t, $htmlsingle ) ) {
1996 if ( $t == 'table' ) {
1997 array_push( $tablestack, $tagstack );
1998 $tagstack = array();
2000 array_push( $tagstack, $t );
2002 # Strip non-approved attributes from the tag
2003 $newparams = $this->fixTagAttributes($params);
2006 if ( ! $badtag ) {
2007 $rest = str_replace( '>', '&gt;', $rest );
2008 $text .= "<$slash$t $newparams$brace$rest";
2009 continue;
2012 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2014 # Close off any remaining tags
2015 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2016 $text .= "</$t>\n";
2017 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2019 } else {
2020 # this might be possible using tidy itself
2021 foreach ( $bits as $x ) {
2022 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2023 $x, $regs );
2024 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2025 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2026 $newparams = $this->fixTagAttributes($params);
2027 $rest = str_replace( '>', '&gt;', $rest );
2028 $text .= "<$slash$t $newparams$brace$rest";
2029 } else {
2030 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2034 wfProfileOut( $fname );
2035 return $text;
2041 * This function accomplishes several tasks:
2042 * 1) Auto-number headings if that option is enabled
2043 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2044 * 3) Add a Table of contents on the top for users who have enabled the option
2045 * 4) Auto-anchor headings
2047 * It loops through all headlines, collects the necessary data, then splits up the
2048 * string and re-inserts the newly formatted headlines.
2052 /* private */ function formatHeadings( $text, $isMain=true ) {
2053 global $wgInputEncoding, $wgMaxTocLevel;
2055 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2056 $doShowToc = $this->mOptions->getShowToc();
2057 $forceTocHere = false;
2058 if( !$this->mTitle->userCanEdit() ) {
2059 $showEditLink = 0;
2060 $rightClickHack = 0;
2061 } else {
2062 $showEditLink = $this->mOptions->getEditSection();
2063 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2066 # Inhibit editsection links if requested in the page
2067 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2068 if( $esw->matchAndRemove( $text ) ) {
2069 $showEditLink = 0;
2071 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2072 # do not add TOC
2073 $mw =& MagicWord::get( MAG_NOTOC );
2074 if( $mw->matchAndRemove( $text ) ) {
2075 $doShowToc = 0;
2078 # never add the TOC to the Main Page. This is an entry page that should not
2079 # be more than 1-2 screens large anyway
2080 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2081 $doShowToc = 0;
2084 # Get all headlines for numbering them and adding funky stuff like [edit]
2085 # links - this is for later, but we need the number of headlines right now
2086 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2088 # if there are fewer than 4 headlines in the article, do not show TOC
2089 if( $numMatches < 4 ) {
2090 $doShowToc = 0;
2093 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2094 # override above conditions and always show TOC at that place
2095 $mw =& MagicWord::get( MAG_TOC );
2096 if ($mw->match( $text ) ) {
2097 $doShowToc = 1;
2098 $forceTocHere = true;
2099 } else {
2100 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2101 # override above conditions and always show TOC above first header
2102 $mw =& MagicWord::get( MAG_FORCETOC );
2103 if ($mw->matchAndRemove( $text ) ) {
2104 $doShowToc = 1;
2110 # We need this to perform operations on the HTML
2111 $sk =& $this->mOptions->getSkin();
2113 # headline counter
2114 $headlineCount = 0;
2116 # Ugh .. the TOC should have neat indentation levels which can be
2117 # passed to the skin functions. These are determined here
2118 $toclevel = 0;
2119 $toc = '';
2120 $full = '';
2121 $head = array();
2122 $sublevelCount = array();
2123 $level = 0;
2124 $prevlevel = 0;
2125 foreach( $matches[3] as $headline ) {
2126 $numbering = '';
2127 if( $level ) {
2128 $prevlevel = $level;
2130 $level = $matches[1][$headlineCount];
2131 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2132 # reset when we enter a new level
2133 $sublevelCount[$level] = 0;
2134 $toc .= $sk->tocIndent( $level - $prevlevel );
2135 $toclevel += $level - $prevlevel;
2137 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2138 # reset when we step back a level
2139 $sublevelCount[$level+1]=0;
2140 $toc .= $sk->tocUnindent( $prevlevel - $level );
2141 $toclevel -= $prevlevel - $level;
2143 # count number of headlines for each level
2144 @$sublevelCount[$level]++;
2145 if( $doNumberHeadings || $doShowToc ) {
2146 $dot = 0;
2147 for( $i = 1; $i <= $level; $i++ ) {
2148 if( !empty( $sublevelCount[$i] ) ) {
2149 if( $dot ) {
2150 $numbering .= '.';
2152 $numbering .= $sublevelCount[$i];
2153 $dot = 1;
2158 # The canonized header is a version of the header text safe to use for links
2159 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2160 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2161 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2163 # strip out HTML
2164 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2165 $tocline = trim( $canonized_headline );
2166 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2167 $replacearray = array(
2168 '%3A' => ':',
2169 '%' => '.'
2171 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2172 $refer[$headlineCount] = $canonized_headline;
2174 # count how many in assoc. array so we can track dupes in anchors
2175 @$refers[$canonized_headline]++;
2176 $refcount[$headlineCount]=$refers[$canonized_headline];
2178 # Prepend the number to the heading text
2180 if( $doNumberHeadings || $doShowToc ) {
2181 $tocline = $numbering . ' ' . $tocline;
2183 # Don't number the heading if it is the only one (looks silly)
2184 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2185 # the two are different if the line contains a link
2186 $headline=$numbering . ' ' . $headline;
2190 # Create the anchor for linking from the TOC to the section
2191 $anchor = $canonized_headline;
2192 if($refcount[$headlineCount] > 1 ) {
2193 $anchor .= '_' . $refcount[$headlineCount];
2195 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2196 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2198 if( $showEditLink ) {
2199 if ( empty( $head[$headlineCount] ) ) {
2200 $head[$headlineCount] = '';
2202 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2205 # Add the edit section span
2206 if( $rightClickHack ) {
2207 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2210 # give headline the correct <h#> tag
2211 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2213 $headlineCount++;
2216 if( $doShowToc ) {
2217 $toclines = $headlineCount;
2218 $toc .= $sk->tocUnindent( $toclevel );
2219 $toc = $sk->tocTable( $toc );
2222 # split up and insert constructed headlines
2224 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2225 $i = 0;
2227 foreach( $blocks as $block ) {
2228 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2229 # This is the [edit] link that appears for the top block of text when
2230 # section editing is enabled
2232 # Disabled because it broke block formatting
2233 # For example, a bullet point in the top line
2234 # $full .= $sk->editSectionLink(0);
2236 $full .= $block;
2237 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2238 # Top anchor now in skin
2239 $full = $full.$toc;
2242 if( !empty( $head[$i] ) ) {
2243 $full .= $head[$i];
2245 $i++;
2247 if($forceTocHere) {
2248 $mw =& MagicWord::get( MAG_TOC );
2249 return $mw->replace( $toc, $full );
2250 } else {
2251 return $full;
2255 # Return an HTML link for the "ISBN 123456" text
2256 /* private */ function magicISBN( $text ) {
2257 global $wgLang;
2258 $fname = 'Parser::magicISBN';
2259 wfProfileIn( $fname );
2261 $a = split( 'ISBN ', " $text" );
2262 if ( count ( $a ) < 2 ) {
2263 wfProfileOut( $fname );
2264 return $text;
2266 $text = substr( array_shift( $a ), 1);
2267 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2269 foreach ( $a as $x ) {
2270 $isbn = $blank = '' ;
2271 while ( ' ' == $x{0} ) {
2272 $blank .= ' ';
2273 $x = substr( $x, 1 );
2275 while ( strstr( $valid, $x{0} ) != false ) {
2276 $isbn .= $x{0};
2277 $x = substr( $x, 1 );
2279 $num = str_replace( '-', '', $isbn );
2280 $num = str_replace( ' ', '', $num );
2282 if ( '' == $num ) {
2283 $text .= "ISBN $blank$x";
2284 } else {
2285 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2286 $text .= '<a href="' .
2287 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2288 "\" class=\"internal\">ISBN $isbn</a>";
2289 $text .= $x;
2292 wfProfileOut( $fname );
2293 return $text;
2296 # Return an HTML link for the "GEO ..." text
2297 /* private */ function magicGEO( $text ) {
2298 global $wgLang, $wgUseGeoMode;
2299 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2300 $fname = 'Parser::magicGEO';
2301 wfProfileIn( $fname );
2303 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2304 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2305 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2306 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2307 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2308 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2310 $a = split( 'GEO ', " $text" );
2311 if ( count ( $a ) < 2 ) {
2312 wfProfileOut( $fname );
2313 return $text;
2315 $text = substr( array_shift( $a ), 1);
2316 $valid = '0123456789.+-:';
2318 foreach ( $a as $x ) {
2319 $geo = $blank = '' ;
2320 while ( ' ' == $x{0} ) {
2321 $blank .= ' ';
2322 $x = substr( $x, 1 );
2324 while ( strstr( $valid, $x{0} ) != false ) {
2325 $geo .= $x{0};
2326 $x = substr( $x, 1 );
2328 $num = str_replace( '+', '', $geo );
2329 $num = str_replace( ' ', '', $num );
2331 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2332 $text .= "GEO $blank$x";
2333 } else {
2334 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2335 $text .= '<a href="' .
2336 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2337 "\" class=\"internal\">GEO $geo</a>";
2338 $text .= $x;
2341 wfProfileOut( $fname );
2342 return $text;
2345 # Return an HTML link for the "RFC 1234" text
2346 /* private */ function magicRFC( $text ) {
2347 global $wgLang;
2349 $a = split( 'RFC ', ' '.$text );
2350 if ( count ( $a ) < 2 ) return $text;
2351 $text = substr( array_shift( $a ), 1);
2352 $valid = '0123456789';
2354 foreach ( $a as $x ) {
2355 $rfc = $blank = '' ;
2356 while ( ' ' == $x{0} ) {
2357 $blank .= ' ';
2358 $x = substr( $x, 1 );
2360 while ( strstr( $valid, $x{0} ) != false ) {
2361 $rfc .= $x{0};
2362 $x = substr( $x, 1 );
2365 if ( '' == $rfc ) {
2366 $text .= "RFC $blank$x";
2367 } else {
2368 $url = wfmsg( 'rfcurl' );
2369 $url = str_replace( '$1', $rfc, $url);
2370 $sk =& $this->mOptions->getSkin();
2371 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2372 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2375 return $text;
2378 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2379 $this->mOptions = $options;
2380 $this->mTitle =& $title;
2381 $this->mOutputType = OT_WIKI;
2383 if ( $clearState ) {
2384 $this->clearState();
2387 $stripState = false;
2388 $pairs = array(
2389 "\r\n" => "\n",
2391 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2392 // now with regexes
2394 $pairs = array(
2395 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2396 "/<br *?>/i" => "<br />",
2398 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2400 $text = $this->strip( $text, $stripState, false );
2401 $text = $this->pstPass2( $text, $user );
2402 $text = $this->unstrip( $text, $stripState );
2403 $text = $this->unstripNoWiki( $text, $stripState );
2404 return $text;
2407 /* private */ function pstPass2( $text, &$user ) {
2408 global $wgLang, $wgLocaltimezone, $wgCurParser;
2410 # Variable replacement
2411 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2412 $text = $this->replaceVariables( $text );
2414 # Signatures
2416 $n = $user->getName();
2417 $k = $user->getOption( 'nickname' );
2418 if ( '' == $k ) { $k = $n; }
2419 if(isset($wgLocaltimezone)) {
2420 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2422 /* Note: this is an ugly timezone hack for the European wikis */
2423 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2424 ' (' . date( 'T' ) . ')';
2425 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2427 $text = preg_replace( '/~~~~~/', $d, $text );
2428 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText(
2429 Namespace::getUser() ) . ":$n|$k]] $d", $text );
2430 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText(
2431 Namespace::getUser() ) . ":$n|$k]]", $text );
2433 # Context links: [[|name]] and [[name (context)|]]
2435 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2436 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2437 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2438 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2440 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2441 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2442 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2443 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2444 # [[ns:page (cont)|]]
2445 $context = "";
2446 $t = $this->mTitle->getText();
2447 if ( preg_match( $conpat, $t, $m ) ) {
2448 $context = $m[2];
2450 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2451 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2452 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2454 if ( '' == $context ) {
2455 $text = preg_replace( $p2, '[[\\1]]', $text );
2456 } else {
2457 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2461 $mw =& MagicWord::get( MAG_SUBST );
2462 $wgCurParser = $this->fork();
2463 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2464 $this->merge( $wgCurParser );
2467 # Trim trailing whitespace
2468 # MAG_END (__END__) tag allows for trailing
2469 # whitespace to be deliberately included
2470 $text = rtrim( $text );
2471 $mw =& MagicWord::get( MAG_END );
2472 $mw->matchAndRemove( $text );
2474 return $text;
2477 # Set up some variables which are usually set up in parse()
2478 # so that an external function can call some class members with confidence
2479 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2480 $this->mTitle =& $title;
2481 $this->mOptions = $options;
2482 $this->mOutputType = $outputType;
2483 if ( $clearState ) {
2484 $this->clearState();
2488 function transformMsg( $text, $options ) {
2489 global $wgTitle;
2490 static $executing = false;
2492 # Guard against infinite recursion
2493 if ( $executing ) {
2494 return $text;
2496 $executing = true;
2498 $this->mTitle = $wgTitle;
2499 $this->mOptions = $options;
2500 $this->mOutputType = OT_MSG;
2501 $this->clearState();
2502 $text = $this->replaceVariables( $text );
2504 $executing = false;
2505 return $text;
2508 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2509 # Callback will be called with the text within
2510 # Transform and return the text within
2511 function setHook( $tag, $callback ) {
2512 $oldVal = @$this->mTagHooks[$tag];
2513 $this->mTagHooks[$tag] = $callback;
2514 return $oldVal;
2518 class ParserOutput
2520 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2521 var $mCacheTime; # Used in ParserCache
2523 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2524 $containsOldMagic = false )
2526 $this->mText = $text;
2527 $this->mLanguageLinks = $languageLinks;
2528 $this->mCategoryLinks = $categoryLinks;
2529 $this->mContainsOldMagic = $containsOldMagic;
2530 $this->mCacheTime = "";
2533 function getText() { return $this->mText; }
2534 function getLanguageLinks() { return $this->mLanguageLinks; }
2535 function getCategoryLinks() { return $this->mCategoryLinks; }
2536 function getCacheTime() { return $this->mCacheTime; }
2537 function containsOldMagic() { return $this->mContainsOldMagic; }
2538 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2539 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2540 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2541 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2542 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2544 function merge( $other ) {
2545 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2546 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2547 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2552 class ParserOptions
2554 # All variables are private
2555 var $mUseTeX; # Use texvc to expand <math> tags
2556 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2557 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2558 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2559 var $mAllowExternalImages; # Allow external images inline
2560 var $mSkin; # Reference to the preferred skin
2561 var $mDateFormat; # Date format index
2562 var $mEditSection; # Create "edit section" links
2563 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2564 var $mNumberHeadings; # Automatically number headings
2565 var $mShowToc; # Show table of contents
2567 function getUseTeX() { return $this->mUseTeX; }
2568 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2569 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2570 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2571 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2572 function getSkin() { return $this->mSkin; }
2573 function getDateFormat() { return $this->mDateFormat; }
2574 function getEditSection() { return $this->mEditSection; }
2575 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2576 function getNumberHeadings() { return $this->mNumberHeadings; }
2577 function getShowToc() { return $this->mShowToc; }
2579 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2580 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2581 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2582 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2583 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2584 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2585 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2586 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2587 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2588 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2590 function setSkin( &$x ) { $this->mSkin =& $x; }
2592 /* static */ function newFromUser( &$user ) {
2593 $popts = new ParserOptions;
2594 $popts->initialiseFromUser( $user );
2595 return $popts;
2598 function initialiseFromUser( &$userInput ) {
2599 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2601 if ( !$userInput ) {
2602 $user = new User;
2603 $user->setLoaded( true );
2604 } else {
2605 $user =& $userInput;
2608 $this->mUseTeX = $wgUseTeX;
2609 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2610 $this->mUseDynamicDates = $wgUseDynamicDates;
2611 $this->mInterwikiMagic = $wgInterwikiMagic;
2612 $this->mAllowExternalImages = $wgAllowExternalImages;
2613 $this->mSkin =& $user->getSkin();
2614 $this->mDateFormat = $user->getOption( 'date' );
2615 $this->mEditSection = $user->getOption( 'editsection' );
2616 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2617 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2618 $this->mShowToc = $user->getOption( 'showtoc' );
2624 # Regex callbacks, used in Parser::replaceVariables
2625 function wfBraceSubstitution( $matches )
2627 global $wgCurParser;
2628 return $wgCurParser->braceSubstitution( $matches );
2631 function wfArgSubstitution( $matches )
2633 global $wgCurParser;
2634 return $wgCurParser->argSubstitution( $matches );
2637 function wfVariableSubstitution( $matches )
2639 global $wgCurParser;
2640 return $wgCurParser->variableSubstitution( $matches );