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