fix a problem by not inserting __MWTEMPLATESECTION for section titles that don't...
[lhc/web/wiklou.git] / includes / Parser.php
1 <?php
2
3 // require_once('Tokenizer.php');
4
5 /**
6 * File for Parser and related classes
7 *
8 * @package MediaWiki
9 * @version $Id$
10 */
11
12 /**
13 * Variable substitution O(N^2) attack
14 *
15 * Without countermeasures, it would be possible to attack the parser by saving
16 * a page filled with a large number of inclusions of large pages. The size of
17 * the generated page would be proportional to the square of the input size.
18 * Hence, we limit the number of inclusions of any given page, thus bringing any
19 * attack back to O(N).
20 */
21 define( 'MAX_INCLUDE_REPEAT', 100 );
22 define( 'MAX_INCLUDE_SIZE', 1000000 ); // 1 Million
23
24 # Allowed values for $mOutputType
25 define( 'OT_HTML', 1 );
26 define( 'OT_WIKI', 2 );
27 define( 'OT_MSG' , 3 );
28
29 # string parameter for extractTags which will cause it
30 # to strip HTML comments in addition to regular
31 # <XML>-style tags. This should not be anything we
32 # may want to use in wikisyntax
33 define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
34
35 # prefix for escaping, used in two functions at least
36 define( 'UNIQ_PREFIX', 'NaodW29');
37
38 # Constants needed for external link processing
39 define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
40 define( 'HTTP_PROTOCOLS', 'http|https' );
41 # Everything except bracket, space, or control characters
42 define( 'EXT_LINK_URL_CLASS', '[^]\\x00-\\x20\\x7F]' );
43 define( 'INVERSE_EXT_LINK_URL_CLASS', '[\]\\x00-\\x20\\x7F]' );
44 # Including space
45 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
46 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
47 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
48 define( 'EXT_LINK_BRACKETED', '/\[(('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
49 define( 'EXT_IMAGE_REGEX',
50 '/^('.HTTP_PROTOCOLS.':)'. # Protocol
51 '('.EXT_LINK_URL_CLASS.'+)\\/'. # Hostname and path
52 '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
53 );
54
55 /**
56 * PHP Parser
57 *
58 * Processes wiki markup
59 *
60 * <pre>
61 * There are three main entry points into the Parser class:
62 * parse()
63 * produces HTML output
64 * preSaveTransform().
65 * produces altered wiki markup.
66 * transformMsg()
67 * performs brace substitution on MediaWiki messages
68 *
69 * Globals used:
70 * objects: $wgLang, $wgDateFormatter, $wgLinkCache, $wgCurParser
71 *
72 * NOT $wgArticle, $wgUser or $wgTitle. Keep them away!
73 *
74 * settings:
75 * $wgUseTex*, $wgUseDynamicDates*, $wgInterwikiMagic*,
76 * $wgNamespacesWithSubpages, $wgAllowExternalImages*,
77 * $wgLocaltimezone
78 *
79 * * only within ParserOptions
80 * </pre>
81 *
82 * @package MediaWiki
83 */
84 class Parser
85 {
86 /**#@+
87 * @access private
88 */
89 # Persistent:
90 var $mTagHooks;
91
92 # Cleared with clearState():
93 var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
94 var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
95
96 # Temporary:
97 var $mOptions, $mTitle, $mOutputType,
98 $mTemplates, // cache of already loaded templates, avoids
99 // multiple SQL queries for the same string
100 $mTemplatePath; // stores an unsorted hash of all the templates already loaded
101 // in this path. Used for loop detection.
102
103 /**#@-*/
104
105 /**
106 * Constructor
107 *
108 * @access public
109 */
110 function Parser() {
111 $this->mTemplates = array();
112 $this->mTemplatePath = array();
113 $this->mTagHooks = array();
114 $this->clearState();
115 }
116
117 /**
118 * Clear Parser state
119 *
120 * @access private
121 */
122 function clearState() {
123 $this->mOutput = new ParserOutput;
124 $this->mAutonumber = 0;
125 $this->mLastSection = "";
126 $this->mDTopen = false;
127 $this->mVariables = false;
128 $this->mIncludeCount = array();
129 $this->mStripState = array();
130 $this->mArgStack = array();
131 $this->mInPre = false;
132 }
133
134 /**
135 * First pass--just handle <nowiki> sections, pass the rest off
136 * to internalParse() which does all the real work.
137 *
138 * @access private
139 * @return ParserOutput a ParserOutput
140 */
141 function parse( $text, &$title, $options, $linestart = true, $clearState = true ) {
142 global $wgUseTidy;
143 $fname = 'Parser::parse';
144 wfProfileIn( $fname );
145
146 if ( $clearState ) {
147 $this->clearState();
148 }
149
150 $this->mOptions = $options;
151 $this->mTitle =& $title;
152 $this->mOutputType = OT_HTML;
153
154 $stripState = NULL;
155 $text = $this->strip( $text, $this->mStripState );
156 $text = $this->internalParse( $text, $linestart );
157 $text = $this->unstrip( $text, $this->mStripState );
158 # Clean up special characters, only run once, next-to-last before doBlockLevels
159 if(!$wgUseTidy) {
160 $fixtags = array(
161 # french spaces, last one Guillemet-left
162 # only if there is something before the space
163 '/(.) (?=\\?|:|;|!|\\302\\273)/i' => '\\1&nbsp;\\2',
164 # french spaces, Guillemet-right
165 "/(\\302\\253) /i"=>"\\1&nbsp;",
166 '/<hr *>/i' => '<hr />',
167 '/<br *>/i' => '<br />',
168 '/<center *>/i' => '<div class="center">',
169 '/<\\/center *>/i' => '</div>',
170 # Clean up spare ampersands; note that we probably ought to be
171 # more careful about named entities.
172 '/&(?!:amp;|#[Xx][0-9A-fa-f]+;|#[0-9]+;|[a-zA-Z0-9]+;)/' => '&amp;'
173 );
174 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
175 } else {
176 $fixtags = array(
177 # french spaces, last one Guillemet-left
178 '/ (\\?|:|;|!|\\302\\273)/i' => '&nbsp;\\1',
179 # french spaces, Guillemet-right
180 '/(\\302\\253) /i' => '\\1&nbsp;',
181 '/<center *>/i' => '<div class="center">',
182 '/<\\/center *>/i' => '</div>'
183 );
184 $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
185 }
186 # only once and last
187 $text = $this->doBlockLevels( $text, $linestart );
188 $text = $this->unstripNoWiki( $text, $this->mStripState );
189 if($wgUseTidy) {
190 $text = $this->tidy($text);
191 }
192 $this->mOutput->setText( $text );
193 wfProfileOut( $fname );
194 return $this->mOutput;
195 }
196
197 /**
198 * Get a random string
199 *
200 * @access private
201 * @static
202 */
203 function getRandomString() {
204 return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
205 }
206
207 /**
208 * Replaces all occurrences of <$tag>content</$tag> in the text
209 * with a random marker and returns the new text. the output parameter
210 * $content will be an associative array filled with data on the form
211 * $unique_marker => content.
212 *
213 * If $content is already set, the additional entries will be appended
214 * If $tag is set to STRIP_COMMENTS, the function will extract
215 * <!-- HTML comments -->
216 *
217 * @access private
218 * @static
219 */
220 function extractTags($tag, $text, &$content, $uniq_prefix = ''){
221 $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
222 if ( !$content ) {
223 $content = array( );
224 }
225 $n = 1;
226 $stripped = '';
227
228 while ( '' != $text ) {
229 if($tag==STRIP_COMMENTS) {
230 $p = preg_split( '/<!--/i', $text, 2 );
231 } else {
232 $p = preg_split( "/<\\s*$tag\\s*>/i", $text, 2 );
233 }
234 $stripped .= $p[0];
235 if ( ( count( $p ) < 2 ) || ( '' == $p[1] ) ) {
236 $text = '';
237 } else {
238 if($tag==STRIP_COMMENTS) {
239 $q = preg_split( '/-->/i', $p[1], 2 );
240 } else {
241 $q = preg_split( "/<\\/\\s*$tag\\s*>/i", $p[1], 2 );
242 }
243 $marker = $rnd . sprintf('%08X', $n++);
244 $content[$marker] = $q[0];
245 $stripped .= $marker;
246 $text = $q[1];
247 }
248 }
249 return $stripped;
250 }
251
252 /**
253 * Strips and renders nowiki, pre, math, hiero
254 * If $render is set, performs necessary rendering operations on plugins
255 * Returns the text, and fills an array with data needed in unstrip()
256 * If the $state is already a valid strip state, it adds to the state
257 *
258 * @param bool $stripcomments when set, HTML comments <!-- like this -->
259 * will be stripped in addition to other tags. This is important
260 * for section editing, where these comments cause confusion when
261 * counting the sections in the wikisource
262 *
263 * @access private
264 */
265 function strip( $text, &$state, $stripcomments = false ) {
266 $render = ($this->mOutputType == OT_HTML);
267 $html_content = array();
268 $nowiki_content = array();
269 $math_content = array();
270 $pre_content = array();
271 $comment_content = array();
272 $ext_content = array();
273
274 # Replace any instances of the placeholders
275 $uniq_prefix = UNIQ_PREFIX;
276 #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
277
278 # html
279 global $wgRawHtml, $wgWhitelistEdit;
280 if( $wgRawHtml && $wgWhitelistEdit ) {
281 $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
282 foreach( $html_content as $marker => $content ) {
283 if ($render ) {
284 # Raw and unchecked for validity.
285 $html_content[$marker] = $content;
286 } else {
287 $html_content[$marker] = '<html>'.$content.'</html>';
288 }
289 }
290 }
291
292 # nowiki
293 $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
294 foreach( $nowiki_content as $marker => $content ) {
295 if( $render ){
296 $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
297 } else {
298 $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
299 }
300 }
301
302 # math
303 $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
304 foreach( $math_content as $marker => $content ){
305 if( $render ) {
306 if( $this->mOptions->getUseTeX() ) {
307 $math_content[$marker] = renderMath( $content );
308 } else {
309 $math_content[$marker] = '&lt;math&gt;'.$content.'&lt;math&gt;';
310 }
311 } else {
312 $math_content[$marker] = '<math>'.$content.'</math>';
313 }
314 }
315
316 # pre
317 $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
318 foreach( $pre_content as $marker => $content ){
319 if( $render ){
320 $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
321 } else {
322 $pre_content[$marker] = '<pre>'.$content.'</pre>';
323 }
324 }
325
326 # Comments
327 if($stripcomments) {
328 $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
329 foreach( $comment_content as $marker => $content ){
330 $comment_content[$marker] = '<!--'.$content.'-->';
331 }
332 }
333
334 # Extensions
335 foreach ( $this->mTagHooks as $tag => $callback ) {
336 $ext_contents[$tag] = array();
337 $text = Parser::extractTags( $tag, $text, $ext_content[$tag], $uniq_prefix );
338 foreach( $ext_content[$tag] as $marker => $content ) {
339 if ( $render ) {
340 $ext_content[$tag][$marker] = $callback( $content );
341 } else {
342 $ext_content[$tag][$marker] = "<$tag>$content</$tag>";
343 }
344 }
345 }
346
347 # Merge state with the pre-existing state, if there is one
348 if ( $state ) {
349 $state['html'] = $state['html'] + $html_content;
350 $state['nowiki'] = $state['nowiki'] + $nowiki_content;
351 $state['math'] = $state['math'] + $math_content;
352 $state['pre'] = $state['pre'] + $pre_content;
353 $state['comment'] = $state['comment'] + $comment_content;
354
355 foreach( $ext_content as $tag => $array ) {
356 if ( array_key_exists( $tag, $state ) ) {
357 $state[$tag] = $state[$tag] + $array;
358 }
359 }
360 } else {
361 $state = array(
362 'html' => $html_content,
363 'nowiki' => $nowiki_content,
364 'math' => $math_content,
365 'pre' => $pre_content,
366 'comment' => $comment_content,
367 ) + $ext_content;
368 }
369 return $text;
370 }
371
372 /**
373 * restores pre, math, and heiro removed by strip()
374 *
375 * always call unstripNoWiki() after this one
376 * @access private
377 */
378 function unstrip( $text, &$state ) {
379 # Must expand in reverse order, otherwise nested tags will be corrupted
380 $contentDict = end( $state );
381 for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
382 if( key($state) != 'nowiki' && key($state) != 'html') {
383 for ( $content = end( $contentDict ); $content !== false; $content = prev( $contentDict ) ) {
384 $text = str_replace( key( $contentDict ), $content, $text );
385 }
386 }
387 }
388
389 return $text;
390 }
391
392 /**
393 * always call this after unstrip() to preserve the order
394 *
395 * @access private
396 */
397 function unstripNoWiki( $text, &$state ) {
398 # Must expand in reverse order, otherwise nested tags will be corrupted
399 for ( $content = end($state['nowiki']); $content !== false; $content = prev( $state['nowiki'] ) ) {
400 $text = str_replace( key( $state['nowiki'] ), $content, $text );
401 }
402
403 global $wgRawHtml;
404 if ($wgRawHtml) {
405 for ( $content = end($state['html']); $content !== false; $content = prev( $state['html'] ) ) {
406 $text = str_replace( key( $state['html'] ), $content, $text );
407 }
408 }
409
410 return $text;
411 }
412
413 /**
414 * Add an item to the strip state
415 * Returns the unique tag which must be inserted into the stripped text
416 * The tag will be replaced with the original text in unstrip()
417 *
418 * @access private
419 */
420 function insertStripItem( $text, &$state ) {
421 $rnd = UNIQ_PREFIX . '-item' . Parser::getRandomString();
422 if ( !$state ) {
423 $state = array(
424 'html' => array(),
425 'nowiki' => array(),
426 'math' => array(),
427 'pre' => array()
428 );
429 }
430 $state['item'][$rnd] = $text;
431 return $rnd;
432 }
433
434 /**
435 * Return allowed HTML attributes
436 *
437 * @access private
438 */
439 function getHTMLattrs () {
440 $htmlattrs = array( # Allowed attributes--no scripting, etc.
441 'title', 'align', 'lang', 'dir', 'width', 'height',
442 'bgcolor', 'clear', /* BR */ 'noshade', /* HR */
443 'cite', /* BLOCKQUOTE, Q */ 'size', 'face', 'color',
444 /* FONT */ 'type', 'start', 'value', 'compact',
445 /* For various lists, mostly deprecated but safe */
446 'summary', 'width', 'border', 'frame', 'rules',
447 'cellspacing', 'cellpadding', 'valign', 'char',
448 'charoff', 'colgroup', 'col', 'span', 'abbr', 'axis',
449 'headers', 'scope', 'rowspan', 'colspan', /* Tables */
450 'id', 'class', 'name', 'style' /* For CSS */
451 );
452 return $htmlattrs ;
453 }
454
455 /**
456 * Remove non approved attributes and javascript in css
457 *
458 * @access private
459 */
460 function fixTagAttributes ( $t ) {
461 if ( trim ( $t ) == '' ) return '' ; # Saves runtime ;-)
462 $htmlattrs = $this->getHTMLattrs() ;
463
464 # Strip non-approved attributes from the tag
465 $t = preg_replace(
466 '/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e',
467 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
468 $t);
469
470 $t = str_replace ( '<></>' , '' , $t ) ; # This should fix bug 980557
471
472 # Strip javascript "expression" from stylesheets. Brute force approach:
473 # If anythin offensive is found, all attributes of the HTML tag are dropped
474
475 if( preg_match(
476 '/style\\s*=.*(expression|tps*:\/\/|url\\s*\().*/is',
477 wfMungeToUtf8( $t ) ) )
478 {
479 $t='';
480 }
481
482 return trim ( $t ) ;
483 }
484
485 /**
486 * interface with html tidy, used if $wgUseTidy = true
487 *
488 * @access private
489 */
490 function tidy ( $text ) {
491 global $wgTidyConf, $wgTidyBin, $wgTidyOpts;
492 global $wgInputEncoding, $wgOutputEncoding;
493 $fname = 'Parser::tidy';
494 wfProfileIn( $fname );
495
496 $cleansource = '';
497 switch(strtoupper($wgOutputEncoding)) {
498 case 'ISO-8859-1':
499 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -latin1':' -raw';
500 break;
501 case 'UTF-8':
502 $wgTidyOpts .= ($wgInputEncoding == $wgOutputEncoding)? ' -utf8':' -raw';
503 break;
504 default:
505 $wgTidyOpts .= ' -raw';
506 }
507
508 $wrappedtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.
509 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>'.
510 '<head><title>test</title></head><body>'.$text.'</body></html>';
511 $descriptorspec = array(
512 0 => array('pipe', 'r'),
513 1 => array('pipe', 'w'),
514 2 => array('file', '/dev/null', 'a')
515 );
516 $process = proc_open("$wgTidyBin -config $wgTidyConf $wgTidyOpts", $descriptorspec, $pipes);
517 if (is_resource($process)) {
518 fwrite($pipes[0], $wrappedtext);
519 fclose($pipes[0]);
520 while (!feof($pipes[1])) {
521 $cleansource .= fgets($pipes[1], 1024);
522 }
523 fclose($pipes[1]);
524 $return_value = proc_close($process);
525 }
526
527 wfProfileOut( $fname );
528
529 if( $cleansource == '' && $text != '') {
530 wfDebug( "Tidy error detected!\n" );
531 return $text . "\n<!-- Tidy found serious XHTML errors -->\n";
532 } else {
533 return $cleansource;
534 }
535 }
536
537 /**
538 * parse the wiki syntax used to render tables
539 *
540 * @access private
541 */
542 function doTableStuff ( $t ) {
543 $fname = 'Parser::doTableStuff';
544 wfProfileIn( $fname );
545
546 $t = explode ( "\n" , $t ) ;
547 $td = array () ; # Is currently a td tag open?
548 $ltd = array () ; # Was it TD or TH?
549 $tr = array () ; # Is currently a tr tag open?
550 $ltr = array () ; # tr attributes
551 $indent_level = 0; # indent level of the table
552 foreach ( $t AS $k => $x )
553 {
554 $x = trim ( $x ) ;
555 $fc = substr ( $x , 0 , 1 ) ;
556 if ( preg_match( '/^(:*)\{\|(.*)$/', $x, $matches ) ) {
557 $indent_level = strlen( $matches[1] );
558 $t[$k] = "\n" .
559 str_repeat( '<dl><dd>', $indent_level ) .
560 '<table ' . $this->fixTagAttributes ( $matches[2] ) . '>' ;
561 array_push ( $td , false ) ;
562 array_push ( $ltd , '' ) ;
563 array_push ( $tr , false ) ;
564 array_push ( $ltr , '' ) ;
565 }
566 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
567 else if ( '|}' == substr ( $x , 0 , 2 ) ) {
568 $z = "</table>\n" ;
569 $l = array_pop ( $ltd ) ;
570 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
571 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
572 array_pop ( $ltr ) ;
573 $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
574 }
575 else if ( '|-' == substr ( $x , 0 , 2 ) ) { # Allows for |---------------
576 $x = substr ( $x , 1 ) ;
577 while ( $x != '' && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
578 $z = '' ;
579 $l = array_pop ( $ltd ) ;
580 if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
581 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
582 array_pop ( $ltr ) ;
583 $t[$k] = $z ;
584 array_push ( $tr , false ) ;
585 array_push ( $td , false ) ;
586 array_push ( $ltd , '' ) ;
587 array_push ( $ltr , $this->fixTagAttributes ( $x ) ) ;
588 }
589 else if ( '|' == $fc || '!' == $fc || '|+' == substr ( $x , 0 , 2 ) ) { # Caption
590 if ( '|+' == substr ( $x , 0 , 2 ) ) {
591 $fc = '+' ;
592 $x = substr ( $x , 1 ) ;
593 }
594 $after = substr ( $x , 1 ) ;
595 if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
596 $after = explode ( '||' , $after ) ;
597 $t[$k] = '' ;
598 foreach ( $after AS $theline )
599 {
600 $z = '' ;
601 if ( $fc != '+' )
602 {
603 $tra = array_pop ( $ltr ) ;
604 if ( !array_pop ( $tr ) ) $z = '<tr '.$tra.">\n" ;
605 array_push ( $tr , true ) ;
606 array_push ( $ltr , '' ) ;
607 }
608
609 $l = array_pop ( $ltd ) ;
610 if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
611 if ( $fc == '|' ) $l = 'td' ;
612 else if ( $fc == '!' ) $l = 'th' ;
613 else if ( $fc == '+' ) $l = 'caption' ;
614 else $l = '' ;
615 array_push ( $ltd , $l ) ;
616 $y = explode ( '|' , $theline , 2 ) ;
617 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
618 else $y = $y = "{$z}<{$l} ".$this->fixTagAttributes($y[0]).">{$y[1]}" ;
619 $t[$k] .= $y ;
620 array_push ( $td , true ) ;
621 }
622 }
623 }
624
625 # Closing open td, tr && table
626 while ( count ( $td ) > 0 )
627 {
628 if ( array_pop ( $td ) ) $t[] = '</td>' ;
629 if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
630 $t[] = '</table>' ;
631 }
632
633 $t = implode ( "\n" , $t ) ;
634 # $t = $this->removeHTMLtags( $t );
635 wfProfileOut( $fname );
636 return $t ;
637 }
638
639 /**
640 * Helper function for parse() that transforms wiki markup into
641 * HTML. Only called for $mOutputType == OT_HTML.
642 *
643 * @access private
644 */
645 function internalParse( $text, $linestart, $args = array(), $isMain=true ) {
646 global $wgLang;
647
648 $fname = 'Parser::internalParse';
649 wfProfileIn( $fname );
650
651 $text = $this->removeHTMLtags( $text );
652 $text = $this->replaceVariables( $text, $args );
653
654 $text = $wgLang->convert($text);
655
656 $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
657
658 $text = $this->doHeadings( $text );
659 if($this->mOptions->getUseDynamicDates()) {
660 global $wgDateFormatter;
661 $text = $wgDateFormatter->reformat( $this->mOptions->getDateFormat(), $text );
662 }
663 $text = $this->doAllQuotes( $text );
664 $text = $this->replaceExternalLinks( $text );
665 $text = $this->doMagicLinks( $text );
666 $text = $this->replaceInternalLinks ( $text );
667 # Another call to replace links and images inside captions of images
668 $text = $this->replaceInternalLinks ( $text );
669
670 $text = $this->unstrip( $text, $this->mStripState );
671 $text = $this->unstripNoWiki( $text, $this->mStripState );
672
673 $text = $this->doTableStuff( $text );
674 $text = $this->formatHeadings( $text, $isMain );
675 $sk =& $this->mOptions->getSkin();
676 $text = $sk->transformContent( $text );
677
678 wfProfileOut( $fname );
679 return $text;
680 }
681
682 /**
683 * Replace special strings like "ISBN xxx" and "RFC xxx" with
684 * magic external links.
685 *
686 * @access private
687 */
688 function &doMagicLinks( &$text ) {
689 global $wgUseGeoMode;
690 $text = $this->magicISBN( $text );
691 if ( isset( $wgUseGeoMode ) && $wgUseGeoMode ) {
692 $text = $this->magicGEO( $text );
693 }
694 $text = $this->magicRFC( $text );
695 return $text;
696 }
697
698 /**
699 * Parse ^^ tokens and return html
700 *
701 * @access private
702 */
703 function doExponent ( $text ) {
704 $fname = 'Parser::doExponent';
705 wfProfileIn( $fname);
706 $text = preg_replace('/\^\^(.*)\^\^/','<small><sup>\\1</sup></small>', $text);
707 wfProfileOut( $fname);
708 return $text;
709 }
710
711 /**
712 * Parse headers and return html
713 *
714 * @access private
715 */
716 function doHeadings( $text ) {
717 $fname = 'Parser::doHeadings';
718 wfProfileIn( $fname );
719 for ( $i = 6; $i >= 1; --$i ) {
720 $h = substr( '======', 0, $i );
721 $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
722 "<h{$i}>\\1</h{$i}>\\2", $text );
723 }
724 wfProfileOut( $fname );
725 return $text;
726 }
727
728 /**
729 * Replace single quotes with HTML markup
730 * @access private
731 * @return string the altered text
732 */
733 function doAllQuotes( $text ) {
734 $fname = 'Parser::doAllQuotes';
735 wfProfileIn( $fname );
736 $outtext = '';
737 $lines = explode( "\n", $text );
738 foreach ( $lines as $line ) {
739 $outtext .= $this->doQuotes ( $line ) . "\n";
740 }
741 $outtext = substr($outtext, 0,-1);
742 wfProfileOut( $fname );
743 return $outtext;
744 }
745
746 /**
747 * Helper function for doAllQuotes()
748 * @access private
749 */
750 function doQuotes( $text ) {
751 $arr = preg_split ("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
752 if (count ($arr) == 1)
753 return $text;
754 else
755 {
756 # First, do some preliminary work. This may shift some apostrophes from
757 # being mark-up to being text. It also counts the number of occurrences
758 # of bold and italics mark-ups.
759 $i = 0;
760 $numbold = 0;
761 $numitalics = 0;
762 foreach ($arr as $r)
763 {
764 if (($i % 2) == 1)
765 {
766 # If there are ever four apostrophes, assume the first is supposed to
767 # be text, and the remaining three constitute mark-up for bold text.
768 if (strlen ($arr[$i]) == 4)
769 {
770 $arr[$i-1] .= "'";
771 $arr[$i] = "'''";
772 }
773 # If there are more than 5 apostrophes in a row, assume they're all
774 # text except for the last 5.
775 else if (strlen ($arr[$i]) > 5)
776 {
777 $arr[$i-1] .= str_repeat ("'", strlen ($arr[$i]) - 5);
778 $arr[$i] = "'''''";
779 }
780 # Count the number of occurrences of bold and italics mark-ups.
781 # We are not counting sequences of five apostrophes.
782 if (strlen ($arr[$i]) == 2) $numitalics++; else
783 if (strlen ($arr[$i]) == 3) $numbold++; else
784 if (strlen ($arr[$i]) == 5) { $numitalics++; $numbold++; }
785 }
786 $i++;
787 }
788
789 # If there is an odd number of both bold and italics, it is likely
790 # that one of the bold ones was meant to be an apostrophe followed
791 # by italics. Which one we cannot know for certain, but it is more
792 # likely to be one that has a single-letter word before it.
793 if (($numbold % 2 == 1) && ($numitalics % 2 == 1))
794 {
795 $i = 0;
796 $firstsingleletterword = -1;
797 $firstmultiletterword = -1;
798 $firstspace = -1;
799 foreach ($arr as $r)
800 {
801 if (($i % 2 == 1) and (strlen ($r) == 3))
802 {
803 $x1 = substr ($arr[$i-1], -1);
804 $x2 = substr ($arr[$i-1], -2, 1);
805 if ($x1 == ' ') {
806 if ($firstspace == -1) $firstspace = $i;
807 } else if ($x2 == ' ') {
808 if ($firstsingleletterword == -1) $firstsingleletterword = $i;
809 } else {
810 if ($firstmultiletterword == -1) $firstmultiletterword = $i;
811 }
812 }
813 $i++;
814 }
815
816 # If there is a single-letter word, use it!
817 if ($firstsingleletterword > -1)
818 {
819 $arr [ $firstsingleletterword ] = "''";
820 $arr [ $firstsingleletterword-1 ] .= "'";
821 }
822 # If not, but there's a multi-letter word, use that one.
823 else if ($firstmultiletterword > -1)
824 {
825 $arr [ $firstmultiletterword ] = "''";
826 $arr [ $firstmultiletterword-1 ] .= "'";
827 }
828 # ... otherwise use the first one that has neither.
829 # (notice that it is possible for all three to be -1 if, for example,
830 # there is only one pentuple-apostrophe in the line)
831 else if ($firstspace > -1)
832 {
833 $arr [ $firstspace ] = "''";
834 $arr [ $firstspace-1 ] .= "'";
835 }
836 }
837
838 # Now let's actually convert our apostrophic mush to HTML!
839 $output = '';
840 $buffer = '';
841 $state = '';
842 $i = 0;
843 foreach ($arr as $r)
844 {
845 if (($i % 2) == 0)
846 {
847 if ($state == 'both')
848 $buffer .= $r;
849 else
850 $output .= $r;
851 }
852 else
853 {
854 if (strlen ($r) == 2)
855 {
856 if ($state == 'i')
857 { $output .= '</i>'; $state = ''; }
858 else if ($state == 'bi')
859 { $output .= '</i>'; $state = 'b'; }
860 else if ($state == 'ib')
861 { $output .= '</b></i><b>'; $state = 'b'; }
862 else if ($state == 'both')
863 { $output .= '<b><i>'.$buffer.'</i>'; $state = 'b'; }
864 else # $state can be 'b' or ''
865 { $output .= '<i>'; $state .= 'i'; }
866 }
867 else if (strlen ($r) == 3)
868 {
869 if ($state == 'b')
870 { $output .= '</b>'; $state = ''; }
871 else if ($state == 'bi')
872 { $output .= '</i></b><i>'; $state = 'i'; }
873 else if ($state == 'ib')
874 { $output .= '</b>'; $state = 'i'; }
875 else if ($state == 'both')
876 { $output .= '<i><b>'.$buffer.'</b>'; $state = 'i'; }
877 else # $state can be 'i' or ''
878 { $output .= '<b>'; $state .= 'b'; }
879 }
880 else if (strlen ($r) == 5)
881 {
882 if ($state == 'b')
883 { $output .= '</b><i>'; $state = 'i'; }
884 else if ($state == 'i')
885 { $output .= '</i><b>'; $state = 'b'; }
886 else if ($state == 'bi')
887 { $output .= '</i></b>'; $state = ''; }
888 else if ($state == 'ib')
889 { $output .= '</b></i>'; $state = ''; }
890 else if ($state == 'both')
891 { $output .= '<i><b>'.$buffer.'</b></i>'; $state = ''; }
892 else # ($state == '')
893 { $buffer = ''; $state = 'both'; }
894 }
895 }
896 $i++;
897 }
898 # Now close all remaining tags. Notice that the order is important.
899 if ($state == 'b' || $state == 'ib')
900 $output .= '</b>';
901 if ($state == 'i' || $state == 'bi' || $state == 'ib')
902 $output .= '</i>';
903 if ($state == 'bi')
904 $output .= '</b>';
905 if ($state == 'both')
906 $output .= '<b><i>'.$buffer.'</i></b>';
907 return $output;
908 }
909 }
910
911 /**
912 * Replace external links
913 *
914 * Note: we have to do external links before the internal ones,
915 * and otherwise take great care in the order of things here, so
916 * that we don't end up interpreting some URLs twice.
917 *
918 * @access private
919 */
920 function replaceExternalLinks( $text ) {
921 $fname = 'Parser::replaceExternalLinks';
922 wfProfileIn( $fname );
923
924 $sk =& $this->mOptions->getSkin();
925 $linktrail = wfMsg('linktrail');
926 $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
927
928 $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
929
930 $i = 0;
931 while ( $i<count( $bits ) ) {
932 $url = $bits[$i++];
933 $protocol = $bits[$i++];
934 $text = $bits[$i++];
935 $trail = $bits[$i++];
936
937 # If the link text is an image URL, replace it with an <img> tag
938 # This happened by accident in the original parser, but some people used it extensively
939 $img = $this->maybeMakeImageLink( $text );
940 if ( $img !== false ) {
941 $text = $img;
942 }
943
944 $dtrail = '';
945
946 # No link text, e.g. [http://domain.tld/some.link]
947 if ( $text == '' ) {
948 # Autonumber if allowed
949 if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
950 $text = '[' . ++$this->mAutonumber . ']';
951 } else {
952 # Otherwise just use the URL
953 $text = htmlspecialchars( $url );
954 }
955 } else {
956 # Have link text, e.g. [http://domain.tld/some.link text]s
957 # Check for trail
958 if ( preg_match( $linktrail, $trail, $m2 ) ) {
959 $dtrail = $m2[1];
960 $trail = $m2[2];
961 }
962 }
963
964 $encUrl = htmlspecialchars( $url );
965 # Bit in parentheses showing the URL for the printable version
966 if( $url == $text || preg_match( "!$protocol://" . preg_quote( $text, '/' ) . "/?$!", $url ) ) {
967 $paren = '';
968 } else {
969 # Expand the URL for printable version
970 if ( ! $sk->suppressUrlExpansion() ) {
971 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $encUrl ) . "</i>)</span>";
972 } else {
973 $paren = '';
974 }
975 }
976
977 # Process the trail (i.e. everything after this link up until start of the next link),
978 # replacing any non-bracketed links
979 $trail = $this->replaceFreeExternalLinks( $trail );
980
981 $la = $sk->getExternalLinkAttributes( $url, $text );
982
983 # Use the encoded URL
984 # This means that users can paste URLs directly into the text
985 # Funny characters like &ouml; aren't valid in URLs anyway
986 # This was changed in August 2004
987 $s .= "<a href=\"{$url}\" {$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
988 }
989
990 wfProfileOut( $fname );
991 return $s;
992 }
993
994 /**
995 * Replace anything that looks like a URL with a link
996 * @access private
997 */
998 function replaceFreeExternalLinks( $text ) {
999 $bits = preg_split( '/((?:'.URL_PROTOCOLS.'):)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
1000 $s = array_shift( $bits );
1001 $i = 0;
1002
1003 $sk =& $this->mOptions->getSkin();
1004
1005 while ( $i < count( $bits ) ){
1006 $protocol = $bits[$i++];
1007 $remainder = $bits[$i++];
1008
1009 if ( preg_match( '/^('.EXT_LINK_URL_CLASS.'+)(.*)$/s', $remainder, $m ) ) {
1010 # Found some characters after the protocol that look promising
1011 $url = $protocol . $m[1];
1012 $trail = $m[2];
1013
1014 # Move trailing punctuation to $trail
1015 $sep = ',;\.:!?';
1016 # If there is no left bracket, then consider right brackets fair game too
1017 if ( strpos( $url, '(' ) === false ) {
1018 $sep .= ')';
1019 }
1020
1021 $numSepChars = strspn( strrev( $url ), $sep );
1022 if ( $numSepChars ) {
1023 $trail = substr( $url, -$numSepChars ) . $trail;
1024 $url = substr( $url, 0, -$numSepChars );
1025 }
1026
1027 # Replace &amp; from obsolete syntax with &
1028 $url = str_replace( '&amp;', '&', $url );
1029
1030 # Is this an external image?
1031 $text = $this->maybeMakeImageLink( $url );
1032 if ( $text === false ) {
1033 # Not an image, make a link
1034 $text = $sk->makeExternalLink( $url, $url );
1035 }
1036 $s .= $text . $trail;
1037 } else {
1038 $s .= $protocol . $remainder;
1039 }
1040 }
1041 return $s;
1042 }
1043
1044 /**
1045 * make an image if it's allowed
1046 * @access private
1047 */
1048 function maybeMakeImageLink( $url ) {
1049 $sk =& $this->mOptions->getSkin();
1050 $text = false;
1051 if ( $this->mOptions->getAllowExternalImages() ) {
1052 if ( preg_match( EXT_IMAGE_REGEX, $url ) ) {
1053 # Image found
1054 $text = $sk->makeImage( htmlspecialchars( $url ) );
1055 }
1056 }
1057 return $text;
1058 }
1059
1060 /**
1061 * Process [[ ]] wikilinks
1062 *
1063 * @access private
1064 */
1065 function replaceInternalLinks( $s ) {
1066 global $wgLang, $wgLinkCache;
1067 global $wgNamespacesWithSubpages;
1068 static $fname = 'Parser::replaceInternalLinks' ;
1069 wfProfileIn( $fname );
1070
1071 wfProfileIn( $fname.'-setup' );
1072 static $tc = FALSE;
1073 # the % is needed to support urlencoded titles as well
1074 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1075 $sk =& $this->mOptions->getSkin();
1076
1077 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1078
1079 $a = explode( '[[', ' ' . $s );
1080 $s = array_shift( $a );
1081 $s = substr( $s, 1 );
1082
1083 # Match a link having the form [[namespace:link|alternate]]trail
1084 static $e1 = FALSE;
1085 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1086 # Match the end of a line for a word that's not followed by whitespace,
1087 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1088 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1089
1090 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1091 # Special and Media are pseudo-namespaces; no pages actually exist in them
1092
1093 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1094
1095 if ( $useLinkPrefixExtension ) {
1096 if ( preg_match( $e2, $s, $m ) ) {
1097 $first_prefix = $m[2];
1098 $s = $m[1];
1099 } else {
1100 $first_prefix = false;
1101 }
1102 } else {
1103 $prefix = '';
1104 }
1105
1106 wfProfileOut( $fname.'-setup' );
1107
1108 # start procedeeding each line
1109 foreach ( $a as $line ) {
1110 wfProfileIn( $fname.'-prefixhandling' );
1111 if ( $useLinkPrefixExtension ) {
1112 if ( preg_match( $e2, $s, $m ) ) {
1113 $prefix = $m[2];
1114 $s = $m[1];
1115 } else {
1116 $prefix='';
1117 }
1118 # first link
1119 if($first_prefix) {
1120 $prefix = $first_prefix;
1121 $first_prefix = false;
1122 }
1123 }
1124 wfProfileOut( $fname.'-prefixhandling' );
1125
1126 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1127 $text = $m[2];
1128 # fix up urlencoded title texts
1129 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1130 $trail = $m[3];
1131 } else { # Invalid form; output directly
1132 $s .= $prefix . '[[' . $line ;
1133 continue;
1134 }
1135
1136 # Valid link forms:
1137 # Foobar -- normal
1138 # :Foobar -- override special treatment of prefix (images, language links)
1139 # /Foobar -- convert to CurrentPage/Foobar
1140 # /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1141
1142 # Look at the first character
1143 $c = substr($m[1],0,1);
1144 $noforce = ($c != ':');
1145
1146 # subpage
1147 if( $c == '/' ) {
1148 # / at end means we don't want the slash to be shown
1149 if(substr($m[1],-1,1)=='/') {
1150 $m[1]=substr($m[1],1,strlen($m[1])-2);
1151 $noslash=$m[1];
1152 } else {
1153 $noslash=substr($m[1],1);
1154 }
1155
1156 # Some namespaces don't allow subpages
1157 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) {
1158 # subpages allowed here
1159 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1160 if( '' == $text ) {
1161 $text= $m[1];
1162 } # this might be changed for ugliness reasons
1163 } else {
1164 # no subpage allowed, use standard link
1165 $link = $noslash;
1166 }
1167
1168 } elseif( $noforce ) { # no subpage
1169 $link = $m[1];
1170 } else {
1171 # We don't want to keep the first character
1172 $link = substr( $m[1], 1 );
1173 }
1174
1175 $wasblank = ( '' == $text );
1176 if( $wasblank ) $text = $link;
1177
1178 $nt = Title::newFromText( $link );
1179 if( !$nt ) {
1180 $s .= $prefix . '[[' . $line;
1181 continue;
1182 }
1183
1184 $ns = $nt->getNamespace();
1185 $iw = $nt->getInterWiki();
1186
1187 # Link not escaped by : , create the various objects
1188 if( $noforce ) {
1189
1190 # Interwikis
1191 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1192 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1193 $tmp = $prefix . $trail ;
1194 $s .= (trim($tmp) == '')? '': $tmp;
1195 continue;
1196 }
1197
1198 if ( $ns == NS_IMAGE ) {
1199 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1200 $wgLinkCache->addImageLinkObj( $nt );
1201 continue;
1202 }
1203
1204 if ( $ns == NS_CATEGORY ) {
1205 $t = $nt->getText() ;
1206 $nnt = Title::newFromText ( Namespace::getCanonicalName(NS_CATEGORY).':'.$t ) ;
1207
1208 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1209 $pPLC=$sk->postParseLinkColour();
1210 $sk->postParseLinkColour( false );
1211 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1212 $sk->postParseLinkColour( $pPLC );
1213 $wgLinkCache->resume();
1214
1215 if ( $wasblank ) {
1216 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
1217 $sortkey = $this->mTitle->getText();
1218 } else {
1219 $sortkey = $this->mTitle->getPrefixedText();
1220 }
1221 } else {
1222 $sortkey = $text;
1223 }
1224 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1225 $this->mOutput->mCategoryLinks[] = $t ;
1226 $s .= $prefix . $trail ;
1227 continue;
1228 }
1229 }
1230
1231 if( ( $nt->getPrefixedText() === $this->mTitle->getPrefixedText() ) &&
1232 ( strpos( $link, '#' ) === FALSE ) ) {
1233 # Self-links are handled specially; generally de-link and change to bold.
1234 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1235 continue;
1236 }
1237
1238 if( $ns == NS_MEDIA ) {
1239 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1240 $wgLinkCache->addImageLinkObj( $nt );
1241 continue;
1242 } elseif( $ns == NS_SPECIAL ) {
1243 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1244 continue;
1245 }
1246 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1247 }
1248 wfProfileOut( $fname );
1249 return $s;
1250 }
1251
1252 /**#@+
1253 * Used by doBlockLevels()
1254 * @access private
1255 */
1256 /* private */ function closeParagraph() {
1257 $result = '';
1258 if ( '' != $this->mLastSection ) {
1259 $result = '</' . $this->mLastSection . ">\n";
1260 }
1261 $this->mInPre = false;
1262 $this->mLastSection = '';
1263 return $result;
1264 }
1265 # getCommon() returns the length of the longest common substring
1266 # of both arguments, starting at the beginning of both.
1267 #
1268 /* private */ function getCommon( $st1, $st2 ) {
1269 $fl = strlen( $st1 );
1270 $shorter = strlen( $st2 );
1271 if ( $fl < $shorter ) { $shorter = $fl; }
1272
1273 for ( $i = 0; $i < $shorter; ++$i ) {
1274 if ( $st1{$i} != $st2{$i} ) { break; }
1275 }
1276 return $i;
1277 }
1278 # These next three functions open, continue, and close the list
1279 # element appropriate to the prefix character passed into them.
1280 #
1281 /* private */ function openList( $char ) {
1282 $result = $this->closeParagraph();
1283
1284 if ( '*' == $char ) { $result .= '<ul><li>'; }
1285 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1286 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1287 else if ( ';' == $char ) {
1288 $result .= '<dl><dt>';
1289 $this->mDTopen = true;
1290 }
1291 else { $result = '<!-- ERR 1 -->'; }
1292
1293 return $result;
1294 }
1295
1296 /* private */ function nextItem( $char ) {
1297 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1298 else if ( ':' == $char || ';' == $char ) {
1299 $close = '</dd>';
1300 if ( $this->mDTopen ) { $close = '</dt>'; }
1301 if ( ';' == $char ) {
1302 $this->mDTopen = true;
1303 return $close . '<dt>';
1304 } else {
1305 $this->mDTopen = false;
1306 return $close . '<dd>';
1307 }
1308 }
1309 return '<!-- ERR 2 -->';
1310 }
1311
1312 /* private */ function closeList( $char ) {
1313 if ( '*' == $char ) { $text = '</li></ul>'; }
1314 else if ( '#' == $char ) { $text = '</li></ol>'; }
1315 else if ( ':' == $char ) {
1316 if ( $this->mDTopen ) {
1317 $this->mDTopen = false;
1318 $text = '</dt></dl>';
1319 } else {
1320 $text = '</dd></dl>';
1321 }
1322 }
1323 else { return '<!-- ERR 3 -->'; }
1324 return $text."\n";
1325 }
1326 /**#@-*/
1327
1328 /**
1329 * Make lists from lines starting with ':', '*', '#', etc.
1330 *
1331 * @access private
1332 * @return string the lists rendered as HTML
1333 */
1334 function doBlockLevels( $text, $linestart ) {
1335 $fname = 'Parser::doBlockLevels';
1336 wfProfileIn( $fname );
1337
1338 # Parsing through the text line by line. The main thing
1339 # happening here is handling of block-level elements p, pre,
1340 # and making lists from lines starting with * # : etc.
1341 #
1342 $textLines = explode( "\n", $text );
1343
1344 $lastPrefix = $output = $lastLine = '';
1345 $this->mDTopen = $inBlockElem = false;
1346 $prefixLength = 0;
1347 $paragraphStack = false;
1348
1349 if ( !$linestart ) {
1350 $output .= array_shift( $textLines );
1351 }
1352 foreach ( $textLines as $oLine ) {
1353 $lastPrefixLength = strlen( $lastPrefix );
1354 $preCloseMatch = preg_match('/<\\/pre/i', $oLine );
1355 $preOpenMatch = preg_match('/<pre/i', $oLine );
1356 if ( !$this->mInPre ) {
1357 # Multiple prefixes may abut each other for nested lists.
1358 $prefixLength = strspn( $oLine, '*#:;' );
1359 $pref = substr( $oLine, 0, $prefixLength );
1360
1361 # eh?
1362 $pref2 = str_replace( ';', ':', $pref );
1363 $t = substr( $oLine, $prefixLength );
1364 $this->mInPre = !empty($preOpenMatch);
1365 } else {
1366 # Don't interpret any other prefixes in preformatted text
1367 $prefixLength = 0;
1368 $pref = $pref2 = '';
1369 $t = $oLine;
1370 }
1371
1372 # List generation
1373 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1374 # Same as the last item, so no need to deal with nesting or opening stuff
1375 $output .= $this->nextItem( substr( $pref, -1 ) );
1376 $paragraphStack = false;
1377
1378 if ( substr( $pref, -1 ) == ';') {
1379 # The one nasty exception: definition lists work like this:
1380 # ; title : definition text
1381 # So we check for : in the remainder text to split up the
1382 # title and definition, without b0rking links.
1383 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1384 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1385 $term = $match[1];
1386 $output .= $term . $this->nextItem( ':' );
1387 $t = $match[2];
1388 }
1389 }
1390 } elseif( $prefixLength || $lastPrefixLength ) {
1391 # Either open or close a level...
1392 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1393 $paragraphStack = false;
1394
1395 while( $commonPrefixLength < $lastPrefixLength ) {
1396 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1397 --$lastPrefixLength;
1398 }
1399 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1400 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1401 }
1402 while ( $prefixLength > $commonPrefixLength ) {
1403 $char = substr( $pref, $commonPrefixLength, 1 );
1404 $output .= $this->openList( $char );
1405
1406 if ( ';' == $char ) {
1407 # FIXME: This is dupe of code above
1408 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1409 $term = $match[1];
1410 $output .= $term . $this->nextItem( ':' );
1411 $t = $match[2];
1412 }
1413 }
1414 ++$commonPrefixLength;
1415 }
1416 $lastPrefix = $pref2;
1417 }
1418 if( 0 == $prefixLength ) {
1419 # No prefix (not in list)--go to paragraph mode
1420 $uniq_prefix = UNIQ_PREFIX;
1421 // XXX: use a stack for nestable elements like span, table and div
1422 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1423 $closematch = preg_match(
1424 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1425 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1426 if ( $openmatch or $closematch ) {
1427 $paragraphStack = false;
1428 $output .= $this->closeParagraph();
1429 if($preOpenMatch and !$preCloseMatch) {
1430 $this->mInPre = true;
1431 }
1432 if ( $closematch ) {
1433 $inBlockElem = false;
1434 } else {
1435 $inBlockElem = true;
1436 }
1437 } else if ( !$inBlockElem && !$this->mInPre ) {
1438 if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1439 // pre
1440 if ($this->mLastSection != 'pre') {
1441 $paragraphStack = false;
1442 $output .= $this->closeParagraph().'<pre>';
1443 $this->mLastSection = 'pre';
1444 }
1445 $t = substr( $t, 1 );
1446 } else {
1447 // paragraph
1448 if ( '' == trim($t) ) {
1449 if ( $paragraphStack ) {
1450 $output .= $paragraphStack.'<br />';
1451 $paragraphStack = false;
1452 $this->mLastSection = 'p';
1453 } else {
1454 if ($this->mLastSection != 'p' ) {
1455 $output .= $this->closeParagraph();
1456 $this->mLastSection = '';
1457 $paragraphStack = '<p>';
1458 } else {
1459 $paragraphStack = '</p><p>';
1460 }
1461 }
1462 } else {
1463 if ( $paragraphStack ) {
1464 $output .= $paragraphStack;
1465 $paragraphStack = false;
1466 $this->mLastSection = 'p';
1467 } else if ($this->mLastSection != 'p') {
1468 $output .= $this->closeParagraph().'<p>';
1469 $this->mLastSection = 'p';
1470 }
1471 }
1472 }
1473 }
1474 }
1475 if ($paragraphStack === false) {
1476 $output .= $t."\n";
1477 }
1478 }
1479 while ( $prefixLength ) {
1480 $output .= $this->closeList( $pref2{$prefixLength-1} );
1481 --$prefixLength;
1482 }
1483 if ( '' != $this->mLastSection ) {
1484 $output .= '</' . $this->mLastSection . '>';
1485 $this->mLastSection = '';
1486 }
1487
1488 wfProfileOut( $fname );
1489 return $output;
1490 }
1491
1492 /**
1493 * Return value of a magic variable (like PAGENAME)
1494 *
1495 * @access private
1496 */
1497 function getVariableValue( $index ) {
1498 global $wgLang, $wgSitename, $wgServer;
1499
1500 switch ( $index ) {
1501 case MAG_CURRENTMONTH:
1502 return $wgLang->formatNum( date( 'm' ) );
1503 case MAG_CURRENTMONTHNAME:
1504 return $wgLang->getMonthName( date('n') );
1505 case MAG_CURRENTMONTHNAMEGEN:
1506 return $wgLang->getMonthNameGen( date('n') );
1507 case MAG_CURRENTDAY:
1508 return $wgLang->formatNum( date('j') );
1509 case MAG_PAGENAME:
1510 return $this->mTitle->getText();
1511 case MAG_PAGENAMEE:
1512 return $this->mTitle->getPartialURL();
1513 case MAG_NAMESPACE:
1514 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1515 return $wgLang->getNsText($this->mTitle->getNamespace()); # Patch by Dori
1516 case MAG_CURRENTDAYNAME:
1517 return $wgLang->getWeekdayName( date('w')+1 );
1518 case MAG_CURRENTYEAR:
1519 return $wgLang->formatNum( date( 'Y' ) );
1520 case MAG_CURRENTTIME:
1521 return $wgLang->time( wfTimestampNow(), false );
1522 case MAG_NUMBEROFARTICLES:
1523 return $wgLang->formatNum( wfNumberOfArticles() );
1524 case MAG_SITENAME:
1525 return $wgSitename;
1526 case MAG_SERVER:
1527 return $wgServer;
1528 default:
1529 return NULL;
1530 }
1531 }
1532
1533 /**
1534 * initialise the magic variables (like CURRENTMONTHNAME)
1535 *
1536 * @access private
1537 */
1538 function initialiseVariables() {
1539 global $wgVariableIDs;
1540 $this->mVariables = array();
1541 foreach ( $wgVariableIDs as $id ) {
1542 $mw =& MagicWord::get( $id );
1543 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1544 }
1545 }
1546
1547 /**
1548 * Replace magic variables, templates, and template arguments
1549 * with the appropriate text. Templates are substituted recursively,
1550 * taking care to avoid infinite loops.
1551 *
1552 * Note that the substitution depends on value of $mOutputType:
1553 * OT_WIKI: only {{subst:}} templates
1554 * OT_MSG: only magic variables
1555 * OT_HTML: all templates and magic variables
1556 *
1557 * @param string $tex The text to transform
1558 * @param array $args Key-value pairs representing template parameters to substitute
1559 * @access private
1560 */
1561 function replaceVariables( $text, $args = array() ) {
1562 global $wgLang, $wgScript, $wgArticlePath;
1563
1564 # Prevent too big inclusions
1565 if(strlen($text)> MAX_INCLUDE_SIZE)
1566 return $text;
1567
1568 $fname = 'Parser::replaceVariables';
1569 wfProfileIn( $fname );
1570
1571 $titleChars = Title::legalChars();
1572
1573 # This function is called recursively. To keep track of arguments we need a stack:
1574 array_push( $this->mArgStack, $args );
1575
1576 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1577 $GLOBALS['wgCurParser'] =& $this;
1578
1579 if ( $this->mOutputType == OT_HTML ) {
1580 # Argument substitution
1581 $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1582 }
1583 # Template substitution
1584 $regex = '/{{(['.$titleChars.']*)(\\|.*?|)}}/s';
1585 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1586
1587 array_pop( $this->mArgStack );
1588
1589 wfProfileOut( $fname );
1590 return $text;
1591 }
1592
1593 # Split template arguments
1594 function getTemplateArgs( $argsString ) {
1595 if ( $argsString === '' ) {
1596 return array();
1597 }
1598
1599 $args = explode( '|', substr( $argsString, 1 ) );
1600
1601 # If any of the arguments contains a '[[' but no ']]', it needs to be
1602 # merged with the next arg because the '|' character between belongs
1603 # to the link syntax and not the template parameter syntax.
1604 $argc = count($args);
1605 $i = 0;
1606 for ( $i = 0; $i < $argc-1; $i++ ) {
1607 if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
1608 $args[$i] .= '|'.$args[$i+1];
1609 array_splice($args, $i+1, 1);
1610 $i--;
1611 $argc--;
1612 }
1613 }
1614
1615 return $args;
1616 }
1617
1618 /**
1619 * Return the text of a template, after recursively
1620 * replacing any variables or templates within the template.
1621 *
1622 * @param array $matches The parts of the template
1623 * $matches[1]: the title, i.e. the part before the |
1624 * $matches[2]: the parameters (including a leading |), if any
1625 * @return string the text of the template
1626 * @access private
1627 */
1628 function braceSubstitution( $matches ) {
1629 global $wgLinkCache, $wgLang;
1630 $fname = 'Parser::braceSubstitution';
1631 $found = false;
1632 $nowiki = false;
1633 $noparse = false;
1634 $itcamefromthedatabase = false;
1635
1636 $title = NULL;
1637
1638 # $part1 is the bit before the first |, and must contain only title characters
1639 # $args is a list of arguments, starting from index 0, not including $part1
1640
1641 $part1 = $matches[1];
1642 # If the second subpattern matched anything, it will start with |
1643
1644 $args = $this->getTemplateArgs($matches[2]);
1645 $argc = count( $args );
1646
1647 # {{{}}}
1648 if ( strpos( $matches[0], '{{{' ) !== false ) {
1649 $text = $matches[0];
1650 $found = true;
1651 $noparse = true;
1652 }
1653
1654 # SUBST
1655 if ( !$found ) {
1656 $mwSubst =& MagicWord::get( MAG_SUBST );
1657 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1658 if ( $this->mOutputType != OT_WIKI ) {
1659 # Invalid SUBST not replaced at PST time
1660 # Return without further processing
1661 $text = $matches[0];
1662 $found = true;
1663 $noparse= true;
1664 }
1665 } elseif ( $this->mOutputType == OT_WIKI ) {
1666 # SUBST not found in PST pass, do nothing
1667 $text = $matches[0];
1668 $found = true;
1669 }
1670 }
1671
1672 # MSG, MSGNW and INT
1673 if ( !$found ) {
1674 # Check for MSGNW:
1675 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1676 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1677 $nowiki = true;
1678 } else {
1679 # Remove obsolete MSG:
1680 $mwMsg =& MagicWord::get( MAG_MSG );
1681 $mwMsg->matchStartAndRemove( $part1 );
1682 }
1683
1684 # Check if it is an internal message
1685 $mwInt =& MagicWord::get( MAG_INT );
1686 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1687 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1688 $text = wfMsgReal( $part1, $args, true );
1689 $found = true;
1690 }
1691 }
1692 }
1693
1694 # NS
1695 if ( !$found ) {
1696 # Check for NS: (namespace expansion)
1697 $mwNs = MagicWord::get( MAG_NS );
1698 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1699 if ( intval( $part1 ) ) {
1700 $text = $wgLang->getNsText( intval( $part1 ) );
1701 $found = true;
1702 } else {
1703 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1704 if ( !is_null( $index ) ) {
1705 $text = $wgLang->getNsText( $index );
1706 $found = true;
1707 }
1708 }
1709 }
1710 }
1711
1712 # LOCALURL and LOCALURLE
1713 if ( !$found ) {
1714 $mwLocal = MagicWord::get( MAG_LOCALURL );
1715 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1716
1717 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1718 $func = 'getLocalURL';
1719 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1720 $func = 'escapeLocalURL';
1721 } else {
1722 $func = '';
1723 }
1724
1725 if ( $func !== '' ) {
1726 $title = Title::newFromText( $part1 );
1727 if ( !is_null( $title ) ) {
1728 if ( $argc > 0 ) {
1729 $text = $title->$func( $args[0] );
1730 } else {
1731 $text = $title->$func();
1732 }
1733 $found = true;
1734 }
1735 }
1736 }
1737
1738 # Internal variables
1739 if ( !$this->mVariables ) {
1740 $this->initialiseVariables();
1741 }
1742 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1743 $text = $this->mVariables[$part1];
1744 $found = true;
1745 $this->mOutput->mContainsOldMagic = true;
1746 }
1747
1748 # GRAMMAR
1749 if ( !$found && $argc == 1 ) {
1750 $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
1751 if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
1752 $text = $wgLang->convertGrammar( $args[0], $part1 );
1753 $found = true;
1754 }
1755 }
1756
1757 # Template table test
1758
1759 # Did we encounter this template already? If yes, it is in the cache
1760 # and we need to check for loops.
1761 if ( isset( $this->mTemplates[$part1] ) ) {
1762 # Infinite loop test
1763 if ( isset( $this->mTemplatePath[$part1] ) ) {
1764 $noparse = true;
1765 $found = true;
1766 }
1767 # set $text to cached message.
1768 $text = $this->mTemplates[$part1];
1769 $found = true;
1770 }
1771
1772 # Load from database
1773 if ( !$found ) {
1774 $title = Title::newFromText( $part1, NS_TEMPLATE );
1775 if ( !is_null( $title ) && !$title->isExternal() ) {
1776 # Check for excessive inclusion
1777 $dbk = $title->getPrefixedDBkey();
1778 if ( $this->incrementIncludeCount( $dbk ) ) {
1779 # This should never be reached.
1780 $article = new Article( $title );
1781 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1782 if ( $articleContent !== false ) {
1783 $found = true;
1784 $text = $articleContent;
1785 $itcamefromthedatabase = true;
1786 }
1787 }
1788
1789 # If the title is valid but undisplayable, make a link to it
1790 if ( $this->mOutputType == OT_HTML && !$found ) {
1791 $text = '[['.$title->getPrefixedText().']]';
1792 $found = true;
1793 }
1794
1795 # Template cache array insertion
1796 $this->mTemplates[$part1] = $text;
1797 }
1798 }
1799
1800 # Recursive parsing, escaping and link table handling
1801 # Only for HTML output
1802 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1803 $text = wfEscapeWikiText( $text );
1804 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1805 # Clean up argument array
1806 $assocArgs = array();
1807 $index = 1;
1808 foreach( $args as $arg ) {
1809 $eqpos = strpos( $arg, '=' );
1810 if ( $eqpos === false ) {
1811 $assocArgs[$index++] = $arg;
1812 } else {
1813 $name = trim( substr( $arg, 0, $eqpos ) );
1814 $value = trim( substr( $arg, $eqpos+1 ) );
1815 if ( $value === false ) {
1816 $value = '';
1817 }
1818 if ( $name !== false ) {
1819 $assocArgs[$name] = $value;
1820 }
1821 }
1822 }
1823
1824 # Do not enter included links in link table
1825 if ( !is_null( $title ) ) {
1826 $wgLinkCache->suspend();
1827 }
1828
1829 # Add a new element to the templace recursion path
1830 $this->mTemplatePath[$part1] = 1;
1831
1832 $text = $this->strip( $text, $this->mStripState );
1833 $text = $this->removeHTMLtags( $text );
1834 $text = $this->replaceVariables( $text, $assocArgs );
1835
1836 # Resume the link cache and register the inclusion as a link
1837 if ( !is_null( $title ) ) {
1838 $wgLinkCache->resume();
1839 $wgLinkCache->addLinkObj( $title );
1840 }
1841 }
1842
1843 # Empties the template path
1844 $this->mTemplatePath = array();
1845 if ( !$found ) {
1846 return $matches[0];
1847 } else {
1848 # replace ==section headers==
1849 # XXX this needs to go away once we have a better parser.
1850 if ( $this->mOutputType != OT_WIKI && $itcamefromthedatabase ) {
1851 if( !is_null( $title ) )
1852 $encodedname = base64_encode($title->getPrefixedDBkey());
1853 else
1854 $encodedname = base64_encode("");
1855 $matches = preg_split('/(^={1,6}.*?={1,6}\s*?$)/m', $text, -1,
1856 PREG_SPLIT_DELIM_CAPTURE);
1857 $text = '';
1858 $nsec = 0;
1859 for( $i = 0; $i < count($matches); $i += 2 ) {
1860 wfDebug("text=[".$matches[$i]."] heading=[".$matches[$i+1]."]\n");
1861 if ($matches[$i] == "" && $matches[$i + 1] == "") break;
1862 $text .= $matches[$i];
1863 $hl = $matches[$i + 1];
1864 if ($hl == "") continue;
1865 if( strstr($hl, "__MWTEMPLATESECTION") ) {
1866 $text .= $hl;
1867 continue;
1868 }
1869 preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
1870 $text .= $m2[1] . $m2[2] . "__MWTEMPLATESECTION="
1871 . $encodedname . "&" . base64_encode("$nsec") . "__" . $m2[3];
1872
1873 $nsec++;
1874 }
1875 }
1876 return $text;
1877 }
1878 }
1879
1880 /**
1881 * Triple brace replacement -- used for template arguments
1882 * @access private
1883 */
1884 function argSubstitution( $matches ) {
1885 $arg = trim( $matches[1] );
1886 $text = $matches[0];
1887 $inputArgs = end( $this->mArgStack );
1888
1889 if ( array_key_exists( $arg, $inputArgs ) ) {
1890 $text = $this->strip( $inputArgs[$arg], $this->mStripState );
1891 $text = $this->removeHTMLtags( $text );
1892 $text = $this->replaceVariables( $text, array() );
1893 }
1894
1895 return $text;
1896 }
1897
1898 /**
1899 * Returns true if the function is allowed to include this entity
1900 * @access private
1901 */
1902 function incrementIncludeCount( $dbk ) {
1903 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1904 $this->mIncludeCount[$dbk] = 0;
1905 }
1906 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1907 return true;
1908 } else {
1909 return false;
1910 }
1911 }
1912
1913
1914 /**
1915 * Cleans up HTML, removes dangerous tags and attributes, and
1916 * removes HTML comments
1917 * @access private
1918 */
1919 function removeHTMLtags( $text ) {
1920 global $wgUseTidy, $wgUserHtml;
1921 $fname = 'Parser::removeHTMLtags';
1922 wfProfileIn( $fname );
1923
1924 if( $wgUserHtml ) {
1925 $htmlpairs = array( # Tags that must be closed
1926 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1927 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1928 'strike', 'strong', 'tt', 'var', 'div', 'center',
1929 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1930 'ruby', 'rt' , 'rb' , 'rp', 'p'
1931 );
1932 $htmlsingle = array(
1933 'br', 'hr', 'li', 'dt', 'dd'
1934 );
1935 $htmlnest = array( # Tags that can be nested--??
1936 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1937 'dl', 'font', 'big', 'small', 'sub', 'sup'
1938 );
1939 $tabletags = array( # Can only appear inside table
1940 'td', 'th', 'tr'
1941 );
1942 } else {
1943 $htmlpairs = array();
1944 $htmlsingle = array();
1945 $htmlnest = array();
1946 $tabletags = array();
1947 }
1948
1949 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1950 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1951
1952 $htmlattrs = $this->getHTMLattrs () ;
1953
1954 # Remove HTML comments
1955 $text = $this->removeHTMLcomments( $text );
1956
1957 $bits = explode( '<', $text );
1958 $text = array_shift( $bits );
1959 if(!$wgUseTidy) {
1960 $tagstack = array(); $tablestack = array();
1961 foreach ( $bits as $x ) {
1962 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1963 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1964 $x, $regs );
1965 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1966 error_reporting( $prev );
1967
1968 $badtag = 0 ;
1969 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1970 # Check our stack
1971 if ( $slash ) {
1972 # Closing a tag...
1973 if ( ! in_array( $t, $htmlsingle ) &&
1974 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1975 @array_push( $tagstack, $ot );
1976 $badtag = 1;
1977 } else {
1978 if ( $t == 'table' ) {
1979 $tagstack = array_pop( $tablestack );
1980 }
1981 $newparams = '';
1982 }
1983 } else {
1984 # Keep track for later
1985 if ( in_array( $t, $tabletags ) &&
1986 ! in_array( 'table', $tagstack ) ) {
1987 $badtag = 1;
1988 } else if ( in_array( $t, $tagstack ) &&
1989 ! in_array ( $t , $htmlnest ) ) {
1990 $badtag = 1 ;
1991 } else if ( ! in_array( $t, $htmlsingle ) ) {
1992 if ( $t == 'table' ) {
1993 array_push( $tablestack, $tagstack );
1994 $tagstack = array();
1995 }
1996 array_push( $tagstack, $t );
1997 }
1998 # Strip non-approved attributes from the tag
1999 $newparams = $this->fixTagAttributes($params);
2000
2001 }
2002 if ( ! $badtag ) {
2003 $rest = str_replace( '>', '&gt;', $rest );
2004 $text .= "<$slash$t $newparams$brace$rest";
2005 continue;
2006 }
2007 }
2008 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2009 }
2010 # Close off any remaining tags
2011 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2012 $text .= "</$t>\n";
2013 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2014 }
2015 } else {
2016 # this might be possible using tidy itself
2017 foreach ( $bits as $x ) {
2018 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2019 $x, $regs );
2020 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2021 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2022 $newparams = $this->fixTagAttributes($params);
2023 $rest = str_replace( '>', '&gt;', $rest );
2024 $text .= "<$slash$t $newparams$brace$rest";
2025 } else {
2026 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2027 }
2028 }
2029 }
2030 wfProfileOut( $fname );
2031 return $text;
2032 }
2033
2034 /**
2035 * Remove '<!--', '-->', and everything between.
2036 * To avoid leaving blank lines, when a comment is both preceded
2037 * and followed by a newline (ignoring spaces), trim leading and
2038 * trailing spaces and one of the newlines.
2039 *
2040 * @access private
2041 */
2042 function removeHTMLcomments( $text ) {
2043 $fname='Parser::removeHTMLcomments';
2044 wfProfileIn( $fname );
2045 while (($start = strpos($text, '<!--')) !== false) {
2046 $end = strpos($text, '-->', $start + 4);
2047 if ($end === false) {
2048 # Unterminated comment; bail out
2049 break;
2050 }
2051
2052 $end += 3;
2053
2054 # Trim space and newline if the comment is both
2055 # preceded and followed by a newline
2056 $spaceStart = max($start - 1, 0);
2057 $spaceLen = $end - $spaceStart;
2058 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
2059 $spaceStart--;
2060 $spaceLen++;
2061 }
2062 while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
2063 $spaceLen++;
2064 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
2065 # Remove the comment, leading and trailing
2066 # spaces, and leave only one newline.
2067 $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
2068 }
2069 else {
2070 # Remove just the comment.
2071 $text = substr_replace($text, '', $start, $end - $start);
2072 }
2073 }
2074 wfProfileOut( $fname );
2075 return $text;
2076 }
2077
2078 /**
2079 * This function accomplishes several tasks:
2080 * 1) Auto-number headings if that option is enabled
2081 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2082 * 3) Add a Table of contents on the top for users who have enabled the option
2083 * 4) Auto-anchor headings
2084 *
2085 * It loops through all headlines, collects the necessary data, then splits up the
2086 * string and re-inserts the newly formatted headlines.
2087 * @access private
2088 */
2089 /* private */ function formatHeadings( $text, $isMain=true ) {
2090 global $wgInputEncoding, $wgMaxTocLevel, $wgLang, $wgLinkHolders;
2091
2092 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2093 $doShowToc = $this->mOptions->getShowToc();
2094 $forceTocHere = false;
2095 if( !$this->mTitle->userCanEdit() ) {
2096 $showEditLink = 0;
2097 $rightClickHack = 0;
2098 } else {
2099 $showEditLink = $this->mOptions->getEditSection();
2100 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2101 }
2102
2103 # Inhibit editsection links if requested in the page
2104 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2105 if( $esw->matchAndRemove( $text ) ) {
2106 $showEditLink = 0;
2107 }
2108 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2109 # do not add TOC
2110 $mw =& MagicWord::get( MAG_NOTOC );
2111 if( $mw->matchAndRemove( $text ) ) {
2112 $doShowToc = 0;
2113 }
2114
2115 # never add the TOC to the Main Page. This is an entry page that should not
2116 # be more than 1-2 screens large anyway
2117 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2118 $doShowToc = 0;
2119 }
2120
2121 # Get all headlines for numbering them and adding funky stuff like [edit]
2122 # links - this is for later, but we need the number of headlines right now
2123 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2124
2125 # if there are fewer than 4 headlines in the article, do not show TOC
2126 if( $numMatches < 4 ) {
2127 $doShowToc = 0;
2128 }
2129
2130 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2131 # override above conditions and always show TOC at that place
2132 $mw =& MagicWord::get( MAG_TOC );
2133 if ($mw->match( $text ) ) {
2134 $doShowToc = 1;
2135 $forceTocHere = true;
2136 } else {
2137 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2138 # override above conditions and always show TOC above first header
2139 $mw =& MagicWord::get( MAG_FORCETOC );
2140 if ($mw->matchAndRemove( $text ) ) {
2141 $doShowToc = 1;
2142 }
2143 }
2144
2145
2146
2147 # We need this to perform operations on the HTML
2148 $sk =& $this->mOptions->getSkin();
2149
2150 # headline counter
2151 $headlineCount = 0;
2152 $sectionCount = 0; # headlineCount excluding template sections
2153
2154 # Ugh .. the TOC should have neat indentation levels which can be
2155 # passed to the skin functions. These are determined here
2156 $toclevel = 0;
2157 $toc = '';
2158 $full = '';
2159 $head = array();
2160 $sublevelCount = array();
2161 $level = 0;
2162 $prevlevel = 0;
2163 foreach( $matches[3] as $headline ) {
2164 $istemplate = 0;
2165 $templatetitle = "";
2166 $templatesection = 0;
2167
2168 if (preg_match("/__MWTEMPLATESECTION=([^&]+)&([^_]+)__/", $headline, $mat)) {
2169 $istemplate = 1;
2170 $templatetitle = base64_decode($mat[1]);
2171 $templatesection = 1 + (int)base64_decode($mat[2]);
2172 $headline = preg_replace("/__MWTEMPLATESECTION=([^&]+)&([^_]+)__/", "", $headline);
2173 }
2174
2175 $numbering = '';
2176 if( $level ) {
2177 $prevlevel = $level;
2178 }
2179 $level = $matches[1][$headlineCount];
2180 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2181 # reset when we enter a new level
2182 $sublevelCount[$level] = 0;
2183 $toc .= $sk->tocIndent( $level - $prevlevel );
2184 $toclevel += $level - $prevlevel;
2185 }
2186 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2187 # reset when we step back a level
2188 $sublevelCount[$level+1]=0;
2189 $toc .= $sk->tocUnindent( $prevlevel - $level );
2190 $toclevel -= $prevlevel - $level;
2191 }
2192 # count number of headlines for each level
2193 @$sublevelCount[$level]++;
2194 if( $doNumberHeadings || $doShowToc ) {
2195 $dot = 0;
2196 for( $i = 1; $i <= $level; $i++ ) {
2197 if( !empty( $sublevelCount[$i] ) ) {
2198 if( $dot ) {
2199 $numbering .= '.';
2200 }
2201 $numbering .= $wgLang->formatNum( $sublevelCount[$i] );
2202 $dot = 1;
2203 }
2204 }
2205 }
2206
2207 # The canonized header is a version of the header text safe to use for links
2208 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2209 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2210 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2211
2212 # Remove link placeholders by the link text.
2213 # <!--LINK number-->
2214 # turns into
2215 # link text with suffix
2216 $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
2217 "\$wgLinkHolders['texts'][\$1]",
2218 $canonized_headline );
2219
2220 # strip out HTML
2221 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2222 $tocline = trim( $canonized_headline );
2223 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2224 $replacearray = array(
2225 '%3A' => ':',
2226 '%' => '.'
2227 );
2228 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2229 $refer[$headlineCount] = $canonized_headline;
2230
2231 # count how many in assoc. array so we can track dupes in anchors
2232 @$refers[$canonized_headline]++;
2233 $refcount[$headlineCount]=$refers[$canonized_headline];
2234
2235 # Prepend the number to the heading text
2236
2237 if( $doNumberHeadings || $doShowToc ) {
2238 $tocline = $numbering . ' ' . $tocline;
2239
2240 # Don't number the heading if it is the only one (looks silly)
2241 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2242 # the two are different if the line contains a link
2243 $headline=$numbering . ' ' . $headline;
2244 }
2245 }
2246
2247 # Create the anchor for linking from the TOC to the section
2248 $anchor = $canonized_headline;
2249 if($refcount[$headlineCount] > 1 ) {
2250 $anchor .= '_' . $refcount[$headlineCount];
2251 }
2252 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2253 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2254 }
2255 if( $showEditLink && ( !$istemplate || $templatetitle !== "" ) ) {
2256 if ( empty( $head[$headlineCount] ) ) {
2257 $head[$headlineCount] = '';
2258 }
2259 if( $istemplate )
2260 $head[$headlineCount] .= $sk->editSectionLinkForOther($templatetitle, $templatesection);
2261 else
2262 $head[$headlineCount] .= $sk->editSectionLink($sectionCount+1);
2263 }
2264
2265 # Add the edit section span
2266 if( $rightClickHack ) {
2267 if( $istemplate )
2268 $headline = $sk->editSectionScriptForOther($templatetitle, $templatesection, $headline);
2269 else
2270 $headline = $sk->editSectionScript($sectionCount+1,$headline);
2271 }
2272
2273 # give headline the correct <h#> tag
2274 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline.'</h'.$level.'>';
2275
2276 $headlineCount++;
2277 if( !$istemplate )
2278 $sectionCount++;
2279 }
2280
2281 if( $doShowToc ) {
2282 $toclines = $headlineCount;
2283 $toc .= $sk->tocUnindent( $toclevel );
2284 $toc = $sk->tocTable( $toc );
2285 }
2286
2287 # split up and insert constructed headlines
2288
2289 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2290 $i = 0;
2291
2292 foreach( $blocks as $block ) {
2293 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2294 # This is the [edit] link that appears for the top block of text when
2295 # section editing is enabled
2296
2297 # Disabled because it broke block formatting
2298 # For example, a bullet point in the top line
2299 # $full .= $sk->editSectionLink(0);
2300 }
2301 $full .= $block;
2302 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2303 # Top anchor now in skin
2304 $full = $full.$toc;
2305 }
2306
2307 if( !empty( $head[$i] ) ) {
2308 $full .= $head[$i];
2309 }
2310 $i++;
2311 }
2312 if($forceTocHere) {
2313 $mw =& MagicWord::get( MAG_TOC );
2314 return $mw->replace( $toc, $full );
2315 } else {
2316 return $full;
2317 }
2318 }
2319
2320 /**
2321 * Return an HTML link for the "ISBN 123456" text
2322 * @access private
2323 */
2324 function magicISBN( $text ) {
2325 global $wgLang;
2326 $fname = 'Parser::magicISBN';
2327 wfProfileIn( $fname );
2328
2329 $a = split( 'ISBN ', ' '.$text );
2330 if ( count ( $a ) < 2 ) {
2331 wfProfileOut( $fname );
2332 return $text;
2333 }
2334 $text = substr( array_shift( $a ), 1);
2335 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2336
2337 foreach ( $a as $x ) {
2338 $isbn = $blank = '' ;
2339 while ( ' ' == $x{0} ) {
2340 $blank .= ' ';
2341 $x = substr( $x, 1 );
2342 }
2343 if ( $x == '' ) { # blank isbn
2344 $text .= "ISBN $blank";
2345 continue;
2346 }
2347 while ( strstr( $valid, $x{0} ) != false ) {
2348 $isbn .= $x{0};
2349 $x = substr( $x, 1 );
2350 }
2351 $num = str_replace( '-', '', $isbn );
2352 $num = str_replace( ' ', '', $num );
2353
2354 if ( '' == $num ) {
2355 $text .= "ISBN $blank$x";
2356 } else {
2357 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2358 $text .= '<a href="' .
2359 $titleObj->escapeLocalUrl( 'isbn='.$num ) .
2360 "\" class=\"internal\">ISBN $isbn</a>";
2361 $text .= $x;
2362 }
2363 }
2364 wfProfileOut( $fname );
2365 return $text;
2366 }
2367
2368 /**
2369 * Return an HTML link for the "GEO ..." text
2370 * @access private
2371 */
2372 function magicGEO( $text ) {
2373 global $wgLang, $wgUseGeoMode;
2374 $fname = 'Parser::magicGEO';
2375 wfProfileIn( $fname );
2376
2377 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2378 $directions = array ( 'N' => 'North' , 'S' => 'South' , 'E' => 'East' , 'W' => 'West' ) ;
2379 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2380 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2381 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2382 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2383
2384 $a = split( 'GEO ', ' '.$text );
2385 if ( count ( $a ) < 2 ) {
2386 wfProfileOut( $fname );
2387 return $text;
2388 }
2389 $text = substr( array_shift( $a ), 1);
2390 $valid = '0123456789.+-:';
2391
2392 foreach ( $a as $x ) {
2393 $geo = $blank = '' ;
2394 while ( ' ' == $x{0} ) {
2395 $blank .= ' ';
2396 $x = substr( $x, 1 );
2397 }
2398 while ( strstr( $valid, $x{0} ) != false ) {
2399 $geo .= $x{0};
2400 $x = substr( $x, 1 );
2401 }
2402 $num = str_replace( '+', '', $geo );
2403 $num = str_replace( ' ', '', $num );
2404
2405 if ( '' == $num || count ( explode ( ':' , $num , 3 ) ) < 2 ) {
2406 $text .= "GEO $blank$x";
2407 } else {
2408 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2409 $text .= '<a href="' .
2410 $titleObj->escapeLocalUrl( 'coordinates='.$num ) .
2411 "\" class=\"internal\">GEO $geo</a>";
2412 $text .= $x;
2413 }
2414 }
2415 wfProfileOut( $fname );
2416 return $text;
2417 }
2418
2419 /**
2420 * Return an HTML link for the "RFC 1234" text
2421 * @access private
2422 * @param string $text text to be processed
2423 */
2424 function magicRFC( $text ) {
2425 global $wgLang;
2426
2427 $valid = '0123456789';
2428 $internal = false;
2429
2430 $a = split( 'RFC ', ' '.$text );
2431 if ( count ( $a ) < 2 ) return $text;
2432 $text = substr( array_shift( $a ), 1);
2433
2434 /* Check if RFC keyword is preceed by [[.
2435 * This test is made here cause of the array_shift above
2436 * that prevent the test to be done in the foreach.
2437 */
2438 if(substr($text, -2) == '[[') { $internal = true; }
2439
2440 foreach ( $a as $x ) {
2441 /* token might be empty if we have RFC RFC 1234 */
2442 if($x=='') {
2443 $text.='RFC ';
2444 continue;
2445 }
2446
2447 $rfc = $blank = '' ;
2448
2449 /** remove and save whitespaces in $blank */
2450 while ( $x{0} == ' ' ) {
2451 $blank .= ' ';
2452 $x = substr( $x, 1 );
2453 }
2454
2455 /** remove and save the rfc number in $rfc */
2456 while ( strstr( $valid, $x{0} ) != false ) {
2457 $rfc .= $x{0};
2458 $x = substr( $x, 1 );
2459 }
2460
2461 if ( $rfc == '') {
2462 /* call back stripped spaces*/
2463 $text .= "RFC $blank$x";
2464 } elseif( $internal) {
2465 /* normal link */
2466 $text .= "RFC $rfc$x";
2467 } else {
2468 /* build the external link*/
2469 $url = wfmsg( 'rfcurl' );
2470 $url = str_replace( '$1', $rfc, $url);
2471 $sk =& $this->mOptions->getSkin();
2472 $la = $sk->getExternalLinkAttributes( $url, 'RFC '.$rfc );
2473 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2474 }
2475
2476 /* Check if the next RFC keyword is preceed by [[ */
2477 $internal = (substr($x,-2) == '[[');
2478 }
2479 return $text;
2480 }
2481
2482 /**
2483 * Transform wiki markup when saving a page by doing \r\n -> \n
2484 * conversion, substitting signatures, {{subst:}} templates, etc.
2485 *
2486 * @param string $text the text to transform
2487 * @param Title &$title the Title object for the current article
2488 * @param User &$user the User object describing the current user
2489 * @param ParserOptions $options parsing options
2490 * @param bool $clearState whether to clear the parser state first
2491 * @return string the altered wiki markup
2492 * @access public
2493 */
2494 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2495 $this->mOptions = $options;
2496 $this->mTitle =& $title;
2497 $this->mOutputType = OT_WIKI;
2498
2499 if ( $clearState ) {
2500 $this->clearState();
2501 }
2502
2503 $stripState = false;
2504 $pairs = array(
2505 "\r\n" => "\n",
2506 );
2507 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2508 // now with regexes
2509 /*
2510 $pairs = array(
2511 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2512 "/<br *?>/i" => "<br />",
2513 );
2514 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2515 */
2516 $text = $this->strip( $text, $stripState, false );
2517 $text = $this->pstPass2( $text, $user );
2518 $text = $this->unstrip( $text, $stripState );
2519 $text = $this->unstripNoWiki( $text, $stripState );
2520 return $text;
2521 }
2522
2523 /**
2524 * Pre-save transform helper function
2525 * @access private
2526 */
2527 function pstPass2( $text, &$user ) {
2528 global $wgLang, $wgLocaltimezone, $wgCurParser;
2529
2530 # Variable replacement
2531 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2532 $text = $this->replaceVariables( $text );
2533
2534 # Signatures
2535 #
2536 $n = $user->getName();
2537 $k = $user->getOption( 'nickname' );
2538 if ( '' == $k ) { $k = $n; }
2539 if(isset($wgLocaltimezone)) {
2540 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2541 }
2542 /* Note: this is an ugly timezone hack for the European wikis */
2543 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2544 ' (' . date( 'T' ) . ')';
2545 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2546
2547 $text = preg_replace( '/~~~~~/', $d, $text );
2548 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]] $d", $text );
2549 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]]", $text );
2550
2551 # Context links: [[|name]] and [[name (context)|]]
2552 #
2553 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2554 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2555 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2556 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2557
2558 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2559 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2560 $p3 = "/\[\[(:*$namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]] and [[:namespace:page|]]
2561 $p4 = "/\[\[(:*$namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/"; # [[ns:page (cont)|]] and [[:ns:page (cont)|]]
2562 $context = '';
2563 $t = $this->mTitle->getText();
2564 if ( preg_match( $conpat, $t, $m ) ) {
2565 $context = $m[2];
2566 }
2567 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2568 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2569 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2570
2571 if ( '' == $context ) {
2572 $text = preg_replace( $p2, '[[\\1]]', $text );
2573 } else {
2574 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2575 }
2576
2577 /*
2578 $mw =& MagicWord::get( MAG_SUBST );
2579 $wgCurParser = $this->fork();
2580 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2581 $this->merge( $wgCurParser );
2582 */
2583
2584 # Trim trailing whitespace
2585 # MAG_END (__END__) tag allows for trailing
2586 # whitespace to be deliberately included
2587 $text = rtrim( $text );
2588 $mw =& MagicWord::get( MAG_END );
2589 $mw->matchAndRemove( $text );
2590
2591 return $text;
2592 }
2593
2594 /**
2595 * Set up some variables which are usually set up in parse()
2596 * so that an external function can call some class members with confidence
2597 * @access public
2598 */
2599 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2600 $this->mTitle =& $title;
2601 $this->mOptions = $options;
2602 $this->mOutputType = $outputType;
2603 if ( $clearState ) {
2604 $this->clearState();
2605 }
2606 }
2607
2608 /**
2609 * Transform a MediaWiki message by replacing magic variables.
2610 *
2611 * @param string $text the text to transform
2612 * @param ParserOptions $options options
2613 * @return string the text with variables substituted
2614 * @access public
2615 */
2616 function transformMsg( $text, $options ) {
2617 global $wgTitle;
2618 static $executing = false;
2619
2620 # Guard against infinite recursion
2621 if ( $executing ) {
2622 return $text;
2623 }
2624 $executing = true;
2625
2626 $this->mTitle = $wgTitle;
2627 $this->mOptions = $options;
2628 $this->mOutputType = OT_MSG;
2629 $this->clearState();
2630 $text = $this->replaceVariables( $text );
2631
2632 $executing = false;
2633 return $text;
2634 }
2635
2636 /**
2637 * Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2638 * Callback will be called with the text within
2639 * Transform and return the text within
2640 * @access public
2641 */
2642 function setHook( $tag, $callback ) {
2643 $oldVal = @$this->mTagHooks[$tag];
2644 $this->mTagHooks[$tag] = $callback;
2645 return $oldVal;
2646 }
2647 }
2648
2649 /**
2650 * @todo document
2651 * @package MediaWiki
2652 */
2653 class ParserOutput
2654 {
2655 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2656 var $mCacheTime; # Used in ParserCache
2657
2658 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
2659 $containsOldMagic = false )
2660 {
2661 $this->mText = $text;
2662 $this->mLanguageLinks = $languageLinks;
2663 $this->mCategoryLinks = $categoryLinks;
2664 $this->mContainsOldMagic = $containsOldMagic;
2665 $this->mCacheTime = '';
2666 }
2667
2668 function getText() { return $this->mText; }
2669 function getLanguageLinks() { return $this->mLanguageLinks; }
2670 function getCategoryLinks() { return $this->mCategoryLinks; }
2671 function getCacheTime() { return $this->mCacheTime; }
2672 function containsOldMagic() { return $this->mContainsOldMagic; }
2673 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2674 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2675 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2676 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2677 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2678
2679 function merge( $other ) {
2680 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2681 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2682 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2683 }
2684
2685 }
2686
2687 /**
2688 * Set options of the Parser
2689 * @todo document
2690 * @package MediaWiki
2691 */
2692 class ParserOptions
2693 {
2694 # All variables are private
2695 var $mUseTeX; # Use texvc to expand <math> tags
2696 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2697 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2698 var $mAllowExternalImages; # Allow external images inline
2699 var $mSkin; # Reference to the preferred skin
2700 var $mDateFormat; # Date format index
2701 var $mEditSection; # Create "edit section" links
2702 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2703 var $mNumberHeadings; # Automatically number headings
2704 var $mShowToc; # Show table of contents
2705
2706 function getUseTeX() { return $this->mUseTeX; }
2707 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2708 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2709 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2710 function getSkin() { return $this->mSkin; }
2711 function getDateFormat() { return $this->mDateFormat; }
2712 function getEditSection() { return $this->mEditSection; }
2713 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2714 function getNumberHeadings() { return $this->mNumberHeadings; }
2715 function getShowToc() { return $this->mShowToc; }
2716
2717 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2718 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2719 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2720 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2721 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2722 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2723 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2724 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2725 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2726
2727 function setSkin( &$x ) { $this->mSkin =& $x; }
2728
2729 # Get parser options
2730 /* static */ function newFromUser( &$user ) {
2731 $popts = new ParserOptions;
2732 $popts->initialiseFromUser( $user );
2733 return $popts;
2734 }
2735
2736 # Get user options
2737 function initialiseFromUser( &$userInput ) {
2738 global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2739
2740 $fname = 'ParserOptions::initialiseFromUser';
2741 wfProfileIn( $fname );
2742 if ( !$userInput ) {
2743 $user = new User;
2744 $user->setLoaded( true );
2745 } else {
2746 $user =& $userInput;
2747 }
2748
2749 $this->mUseTeX = $wgUseTeX;
2750 $this->mUseDynamicDates = $wgUseDynamicDates;
2751 $this->mInterwikiMagic = $wgInterwikiMagic;
2752 $this->mAllowExternalImages = $wgAllowExternalImages;
2753 wfProfileIn( $fname.'-skin' );
2754 $this->mSkin =& $user->getSkin();
2755 wfProfileOut( $fname.'-skin' );
2756 $this->mDateFormat = $user->getOption( 'date' );
2757 $this->mEditSection = $user->getOption( 'editsection' );
2758 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2759 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2760 $this->mShowToc = $user->getOption( 'showtoc' );
2761 wfProfileOut( $fname );
2762 }
2763
2764
2765 }
2766
2767 # Regex callbacks, used in Parser::replaceVariables
2768 function wfBraceSubstitution( $matches ) {
2769 global $wgCurParser;
2770 return $wgCurParser->braceSubstitution( $matches );
2771 }
2772
2773 function wfArgSubstitution( $matches ) {
2774 global $wgCurParser;
2775 return $wgCurParser->argSubstitution( $matches );
2776 }
2777
2778 /**
2779 * Return the total number of articles
2780 */
2781 function wfNumberOfArticles() {
2782 global $wgNumberOfArticles;
2783
2784 wfLoadSiteStats();
2785 return $wgNumberOfArticles;
2786 }
2787
2788 /**
2789 * Get various statistics from the database
2790 * @private
2791 */
2792 function wfLoadSiteStats() {
2793 global $wgNumberOfArticles, $wgTotalViews, $wgTotalEdits;
2794 $fname = 'wfLoadSiteStats';
2795
2796 if ( -1 != $wgNumberOfArticles ) return;
2797 $dbr =& wfGetDB( DB_SLAVE );
2798 $s = $dbr->getArray( 'site_stats',
2799 array( 'ss_total_views', 'ss_total_edits', 'ss_good_articles' ),
2800 array( 'ss_row_id' => 1 ), $fname
2801 );
2802
2803 if ( $s === false ) {
2804 return;
2805 } else {
2806 $wgTotalViews = $s->ss_total_views;
2807 $wgTotalEdits = $s->ss_total_edits;
2808 $wgNumberOfArticles = $s->ss_good_articles;
2809 }
2810 }
2811
2812 function wfEscapeHTMLTagsOnly( $in ) {
2813 return str_replace(
2814 array( '"', '>', '<' ),
2815 array( '&quot;', '&gt;', '&lt;' ),
2816 $in );
2817 }
2818
2819 ?>