Fix sort for proper index
[mediawiki.git] / includes / OutputPage.php
blob37379c83a837da8db2299d553d4a9c0dc2cafc56
1 <?
2 # See design.doc
4 if($wgUseTeX) include_once( "Math.php" );
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;
13 var $mDTopen, $mLastSection; # Used for processing DL, PRE
14 var $mLanguageLinks, $mSupressQuickbar;
16 function OutputPage()
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;
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; }
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 ) ); }
39 function checkLastModified ( $timestamp )
41 global $wgLang, $wgCachePages, $wgUser;
42 if( !$wgCachePages ) {
43 wfDebug( "CACHE DISABLED\n", false );
44 return;
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;
51 if( $wgUser->getOption( "nocache" ) ) {
52 wfDebug( "USER DISABLED CACHE\n", false );
53 return;
56 $lastmod = gmdate( "D, j M Y H:i:s", wfTimestamp2Unix(
57 max( $timestamp, $wgUser->mTouched ) ) ) . " GMT";
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 );
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;
81 } else {
82 wfDebug( "We're confused.\n", false );
83 $this->mLastModified = $lastmod;
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; }
98 function getLanguageLinks() {
99 global $wgTitle, $wgLanguageCode;
100 global $wgDBconnection, $wgDBname;
101 return $this->mLanguageLinks;
103 function supressQuickbar() { $this->mSupressQuickbar = true; }
104 function isQuickbarSupressed() { return $this->mSupressQuickbar; }
106 function addHTML( $text ) { $this->mBodytext .= $text; }
107 function addHeadtext( $text ) { $this->mHeadtext .= $text; }
108 function debug( $text ) { $this->mDebugtext .= $text; }
110 # First pass--just handle <nowiki> sections, pass the rest off
111 # to doWikiPass2() which does all the real work.
114 function addWikiText( $text, $linestart = true )
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 = "";
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];
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];
158 } else {
159 $stripped2 = $stripped;
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];
175 $text = $this->doWikiPass2( $stripped3, $linestart );
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 );
184 for ( $i = 1; $i <= $mathsecs; ++$i ) {
185 $text = preg_replace( "/{$unique2}/", str_replace( $specialChars,
186 $escapedChars, $mathlist[$i] ), $text, 1 );
189 for ( $i = 1; $i <= $nwsecs; ++$i ) {
190 $text = preg_replace( "/{$unique}/", str_replace( $specialChars,
191 $escapedChars, $nwlist[$i] ), $text, 1 );
193 $this->addHTML( $text );
194 wfProfileOut( $fname );
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" );
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" );
214 header( "Expires: Mon, 15 Jan 2001 00:00:00 GMT" ); # Cachers always validate the page!
217 # Finally, all the text has been munged and accumulated into
218 # the object, let's actually output it:
220 function output()
222 global $wgUser, $wgLang, $wgDebugComments, $wgCookieExpiration;
223 global $wgInputEncoding, $wgOutputEncoding, $wgLanguageCode;
225 $fname = "OutputPage::output";
226 wfProfileIn( $fname );
228 $sk = $wgUser->getSkin();
230 $this->sendCacheControl();
232 header( "Content-type: text/html; charset={$wgOutputEncoding}" );
233 header( "Content-language: {$wgLanguageCode}" );
235 if ( "" != $this->mRedirect ) {
236 header( "Location: {$this->mRedirect}" );
237 return;
240 $exp = time() + $wgCookieExpiration;
241 foreach( $this->mCookies as $name => $val ) {
242 setcookie( $name, $val, $exp, "/" );
245 $sk->outputPage( $this );
246 flush();
249 function out( $ins )
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; }
258 print $outs;
261 function setEncodings()
263 global $wgInputEncoding, $wgOutputEncoding;
264 global $wgUser, $wgLang;
266 $wgInputEncoding = strtolower( $wgInputEncoding );
268 if( $wgUser->getOption( 'altencoding' ) ) {
269 $wgLang->setAltEncoding();
270 return;
273 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
274 $wgOutputEncoding = strtolower( $wgOutputEncoding );
275 return;
279 # This code is unused anyway!
280 # Commenting out. --bv 2003-11-15
282 $a = explode( ",", $_SERVER['HTTP_ACCEPT_CHARSET'] );
283 $best = 0.0;
284 $bestset = "*";
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;
294 if ( $q > $best ) {
295 $bestset = $set;
296 $best = $q;
299 #if ( "*" == $bestset ) { $bestset = "iso-8859-1"; }
300 if ( "*" == $bestset ) { $bestset = $wgOutputEncoding; }
301 $wgOutputEncoding = strtolower( $bestset );
303 # Disable for now
306 $wgOutputEncoding = $wgInputEncoding;
309 function reportTime()
311 global $wgRequestTime, $wgDebugLogFile;
312 global $wgProfiling, $wgProfileStack, $wgProfileLimit, $wgUser;
314 list( $usec, $sec ) = explode( " ", microtime() );
315 $now = (float)$sec + (float)$usec;
317 list( $usec, $sec ) = explode( " ", $wgRequestTime );
318 $start = (float)$sec + (float)$usec;
319 $elapsed = $now - $start;
321 if ( "" != $wgDebugLogFile ) {
322 $prof = wfGetProfilingOutput( $start, $elapsed );
323 if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
324 $forward = " forwarded for " . $_SERVER['HTTP_X_FORWARDED_FOR'];
325 if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
326 $forward .= " client IP " . $_SERVER['HTTP_CLIENT_IP'];
327 if( !empty( $_SERVER['HTTP_FROM'] ) )
328 $forward .= " from " . $_SERVER['HTTP_FROM'];
329 if( $forward )
330 $forward = "\t(proxied via {$_SERVER['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( $_SERVER['REQUEST_URI'] . $forward ) );
336 error_log( $log . $prof, 3, $wgDebugLogFile );
338 $com = sprintf( "<!-- Time since request: %01.2f secs. -->",
339 $elapsed );
340 return $com;
343 # Note: these arguments are keys into wfMsg(), not text!
345 function errorpage( $title, $msg )
347 global $wgTitle;
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 );
356 $this->mBodytext = "";
357 $this->addHTML( "<p>" . wfMsg( $msg ) . "\n" );
358 $this->returnToMain( false );
360 $this->output();
361 exit;
364 function sysopRequired()
366 global $wgUser;
368 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
369 $this->setPageTitle( wfMsg( "sysoptitle" ) );
370 $this->setRobotpolicy( "noindex,nofollow" );
371 $this->setArticleFlag( false );
372 $this->mBodytext = "";
374 $sk = $wgUser->getSkin();
375 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
376 $this->addHTML( wfMsg( "sysoptext", $ap ) );
377 $this->returnToMain();
380 function developerRequired()
382 global $wgUser;
384 $this->setHTMLTitle( wfMsg( "errorpagetitle" ) );
385 $this->setPageTitle( wfMsg( "developertitle" ) );
386 $this->setRobotpolicy( "noindex,nofollow" );
387 $this->setArticleFlag( false );
388 $this->mBodytext = "";
390 $sk = $wgUser->getSkin();
391 $ap = $sk->makeKnownLink( wfMsg( "administrators" ), "" );
392 $this->addHTML( wfMsg( "developertext", $ap ) );
393 $this->returnToMain();
396 function databaseError( $fname )
398 global $wgUser, $wgCommandLineMode;
400 $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
401 $this->setRobotpolicy( "noindex,nofollow" );
402 $this->setArticleFlag( false );
404 if ( $wgCommandLineMode ) {
405 $msg = wfMsgNoDB( "dberrortextcl" );
406 } else {
407 $msg = wfMsgNoDB( "dberrortext" );
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 );
415 if ( $wgCommandLineMode ) {
416 print "$msg\n";
417 exit();
419 $sk = $wgUser->getSkin();
420 $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
421 wfMsgNoDB( "searchingwikipedia" ) );
422 $msg = str_replace( "$5", $shlink, $msg );
424 $this->mBodytext = $msg;
425 $this->output();
426 exit();
429 function readOnlyPage( $source = "", $protected = false )
431 global $wgUser, $wgReadOnlyFile;
433 $this->setRobotpolicy( "noindex,nofollow" );
434 $this->setArticleFlag( false );
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 ) );
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 );
453 $this->returnToMain( false );
456 function fatalError( $message )
458 $this->setPageTitle( wfMsg( "internalerror" ) );
459 $this->setRobotpolicy( "noindex,nofollow" );
460 $this->setArticleFlag( false );
462 $this->mBodytext = $message;
463 $this->output();
464 exit;
467 function unexpectedValueError( $name, $val )
469 $this->fatalError( wfMsg( "unexpected", $name, $val ) );
472 function fileCopyError( $old, $new )
474 $this->fatalError( wfMsg( "filecopyerror", $old, $new ) );
477 function fileRenameError( $old, $new )
479 $this->fatalError( wfMsg( "filerenameerror", $old, $new ) );
482 function fileDeleteError( $name )
484 $this->fatalError( wfMsg( "filedeleteerror", $name ) );
487 function fileNotFoundError( $name )
489 $this->fatalError( wfMsg( "filenotfound", $name ) );
492 function returnToMain( $auto = true )
494 global $wgUser, $wgOut, $returnto;
496 $sk = $wgUser->getSkin();
497 if ( "" == $returnto ) {
498 $returnto = wfMsg( "mainpage" );
500 $link = $sk->makeKnownLink( $returnto, "" );
502 $r = wfMsg( "returnto", $link );
503 if ( $auto ) {
504 $wgOut->addMeta( "http:Refresh", "10;url=" .
505 wfLocalUrlE( wfUrlencode( $returnto ) ) );
507 $wgOut->addHTML( "\n<p>$r\n" );
511 function categoryMagic ()
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" ;
522 $articles = array() ;
523 $parents = array () ;
524 $children = array() ;
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 ) )
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 ) ) ;
544 wfFreeResult ( $res ) ;
546 # Children
547 if ( count ( $children ) > 0 )
549 asort ( $children ) ;
550 $r .= "<h2>".wfMsg("subcategories")."</h2>\n" ;
551 $r .= implode ( ", " , $children ) ;
554 # Articles
555 if ( count ( $articles ) > 0 )
557 asort ( $articles ) ;
558 $h = wfMsg( "category_header", $ti[1] );
559 $r .= "<h2>{$h}</h2>\n" ;
560 $r .= implode ( ", " , $articles ) ;
564 return $r ;
567 function getHTMLattrs ()
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 */
581 return $htmlattrs ;
584 function fixTableTags ( $t )
586 if ( trim ( $t ) == "" ) return "" ; # Saves runtime ;-)
587 $htmlattrs = $this->getHTMLattrs() ;
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);
595 return trim ( $t ) ;
598 function doTableStuff ( $t )
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 )
607 $x = rtrim ( $x ) ;
608 $fc = substr ( $x , 0 , 1 ) ;
609 if ( "{|" == substr ( $x , 0 , 2 ) )
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 , "" ) ;
617 else if ( count ( $td ) == 0 ) { } # Don't do any of the following
618 else if ( "|}" == substr ( $x , 0 , 2 ) )
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 ;
627 /* else if ( "|_" == substr ( $x , 0 , 2 ) ) # Caption
629 $z = trim ( substr ( $x , 2 ) ) ;
630 $t[$k] = "<caption>{$z}</caption>\n" ;
632 else if ( "|-" == substr ( $x , 0 , 2 ) ) # Allows for |---------------
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 ) ) ;
647 else if ( "|" == $fc || "!" == $fc || "|+" == substr ( $x , 0 , 2 ) ) # Caption
649 if ( "|+" == substr ( $x , 0 , 2 ) )
651 $fc = "+" ;
652 $x = substr ( $x , 1 ) ;
654 $after = substr ( $x , 1 ) ;
655 if ( $fc == "!" ) $after = str_replace ( "!!" , "||" , $after ) ;
656 $after = explode ( "||" , $after ) ;
657 $t[$k] = "" ;
658 foreach ( $after AS $theline )
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 , "" ) ;
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 ) ;
682 # Closing open td, tr && table
683 while ( count ( $td ) > 0 )
685 if ( array_pop ( $td ) ) $t[] = "</td>" ;
686 if ( array_pop ( $tr ) ) $t[] = "</tr>" ;
687 $t[] = "</table>" ;
690 $t = implode ( "\n" , $t ) ;
691 # $t = $this->removeHTMLtags( $t );
692 return $t ;
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.
699 function doWikiPass2( $text, $linestart )
701 global $wgUser, $wgLang, $wgUseDynamicDates;
702 $fname = "OutputPage::doWikiPass2";
703 wfProfileIn( $fname );
705 $text = $this->removeHTMLtags( $text );
706 $text = $this->replaceVariables( $text );
708 $text = preg_replace( "/(^|\n)-----*/", "\\1<hr>", $text );
709 $text = str_replace ( "<HR>", "<hr>", $text );
711 $text = $this->doAllQuotes( $text );
712 $text = $this->doHeadings( $text );
713 $text = $this->doBlockLevels( $text, $linestart );
715 if($wgUseDynamicDates) {
716 $text = $wgLang->replaceDates( $text );
719 $text = $this->replaceExternalLinks( $text );
720 $text = $this->replaceInternalLinks ( $text );
721 $text = $this->doTableStuff ( $text ) ;
723 $text = $this->magicISBN( $text );
724 $text = $this->magicRFC( $text );
725 $text = $this->formatHeadings( $text );
727 $sk = $wgUser->getSkin();
728 $text = $sk->transformContent( $text );
729 $text .= $this->categoryMagic () ;
731 wfProfileOut( $fname );
732 return $text;
735 /* private */ function doAllQuotes( $text )
737 $outtext = "";
738 $lines = explode( "\r\n", $text );
739 foreach ( $lines as $line ) {
740 $outtext .= $this->doQuotes ( "", $line, "" ) . "\r\n";
742 return $outtext;
745 /* private */ function doQuotes( $pre, $text, $mode )
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" );
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" );
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>";
793 /* private */ function doHeadings( $text )
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 );
800 return $text;
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.
807 /* private */ function replaceExternalLinks( $text )
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;
821 /* private */ function subReplaceExternalLinks( $s, $protocol, $autonumber )
823 global $wgUser, $printable;
824 global $wgAllowExternalImages;
827 $unique = "4jzAfzB8hNvf4sqyO9Edd8pSmk9rE2in0Tgw3";
828 $uc = "A-Za-z0-9_\\/~%\\-+&*#?!=()@\\x80-\\xFF";
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";
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}]|$)/";
845 $e2 = "/(^|[^\\[])({$protocol}:)(([".$uc."]|[".$sep."][".$uc."])+)([^". $uc . $sep. "]|[".$sep."]|$)/";
846 $sk = $wgUser->getSkin();
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 );
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 );
858 $a = explode( "[{$protocol}:", " " . $s );
859 $s = array_shift( $a );
860 $s = substr( $s, 1 );
862 $e1 = "/^([{$uc}"."{$sep}]+)](.*)\$/sD";
863 $e2 = "/^([{$uc}"."{$sep}]+)\\s+([^\\]]+)](.*)\$/sD";
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;
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}";
885 return $s;
888 /* private */ function replaceInternalLinks( $s )
890 global $wgTitle, $wgUser, $wgLang;
891 global $wgLinkCache, $wgInterwikiMagic, $wgUseCategoryMagic;
892 global $wgNamespacesWithSubpages, $wgLanguageCode;
893 wfProfileIn( $fname = "OutputPage::replaceInternalLinks" );
895 wfProfileIn( "$fname-setup" );
896 $tc = Title::legalChars() . "#";
897 $sk = $wgUser->getSkin();
899 $a = explode( "[[", " " . $s );
900 $s = array_shift( $a );
901 $s = substr( $s, 1 );
903 $e1 = "/^([{$tc}]+)(?:\\|([^]]+))?]](.*)\$/sD";
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" );
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;
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
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);
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
944 } elseif( $noforce ) { # no subpage
945 $link = $m[1];
946 } else {
947 $link = substr( $m[1], 1 );
949 if( empty( $text ) )
950 $text = $link;
952 $nt = Title::newFromText( $link );
953 if( !$nt ) {
954 $s .= "[[" . $line;
955 continue;
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(); }
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;
983 $s .= $sk->makeLink( $link, $text, "", $trail );
984 } else if ( $pre != $wgLanguageCode ) {
985 array_push( $this->mLanguageLinks, "$pre:$suf" );
986 $s .= $trail;
989 continue;
991 if( $ns == $image ) {
992 $s .= $sk->makeImageLinkObj( $nt, $text ) . $trail;
993 $wgLinkCache->addImageLinkObj( $nt );
994 continue;
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 );
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;
1014 $s .= $sk->makeLinkObj( $nt, $text, "", $trail );
1016 wfProfileOut( $fname );
1017 return $s;
1020 # Some functions here used by doBlockLevels()
1022 /* private */ function closeParagraph()
1024 $result = "";
1025 if ( 0 != strcmp( "p", $this->mLastSection ) &&
1026 0 != strcmp( "", $this->mLastSection ) ) {
1027 $result = "</" . $this->mLastSection . ">";
1029 $this->mLastSection = "";
1030 return $result;
1032 # getCommon() returns the length of the longest common substring
1033 # of both arguments, starting at the beginning of both.
1035 /* private */ function getCommon( $st1, $st2 )
1037 $fl = strlen( $st1 );
1038 $shorter = strlen( $st2 );
1039 if ( $fl < $shorter ) { $shorter = $fl; }
1041 for ( $i = 0; $i < $shorter; ++$i ) {
1042 if ( $st1{$i} != $st2{$i} ) { break; }
1044 return $i;
1046 # These next three functions open, continue, and close the list
1047 # element appropriate to the prefix character passed into them.
1049 /* private */ function openList( $char )
1051 $result = $this->closeParagraph();
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;
1060 else { $result = "<!-- ERR 1 -->"; }
1062 return $result;
1065 /* private */ function nextItem( $char )
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>";
1079 return "<!-- ERR 2 -->";
1082 /* private */function closeList( $char )
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>";
1094 return "<!-- ERR 3 -->";
1097 /* private */ function doBlockLevels( $text, $linestart )
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.
1105 $a = explode( "\n", $text );
1106 $text = $lastPref = "";
1107 $this->mDTopen = $inBlockElem = false;
1109 if ( ! $linestart ) { $text .= array_shift( $a ); }
1110 foreach ( $a as $t ) {
1111 if ( "" != $text ) { $text .= "\n"; }
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 );
1120 if ( 0 != $npl && 0 == strcmp( $lastPref, $pref2 ) ) {
1121 $text .= $this->nextItem( substr( $pref, -1 ) );
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 );
1131 } else if (0 != $npl || 0 != $opl) {
1132 $cpl = $this->getCommon( $pref, $lastPref );
1134 while ( $cpl < $opl ) {
1135 $text .= $this->closeList( $lastPref{$opl-1} );
1136 --$opl;
1138 if ( $npl <= $cpl && $cpl > 0 ) {
1139 $text .= $this->nextItem( $pref{$cpl-1} );
1141 while ( $npl > $cpl ) {
1142 $char = substr( $pref, $cpl, 1 );
1143 $text .= $this->openList( $char );
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 );
1153 ++$cpl;
1155 $lastPref = $pref2;
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;
1163 if ( ! $inBlockElem ) {
1164 if ( " " == $t{0} ) {
1165 $newSection = "pre";
1166 # $t = wfEscapeHTML( $t );
1168 else { $newSection = "p"; }
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 . ">";
1180 $this->mLastSection = $newSection;
1182 if ( $inBlockElem &&
1183 preg_match( "/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6)/i", $t ) ) {
1184 $inBlockElem = false;
1187 $text .= $t;
1189 while ( $npl ) {
1190 $text .= $this->closeList( $pref2{$npl-1} );
1191 --$npl;
1193 if ( "" != $this->mLastSection ) {
1194 if ( "p" != $this->mLastSection ) {
1195 $text .= "</" . $this->mLastSection . ">";
1197 $this->mLastSection = "";
1199 wfProfileOut( $fname );
1200 return $text;
1203 /* private */ function replaceVariables( $text )
1205 global $wgLang;
1206 $fname = "OutputPage::replaceVariables";
1207 wfProfileIn( $fname );
1210 # Basic variables
1211 # See Language.php for the definition of each magic word
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 );
1219 $v = $wgLang->getMonthName( date( "n" ) );
1220 $mw =& MagicWord::get( MAG_CURRENTMONTHNAME );
1221 $text = $mw->replace( $v, $text );
1223 $v = $wgLang->getMonthNameGen( date( "n" ) );
1224 $mw =& MagicWord::get( MAG_CURRENTMONTHNAMEGEN );
1225 $text = $mw->replace( $v, $text );
1227 $v = date( "j" );
1228 $mw = MagicWord::get( MAG_CURRENTDAY );
1229 $text = $mw->replace( $v, $text );
1231 $v = $wgLang->getWeekdayName( date( "w" )+1 );
1232 $mw =& MagicWord::get( MAG_CURRENTDAYNAME );
1233 $text = $mw->replace( $v, $text );
1235 $v = date( "Y" );
1236 $mw =& MagicWord::get( MAG_CURRENTYEAR );
1237 $text = $mw->replace( $v, $text );
1239 $v = $wgLang->time( wfTimestampNow(), false );
1240 $mw =& MagicWord::get( MAG_CURRENTTIME );
1241 $text = $mw->replace( $v, $text );
1243 $mw =& MagicWord::get( MAG_NUMBEROFARTICLES );
1244 if ( $mw->match( $text ) ) {
1245 $v = wfNumberOfArticles();
1246 $text = $mw->replace( $v, $text );
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" );
1254 $mw =& MagicWord::get( MAG_MSGNW );
1255 $text = $mw->substituteCallback( $text, "wfReplaceMsgnwVar" );
1257 wfProfileOut( $fname );
1258 return $text;
1261 # Cleans up HTML, removes dangerous tags and attributes
1262 /* private */ function removeHTMLtags( $text )
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"
1273 $htmlsingle = array(
1274 "br", "p", "hr", "li", "dt", "dd"
1276 $htmlnest = array( # Tags that can be nested--??
1277 "table", "tr", "td", "th", "div", "blockquote", "ol", "ul",
1278 "dl", "font", "big", "small", "sub", "sup"
1280 $tabletags = array( # Can only appear inside table
1281 "td", "th", "tr"
1284 $htmlsingle = array_merge( $tabletags, $htmlsingle );
1285 $htmlelements = array_merge( $htmlsingle, $htmlpairs );
1287 $htmlattrs = $this->getHTMLattrs () ;
1289 # Remove HTML comments
1290 $text = preg_replace( "/<!--.*-->/sU", "", $text );
1292 $bits = explode( "<", $text );
1293 $text = array_shift( $bits );
1294 $tagstack = array(); $tablestack = array();
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 );
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 );
1316 $newparams = "";
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();
1331 array_push( $tagstack, $t );
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);
1339 if ( ! $badtag ) {
1340 $rest = str_replace( ">", "&gt;", $rest );
1341 $text .= "<$slash$t$newparams$brace$rest";
1342 continue;
1345 $text .= "&lt;" . str_replace( ">", "&gt;", $x);
1347 # Close off any remaining tags
1348 while ( $t = array_pop( $tagstack ) ) {
1349 $text .= "</$t>\n";
1350 if ( $t == "table" ) { $tagstack = array_pop( $tablestack ); }
1352 wfProfileOut( $fname );
1353 return $text;
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
1365 * It loops through all headlines, collects the necessary data, then splits up the
1366 * string and re-inserts the newly formatted headlines.
1368 * */
1369 /* private */ function formatHeadings( $text )
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" );
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;
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 ))
1394 $st = 0;
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;}
1401 # We need this to perform operations on the HTML
1402 $sk=$wgUser->getSkin();
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);
1408 # headline counter
1409 $c=0;
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) {
1418 $h[$level]=0; // reset when we enter a new level
1419 $toc.=$sk->tocIndent($level-$prevlevel);
1420 $toclevel+=$level-$prevlevel;
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;
1429 $h[$level]++; // count number of headlines for each level
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;
1441 // The canonized header is a version of the header text safe to use for links
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];
1451 // Prepend the number to the heading text
1453 if($nh||$st) {
1454 $tocline=$numbering ." ". $tocline;
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
1462 // Create the anchor for linking from the TOC to the section
1464 $anchor=$canonized_headline;
1465 if($refcount[$c]>1) {$anchor.="_".$refcount[$c];}
1466 if($st) {
1467 $toc.=$sk->tocLine($anchor,$tocline,$toclevel);
1469 if($es && !isset($wpPreview)) {
1470 $head[$c].=$sk->editSectionLink($c+1);
1473 // Put it all together
1475 $head[$c].="<h".$level.$matches[2][$c]
1476 ."<a name=\"".$anchor."\">"
1477 .$headline
1478 ."</a>"
1479 ."</h".$level.">";
1481 // Add the edit section link
1483 if($esr && !isset($wpPreview)) {
1484 $head[$c]=$sk->editSectionScript($c+1,$head[$c]);
1487 $numbering="";
1488 $c++;
1489 $dot=0;
1492 if($st) {
1493 $toclines=$c;
1494 $toc.=$sk->tocUnindent($toclevel);
1495 $toc=$sk->tocTable($toc);
1498 // split up and insert constructed headlines
1500 $blocks=preg_split("/<H[1-6].*?>.*?<\/H[1-6]>/i",$text);
1501 $i=0;
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);
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;
1515 $full.=$head[$i];
1516 $i++;
1519 return $full;
1522 /* private */ function magicISBN( $text )
1524 global $wgLang;
1526 $a = split( "ISBN ", " $text" );
1527 if ( count ( $a ) < 2 ) return $text;
1528 $text = substr( array_shift( $a ), 1);
1529 $valid = "0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1531 foreach ( $a as $x ) {
1532 $isbn = $blank = "" ;
1533 while ( " " == $x{0} ) {
1534 $blank .= " ";
1535 $x = substr( $x, 1 );
1537 while ( strstr( $valid, $x{0} ) != false ) {
1538 $isbn .= $x{0};
1539 $x = substr( $x, 1 );
1541 $num = str_replace( "-", "", $isbn );
1542 $num = str_replace( " ", "", $num );
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;
1552 return $text;
1555 /* private */ function magicRFC( $text )
1557 return $text;
1560 /* private */ function headElement()
1562 global $wgDocType, $wgDTD, $wgUser, $wgLanguageCode, $wgOutputEncoding, $wgLang;
1564 $ret = "<!DOCTYPE HTML PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
1566 if ( "" == $this->mHTMLtitle ) {
1567 $this->mHTMLtitle = $this->mPagetitle;
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";
1579 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\">\n";
1581 $p = $this->mRobotpolicy;
1582 if ( "" == $p ) { $p = "index,follow"; }
1583 $ret .= "<meta name=\"robots\" content=\"$p\">\n";
1585 if ( count( $this->mKeywords ) > 0 ) {
1586 $ret .= "<meta name=\"keywords\" content=\"" .
1587 implode( ",", $this->mKeywords ) . "\">\n";
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";
1595 $sk = $wgUser->getSkin();
1596 $ret .= $sk->getHeadScripts();
1597 $ret .= $sk->getUserStyles();
1599 $ret .= "</head>\n";
1600 return $ret;
1604 # Regex callbacks, used in OutputPage::replaceVariables
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;
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;