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