Fix for rollback
[lhc/web/wiklou.git] / includes / Article.php
1 <?
2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
4
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
7
8 include_once( "CacheManager.php" );
9
10 class Article {
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache;
16
17 function Article() { $this->clear(); }
18
19 /* private */ function clear()
20 {
21 $this->mContentLoaded = false;
22 $this->mUser = $this->mCounter = -1; # Not loaded
23 $this->mRedirectedFrom = $this->mUserText =
24 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
25 $this->mCountAdjustment = 0;
26 $this->mTouched = "19700101000000";
27 }
28
29 /* static */ function newFromID( $newid )
30 {
31 global $wgOut, $wgTitle, $wgArticle;
32 $a = new Article();
33 $n = Article::nameOf( $newid );
34
35 $wgTitle = Title::newFromDBkey( $n );
36 $wgTitle->resetArticleID( $newid );
37
38 return $a;
39 }
40
41 /* static */ function nameOf( $id )
42 {
43 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
44 "cur_id={$id}";
45 $res = wfQuery( $sql, "Article::nameOf" );
46 if ( 0 == wfNumRows( $res ) ) { return NULL; }
47
48 $s = wfFetchObject( $res );
49 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
50 return $n;
51 }
52
53 # Note that getContent/loadContent may follow redirects if
54 # not told otherwise, and so may cause a change to wgTitle.
55
56 function getContent( $noredir = false )
57 {
58 global $action,$section,$count,$wgTitle; # From query string
59 wfProfileIn( "Article::getContent" );
60
61 if ( 0 == $this->getID() ) {
62 if ( "edit" == $action ) {
63
64 global $wgTitle;
65 return ""; # was "newarticletext", now moved above the box)
66
67
68 }
69 wfProfileOut();
70 return wfMsg( "noarticletext" );
71 } else {
72 $this->loadContent( $noredir );
73 wfProfileOut();
74
75 if(
76 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
77 ( $wgTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
78 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$wgTitle->getText()) &&
79 $action=="view"
80 )
81 {
82 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
83 else {
84 if($action=="edit") {
85 if($section!="") {
86 if($section=="new") { return ""; }
87
88 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
89 $this->mContent, -1,
90 PREG_SPLIT_DELIM_CAPTURE);
91 if($section==0) {
92 return trim($secs[0]);
93 } else {
94 return trim($secs[$section*2-1] . $secs[$section*2]);
95 }
96 }
97 }
98 return $this->mContent;
99 }
100 }
101 }
102
103 function loadContent( $noredir = false )
104 {
105 global $wgOut, $wgTitle;
106 global $oldid, $redirect; # From query
107
108 if ( $this->mContentLoaded ) return;
109 $fname = "Article::loadContent";
110
111 # Pre-fill content with error message so that if something
112 # fails we'll have something telling us what we intended.
113
114 $t = $wgTitle->getPrefixedText();
115 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
116 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
117 $this->mContent = str_replace( "$1", $t, wfMsg( "missingarticle" ) );
118
119 if ( ! $oldid ) { # Retrieve current version
120 $id = $this->getID();
121 if ( 0 == $id ) return;
122
123 $sql = "SELECT " .
124 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
125 "FROM cur WHERE cur_id={$id}";
126 $res = wfQuery( $sql, $fname );
127 if ( 0 == wfNumRows( $res ) ) { return; }
128
129 $s = wfFetchObject( $res );
130
131 # If we got a redirect, follow it (unless we've been told
132 # not to by either the function parameter or the query
133
134 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
135 ( preg_match( "/^#redirect/i", $s->cur_text ) ) ) {
136 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
137 $s->cur_text, $m ) ) {
138 $rt = Title::newFromText( $m[1] );
139
140 # Gotta hand redirects to special pages differently:
141 # Fill the HTTP response "Location" header and ignore
142 # the rest of the page we're on.
143
144 if ( $rt->getInterwiki() != "" ) {
145 $wgOut->redirect( $rt->getFullURL() ) ;
146 return;
147 }
148 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
149 $wgOut->redirect( wfLocalUrl(
150 $rt->getPrefixedURL() ) );
151 return;
152 }
153 $rid = $rt->getArticleID();
154 if ( 0 != $rid ) {
155 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
156 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
157 $res = wfQuery( $sql, $fname );
158
159 if ( 0 != wfNumRows( $res ) ) {
160 $this->mRedirectedFrom = $wgTitle->getPrefixedText();
161 $wgTitle = $rt;
162 $s = wfFetchObject( $res );
163 }
164 }
165 }
166 }
167 $this->mContent = $s->cur_text;
168 $this->mUser = $s->cur_user;
169 $this->mCounter = $s->cur_counter;
170 $this->mTimestamp = $s->cur_timestamp;
171 $this->mTouched = $s->cur_touched;
172 $wgTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
173 $wgTitle->mRestrictionsLoaded = true;
174 wfFreeResult( $res );
175 } else { # oldid set, retrieve historical version
176 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
177 "WHERE old_id={$oldid}";
178 $res = wfQuery( $sql, $fname );
179 if ( 0 == wfNumRows( $res ) ) { return; }
180
181 $s = wfFetchObject( $res );
182 $this->mContent = $s->old_text;
183 $this->mUser = $s->old_user;
184 $this->mCounter = 0;
185 $this->mTimestamp = $s->old_timestamp;
186 wfFreeResult( $res );
187 }
188 $this->mContentLoaded = true;
189 }
190
191 function getID() { global $wgTitle; return $wgTitle->getArticleID(); }
192
193 function getCount()
194 {
195 if ( -1 == $this->mCounter ) {
196 $id = $this->getID();
197 $this->mCounter = wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
198 }
199 return $this->mCounter;
200 }
201
202 # Would the given text make this article a "good" article (i.e.,
203 # suitable for including in the article count)?
204
205 function isCountable( $text )
206 {
207 global $wgTitle, $wgUseCommaCount;
208
209 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
210 if ( preg_match( "/^#redirect/i", $text ) ) { return 0; }
211 $token = ($wgUseCommaCount ? "," : "[[" );
212 if ( false === strstr( $text, $token ) ) { return 0; }
213 return 1;
214 }
215
216 # Load the field related to the last edit time of the article.
217 # This isn't necessary for all uses, so it's only done if needed.
218
219 /* private */ function loadLastEdit()
220 {
221 global $wgOut;
222 if ( -1 != $this->mUser ) return;
223
224 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
225 "cur_comment,cur_minor_edit FROM cur WHERE " .
226 "cur_id=" . $this->getID();
227 $res = wfQuery( $sql, "Article::loadLastEdit" );
228
229 if ( wfNumRows( $res ) > 0 ) {
230 $s = wfFetchObject( $res );
231 $this->mUser = $s->cur_user;
232 $this->mUserText = $s->cur_user_text;
233 $this->mTimestamp = $s->cur_timestamp;
234 $this->mComment = $s->cur_comment;
235 $this->mMinorEdit = $s->cur_minor_edit;
236 }
237 }
238
239 function getTimestamp()
240 {
241 $this->loadLastEdit();
242 return $this->mTimestamp;
243 }
244
245 function getUser()
246 {
247 $this->loadLastEdit();
248 return $this->mUser;
249 }
250
251 function getUserText()
252 {
253 $this->loadLastEdit();
254 return $this->mUserText;
255 }
256
257 function getComment()
258 {
259 $this->loadLastEdit();
260 return $this->mComment;
261 }
262
263 function getMinorEdit()
264 {
265 $this->loadLastEdit();
266 return $this->mMinorEdit;
267 }
268
269 # This is the default action of the script: just view the page of
270 # the given title.
271
272 function view()
273 {
274 global $wgUser, $wgOut, $wgTitle, $wgLang;
275 global $oldid, $diff; # From query
276 global $wgLinkCache;
277 wfProfileIn( "Article::view" );
278
279 $wgOut->setArticleFlag( true );
280 $wgOut->setRobotpolicy( "index,follow" );
281
282 # If we got diff and oldid in the query, we want to see a
283 # diff page instead of the article.
284
285 if ( isset( $diff ) ) {
286 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
287 $de = new DifferenceEngine( $oldid, $diff );
288 $de->showDiffPage();
289 wfProfileOut();
290 return;
291 }
292 $text = $this->getContent(); # May change wgTitle!
293 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
294 $wgOut->setHTMLTitle( $wgTitle->getPrefixedText() .
295 " - " . wfMsg( "wikititlesuffix" ) );
296
297 # We're looking at an old revision
298
299 if ( $oldid ) {
300 $this->setOldSubtitle();
301 $wgOut->setRobotpolicy( "noindex,follow" );
302 }
303 if ( "" != $this->mRedirectedFrom ) {
304 $sk = $wgUser->getSkin();
305 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
306 "redirect=no" );
307 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
308 $wgOut->setSubtitle( $s );
309 }
310 $wgOut->checkLastModified( $this->mTouched );
311 $this->tryFileCache();
312 $wgLinkCache->preFill( $wgTitle );
313 $wgOut->addWikiText( $text );
314
315 # If the article we've just shown is in the "Image" namespace,
316 # follow it with the history list and link list for the image
317 # it describes.
318
319 if ( Namespace::getImage() == $wgTitle->getNamespace() ) {
320 $this->imageHistory();
321 $this->imageLinks();
322 }
323 $this->viewUpdates();
324 wfProfileOut();
325 }
326
327 # Theoretically we could defer these whole insert and update
328 # functions for after display, but that's taking a big leap
329 # of faith, and we want to be able to report database
330 # errors at some point.
331
332 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
333 {
334 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
335 $fname = "Article::insertNewArticle";
336
337 $ns = $wgTitle->getNamespace();
338 $ttl = $wgTitle->getDBkey();
339 $text = $this->preSaveTransform( $text );
340 if ( preg_match( "/^#redirect/i", $text ) ) { $redir = 1; }
341 else { $redir = 0; }
342
343 $now = wfTimestampNow();
344 $won = wfInvertTimestamp( $now );
345 wfSeedRandom();
346 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
347 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
348 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
349 "cur_restrictions,cur_user_text,cur_is_redirect," .
350 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
351 wfStrencode( $text ) . "', '" .
352 wfStrencode( $summary ) . "', '" .
353 $wgUser->getID() . "', '{$now}', " .
354 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
355 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
356 $res = wfQuery( $sql, $fname );
357
358 $newid = wfInsertId();
359 $wgTitle->resetArticleID( $newid );
360
361 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
362 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
363 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
364 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
365 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
366 wfStrencode( $wgUser->getName() ) . "','" .
367 wfStrencode( $summary ) . "',0,0," .
368 ( $wgUser->isBot() ? 1 : 0 ) . ")";
369 wfQuery( $sql, $fname );
370 if ($watchthis) {
371 if(!$wgTitle->userIsWatching()) $this->watch();
372 } else {
373 if ( $wgTitle->userIsWatching() ) {
374 $this->unwatch();
375 }
376 }
377
378 $this->showArticle( $text, wfMsg( "newarticle" ) );
379 }
380
381 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
382 {
383 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
384 global $wgDBtransactions;
385 $fname = "Article::updateArticle";
386 // insert updated section into old text if we have only edited part
387 // of the article
388 if ($section != "") {
389 $oldtext=$this->getContent();
390 if($section=="new") {
391 if($summary) $subject="== {$summary} ==\n\n";
392 $text=$oldtext."\n\n".$subject.$text;
393 } else {
394 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
395 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
396 $secs[$section*2]=$text."\n\n"; // replace with edited
397 if($section) { $secs[$section*2-1]=""; } // erase old headline
398 $text=join("",$secs);
399 }
400 }
401 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
402 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
403 if ( preg_match( "/^(#redirect[^\\n]+)/i", $text, $m ) ) {
404 $redir = 1;
405 $text = $m[1] . "\n"; # Remove all content but redirect
406 }
407 else { $redir = 0; }
408 $this->loadLastEdit();
409
410 $text = $this->preSaveTransform( $text );
411
412 # Update article, but only if changed.
413
414 if( $wgDBtransactions ) {
415 $sql = "BEGIN";
416 wfQuery( $sql );
417 }
418 $oldtext = $this->getContent( true );
419
420 if ( 0 != strcmp( $text, $oldtext ) ) {
421 $this->mCountAdjustment = $this->isCountable( $text )
422 - $this->isCountable( $oldtext );
423
424 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
425 "old_comment,old_user,old_user_text,old_timestamp," .
426 "old_minor_edit,inverse_timestamp) VALUES (" .
427 $wgTitle->getNamespace() . ", '" .
428 wfStrencode( $wgTitle->getDBkey() ) . "', '" .
429 wfStrencode( $oldtext ) . "', '" .
430 wfStrencode( $this->getComment() ) . "', " .
431 $this->getUser() . ", '" .
432 wfStrencode( $this->getUserText() ) . "', '" .
433 $this->getTimestamp() . "', " . $me1 . ", '" .
434 wfInvertTimestamp( $this->getTimestamp() ) . "')";
435 $res = wfQuery( $sql, $fname );
436 $oldid = wfInsertID( $res );
437
438 $now = wfTimestampNow();
439 $won = wfInvertTimestamp( $now );
440 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
441 "',cur_comment='" . wfStrencode( $summary ) .
442 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
443 ",cur_timestamp='{$now}',cur_user_text='" .
444 wfStrencode( $wgUser->getName() ) .
445 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
446 "WHERE cur_id=" . $this->getID();
447 wfQuery( $sql, $fname );
448
449 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
450 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
451 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
452 "'{$now}','{$now}'," . $wgTitle->getNamespace() . ",'" .
453 wfStrencode( $wgTitle->getDBkey() ) . "',0,{$me2}," .
454 ( $wgUser->isBot() ? 1 : 0 ) . "," .
455 $this->getID() . "," . $wgUser->getID() . ",'" .
456 wfStrencode( $wgUser->getName() ) . "','" .
457 wfStrencode( $summary ) . "',0,{$oldid})";
458 wfQuery( $sql, $fname );
459
460 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
461 "WHERE rc_namespace=" . $wgTitle->getNamespace() . " AND " .
462 "rc_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' AND " .
463 "rc_timestamp='" . $this->getTimestamp() . "'";
464 wfQuery( $sql, $fname );
465
466 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
467 "WHERE rc_cur_id=" . $this->getID();
468 wfQuery( $sql, $fname );
469 }
470 if( $wgDBtransactions ) {
471 $sql = "COMMIT";
472 wfQuery( $sql );
473 }
474
475 if ($watchthis) {
476 if (!$wgTitle->userIsWatching()) $this->watch();
477 } else {
478 if ( $wgTitle->userIsWatching() ) {
479 $this->unwatch();
480 }
481 }
482
483 $this->showArticle( $text, wfMsg( "updated" ) );
484 }
485
486 # After we've either updated or inserted the article, update
487 # the link tables and redirect to the new page.
488
489 function showArticle( $text, $subtitle )
490 {
491 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
492
493 $wgLinkCache = new LinkCache();
494
495 # Get old version of link table to allow incremental link updates
496 if ( $wgUseBetterLinksUpdate ) {
497 $wgLinkCache->preFill( $wgTitle );
498 $wgLinkCache->clear();
499 }
500
501 # Now update the link cache by parsing the text
502 $wgOut->addWikiText( $text );
503
504 $this->editUpdates( $text );
505 if( preg_match( "/^#redirect/i", $text ) )
506 $r = "redirect=no";
507 else
508 $r = "";
509 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL(), $r ) );
510 }
511
512 # If the page we've just displayed is in the "Image" namespace,
513 # we follow it with an upload history of the image and its usage.
514
515 function imageHistory()
516 {
517 global $wgUser, $wgOut, $wgLang, $wgTitle;
518 $fname = "Article::imageHistory";
519
520 $sql = "SELECT img_size,img_description,img_user," .
521 "img_user_text,img_timestamp FROM image WHERE " .
522 "img_name='" . wfStrencode( $wgTitle->getDBkey() ) . "'";
523 $res = wfQuery( $sql, $fname );
524
525 if ( 0 == wfNumRows( $res ) ) { return; }
526
527 $sk = $wgUser->getSkin();
528 $s = $sk->beginImageHistoryList();
529
530 $line = wfFetchObject( $res );
531 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
532 $wgTitle->getText(), $line->img_user,
533 $line->img_user_text, $line->img_size, $line->img_description );
534
535 $sql = "SELECT oi_size,oi_description,oi_user," .
536 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
537 "oi_name='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
538 "ORDER BY oi_timestamp DESC";
539 $res = wfQuery( $sql, $fname );
540
541 while ( $line = wfFetchObject( $res ) ) {
542 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
543 $line->oi_archive_name, $line->oi_user,
544 $line->oi_user_text, $line->oi_size, $line->oi_description );
545 }
546 $s .= $sk->endImageHistoryList();
547 $wgOut->addHTML( $s );
548 }
549
550 function imageLinks()
551 {
552 global $wgUser, $wgOut, $wgTitle;
553
554 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
555
556 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
557 wfStrencode( $wgTitle->getDBkey() ) . "'";
558 $res = wfQuery( $sql, "Article::imageLinks" );
559
560 if ( 0 == wfNumRows( $res ) ) {
561 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
562 return;
563 }
564 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
565
566 $sk = $wgUser->getSkin();
567 while ( $s = wfFetchObject( $res ) ) {
568 $name = $s->il_from;
569 $link = $sk->makeKnownLink( $name, "" );
570 $wgOut->addHTML( "<li>{$link}</li>\n" );
571 }
572 $wgOut->addHTML( "</ul>\n" );
573 }
574
575 # Add this page to my watchlist
576
577 function watch()
578 {
579 global $wgUser, $wgTitle, $wgOut, $wgLang;
580 global $wgDeferredUpdateList;
581
582 if ( 0 == $wgUser->getID() ) {
583 $wgOut->errorpage( "watchnologin", "watchnologintext" );
584 return;
585 }
586 if ( wfReadOnly() ) {
587 $wgOut->readOnlyPage();
588 return;
589 }
590 $wgUser->addWatch( $wgTitle );
591
592 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
593 $wgOut->setRobotpolicy( "noindex,follow" );
594
595 $sk = $wgUser->getSkin() ;
596 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
597
598 $text = str_replace( "$1", $link ,
599 wfMsg( "addedwatchtext" ) );
600 $wgOut->addHTML( $text );
601
602 $up = new UserUpdate();
603 array_push( $wgDeferredUpdateList, $up );
604
605 $wgOut->returnToMain( false );
606 }
607
608 function unwatch()
609 {
610 global $wgUser, $wgTitle, $wgOut, $wgLang;
611 global $wgDeferredUpdateList;
612
613 if ( 0 == $wgUser->getID() ) {
614 $wgOut->errorpage( "watchnologin", "watchnologintext" );
615 return;
616 }
617 if ( wfReadOnly() ) {
618 $wgOut->readOnlyPage();
619 return;
620 }
621 $wgUser->removeWatch( $wgTitle );
622
623 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
624 $wgOut->setRobotpolicy( "noindex,follow" );
625
626 $sk = $wgUser->getSkin() ;
627 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
628
629 $text = str_replace( "$1", $link ,
630 wfMsg( "removedwatchtext" ) );
631 $wgOut->addHTML( $text );
632
633 $up = new UserUpdate();
634 array_push( $wgDeferredUpdateList, $up );
635
636 $wgOut->returnToMain( false );
637 }
638
639 # This shares a lot of issues (and code) with Recent Changes
640
641 function history()
642 {
643 global $wgUser, $wgOut, $wgLang, $wgTitle, $offset, $limit;
644
645 # If page hasn't changed, client can cache this
646
647 $wgOut->checkLastModified( $this->getTimestamp() );
648 wfProfileIn( "Article::history" );
649
650 $wgOut->setPageTitle( $wgTitle->getPRefixedText() );
651 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
652 $wgOut->setArticleFlag( false );
653 $wgOut->setRobotpolicy( "noindex,nofollow" );
654
655 if( $wgTitle->getArticleID() == 0 ) {
656 $wgOut->addHTML( wfMsg( "nohistory" ) );
657 wfProfileOut();
658 return;
659 }
660
661 $offset = (int)$offset;
662 $limit = (int)$limit;
663 if( $limit == 0 ) $limit = 50;
664 $namespace = $wgTitle->getNamespace();
665 $title = $wgTitle->getText();
666 $sql = "SELECT old_id,old_user," .
667 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
668 "FROM old USE INDEX (name_title_timestamp) " .
669 "WHERE old_namespace={$namespace} AND " .
670 "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
671 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
672 $res = wfQuery( $sql, "Article::history" );
673
674 $revs = wfNumRows( $res );
675 if( $wgTitle->getArticleID() == 0 ) {
676 $wgOut->addHTML( wfMsg( "nohistory" ) );
677 wfProfileOut();
678 return;
679 }
680
681 $sk = $wgUser->getSkin();
682 $numbar = wfViewPrevNext(
683 $offset, $limit,
684 $wgTitle->getPrefixedText(),
685 "action=history" );
686 $s = $numbar;
687 $s .= $sk->beginHistoryList();
688
689 if($offset == 0 )
690 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
691 $this->getUserText(), $namespace,
692 $title, 0, $this->getComment(),
693 ( $this->getMinorEdit() > 0 ) );
694
695 $revs = wfNumRows( $res );
696 while ( $line = wfFetchObject( $res ) ) {
697 $s .= $sk->historyLine( $line->old_timestamp, $line->old_user,
698 $line->old_user_text, $namespace,
699 $title, $line->old_id,
700 $line->old_comment, ( $line->old_minor_edit > 0 ) );
701 }
702 $s .= $sk->endHistoryList();
703 $s .= $numbar;
704 $wgOut->addHTML( $s );
705 wfProfileOut();
706 }
707
708 function protect()
709 {
710 global $wgUser, $wgOut, $wgTitle;
711
712 if ( ! $wgUser->isSysop() ) {
713 $wgOut->sysopRequired();
714 return;
715 }
716 if ( wfReadOnly() ) {
717 $wgOut->readOnlyPage();
718 return;
719 }
720 $id = $wgTitle->getArticleID();
721 if ( 0 == $id ) {
722 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
723 return;
724 }
725 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
726 "cur_restrictions='sysop' WHERE cur_id={$id}";
727 wfQuery( $sql, "Article::protect" );
728
729 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
730 }
731
732 function unprotect()
733 {
734 global $wgUser, $wgOut, $wgTitle;
735
736 if ( ! $wgUser->isSysop() ) {
737 $wgOut->sysopRequired();
738 return;
739 }
740 if ( wfReadOnly() ) {
741 $wgOut->readOnlyPage();
742 return;
743 }
744 $id = $wgTitle->getArticleID();
745 if ( 0 == $id ) {
746 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
747 return;
748 }
749 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
750 "cur_restrictions='' WHERE cur_id={$id}";
751 wfQuery( $sql, "Article::unprotect" );
752
753 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
754 }
755
756 function delete()
757 {
758 global $wgUser, $wgOut, $wgTitle;
759 global $wpConfirm, $wpReason, $image, $oldimage;
760
761 # Anybody can delete old revisions of images; only sysops
762 # can delete articles and current images
763
764 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
765 $wgOut->sysopRequired();
766 return;
767 }
768 if ( wfReadOnly() ) {
769 $wgOut->readOnlyPage();
770 return;
771 }
772
773 # Better double-check that it hasn't been deleted yet!
774 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
775 if ( $image ) {
776 if ( "" == trim( $image ) ) {
777 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
778 return;
779 }
780 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
781 } else {
782
783 if ( ( "" == trim( $wgTitle->getText() ) )
784 or ( $wgTitle->getArticleId() == 0 ) ) {
785 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
786 return;
787 }
788 $sub = str_replace( "$1", $wgTitle->getPrefixedText(),
789 wfMsg( "deletesub" ) );
790
791 # determine whether this page has earlier revisions
792 # and insert a warning if it does
793 # we select the text because it might be useful below
794 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
795 $res=wfQuery($sql,$fname);
796 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
797 $skin=$wgUser->getSkin();
798 $wgOut->addHTML("<B>".wfMsg("historywarning"));
799 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
800 }
801
802 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."'";
803 $res=wfQuery($sql,$fname);
804 if( ($s=wfFetchObject($res))) {
805
806 # if this is a mini-text, we can paste part of it into the deletion reason
807
808 #if this is empty, an earlier revision may contain "useful" text
809 if($s->cur_text!="") {
810 $text=$s->cur_text;
811 } else {
812 if($old) {
813 $text=$old->old_text;
814 $blanked=1;
815 }
816
817 }
818
819 $length=strlen($text);
820
821 # this should not happen, since it is not possible to store an empty, new
822 # page. Let's insert a standard text in case it does, though
823 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
824
825
826 if($length < 500 && !$wpReason) {
827
828 # comment field=255, let's grep the first 150 to have some user
829 # space left
830 $text=substr($text,0,150);
831 # let's strip out newlines and HTML tags
832 $text=preg_replace("/\"/","'",$text);
833 $text=preg_replace("/\</","&lt;",$text);
834 $text=preg_replace("/\>/","&gt;",$text);
835 $text=preg_replace("/[\n\r]/","",$text);
836 if(!$blanked) {
837 $wpReason=wfMsg("excontent"). " '".$text;
838 } else {
839 $wpReason=wfMsg("exbeforeblank") . " '".$text;
840 }
841 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
842 $wpReason.="'";
843 }
844 }
845
846 }
847
848 # Likewise, deleting old images doesn't require confirmation
849 if ( $oldimage || 1 == $wpConfirm ) {
850 $this->doDelete();
851 return;
852 }
853
854 $wgOut->setSubtitle( $sub );
855 $wgOut->setRobotpolicy( "noindex,nofollow" );
856 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
857
858 $t = $wgTitle->getPrefixedURL();
859 $q = "action=delete";
860
861 if ( $image ) {
862 $q .= "&image={$image}";
863 } else if ( $oldimage ) {
864 $q .= "&oldimage={$oldimage}";
865 } else {
866 $q .= "&title={$t}";
867 }
868 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
869 $confirm = wfMsg( "confirm" );
870 $check = wfMsg( "confirmcheck" );
871 $delcom = wfMsg( "deletecomment" );
872
873 $wgOut->addHTML( "
874 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
875 <table border=0><tr><td align=right>
876 {$delcom}:</td><td align=left>
877 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
878 </td></tr><tr><td>&nbsp;</td></tr>
879 <tr><td align=right>
880 <input type=checkbox name=\"wpConfirm\" value='1'>
881 </td><td>{$check}</td>
882 </tr><tr><td>&nbsp;</td><td>
883 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
884 </td></tr></table></form>\n" );
885
886 $wgOut->returnToMain( false );
887 }
888
889 function doDelete()
890 {
891 global $wgOut, $wgTitle, $wgUser, $wgLang;
892 global $image, $oldimage, $wpReason;
893 $fname = "Article::doDelete";
894
895 if ( $image ) {
896 $dest = wfImageDir( $image );
897 $archive = wfImageDir( $image );
898 if ( ! unlink( "{$dest}/{$image}" ) ) {
899 $wgOut->fileDeleteError( "{$dest}/{$image}" );
900 return;
901 }
902 $sql = "DELETE FROM image WHERE img_name='" .
903 wfStrencode( $image ) . "'";
904 wfQuery( $sql, $fname );
905
906 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
907 wfStrencode( $image ) . "'";
908 $res = wfQuery( $sql, $fname );
909
910 while ( $s = wfFetchObject( $res ) ) {
911 $this->doDeleteOldImage( $s->oi_archive_name );
912 }
913 $sql = "DELETE FROM oldimage WHERE oi_name='" .
914 wfStrencode( $image ) . "'";
915 wfQuery( $sql, $fname );
916
917 # Image itself is now gone, and database is cleaned.
918 # Now we remove the image description page.
919
920 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
921 $this->doDeleteArticle( $nt );
922
923 $deleted = $image;
924 } else if ( $oldimage ) {
925 $this->doDeleteOldImage( $oldimage );
926 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
927 wfStrencode( $oldimage ) . "'";
928 wfQuery( $sql, $fname );
929
930 $deleted = $oldimage;
931 } else {
932 $this->doDeleteArticle( $wgTitle );
933 $deleted = $wgTitle->getPrefixedText();
934 }
935 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
936 $wgOut->setRobotpolicy( "noindex,nofollow" );
937
938 $sk = $wgUser->getSkin();
939 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
940 Namespace::getWikipedia() ) .
941 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
942
943 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
944 $text = str_replace( "$2", $loglink, $text );
945
946 $wgOut->addHTML( "<p>" . $text );
947 $wgOut->returnToMain( false );
948 }
949
950 function doDeleteOldImage( $oldimage )
951 {
952 global $wgOut;
953
954 $name = substr( $oldimage, 15 );
955 $archive = wfImageArchiveDir( $name );
956 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
957 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
958 }
959 }
960
961 function doDeleteArticle( $title )
962 {
963 global $wgUser, $wgOut, $wgLang, $wpReason, $wgTitle, $wgDeferredUpdateList;
964
965 $fname = "Article::doDeleteArticle";
966 $ns = $title->getNamespace();
967 $t = wfStrencode( $title->getDBkey() );
968 $id = $title->getArticleID();
969
970 if ( "" == $t ) {
971 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
972 return;
973 }
974
975 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
976 array_push( $wgDeferredUpdateList, $u );
977
978 # Move article and history to the "archive" table
979 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
980 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
981 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
982 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
983 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
984 wfQuery( $sql, $fname );
985
986 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
987 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
988 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
989 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
990 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
991 wfQuery( $sql, $fname );
992
993 # Now that it's safely backed up, delete it
994
995 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
996 "cur_title='{$t}'";
997 wfQuery( $sql, $fname );
998
999 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1000 "old_title='{$t}'";
1001 wfQuery( $sql, $fname );
1002
1003 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1004 "rc_title='{$t}'";
1005 wfQuery( $sql, $fname );
1006
1007 # Finally, clean up the link tables
1008
1009 if ( 0 != $id ) {
1010 $t = wfStrencode( $title->getPrefixedDBkey() );
1011 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1012 $res = wfQuery( $sql, $fname );
1013
1014 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1015 $now = wfTimestampNow();
1016 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1017 $first = true;
1018
1019 while ( $s = wfFetchObject( $res ) ) {
1020 $nt = Title::newFromDBkey( $s->l_from );
1021 $lid = $nt->getArticleID();
1022
1023 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1024 $first = false;
1025 $sql .= "({$lid},'{$t}')";
1026 $sql2 .= "{$lid}";
1027 }
1028 $sql2 .= ")";
1029 if ( ! $first ) {
1030 wfQuery( $sql, $fname );
1031 wfQuery( $sql2, $fname );
1032 }
1033 wfFreeResult( $res );
1034
1035 $sql = "DELETE FROM links WHERE l_to={$id}";
1036 wfQuery( $sql, $fname );
1037
1038 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1039 wfQuery( $sql, $fname );
1040
1041 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1042 wfQuery( $sql, $fname );
1043
1044 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1045 wfQuery( $sql, $fname );
1046 }
1047
1048 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1049 $art = $title->getPrefixedText();
1050 $wpReason = wfCleanQueryVar( $wpReason );
1051 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1052
1053 # Clear the cached article id so the interface doesn't act like we exist
1054 $wgTitle->resetArticleID( 0 );
1055 $wgTitle->mArticleID = 0;
1056 }
1057
1058 function revert()
1059 {
1060 global $wgOut;
1061 global $oldimage;
1062
1063 if ( strlen( $oldimage ) < 16 ) {
1064 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1065 return;
1066 }
1067 if ( wfReadOnly() ) {
1068 $wgOut->readOnlyPage();
1069 return;
1070 }
1071 $name = substr( $oldimage, 15 );
1072
1073 $dest = wfImageDir( $name );
1074 $archive = wfImageArchiveDir( $name );
1075 $curfile = "{$dest}/{$name}";
1076
1077 if ( ! is_file( $curfile ) ) {
1078 $wgOut->fileNotFoundError( $curfile );
1079 return;
1080 }
1081 $oldver = wfTimestampNow() . "!{$name}";
1082 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1083 wfStrencode( $oldimage ) . "'" );
1084
1085 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1086 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1087 return;
1088 }
1089 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1090 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1091 }
1092 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1093
1094 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1095 $wgOut->setRobotpolicy( "noindex,nofollow" );
1096 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1097 $wgOut->returnToMain( false );
1098 }
1099
1100 function rollback()
1101 {
1102 global $wgUser, $wgTitle, $wgLang, $wgOut, $from;
1103
1104 if ( ! $wgUser->isSysop() ) {
1105 $wgOut->sysopRequired();
1106 return;
1107 }
1108
1109 # Replace all this user's current edits with the next one down
1110 $tt = wfStrencode( $wgTitle->getDBKey() );
1111 $n = $wgTitle->getNamespace();
1112
1113 # Get the last editor
1114 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1115 $res = wfQuery( $sql );
1116 if( ($x = wfNumRows( $res )) != 1 ) {
1117 # Something wrong
1118 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1119 return;
1120 }
1121 $s = wfFetchObject( $res );
1122 $ut = wfStrencode( $s->cur_user_text );
1123 $uid = $s->cur_user;
1124 $pid = $s->cur_id;
1125
1126 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1127 if( $from != $s->cur_user_text ) {
1128 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1129 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1130 htmlspecialchars( $wgTitle->getPrefixedText()),
1131 htmlspecialchars( $from ),
1132 htmlspecialchars( $s->cur_user_text ) ) );
1133 if($s->cur_comment != "") {
1134 $wgOut->addHTML(
1135 wfMsg("editcomment",
1136 htmlspecialchars( $s->cur_comment ) ) );
1137 }
1138 return;
1139 }
1140
1141 # Get the last edit not by this guy
1142 $sql = "SELECT old_text,old_user,old_user_text
1143 FROM old USE INDEX (name_title_timestamp)
1144 WHERE old_namespace={$n} AND old_title='{$tt}'
1145 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1146 ORDER BY inverse_timestamp LIMIT 1";
1147 $res = wfQuery( $sql );
1148 if( wfNumRows( $res ) != 1 ) {
1149 # Something wrong
1150 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1151 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1152 return;
1153 }
1154 $s = wfFetchObject( $res );
1155
1156 # Save it!
1157 $newcomment = str_replace( "$1", $s->old_user_text, wfMsg( "revertpage" ) );
1158 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1159 $wgOut->setRobotpolicy( "noindex,nofollow" );
1160 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1161 $this->updateArticle( $s->old_text, $newcomment, 1, $wgTitle->userIsWatching() );
1162
1163 $wgOut->returnToMain( false );
1164 }
1165
1166
1167 # Do standard deferred updates after page view
1168
1169 /* private */ function viewUpdates()
1170 {
1171 global $wgDeferredUpdateList, $wgTitle;
1172
1173 if ( 0 != $this->getID() ) {
1174 $u = new ViewCountUpdate( $this->getID() );
1175 array_push( $wgDeferredUpdateList, $u );
1176 $u = new SiteStatsUpdate( 1, 0, 0 );
1177 array_push( $wgDeferredUpdateList, $u );
1178
1179 $u = new UserTalkUpdate( 0, $wgTitle->getNamespace(),
1180 $wgTitle->getDBkey() );
1181 array_push( $wgDeferredUpdateList, $u );
1182 }
1183 }
1184
1185 # Do standard deferred updates after page edit.
1186 # Every 1000th edit, prune the recent changes table.
1187
1188 /* private */ function editUpdates( $text )
1189 {
1190 global $wgDeferredUpdateList, $wgTitle;
1191
1192 wfSeedRandom();
1193 if ( 0 == mt_rand( 0, 999 ) ) {
1194 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1195 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1196 wfQuery( $sql );
1197 }
1198 $id = $this->getID();
1199 $title = $wgTitle->getPrefixedDBkey();
1200 $adj = $this->mCountAdjustment;
1201
1202 if ( 0 != $id ) {
1203 $u = new LinksUpdate( $id, $title );
1204 array_push( $wgDeferredUpdateList, $u );
1205 $u = new SiteStatsUpdate( 0, 1, $adj );
1206 array_push( $wgDeferredUpdateList, $u );
1207 $u = new SearchUpdate( $id, $title, $text );
1208 array_push( $wgDeferredUpdateList, $u );
1209
1210 $u = new UserTalkUpdate( 1, $wgTitle->getNamespace(),
1211 $wgTitle->getDBkey() );
1212 array_push( $wgDeferredUpdateList, $u );
1213 }
1214 }
1215
1216 /* private */ function setOldSubtitle()
1217 {
1218 global $wgLang, $wgOut;
1219
1220 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1221 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1222 $wgOut->setSubtitle( "({$r})" );
1223 }
1224
1225 # This function is called right before saving the wikitext,
1226 # so we can do things like signatures and links-in-context.
1227
1228 function preSaveTransform( $text )
1229 {
1230 $s = "";
1231 while ( "" != $text ) {
1232 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1233 $s .= $this->pstPass2( $p[0] );
1234
1235 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1236 else {
1237 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1238 $s .= "<nowiki>{$q[0]}</nowiki>";
1239 $text = $q[1];
1240 }
1241 }
1242 return rtrim( $s );
1243 }
1244
1245 /* private */ function pstPass2( $text )
1246 {
1247 global $wgUser, $wgLang, $wgTitle, $wgLocaltimezone;
1248
1249 # Signatures
1250 #
1251 $n = $wgUser->getName();
1252 $k = $wgUser->getOption( "nickname" );
1253 if ( "" == $k ) { $k = $n; }
1254 if(isset($wgLocaltimezone)) {
1255 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1256 }
1257 /* Note: this is an ugly timezone hack for the European wikis */
1258 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1259 " (" . date( "T" ) . ")";
1260 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1261
1262 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1263 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1264 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1265 Namespace::getUser() ) . ":$n|$k]]", $text );
1266
1267 # Context links: [[|name]] and [[name (context)|]]
1268 #
1269 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1270 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1271 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1272
1273 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1274 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1275 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1276 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1277 # [[ns:page (cont)|]]
1278 $context = "";
1279 $t = $wgTitle->getText();
1280 if ( preg_match( $conpat, $t, $m ) ) {
1281 $context = $m[2];
1282 }
1283 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1284 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1285 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1286
1287 if ( "" == $context ) {
1288 $text = preg_replace( $p2, "[[\\1]]", $text );
1289 } else {
1290 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1291 }
1292 # Replace local image links with new [[image:]] style
1293
1294 $text = preg_replace(
1295 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1296 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1297 "\\1[[image:\\3.\\4]]", $text );
1298 $text = preg_replace(
1299 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1300 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1301 "\\1[[image:\\3.\\4]]", $text );
1302
1303 return $text;
1304 }
1305
1306 /* Caching functions */
1307
1308 function tryFileCache() {
1309 global $wgTitle;
1310
1311 if($this->isFileCacheable()) {
1312 $cache = new CacheManager( $wgTitle );
1313 if($cache->isFileCacheGood( $this->mTouched )) {
1314 wfDebug( " tryFileCache() - about to load\n" );
1315 $cache->loadFromFileCache();
1316 exit;
1317 } else {
1318 wfDebug( " tryFileCache() - starting buffer\n" );
1319 if($cache->useGzip() && wfClientAcceptsGzip()) {
1320 /* For some reason, adding this header line over in
1321 CacheManager::saveToFileCache() fails on my test
1322 setup at home, though it works on the live install.
1323 Make double-sure... --brion */
1324 header( "Content-Encoding: gzip" );
1325 }
1326 ob_start( array(&$cache, 'saveToFileCache' ) );
1327 }
1328 } else {
1329 wfDebug( " tryFileCache() - not cacheable\n" );
1330 }
1331 }
1332
1333 function isFileCacheable() {
1334 global $wgUser, $wgTitle, $wgUseFileCache, $wgShowIPinHeader;
1335 global $action, $oldid, $diff, $redirect, $printable;
1336 return $wgUseFileCache
1337 and (!$wgShowIPinHeader)
1338 and ($wgUser->getId() == 0)
1339 and (!$wgUser->getNewtalk())
1340 and ($wgTitle->getNamespace != Namespace::getSpecial())
1341 and ($action == "view")
1342 and (!isset($oldid))
1343 and (!isset($diff))
1344 and (!isset($redirect))
1345 and (!isset($printable))
1346 and (!$this->mRedirectedFrom);
1347
1348 }
1349
1350
1351 }
1352
1353 ?>