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