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