a0e8649dd4eb6303d1b4a3d9a97758d095089859
[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 # We are not counting sequences of five apostrophes.
927 if (strlen ($arr[$i]) == 2) $numitalics++; else
928 if (strlen ($arr[$i]) == 3) $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 # Now close all remaining tags. Notice that the order is important.
1041 if ($state == 'strong' || $state == 'emstrong')
1042 $output .= "</strong>";
1043 if ($state == 'em' || $state == 'strongem' || $state == 'emstrong')
1044 $output .= "</em>";
1045 if ($state == 'strongem')
1046 $output .= "</strong>";
1047 if ($state == 'both')
1048 $output .= "<strong><em>{$buffer}</em></strong>";
1049 return $output;
1050 }
1051 }
1052
1053 # Note: we have to do external links before the internal ones,
1054 # and otherwise take great care in the order of things here, so
1055 # that we don't end up interpreting some URLs twice.
1056
1057 /* private */ function replaceExternalLinks( $text ) {
1058 $fname = 'Parser::replaceExternalLinks';
1059 wfProfileIn( $fname );
1060
1061 $text = $this->subReplaceExternalLinks( $text, 'http', true );
1062 $text = $this->subReplaceExternalLinks( $text, 'https', true );
1063 $text = $this->subReplaceExternalLinks( $text, 'ftp', false );
1064 $text = $this->subReplaceExternalLinks( $text, 'irc', false );
1065 $text = $this->subReplaceExternalLinks( $text, 'gopher', false );
1066 $text = $this->subReplaceExternalLinks( $text, 'news', false );
1067 $text = $this->subReplaceExternalLinks( $text, 'mailto', false );
1068 wfProfileOut( $fname );
1069 return $text;
1070 }
1071
1072 # Replaces all external links with a given protocol
1073 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber ) {
1074 $unique = '4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3';
1075
1076 # URL character class
1077 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
1078
1079 # this is the list of separators that should be ignored if they
1080 # are the last character of an URL but that should be included
1081 # if they occur within the URL, e.g. "go to www.foo.com, where .."
1082 # in this case, the last comma should not become part of the URL,
1083 # but in "www.foo.com/123,2342,32.htm" it should.
1084 $sep = ",;\.:";
1085
1086 # File name character class, used for identifying images
1087 $fnc = 'A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF';
1088 # Recognised image extensions
1089 $images = 'gif|png|jpg|jpeg';
1090
1091 # PLEASE NOTE: The curly braces { } are not part of the regex,
1092 # they are interpreted as part of the string (used to tell PHP
1093 # that the content of the string should be inserted there).
1094
1095 # Regexp for matching image URLs
1096 $rxImageURL = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
1097 "((?i){$images})([^{$uc}]|$)/";
1098
1099 # Regexp for matching non-delimited URLs
1100 $rxFreeURL = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
1101 $sk =& $this->mOptions->getSkin();
1102
1103 # Replace image URLs with <img> tags, but only for HTTP and HTTPS ($autonumber=true)
1104 if ( $autonumber and $this->mOptions->getAllowExternalImages() ) {
1105 $s = preg_replace( $rxImageURL, '\\1' . $sk->makeImage( "{$unique}:\\3" .
1106 '/\\4.\\5', '\\4.\\5' ) . '\\6', $s );
1107 }
1108
1109 # Replace free URLs
1110 $s = preg_replace( $rxFreeURL, '\\1' . "<a href=\"{$unique}:\\3\"" .
1111 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
1112 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
1113 '</a>\\5', $s );
1114 $s = str_replace( $unique, $protocol, $s );
1115
1116 # Search for external links with square brackets by splitting with explode()
1117 $a = explode( "[{$protocol}:", " " . $s );
1118 $s = array_shift( $a );
1119 $s = substr( $s, 1 );
1120
1121 # Regexp for URL in square brackets
1122 $rxWithoutText = "/^([{$uc}{$sep}]+)\\](.*)\$/sD";
1123 # Regexp for URL with link text in square brackets
1124 $rxWithText = "/^([{$uc}{$sep}]+)\\s+([^\\]]+)\\](.*)\$/sD";
1125
1126 # Now interpret each instance of [protocol:
1127 foreach ( $a as $line ) {
1128
1129 # CASE 1: Link in square brackets, e.g.
1130 # some text [http://domain.tld/some.link] more text
1131 if ( preg_match( $rxWithoutText, $line, $m ) ) {
1132 $link = "{$protocol}:{$m[1]}";
1133 $trail = $m[2];
1134 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
1135 else { $text = wfEscapeHTML( $link ); }
1136 }
1137
1138 # CASE 2: Link with link text and text directly following it, e.g.
1139 # This is a collection of [http://domain.tld/some.link link]s
1140 else if ( preg_match( $rxWithText, $line, $m ) ) {
1141 $link = "{$protocol}:{$m[1]}";
1142 $text = $m[2];
1143 $dtrail = '';
1144 $trail = $m[3];
1145 if ( preg_match( wfMsg ('linktrail'), $trail, $m2 ) ) {
1146 $dtrail = $m2[1];
1147 $trail = $m2[2];
1148 }
1149 }
1150
1151 # CASE 3: Nothing matches, just output the source text
1152 else {
1153 $s .= "[{$protocol}:" . $line;
1154 continue;
1155 }
1156
1157 if( $link == $text || preg_match( "!$protocol://" . preg_quote( $text, "/" ) . "/?$!", $link ) ) {
1158 $paren = '';
1159 } else {
1160 # Expand the URL for printable version
1161 $paren = "<span class='urlexpansion'> (<i>" . htmlspecialchars ( $link ) . "</i>)</span>";
1162 }
1163 $la = $sk->getExternalLinkAttributes( $link, $text );
1164 $s .= "<a href='{$link}'{$la}>{$text}</a>{$dtrail}{$paren}{$trail}";
1165
1166 }
1167 return $s;
1168 }
1169
1170
1171 /* private */ function replaceInternalLinks( $s ) {
1172 global $wgLang, $wgLinkCache;
1173 global $wgNamespacesWithSubpages, $wgLanguageCode;
1174 static $fname = 'Parser::replaceInternalLinks' ;
1175 wfProfileIn( $fname );
1176
1177 wfProfileIn( $fname.'-setup' );
1178 static $tc = FALSE;
1179 # the % is needed to support urlencoded titles as well
1180 if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
1181 $sk =& $this->mOptions->getSkin();
1182
1183 $redirect = MagicWord::get ( MAG_REDIRECT ) ;
1184
1185 $a = explode( '[[', ' ' . $s );
1186 $s = array_shift( $a );
1187 $s = substr( $s, 1 );
1188
1189 # Match a link having the form [[namespace:link|alternate]]trail
1190 static $e1 = FALSE;
1191 if ( !$e1 ) { $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD"; }
1192 # Match the end of a line for a word that's not followed by whitespace,
1193 # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
1194 static $e2 = '/^(.*?)([a-zA-Z\x80-\xff]+)$/sD';
1195
1196 $useLinkPrefixExtension = $wgLang->linkPrefixExtension();
1197 # Special and Media are pseudo-namespaces; no pages actually exist in them
1198
1199 $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
1200
1201 if ( $useLinkPrefixExtension ) {
1202 if ( preg_match( $e2, $s, $m ) ) {
1203 $first_prefix = $m[2];
1204 $s = $m[1];
1205 } else {
1206 $first_prefix = false;
1207 }
1208 } else {
1209 $prefix = '';
1210 }
1211
1212 wfProfileOut( $fname.'-setup' );
1213
1214 foreach ( $a as $line ) {
1215 wfProfileIn( $fname.'-prefixhandling' );
1216 if ( $useLinkPrefixExtension ) {
1217 if ( preg_match( $e2, $s, $m ) ) {
1218 $prefix = $m[2];
1219 $s = $m[1];
1220 } else {
1221 $prefix='';
1222 }
1223 # first link
1224 if($first_prefix) {
1225 $prefix = $first_prefix;
1226 $first_prefix = false;
1227 }
1228 }
1229 wfProfileOut( $fname.'-prefixhandling' );
1230
1231 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
1232 $text = $m[2];
1233 # fix up urlencoded title texts
1234 if(preg_match('/%/', $m[1] )) $m[1] = urldecode($m[1]);
1235 $trail = $m[3];
1236 } else { # Invalid form; output directly
1237 $s .= $prefix . '[[' . $line ;
1238 continue;
1239 }
1240
1241 /* Valid link forms:
1242 Foobar -- normal
1243 :Foobar -- override special treatment of prefix (images, language links)
1244 /Foobar -- convert to CurrentPage/Foobar
1245 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
1246 */
1247 $c = substr($m[1],0,1);
1248 $noforce = ($c != ':');
1249 if( $c == '/' ) { # subpage
1250 if(substr($m[1],-1,1)=='/') { # / at end means we don't want the slash to be shown
1251 $m[1]=substr($m[1],1,strlen($m[1])-2);
1252 $noslash=$m[1];
1253 } else {
1254 $noslash=substr($m[1],1);
1255 }
1256 if(!empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()])) { # subpages allowed here
1257 $link = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
1258 if( '' == $text ) {
1259 $text= $m[1];
1260 } # this might be changed for ugliness reasons
1261 } else {
1262 $link = $noslash; # no subpage allowed, use standard link
1263 }
1264 } elseif( $noforce ) { # no subpage
1265 $link = $m[1];
1266 } else {
1267 $link = substr( $m[1], 1 );
1268 }
1269 $wasblank = ( '' == $text );
1270 if( $wasblank )
1271 $text = $link;
1272
1273 $nt = Title::newFromText( $link );
1274 if( !$nt ) {
1275 $s .= $prefix . '[[' . $line;
1276 continue;
1277 }
1278 $ns = $nt->getNamespace();
1279 $iw = $nt->getInterWiki();
1280 if( $noforce ) {
1281 if( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && $wgLang->getLanguageName( $iw ) ) {
1282 array_push( $this->mOutput->mLanguageLinks, $nt->getFullText() );
1283 $tmp = $prefix . $trail ;
1284 $s .= (trim($tmp) == '')? '': $tmp;
1285 continue;
1286 }
1287 if ( $ns == NS_IMAGE ) {
1288 $s .= $prefix . $sk->makeImageLinkObj( $nt, $text ) . $trail;
1289 $wgLinkCache->addImageLinkObj( $nt );
1290 continue;
1291 }
1292 if ( $ns == NS_CATEGORY ) {
1293 $t = $nt->getText() ;
1294 $nnt = Title::newFromText ( Namespace::getCanonicalName(NS_CATEGORY).":".$t ) ;
1295
1296 $wgLinkCache->suspend(); # Don't save in links/brokenlinks
1297 $pPLC=$sk->postParseLinkColour();
1298 $sk->postParseLinkColour( false );
1299 $t = $sk->makeLinkObj( $nnt, $t, '', '' , $prefix );
1300 $sk->postParseLinkColour( $pPLC );
1301 $wgLinkCache->resume();
1302
1303 $sortkey = $wasblank ? $this->mTitle->getPrefixedText() : $text;
1304 $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
1305 $this->mOutput->mCategoryLinks[] = $t ;
1306 $s .= $prefix . $trail ;
1307 continue;
1308 }
1309 }
1310 if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
1311 ( strpos( $link, '#' ) == FALSE ) ) {
1312 # Self-links are handled specially; generally de-link and change to bold.
1313 $s .= $prefix . $sk->makeSelfLinkObj( $nt, $text, '', $trail );
1314 continue;
1315 }
1316
1317 if( $ns == NS_MEDIA ) {
1318 $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1319 $wgLinkCache->addImageLinkObj( $nt );
1320 continue;
1321 } elseif( $ns == NS_SPECIAL ) {
1322 $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
1323 continue;
1324 }
1325 $s .= $sk->makeLinkObj( $nt, $text, '', $trail, $prefix );
1326 }
1327 wfProfileOut( $fname );
1328 return $s;
1329 }
1330
1331 # Some functions here used by doBlockLevels()
1332 #
1333 /* private */ function closeParagraph() {
1334 $result = '';
1335 if ( '' != $this->mLastSection ) {
1336 $result = '</' . $this->mLastSection . ">\n";
1337 }
1338 $this->mInPre = false;
1339 $this->mLastSection = '';
1340 return $result;
1341 }
1342 # getCommon() returns the length of the longest common substring
1343 # of both arguments, starting at the beginning of both.
1344 #
1345 /* private */ function getCommon( $st1, $st2 ) {
1346 $fl = strlen( $st1 );
1347 $shorter = strlen( $st2 );
1348 if ( $fl < $shorter ) { $shorter = $fl; }
1349
1350 for ( $i = 0; $i < $shorter; ++$i ) {
1351 if ( $st1{$i} != $st2{$i} ) { break; }
1352 }
1353 return $i;
1354 }
1355 # These next three functions open, continue, and close the list
1356 # element appropriate to the prefix character passed into them.
1357 #
1358 /* private */ function openList( $char )
1359 {
1360 $result = $this->closeParagraph();
1361
1362 if ( '*' == $char ) { $result .= '<ul><li>'; }
1363 else if ( '#' == $char ) { $result .= '<ol><li>'; }
1364 else if ( ':' == $char ) { $result .= '<dl><dd>'; }
1365 else if ( ';' == $char ) {
1366 $result .= '<dl><dt>';
1367 $this->mDTopen = true;
1368 }
1369 else { $result = '<!-- ERR 1 -->'; }
1370
1371 return $result;
1372 }
1373
1374 /* private */ function nextItem( $char ) {
1375 if ( '*' == $char || '#' == $char ) { return '</li><li>'; }
1376 else if ( ':' == $char || ';' == $char ) {
1377 $close = "</dd>";
1378 if ( $this->mDTopen ) { $close = '</dt>'; }
1379 if ( ';' == $char ) {
1380 $this->mDTopen = true;
1381 return $close . '<dt>';
1382 } else {
1383 $this->mDTopen = false;
1384 return $close . '<dd>';
1385 }
1386 }
1387 return '<!-- ERR 2 -->';
1388 }
1389
1390 /* private */function closeList( $char ) {
1391 if ( '*' == $char ) { $text = '</li></ul>'; }
1392 else if ( '#' == $char ) { $text = '</li></ol>'; }
1393 else if ( ':' == $char ) {
1394 if ( $this->mDTopen ) {
1395 $this->mDTopen = false;
1396 $text = '</dt></dl>';
1397 } else {
1398 $text = '</dd></dl>';
1399 }
1400 }
1401 else { return '<!-- ERR 3 -->'; }
1402 return $text."\n";
1403 }
1404
1405 /* private */ function doBlockLevels( $text, $linestart ) {
1406 $fname = 'Parser::doBlockLevels';
1407 wfProfileIn( $fname );
1408
1409 # Parsing through the text line by line. The main thing
1410 # happening here is handling of block-level elements p, pre,
1411 # and making lists from lines starting with * # : etc.
1412 #
1413 $textLines = explode( "\n", $text );
1414
1415 $lastPrefix = $output = $lastLine = '';
1416 $this->mDTopen = $inBlockElem = false;
1417 $prefixLength = 0;
1418 $paragraphStack = false;
1419
1420 if ( !$linestart ) {
1421 $output .= array_shift( $textLines );
1422 }
1423 foreach ( $textLines as $oLine ) {
1424 $lastPrefixLength = strlen( $lastPrefix );
1425 $preCloseMatch = preg_match("/<\\/pre/i", $oLine );
1426 $preOpenMatch = preg_match("/<pre/i", $oLine );
1427 if ( !$this->mInPre ) {
1428 # Multiple prefixes may abut each other for nested lists.
1429 $prefixLength = strspn( $oLine, '*#:;' );
1430 $pref = substr( $oLine, 0, $prefixLength );
1431
1432 # eh?
1433 $pref2 = str_replace( ';', ':', $pref );
1434 $t = substr( $oLine, $prefixLength );
1435 $this->mInPre = !empty($preOpenMatch);
1436 } else {
1437 # Don't interpret any other prefixes in preformatted text
1438 $prefixLength = 0;
1439 $pref = $pref2 = '';
1440 $t = $oLine;
1441 }
1442
1443 # List generation
1444 if( $prefixLength && 0 == strcmp( $lastPrefix, $pref2 ) ) {
1445 # Same as the last item, so no need to deal with nesting or opening stuff
1446 $output .= $this->nextItem( substr( $pref, -1 ) );
1447 $paragraphStack = false;
1448
1449 if ( ";" == substr( $pref, -1 ) ) {
1450 # The one nasty exception: definition lists work like this:
1451 # ; title : definition text
1452 # So we check for : in the remainder text to split up the
1453 # title and definition, without b0rking links.
1454 # FIXME: This is not foolproof. Something better in Tokenizer might help.
1455 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1456 $term = $match[1];
1457 $output .= $term . $this->nextItem( ':' );
1458 $t = $match[2];
1459 }
1460 }
1461 } elseif( $prefixLength || $lastPrefixLength ) {
1462 # Either open or close a level...
1463 $commonPrefixLength = $this->getCommon( $pref, $lastPrefix );
1464 $paragraphStack = false;
1465
1466 while( $commonPrefixLength < $lastPrefixLength ) {
1467 $output .= $this->closeList( $lastPrefix{$lastPrefixLength-1} );
1468 --$lastPrefixLength;
1469 }
1470 if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
1471 $output .= $this->nextItem( $pref{$commonPrefixLength-1} );
1472 }
1473 while ( $prefixLength > $commonPrefixLength ) {
1474 $char = substr( $pref, $commonPrefixLength, 1 );
1475 $output .= $this->openList( $char );
1476
1477 if ( ';' == $char ) {
1478 # FIXME: This is dupe of code above
1479 if( preg_match( '/^(.*?(?:\s|&nbsp;)):(.*)$/', $t, $match ) ) {
1480 $term = $match[1];
1481 $output .= $term . $this->nextItem( ":" );
1482 $t = $match[2];
1483 }
1484 }
1485 ++$commonPrefixLength;
1486 }
1487 $lastPrefix = $pref2;
1488 }
1489 if( 0 == $prefixLength ) {
1490 # No prefix (not in list)--go to paragraph mode
1491 $uniq_prefix = UNIQ_PREFIX;
1492 // XXX: use a stack for nestable elements like span, table and div
1493 $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/i', $t );
1494 $closematch = preg_match(
1495 '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
1496 '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$uniq_prefix.'-pre|<\\/li|<\\/ul)/i', $t );
1497 if ( $openmatch or $closematch ) {
1498 $paragraphStack = false;
1499 $output .= $this->closeParagraph();
1500 if($preOpenMatch and !$preCloseMatch) {
1501 $this->mInPre = true;
1502 }
1503 if ( $closematch ) {
1504 $inBlockElem = false;
1505 } else {
1506 $inBlockElem = true;
1507 }
1508 } else if ( !$inBlockElem && !$this->mInPre ) {
1509 if ( " " == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
1510 // pre
1511 if ($this->mLastSection != 'pre') {
1512 $paragraphStack = false;
1513 $output .= $this->closeParagraph().'<pre>';
1514 $this->mLastSection = 'pre';
1515 }
1516 } else {
1517 // paragraph
1518 if ( '' == trim($t) ) {
1519 if ( $paragraphStack ) {
1520 $output .= $paragraphStack.'<br />';
1521 $paragraphStack = false;
1522 $this->mLastSection = 'p';
1523 } else {
1524 if ($this->mLastSection != 'p' ) {
1525 $output .= $this->closeParagraph();
1526 $this->mLastSection = '';
1527 $paragraphStack = '<p>';
1528 } else {
1529 $paragraphStack = '</p><p>';
1530 }
1531 }
1532 } else {
1533 if ( $paragraphStack ) {
1534 $output .= $paragraphStack;
1535 $paragraphStack = false;
1536 $this->mLastSection = 'p';
1537 } else if ($this->mLastSection != 'p') {
1538 $output .= $this->closeParagraph().'<p>';
1539 $this->mLastSection = 'p';
1540 }
1541 }
1542 }
1543 }
1544 }
1545 if ($paragraphStack === false) {
1546 $output .= $t."\n";
1547 }
1548 }
1549 while ( $prefixLength ) {
1550 $output .= $this->closeList( $pref2{$prefixLength-1} );
1551 --$prefixLength;
1552 }
1553 if ( '' != $this->mLastSection ) {
1554 $output .= '</' . $this->mLastSection . '>';
1555 $this->mLastSection = '';
1556 }
1557
1558 wfProfileOut( $fname );
1559 return $output;
1560 }
1561
1562 # Return value of a magic variable (like PAGENAME)
1563 function getVariableValue( $index ) {
1564 global $wgLang, $wgSitename, $wgServer;
1565
1566 switch ( $index ) {
1567 case MAG_CURRENTMONTH:
1568 return $wgLang->formatNum( date( 'm' ) );
1569 case MAG_CURRENTMONTHNAME:
1570 return $wgLang->getMonthName( date('n') );
1571 case MAG_CURRENTMONTHNAMEGEN:
1572 return $wgLang->getMonthNameGen( date('n') );
1573 case MAG_CURRENTDAY:
1574 return $wgLang->formatNum( date('j') );
1575 case MAG_PAGENAME:
1576 return $this->mTitle->getText();
1577 case MAG_NAMESPACE:
1578 # return Namespace::getCanonicalName($this->mTitle->getNamespace());
1579 return $wgLang->getNsText($this->mTitle->getNamespace()); // Patch by Dori
1580 case MAG_CURRENTDAYNAME:
1581 return $wgLang->getWeekdayName( date('w')+1 );
1582 case MAG_CURRENTYEAR:
1583 return $wgLang->formatNum( date( 'Y' ) );
1584 case MAG_CURRENTTIME:
1585 return $wgLang->time( wfTimestampNow(), false );
1586 case MAG_NUMBEROFARTICLES:
1587 return $wgLang->formatNum( wfNumberOfArticles() );
1588 case MAG_SITENAME:
1589 return $wgSitename;
1590 case MAG_SERVER:
1591 return $wgServer;
1592 default:
1593 return NULL;
1594 }
1595 }
1596
1597 # initialise the magic variables (like CURRENTMONTHNAME)
1598 function initialiseVariables() {
1599 global $wgVariableIDs;
1600 $this->mVariables = array();
1601 foreach ( $wgVariableIDs as $id ) {
1602 $mw =& MagicWord::get( $id );
1603 $mw->addToArray( $this->mVariables, $this->getVariableValue( $id ) );
1604 }
1605 }
1606
1607 /* private */ function replaceVariables( $text, $args = array() ) {
1608 global $wgLang, $wgScript, $wgArticlePath;
1609
1610 # Prevent too big inclusions
1611 if(strlen($text)> MAX_INCLUDE_SIZE)
1612 return $text;
1613
1614 $fname = 'Parser::replaceVariables';
1615 wfProfileIn( $fname );
1616
1617 $bail = false;
1618 $titleChars = Title::legalChars();
1619 $nonBraceChars = str_replace( array( '{', '}' ), array( '', '' ), $titleChars );
1620
1621 # This function is called recursively. To keep track of arguments we need a stack:
1622 array_push( $this->mArgStack, $args );
1623
1624 # PHP global rebinding syntax is a bit weird, need to use the GLOBALS array
1625 $GLOBALS['wgCurParser'] =& $this;
1626
1627
1628 if ( $this->mOutputType == OT_HTML ) {
1629 # Variable substitution
1630 $text = preg_replace_callback( "/{{([$nonBraceChars]*?)}}/", 'wfVariableSubstitution', $text );
1631
1632 # Argument substitution
1633 $text = preg_replace_callback( "/(\\n?){{{([$titleChars]*?)}}}/", 'wfArgSubstitution', $text );
1634 }
1635 # Template substitution
1636 $regex = '/(\\n?){{(['.$nonBraceChars.']*)(\\|.*?|)}}/s';
1637 $text = preg_replace_callback( $regex, 'wfBraceSubstitution', $text );
1638
1639 array_pop( $this->mArgStack );
1640
1641 wfProfileOut( $fname );
1642 return $text;
1643 }
1644
1645 function variableSubstitution( $matches ) {
1646 if ( !$this->mVariables ) {
1647 $this->initialiseVariables();
1648 }
1649 if ( array_key_exists( $matches[1], $this->mVariables ) ) {
1650 $text = $this->mVariables[$matches[1]];
1651 $this->mOutput->mContainsOldMagic = true;
1652 } else {
1653 $text = $matches[0];
1654 }
1655 return $text;
1656 }
1657
1658 # Split template arguments
1659 function getTemplateArgs( $argsString ) {
1660 if ( $argsString === '' ) {
1661 return array();
1662 }
1663
1664 $args = explode( '|', substr( $argsString, 1 ) );
1665
1666 # If any of the arguments contains a '[[' but no ']]', it needs to be
1667 # merged with the next arg because the '|' character between belongs
1668 # to the link syntax and not the template parameter syntax.
1669 $argc = count($args);
1670 $i = 0;
1671 for ( $i = 0; $i < $argc-1; $i++ ) {
1672 if ( substr_count ( $args[$i], "[[" ) != substr_count ( $args[$i], "]]" ) ) {
1673 $args[$i] .= "|".$args[$i+1];
1674 array_splice($args, $i+1, 1);
1675 $i--;
1676 $argc--;
1677 }
1678 }
1679
1680 return $args;
1681 }
1682
1683 function braceSubstitution( $matches ) {
1684 global $wgLinkCache, $wgLang;
1685 $fname = 'Parser::braceSubstitution';
1686 $found = false;
1687 $nowiki = false;
1688 $noparse = false;
1689
1690 $title = NULL;
1691
1692 # $newline is an optional newline character before the braces
1693 # $part1 is the bit before the first |, and must contain only title characters
1694 # $args is a list of arguments, starting from index 0, not including $part1
1695
1696 $newline = $matches[1];
1697 $part1 = $matches[2];
1698 # If the third subpattern matched anything, it will start with |
1699
1700 $args = $this->getTemplateArgs($matches[3]);
1701 $argc = count( $args );
1702
1703 # {{{}}}
1704 if ( strpos( $matches[0], '{{{' ) !== false ) {
1705 $text = $matches[0];
1706 $found = true;
1707 $noparse = true;
1708 }
1709
1710 # SUBST
1711 if ( !$found ) {
1712 $mwSubst =& MagicWord::get( MAG_SUBST );
1713 if ( $mwSubst->matchStartAndRemove( $part1 ) ) {
1714 if ( $this->mOutputType != OT_WIKI ) {
1715 # Invalid SUBST not replaced at PST time
1716 # Return without further processing
1717 $text = $matches[0];
1718 $found = true;
1719 $noparse= true;
1720 }
1721 } elseif ( $this->mOutputType == OT_WIKI ) {
1722 # SUBST not found in PST pass, do nothing
1723 $text = $matches[0];
1724 $found = true;
1725 }
1726 }
1727
1728 # MSG, MSGNW and INT
1729 if ( !$found ) {
1730 # Check for MSGNW:
1731 $mwMsgnw =& MagicWord::get( MAG_MSGNW );
1732 if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
1733 $nowiki = true;
1734 } else {
1735 # Remove obsolete MSG:
1736 $mwMsg =& MagicWord::get( MAG_MSG );
1737 $mwMsg->matchStartAndRemove( $part1 );
1738 }
1739
1740 # Check if it is an internal message
1741 $mwInt =& MagicWord::get( MAG_INT );
1742 if ( $mwInt->matchStartAndRemove( $part1 ) ) {
1743 if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
1744 $text = wfMsgReal( $part1, $args, true );
1745 $found = true;
1746 }
1747 }
1748 }
1749
1750 # NS
1751 if ( !$found ) {
1752 # Check for NS: (namespace expansion)
1753 $mwNs = MagicWord::get( MAG_NS );
1754 if ( $mwNs->matchStartAndRemove( $part1 ) ) {
1755 if ( intval( $part1 ) ) {
1756 $text = $wgLang->getNsText( intval( $part1 ) );
1757 $found = true;
1758 } else {
1759 $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
1760 if ( !is_null( $index ) ) {
1761 $text = $wgLang->getNsText( $index );
1762 $found = true;
1763 }
1764 }
1765 }
1766 }
1767
1768 # LOCALURL and LOCALURLE
1769 if ( !$found ) {
1770 $mwLocal = MagicWord::get( MAG_LOCALURL );
1771 $mwLocalE = MagicWord::get( MAG_LOCALURLE );
1772
1773 if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
1774 $func = 'getLocalURL';
1775 } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
1776 $func = 'escapeLocalURL';
1777 } else {
1778 $func = '';
1779 }
1780
1781 if ( $func !== '' ) {
1782 $title = Title::newFromText( $part1 );
1783 if ( !is_null( $title ) ) {
1784 if ( $argc > 0 ) {
1785 $text = $title->$func( $args[0] );
1786 } else {
1787 $text = $title->$func();
1788 }
1789 $found = true;
1790 }
1791 }
1792 }
1793
1794 # Internal variables
1795 if ( !$this->mVariables ) {
1796 $this->initialiseVariables();
1797 }
1798 if ( !$found && array_key_exists( $part1, $this->mVariables ) ) {
1799 $text = $this->mVariables[$part1];
1800 $found = true;
1801 $this->mOutput->mContainsOldMagic = true;
1802 }
1803
1804 # Template table test
1805
1806 # Did we encounter this template already? If yes, it is in the cache
1807 # and we need to check for loops.
1808 if ( isset( $this->mTemplates[$part1] ) ) {
1809 # Infinite loop test
1810 if ( isset( $this->mTemplatePath[$part1] ) ) {
1811 $noparse = true;
1812 $found = true;
1813 }
1814 # set $text to cached message.
1815 $text = $this->mTemplates[$part1];
1816 $found = true;
1817 }
1818
1819 # Load from database
1820 if ( !$found ) {
1821 $title = Title::newFromText( $part1, NS_TEMPLATE );
1822 if ( !is_null( $title ) && !$title->isExternal() ) {
1823 # Check for excessive inclusion
1824 $dbk = $title->getPrefixedDBkey();
1825 if ( $this->incrementIncludeCount( $dbk ) ) {
1826 # This should never be reached.
1827 $article = new Article( $title );
1828 $articleContent = $article->getContentWithoutUsingSoManyDamnGlobals();
1829 if ( $articleContent !== false ) {
1830 $found = true;
1831 $text = $articleContent;
1832
1833 }
1834 }
1835
1836 # If the title is valid but undisplayable, make a link to it
1837 if ( $this->mOutputType == OT_HTML && !$found ) {
1838 $text = '[[' . $title->getPrefixedText() . ']]';
1839 $found = true;
1840 }
1841
1842 # Template cache array insertion
1843 $this->mTemplates[$part1] = $text;
1844 }
1845 }
1846
1847 # Recursive parsing, escaping and link table handling
1848 # Only for HTML output
1849 if ( $nowiki && $found && $this->mOutputType == OT_HTML ) {
1850 $text = wfEscapeWikiText( $text );
1851 } elseif ( $this->mOutputType == OT_HTML && $found && !$noparse) {
1852 # Clean up argument array
1853 $assocArgs = array();
1854 $index = 1;
1855 foreach( $args as $arg ) {
1856 $eqpos = strpos( $arg, '=' );
1857 if ( $eqpos === false ) {
1858 $assocArgs[$index++] = $arg;
1859 } else {
1860 $name = trim( substr( $arg, 0, $eqpos ) );
1861 $value = trim( substr( $arg, $eqpos+1 ) );
1862 if ( $value === false ) {
1863 $value = '';
1864 }
1865 if ( $name !== false ) {
1866 $assocArgs[$name] = $value;
1867 }
1868 }
1869 }
1870
1871 # Do not enter included links in link table
1872 if ( !is_null( $title ) ) {
1873 $wgLinkCache->suspend();
1874 }
1875
1876 # Add a new element to the templace recursion path
1877 $this->mTemplatePath[$part1] = 1;
1878
1879 # Run full parser on the included text
1880 $text = $this->internalParse( $text, $newline, $assocArgs );
1881 # I replaced the line below with the line above, as it former seems to cause several bugs
1882 #$text = $this->stripParse( $text, $newline, $assocArgs );
1883
1884 # Resume the link cache and register the inclusion as a link
1885 if ( !is_null( $title ) ) {
1886 $wgLinkCache->resume();
1887 $wgLinkCache->addLinkObj( $title );
1888 }
1889 }
1890 # Empties the template path
1891 $this->mTemplatePath = array();
1892
1893 if ( !$found ) {
1894 return $matches[0];
1895 } else {
1896 return $text;
1897 }
1898 }
1899
1900 # Triple brace replacement -- used for template arguments
1901 function argSubstitution( $matches ) {
1902 $newline = $matches[1];
1903 $arg = trim( $matches[2] );
1904 $text = $matches[0];
1905 $inputArgs = end( $this->mArgStack );
1906
1907 if ( array_key_exists( $arg, $inputArgs ) ) {
1908 $text = $this->stripParse( $inputArgs[$arg], $newline, array() );
1909 }
1910
1911 return $text;
1912 }
1913
1914 # Returns true if the function is allowed to include this entity
1915 function incrementIncludeCount( $dbk ) {
1916 if ( !array_key_exists( $dbk, $this->mIncludeCount ) ) {
1917 $this->mIncludeCount[$dbk] = 0;
1918 }
1919 if ( ++$this->mIncludeCount[$dbk] <= MAX_INCLUDE_REPEAT ) {
1920 return true;
1921 } else {
1922 return false;
1923 }
1924 }
1925
1926
1927 # Cleans up HTML, removes dangerous tags and attributes
1928 /* private */ function removeHTMLtags( $text ) {
1929 global $wgUseTidy, $wgUserHtml;
1930 $fname = 'Parser::removeHTMLtags';
1931 wfProfileIn( $fname );
1932
1933 if( $wgUserHtml ) {
1934 $htmlpairs = array( # Tags that must be closed
1935 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
1936 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
1937 'strike', 'strong', 'tt', 'var', 'div', 'center',
1938 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
1939 'ruby', 'rt' , 'rb' , 'rp', 'p'
1940 );
1941 $htmlsingle = array(
1942 'br', 'hr', 'li', 'dt', 'dd'
1943 );
1944 $htmlnest = array( # Tags that can be nested--??
1945 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
1946 'dl', 'font', 'big', 'small', 'sub', 'sup'
1947 );
1948 $tabletags = array( # Can only appear inside table
1949 'td', 'th', 'tr'
1950 );
1951 } else {
1952 $htmlpairs = array();
1953 $htmlsingle = array();
1954 $htmlnest = array();
1955 $tabletags = array();
1956 }
1957
1958 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1959 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1960
1961 $htmlattrs = $this->getHTMLattrs () ;
1962
1963 # Remove HTML comments
1964 $text = preg_replace( '/(\\n *<!--.*--> *(?=\\n)|<!--.*-->)/sU', '$2', $text );
1965
1966 $bits = explode( '<', $text );
1967 $text = array_shift( $bits );
1968 if(!$wgUseTidy) {
1969 $tagstack = array(); $tablestack = array();
1970 foreach ( $bits as $x ) {
1971 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1972 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
1973 $x, $regs );
1974 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1975 error_reporting( $prev );
1976
1977 $badtag = 0 ;
1978 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1979 # Check our stack
1980 if ( $slash ) {
1981 # Closing a tag...
1982 if ( ! in_array( $t, $htmlsingle ) &&
1983 ( $ot = @array_pop( $tagstack ) ) != $t ) {
1984 @array_push( $tagstack, $ot );
1985 $badtag = 1;
1986 } else {
1987 if ( $t == 'table' ) {
1988 $tagstack = array_pop( $tablestack );
1989 }
1990 $newparams = '';
1991 }
1992 } else {
1993 # Keep track for later
1994 if ( in_array( $t, $tabletags ) &&
1995 ! in_array( 'table', $tagstack ) ) {
1996 $badtag = 1;
1997 } else if ( in_array( $t, $tagstack ) &&
1998 ! in_array ( $t , $htmlnest ) ) {
1999 $badtag = 1 ;
2000 } else if ( ! in_array( $t, $htmlsingle ) ) {
2001 if ( $t == 'table' ) {
2002 array_push( $tablestack, $tagstack );
2003 $tagstack = array();
2004 }
2005 array_push( $tagstack, $t );
2006 }
2007 # Strip non-approved attributes from the tag
2008 $newparams = $this->fixTagAttributes($params);
2009
2010 }
2011 if ( ! $badtag ) {
2012 $rest = str_replace( '>', '&gt;', $rest );
2013 $text .= "<$slash$t $newparams$brace$rest";
2014 continue;
2015 }
2016 }
2017 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2018 }
2019 # Close off any remaining tags
2020 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
2021 $text .= "</$t>\n";
2022 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
2023 }
2024 } else {
2025 # this might be possible using tidy itself
2026 foreach ( $bits as $x ) {
2027 preg_match( '/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/',
2028 $x, $regs );
2029 @list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
2030 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
2031 $newparams = $this->fixTagAttributes($params);
2032 $rest = str_replace( '>', '&gt;', $rest );
2033 $text .= "<$slash$t $newparams$brace$rest";
2034 } else {
2035 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
2036 }
2037 }
2038 }
2039 wfProfileOut( $fname );
2040 return $text;
2041 }
2042
2043
2044 /*
2045 *
2046 * This function accomplishes several tasks:
2047 * 1) Auto-number headings if that option is enabled
2048 * 2) Add an [edit] link to sections for logged in users who have enabled the option
2049 * 3) Add a Table of contents on the top for users who have enabled the option
2050 * 4) Auto-anchor headings
2051 *
2052 * It loops through all headlines, collects the necessary data, then splits up the
2053 * string and re-inserts the newly formatted headlines.
2054 *
2055 */
2056
2057 /* private */ function formatHeadings( $text, $isMain=true ) {
2058 global $wgInputEncoding, $wgMaxTocLevel;
2059
2060 $doNumberHeadings = $this->mOptions->getNumberHeadings();
2061 $doShowToc = $this->mOptions->getShowToc();
2062 $forceTocHere = false;
2063 if( !$this->mTitle->userCanEdit() ) {
2064 $showEditLink = 0;
2065 $rightClickHack = 0;
2066 } else {
2067 $showEditLink = $this->mOptions->getEditSection();
2068 $rightClickHack = $this->mOptions->getEditSectionOnRightClick();
2069 }
2070
2071 # Inhibit editsection links if requested in the page
2072 $esw =& MagicWord::get( MAG_NOEDITSECTION );
2073 if( $esw->matchAndRemove( $text ) ) {
2074 $showEditLink = 0;
2075 }
2076 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
2077 # do not add TOC
2078 $mw =& MagicWord::get( MAG_NOTOC );
2079 if( $mw->matchAndRemove( $text ) ) {
2080 $doShowToc = 0;
2081 }
2082
2083 # never add the TOC to the Main Page. This is an entry page that should not
2084 # be more than 1-2 screens large anyway
2085 if( $this->mTitle->getPrefixedText() == wfMsg('mainpage') ) {
2086 $doShowToc = 0;
2087 }
2088
2089 # Get all headlines for numbering them and adding funky stuff like [edit]
2090 # links - this is for later, but we need the number of headlines right now
2091 $numMatches = preg_match_all( '/<H([1-6])(.*?' . '>)(.*?)<\/H[1-6]>/i', $text, $matches );
2092
2093 # if there are fewer than 4 headlines in the article, do not show TOC
2094 if( $numMatches < 4 ) {
2095 $doShowToc = 0;
2096 }
2097
2098 # if the string __TOC__ (not case-sensitive) occurs in the HTML,
2099 # override above conditions and always show TOC at that place
2100 $mw =& MagicWord::get( MAG_TOC );
2101 if ($mw->match( $text ) ) {
2102 $doShowToc = 1;
2103 $forceTocHere = true;
2104 } else {
2105 # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
2106 # override above conditions and always show TOC above first header
2107 $mw =& MagicWord::get( MAG_FORCETOC );
2108 if ($mw->matchAndRemove( $text ) ) {
2109 $doShowToc = 1;
2110 }
2111 }
2112
2113
2114
2115 # We need this to perform operations on the HTML
2116 $sk =& $this->mOptions->getSkin();
2117
2118 # headline counter
2119 $headlineCount = 0;
2120
2121 # Ugh .. the TOC should have neat indentation levels which can be
2122 # passed to the skin functions. These are determined here
2123 $toclevel = 0;
2124 $toc = '';
2125 $full = '';
2126 $head = array();
2127 $sublevelCount = array();
2128 $level = 0;
2129 $prevlevel = 0;
2130 foreach( $matches[3] as $headline ) {
2131 $numbering = '';
2132 if( $level ) {
2133 $prevlevel = $level;
2134 }
2135 $level = $matches[1][$headlineCount];
2136 if( ( $doNumberHeadings || $doShowToc ) && $prevlevel && $level > $prevlevel ) {
2137 # reset when we enter a new level
2138 $sublevelCount[$level] = 0;
2139 $toc .= $sk->tocIndent( $level - $prevlevel );
2140 $toclevel += $level - $prevlevel;
2141 }
2142 if( ( $doNumberHeadings || $doShowToc ) && $level < $prevlevel ) {
2143 # reset when we step back a level
2144 $sublevelCount[$level+1]=0;
2145 $toc .= $sk->tocUnindent( $prevlevel - $level );
2146 $toclevel -= $prevlevel - $level;
2147 }
2148 # count number of headlines for each level
2149 @$sublevelCount[$level]++;
2150 if( $doNumberHeadings || $doShowToc ) {
2151 $dot = 0;
2152 for( $i = 1; $i <= $level; $i++ ) {
2153 if( !empty( $sublevelCount[$i] ) ) {
2154 if( $dot ) {
2155 $numbering .= '.';
2156 }
2157 $numbering .= $sublevelCount[$i];
2158 $dot = 1;
2159 }
2160 }
2161 }
2162
2163 # The canonized header is a version of the header text safe to use for links
2164 # Avoid insertion of weird stuff like <math> by expanding the relevant sections
2165 $canonized_headline = $this->unstrip( $headline, $this->mStripState );
2166 $canonized_headline = $this->unstripNoWiki( $headline, $this->mStripState );
2167
2168 # strip out HTML
2169 $canonized_headline = preg_replace( '/<.*?' . '>/','',$canonized_headline );
2170 $tocline = trim( $canonized_headline );
2171 $canonized_headline = urlencode( do_html_entity_decode( str_replace(' ', '_', $tocline), ENT_COMPAT, $wgInputEncoding ) );
2172 $replacearray = array(
2173 '%3A' => ':',
2174 '%' => '.'
2175 );
2176 $canonized_headline = str_replace(array_keys($replacearray),array_values($replacearray),$canonized_headline);
2177 $refer[$headlineCount] = $canonized_headline;
2178
2179 # count how many in assoc. array so we can track dupes in anchors
2180 @$refers[$canonized_headline]++;
2181 $refcount[$headlineCount]=$refers[$canonized_headline];
2182
2183 # Prepend the number to the heading text
2184
2185 if( $doNumberHeadings || $doShowToc ) {
2186 $tocline = $numbering . ' ' . $tocline;
2187
2188 # Don't number the heading if it is the only one (looks silly)
2189 if( $doNumberHeadings && count( $matches[3] ) > 1) {
2190 # the two are different if the line contains a link
2191 $headline=$numbering . ' ' . $headline;
2192 }
2193 }
2194
2195 # Create the anchor for linking from the TOC to the section
2196 $anchor = $canonized_headline;
2197 if($refcount[$headlineCount] > 1 ) {
2198 $anchor .= '_' . $refcount[$headlineCount];
2199 }
2200 if( $doShowToc && ( !isset($wgMaxTocLevel) || $toclevel<$wgMaxTocLevel ) ) {
2201 $toc .= $sk->tocLine($anchor,$tocline,$toclevel);
2202 }
2203 if( $showEditLink ) {
2204 if ( empty( $head[$headlineCount] ) ) {
2205 $head[$headlineCount] = '';
2206 }
2207 $head[$headlineCount] .= $sk->editSectionLink($headlineCount+1);
2208 }
2209
2210 # Add the edit section span
2211 if( $rightClickHack ) {
2212 $headline = $sk->editSectionScript($headlineCount+1,$headline);
2213 }
2214
2215 # give headline the correct <h#> tag
2216 @$head[$headlineCount] .= "<a name=\"$anchor\"></a><h".$level.$matches[2][$headlineCount] .$headline."</h".$level.">";
2217
2218 $headlineCount++;
2219 }
2220
2221 if( $doShowToc ) {
2222 $toclines = $headlineCount;
2223 $toc .= $sk->tocUnindent( $toclevel );
2224 $toc = $sk->tocTable( $toc );
2225 }
2226
2227 # split up and insert constructed headlines
2228
2229 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
2230 $i = 0;
2231
2232 foreach( $blocks as $block ) {
2233 if( $showEditLink && $headlineCount > 0 && $i == 0 && $block != "\n" ) {
2234 # This is the [edit] link that appears for the top block of text when
2235 # section editing is enabled
2236
2237 # Disabled because it broke block formatting
2238 # For example, a bullet point in the top line
2239 # $full .= $sk->editSectionLink(0);
2240 }
2241 $full .= $block;
2242 if( $doShowToc && !$i && $isMain && !$forceTocHere) {
2243 # Top anchor now in skin
2244 $full = $full.$toc;
2245 }
2246
2247 if( !empty( $head[$i] ) ) {
2248 $full .= $head[$i];
2249 }
2250 $i++;
2251 }
2252 if($forceTocHere) {
2253 $mw =& MagicWord::get( MAG_TOC );
2254 return $mw->replace( $toc, $full );
2255 } else {
2256 return $full;
2257 }
2258 }
2259
2260 # Return an HTML link for the "ISBN 123456" text
2261 /* private */ function magicISBN( $text ) {
2262 global $wgLang;
2263 $fname = 'Parser::magicISBN';
2264 wfProfileIn( $fname );
2265
2266 $a = split( 'ISBN ', " $text" );
2267 if ( count ( $a ) < 2 ) {
2268 wfProfileOut( $fname );
2269 return $text;
2270 }
2271 $text = substr( array_shift( $a ), 1);
2272 $valid = '0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2273
2274 foreach ( $a as $x ) {
2275 $isbn = $blank = '' ;
2276 while ( ' ' == $x{0} ) {
2277 $blank .= ' ';
2278 $x = substr( $x, 1 );
2279 }
2280 while ( strstr( $valid, $x{0} ) != false ) {
2281 $isbn .= $x{0};
2282 $x = substr( $x, 1 );
2283 }
2284 $num = str_replace( '-', '', $isbn );
2285 $num = str_replace( ' ', '', $num );
2286
2287 if ( '' == $num ) {
2288 $text .= "ISBN $blank$x";
2289 } else {
2290 $titleObj = Title::makeTitle( NS_SPECIAL, 'Booksources' );
2291 $text .= '<a href="' .
2292 $titleObj->escapeLocalUrl( "isbn={$num}" ) .
2293 "\" class=\"internal\">ISBN $isbn</a>";
2294 $text .= $x;
2295 }
2296 }
2297 wfProfileOut( $fname );
2298 return $text;
2299 }
2300
2301 # Return an HTML link for the "GEO ..." text
2302 /* private */ function magicGEO( $text ) {
2303 global $wgLang, $wgUseGeoMode;
2304 if ( !isset ( $wgUseGeoMode ) || !$wgUseGeoMode ) return $text ;
2305 $fname = 'Parser::magicGEO';
2306 wfProfileIn( $fname );
2307
2308 # These next five lines are only for the ~35000 U.S. Census Rambot pages...
2309 $directions = array ( "N" => "North" , "S" => "South" , "E" => "East" , "W" => "West" ) ;
2310 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2311 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['N']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2312 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['W']}/" , "(GEO +\$1.\$2.\$3:-\$4.\$5.\$6)" , $text ) ;
2313 $text = preg_replace ( "/(\d+)&deg;(\d+)'(\d+)\" {$directions['S']}, (\d+)&deg;(\d+)'(\d+)\" {$directions['E']}/" , "(GEO +\$1.\$2.\$3:+\$4.\$5.\$6)" , $text ) ;
2314
2315 $a = split( 'GEO ', " $text" );
2316 if ( count ( $a ) < 2 ) {
2317 wfProfileOut( $fname );
2318 return $text;
2319 }
2320 $text = substr( array_shift( $a ), 1);
2321 $valid = '0123456789.+-:';
2322
2323 foreach ( $a as $x ) {
2324 $geo = $blank = '' ;
2325 while ( ' ' == $x{0} ) {
2326 $blank .= ' ';
2327 $x = substr( $x, 1 );
2328 }
2329 while ( strstr( $valid, $x{0} ) != false ) {
2330 $geo .= $x{0};
2331 $x = substr( $x, 1 );
2332 }
2333 $num = str_replace( '+', '', $geo );
2334 $num = str_replace( ' ', '', $num );
2335
2336 if ( '' == $num || count ( explode ( ":" , $num , 3 ) ) < 2 ) {
2337 $text .= "GEO $blank$x";
2338 } else {
2339 $titleObj = Title::makeTitle( NS_SPECIAL, 'Geo' );
2340 $text .= '<a href="' .
2341 $titleObj->escapeLocalUrl( "coordinates={$num}" ) .
2342 "\" class=\"internal\">GEO $geo</a>";
2343 $text .= $x;
2344 }
2345 }
2346 wfProfileOut( $fname );
2347 return $text;
2348 }
2349
2350 # Return an HTML link for the "RFC 1234" text
2351 /* private */ function magicRFC( $text ) {
2352 global $wgLang;
2353
2354 $a = split( 'RFC ', ' '.$text );
2355 if ( count ( $a ) < 2 ) return $text;
2356 $text = substr( array_shift( $a ), 1);
2357 $valid = '0123456789';
2358
2359 foreach ( $a as $x ) {
2360 $rfc = $blank = '' ;
2361 while ( ' ' == $x{0} ) {
2362 $blank .= ' ';
2363 $x = substr( $x, 1 );
2364 }
2365 while ( strstr( $valid, $x{0} ) != false ) {
2366 $rfc .= $x{0};
2367 $x = substr( $x, 1 );
2368 }
2369
2370 if ( '' == $rfc ) {
2371 $text .= "RFC $blank$x";
2372 } else {
2373 $url = wfmsg( 'rfcurl' );
2374 $url = str_replace( '$1', $rfc, $url);
2375 $sk =& $this->mOptions->getSkin();
2376 $la = $sk->getExternalLinkAttributes( $url, "RFC {$rfc}" );
2377 $text .= "<a href='{$url}'{$la}>RFC {$rfc}</a>{$x}";
2378 }
2379 }
2380 return $text;
2381 }
2382
2383 function preSaveTransform( $text, &$title, &$user, $options, $clearState = true ) {
2384 $this->mOptions = $options;
2385 $this->mTitle =& $title;
2386 $this->mOutputType = OT_WIKI;
2387
2388 if ( $clearState ) {
2389 $this->clearState();
2390 }
2391
2392 $stripState = false;
2393 $pairs = array(
2394 "\r\n" => "\n",
2395 );
2396 $text = str_replace(array_keys($pairs), array_values($pairs), $text);
2397 // now with regexes
2398 /*
2399 $pairs = array(
2400 "/<br.+(clear|break)=[\"']?(all|both)[\"']?\\/?>/i" => '<br style="clear:both;"/>',
2401 "/<br *?>/i" => "<br />",
2402 );
2403 $text = preg_replace(array_keys($pairs), array_values($pairs), $text);
2404 */
2405 $text = $this->strip( $text, $stripState, false );
2406 $text = $this->pstPass2( $text, $user );
2407 $text = $this->unstrip( $text, $stripState );
2408 $text = $this->unstripNoWiki( $text, $stripState );
2409 return $text;
2410 }
2411
2412 /* private */ function pstPass2( $text, &$user ) {
2413 global $wgLang, $wgLocaltimezone, $wgCurParser;
2414
2415 # Variable replacement
2416 # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
2417 $text = $this->replaceVariables( $text );
2418
2419 # Signatures
2420 #
2421 $n = $user->getName();
2422 $k = $user->getOption( 'nickname' );
2423 if ( '' == $k ) { $k = $n; }
2424 if(isset($wgLocaltimezone)) {
2425 $oldtz = getenv('TZ'); putenv('TZ='.$wgLocaltimezone);
2426 }
2427 /* Note: this is an ugly timezone hack for the European wikis */
2428 $d = $wgLang->timeanddate( date( 'YmdHis' ), false ) .
2429 ' (' . date( 'T' ) . ')';
2430 if(isset($wgLocaltimezone)) putenv('TZ='.$oldtzs);
2431
2432 $text = preg_replace( '/~~~~~/', $d, $text );
2433 $text = preg_replace( '/~~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]] $d", $text );
2434 $text = preg_replace( '/~~~/', '[[' . $wgLang->getNsText( NS_USER ) . ":$n|$k]]", $text );
2435
2436 # Context links: [[|name]] and [[name (context)|]]
2437 #
2438 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
2439 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
2440 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
2441 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
2442
2443 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
2444 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
2445 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
2446 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
2447 # [[ns:page (cont)|]]
2448 $context = "";
2449 $t = $this->mTitle->getText();
2450 if ( preg_match( $conpat, $t, $m ) ) {
2451 $context = $m[2];
2452 }
2453 $text = preg_replace( $p4, '[[\\1:\\2 (\\3)|\\2]]', $text );
2454 $text = preg_replace( $p1, '[[\\1 (\\2)|\\1]]', $text );
2455 $text = preg_replace( $p3, '[[\\1:\\2|\\2]]', $text );
2456
2457 if ( '' == $context ) {
2458 $text = preg_replace( $p2, '[[\\1]]', $text );
2459 } else {
2460 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
2461 }
2462
2463 /*
2464 $mw =& MagicWord::get( MAG_SUBST );
2465 $wgCurParser = $this->fork();
2466 $text = $mw->substituteCallback( $text, "wfBraceSubstitution" );
2467 $this->merge( $wgCurParser );
2468 */
2469
2470 # Trim trailing whitespace
2471 # MAG_END (__END__) tag allows for trailing
2472 # whitespace to be deliberately included
2473 $text = rtrim( $text );
2474 $mw =& MagicWord::get( MAG_END );
2475 $mw->matchAndRemove( $text );
2476
2477 return $text;
2478 }
2479
2480 # Set up some variables which are usually set up in parse()
2481 # so that an external function can call some class members with confidence
2482 function startExternalParse( &$title, $options, $outputType, $clearState = true ) {
2483 $this->mTitle =& $title;
2484 $this->mOptions = $options;
2485 $this->mOutputType = $outputType;
2486 if ( $clearState ) {
2487 $this->clearState();
2488 }
2489 }
2490
2491 function transformMsg( $text, $options ) {
2492 global $wgTitle;
2493 static $executing = false;
2494
2495 # Guard against infinite recursion
2496 if ( $executing ) {
2497 return $text;
2498 }
2499 $executing = true;
2500
2501 $this->mTitle = $wgTitle;
2502 $this->mOptions = $options;
2503 $this->mOutputType = OT_MSG;
2504 $this->clearState();
2505 $text = $this->replaceVariables( $text );
2506
2507 $executing = false;
2508 return $text;
2509 }
2510
2511 # Create an HTML-style tag, e.g. <yourtag>special text</yourtag>
2512 # Callback will be called with the text within
2513 # Transform and return the text within
2514 function setHook( $tag, $callback ) {
2515 $oldVal = @$this->mTagHooks[$tag];
2516 $this->mTagHooks[$tag] = $callback;
2517 return $oldVal;
2518 }
2519 }
2520
2521 class ParserOutput
2522 {
2523 var $mText, $mLanguageLinks, $mCategoryLinks, $mContainsOldMagic;
2524 var $mCacheTime; # Used in ParserCache
2525
2526 function ParserOutput( $text = "", $languageLinks = array(), $categoryLinks = array(),
2527 $containsOldMagic = false )
2528 {
2529 $this->mText = $text;
2530 $this->mLanguageLinks = $languageLinks;
2531 $this->mCategoryLinks = $categoryLinks;
2532 $this->mContainsOldMagic = $containsOldMagic;
2533 $this->mCacheTime = "";
2534 }
2535
2536 function getText() { return $this->mText; }
2537 function getLanguageLinks() { return $this->mLanguageLinks; }
2538 function getCategoryLinks() { return $this->mCategoryLinks; }
2539 function getCacheTime() { return $this->mCacheTime; }
2540 function containsOldMagic() { return $this->mContainsOldMagic; }
2541 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
2542 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
2543 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategoryLinks, $cl ); }
2544 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
2545 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
2546
2547 function merge( $other ) {
2548 $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $other->mLanguageLinks );
2549 $this->mCategoryLinks = array_merge( $this->mCategoryLinks, $this->mLanguageLinks );
2550 $this->mContainsOldMagic = $this->mContainsOldMagic || $other->mContainsOldMagic;
2551 }
2552
2553 }
2554
2555 class ParserOptions
2556 {
2557 # All variables are private
2558 var $mUseTeX; # Use texvc to expand <math> tags
2559 var $mUseCategoryMagic; # Treat [[Category:xxxx]] tags specially
2560 var $mUseDynamicDates; # Use $wgDateFormatter to format dates
2561 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
2562 var $mAllowExternalImages; # Allow external images inline
2563 var $mSkin; # Reference to the preferred skin
2564 var $mDateFormat; # Date format index
2565 var $mEditSection; # Create "edit section" links
2566 var $mEditSectionOnRightClick; # Generate JavaScript to edit section on right click
2567 var $mNumberHeadings; # Automatically number headings
2568 var $mShowToc; # Show table of contents
2569
2570 function getUseTeX() { return $this->mUseTeX; }
2571 function getUseCategoryMagic() { return $this->mUseCategoryMagic; }
2572 function getUseDynamicDates() { return $this->mUseDynamicDates; }
2573 function getInterwikiMagic() { return $this->mInterwikiMagic; }
2574 function getAllowExternalImages() { return $this->mAllowExternalImages; }
2575 function getSkin() { return $this->mSkin; }
2576 function getDateFormat() { return $this->mDateFormat; }
2577 function getEditSection() { return $this->mEditSection; }
2578 function getEditSectionOnRightClick() { return $this->mEditSectionOnRightClick; }
2579 function getNumberHeadings() { return $this->mNumberHeadings; }
2580 function getShowToc() { return $this->mShowToc; }
2581
2582 function setUseTeX( $x ) { return wfSetVar( $this->mUseTeX, $x ); }
2583 function setUseCategoryMagic( $x ) { return wfSetVar( $this->mUseCategoryMagic, $x ); }
2584 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
2585 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
2586 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
2587 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
2588 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
2589 function setEditSectionOnRightClick( $x ) { return wfSetVar( $this->mEditSectionOnRightClick, $x ); }
2590 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
2591 function setShowToc( $x ) { return wfSetVar( $this->mShowToc, $x ); }
2592
2593 function setSkin( &$x ) { $this->mSkin =& $x; }
2594
2595 /* static */ function newFromUser( &$user ) {
2596 $popts = new ParserOptions;
2597 $popts->initialiseFromUser( $user );
2598 return $popts;
2599 }
2600
2601 function initialiseFromUser( &$userInput ) {
2602 global $wgUseTeX, $wgUseCategoryMagic, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
2603
2604 if ( !$userInput ) {
2605 $user = new User;
2606 $user->setLoaded( true );
2607 } else {
2608 $user =& $userInput;
2609 }
2610
2611 $this->mUseTeX = $wgUseTeX;
2612 $this->mUseCategoryMagic = $wgUseCategoryMagic;
2613 $this->mUseDynamicDates = $wgUseDynamicDates;
2614 $this->mInterwikiMagic = $wgInterwikiMagic;
2615 $this->mAllowExternalImages = $wgAllowExternalImages;
2616 $this->mSkin =& $user->getSkin();
2617 $this->mDateFormat = $user->getOption( 'date' );
2618 $this->mEditSection = $user->getOption( 'editsection' );
2619 $this->mEditSectionOnRightClick = $user->getOption( 'editsectiononrightclick' );
2620 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
2621 $this->mShowToc = $user->getOption( 'showtoc' );
2622 }
2623
2624
2625 }
2626
2627 # Regex callbacks, used in Parser::replaceVariables
2628 function wfBraceSubstitution( $matches )
2629 {
2630 global $wgCurParser;
2631 return $wgCurParser->braceSubstitution( $matches );
2632 }
2633
2634 function wfArgSubstitution( $matches )
2635 {
2636 global $wgCurParser;
2637 return $wgCurParser->argSubstitution( $matches );
2638 }
2639
2640 function wfVariableSubstitution( $matches )
2641 {
2642 global $wgCurParser;
2643 return $wgCurParser->variableSubstitution( $matches );
2644 }
2645
2646 ?>