Bug 839664; use 'empty' to avoid notifications for people with insane PHP error repor...
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?
2 # See design.doc
3
4 if($wgUseTeX) include_once( "Math.php" );
5
6 class OutputPage {
7 var $mHeaders, $mCookies, $mMetatags, $mKeywords;
8 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
9 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
10 var $mSubtitle, $mRedirect, $mAutonumber, $mHeadtext;
11 var $mLastModified, $mCategoryLinks;
12
13 var $mDTopen, $mLastSection; # Used for processing DL, PRE
14 var $mLanguageLinks, $mSupressQuickbar;
15
16 function OutputPage()
17 {
18 $this->mHeaders = $this->mCookies = $this->mMetatags =
19 $this->mKeywords = $this->mLinktags = array();
20 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
21 $this->mLastSection = $this->mRedirect = $this->mLastModified =
22 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy = "";
23 $this->mIsarticle = $this->mPrintable = true;
24 $this->mSupressQuickbar = $this->mDTopen = $this->mPrintable = false;
25 $this->mLanguageLinks = array();
26 $this->mCategoryLinks = array() ;
27 $this->mAutonumber = 0;
28 }
29
30 function addHeader( $name, $val ) { array_push( $this->mHeaders, "$name: $val" ) ; }
31 function addCookie( $name, $val ) { array_push( $this->mCookies, array( $name, $val ) ); }
32 function redirect( $url ) { $this->mRedirect = $url; }
33
34 # To add an http-equiv meta tag, precede the name with "http:"
35 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
36 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
37 function addLink( $rel, $rev, $target ) { array_push( $this->mLinktags, array( $rel, $rev, $target ) ); }
38
39 function checkLastModified ( $timestamp )
40 {
41 global $wgLang, $wgCachePages, $wgUser;
42 if( !$wgCachePages ) {
43 wfDebug( "CACHE DISABLED\n", false );
44 return;
45 }
46 if( preg_match( '/MSIE ([1-4]|5\.0)/', $_SERVER["HTTP_USER_AGENT"] ) ) {
47 # IE 5.0 has probs with our caching
48 wfDebug( "-- bad client, not caching\n", false );
49 return;
50 }
51 if( $wgUser->getOption( "nocache" ) ) {
52 wfDebug( "USER DISABLED CACHE\n", false );
53 return;
54 }
55
56 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
57 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
58
59 if( !empty( $_SERVER["HTTP_IF_MODIFIED_SINCE"] ) ) {
60 # IE sends sizes after the date like this:
61 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
62 # this breaks strtotime().
63 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
64 $ismodsince = wfUnix2Timestamp( strtotime( $modsince ) );
65 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
66 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
67
68 if( ($ismodsince >= $timestamp ) and $wgUser->validateCache( $ismodsince ) ) {
69 # Make sure you're in a place you can leave when you call us!
70 header( "HTTP/1.0 304 Not Modified" );
71 header( "Expires: Mon, 15 Jan 2001 00:00:00 GMT" ); # Cachers always validate the page!
72 header( "Cache-Control: private, must-revalidate, max-age=0" );
73 header( "Last-Modified: {$lastmod}" );
74 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
75 $this->reportTime(); # For profiling
76 exit;
77 } else {
78 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
79 $this->mLastModified = $lastmod;
80 }
81 } else {
82 wfDebug( "We're confused.\n", false );
83 $this->mLastModified = $lastmod;
84 }
85 }
86
87 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
88 function setHTMLtitle( $name ) { $this->mHTMLtitle = $name; }
89 function setPageTitle( $name ) { $this->mPagetitle = $name; }
90 function getPageTitle() { return $this->mPagetitle; }
91 function setSubtitle( $str ) { $this->mSubtitle = $str; }
92 function getSubtitle() { return $this->mSubtitle; }
93 function setArticleFlag( $v ) { $this->mIsarticle = $v; }
94 function isArticle() { return $this->mIsarticle; }
95 function setPrintable() { $this->mPrintable = true; }
96 function isPrintable() { return $this->mPrintable; }
97
98 function getLanguageLinks() {
99 global $wgTitle, $wgLanguageCode;
100 global $wgDBconnection, $wgDBname;
101 return $this->mLanguageLinks;
102 }
103 function supressQuickbar() { $this->mSupressQuickbar = true; }
104 function isQuickbarSupressed() { return $this->mSupressQuickbar; }
105
106 function addHTML( $text ) { $this->mBodytext .= $text; }
107 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
108 function debug( $text ) { $this->mDebugtext .= $text; }
109
110 # First pass--just handle <nowiki> sections, pass the rest off
111 # to doWikiPass2() which does all the real work.
112 #
113
114 function addWikiText( $text, $linestart = true )
115 {
116 global $wgUseTeX;
117 $fname = "OutputPage::addWikiText";
118 wfProfileIn( $fname );
119 $unique = "3iyZiyA7iMwg5rhxP0Dcc9oTnj8qD1jm1Sfv4";
120 $unique2 = "4LIQ9nXtiYFPCSfitVwDw7EYwQlL4GeeQ7qSO";
121 $unique3 = "fPaA8gDfdLBqzj68Yjg9Hil3qEF8JGO0uszIp";
122 $nwlist = array();
123 $nwsecs = 0;
124 $mathlist = array();
125 $mathsecs = 0;
126 $prelist = array ();
127 $presecs = 0;
128 $stripped = "";
129 $stripped2 = "";
130 $stripped3 = "";
131
132 while ( "" != $text ) {
133 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
134 $stripped .= $p[0];
135 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
136 else {
137 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
138 ++$nwsecs;
139 $nwlist[$nwsecs] = wfEscapeHTMLTagsOnly($q[0]);
140 $stripped .= $unique;
141 $text = $q[1];
142 }
143 }
144
145 if( $wgUseTeX ) {
146 while ( "" != $stripped ) {
147 $p = preg_split( "/<\\s*math\\s*>/i", $stripped, 2 );
148 $stripped2 .= $p[0];
149 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $stripped = ""; }
150 else {
151 $q = preg_split( "/<\\/\\s*math\\s*>/i", $p[1], 2 );
152 ++$mathsecs;
153 $mathlist[$mathsecs] = renderMath($q[0]);
154 $stripped2 .= $unique2;
155 $stripped = $q[1];
156 }
157 }
158 } else {
159 $stripped2 = $stripped;
160 }
161
162 while ( "" != $stripped2 ) {
163 $p = preg_split( "/<\\s*pre\\s*>/i", $stripped2, 2 );
164 $stripped3 .= $p[0];
165 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $stripped2 = ""; }
166 else {
167 $q = preg_split( "/<\\/\\s*pre\\s*>/i", $p[1], 2 );
168 ++$presecs;
169 $prelist[$presecs] = "<pre>". wfEscapeHTMLTagsOnly($q[0]). "</pre>";
170 $stripped3 .= $unique3;
171 $stripped2 = $q[1];
172 }
173 }
174
175 $text = $this->doWikiPass2( $stripped3, $linestart );
176
177 $specialChars = array("\\", "$");
178 $escapedChars = array("\\\\", "\\$");
179 for ( $i = 1; $i <= $presecs; ++$i ) {
180 $text = preg_replace( "/{$unique3}/", str_replace( $specialChars,
181 $escapedChars, $prelist[$i] ), $text, 1 );
182 }
183
184 for ( $i = 1; $i <= $mathsecs; ++$i ) {
185 $text = preg_replace( "/{$unique2}/", str_replace( $specialChars,
186 $escapedChars, $mathlist[$i] ), $text, 1 );
187 }
188
189 for ( $i = 1; $i <= $nwsecs; ++$i ) {
190 $text = preg_replace( "/{$unique}/", str_replace( $specialChars,
191 $escapedChars, $nwlist[$i] ), $text, 1 );
192 }
193 $this->addHTML( $text );
194 wfProfileOut( $fname );
195 }
196
197 function sendCacheControl() {
198 global $wgUseGzip;
199 if( $this->mLastModified != "" ) {
200 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
201 header( "Cache-Control: private, must-revalidate, max-age=0" );
202 header( "Last-modified: {$this->mLastModified}" );
203 if( $wgUseGzip ) {
204 # We should put in Accept-Encoding, but IE chokes on anything but
205 # User-Agent in a Vary: header (at least through 6.0)
206 header( "Vary: User-Agent" );
207 }
208 } else {
209 wfDebug( "** no caching **\n", false );
210 header( "Cache-Control: no-cache" ); # Experimental - see below
211 header( "Pragma: no-cache" );
212 header( "Last-modified: " . gmdate( "D, j M Y H:i:s" ) . " GMT" );
213 }
214 header( "Expires: Mon, 15 Jan 2001 00:00:00 GMT" ); # Cachers always validate the page!
215 }
216
217 # Finally, all the text has been munged and accumulated into
218 # the object, let's actually output it:
219 #
220 function output()
221 {
222 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
223 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
224
225 $fname = "OutputPage::output";
226 wfProfileIn( $fname );
227
228 $sk = $wgUser->getSkin();
229
230 $this->sendCacheControl();
231
232 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
233 header( "Content-language: {$wgLanguageCode}" );
234
235 if ( "" != $this->mRedirect ) {
236 header( "Location: {$this->mRedirect}" );
237 return;
238 }
239
240 $exp = time() + $wgCookieExpiration;
241 foreach( $this->mCookies as $name => $val ) {
242 setcookie( $name, $val, $exp, "/" );
243 }
244
245 $sk->outputPage( $this );
246 flush();
247 }
248
249 function out( $ins )
250 {
251 global $wgInputEncoding, $wgOutputEncoding, $wgLang;
252 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
253 $outs = $ins;
254 } else {
255 $outs = $wgLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
256 if ( false === $outs ) { $outs = $ins; }
257 }
258 print $outs;
259 }
260
261 function setEncodings()
262 {
263 global $HTTP_SERVER_VARS, $wgInputEncoding, $wgOutputEncoding;
264 global $wgUser, $wgLang;
265
266 $wgInputEncoding = strtolower( $wgInputEncoding );
267
268 if( $wgUser->getOption( 'altencoding' ) ) {
269 $wgLang->setAltEncoding();
270 return;
271 }
272
273 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
274 $wgOutputEncoding = strtolower( $wgOutputEncoding );
275 return;
276 }
277
278 /*
279 # This code is unused anyway!
280 # Commenting out. --bv 2003-11-15
281
282 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
283 $best = 0.0;
284 $bestset = "*";
285
286 foreach ( $a as $s ) {
287 if ( preg_match( "/(.*);q=(.*)/", $s, $m ) ) {
288 $set = $m[1];
289 $q = (float)($m[2]);
290 } else {
291 $set = $s;
292 $q = 1.0;
293 }
294 if ( $q > $best ) {
295 $bestset = $set;
296 $best = $q;
297 }
298 }
299 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
300 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
301 $wgOutputEncoding = strtolower( $bestset );
302
303 # Disable for now
304 #
305 */
306 $wgOutputEncoding = $wgInputEncoding;
307 }
308
309 function reportTime()
310 {
311 global $wgRequestTime, $wgDebugLogFile, $HTTP_SERVER_VARS;
312 global $wgProfiling, $wgProfileStack, $wgProfileLimit, $wgUser;
313
314 list( $usec, $sec ) = explode( " ", microtime() );
315 $now = (float)$sec + (float)$usec;
316
317 list( $usec, $sec ) = explode( " ", $wgRequestTime );
318 $start = (float)$sec + (float)$usec;
319 $elapsed = $now - $start;
320
321 if ( "" != $wgDebugLogFile ) {
322 $prof = wfGetProfilingOutput( $start, $elapsed );
323 if( $forward = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'] )
324 $forward = " forwarded for $forward";
325 if( $client = $HTTP_SERVER_VARS['HTTP_CLIENT_IP'] )
326 $forward .= " client IP $client";
327 if( $from = $HTTP_SERVER_VARS['HTTP_FROM'] )
328 $forward .= " from $from";
329 if( $forward )
330 $forward = "\t(proxied via {$HTTP_SERVER_VARS['REMOTE_ADDR']}{$forward})";
331 if($wgUser->getId() == 0)
332 $forward .= " anon";
333 $log = sprintf( "%s\t%04.3f\t%s\n",
334 gmdate( "YmdHis" ), $elapsed,
335 urldecode( $HTTP_SERVER_VARS['REQUEST_URI'] . $forward ) );
336 error_log( $log . $prof, 3, $wgDebugLogFile );
337 }
338 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
339 $elapsed );
340 return $com;
341 }
342
343 # Note: these arguments are keys into wfMsg(), not text!
344 #
345 function errorpage( $title, $msg )
346 {
347 global $wgTitle;
348
349 $this->mDebugtext .= "Original title: " .
350 $wgTitle->getPrefixedText() . "\n";
351 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
352 $this->setPageTitle( wfMsg( $title ) );
353 $this->setRobotpolicy( "noindex,nofollow" );
354 $this->setArticleFlag( false );
355
356 $this->mBodytext = "";
357 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
358 $this->returnToMain( false );
359
360 $this->output();
361 exit;
362 }
363
364 function sysopRequired()
365 {
366 global $wgUser;
367
368 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
369 $this->setPageTitle( wfMsg( "sysoptitle" ) );
370 $this->setRobotpolicy( "noindex,nofollow" );
371 $this->setArticleFlag( false );
372 $this->mBodytext = "";
373
374 $sk = $wgUser->getSkin();
375 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
376 $this->addHTML( wfMsg( "sysoptext", $ap ) );
377 $this->returnToMain();
378 }
379
380 function developerRequired()
381 {
382 global $wgUser;
383
384 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
385 $this->setPageTitle( wfMsg( "developertitle" ) );
386 $this->setRobotpolicy( "noindex,nofollow" );
387 $this->setArticleFlag( false );
388 $this->mBodytext = "";
389
390 $sk = $wgUser->getSkin();
391 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
392 $this->addHTML( wfMsg( "developertext", $ap ) );
393 $this->returnToMain();
394 }
395
396 function databaseError( $fname )
397 {
398 global $wgUser, $wgCommandLineMode;
399
400 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
401 $this->setRobotpolicy( "noindex,nofollow" );
402 $this->setArticleFlag( false );
403
404 if ( $wgCommandLineMode ) {
405 $msg = wfMsgNoDB( "dberrortextcl" );
406 } else {
407 $msg = wfMsgNoDB( "dberrortext" );
408 }
409
410 $msg = str_replace( "$1", htmlspecialchars( wfLastDBquery() ), $msg );
411 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
412 $msg = str_replace( "$3", wfLastErrno(), $msg );
413 $msg = str_replace( "$4", htmlspecialchars( wfLastError() ), $msg );
414
415 if ( $wgCommandLineMode ) {
416 print "$msg\n";
417 exit();
418 }
419 $sk = $wgUser->getSkin();
420 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
421 wfMsgNoDB( "searchingwikipedia" ) );
422 $msg = str_replace( "$5", $shlink, $msg );
423
424 $this->mBodytext = $msg;
425 $this->output();
426 exit();
427 }
428
429 function readOnlyPage( $source = "", $protected = false )
430 {
431 global $wgUser, $wgReadOnlyFile;
432
433 $this->setRobotpolicy( "noindex,nofollow" );
434 $this->setArticleFlag( false );
435
436 if( $protected ) {
437 $this->setPageTitle( wfMsg( "viewsource" ) );
438 $this->addWikiText( wfMsg( "protectedtext" ) );
439 } else {
440 $this->setPageTitle( wfMsg( "readonly" ) );
441 $reason = file_get_contents( $wgReadOnlyFile );
442 $this->addHTML( wfMsg( "readonlytext", $reason ) );
443 }
444
445 if($source) {
446 $rows = $wgUser->getOption( "rows" );
447 $cols = $wgUser->getOption( "cols" );
448 $text .= "</p>\n<textarea cols='$cols' rows='$rows' readonly>" .
449 htmlspecialchars( $source ) . "\n</textarea>";
450 $this->addHTML( $text );
451 }
452
453 $this->returnToMain( false );
454 }
455
456 function fatalError( $message )
457 {
458 $this->setPageTitle( wfMsg( "internalerror" ) );
459 $this->setRobotpolicy( "noindex,nofollow" );
460 $this->setArticleFlag( false );
461
462 $this->mBodytext = $message;
463 $this->output();
464 exit;
465 }
466
467 function unexpectedValueError( $name, $val )
468 {
469 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
470 }
471
472 function fileCopyError( $old, $new )
473 {
474 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
475 }
476
477 function fileRenameError( $old, $new )
478 {
479 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
480 }
481
482 function fileDeleteError( $name )
483 {
484 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
485 }
486
487 function fileNotFoundError( $name )
488 {
489 $this->fatalError( wfMsg( "filenotfound", $name ) );
490 }
491
492 function returnToMain( $auto = true )
493 {
494 global $wgUser, $wgOut, $returnto;
495
496 $sk = $wgUser->getSkin();
497 if ( "" == $returnto ) {
498 $returnto = wfMsg( "mainpage" );
499 }
500 $link = $sk->makeKnownLink( $returnto, "" );
501
502 $r = wfMsg( "returnto", $link );
503 if ( $auto ) {
504 $wgOut->addMeta( "http:Refresh", "10;url=" .
505 wfLocalUrlE( wfUrlencode( $returnto ) ) );
506 }
507 $wgOut->addHTML( "\n<p>$r\n" );
508 }
509
510
511 function categoryMagic ()
512 {
513 global $wgTitle , $wgUseCategoryMagic ;
514 if ( !isset ( $wgUseCategoryMagic ) || !$wgUseCategoryMagic ) return ;
515 $id = $wgTitle->getArticleID() ;
516 $cat = ucfirst ( wfMsg ( "category" ) ) ;
517 $ti = $wgTitle->getText() ;
518 $ti = explode ( ":" , $ti , 2 ) ;
519 if ( $cat != $ti[0] ) return "" ;
520 $r = "<br break=all>\n" ;
521
522 $articles = array() ;
523 $parents = array () ;
524 $children = array() ;
525
526
527 global $wgUser ;
528 $sk = $wgUser->getSkin() ;
529 $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
530 $res = wfQuery ( $sql, DB_READ ) ;
531 while ( $x = wfFetchObject ( $res ) )
532 {
533 # $t = new Title ;
534 # $t->newFromDBkey ( $x->l_from ) ;
535 # $t = $t->getText() ;
536 $t = $x->l_from ;
537 $y = explode ( ":" , $t , 2 ) ;
538 if ( count ( $y ) == 2 && $y[0] == $cat ) {
539 array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
540 } else {
541 array_push ( $articles , $sk->makeLink ( $t ) ) ;
542 }
543 }
544 wfFreeResult ( $res ) ;
545
546 # Children
547 if ( count ( $children ) > 0 )
548 {
549 asort ( $children ) ;
550 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
551 $r .= implode ( ", " , $children ) ;
552 }
553
554 # Articles
555 if ( count ( $articles ) > 0 )
556 {
557 asort ( $articles ) ;
558 $h = wfMsg( "category_header", $ti[1] );
559 $r .= "<h2>{$h}</h2>\n" ;
560 $r .= implode ( ", " , $articles ) ;
561 }
562
563
564 return $r ;
565 }
566
567 function getHTMLattrs ()
568 {
569 $htmlattrs = array( # Allowed attributes--no scripting, etc.
570 "title", "align", "lang", "dir", "width", "height",
571 "bgcolor", "clear", /* BR */ "noshade", /* HR */
572 "cite", /* BLOCKQUOTE, Q */ "size", "face", "color",
573 /* FONT */ "type", "start", "value", "compact",
574 /* For various lists, mostly deprecated but safe */
575 "summary", "width", "border", "frame", "rules",
576 "cellspacing", "cellpadding", "valign", "char",
577 "charoff", "colgroup", "col", "span", "abbr", "axis",
578 "headers", "scope", "rowspan", "colspan", /* Tables */
579 "id", "class", "name", "style" /* For CSS */
580 );
581 return $htmlattrs ;
582 }
583
584 function fixTableTags ( $t )
585 {
586 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
587 $htmlattrs = $this->getHTMLattrs() ;
588
589 # Strip non-approved attributes from the tag
590 $t = preg_replace(
591 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
592 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
593 $t);
594
595 return trim ( $t ) ;
596 }
597
598 function doTableStuff ( $t )
599 {
600 $t = explode ( "\n" , $t ) ;
601 $td = array () ; # Is currently a td tag open?
602 $ltd = array () ; # Was it TD or TH?
603 $tr = array () ; # Is currently a tr tag open?
604 $ltr = array () ; # tr attributes
605 foreach ( $t AS $k => $x )
606 {
607 $x = rtrim ( $x ) ;
608 $fc = substr ( $x , 0 , 1 ) ;
609 if ( "{|" == substr ( $x , 0 , 2 ) )
610 {
611 $t[$k] = "<table " . $this->fixTableTags ( substr ( $x , 3 ) ) . ">" ;
612 array_push ( $td , false ) ;
613 array_push ( $ltd , "" ) ;
614 array_push ( $tr , false ) ;
615 array_push ( $ltr , "" ) ;
616 }
617 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
618 else if ( "|}" == substr ( $x , 0 , 2 ) )
619 {
620 $z = "</table>\n" ;
621 $l = array_pop ( $ltd ) ;
622 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
623 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
624 array_pop ( $ltr ) ;
625 $t[$k] = $z ;
626 }
627 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
628 {
629 $z = trim ( substr ( $x , 2 ) ) ;
630 $t[$k] = "<caption>{$z}</caption>\n" ;
631 }*/
632 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
633 {
634 $x = substr ( $x , 1 ) ;
635 while ( $x != "" && substr ( $x , 0 , 1 ) == '-' ) $x = substr ( $x , 1 ) ;
636 $z = "" ;
637 $l = array_pop ( $ltd ) ;
638 if ( array_pop ( $tr ) ) $z = "</tr>" . $z ;
639 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
640 array_pop ( $ltr ) ;
641 $t[$k] = $z ;
642 array_push ( $tr , false ) ;
643 array_push ( $td , false ) ;
644 array_push ( $ltd , "" ) ;
645 array_push ( $ltr , $this->fixTableTags ( $x ) ) ;
646 }
647 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
648 {
649 if ( "|+" == substr ( $x , 0 , 2 ) )
650 {
651 $fc = "+" ;
652 $x = substr ( $x , 1 ) ;
653 }
654 $after = substr ( $x , 1 ) ;
655 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
656 $after = explode ( "||" , $after ) ;
657 $t[$k] = "" ;
658 foreach ( $after AS $theline )
659 {
660 $z = "" ;
661 $tra = array_pop ( $ltr ) ;
662 if ( !array_pop ( $tr ) ) $z = "<tr {$tra}>\n" ;
663 array_push ( $tr , true ) ;
664 array_push ( $ltr , "" ) ;
665
666 $l = array_pop ( $ltd ) ;
667 if ( array_pop ( $td ) ) $z = "</{$l}>" . $z ;
668 if ( $fc == "|" ) $l = "TD" ;
669 else if ( $fc == "!" ) $l = "TH" ;
670 else if ( $fc == "+" ) $l = "CAPTION" ;
671 else $l = "" ;
672 array_push ( $ltd , $l ) ;
673 $y = explode ( "|" , $theline , 2 ) ;
674 if ( count ( $y ) == 1 ) $y = "{$z}<{$l}>{$y[0]}" ;
675 else $y = $y = "{$z}<{$l} ".$this->fixTableTags($y[0]).">{$y[1]}" ;
676 $t[$k] .= $y ;
677 array_push ( $td , true ) ;
678 }
679 }
680 }
681
682 # Closing open td, tr && table
683 while ( count ( $td ) > 0 )
684 {
685 if ( array_pop ( $td ) ) $t[] = "</td>" ;
686 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
687 $t[] = "</table>" ;
688 }
689
690 $t = implode ( "\n" , $t ) ;
691 # $t = $this->removeHTMLtags( $t );
692 return $t ;
693 }
694
695 # Well, OK, it's actually about 14 passes. But since all the
696 # hard lifting is done inside PHP's regex code, it probably
697 # wouldn't speed things up much to add a real parser.
698 #
699 function doWikiPass2( $text, $linestart )
700 {
701 global $wgUser, $wgLang, $wgUseDynamicDates;
702 $fname = "OutputPage::doWikiPass2";
703 wfProfileIn( $fname );
704
705 $text = $this->removeHTMLtags( $text );
706 $text = $this->replaceVariables( $text );
707
708 $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
709 $text = str_replace ( "<HR>", "<hr>", $text );
710
711 $text = $this->doAllQuotes( $text );
712 $text = $this->doHeadings( $text );
713 $text = $this->doBlockLevels( $text, $linestart );
714
715 if($wgUseDynamicDates) {
716 $text = $wgLang->replaceDates( $text );
717 }
718
719 $text = $this->replaceExternalLinks( $text );
720 $text = $this->replaceInternalLinks ( $text );
721 $text = $this->doTableStuff ( $text ) ;
722
723 $text = $this->magicISBN( $text );
724 $text = $this->magicRFC( $text );
725 $text = $this->formatHeadings( $text );
726
727 $sk = $wgUser->getSkin();
728 $text = $sk->transformContent( $text );
729 $text .= $this->categoryMagic () ;
730
731 wfProfileOut( $fname );
732 return $text;
733 }
734
735 /* private */ function doAllQuotes( $text )
736 {
737 $outtext = "";
738 $lines = explode( "\r\n", $text );
739 foreach ( $lines as $line ) {
740 $outtext .= $this->doQuotes ( "", $line, "" ) . "\r\n";
741 }
742 return $outtext;
743 }
744
745 /* private */ function doQuotes( $pre, $text, $mode )
746 {
747 if ( preg_match( "/^(.*)''(.*)$/sU", $text, $m ) ) {
748 $m1_strong = ($m[1] == "") ? "" : "<strong>{$m[1]}</strong>";
749 $m1_em = ($m[1] == "") ? "" : "<em>{$m[1]}</em>";
750 if ( substr ($m[2], 0, 1) == "'" ) {
751 $m[2] = substr ($m[2], 1);
752 if ($mode == "em") {
753 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ? "both" : "emstrong" );
754 } else if ($mode == "strong") {
755 return $m1_strong . $this->doQuotes ( "", $m[2], "" );
756 } else if (($mode == "emstrong") || ($mode == "both")) {
757 return $this->doQuotes ( "", $pre.$m1_strong.$m[2], "em" );
758 } else if ($mode == "strongem") {
759 return "<strong>{$pre}{$m1_em}</strong>" . $this->doQuotes ( "", $m[2], "em" );
760 } else {
761 return $m[1] . $this->doQuotes ( "", $m[2], "strong" );
762 }
763 } else {
764 if ($mode == "strong") {
765 return $this->doQuotes ( $m[1], $m[2], ($m[1] == "") ? "both" : "strongem" );
766 } else if ($mode == "em") {
767 return $m1_em . $this->doQuotes ( "", $m[2], "" );
768 } else if ($mode == "emstrong") {
769 return "<em>{$pre}{$m1_strong}</em>" . $this->doQuotes ( "", $m[2], "strong" );
770 } else if (($mode == "strongem") || ($mode == "both")) {
771 return $this->doQuotes ( "", $pre.$m1_em.$m[2], "strong" );
772 } else {
773 return $m[1] . $this->doQuotes ( "", $m[2], "em" );
774 }
775 }
776 } else {
777 $text_strong = ($text == "") ? "" : "<strong>{$text}</strong>";
778 $text_em = ($text == "") ? "" : "<em>{$text}</em>";
779 if ($mode == "") {
780 return $pre . $text;
781 } else if ($mode == "em") {
782 return $pre . $text_em;
783 } else if ($mode == "strong") {
784 return $pre . $text_strong;
785 } else if ($mode == "strongem") {
786 return (($pre == "") && ($text == "")) ? "" : "<strong>{$pre}{$text_em}</strong>";
787 } else {
788 return (($pre == "") && ($text == "")) ? "" : "<em>{$pre}{$text_strong}</em>";
789 }
790 }
791 }
792
793 /* private */ function doHeadings( $text )
794 {
795 for ( $i = 6; $i >= 1; --$i ) {
796 $h = substr( "======", 0, $i );
797 $text = preg_replace( "/^{$h}([^=]+){$h}(\\s|$)/m",
798 "<h{$i}>\\1</h{$i}>\\2", $text );
799 }
800 return $text;
801 }
802
803 # Note: we have to do external links before the internal ones,
804 # and otherwise take great care in the order of things here, so
805 # that we don't end up interpreting some URLs twice.
806
807 /* private */ function replaceExternalLinks( $text )
808 {
809 $fname = "OutputPage::replaceExternalLinks";
810 wfProfileIn( $fname );
811 $text = $this->subReplaceExternalLinks( $text, "http", true );
812 $text = $this->subReplaceExternalLinks( $text, "https", true );
813 $text = $this->subReplaceExternalLinks( $text, "ftp", false );
814 $text = $this->subReplaceExternalLinks( $text, "gopher", false );
815 $text = $this->subReplaceExternalLinks( $text, "news", false );
816 $text = $this->subReplaceExternalLinks( $text, "mailto", false );
817 wfProfileOut( $fname );
818 return $text;
819 }
820
821 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
822 {
823 global $wgUser, $printable;
824 global $wgAllowExternalImages;
825
826
827 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
828 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
829
830 # this is the list of separators that should be ignored if they
831 # are the last character of an URL but that should be included
832 # if they occur within the URL, e.g. "go to www.foo.com, where .."
833 # in this case, the last comma should not become part of the URL,
834 # but in "www.foo.com/123,2342,32.htm" it should.
835 $sep = ",;\.:";
836 $fnc = "A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF";
837 $images = "gif|png|jpg|jpeg";
838
839 # PLEASE NOTE: The curly braces { } are not part of the regex,
840 # they are interpreted as part of the string (used to tell PHP
841 # that the content of the string should be inserted there).
842 $e1 = "/(^|[^\\[])({$protocol}:)([{$uc}{$sep}]+)\\/([{$fnc}]+)\\." .
843 "((?i){$images})([^{$uc}]|$)/";
844
845 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
846 $sk = $wgUser->getSkin();
847
848 if ( $autonumber and $wgAllowExternalImages) { # Use img tags only for HTTP urls
849 $s = preg_replace( $e1, "\\1" . $sk->makeImage( "{$unique}:\\3" .
850 "/\\4.\\5", "\\4.\\5" ) . "\\6", $s );
851 }
852 $s = preg_replace( $e2, "\\1" . "<a href=\"{$unique}:\\3\"" .
853 $sk->getExternalLinkAttributes( "{$unique}:\\3", wfEscapeHTML(
854 "{$unique}:\\3" ) ) . ">" . wfEscapeHTML( "{$unique}:\\3" ) .
855 "</a>\\5", $s );
856 $s = str_replace( $unique, $protocol, $s );
857
858 $a = explode( "[{$protocol}:", " " . $s );
859 $s = array_shift( $a );
860 $s = substr( $s, 1 );
861
862 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
863 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
864
865 foreach ( $a as $line ) {
866 if ( preg_match( $e1, $line, $m ) ) {
867 $link = "{$protocol}:{$m[1]}";
868 $trail = $m[2];
869 if ( $autonumber ) { $text = "[" . ++$this->mAutonumber . "]"; }
870 else { $text = wfEscapeHTML( $link ); }
871 } else if ( preg_match( $e2, $line, $m ) ) {
872 $link = "{$protocol}:{$m[1]}";
873 $text = $m[2];
874 $trail = $m[3];
875 } else {
876 $s .= "[{$protocol}:" . $line;
877 continue;
878 }
879 if ( $printable == "yes") $paren = " (<i>" . htmlspecialchars ( $link ) . "</i>)";
880 else $paren = "";
881 $la = $sk->getExternalLinkAttributes( $link, $text );
882 $s .= "<a href='{$link}'{$la}>{$text}</a>{$paren}{$trail}";
883
884 }
885 return $s;
886 }
887
888 /* private */ function replaceInternalLinks( $s )
889 {
890 global $wgTitle, $wgUser, $wgLang;
891 global $wgLinkCache, $wgInterwikiMagic, $wgUseCategoryMagic;
892 global $wgNamespacesWithSubpages, $wgLanguageCode;
893 wfProfileIn( $fname = "OutputPage::replaceInternalLinks" );
894
895 wfProfileIn( "$fname-setup" );
896 $tc = Title::legalChars() . "#";
897 $sk = $wgUser->getSkin();
898
899 $a = explode( "[[", " " . $s );
900 $s = array_shift( $a );
901 $s = substr( $s, 1 );
902
903 $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD";
904
905 # Special and Media are pseudo-namespaces; no pages actually exist in them
906 $image = Namespace::getImage();
907 $special = Namespace::getSpecial();
908 $media = Namespace::getMedia();
909 $nottalk = !Namespace::isTalk( $wgTitle->getNamespace() );
910 wfProfileOut( "$fname-setup" );
911
912 foreach ( $a as $line ) {
913 if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
914 $text = $m[2];
915 $trail = $m[3];
916 } else { # Invalid form; output directly
917 $s .= "[[" . $line ;
918 continue;
919 }
920
921 /* Valid link forms:
922 Foobar -- normal
923 :Foobar -- override special treatment of prefix (images, language links)
924 /Foobar -- convert to CurrentPage/Foobar
925 /Foobar/ -- convert to CurrentPage/Foobar, strip the initial / from text
926 */
927 $c = substr($m[1],0,1);
928 $noforce = ($c != ":");
929 if( $c == "/" ) { # subpage
930 if(substr($m[1],-1,1)=="/") { # / at end means we don't want the slash to be shown
931 $m[1]=substr($m[1],1,strlen($m[1])-2);
932 $noslash=$m[1];
933 } else {
934 $noslash=substr($m[1],1);
935 }
936 if($wgNamespacesWithSubpages[$wgTitle->getNamespace()]) { # subpages allowed here
937 $link = $wgTitle->getPrefixedText(). "/" . trim($noslash);
938 if(!$text) {
939 $text= $m[1];
940 } # this might be changed for ugliness reasons
941 } else {
942 $link = $noslash; # no subpage allowed, use standard link
943 }
944 } elseif( $noforce ) { # no subpage
945 $link = $m[1];
946 } else {
947 $link = substr( $m[1], 1 );
948 }
949 if( empty( $text ) )
950 $text = $link;
951
952 $nt = Title::newFromText( $link );
953 if( !$nt ) {
954 $s .= "[[" . $line;
955 continue;
956 }
957 $ns = $nt->getNamespace();
958 $iw = $nt->getInterWiki();
959 if( $noforce ) {
960 if( $iw && $wgInterwikiMagic && $nottalk && $wgLang->getLanguageName( $iw ) ) {
961 array_push( $this->mLanguageLinks, $nt->getPrefixedText() );
962 $s .= $trail;
963 /* CHECK MERGE @@@
964 } else if ( "media" == $pre ) {
965 $nt = Title::newFromText( $suf );
966 $name = $nt->getDBkey();
967 if ( "" == $text ) { $text = $nt->GetText(); }
968
969 $wgLinkCache->addImageLink( $name );
970 $s .= $sk->makeMediaLink( $name,
971 wfImageUrl( $name ), $text );
972 $s .= $trail;
973 } else if ( isset($wgUseCategoryMagic) && $wgUseCategoryMagic && $pre == wfMsg ( "category" ) ) {
974 $l = $sk->makeLink ( $pre.":".ucfirst( $m[2] ), ucfirst ( $m[2] ) ) ;
975 array_push ( $this->mCategoryLinks , $l ) ;
976 $s .= $trail ;
977 } else {
978 $l = $wgLang->getLanguageName( $pre );
979 if ( "" == $l or !$wgInterwikiMagic or Namespace::isTalk( $wgTitle->getNamespace() ) ) {
980 if ( "" == $text ) {
981 $text = $link;
982 }
983 $s .= $sk->makeLink( $link, $text, "", $trail );
984 } else if ( $pre != $wgLanguageCode ) {
985 array_push( $this->mLanguageLinks, "$pre:$suf" );
986 $s .= $trail;
987 }
988 */
989 continue;
990 }
991 if( $ns == $image ) {
992 $s .= $sk->makeImageLinkObj( $nt, $text ) . $trail;
993 $wgLinkCache->addImageLinkObj( $nt );
994 continue;
995 }
996 /* CHECK MERGE @@@
997 # } else if ( 0 == strcmp( "##", substr( $link, 0, 2 ) ) ) {
998 # $link = substr( $link, 2 );
999 # $s .= "<a name=\"{$link}\">{$text}</a>{$trail}";
1000 } else {
1001 if ( "" == $text ) { $text = $link; }
1002 # Hotspot:
1003 $s .= $sk->makeLink( $link, $text, "", $trail );
1004 */
1005 }
1006 if( $ns == $media ) {
1007 $s .= $sk->makeMediaLinkObj( $nt, $text ) . $trail;
1008 $wgLinkCache->addImageLinkObj( $nt );
1009 continue;
1010 } elseif( $ns == $special ) {
1011 $s .= $sk->makeKnownLinkObj( $nt, $text, "", $trail );
1012 continue;
1013 }
1014 $s .= $sk->makeLinkObj( $nt, $text, "", $trail );
1015 }
1016 wfProfileOut( $fname );
1017 return $s;
1018 }
1019
1020 # Some functions here used by doBlockLevels()
1021 #
1022 /* private */ function closeParagraph()
1023 {
1024 $result = "";
1025 if ( 0 != strcmp( "p", $this->mLastSection ) &&
1026 0 != strcmp( "", $this->mLastSection ) ) {
1027 $result = "</" . $this->mLastSection . ">";
1028 }
1029 $this->mLastSection = "";
1030 return $result;
1031 }
1032 # getCommon() returns the length of the longest common substring
1033 # of both arguments, starting at the beginning of both.
1034 #
1035 /* private */ function getCommon( $st1, $st2 )
1036 {
1037 $fl = strlen( $st1 );
1038 $shorter = strlen( $st2 );
1039 if ( $fl < $shorter ) { $shorter = $fl; }
1040
1041 for ( $i = 0; $i < $shorter; ++$i ) {
1042 if ( $st1{$i} != $st2{$i} ) { break; }
1043 }
1044 return $i;
1045 }
1046 # These next three functions open, continue, and close the list
1047 # element appropriate to the prefix character passed into them.
1048 #
1049 /* private */ function openList( $char )
1050 {
1051 $result = $this->closeParagraph();
1052
1053 if ( "*" == $char ) { $result .= "<ul><li>"; }
1054 else if ( "#" == $char ) { $result .= "<ol><li>"; }
1055 else if ( ":" == $char ) { $result .= "<dl><dd>"; }
1056 else if ( ";" == $char ) {
1057 $result .= "<dl><dt>";
1058 $this->mDTopen = true;
1059 }
1060 else { $result = "<!-- ERR 1 -->"; }
1061
1062 return $result;
1063 }
1064
1065 /* private */ function nextItem( $char )
1066 {
1067 if ( "*" == $char || "#" == $char ) { return "</li><li>"; }
1068 else if ( ":" == $char || ";" == $char ) {
1069 $close = "</dd>";
1070 if ( $this->mDTopen ) { $close = "</dt>"; }
1071 if ( ";" == $char ) {
1072 $this->mDTopen = true;
1073 return $close . "<dt>";
1074 } else {
1075 $this->mDTopen = false;
1076 return $close . "<dd>";
1077 }
1078 }
1079 return "<!-- ERR 2 -->";
1080 }
1081
1082 /* private */function closeList( $char )
1083 {
1084 if ( "*" == $char ) { return "</li></ul>"; }
1085 else if ( "#" == $char ) { return "</li></ol>"; }
1086 else if ( ":" == $char ) {
1087 if ( $this->mDTopen ) {
1088 $this->mDTopen = false;
1089 return "</dt></dl>";
1090 } else {
1091 return "</dd></dl>";
1092 }
1093 }
1094 return "<!-- ERR 3 -->";
1095 }
1096
1097 /* private */ function doBlockLevels( $text, $linestart )
1098 {
1099 $fname = "OutputPage::doBlockLevels";
1100 wfProfileIn( $fname );
1101 # Parsing through the text line by line. The main thing
1102 # happening here is handling of block-level elements p, pre,
1103 # and making lists from lines starting with * # : etc.
1104 #
1105 $a = explode( "\n", $text );
1106 $text = $lastPref = "";
1107 $this->mDTopen = $inBlockElem = false;
1108
1109 if ( ! $linestart ) { $text .= array_shift( $a ); }
1110 foreach ( $a as $t ) {
1111 if ( "" != $text ) { $text .= "\n"; }
1112
1113 $oLine = $t;
1114 $opl = strlen( $lastPref );
1115 $npl = strspn( $t, "*#:;" );
1116 $pref = substr( $t, 0, $npl );
1117 $pref2 = str_replace( ";", ":", $pref );
1118 $t = substr( $t, $npl );
1119
1120 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
1121 $text .= $this->nextItem( substr( $pref, -1 ) );
1122
1123 if ( ";" == substr( $pref, -1 ) ) {
1124 $cpos = strpos( $t, ":" );
1125 if ( ! ( false === $cpos ) ) {
1126 $term = substr( $t, 0, $cpos );
1127 $text .= $term . $this->nextItem( ":" );
1128 $t = substr( $t, $cpos + 1 );
1129 }
1130 }
1131 } else if (0 != $npl || 0 != $opl) {
1132 $cpl = $this->getCommon( $pref, $lastPref );
1133
1134 while ( $cpl < $opl ) {
1135 $text .= $this->closeList( $lastPref{$opl-1} );
1136 --$opl;
1137 }
1138 if ( $npl <= $cpl && $cpl > 0 ) {
1139 $text .= $this->nextItem( $pref{$cpl-1} );
1140 }
1141 while ( $npl > $cpl ) {
1142 $char = substr( $pref, $cpl, 1 );
1143 $text .= $this->openList( $char );
1144
1145 if ( ";" == $char ) {
1146 $cpos = strpos( $t, ":" );
1147 if ( ! ( false === $cpos ) ) {
1148 $term = substr( $t, 0, $cpos );
1149 $text .= $term . $this->nextItem( ":" );
1150 $t = substr( $t, $cpos + 1 );
1151 }
1152 }
1153 ++$cpl;
1154 }
1155 $lastPref = $pref2;
1156 }
1157 if ( 0 == $npl ) { # No prefix--go to paragraph mode
1158 if ( preg_match(
1159 "/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6)/i", $t ) ) {
1160 $text .= $this->closeParagraph();
1161 $inBlockElem = true;
1162 }
1163 if ( ! $inBlockElem ) {
1164 if ( " " == $t{0} ) {
1165 $newSection = "pre";
1166 # $t = wfEscapeHTML( $t );
1167 }
1168 else { $newSection = "p"; }
1169
1170 if ( 0 == strcmp( "", trim( $oLine ) ) ) {
1171 $text .= $this->closeParagraph();
1172 $text .= "<" . $newSection . ">";
1173 } else if ( 0 != strcmp( $this->mLastSection,
1174 $newSection ) ) {
1175 $text .= $this->closeParagraph();
1176 if ( 0 != strcmp( "p", $newSection ) ) {
1177 $text .= "<" . $newSection . ">";
1178 }
1179 }
1180 $this->mLastSection = $newSection;
1181 }
1182 if ( $inBlockElem &&
1183 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6)/i", $t ) ) {
1184 $inBlockElem = false;
1185 }
1186 }
1187 $text .= $t;
1188 }
1189 while ( $npl ) {
1190 $text .= $this->closeList( $pref2{$npl-1} );
1191 --$npl;
1192 }
1193 if ( "" != $this->mLastSection ) {
1194 if ( "p" != $this->mLastSection ) {
1195 $text .= "</" . $this->mLastSection . ">";
1196 }
1197 $this->mLastSection = "";
1198 }
1199 wfProfileOut( $fname );
1200 return $text;
1201 }
1202
1203 /* private */ function replaceVariables( $text )
1204 {
1205 global $wgLang;
1206 $fname = "OutputPage::replaceVariables";
1207 wfProfileIn( $fname );
1208
1209
1210 # Basic variables
1211 # See Language.php for the definition of each magic word
1212
1213 # As with sigs, this uses the server's local time -- ensure
1214 # this is appropriate for your audience!
1215 $v = date( "m" );
1216 $mw =& MagicWord::get( MAG_CURRENTMONTH );
1217 $text = $mw->replace( $v, $text );
1218
1219 $v = $wgLang->getMonthName( date( "n" ) );
1220 $mw =& MagicWord::get( MAG_CURRENTMONTHNAME );
1221 $text = $mw->replace( $v, $text );
1222
1223 $v = $wgLang->getMonthNameGen( date( "n" ) );
1224 $mw =& MagicWord::get( MAG_CURRENTMONTHNAMEGEN );
1225 $text = $mw->replace( $v, $text );
1226
1227 $v = date( "j" );
1228 $mw = MagicWord::get( MAG_CURRENTDAY );
1229 $text = $mw->replace( $v, $text );
1230
1231 $v = $wgLang->getWeekdayName( date( "w" )+1 );
1232 $mw =& MagicWord::get( MAG_CURRENTDAYNAME );
1233 $text = $mw->replace( $v, $text );
1234
1235 $v = date( "Y" );
1236 $mw =& MagicWord::get( MAG_CURRENTYEAR );
1237 $text = $mw->replace( $v, $text );
1238
1239 $v = $wgLang->time( wfTimestampNow(), false );
1240 $mw =& MagicWord::get( MAG_CURRENTTIME );
1241 $text = $mw->replace( $v, $text );
1242
1243 $mw =& MagicWord::get( MAG_NUMBEROFARTICLES );
1244 if ( $mw->match( $text ) ) {
1245 $v = wfNumberOfArticles();
1246 $text = $mw->replace( $v, $text );
1247 }
1248
1249 # "Variables" with an additional parameter e.g. {{MSG:wikipedia}}
1250 # The callbacks are at the bottom of this file
1251 $mw =& MagicWord::get( MAG_MSG );
1252 $text = $mw->substituteCallback( $text, "wfReplaceMsgVar" );
1253
1254 $mw =& MagicWord::get( MAG_MSGNW );
1255 $text = $mw->substituteCallback( $text, "wfReplaceMsgnwVar" );
1256
1257 wfProfileOut( $fname );
1258 return $text;
1259 }
1260
1261 # Cleans up HTML, removes dangerous tags and attributes
1262 /* private */ function removeHTMLtags( $text )
1263 {
1264 $fname = "OutputPage::removeHTMLtags";
1265 wfProfileIn( $fname );
1266 $htmlpairs = array( # Tags that must be closed
1267 "b", "i", "u", "font", "big", "small", "sub", "sup", "h1",
1268 "h2", "h3", "h4", "h5", "h6", "cite", "code", "em", "s",
1269 "strike", "strong", "tt", "var", "div", "center",
1270 "blockquote", "ol", "ul", "dl", "table", "caption", "pre",
1271 "ruby", "rt" , "rb" , "rp"
1272 );
1273 $htmlsingle = array(
1274 "br", "p", "hr", "li", "dt", "dd"
1275 );
1276 $htmlnest = array( # Tags that can be nested--??
1277 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1278 "dl", "font", "big", "small", "sub", "sup"
1279 );
1280 $tabletags = array( # Can only appear inside table
1281 "td", "th", "tr"
1282 );
1283
1284 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1285 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1286
1287 $htmlattrs = $this->getHTMLattrs () ;
1288
1289 # Remove HTML comments
1290 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1291
1292 $bits = explode( "<", $text );
1293 $text = array_shift( $bits );
1294 $tagstack = array(); $tablestack = array();
1295
1296 foreach ( $bits as $x ) {
1297 $prev = error_reporting( E_ALL & ~( E_NOTICE | E_WARNING ) );
1298 preg_match( "/^(\\/?)(\\w+)([^>]*)(\\/{0,1}>)([^<]*)$/",
1299 $x, $regs );
1300 list( $qbar, $slash, $t, $params, $brace, $rest ) = $regs;
1301 error_reporting( $prev );
1302
1303 $badtag = 0 ;
1304 if ( in_array( $t = strtolower( $t ), $htmlelements ) ) {
1305 # Check our stack
1306 if ( $slash ) {
1307 # Closing a tag...
1308 if ( ! in_array( $t, $htmlsingle ) &&
1309 ( $ot = array_pop( $tagstack ) ) != $t ) {
1310 array_push( $tagstack, $ot );
1311 $badtag = 1;
1312 } else {
1313 if ( $t == "table" ) {
1314 $tagstack = array_pop( $tablestack );
1315 }
1316 $newparams = "";
1317 }
1318 } else {
1319 # Keep track for later
1320 if ( in_array( $t, $tabletags ) &&
1321 ! in_array( "table", $tagstack ) ) {
1322 $badtag = 1;
1323 } else if ( in_array( $t, $tagstack ) &&
1324 ! in_array ( $t , $htmlnest ) ) {
1325 $badtag = 1 ;
1326 } else if ( ! in_array( $t, $htmlsingle ) ) {
1327 if ( $t == "table" ) {
1328 array_push( $tablestack, $tagstack );
1329 $tagstack = array();
1330 }
1331 array_push( $tagstack, $t );
1332 }
1333 # Strip non-approved attributes from the tag
1334 $newparams = preg_replace(
1335 "/(\\w+)(\\s*=\\s*([^\\s\">]+|\"[^\">]*\"))?/e",
1336 "(in_array(strtolower(\"\$1\"),\$htmlattrs)?(\"\$1\".((\"x\$3\" != \"x\")?\"=\$3\":'')):'')",
1337 $params);
1338 }
1339 if ( ! $badtag ) {
1340 $rest = str_replace( ">", "&gt;", $rest );
1341 $text .= "<$slash$t$newparams$brace$rest";
1342 continue;
1343 }
1344 }
1345 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1346 }
1347 # Close off any remaining tags
1348 while ( $t = array_pop( $tagstack ) ) {
1349 $text .= "</$t>\n";
1350 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1351 }
1352 wfProfileOut( $fname );
1353 return $text;
1354 }
1355
1356
1357 /*
1358 *
1359 * This function accomplishes several tasks:
1360 * 1) Auto-number headings if that option is enabled
1361 * 2) Add an [edit] link to sections for logged in users who have enabled the option
1362 * 3) Add a Table of contents on the top for users who have enabled the option
1363 * 4) Auto-anchor headings
1364 *
1365 * It loops through all headlines, collects the necessary data, then splits up the
1366 * string and re-inserts the newly formatted headlines.
1367 *
1368 * */
1369 /* private */ function formatHeadings( $text )
1370 {
1371 global $wgUser,$wgArticle,$wgTitle,$wpPreview;
1372 $nh=$wgUser->getOption( "numberheadings" );
1373 $st=$wgUser->getOption( "showtoc" );
1374 if(!$wgTitle->userCanEdit()) {
1375 $es=0;
1376 $esr=0;
1377 } else {
1378 $es=$wgUser->getID() && $wgUser->getOption( "editsection" );
1379 $esr=$wgUser->getID() && $wgUser->getOption( "editsectiononrightclick" );
1380 }
1381
1382 # Inhibit editsection links if requested in the page
1383 if ($es) {
1384 $esw=& MagicWord::get(MAG_NOEDITSECTION);
1385 if ($esw->matchAndRemove( $text )) {
1386 $es=0;
1387 }
1388 }
1389 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
1390 # do not add TOC
1391 $mw =& MagicWord::get( MAG_NOTOC );
1392 if ($mw->matchAndRemove( $text ))
1393 {
1394 $st = 0;
1395 }
1396
1397 # never add the TOC to the Main Page. This is an entry page that should not
1398 # be more than 1-2 screens large anyway
1399 if($wgTitle->getPrefixedText()==wfMsg("mainpage")) {$st=0;}
1400
1401 # We need this to perform operations on the HTML
1402 $sk=$wgUser->getSkin();
1403
1404 # Get all headlines for numbering them and adding funky stuff like [edit]
1405 # links
1406 preg_match_all("/<H([1-6])(.*?>)(.*?)<\/H[1-6]>/i",$text,$matches);
1407
1408 # headline counter
1409 $c=0;
1410
1411 # Ugh .. the TOC should have neat indentation levels which can be
1412 # passed to the skin functions. These are determined here
1413 foreach($matches[3] as $headline) {
1414 if($level) { $prevlevel=$level;}
1415 $level=$matches[1][$c];
1416 if(($nh||$st) && $prevlevel && $level>$prevlevel) {
1417
1418 $h[$level]=0; // reset when we enter a new level
1419 $toc.=$sk->tocIndent($level-$prevlevel);
1420 $toclevel+=$level-$prevlevel;
1421
1422 }
1423 if(($nh||$st) && $level<$prevlevel) {
1424 $h[$level+1]=0; // reset when we step back a level
1425 $toc.=$sk->tocUnindent($prevlevel-$level);
1426 $toclevel-=$prevlevel-$level;
1427
1428 }
1429 $h[$level]++; // count number of headlines for each level
1430
1431 if($nh||$st) {
1432 for($i=1;$i<=$level;$i++) {
1433 if($h[$i]) {
1434 if($dot) {$numbering.=".";}
1435 $numbering.=$h[$i];
1436 $dot=1;
1437 }
1438 }
1439 }
1440
1441 // The canonized header is a version of the header text safe to use for links
1442
1443 $canonized_headline=preg_replace("/<.*?>/","",$headline); // strip out HTML
1444 $tocline=$canonized_headline;
1445 $canonized_headline=str_replace('"',"",$canonized_headline);
1446 $canonized_headline=str_replace(" ","_",trim($canonized_headline));
1447 $refer[$c]=$canonized_headline;
1448 $refers[$canonized_headline]++; // count how many in assoc. array so we can track dupes in anchors
1449 $refcount[$c]=$refers[$canonized_headline];
1450
1451 // Prepend the number to the heading text
1452
1453 if($nh||$st) {
1454 $tocline=$numbering ." ". $tocline;
1455
1456 // Don't number the heading if it is the only one (looks silly)
1457 if($nh && count($matches[3]) > 1) {
1458 $headline=$numbering . " " . $headline; // the two are different if the line contains a link
1459 }
1460 }
1461
1462 // Create the anchor for linking from the TOC to the section
1463
1464 $anchor=$canonized_headline;
1465 if($refcount[$c]>1) {$anchor.="_".$refcount[$c];}
1466 if($st) {
1467 $toc.=$sk->tocLine($anchor,$tocline,$toclevel);
1468 }
1469 if($es && !isset($wpPreview)) {
1470 $head[$c].=$sk->editSectionLink($c+1);
1471 }
1472
1473 // Put it all together
1474
1475 $head[$c].="<h".$level.$matches[2][$c]
1476 ."<a name=\"".$anchor."\">"
1477 .$headline
1478 ."</a>"
1479 ."</h".$level.">";
1480
1481 // Add the edit section link
1482
1483 if($esr && !isset($wpPreview)) {
1484 $head[$c]=$sk->editSectionScript($c+1,$head[$c]);
1485 }
1486
1487 $numbering="";
1488 $c++;
1489 $dot=0;
1490 }
1491
1492 if($st) {
1493 $toclines=$c;
1494 $toc.=$sk->tocUnindent($toclevel);
1495 $toc=$sk->tocTable($toc);
1496 }
1497
1498 // split up and insert constructed headlines
1499
1500 $blocks=preg_split("/<H[1-6].*?>.*?<\/H[1-6]>/i",$text);
1501 $i=0;
1502
1503 foreach($blocks as $block) {
1504 if(($es) && !isset($wpPreview) && $c>0 && $i==0) {
1505 # This is the [edit] link that appears for the top block of text when
1506 # section editing is enabled
1507 $full.=$sk->editSectionLink(0);
1508 }
1509 $full.=$block;
1510 if($st && $toclines>3 && !$i) {
1511 # Let's add a top anchor just in case we want to link to the top of the page
1512 $full="<a name=\"top\"></a>".$full.$toc;
1513 }
1514
1515 $full.=$head[$i];
1516 $i++;
1517 }
1518
1519 return $full;
1520 }
1521
1522 /* private */ function magicISBN( $text )
1523 {
1524 global $wgLang;
1525
1526 $a = split( "ISBN ", " $text" );
1527 if ( count ( $a ) < 2 ) return $text;
1528 $text = substr( array_shift( $a ), 1);
1529 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1530
1531 foreach ( $a as $x ) {
1532 $isbn = $blank = "" ;
1533 while ( " " == $x{0} ) {
1534 $blank .= " ";
1535 $x = substr( $x, 1 );
1536 }
1537 while ( strstr( $valid, $x{0} ) != false ) {
1538 $isbn .= $x{0};
1539 $x = substr( $x, 1 );
1540 }
1541 $num = str_replace( "-", "", $isbn );
1542 $num = str_replace( " ", "", $num );
1543
1544 if ( "" == $num ) {
1545 $text .= "ISBN $blank$x";
1546 } else {
1547 $text .= "<a href=\"" . wfLocalUrlE( $wgLang->specialPage(
1548 "Booksources"), "isbn={$num}" ) . "\" class=\"internal\">ISBN $isbn</a>";
1549 $text .= $x;
1550 }
1551 }
1552 return $text;
1553 }
1554
1555 /* private */ function magicRFC( $text )
1556 {
1557 return $text;
1558 }
1559
1560 /* private */ function headElement()
1561 {
1562 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
1563
1564 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
1565
1566 if ( "" == $this->mHTMLtitle ) {
1567 $this->mHTMLtitle = $this->mPagetitle;
1568 }
1569 $rtl = $wgLang->isRTL() ? " dir='RTL'" : "";
1570 $ret .= "<html lang=\"$wgLanguageCode\"$rtl><head><title>{$this->mHTMLtitle}</title>\n";
1571 array_push( $this->mMetatags, array( "http:Content-type", "text/html; charset={$wgOutputEncoding}" ) );
1572 foreach ( $this->mMetatags as $tag ) {
1573 if ( 0 == strcasecmp( "http:", substr( $tag[0], 0, 5 ) ) ) {
1574 $a = "http-equiv";
1575 $tag[0] = substr( $tag[0], 5 );
1576 } else {
1577 $a = "name";
1578 }
1579 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
1580 }
1581 $p = $this->mRobotpolicy;
1582 if ( "" == $p ) { $p = "index,follow"; }
1583 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
1584
1585 if ( count( $this->mKeywords ) > 0 ) {
1586 $ret .= "<meta name=\"keywords\" content=\"" .
1587 implode( ",", $this->mKeywords ) . "\">\n";
1588 }
1589 foreach ( $this->mLinktags as $tag ) {
1590 $ret .= "<link ";
1591 if ( "" != $tag[0] ) { $ret .= "rel=\"{$tag[0]}\" "; }
1592 if ( "" != $tag[1] ) { $ret .= "rev=\"{$tag[1]}\" "; }
1593 $ret .= "href=\"{$tag[2]}\">\n";
1594 }
1595 $sk = $wgUser->getSkin();
1596 $ret .= $sk->getHeadScripts();
1597 $ret .= $sk->getUserStyles();
1598
1599 $ret .= "</head>\n";
1600 return $ret;
1601 }
1602 }
1603
1604 # Regex callbacks, used in OutputPage::replaceVariables
1605
1606 # Just get rid of the dangerous stuff
1607 # Necessary because replaceVariables is called after removeHTMLtags,
1608 # and message text can come from any user
1609 function wfReplaceMsgVar( $matches ) {
1610 global $wgOut;
1611 $text = $wgOut->removeHTMLtags( wfMsg( $matches[1] ) );
1612 return $text;
1613 }
1614
1615 # Effective <nowiki></nowiki>
1616 # Not real <nowiki> because this is called after nowiki sections are processed
1617 function wfReplaceMsgnwVar( $matches ) {
1618 $text = wfEscapeWikiText( wfMsg( $matches[1] ) );
1619 return $text;
1620 }
1621
1622 ?>