3 // require_once('Tokenizer.php');
7 # Processes wiki markup
9 # There are two main entry points into the Parser class:
11 # produces HTML output
13 # produces altered wiki markup.
16 # objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
18 # NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
21 # $wgUseTex*, $wgUseCategoryMagic*, $wgUseDynamicDates*, $wgInterwikiMagic*,
22 # $wgNamespacesWithSubpages, $wgLanguageCode, $wgAllowExternalImages*,
25 # * only within ParserOptions
27 #----------------------------------------
28 # Variable substitution O(N^2) attack
29 #-----------------------------------------
30 # Without countermeasures, it would be possible to attack the parser by saving
31 # a page filled with a large number of inclusions of large pages. The size of
32 # the generated page would be proportional to the square of the input size.
33 # Hence, we limit the number of inclusions of any given page, thus bringing any
34 # attack back to O(N).
35 define( "MAX_INCLUDE_REPEAT", 100 );
36 define( "MAX_INCLUDE_SIZE", 1000000 ); // 1 Million
38 # Allowed values for $mOutputType
39 define( "OT_HTML", 1 );
40 define( "OT_WIKI", 2 );
41 define( "OT_MSG" , 3 );
43 # string parameter for extractTags which will cause it
44 # to strip HTML comments in addition to regular
45 # <XML>-style tags. This should not be anything we
46 # may want to use in wikisyntax
47 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
49 # prefix for escaping, used in two functions at least
50 define( 'UNIQ_PREFIX', 'NaodW29');
52 # Constants needed for external link processing
53 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
54 define( 'HTTP_PROTOCOLS', 'http|https' );
55 # Everything except bracket, space, or control characters
56 define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
57 define( 'INVERSE_EXT_LINK_URL_CLASS', '[\]\\x00-\\x20\\x7F]' );
59 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
60 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
61 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
62 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS
.'):'.EXT_LINK_URL_CLASS
.'+) *('.EXT_LINK_TEXT_CLASS
.'*?)\]/S' );
63 define( 'EXT_IMAGE_REGEX',
64 '/^('.HTTP_PROTOCOLS
.':)'. # Protocol
65 '('.EXT_LINK_URL_CLASS
.'+)\\/'. # Hostname and path
66 '('.EXT_IMAGE_FNAME_CLASS
.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS
.')$/S' # Filename
74 # Cleared with clearState():
75 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
76 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
79 var $mOptions, $mTitle, $mOutputType,
80 $mTemplates, // cache of already loaded templates, avoids
81 // multiple SQL queries for the same string
82 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
83 // in this path. Used for loop detection.
86 $this->mTemplates
= array();
87 $this->mTemplatePath
= array();
88 $this->mTagHooks
= array();
92 function clearState() {
93 $this->mOutput
= new ParserOutput
;
94 $this->mAutonumber
= 0;
95 $this->mLastSection
= "";
96 $this->mDTopen
= false;
97 $this->mVariables
= false;
98 $this->mIncludeCount
= array();
99 $this->mStripState
= array();
100 $this->mArgStack
= array();
101 $this->mInPre
= false;
104 # First pass--just handle <nowiki> sections, pass the rest off
105 # to internalParse() which does all the real work.
107 # Returns a ParserOutput
109 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
111 $fname = "Parser::parse";
112 wfProfileIn( $fname );
118 $this->mOptions
= $options;
119 $this->mTitle
=& $title;
120 $this->mOutputType
= OT_HTML
;
123 $text = $this->strip( $text, $this->mStripState
);
124 $text = $this->internalParse( $text, $linestart );
125 $text = $this->unstrip( $text, $this->mStripState
);
126 # Clean up special characters, only run once, next-to-last before doBlockLevels
129 # french spaces, last one Guillemet-left
130 # only if there is something before the space
131 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1 \\2',
132 # french spaces, Guillemet-right
133 "/(\\302\\253) /i"=>"\\1 ",
134 '/<hr *>/i' => '<hr />',
135 '/<br *>/i' => '<br />',
136 '/<center *>/i' => '<div class="center">',
137 '/<\\/center *>/i' => '</div>',
138 # Clean up spare ampersands; note that we probably ought to be
139 # more careful about named entities.
140 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&'
142 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
145 # french spaces, last one Guillemet-left
146 '/ (\\?|:|;|!|\\302\\273)/i' => ' \\1',
147 # french spaces, Guillemet-right
148 '/(\\302\\253) /i' => '\\1 ',
149 '/([^> ]+(0(1|3|9);)[^< ]*)/i' => '<span class="diacrit">\\1</span>',
150 '/<center *>/i' => '<div class="center">',
151 '/<\\/center *>/i' => '</div>'
153 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
156 $text = $this->doBlockLevels( $text, $linestart );
157 $text = $this->unstripNoWiki( $text, $this->mStripState
);
159 $text = $this->tidy($text);
161 $this->mOutput
->setText( $text );
162 wfProfileOut( $fname );
163 return $this->mOutput
;
166 /* static */ function getRandomString() {
167 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
170 # Replaces all occurrences of <$tag>content</$tag> in the text
171 # with a random marker and returns the new text. the output parameter
172 # $content will be an associative array filled with data on the form
173 # $unique_marker => content.
175 # If $content is already set, the additional entries will be appended
177 # If $tag is set to STRIP_COMMENTS, the function will extract
178 # <!-- HTML comments -->
180 /* static */ function extractTags($tag, $text, &$content, $uniq_prefix = ""){
181 $rnd = $uniq_prefix . '-' . $tag . Parser
::getRandomString();
188 while ( '' != $text ) {
189 if($tag==STRIP_COMMENTS
) {
190 $p = preg_split( '/<!--/i', $text, 2 );
192 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
195 if ( ( count( $p ) < 2 ) ||
( '' == $p[1] ) ) {
198 if($tag==STRIP_COMMENTS
) {
199 $q = preg_split( '/-->/i', $p[1], 2 );
201 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
203 $marker = $rnd . sprintf('%08X', $n++
);
204 $content[$marker] = $q[0];
205 $stripped .= $marker;
212 # Strips and renders <nowiki>, <pre>, <math>, <hiero>
213 # If $render is set, performs necessary rendering operations on plugins
214 # Returns the text, and fills an array with data needed in unstrip()
215 # If the $state is already a valid strip state, it adds to the state
217 # When $stripcomments is set, HTML comments <!-- like this -->
218 # will be stripped in addition to other tags. This is important
219 # for section editing, where these comments cause confusion when
220 # counting the sections in the wikisource
221 function strip( $text, &$state, $stripcomments = false ) {
222 $render = ($this->mOutputType
== OT_HTML
);
223 $html_content = array();
224 $nowiki_content = array();
225 $math_content = array();
226 $pre_content = array();
227 $comment_content = array();
228 $ext_content = array();
230 # Replace any instances of the placeholders
231 $uniq_prefix = UNIQ_PREFIX
;
232 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
237 $text = Parser
::extractTags('html', $text, $html_content, $uniq_prefix);
238 foreach( $html_content as $marker => $content ) {
240 # Raw and unchecked for validity.
241 $html_content[$marker] = $content;
243 $html_content[$marker] = "<html>$content</html>";
249 $text = Parser
::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
250 foreach( $nowiki_content as $marker => $content ) {
252 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
254 $nowiki_content[$marker] = "<nowiki>$content</nowiki>";
259 $text = Parser
::extractTags('math', $text, $math_content, $uniq_prefix);
260 foreach( $math_content as $marker => $content ){
262 if( $this->mOptions
->getUseTeX() ) {
263 $math_content[$marker] = renderMath( $content );
265 $math_content[$marker] = "<math>$content<math>";
268 $math_content[$marker] = "<math>$content</math>";
273 $text = Parser
::extractTags('pre', $text, $pre_content, $uniq_prefix);
274 foreach( $pre_content as $marker => $content ){
276 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
278 $pre_content[$marker] = "<pre>$content</pre>";
284 $text = Parser
::extractTags(STRIP_COMMENTS
, $text, $comment_content, $uniq_prefix);
285 foreach( $comment_content as $marker => $content ){
286 $comment_content[$marker] = "<!--$content-->";
291 foreach ( $this->mTagHooks
as $tag => $callback ) {
292 $ext_contents[$tag] = array();
293 $text = Parser
::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
294 foreach( $ext_content[$tag] as $marker => $content ) {
296 $ext_content[$tag][$marker] = $callback( $content );
298 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
303 # Merge state with the pre-existing state, if there is one
305 $state['html'] = $state['html'] +
$html_content;
306 $state['nowiki'] = $state['nowiki'] +
$nowiki_content;
307 $state['math'] = $state['math'] +
$math_content;
308 $state['pre'] = $state['pre'] +
$pre_content;
309 $state['comment'] = $state['comment'] +
$comment_content;
311 foreach( $ext_content as $tag => $array ) {
312 if ( array_key_exists( $tag, $state ) ) {
313 $state[$tag] = $state[$tag] +
$array;
318 'html' => $html_content,
319 'nowiki' => $nowiki_content,
320 'math' => $math_content,
321 'pre' => $pre_content,
322 'comment' => $comment_content,
328 # always call unstripNoWiki() after this one
329 function unstrip( $text, &$state ) {
330 # Must expand in reverse order, otherwise nested tags will be corrupted
331 $contentDict = end( $state );
332 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
333 if( key($state) != 'nowiki' && key($state) != 'html') {
334 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
335 $text = str_replace( key( $contentDict ), $content, $text );
342 # always call this after unstrip() to preserve the order
343 function unstripNoWiki( $text, &$state ) {
344 # Must expand in reverse order, otherwise nested tags will be corrupted
345 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
346 $text = str_replace( key( $state['nowiki'] ), $content, $text );
351 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
352 $text = str_replace( key( $state['html'] ), $content, $text );
359 # Add an item to the strip state
360 # Returns the unique tag which must be inserted into the stripped text
361 # The tag will be replaced with the original text in unstrip()
362 function insertStripItem( $text, &$state ) {
363 $rnd = UNIQ_PREFIX
. '-item' . Parser
::getRandomString();
372 $state['item'][$rnd] = $text;
376 # Return allowed HTML attributes
377 function getHTMLattrs () {
378 $htmlattrs = array( # Allowed attributes--no scripting, etc.
379 'title', 'align', 'lang', 'dir', 'width', 'height',
380 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
381 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
382 /* FONT */ 'type', 'start', 'value', 'compact',
383 /* For various lists, mostly deprecated but safe */
384 'summary', 'width', 'border', 'frame', 'rules',
385 'cellspacing', 'cellpadding', 'valign', 'char',
386 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
387 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
388 'id', 'class', 'name', 'style' /* For CSS */
393 # Remove non approved attributes and javascript in css
394 function fixTagAttributes ( $t ) {
395 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
396 $htmlattrs = $this->getHTMLattrs() ;
398 # Strip non-approved attributes from the tag
400 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
401 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
404 $t = str_replace ( "<></>" , "" , $t ) ; # This should fix bug 980557
406 # Strip javascript "expression" from stylesheets. Brute force approach:
407 # If anythin offensive is found, all attributes of the HTML tag are dropped
410 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
411 wfMungeToUtf8( $t ) ) )
419 # interface with html tidy, used if $wgUseTidy = true
420 function tidy ( $text ) {
421 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
422 global $wgInputEncoding, $wgOutputEncoding;
423 $fname = 'Parser::tidy';
424 wfProfileIn( $fname );
427 switch(strtoupper($wgOutputEncoding)) {
429 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -latin1':' -raw';
432 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)?
' -utf8':' -raw';
435 $wgTidyOpts .= ' -raw';
438 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
439 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
440 '<head><title>test</title></head><body>'.$text.'</body></html>';
441 $descriptorspec = array(
442 0 => array('pipe', 'r'),
443 1 => array('pipe', 'w'),
444 2 => array('file', '/dev/null', 'a')
446 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
447 if (is_resource($process)) {
448 fwrite($pipes[0], $wrappedtext);
450 while (!feof($pipes[1])) {
451 $cleansource .= fgets($pipes[1], 1024);
454 $return_value = proc_close($process);
457 wfProfileOut( $fname );
459 if( $cleansource == '' && $text != '') {
460 wfDebug( "Tidy error detected!\n" );
461 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
467 # parse the wiki syntax used to render tables
468 function doTableStuff ( $t ) {
469 $fname = 'Parser::doTableStuff';
470 wfProfileIn( $fname );
472 $t = explode ( "\n" , $t ) ;
473 $td = array () ; # Is currently a td tag open?
474 $ltd = array () ; # Was it TD or TH?
475 $tr = array () ; # Is currently a tr tag open?
476 $ltr = array () ; # tr attributes
477 $indent_level = 0; # indent level of the table
478 foreach ( $t AS $k => $x )
481 $fc = substr ( $x , 0 , 1 ) ;
482 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
483 $indent_level = strlen( $matches[1] );
485 str_repeat( "<dl><dd>", $indent_level ) .
486 "<table " . $this->fixTagAttributes ( $matches[2] ) . '>' ;
487 array_push ( $td , false ) ;
488 array_push ( $ltd , '' ) ;
489 array_push ( $tr , false ) ;
490 array_push ( $ltr , '' ) ;
492 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
493 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
495 $l = array_pop ( $ltd ) ;
496 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
497 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
499 $t[$k] = $z . str_repeat( "</dd></dl>", $indent_level );
501 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
502 $x = substr ( $x , 1 ) ;
503 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
505 $l = array_pop ( $ltd ) ;
506 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
507 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
510 array_push ( $tr , false ) ;
511 array_push ( $td , false ) ;
512 array_push ( $ltd , '' ) ;
513 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
515 else if ( '|' == $fc ||
'!' == $fc ||
'|+' == substr ( $x , 0 , 2 ) ) { # Caption
516 if ( '|+' == substr ( $x , 0 , 2 ) ) {
518 $x = substr ( $x , 1 ) ;
520 $after = substr ( $x , 1 ) ;
521 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
522 $after = explode ( '||' , $after ) ;
524 foreach ( $after AS $theline )
529 $tra = array_pop ( $ltr ) ;
530 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
531 array_push ( $tr , true ) ;
532 array_push ( $ltr , '' ) ;
535 $l = array_pop ( $ltd ) ;
536 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
537 if ( $fc == '|' ) $l = 'td' ;
538 else if ( $fc == '!' ) $l = 'th' ;
539 else if ( $fc == '+' ) $l = 'caption' ;
541 array_push ( $ltd , $l ) ;
542 $y = explode ( '|' , $theline , 2 ) ;
543 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
544 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
546 array_push ( $td , true ) ;
551 # Closing open td, tr && table
552 while ( count ( $td ) > 0 )
554 if ( array_pop ( $td ) ) $t[] = '</td>' ;
555 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
559 $t = implode ( "\n" , $t ) ;
560 # $t = $this->removeHTMLtags( $t );
561 wfProfileOut( $fname );
565 # Parses the text and adds the result to the strip state
566 # Returns the strip tag
567 function stripParse( $text, $newline, $args ) {
568 $text = $this->strip( $text, $this->mStripState
);
569 $text = $this->internalParse( $text, (bool)$newline, $args, false );
570 return $newline.$this->insertStripItem( $text, $this->mStripState
);
573 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
574 $fname = 'Parser::internalParse';
575 wfProfileIn( $fname );
577 $text = $this->removeHTMLtags( $text );
578 $text = $this->replaceVariables( $text, $args );
580 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
582 $text = $this->doHeadings( $text );
583 if($this->mOptions
->getUseDynamicDates()) {
584 global $wgDateFormatter;
585 $text = $wgDateFormatter->reformat( $this->mOptions
->getDateFormat(), $text );
587 $text = $this->doAllQuotes( $text );
588 $text = $this->replaceExternalLinks( $text );
589 $text = $this->doMagicLinks( $text );
590 $text = $this->replaceInternalLinks ( $text );
591 $text = $this->replaceInternalLinks ( $text );
593 $text = $this->unstrip( $text, $this->mStripState
);
594 $text = $this->unstripNoWiki( $text, $this->mStripState
);
596 $text = $this->doTableStuff( $text );
597 $text = $this->formatHeadings( $text, $isMain );
598 $sk =& $this->mOptions
->getSkin();
599 $text = $sk->transformContent( $text );
601 // if ( $isMain && !isset ( $this->categoryMagicDone ) ) {
602 // $text .= $this->categoryMagic () ;
603 // $this->categoryMagicDone = true ;
606 wfProfileOut( $fname );
610 /* private */ function &doMagicLinks( &$text ) {
611 global $wgUseGeoMode;
612 $text = $this->magicISBN( $text );
613 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
614 $text = $this->magicGEO( $text );
616 $text = $this->magicRFC( $text );
620 # Parse ^^ tokens and return html
621 /* private */ function doExponent ( $text ) {
622 $fname = 'Parser::doExponent';
623 wfProfileIn( $fname);
624 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
625 wfProfileOut( $fname);
629 # Parse headers and return html
630 /* private */ function doHeadings( $text ) {
631 $fname = 'Parser::doHeadings';
632 wfProfileIn( $fname );
633 for ( $i = 6; $i >= 1; --$i ) {
634 $h = substr( '======', 0, $i );
635 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
636 "<h{$i}>\\1</h{$i}>\\2", $text );
638 wfProfileOut( $fname );
642 /* private */ function doAllQuotes( $text ) {
643 $fname = 'Parser::doAllQuotes';
644 wfProfileIn( $fname );
646 $lines = explode( "\n", $text );
647 foreach ( $lines as $line ) {
648 $outtext .= $this->doQuotes ( $line ) . "\n";
650 $outtext = substr($outtext, 0,-1);
651 wfProfileOut( $fname );
655 /* private */ function doQuotes( $text ) {
656 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
657 if (count ($arr) == 1)
661 # First, do some preliminary work. This may shift some apostrophes from
662 # being mark-up to being text. It also counts the number of occurrences
663 # of bold and italics mark-ups.
671 # If there are ever four apostrophes, assume the first is supposed to
672 # be text, and the remaining three constitute mark-up for bold text.
673 if (strlen ($arr[$i]) == 4)
678 # If there are more than 5 apostrophes in a row, assume they're all
679 # text except for the last 5.
680 else if (strlen ($arr[$i]) > 5)
682 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
685 # Count the number of occurrences of bold and italics mark-ups.
686 # We are not counting sequences of five apostrophes.
687 if (strlen ($arr[$i]) == 2) $numitalics++
; else
688 if (strlen ($arr[$i]) == 3) $numbold++
; else
689 if (strlen ($arr[$i]) == 5) { $numitalics++
; $numbold++
; }
694 # If there is an odd number of both bold and italics, it is likely
695 # that one of the bold ones was meant to be an apostrophe followed
696 # by italics. Which one we cannot know for certain, but it is more
697 # likely to be one that has a single-letter word before it.
698 if (($numbold %
2 == 1) && ($numitalics %
2 == 1))
701 $firstsingleletterword = -1;
702 $firstmultiletterword = -1;
706 if (($i %
2 == 1) and (strlen ($r) == 3))
708 $x1 = substr ($arr[$i-1], -1);
709 $x2 = substr ($arr[$i-1], -2, 1);
711 if ($firstspace == -1) $firstspace = $i;
712 } else if ($x2 == " ") {
713 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
715 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
721 # If there is a single-letter word, use it!
722 if ($firstsingleletterword > -1)
724 $arr [ $firstsingleletterword ] = "''";
725 $arr [ $firstsingleletterword-1 ] .= "'";
727 # If not, but there's a multi-letter word, use that one.
728 else if ($firstmultiletterword > -1)
730 $arr [ $firstmultiletterword ] = "''";
731 $arr [ $firstmultiletterword-1 ] .= "'";
733 # ... otherwise use the first one that has neither.
734 # (notice that it is possible for all three to be -1 if, for example,
735 # there is only one pentuple-apostrophe in the line)
736 else if ($firstspace > -1)
738 $arr [ $firstspace ] = "''";
739 $arr [ $firstspace-1 ] .= "'";
743 # Now let's actually convert our apostrophic mush to HTML!
752 if ($state == 'both')
759 if (strlen ($r) == 2)
762 { $output .= "</em>"; $state = ''; }
763 else if ($state == 'strongem')
764 { $output .= "</em>"; $state = 'strong'; }
765 else if ($state == 'emstrong')
766 { $output .= "</strong></em><strong>"; $state = 'strong'; }
767 else if ($state == 'both')
768 { $output .= "<strong><em>{$buffer}</em>"; $state = 'strong'; }
769 else # $state can be 'strong' or ''
770 { $output .= "<em>"; $state .= 'em'; }
772 else if (strlen ($r) == 3)
774 if ($state == 'strong')
775 { $output .= "</strong>"; $state = ''; }
776 else if ($state == 'strongem')
777 { $output .= "</em></strong><em>"; $state = 'em'; }
778 else if ($state == 'emstrong')
779 { $output .= "</strong>"; $state = 'em'; }
780 else if ($state == 'both')
781 { $output .= "<em><strong>{$buffer}</strong>"; $state = 'em'; }
782 else # $state can be 'em' or ''
783 { $output .= "<strong>"; $state .= 'strong'; }
785 else if (strlen ($r) == 5)
787 if ($state == 'strong')
788 { $output .= "</strong><em>"; $state = 'em'; }
789 else if ($state == 'em')
790 { $output .= "</em><strong>"; $state = 'strong'; }
791 else if ($state == 'strongem')
792 { $output .= "</em></strong>"; $state = ''; }
793 else if ($state == 'emstrong')
794 { $output .= "</strong></em>"; $state = ''; }
795 else if ($state == 'both')
796 { $output .= "<em><strong>{$buffer}</strong></em>"; $state = ''; }
797 else # ($state == '')
798 { $buffer = ''; $state = 'both'; }
803 # Now close all remaining tags. Notice that the order is important.
804 if ($state == 'strong' ||
$state == 'emstrong')
805 $output .= '</strong>';
806 if ($state == 'em' ||
$state == 'strongem' ||
$state == 'emstrong')
808 if ($state == 'strongem')
809 $output .= '</strong>';
810 if ($state == 'both')
811 $output .= "<strong><em>{$buffer}</em></strong>";
816 # Note: we have to do external links before the internal ones,
817 # and otherwise take great care in the order of things here, so
818 # that we don't end up interpreting some URLs twice.
820 /* private */ function replaceExternalLinks( $text ) {
821 $fname = 'Parser::replaceExternalLinks';
822 wfProfileIn( $fname );
824 $sk =& $this->mOptions
->getSkin();
825 $linktrail = wfMsg('linktrail');
826 $bits = preg_split( EXT_LINK_BRACKETED
, $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
828 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
831 while ( $i<count( $bits ) ) {
833 $protocol = $bits[$i++
];
835 $trail = $bits[$i++
];
837 # If the link text is an image URL, replace it with an <img> tag
838 # This happened by accident in the original parser, but some people used it extensively
839 $img = $this->maybeMakeImageLink( $text );
840 if ( $img !== false ) {
846 # No link text, e.g. [http://domain.tld/some.link]
848 # Autonumber if allowed
849 if ( strpos( HTTP_PROTOCOLS
, $protocol ) !== false ) {
850 $text = "[" . ++
$this->mAutonumber
. "]";
852 # Otherwise just use the URL
853 $text = htmlspecialchars( $url );
856 # Have link text, e.g. [http://domain.tld/some.link text]s
858 if ( preg_match( $linktrail, $trail, $m2 ) ) {
864 $encUrl = htmlspecialchars( $url );
865 # Bit in parentheses showing the URL for the printable version
866 if( $url == $text ||
preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $url ) ) {
869 # Expand the URL for printable version
870 if ( ! $sk->suppressUrlExpansion() ) {
871 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
877 # Process the trail (i.e. everything after this link up until start of the next link),
878 # replacing any non-bracketed links
879 $trail = $this->replaceFreeExternalLinks( $trail );
881 $la = $sk->getExternalLinkAttributes( $url, $text );
883 # Use the encoded URL
884 # This means that users can paste URLs directly into the text
885 # Funny characters like ö aren't valid in URLs anyway
886 # This was changed in August 2004
887 $s .= "<a href=\"{$url}\" {$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
890 wfProfileOut( $fname );
894 # Replace anything that looks like a URL with a link
895 function replaceFreeExternalLinks( $text ) {
896 $bits = preg_split( '/((?:'.URL_PROTOCOLS
.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE
);
897 $s = array_shift( $bits );
900 $sk =& $this->mOptions
->getSkin();
902 while ( $i < count( $bits ) ){
903 $protocol = $bits[$i++
];
904 $remainder = $bits[$i++
];
906 if ( preg_match( '/^('.EXT_LINK_URL_CLASS
.'+)(.*)$/s', $remainder, $m ) ) {
907 # Found some characters after the protocol that look promising
908 $url = $protocol . $m[1];
911 # Move trailing punctuation to $trail
913 # If there is no left bracket, then consider right brackets fair game too
914 if ( strpos( $url, '(' ) === false ) {
918 $numSepChars = strspn( strrev( $url ), $sep );
919 if ( $numSepChars ) {
920 $trail = substr( $url, -$numSepChars ) . $trail;
921 $url = substr( $url, 0, -$numSepChars );
924 # Replace & from obsolete syntax with &
925 $url = str_replace( '&', '&', $url );
927 # Is this an external image?
928 $text = $this->maybeMakeImageLink( $url );
929 if ( $text === false ) {
930 # Not an image, make a link
931 $text = $sk->makeExternalLink( $url, $url );
933 $s .= $text . $trail;
935 $s .= $protocol . $remainder;
941 # make an image if it's allowed
942 function maybeMakeImageLink( $url ) {
943 $sk =& $this->mOptions
->getSkin();
945 if ( $this->mOptions
->getAllowExternalImages() ) {
946 if ( preg_match( EXT_IMAGE_REGEX
, $url ) ) {
948 $text = $sk->makeImage( htmlspecialchars( $url ) );
954 # The wikilinks [[ ]] are procedeed here.
955 /* private */ function replaceInternalLinks( $s ) {
956 global $wgLang, $wgLinkCache;
957 global $wgNamespacesWithSubpages, $wgLanguageCode;
958 static $fname = 'Parser::replaceInternalLinks' ;
959 wfProfileIn( $fname );
961 wfProfileIn( $fname.'-setup' );
963 # the % is needed to support urlencoded titles as well
964 if ( !$tc ) { $tc = Title
::legalChars() . '#%'; }
965 $sk =& $this->mOptions
->getSkin();
967 $redirect = MagicWord
::get ( MAG_REDIRECT
) ;
969 $a = explode( '[[', ' ' . $s );
970 $s = array_shift( $a );
971 $s = substr( $s, 1 );
973 # Match a link having the form [[namespace:link|alternate]]trail
975 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
976 # Match the end of a line for a word that's not followed by whitespace,
977 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
978 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
980 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
981 # Special and Media are pseudo-namespaces; no pages actually exist in them
983 $nottalk = !Namespace::isTalk( $this->mTitle
->getNamespace() );
985 if ( $useLinkPrefixExtension ) {
986 if ( preg_match( $e2, $s, $m ) ) {
987 $first_prefix = $m[2];
990 $first_prefix = false;
996 wfProfileOut( $fname.'-setup' );
998 # start procedeeding each line
999 foreach ( $a as $line ) {
1000 wfProfileIn( $fname.'-prefixhandling' );
1001 if ( $useLinkPrefixExtension ) {
1002 if ( preg_match( $e2, $s, $m ) ) {
1010 $prefix = $first_prefix;
1011 $first_prefix = false;
1014 wfProfileOut( $fname.'-prefixhandling' );
1016 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1018 # fix up urlencoded title texts
1019 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1021 } else { # Invalid form; output directly
1022 $s .= $prefix . '[[' . $line ;
1028 # :Foobar -- override special treatment of prefix (images, language links)
1029 # /Foobar -- convert to CurrentPage/Foobar
1030 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1032 # Look at the first character
1033 $c = substr($m[1],0,1);
1034 $noforce = ($c != ':');
1038 # / at end means we don't want the slash to be shown
1039 if(substr($m[1],-1,1)=='/') {
1040 $m[1]=substr($m[1],1,strlen($m[1])-2);
1043 $noslash=substr($m[1],1);
1046 # Some namespaces don't allow subpages
1047 if(!empty($wgNamespacesWithSubpages[$this->mTitle
->getNamespace()])) {
1048 # subpages allowed here
1049 $link = $this->mTitle
->getPrefixedText(). '/' . trim($noslash);
1052 } # this might be changed for ugliness reasons
1054 # no subpage allowed, use standard link
1058 } elseif( $noforce ) { # no subpage
1061 # We don't want to keep the first character
1062 $link = substr( $m[1], 1 );
1065 $wasblank = ( '' == $text );
1066 if( $wasblank ) $text = $link;
1068 $nt = Title
::newFromText( $link );
1070 $s .= $prefix . '[[' . $line;
1074 $ns = $nt->getNamespace();
1075 $iw = $nt->getInterWiki();
1077 # Link not escaped by : , create the various objects
1081 if( $iw && $this->mOptions
->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1082 array_push( $this->mOutput
->mLanguageLinks
, $nt->getFullText() );
1083 $tmp = $prefix . $trail ;
1084 $s .= (trim($tmp) == '')?
'': $tmp;
1088 if ( $ns == NS_IMAGE
) {
1089 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1090 $wgLinkCache->addImageLinkObj( $nt );
1094 if ( $ns == NS_CATEGORY
) {
1095 $t = $nt->getText() ;
1096 $nnt = Title
::newFromText ( Namespace::getCanonicalName(NS_CATEGORY
).":".$t ) ;
1098 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1099 $pPLC=$sk->postParseLinkColour();
1100 $sk->postParseLinkColour( false );
1101 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1102 $sk->postParseLinkColour( $pPLC );
1103 $wgLinkCache->resume();
1105 $sortkey = $wasblank ?
$this->mTitle
->getPrefixedText() : $text;
1106 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1107 $this->mOutput
->mCategoryLinks
[] = $t ;
1108 $s .= $prefix . $trail ;
1113 if( ( $nt->getPrefixedText() == $this->mTitle
->getPrefixedText() ) &&
1114 ( strpos( $link, '#' ) == FALSE ) ) {
1115 # Self-links are handled specially; generally de-link and change to bold.
1116 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1120 if( $ns == NS_MEDIA
) {
1121 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1122 $wgLinkCache->addImageLinkObj( $nt );
1124 } elseif( $ns == NS_SPECIAL
) {
1125 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1128 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1130 wfProfileOut( $fname );
1134 # Some functions here used by doBlockLevels()
1136 /* private */ function closeParagraph() {
1138 if ( '' != $this->mLastSection
) {
1139 $result = '</' . $this->mLastSection
. ">\n";
1141 $this->mInPre
= false;
1142 $this->mLastSection
= '';
1145 # getCommon() returns the length of the longest common substring
1146 # of both arguments, starting at the beginning of both.
1148 /* private */ function getCommon( $st1, $st2 ) {
1149 $fl = strlen( $st1 );
1150 $shorter = strlen( $st2 );
1151 if ( $fl < $shorter ) { $shorter = $fl; }
1153 for ( $i = 0; $i < $shorter; ++
$i ) {
1154 if ( $st1{$i} != $st2{$i} ) { break; }
1158 # These next three functions open, continue, and close the list
1159 # element appropriate to the prefix character passed into them.
1161 /* private */ function openList( $char ) {
1162 $result = $this->closeParagraph();
1164 if ( '*' == $char ) { $result .= '<ul><li>'; }
1165 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1166 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1167 else if ( ';' == $char ) {
1168 $result .= '<dl><dt>';
1169 $this->mDTopen
= true;
1171 else { $result = '<!-- ERR 1 -->'; }
1176 /* private */ function nextItem( $char ) {
1177 if ( '*' == $char ||
'#' == $char ) { return '</li><li>'; }
1178 else if ( ':' == $char ||
';' == $char ) {
1180 if ( $this->mDTopen
) { $close = '</dt>'; }
1181 if ( ';' == $char ) {
1182 $this->mDTopen
= true;
1183 return $close . '<dt>';
1185 $this->mDTopen
= false;
1186 return $close . '<dd>';
1189 return '<!-- ERR 2 -->';
1192 /* private */ function closeList( $char ) {
1193 if ( '*' == $char ) { $text = '</li></ul>'; }
1194 else if ( '#' == $char ) { $text = '</li></ol>'; }
1195 else if ( ':' == $char ) {
1196 if ( $this->mDTopen
) {
1197 $this->mDTopen
= false;
1198 $text = '</dt></dl>';
1200 $text = '</dd></dl>';
1203 else { return '<!-- ERR 3 -->'; }
1207 /* private */ function doBlockLevels( $text, $linestart ) {
1208 $fname = 'Parser::doBlockLevels';
1209 wfProfileIn( $fname );
1211 # Parsing through the text line by line. The main thing
1212 # happening here is handling of block-level elements p, pre,
1213 # and making lists from lines starting with * # : etc.
1215 $textLines = explode( "\n", $text );
1217 $lastPrefix = $output = $lastLine = '';
1218 $this->mDTopen
= $inBlockElem = false;
1220 $paragraphStack = false;
1222 if ( !$linestart ) {
1223 $output .= array_shift( $textLines );
1225 foreach ( $textLines as $oLine ) {
1226 $lastPrefixLength = strlen( $lastPrefix );
1227 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1228 $preOpenMatch = preg_match('/<pre/i', $oLine );
1229 if ( !$this->mInPre
) {
1230 # Multiple prefixes may abut each other for nested lists.
1231 $prefixLength = strspn( $oLine, '*#:;' );
1232 $pref = substr( $oLine, 0, $prefixLength );
1235 $pref2 = str_replace( ';', ':', $pref );
1236 $t = substr( $oLine, $prefixLength );
1237 $this->mInPre
= !empty($preOpenMatch);
1239 # Don't interpret any other prefixes in preformatted text
1241 $pref = $pref2 = '';
1246 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1247 # Same as the last item, so no need to deal with nesting or opening stuff
1248 $output .= $this->nextItem( substr( $pref, -1 ) );
1249 $paragraphStack = false;
1251 if ( substr( $pref, -1 ) == ';') {
1252 # The one nasty exception: definition lists work like this:
1253 # ; title : definition text
1254 # So we check for : in the remainder text to split up the
1255 # title and definition, without b0rking links.
1256 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1257 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1259 $output .= $term . $this->nextItem( ':' );
1263 } elseif( $prefixLength ||
$lastPrefixLength ) {
1264 # Either open or close a level...
1265 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1266 $paragraphStack = false;
1268 while( $commonPrefixLength < $lastPrefixLength ) {
1269 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1270 --$lastPrefixLength;
1272 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1273 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1275 while ( $prefixLength > $commonPrefixLength ) {
1276 $char = substr( $pref, $commonPrefixLength, 1 );
1277 $output .= $this->openList( $char );
1279 if ( ';' == $char ) {
1280 # FIXME: This is dupe of code above
1281 if( preg_match( '/^(.*?(?:\s| )):(.*)$/', $t, $match ) ) {
1283 $output .= $term . $this->nextItem( ":" );
1287 ++
$commonPrefixLength;
1289 $lastPrefix = $pref2;
1291 if( 0 == $prefixLength ) {
1292 # No prefix (not in list)--go to paragraph mode
1293 $uniq_prefix = UNIQ_PREFIX
;
1294 // XXX: use a stack for nestable elements like span, table and div
1295 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1296 $closematch = preg_match(
1297 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1298 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1299 if ( $openmatch or $closematch ) {
1300 $paragraphStack = false;
1301 $output .= $this->closeParagraph();
1302 if($preOpenMatch and !$preCloseMatch) {
1303 $this->mInPre
= true;
1305 if ( $closematch ) {
1306 $inBlockElem = false;
1308 $inBlockElem = true;
1310 } else if ( !$inBlockElem && !$this->mInPre
) {
1311 if ( " " == $t{0} and ( $this->mLastSection
== 'pre' or trim($t) != '' ) ) {
1313 if ($this->mLastSection
!= 'pre') {
1314 $paragraphStack = false;
1315 $output .= $this->closeParagraph().'<pre>';
1316 $this->mLastSection
= 'pre';
1320 if ( '' == trim($t) ) {
1321 if ( $paragraphStack ) {
1322 $output .= $paragraphStack.'<br />';
1323 $paragraphStack = false;
1324 $this->mLastSection
= 'p';
1326 if ($this->mLastSection
!= 'p' ) {
1327 $output .= $this->closeParagraph();
1328 $this->mLastSection
= '';
1329 $paragraphStack = '<p>';
1331 $paragraphStack = '</p><p>';
1335 if ( $paragraphStack ) {
1336 $output .= $paragraphStack;
1337 $paragraphStack = false;
1338 $this->mLastSection
= 'p';
1339 } else if ($this->mLastSection
!= 'p') {
1340 $output .= $this->closeParagraph().'<p>';
1341 $this->mLastSection
= 'p';
1347 if ($paragraphStack === false) {
1351 while ( $prefixLength ) {
1352 $output .= $this->closeList( $pref2{$prefixLength-1} );
1355 if ( '' != $this->mLastSection
) {
1356 $output .= '</' . $this->mLastSection
. '>';
1357 $this->mLastSection
= '';
1360 wfProfileOut( $fname );
1364 # Return value of a magic variable (like PAGENAME)
1365 function getVariableValue( $index ) {
1366 global $wgLang, $wgSitename, $wgServer;
1369 case MAG_CURRENTMONTH
:
1370 return $wgLang->formatNum( date( 'm' ) );
1371 case MAG_CURRENTMONTHNAME
:
1372 return $wgLang->getMonthName( date('n') );
1373 case MAG_CURRENTMONTHNAMEGEN
:
1374 return $wgLang->getMonthNameGen( date('n') );
1375 case MAG_CURRENTDAY
:
1376 return $wgLang->formatNum( date('j') );
1378 return $this->mTitle
->getText();
1380 return $this->mTitle
->getPartialURL();
1382 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1383 return $wgLang->getNsText($this->mTitle
->getNamespace()); # Patch by Dori
1384 case MAG_CURRENTDAYNAME
:
1385 return $wgLang->getWeekdayName( date('w')+
1 );
1386 case MAG_CURRENTYEAR
:
1387 return $wgLang->formatNum( date( 'Y' ) );
1388 case MAG_CURRENTTIME
:
1389 return $wgLang->time( wfTimestampNow(), false );
1390 case MAG_NUMBEROFARTICLES
:
1391 return $wgLang->formatNum( wfNumberOfArticles() );
1401 # initialise the magic variables (like CURRENTMONTHNAME)
1402 function initialiseVariables() {
1403 global $wgVariableIDs;
1404 $this->mVariables
= array();
1405 foreach ( $wgVariableIDs as $id ) {
1406 $mw =& MagicWord
::get( $id );
1407 $mw->addToArray( $this->mVariables
, $this->getVariableValue( $id ) );
1411 /* private */ function replaceVariables( $text, $args = array() ) {
1412 global $wgLang, $wgScript, $wgArticlePath;
1414 # Prevent too big inclusions
1415 if(strlen($text)> MAX_INCLUDE_SIZE
)
1418 $fname = 'Parser::replaceVariables';
1419 wfProfileIn( $fname );
1422 $titleChars = Title
::legalChars();
1423 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1425 # This function is called recursively. To keep track of arguments we need a stack:
1426 array_push( $this->mArgStack
, $args );
1428 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1429 $GLOBALS['wgCurParser'] =& $this;
1432 if ( $this->mOutputType
== OT_HTML
) {
1433 # Variable substitution
1434 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1436 # Argument substitution
1437 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1439 # Template substitution
1440 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1441 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1443 array_pop( $this->mArgStack
);
1445 wfProfileOut( $fname );
1449 function variableSubstitution( $matches ) {
1450 if ( !$this->mVariables
) {
1451 $this->initialiseVariables();
1453 if ( array_key_exists( $matches[1], $this->mVariables
) ) {
1454 $text = $this->mVariables
[$matches[1]];
1455 $this->mOutput
->mContainsOldMagic
= true;
1457 $text = $matches[0];
1462 # Split template arguments
1463 function getTemplateArgs( $argsString ) {
1464 if ( $argsString === '' ) {
1468 $args = explode( '|', substr( $argsString, 1 ) );
1470 # If any of the arguments contains a '[[' but no ']]', it needs to be
1471 # merged with the next arg because the '|' character between belongs
1472 # to the link syntax and not the template parameter syntax.
1473 $argc = count($args);
1475 for ( $i = 0; $i < $argc-1; $i++
) {
1476 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1477 $args[$i] .= "|".$args[$i+
1];
1478 array_splice($args, $i+
1, 1);
1487 function braceSubstitution( $matches ) {
1488 global $wgLinkCache, $wgLang;
1489 $fname = 'Parser::braceSubstitution';
1496 # $newline is an optional newline character before the braces
1497 # $part1 is the bit before the first |, and must contain only title characters
1498 # $args is a list of arguments, starting from index 0, not including $part1
1500 $newline = $matches[1];
1501 $part1 = $matches[2];
1502 # If the third subpattern matched anything, it will start with |
1504 $args = $this->getTemplateArgs($matches[3]);
1505 $argc = count( $args );
1508 if ( strpos( $matches[0], '{{{' ) !== false ) {
1509 $text = $matches[0];
1516 $mwSubst =& MagicWord
::get( MAG_SUBST
);
1517 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1518 if ( $this->mOutputType
!= OT_WIKI
) {
1519 # Invalid SUBST not replaced at PST time
1520 # Return without further processing
1521 $text = $matches[0];
1525 } elseif ( $this->mOutputType
== OT_WIKI
) {
1526 # SUBST not found in PST pass, do nothing
1527 $text = $matches[0];
1532 # MSG, MSGNW and INT
1535 $mwMsgnw =& MagicWord
::get( MAG_MSGNW
);
1536 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1539 # Remove obsolete MSG:
1540 $mwMsg =& MagicWord
::get( MAG_MSG
);
1541 $mwMsg->matchStartAndRemove( $part1 );
1544 # Check if it is an internal message
1545 $mwInt =& MagicWord
::get( MAG_INT
);
1546 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1547 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1548 $text = wfMsgReal( $part1, $args, true );
1556 # Check for NS: (namespace expansion)
1557 $mwNs = MagicWord
::get( MAG_NS
);
1558 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1559 if ( intval( $part1 ) ) {
1560 $text = $wgLang->getNsText( intval( $part1 ) );
1563 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1564 if ( !is_null( $index ) ) {
1565 $text = $wgLang->getNsText( $index );
1572 # LOCALURL and LOCALURLE
1574 $mwLocal = MagicWord
::get( MAG_LOCALURL
);
1575 $mwLocalE = MagicWord
::get( MAG_LOCALURLE
);
1577 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1578 $func = 'getLocalURL';
1579 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1580 $func = 'escapeLocalURL';
1585 if ( $func !== '' ) {
1586 $title = Title
::newFromText( $part1 );
1587 if ( !is_null( $title ) ) {
1589 $text = $title->$func( $args[0] );
1591 $text = $title->$func();
1598 # Internal variables
1599 if ( !$this->mVariables
) {
1600 $this->initialiseVariables();
1602 if ( !$found && array_key_exists( $part1, $this->mVariables
) ) {
1603 $text = $this->mVariables
[$part1];
1605 $this->mOutput
->mContainsOldMagic
= true;
1608 # Template table test
1610 # Did we encounter this template already? If yes, it is in the cache
1611 # and we need to check for loops.
1612 if ( isset( $this->mTemplates
[$part1] ) ) {
1613 # Infinite loop test
1614 if ( isset( $this->mTemplatePath
[$part1] ) ) {
1618 # set $text to cached message.
1619 $text = $this->mTemplates
[$part1];
1623 # Load from database
1625 $title = Title
::newFromText( $part1, NS_TEMPLATE
);
1626 if ( !is_null( $title ) && !$title->isExternal() ) {
1627 # Check for excessive inclusion
1628 $dbk = $title->getPrefixedDBkey();
1629 if ( $this->incrementIncludeCount( $dbk ) ) {
1630 # This should never be reached.
1631 $article = new Article( $title );
1632 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1633 if ( $articleContent !== false ) {
1635 $text = $articleContent;
1639 # If the title is valid but undisplayable, make a link to it
1640 if ( $this->mOutputType
== OT_HTML
&& !$found ) {
1641 $text = '[['.$title->getPrefixedText().']]';
1645 # Template cache array insertion
1646 $this->mTemplates
[$part1] = $text;
1650 # Recursive parsing, escaping and link table handling
1651 # Only for HTML output
1652 if ( $nowiki && $found && $this->mOutputType
== OT_HTML
) {
1653 $text = wfEscapeWikiText( $text );
1654 } elseif ( $this->mOutputType
== OT_HTML
&& $found && !$noparse) {
1655 # Clean up argument array
1656 $assocArgs = array();
1658 foreach( $args as $arg ) {
1659 $eqpos = strpos( $arg, '=' );
1660 if ( $eqpos === false ) {
1661 $assocArgs[$index++
] = $arg;
1663 $name = trim( substr( $arg, 0, $eqpos ) );
1664 $value = trim( substr( $arg, $eqpos+
1 ) );
1665 if ( $value === false ) {
1668 if ( $name !== false ) {
1669 $assocArgs[$name] = $value;
1674 # Do not enter included links in link table
1675 if ( !is_null( $title ) ) {
1676 $wgLinkCache->suspend();
1679 # Add a new element to the templace recursion path
1680 $this->mTemplatePath
[$part1] = 1;
1682 $text = $this->stripParse( $text, $newline, $assocArgs );
1684 # Resume the link cache and register the inclusion as a link
1685 if ( !is_null( $title ) ) {
1686 $wgLinkCache->resume();
1687 $wgLinkCache->addLinkObj( $title );
1690 # Empties the template path
1691 $this->mTemplatePath
= array();
1700 # Triple brace replacement -- used for template arguments
1701 function argSubstitution( $matches ) {
1702 $newline = $matches[1];
1703 $arg = trim( $matches[2] );
1704 $text = $matches[0];
1705 $inputArgs = end( $this->mArgStack
);
1707 if ( array_key_exists( $arg, $inputArgs ) ) {
1708 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1714 # Returns true if the function is allowed to include this entity
1715 function incrementIncludeCount( $dbk ) {
1716 if ( !array_key_exists( $dbk, $this->mIncludeCount
) ) {
1717 $this->mIncludeCount
[$dbk] = 0;
1719 if ( ++
$this->mIncludeCount
[$dbk] <= MAX_INCLUDE_REPEAT
) {
1727 # Cleans up HTML, removes dangerous tags and attributes
1728 /* private */ function removeHTMLtags( $text ) {
1729 global $wgUseTidy, $wgUserHtml;
1730 $fname = 'Parser::removeHTMLtags';
1731 wfProfileIn( $fname );
1734 $htmlpairs = array( # Tags that must be closed
1735 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1736 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1737 'strike', 'strong', 'tt', 'var', 'div', 'center',
1738 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1739 'ruby', 'rt' , 'rb' , 'rp', 'p'
1741 $htmlsingle = array(
1742 'br', 'hr', 'li', 'dt', 'dd'
1744 $htmlnest = array( # Tags that can be nested--??
1745 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1746 'dl', 'font', 'big', 'small', 'sub', 'sup'
1748 $tabletags = array( # Can only appear inside table
1752 $htmlpairs = array();
1753 $htmlsingle = array();
1754 $htmlnest = array();
1755 $tabletags = array();
1758 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1759 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1761 $htmlattrs = $this->getHTMLattrs () ;
1763 # Remove HTML comments
1764 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1766 $bits = explode( '<', $text );
1767 $text = array_shift( $bits );
1769 $tagstack = array(); $tablestack = array();
1770 foreach ( $bits as $x ) {
1771 $prev = error_reporting( E_ALL
& ~
( E_NOTICE | E_WARNING
) );
1772 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1774 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1775 error_reporting( $prev );
1778 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1782 if ( ! in_array( $t, $htmlsingle ) &&
1783 ( $ot = @array_pop
( $tagstack ) ) != $t ) {
1784 @array_push
( $tagstack, $ot );
1787 if ( $t == 'table' ) {
1788 $tagstack = array_pop( $tablestack );
1793 # Keep track for later
1794 if ( in_array( $t, $tabletags ) &&
1795 ! in_array( 'table', $tagstack ) ) {
1797 } else if ( in_array( $t, $tagstack ) &&
1798 ! in_array ( $t , $htmlnest ) ) {
1800 } else if ( ! in_array( $t, $htmlsingle ) ) {
1801 if ( $t == 'table' ) {
1802 array_push( $tablestack, $tagstack );
1803 $tagstack = array();
1805 array_push( $tagstack, $t );
1807 # Strip non-approved attributes from the tag
1808 $newparams = $this->fixTagAttributes($params);
1812 $rest = str_replace( '>', '>', $rest );
1813 $text .= "<$slash$t $newparams$brace$rest";
1817 $text .= '<' . str_replace( '>', '>', $x);
1819 # Close off any remaining tags
1820 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
1822 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
1825 # this might be possible using tidy itself
1826 foreach ( $bits as $x ) {
1827 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1829 @list
( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1830 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1831 $newparams = $this->fixTagAttributes($params);
1832 $rest = str_replace( '>', '>', $rest );
1833 $text .= "<$slash$t $newparams$brace$rest";
1835 $text .= '<' . str_replace( '>', '>', $x);
1839 wfProfileOut( $fname );
1844 # This function accomplishes several tasks:
1845 # 1) Auto-number headings if that option is enabled
1846 # 2) Add an [edit] link to sections for logged in users who have enabled the option
1847 # 3) Add a Table of contents on the top for users who have enabled the option
1848 # 4) Auto-anchor headings
1850 # It loops through all headlines, collects the necessary data, then splits up the
1851 # string and re-inserts the newly formatted headlines.
1852 /* private */ function formatHeadings( $text, $isMain=true ) {
1853 global $wgInputEncoding, $wgMaxTocLevel;
1855 $doNumberHeadings = $this->mOptions
->getNumberHeadings();
1856 $doShowToc = $this->mOptions
->getShowToc();
1857 $forceTocHere = false;
1858 if( !$this->mTitle
->userCanEdit() ) {
1860 $rightClickHack = 0;
1862 $showEditLink = $this->mOptions
->getEditSection();
1863 $rightClickHack = $this->mOptions
->getEditSectionOnRightClick();
1866 # Inhibit editsection links if requested in the page
1867 $esw =& MagicWord
::get( MAG_NOEDITSECTION
);
1868 if( $esw->matchAndRemove( $text ) ) {
1871 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1873 $mw =& MagicWord
::get( MAG_NOTOC
);
1874 if( $mw->matchAndRemove( $text ) ) {
1878 # never add the TOC to the Main Page. This is an entry page that should not
1879 # be more than 1-2 screens large anyway
1880 if( $this->mTitle
->getPrefixedText() == wfMsg('mainpage') ) {
1884 # Get all headlines for numbering them and adding funky stuff like [edit]
1885 # links - this is for later, but we need the number of headlines right now
1886 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
1888 # if there are fewer than 4 headlines in the article, do not show TOC
1889 if( $numMatches < 4 ) {
1893 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
1894 # override above conditions and always show TOC at that place
1895 $mw =& MagicWord
::get( MAG_TOC
);
1896 if ($mw->match( $text ) ) {
1898 $forceTocHere = true;
1900 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
1901 # override above conditions and always show TOC above first header
1902 $mw =& MagicWord
::get( MAG_FORCETOC
);
1903 if ($mw->matchAndRemove( $text ) ) {
1910 # We need this to perform operations on the HTML
1911 $sk =& $this->mOptions
->getSkin();
1916 # Ugh .. the TOC should have neat indentation levels which can be
1917 # passed to the skin functions. These are determined here
1922 $sublevelCount = array();
1925 foreach( $matches[3] as $headline ) {
1928 $prevlevel = $level;
1930 $level = $matches[1][$headlineCount];
1931 if( ( $doNumberHeadings ||
$doShowToc ) && $prevlevel && $level > $prevlevel ) {
1932 # reset when we enter a new level
1933 $sublevelCount[$level] = 0;
1934 $toc .= $sk->tocIndent( $level - $prevlevel );
1935 $toclevel +
= $level - $prevlevel;
1937 if( ( $doNumberHeadings ||
$doShowToc ) && $level < $prevlevel ) {
1938 # reset when we step back a level
1939 $sublevelCount[$level+
1]=0;
1940 $toc .= $sk->tocUnindent( $prevlevel - $level );
1941 $toclevel -= $prevlevel - $level;
1943 # count number of headlines for each level
1944 @$sublevelCount[$level]++
;
1945 if( $doNumberHeadings ||
$doShowToc ) {
1947 for( $i = 1; $i <= $level; $i++
) {
1948 if( !empty( $sublevelCount[$i] ) ) {
1952 $numbering .= $sublevelCount[$i];
1958 # The canonized header is a version of the header text safe to use for links
1959 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
1960 $canonized_headline = $this->unstrip( $headline, $this->mStripState
);
1961 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState
);
1964 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
1965 $tocline = trim( $canonized_headline );
1966 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT
, $wgInputEncoding ) );
1967 $replacearray = array(
1971 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
1972 $refer[$headlineCount] = $canonized_headline;
1974 # count how many in assoc. array so we can track dupes in anchors
1975 @$refers[$canonized_headline]++
;
1976 $refcount[$headlineCount]=$refers[$canonized_headline];
1978 # Prepend the number to the heading text
1980 if( $doNumberHeadings ||
$doShowToc ) {
1981 $tocline = $numbering . ' ' . $tocline;
1983 # Don't number the heading if it is the only one (looks silly)
1984 if( $doNumberHeadings && count( $matches[3] ) > 1) {
1985 # the two are different if the line contains a link
1986 $headline=$numbering . ' ' . $headline;
1990 # Create the anchor for linking from the TOC to the section
1991 $anchor = $canonized_headline;
1992 if($refcount[$headlineCount] > 1 ) {
1993 $anchor .= '_' . $refcount[$headlineCount];
1995 if( $doShowToc && ( !isset($wgMaxTocLevel) ||
$toclevel<$wgMaxTocLevel ) ) {
1996 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
1998 if( $showEditLink ) {
1999 if ( empty( $head[$headlineCount] ) ) {
2000 $head[$headlineCount] = '';
2002 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+
1);
2005 # Add the edit section span
2006 if( $rightClickHack ) {
2007 $headline = $sk->editSectionScript($headlineCount+
1,$headline);
2010 # give headline the correct <h#> tag
2011 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2017 $toclines = $headlineCount;
2018 $toc .= $sk->tocUnindent( $toclevel );
2019 $toc = $sk->tocTable( $toc );
2022 # split up and insert constructed headlines
2024 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2027 foreach( $blocks as $block ) {
2028 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2029 # This is the [edit] link that appears for the top block of text when
2030 # section editing is enabled
2032 # Disabled because it broke block formatting
2033 # For example, a bullet point in the top line
2034 # $full .= $sk->editSectionLink(0);
2037 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2038 # Top anchor now in skin
2042 if( !empty( $head[$i] ) ) {
2048 $mw =& MagicWord
::get( MAG_TOC
);
2049 return $mw->replace( $toc, $full );
2055 # Return an HTML link for the "ISBN 123456" text
2056 /* private */ function magicISBN( $text ) {
2058 $fname = 'Parser::magicISBN';
2059 wfProfileIn( $fname );
2061 $a = split( 'ISBN ', " $text" );
2062 if ( count ( $a ) < 2 ) {
2063 wfProfileOut( $fname );
2066 $text = substr( array_shift( $a ), 1);
2067 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2069 foreach ( $a as $x ) {
2070 $isbn = $blank = '' ;
2071 while ( ' ' == $x{0} ) {
2073 $x = substr( $x, 1 );
2075 while ( strstr( $valid, $x{0} ) != false ) {
2077 $x = substr( $x, 1 );
2079 $num = str_replace( '-', '', $isbn );
2080 $num = str_replace( ' ', '', $num );
2083 $text .= "ISBN $blank$x";
2085 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Booksources' );
2086 $text .= '<a href="' .
2087 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2088 "\" class=\"internal\">ISBN $isbn</a>";
2092 wfProfileOut( $fname );
2096 # Return an HTML link for the "GEO ..." text
2097 /* private */ function magicGEO( $text ) {
2098 global $wgLang, $wgUseGeoMode;
2099 $fname = 'Parser::magicGEO';
2100 wfProfileIn( $fname );
2102 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2103 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2104 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2105 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['N']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2106 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2107 $text = preg_replace ( "/(\d+)°(\d+)'(\d+)\" {$directions['S']}, (\d+)°(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2109 $a = split( 'GEO ', " $text" );
2110 if ( count ( $a ) < 2 ) {
2111 wfProfileOut( $fname );
2114 $text = substr( array_shift( $a ), 1);
2115 $valid = '0123456789.+-:';
2117 foreach ( $a as $x ) {
2118 $geo = $blank = '' ;
2119 while ( ' ' == $x{0} ) {
2121 $x = substr( $x, 1 );
2123 while ( strstr( $valid, $x{0} ) != false ) {
2125 $x = substr( $x, 1 );
2127 $num = str_replace( '+', '', $geo );
2128 $num = str_replace( ' ', '', $num );
2130 if ( '' == $num ||
count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2131 $text .= "GEO $blank$x";
2133 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Geo' );
2134 $text .= '<a href="' .
2135 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2136 "\" class=\"internal\">GEO $geo</a>";
2140 wfProfileOut( $fname );
2144 # Return an HTML link for the "RFC 1234" text
2145 /* private */ function magicRFC( $text ) {
2148 $a = split( 'RFC ', ' '.$text );
2149 if ( count ( $a ) < 2 ) return $text;
2150 $text = substr( array_shift( $a ), 1);
2151 $valid = '0123456789';
2153 foreach ( $a as $x ) {
2154 $rfc = $blank = '' ;
2155 while ( ' ' == $x{0} ) {
2157 $x = substr( $x, 1 );
2159 while ( strstr( $valid, $x{0} ) != false ) {
2161 $x = substr( $x, 1 );
2165 $text .= "RFC $blank$x";
2167 $url = wfmsg( 'rfcurl' );
2168 $url = str_replace( '$1', $rfc, $url);
2169 $sk =& $this->mOptions
->getSkin();
2170 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2171 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2177 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2178 $this->mOptions
= $options;
2179 $this->mTitle
=& $title;
2180 $this->mOutputType
= OT_WIKI
;
2182 if ( $clearState ) {
2183 $this->clearState();
2186 $stripState = false;
2190 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2194 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2195 "/<br *?>/i" => "<br />",
2197 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2199 $text = $this->strip( $text, $stripState, false );
2200 $text = $this->pstPass2( $text, $user );
2201 $text = $this->unstrip( $text, $stripState );
2202 $text = $this->unstripNoWiki( $text, $stripState );
2206 /* private */ function pstPass2( $text, &$user ) {
2207 global $wgLang, $wgLocaltimezone, $wgCurParser;
2209 # Variable replacement
2210 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2211 $text = $this->replaceVariables( $text );
2215 $n = $user->getName();
2216 $k = $user->getOption( 'nickname' );
2217 if ( '' == $k ) { $k = $n; }
2218 if(isset($wgLocaltimezone)) {
2219 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2221 /* Note: this is an ugly timezone hack for the European wikis */
2222 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2223 ' (' . date( 'T' ) . ')';
2224 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2226 $text = preg_replace( '/~~~~~/', $d, $text );
2227 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]] $d", $text );
2228 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER
) . ":$n|$k]]", $text );
2230 # Context links: [[|name]] and [[name (context)|]]
2232 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2233 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2234 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2235 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2237 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2238 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2239 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2240 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2241 # [[ns:page (cont)|]]
2243 $t = $this->mTitle
->getText();
2244 if ( preg_match( $conpat, $t, $m ) ) {
2247 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2248 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2249 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2251 if ( '' == $context ) {
2252 $text = preg_replace( $p2, '[[\\1]]', $text );
2254 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2258 $mw =& MagicWord::get( MAG_SUBST );
2259 $wgCurParser = $this->fork();
2260 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2261 $this->merge( $wgCurParser );
2264 # Trim trailing whitespace
2265 # MAG_END (__END__) tag allows for trailing
2266 # whitespace to be deliberately included
2267 $text = rtrim( $text );
2268 $mw =& MagicWord
::get( MAG_END
);
2269 $mw->matchAndRemove( $text );
2274 # Set up some variables which are usually set up in parse()
2275 # so that an external function can call some class members with confidence
2276 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2277 $this->mTitle
=& $title;
2278 $this->mOptions
= $options;
2279 $this->mOutputType
= $outputType;
2280 if ( $clearState ) {
2281 $this->clearState();
2285 function transformMsg( $text, $options ) {
2287 static $executing = false;
2289 # Guard against infinite recursion
2295 $this->mTitle
= $wgTitle;
2296 $this->mOptions
= $options;
2297 $this->mOutputType
= OT_MSG
;
2298 $this->clearState();
2299 $text = $this->replaceVariables( $text );
2305 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2306 # Callback will be called with the text within
2307 # Transform and return the text within
2308 function setHook( $tag, $callback ) {
2309 $oldVal = @$this->mTagHooks
[$tag];
2310 $this->mTagHooks
[$tag] = $callback;
2317 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2318 var $mCacheTime; # Used in ParserCache
2320 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2321 $containsOldMagic = false )
2323 $this->mText
= $text;
2324 $this->mLanguageLinks
= $languageLinks;
2325 $this->mCategoryLinks
= $categoryLinks;
2326 $this->mContainsOldMagic
= $containsOldMagic;
2327 $this->mCacheTime
= "";
2330 function getText() { return $this->mText
; }
2331 function getLanguageLinks() { return $this->mLanguageLinks
; }
2332 function getCategoryLinks() { return $this->mCategoryLinks
; }
2333 function getCacheTime() { return $this->mCacheTime
; }
2334 function containsOldMagic() { return $this->mContainsOldMagic
; }
2335 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
2336 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
2337 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks
, $cl ); }
2338 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
2339 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
2341 function merge( $other ) {
2342 $this->mLanguageLinks
= array_merge( $this->mLanguageLinks
, $other->mLanguageLinks
);
2343 $this->mCategoryLinks
= array_merge( $this->mCategoryLinks
, $this->mLanguageLinks
);
2344 $this->mContainsOldMagic
= $this->mContainsOldMagic ||
$other->mContainsOldMagic
;
2351 # All variables are private
2352 var $mUseTeX; # Use texvc to expand <math> tags
2353 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2354 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2355 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2356 var $mAllowExternalImages; # Allow external images inline
2357 var $mSkin; # Reference to the preferred skin
2358 var $mDateFormat; # Date format index
2359 var $mEditSection; # Create "edit section" links
2360 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2361 var $mNumberHeadings; # Automatically number headings
2362 var $mShowToc; # Show table of contents
2364 function getUseTeX() { return $this->mUseTeX
; }
2365 function getUseCategoryMagic() { return $this->mUseCategoryMagic
; }
2366 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
2367 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
2368 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
2369 function getSkin() { return $this->mSkin
; }
2370 function getDateFormat() { return $this->mDateFormat
; }
2371 function getEditSection() { return $this->mEditSection
; }
2372 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick
; }
2373 function getNumberHeadings() { return $this->mNumberHeadings
; }
2374 function getShowToc() { return $this->mShowToc
; }
2376 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX
, $x ); }
2377 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic
, $x ); }
2378 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
2379 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
2380 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
2381 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
2382 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
2383 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick
, $x ); }
2384 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
2385 function setShowToc( $x ) { return wfSetVar( $this->mShowToc
, $x ); }
2387 function setSkin( &$x ) { $this->mSkin
=& $x; }
2389 # Get parser options
2390 /* static */ function newFromUser( &$user ) {
2391 $popts = new ParserOptions
;
2392 $popts->initialiseFromUser( $user );
2397 function initialiseFromUser( &$userInput ) {
2398 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2400 $fname = "ParserOptions::initialiseFromUser";
2401 wfProfileIn( $fname );
2402 if ( !$userInput ) {
2404 $user->setLoaded( true );
2406 $user =& $userInput;
2409 $this->mUseTeX
= $wgUseTeX;
2410 $this->mUseCategoryMagic
= $wgUseCategoryMagic;
2411 $this->mUseDynamicDates
= $wgUseDynamicDates;
2412 $this->mInterwikiMagic
= $wgInterwikiMagic;
2413 $this->mAllowExternalImages
= $wgAllowExternalImages;
2414 wfProfileIn( "$fname-skin" );
2415 $this->mSkin
=& $user->getSkin();
2416 wfProfileOut( "$fname-skin" );
2417 $this->mDateFormat
= $user->getOption( 'date' );
2418 $this->mEditSection
= $user->getOption( 'editsection' );
2419 $this->mEditSectionOnRightClick
= $user->getOption( 'editsectiononrightclick' );
2420 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
2421 $this->mShowToc
= $user->getOption( 'showtoc' );
2422 wfProfileOut( $fname );
2428 # Regex callbacks, used in Parser::replaceVariables
2429 function wfBraceSubstitution( $matches ) {
2430 global $wgCurParser;
2431 return $wgCurParser->braceSubstitution( $matches );
2434 function wfArgSubstitution( $matches ) {
2435 global $wgCurParser;
2436 return $wgCurParser->argSubstitution( $matches );
2439 function wfVariableSubstitution( $matches ) {
2440 global $wgCurParser;
2441 return $wgCurParser->variableSubstitution( $matches );
2444 # Return the total number of articles
2445 function wfNumberOfArticles() {
2446 global $wgNumberOfArticles;
2449 return $wgNumberOfArticles;
2452 # Get various statistics from the database
2453 /* private */ function wfLoadSiteStats() {
2454 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2455 $fname = 'wfLoadSiteStats';
2457 if ( -1 != $wgNumberOfArticles ) return;
2458 $dbr =& wfGetDB( DB_SLAVE
);
2459 $s = $dbr->getArray( 'site_stats',
2460 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
2461 array( 'ss_row_id' => 1 ), $fname
2464 if ( $s === false ) {
2467 $wgTotalViews = $s->ss_total_views
;
2468 $wgTotalEdits = $s->ss_total_edits
;
2469 $wgNumberOfArticles = $s->ss_good_articles
;
2473 function wfEscapeHTMLTagsOnly( $in ) {
2475 array( '"', '>', '<' ),
2476 array( '"', '>', '<' ),