2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
11 * s/\$wgTitle/\$this->mTitle/ performed, many replacements
12 * mTitle variable added to class
15 include_once( "CacheManager.php" );
18 /* private */ var $mContent, $mContentLoaded;
19 /* private */ var $mUser, $mTimestamp, $mUserText;
20 /* private */ var $mCounter, $mComment, $mCountAdjustment;
21 /* private */ var $mMinorEdit, $mRedirectedFrom;
22 /* private */ var $mTouched, $mFileCache, $mTitle;
24 function Article( &$title ) {
25 $this->mTitle
=& $title;
29 /* private */ function clear()
31 $this->mContentLoaded
= false;
32 $this->mUser
= $this->mCounter
= -1; # Not loaded
33 $this->mRedirectedFrom
= $this->mUserText
=
34 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
35 $this->mCountAdjustment
= 0;
36 $this->mTouched
= "19700101000000";
39 # Note that getContent/loadContent may follow redirects if
40 # not told otherwise, and so may cause a change to mTitle.
42 function getContent( $noredir = false )
44 global $action,$section,$count; # From query string
45 $fname = "Article::getContent";
46 wfProfileIn( $fname );
48 if ( 0 == $this->getID() ) {
49 if ( "edit" == $action ) {
50 wfProfileOut( $fname );
51 return ""; # was "newarticletext", now moved above the box)
53 wfProfileOut( $fname );
54 return wfMsg( "noarticletext" );
56 $this->loadContent( $noredir );
59 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
60 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
61 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
65 wfProfileOut( $fname );
66 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
71 wfProfileOut( $fname );
75 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
77 PREG_SPLIT_DELIM_CAPTURE
);
79 wfProfileOut( $fname );
80 return trim($secs[0]);
82 wfProfileOut( $fname );
83 return trim($secs[$section*2-1] . $secs[$section*2]);
87 wfProfileOut( $fname );
88 return $this->mContent
;
93 function loadContent( $noredir = false )
95 global $wgOut, $wgMwRedir;
96 global $oldid, $redirect; # From query
98 if ( $this->mContentLoaded
) return;
99 $fname = "Article::loadContent";
101 # Pre-fill content with error message so that if something
102 # fails we'll have something telling us what we intended.
104 $t = $this->mTitle
->getPrefixedText();
105 if ( isset( $oldid ) ) {
106 $oldid = IntVal( $oldid );
107 $t .= ",oldid={$oldid}";
109 if ( isset( $redirect ) ) {
110 $redirect = ($redirect == "no") ?
"no" : "yes";
111 $t .= ",redirect={$redirect}";
113 $this->mContent
= wfMsg( "missingarticle", $t );
115 if ( ! $oldid ) { # Retrieve current version
116 $id = $this->getID();
117 if ( 0 == $id ) return;
120 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
121 "FROM cur WHERE cur_id={$id}";
123 $res = wfQuery( $sql, DB_READ
, $fname );
124 if ( 0 == wfNumRows( $res ) ) {
128 $s = wfFetchObject( $res );
129 # If we got a redirect, follow it (unless we've been told
130 # not to by either the function parameter or the query
131 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
132 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
133 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
134 $s->cur_text
, $m ) ) {
135 $rt = Title
::newFromText( $m[1] );
137 # Gotta hand redirects to special pages differently:
138 # Fill the HTTP response "Location" header and ignore
139 # the rest of the page we're on.
141 if ( $rt->getInterwiki() != "" ) {
142 $wgOut->redirect( $rt->getFullURL() ) ;
145 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
146 $wgOut->redirect( wfLocalUrl(
147 $rt->getPrefixedURL() ) );
150 $rid = $rt->getArticleID();
152 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
153 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
154 $res = wfQuery( $sql, DB_READ
, $fname );
156 if ( 0 != wfNumRows( $res ) ) {
157 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
159 $s = wfFetchObject( $res );
166 $this->mContent
= $s->cur_text
;
167 $this->mUser
= $s->cur_user
;
168 $this->mCounter
= $s->cur_counter
;
169 $this->mTimestamp
= $s->cur_timestamp
;
170 $this->mTouched
= $s->cur_touched
;
171 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
172 $this->mTitle
->mRestrictionsLoaded
= true;
173 wfFreeResult( $res );
174 } else { # oldid set, retrieve historical version
175 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
176 "WHERE old_id={$oldid}";
177 $res = wfQuery( $sql, DB_READ
, $fname );
178 if ( 0 == wfNumRows( $res ) ) { return; }
180 $s = wfFetchObject( $res );
181 $this->mContent
= $s->old_text
;
182 $this->mUser
= $s->old_user
;
184 $this->mTimestamp
= $s->old_timestamp
;
185 wfFreeResult( $res );
187 $this->mContentLoaded
= true;
191 if( $this->mTitle
) {
192 return $this->mTitle
->getArticleID();
200 if ( -1 == $this->mCounter
) {
201 $id = $this->getID();
202 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
204 return $this->mCounter
;
207 # Would the given text make this article a "good" article (i.e.,
208 # suitable for including in the article count)?
210 function isCountable( $text )
212 global $wgUseCommaCount, $wgMwRedir;
214 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
215 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
216 $token = ($wgUseCommaCount ?
"," : "[[" );
217 if ( false === strstr( $text, $token ) ) { return 0; }
221 # Load the field related to the last edit time of the article.
222 # This isn't necessary for all uses, so it's only done if needed.
224 /* private */ function loadLastEdit()
227 if ( -1 != $this->mUser
) return;
229 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
230 "cur_comment,cur_minor_edit FROM cur WHERE " .
231 "cur_id=" . $this->getID();
232 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
234 if ( wfNumRows( $res ) > 0 ) {
235 $s = wfFetchObject( $res );
236 $this->mUser
= $s->cur_user
;
237 $this->mUserText
= $s->cur_user_text
;
238 $this->mTimestamp
= $s->cur_timestamp
;
239 $this->mComment
= $s->cur_comment
;
240 $this->mMinorEdit
= $s->cur_minor_edit
;
244 function getTimestamp()
246 $this->loadLastEdit();
247 return $this->mTimestamp
;
252 $this->loadLastEdit();
256 function getUserText()
258 $this->loadLastEdit();
259 return $this->mUserText
;
262 function getComment()
264 $this->loadLastEdit();
265 return $this->mComment
;
268 function getMinorEdit()
270 $this->loadLastEdit();
271 return $this->mMinorEdit
;
274 # This is the default action of the script: just view the page of
279 global $wgUser, $wgOut, $wgLang;
280 global $oldid, $diff; # From query
281 global $wgLinkCache, $IP;
282 $fname = "Article::view";
283 wfProfileIn( $fname );
285 $wgOut->setArticleFlag( true );
286 $wgOut->setRobotpolicy( "index,follow" );
288 # If we got diff and oldid in the query, we want to see a
289 # diff page instead of the article.
291 if ( isset( $diff ) ) {
292 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
293 $de = new DifferenceEngine( $oldid, $diff );
295 wfProfileOut( $fname );
299 if ( !isset( $oldid ) and $this->checkTouched() ) {
300 if( $wgOut->checkLastModified( $this->mTouched
) ){
302 } else if ( $this->tryFileCache() ) {
303 # tell wgOut that output is taken care of
305 $this->viewUpdates();
310 $text = $this->getContent(); # May change mTitle
311 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
312 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
313 " - " . wfMsg( "wikititlesuffix" ) );
315 # We're looking at an old revision
318 $this->setOldSubtitle();
319 $wgOut->setRobotpolicy( "noindex,follow" );
321 if ( "" != $this->mRedirectedFrom
) {
322 $sk = $wgUser->getSkin();
323 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
325 $s = wfMsg( "redirectedfrom", $redir );
326 $wgOut->setSubtitle( $s );
329 $wgLinkCache->preFill( $this->mTitle
);
330 $wgOut->addWikiText( $text );
332 $this->viewUpdates();
333 wfProfileOut( $fname );
336 # Theoretically we could defer these whole insert and update
337 # functions for after display, but that's taking a big leap
338 # of faith, and we want to be able to report database
339 # errors at some point.
341 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
343 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
344 global $wgEnablePersistentLC;
346 $fname = "Article::insertNewArticle";
348 $this->mCountAdjustment
= $this->isCountable( $text );
350 $ns = $this->mTitle
->getNamespace();
351 $ttl = $this->mTitle
->getDBkey();
352 $text = $this->preSaveTransform( $text );
353 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
356 $now = wfTimestampNow();
357 $won = wfInvertTimestamp( $now );
359 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
360 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
361 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
362 "cur_restrictions,cur_user_text,cur_is_redirect," .
363 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
364 wfStrencode( $text ) . "', '" .
365 wfStrencode( $summary ) . "', '" .
366 $wgUser->getID() . "', '{$now}', " .
367 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
368 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
369 $res = wfQuery( $sql, DB_WRITE
, $fname );
371 $newid = wfInsertId();
372 $this->mTitle
->resetArticleID( $newid );
374 if ( $wgEnablePersistentLC ) {
375 // Purge related entries in links cache on new page, to heal broken links
376 $ptitle = wfStrencode( $ttl );
377 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
378 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE
);
381 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
382 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
383 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
384 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
385 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
386 wfStrencode( $wgUser->getName() ) . "','" .
387 wfStrencode( $summary ) . "',0,0," .
388 ( $wgUser->isBot() ?
1 : 0 ) . ")";
389 wfQuery( $sql, DB_WRITE
, $fname );
391 if(!$this->mTitle
->userIsWatching()) $this->watch();
393 if ( $this->mTitle
->userIsWatching() ) {
398 # The talk page isn't in the regular link tables, so we need to update manually:
399 $talkns = $ns ^
1; # talk -> normal; normal -> talk
400 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
401 wfQuery( $sql, DB_WRITE
);
403 $this->showArticle( $text, wfMsg( "newarticle" ) );
406 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
408 global $wgOut, $wgUser, $wgLinkCache;
409 global $wgDBtransactions, $wgMwRedir;
410 $fname = "Article::updateArticle";
412 $this->loadLastEdit();
414 // insert updated section into old text if we have only edited part
416 if ($section != "") {
417 $oldtext=$this->getContent();
418 if($section=="new") {
419 if($summary) $subject="== {$summary} ==\n\n";
420 $text=$oldtext."\n\n".$subject.$text;
422 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
423 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
424 $secs[$section*2]=$text."\n\n"; // replace with edited
425 if($section) { $secs[$section*2-1]=""; } // erase old headline
426 $text=join("",$secs);
429 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
430 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
431 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
433 $text = $m[1] . "\n"; # Remove all content but redirect
437 $text = $this->preSaveTransform( $text );
439 # Update article, but only if changed.
441 if( $wgDBtransactions ) {
443 wfQuery( $sql, DB_WRITE
);
445 $oldtext = $this->getContent( true );
447 if ( 0 != strcmp( $text, $oldtext ) ) {
448 $this->mCountAdjustment
= $this->isCountable( $text )
449 - $this->isCountable( $oldtext );
451 $now = wfTimestampNow();
452 $won = wfInvertTimestamp( $now );
453 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
454 "',cur_comment='" . wfStrencode( $summary ) .
455 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
456 ",cur_timestamp='{$now}',cur_user_text='" .
457 wfStrencode( $wgUser->getName() ) .
458 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
459 "WHERE cur_id=" . $this->getID() .
460 " AND cur_timestamp='" . $this->getTimestamp() . "'";
461 $res = wfQuery( $sql, DB_WRITE
, $fname );
463 if( wfAffectedRows() == 0 ) {
464 /* Belated edit conflict! Run away!! */
468 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
469 "old_comment,old_user,old_user_text,old_timestamp," .
470 "old_minor_edit,inverse_timestamp) VALUES (" .
471 $this->mTitle
->getNamespace() . ", '" .
472 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
473 wfStrencode( $oldtext ) . "', '" .
474 wfStrencode( $this->getComment() ) . "', " .
475 $this->getUser() . ", '" .
476 wfStrencode( $this->getUserText() ) . "', '" .
477 $this->getTimestamp() . "', " . $me1 . ", '" .
478 wfInvertTimestamp( $this->getTimestamp() ) . "')";
479 $res = wfQuery( $sql, DB_WRITE
, $fname );
480 $oldid = wfInsertID( $res );
482 $bot = (int)($wgUser->isBot() ||
$forceBot);
484 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
485 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
486 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
487 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
488 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
489 "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
490 wfStrencode( $wgUser->getName() ) . "','" .
491 wfStrencode( $summary ) . "',0,{$oldid})";
492 wfQuery( $sql, DB_WRITE
, $fname );
494 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
495 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
496 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
497 "rc_timestamp='" . $this->getTimestamp() . "'";
498 wfQuery( $sql, DB_WRITE
, $fname );
500 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
501 "WHERE rc_cur_id=" . $this->getID();
502 wfQuery( $sql, DB_WRITE
, $fname );
504 global $wgEnablePersistentLC;
505 if ( $wgEnablePersistentLC ) {
506 // Purge link cache for this page
507 $pageid=$this->getID();
508 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE
);
512 if( $wgDBtransactions ) {
514 wfQuery( $sql, DB_WRITE
);
518 if (!$this->mTitle
->userIsWatching()) $this->watch();
520 if ( $this->mTitle
->userIsWatching() ) {
525 $this->showArticle( $text, wfMsg( "updated" ) );
529 # After we've either updated or inserted the article, update
530 # the link tables and redirect to the new page.
532 function showArticle( $text, $subtitle )
534 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
537 $wgLinkCache = new LinkCache();
539 # Get old version of link table to allow incremental link updates
540 if ( $wgUseBetterLinksUpdate ) {
541 $wgLinkCache->preFill( $this->mTitle
);
542 $wgLinkCache->clear();
545 # Now update the link cache by parsing the text
546 $wgOut = new OutputPage();
547 $wgOut->addWikiText( $text );
549 $this->editUpdates( $text );
550 if( $wgMwRedir->matchStart( $text ) )
554 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
557 # Add this page to my watchlist
559 function watch( $add = true )
561 global $wgUser, $wgOut, $wgLang;
562 global $wgDeferredUpdateList;
564 if ( 0 == $wgUser->getID() ) {
565 $wgOut->errorpage( "watchnologin", "watchnologintext" );
568 if ( wfReadOnly() ) {
569 $wgOut->readOnlyPage();
573 $wgUser->addWatch( $this->mTitle
);
575 $wgUser->removeWatch( $this->mTitle
);
577 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
578 $wgOut->setRobotpolicy( "noindex,follow" );
580 $sk = $wgUser->getSkin() ;
581 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
584 $text = wfMsg( "addedwatchtext", $link );
586 $text = wfMsg( "removedwatchtext", $link );
587 $wgOut->addHTML( $text );
589 $up = new UserUpdate();
590 array_push( $wgDeferredUpdateList, $up );
592 $wgOut->returnToMain( false );
597 $this->watch( false );
600 function protect( $limit = "sysop" )
602 global $wgUser, $wgOut;
604 if ( ! $wgUser->isSysop() ) {
605 $wgOut->sysopRequired();
608 if ( wfReadOnly() ) {
609 $wgOut->readOnlyPage();
612 $id = $this->mTitle
->getArticleID();
614 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
617 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
618 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
619 wfQuery( $sql, DB_WRITE
, "Article::protect" );
621 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
622 if ( $limit === "" ) {
623 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
625 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
627 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
632 return $this->protect( "" );
637 global $wgUser, $wgOut, $wgMessageCache;
638 global $wpConfirm, $wpReason, $image, $oldimage;
640 # This code desperately needs to be totally rewritten
642 if ( ( ! $wgUser->isSysop() ) ) {
643 $wgOut->sysopRequired();
646 if ( wfReadOnly() ) {
647 $wgOut->readOnlyPage();
651 # Can't delete cached MediaWiki namespace (i.e. vital messages)
652 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
653 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
657 # Better double-check that it hasn't been deleted yet!
658 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
659 if ( ( "" == trim( $this->mTitle
->getText() ) )
660 or ( $this->mTitle
->getArticleId() == 0 ) ) {
661 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
665 if ( $_POST["wpConfirm"] ) {
670 # determine whether this page has earlier revisions
671 # and insert a warning if it does
672 # we select the text because it might be useful below
673 $ns = $this->mTitle
->getNamespace();
674 $title = $this->mTitle
->getDBkey();
675 $etitle = wfStrencode( $title );
676 $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
677 $res = wfQuery( $sql, DB_READ
, $fname );
678 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
679 $skin=$wgUser->getSkin();
680 $wgOut->addHTML("<B>".wfMsg("historywarning"));
681 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
684 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
685 $res=wfQuery($sql, DB_READ
, $fname);
686 if( ($s=wfFetchObject($res))) {
688 # if this is a mini-text, we can paste part of it into the deletion reason
690 #if this is empty, an earlier revision may contain "useful" text
691 if($s->cur_text
!="") {
695 $text=$old->old_text
;
701 $length=strlen($text);
703 # this should not happen, since it is not possible to store an empty, new
704 # page. Let's insert a standard text in case it does, though
705 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
708 if($length < 500 && !$wpReason) {
710 # comment field=255, let's grep the first 150 to have some user
712 $text=substr($text,0,150);
713 # let's strip out newlines and HTML tags
714 $text=preg_replace("/\"/","'",$text);
715 $text=preg_replace("/\</","<",$text);
716 $text=preg_replace("/\>/",">",$text);
717 $text=preg_replace("/[\n\r]/","",$text);
719 $wpReason=wfMsg("excontent"). " '".$text;
721 $wpReason=wfMsg("exbeforeblank") . " '".$text;
723 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
728 return $this->confirmDelete();
731 function confirmDelete( $par = "" )
736 wfDebug( "Article::confirmDelete\n" );
738 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
739 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
740 $wgOut->setRobotpolicy( "noindex,nofollow" );
741 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
743 $t = $this->mTitle
->getPrefixedURL();
745 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
746 $confirm = wfMsg( "confirm" );
747 $check = wfMsg( "confirmcheck" );
748 $delcom = wfMsg( "deletecomment" );
751 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
752 <table border=0><tr><td align=right>
753 {$delcom}:</td><td align=left>
754 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
755 </td></tr><tr><td> </td></tr>
757 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
758 </td><td><label for=\"wpConfirm\">{$check}</label></td>
759 </tr><tr><td> </td><td>
760 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
761 </td></tr></table></form>\n" );
763 $wgOut->returnToMain( false );
768 global $wgOut, $wgUser, $wgLang;
770 $fname = "Article::doDelete";
771 wfDebug( "$fname\n" );
773 $this->doDeleteArticle( $this->mTitle
);
774 $deleted = $this->mTitle
->getPrefixedText();
776 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
777 $wgOut->setRobotpolicy( "noindex,nofollow" );
779 $sk = $wgUser->getSkin();
780 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
781 Namespace::getWikipedia() ) .
782 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
784 $text = wfMsg( "deletedtext", $deleted, $loglink );
786 $wgOut->addHTML( "<p>" . $text );
787 $wgOut->returnToMain( false );
790 function doDeleteArticle( $title )
792 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
793 $wgEnablePersistentLC;
795 $fname = "Article::doDeleteArticle";
796 wfDebug( "$fname\n" );
798 $ns = $title->getNamespace();
799 $t = wfStrencode( $title->getDBkey() );
800 $id = $title->getArticleID();
803 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
807 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
808 array_push( $wgDeferredUpdateList, $u );
810 # Move article and history to the "archive" table
811 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
812 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
813 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
814 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
815 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
816 wfQuery( $sql, DB_WRITE
, $fname );
818 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
819 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
820 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
821 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
822 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
823 wfQuery( $sql, DB_WRITE
, $fname );
825 # Now that it's safely backed up, delete it
827 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
829 wfQuery( $sql, DB_WRITE
, $fname );
831 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
833 wfQuery( $sql, DB_WRITE
, $fname );
835 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
837 wfQuery( $sql, DB_WRITE
, $fname );
839 # Finally, clean up the link tables
843 $t = wfStrencode( $title->getPrefixedDBkey() );
845 if ( $wgEnablePersistentLC ) {
846 // Purge related entries in links cache on delete,
847 wfQuery("DELETE linkscc FROM linkscc,links ".
848 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE
);
849 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE
);
852 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
853 $res = wfQuery( $sql, DB_READ
, $fname );
855 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
856 $now = wfTimestampNow();
857 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
860 while ( $s = wfFetchObject( $res ) ) {
861 $nt = Title
::newFromDBkey( $s->l_from
);
862 $lid = $nt->getArticleID();
864 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
866 $sql .= "({$lid},'{$t}')";
871 wfQuery( $sql, DB_WRITE
, $fname );
872 wfQuery( $sql2, DB_WRITE
, $fname );
874 wfFreeResult( $res );
876 $sql = "DELETE FROM links WHERE l_to={$id}";
877 wfQuery( $sql, DB_WRITE
, $fname );
879 $sql = "DELETE FROM links WHERE l_from='{$t}'";
880 wfQuery( $sql, DB_WRITE
, $fname );
882 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
883 wfQuery( $sql, DB_WRITE
, $fname );
885 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
886 wfQuery( $sql, DB_WRITE
, $fname );
889 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
890 $art = $title->getPrefixedText();
891 $wpReason = wfCleanQueryVar( $wpReason );
892 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
894 # Clear the cached article id so the interface doesn't act like we exist
895 $this->mTitle
->resetArticleID( 0 );
896 $this->mTitle
->mArticleID
= 0;
901 global $wgUser, $wgLang, $wgOut, $from;
903 if ( ! $wgUser->isSysop() ) {
904 $wgOut->sysopRequired();
907 if ( wfReadOnly() ) {
908 $wgOut->readOnlyPage( $this->getContent() );
912 # Secret enhanced rollback, marks edits rc_bot=1
913 $bot = !!$_REQUEST['bot'];
915 # Replace all this user's current edits with the next one down
916 $tt = wfStrencode( $this->mTitle
->getDBKey() );
917 $n = $this->mTitle
->getNamespace();
919 # Get the last editor
920 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
921 $res = wfQuery( $sql, DB_READ
);
922 if( ($x = wfNumRows( $res )) != 1 ) {
924 $wgOut->addHTML( wfMsg( "notanarticle" ) );
927 $s = wfFetchObject( $res );
928 $ut = wfStrencode( $s->cur_user_text
);
932 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
933 if( $from != $s->cur_user_text
) {
934 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
935 $wgOut->addWikiText( wfMsg( "alreadyrolled",
936 htmlspecialchars( $this->mTitle
->getPrefixedText()),
937 htmlspecialchars( $from ),
938 htmlspecialchars( $s->cur_user_text
) ) );
939 if($s->cur_comment
!= "") {
942 htmlspecialchars( $s->cur_comment
) ) );
947 # Get the last edit not by this guy
948 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp
949 FROM old USE INDEX (name_title_timestamp)
950 WHERE old_namespace={$n} AND old_title='{$tt}'
951 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
952 ORDER BY inverse_timestamp LIMIT 1";
953 $res = wfQuery( $sql, DB_READ
);
954 if( wfNumRows( $res ) != 1 ) {
956 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
957 $wgOut->addHTML( wfMsg( "cantrollback" ) );
960 $s = wfFetchObject( $res );
963 # Mark all reverted edits as bot
964 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
965 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
966 wfQuery( $sql, DB_WRITE
, $fname );
970 $newcomment = wfMsg( "revertpage", $s->old_user_text
);
971 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
972 $wgOut->setRobotpolicy( "noindex,nofollow" );
973 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
974 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
976 global $wgEnablePersistentLC;
977 if ( $wgEnablePersistentLC ) {
978 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
982 $wgOut->returnToMain( false );
986 # Do standard deferred updates after page view
988 /* private */ function viewUpdates()
990 global $wgDeferredUpdateList;
991 if ( 0 != $this->getID() ) {
992 global $wgDisableCounters;
993 if( !$wgDisableCounters ) {
994 Article
::incViewCount( $this->getID() );
995 $u = new SiteStatsUpdate( 1, 0, 0 );
996 array_push( $wgDeferredUpdateList, $u );
998 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
999 $this->mTitle
->getDBkey() );
1000 array_push( $wgDeferredUpdateList, $u );
1004 # Do standard deferred updates after page edit.
1005 # Every 1000th edit, prune the recent changes table.
1007 /* private */ function editUpdates( $text )
1009 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1010 global $wgMessageCache;
1013 if ( 0 == mt_rand( 0, 999 ) ) {
1014 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1015 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1016 wfQuery( $sql, DB_WRITE
);
1018 $id = $this->getID();
1019 $title = $this->mTitle
->getPrefixedDBkey();
1020 $shortTitle = $this->mTitle
->getDBkey();
1022 $adj = $this->mCountAdjustment
;
1025 $u = new LinksUpdate( $id, $title );
1026 array_push( $wgDeferredUpdateList, $u );
1027 $u = new SiteStatsUpdate( 0, 1, $adj );
1028 array_push( $wgDeferredUpdateList, $u );
1029 $u = new SearchUpdate( $id, $title, $text );
1030 array_push( $wgDeferredUpdateList, $u );
1032 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1033 array_push( $wgDeferredUpdateList, $u );
1035 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1036 $wgMessageCache->replace( $shortTitle, $text );
1041 /* private */ function setOldSubtitle()
1043 global $wgLang, $wgOut;
1045 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1046 $r = wfMsg( "revisionasof", $td );
1047 $wgOut->setSubtitle( "({$r})" );
1050 # This function is called right before saving the wikitext,
1051 # so we can do things like signatures and links-in-context.
1053 function preSaveTransform( $text )
1056 while ( "" != $text ) {
1057 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1058 $s .= $this->pstPass2( $p[0] );
1060 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1062 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1063 $s .= "<nowiki>{$q[0]}</nowiki>";
1070 /* private */ function pstPass2( $text )
1072 global $wgUser, $wgLang, $wgLocaltimezone;
1076 $n = $wgUser->getName();
1077 $k = $wgUser->getOption( "nickname" );
1078 if ( "" == $k ) { $k = $n; }
1079 if(isset($wgLocaltimezone)) {
1080 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1082 /* Note: this is an ugly timezone hack for the European wikis */
1083 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1084 " (" . date( "T" ) . ")";
1085 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1087 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1088 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1089 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1090 Namespace::getUser() ) . ":$n|$k]]", $text );
1092 # Context links: [[|name]] and [[name (context)|]]
1094 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1095 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1096 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1097 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1099 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1100 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1101 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1102 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1103 # [[ns:page (cont)|]]
1105 $t = $this->mTitle
->getText();
1106 if ( preg_match( $conpat, $t, $m ) ) {
1109 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1110 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1111 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1113 if ( "" == $context ) {
1114 $text = preg_replace( $p2, "[[\\1]]", $text );
1116 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1119 # {{SUBST:xxx}} variables
1121 $mw =& MagicWord
::get( MAG_SUBST
);
1122 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1125 # Trim trailing whitespace
1126 # MAG_END (__END__) tag allows for trailing
1127 # whitespace to be deliberately included
1128 $text = rtrim( $text );
1129 $mw =& MagicWord::get( MAG_END );
1130 $mw->matchAndRemove( $text );
1135 /* Caching functions */
1137 # checkLastModified returns true iff it has taken care of all
1138 # output to the client that is necessary for this request.
1139 # (that is, it has sent a cached version of the page)
1140 function tryFileCache() {
1141 static $called = false;
1143 wfDebug( " tryFileCache() -- called twice!?\n" );
1147 if($this->isFileCacheable()) {
1148 $touched = $this->mTouched
;
1149 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1150 # Expire the main page quicker
1151 $expire = wfUnix2Timestamp( time() - 3600 );
1152 $touched = max( $expire, $touched );
1154 $cache = new CacheManager( $this->mTitle
);
1155 if($cache->isFileCacheGood( $touched )) {
1157 wfDebug( " tryFileCache() - about to load\n" );
1158 $cache->loadFromFileCache();
1161 wfDebug( " tryFileCache() - starting buffer\n" );
1162 if($cache->useGzip() && wfClientAcceptsGzip()) {
1163 /* For some reason, adding this header line over in
1164 CacheManager::saveToFileCache() fails on my test
1165 setup at home, though it works on the live install.
1166 Make double-sure... --brion */
1167 header( "Content-Encoding: gzip" );
1169 ob_start( array(&$cache, 'saveToFileCache' ) );
1172 wfDebug( " tryFileCache() - not cacheable\n" );
1176 function isFileCacheable() {
1177 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1178 global $action, $oldid, $diff, $redirect, $printable;
1179 return $wgUseFileCache
1180 and (!$wgShowIPinHeader)
1181 and ($this->getID() != 0)
1182 and ($wgUser->getId() == 0)
1183 and (!$wgUser->getNewtalk())
1184 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1185 and ($action == "view")
1186 and (!isset($oldid))
1188 and (!isset($redirect))
1189 and (!isset($printable))
1190 and (!$this->mRedirectedFrom
);
1193 function checkTouched() {
1194 $id = $this->getID();
1195 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1196 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1197 if( $s = wfFetchObject( $res ) ) {
1198 $this->mTouched
= $s->cur_touched
;
1199 return !$s->cur_is_redirect
;
1205 /* static */ function incViewCount( $id )
1207 $id = intval( $id );
1209 # Not important enough to warrant an error page in case of failure
1210 $oldignore = wfIgnoreSQLErrors( true );
1212 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1214 if( (rand() %
23 != 0) or (wfLastErrno() != 0) ){
1215 # Most of the time (or on SQL errors), skip row count check
1216 wfIgnoreSQLErrors( $oldignore );
1220 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1221 $row = wfFetchObject( $res );
1222 $rown = intval( $row->n
);
1223 if( $rown > 1000 ){ // update counters after ~1000 hits
1224 wfProfileIn( "Article::incViewCount-collect" );
1225 $old_user_abort = ignore_user_abort( true );
1227 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1228 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1229 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1230 "GROUP BY hc_id", DB_WRITE
);
1231 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1232 wfQuery("UNLOCK TABLES", DB_WRITE
);
1233 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1234 "WHERE cur_id = hc_id", DB_WRITE
);
1235 wfQuery("DROP TABLE acchits", DB_WRITE
);
1237 ignore_user_abort( $old_user_abort );
1238 wfProfileOut( "Article::incViewCount-collect" );
1240 wfIgnoreSQLErrors( $oldignore );
1244 function wfReplaceSubstVar( $matches ) {
1245 return wfMsg( $matches[1] );