435c19640dfa80c91e5a83f1f8cbf29f0c17b75a
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.
8 include_once( "CacheManager.php" );
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, $mTitle;
16 /* private */ var $mId, $mTable;
18 function Article( &$title ) {
19 $this->mTitle
=& $title;
23 /* private */ function clear()
25 $this->mContentLoaded
= false;
26 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
27 $this->mRedirectedFrom
= $this->mUserText
=
28 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
29 $this->mCountAdjustment
= 0;
30 $this->mTouched
= "19700101000000";
33 /* static */ function getRevisionText( $row, $prefix = "old_" ) {
34 # Deal with optional compression of archived pages.
35 # This can be done periodically via maintenance/compressOld.php, and
36 # as pages are saved if $wgCompressRevisions is set.
37 $text = $prefix . "text";
38 $flags = $prefix . "flags";
39 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, "gzip" ) ) ) {
40 return gzinflate( $row->$text );
42 if( isset( $row->$text ) ) {
48 /* static */ function compressRevisionText( &$text ) {
49 global $wgCompressRevisions;
50 if( !$wgCompressRevisions ) {
53 if( !function_exists( "gzdeflate" ) ) {
54 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
57 $text = gzdeflate( $text );
61 # Note that getContent/loadContent may follow redirects if
62 # not told otherwise, and so may cause a change to mTitle.
64 # Return the text of this revision
65 function getContent( $noredir = false )
67 global $action,$section,$count; # From query string
68 $fname = "Article::getContent";
69 wfProfileIn( $fname );
71 if ( 0 == $this->getID() ) {
72 if ( "edit" == $action ) {
73 wfProfileOut( $fname );
74 return ""; # was "newarticletext", now moved above the box)
76 wfProfileOut( $fname );
77 return wfMsg( "noarticletext" );
79 $this->loadContent( $noredir );
82 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
83 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
84 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
88 wfProfileOut( $fname );
89 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
94 wfProfileOut( $fname );
98 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
100 PREG_SPLIT_DELIM_CAPTURE
);
102 wfProfileOut( $fname );
103 return trim($secs[0]);
105 wfProfileOut( $fname );
106 return trim($secs[$section*2-1] . $secs[$section*2]);
110 wfProfileOut( $fname );
111 return $this->mContent
;
116 # Load the revision (including cur_text) into this object
117 function loadContent( $noredir = false )
119 global $wgOut, $wgMwRedir;
120 global $oldid, $redirect; # From query
122 if ( $this->mContentLoaded
) return;
123 $fname = "Article::loadContent";
126 if ( ! $oldid ) { # Retrieve current version
127 $id = $this->getID();
128 if ( 0 == $id ) return;
131 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
132 "FROM cur WHERE cur_id={$id}";
134 $res = wfQuery( $sql, DB_READ
, $fname );
135 if ( 0 == wfNumRows( $res ) ) {
139 $s = wfFetchObject( $res );
140 # If we got a redirect, follow it (unless we've been told
141 # not to by either the function parameter or the query
142 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
143 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
144 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
145 $s->cur_text
, $m ) ) {
146 $rt = Title
::newFromText( $m[1] );
148 # Gotta hand redirects to special pages differently:
149 # Fill the HTTP response "Location" header and ignore
150 # the rest of the page we're on.
152 if ( $rt->getInterwiki() != "" ) {
153 $wgOut->redirect( $rt->getFullURL() ) ;
156 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
157 $wgOut->redirect( $rt->getFullURL() );
160 $rid = $rt->getArticleID();
162 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
163 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
164 $res = wfQuery( $sql, DB_READ
, $fname );
166 if ( 0 != wfNumRows( $res ) ) {
167 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
169 $s = wfFetchObject( $res );
176 $this->mContent
= $s->cur_text
;
177 $this->mUser
= $s->cur_user
;
178 $this->mCounter
= $s->cur_counter
;
179 $this->mTimestamp
= $s->cur_timestamp
;
180 $this->mTouched
= $s->cur_touched
;
181 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
182 $this->mTitle
->mRestrictionsLoaded
= true;
183 wfFreeResult( $res );
184 } else { # oldid set, retrieve historical version
185 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
186 "WHERE old_id={$oldid}";
187 $res = wfQuery( $sql, DB_READ
, $fname );
188 if ( 0 == wfNumRows( $res ) ) { return; }
190 $s = wfFetchObject( $res );
191 $this->mContent
= Article
::getRevisionText( $s );
192 $this->mUser
= $s->old_user
;
194 $this->mTimestamp
= $s->old_timestamp
;
195 wfFreeResult( $res );
198 # Return error message :P
199 # Horrible, confusing UI and data. I think this should return false on error -- TS
201 $t = $this->mTitle
->getPrefixedText();
202 if ( isset( $oldid ) ) {
203 $oldid = IntVal( $oldid );
204 $t .= ",oldid={$oldid}";
206 if ( isset( $redirect ) ) {
207 $redirect = ($redirect == "no") ?
"no" : "yes";
208 $t .= ",redirect={$redirect}";
210 $this->mContent
= wfMsg( "missingarticle", $t );
213 $this->mContentLoaded
= true;
217 if( $this->mTitle
) {
218 return $this->mTitle
->getArticleID();
226 if ( -1 == $this->mCounter
) {
227 $id = $this->getID();
228 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
230 return $this->mCounter
;
233 # Would the given text make this article a "good" article (i.e.,
234 # suitable for including in the article count)?
236 function isCountable( $text )
238 global $wgUseCommaCount, $wgMwRedir;
240 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
241 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
242 $token = ($wgUseCommaCount ?
"," : "[[" );
243 if ( false === strstr( $text, $token ) ) { return 0; }
247 # Loads everything from cur except cur_text
248 # This isn't necessary for all uses, so it's only done if needed.
250 /* private */ function loadLastEdit()
253 if ( -1 != $this->mUser
) return;
255 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
256 "cur_comment,cur_minor_edit FROM cur WHERE " .
257 "cur_id=" . $this->getID();
258 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
260 if ( wfNumRows( $res ) > 0 ) {
261 $s = wfFetchObject( $res );
262 $this->mUser
= $s->cur_user
;
263 $this->mUserText
= $s->cur_user_text
;
264 $this->mTimestamp
= $s->cur_timestamp
;
265 $this->mComment
= $s->cur_comment
;
266 $this->mMinorEdit
= $s->cur_minor_edit
;
270 function getTimestamp()
272 $this->loadLastEdit();
273 return $this->mTimestamp
;
278 $this->loadLastEdit();
282 function getUserText()
284 $this->loadLastEdit();
285 return $this->mUserText
;
288 function getComment()
290 $this->loadLastEdit();
291 return $this->mComment
;
294 function getMinorEdit()
296 $this->loadLastEdit();
297 return $this->mMinorEdit
;
300 # This is the default action of the script: just view the page of
305 global $wgUser, $wgOut, $wgLang;
306 global $oldid, $diff, $printable; # From query
307 global $wgLinkCache, $IP, $wgEnableParserCache;
309 $fname = "Article::view";
310 wfProfileIn( $fname );
312 $wgOut->setArticleFlag( true );
313 $wgOut->setRobotpolicy( "index,follow" );
315 # If we got diff and oldid in the query, we want to see a
316 # diff page instead of the article.
318 if ( isset( $diff ) ) {
319 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
320 $de = new DifferenceEngine( $oldid, $diff );
322 wfProfileOut( $fname );
326 if ( !isset( $oldid ) and $this->checkTouched() ) {
327 if( $wgOut->checkLastModified( $this->mTouched
) ){
329 } else if ( $this->tryFileCache() ) {
330 # tell wgOut that output is taken care of
332 $this->viewUpdates();
337 $text = $this->getContent(); # May change mTitle
338 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
339 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
340 " - " . wfMsg( "wikititlesuffix" ) );
342 # We're looking at an old revision
345 $this->setOldSubtitle();
346 $wgOut->setRobotpolicy( "noindex,follow" );
348 if ( "" != $this->mRedirectedFrom
) {
349 $sk = $wgUser->getSkin();
350 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
352 $s = wfMsg( "redirectedfrom", $redir );
353 $wgOut->setSubtitle( $s );
356 $wgLinkCache->preFill( $this->mTitle
);
358 if ( $printable == "yes" ) {
359 $wgOut->mParserOptions
->setPrintable( true );
362 if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
363 $wgOut->addWikiText( $text, true, $this );
365 $wgOut->addWikiText( $text );
368 # Add link titles as META keywords
369 $wgOut->addMetaTags() ;
371 $this->viewUpdates();
372 wfProfileOut( $fname );
375 # Theoretically we could defer these whole insert and update
376 # functions for after display, but that's taking a big leap
377 # of faith, and we want to be able to report database
378 # errors at some point.
380 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
382 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
383 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
385 $fname = "Article::insertNewArticle";
387 $this->mCountAdjustment
= $this->isCountable( $text );
389 $ns = $this->mTitle
->getNamespace();
390 $ttl = $this->mTitle
->getDBkey();
391 $text = $this->preSaveTransform( $text );
392 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
395 $now = wfTimestampNow();
396 $won = wfInvertTimestamp( $now );
398 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
399 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
400 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
401 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
402 "cur_restrictions,cur_user_text,cur_is_redirect," .
403 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
404 wfStrencode( $text ) . "', '" .
405 wfStrencode( $summary ) . "', '" .
406 $wgUser->getID() . "', '{$now}', " .
407 $isminor . ", 0, '', '" .
408 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
409 $res = wfQuery( $sql, DB_WRITE
, $fname );
411 $newid = wfInsertId();
412 $this->mTitle
->resetArticleID( $newid );
414 Article
::onArticleCreate( $this->mTitle
);
415 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
418 if(!$this->mTitle
->userIsWatching()) $this->watch();
420 if ( $this->mTitle
->userIsWatching() ) {
425 # The talk page isn't in the regular link tables, so we need to update manually:
426 $talkns = $ns ^
1; # talk -> normal; normal -> talk
427 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
428 wfQuery( $sql, DB_WRITE
);
430 # standard deferred updates
431 $this->editUpdates( $text );
433 $this->showArticle( $text, wfMsg( "newarticle" ) );
437 /* Side effects: loads last edit */
438 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
439 $this->loadLastEdit();
440 $oldtext = $this->getContent();
441 if ($section != "") {
442 if($section=="new") {
443 if($summary) $subject="== {$summary} ==\n\n";
444 $text=$oldtext."\n\n".$subject.$text;
446 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
447 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
448 $secs[$section*2]=$text."\n\n"; // replace with edited
449 if($section) { $secs[$section*2-1]=""; } // erase old headline
450 $text=join("",$secs);
456 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false )
458 global $wgOut, $wgUser, $wgLinkCache;
459 global $wgDBtransactions, $wgMwRedir;
460 global $wgUseSquid, $wgInternalServer;
461 $fname = "Article::updateArticle";
463 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
464 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
465 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
467 $text = $m[1] . "\n"; # Remove all content but redirect
471 $text = $this->preSaveTransform( $text );
473 # Update article, but only if changed.
475 if( $wgDBtransactions ) {
477 wfQuery( $sql, DB_WRITE
);
479 $oldtext = $this->getContent( true );
481 if ( 0 != strcmp( $text, $oldtext ) ) {
482 $this->mCountAdjustment
= $this->isCountable( $text )
483 - $this->isCountable( $oldtext );
485 $now = wfTimestampNow();
486 $won = wfInvertTimestamp( $now );
487 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
488 "',cur_comment='" . wfStrencode( $summary ) .
489 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
490 ",cur_timestamp='{$now}',cur_user_text='" .
491 wfStrencode( $wgUser->getName() ) .
492 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
493 "WHERE cur_id=" . $this->getID() .
494 " AND cur_timestamp='" . $this->getTimestamp() . "'";
495 $res = wfQuery( $sql, DB_WRITE
, $fname );
497 if( wfAffectedRows() == 0 ) {
498 /* Belated edit conflict! Run away!! */
502 # This overwrites $oldtext if revision compression is on
503 $flags = Article
::compressRevisionText( $oldtext );
505 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
506 "old_comment,old_user,old_user_text,old_timestamp," .
507 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
508 $this->mTitle
->getNamespace() . ", '" .
509 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
510 wfStrencode( $oldtext ) . "', '" .
511 wfStrencode( $this->getComment() ) . "', " .
512 $this->getUser() . ", '" .
513 wfStrencode( $this->getUserText() ) . "', '" .
514 $this->getTimestamp() . "', " . $me1 . ", '" .
515 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
516 $res = wfQuery( $sql, DB_WRITE
, $fname );
517 $oldid = wfInsertID( $res );
519 $bot = (int)($wgUser->isBot() ||
$forceBot);
520 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
521 $oldid, $this->getTimestamp(), $bot );
522 Article
::onArticleEdit( $this->mTitle
);
525 if( $wgDBtransactions ) {
527 wfQuery( $sql, DB_WRITE
);
531 if (!$this->mTitle
->userIsWatching()) $this->watch();
533 if ( $this->mTitle
->userIsWatching() ) {
537 # standard deferred updates
538 $this->editUpdates( $text );
543 # Purge all articles linking here
544 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
545 $titles = $this->mTitle
->getLinksTo();
546 Title
::touchArray( $titles );
548 foreach ( $titles as $title ) {
549 $urls[] = $title->getInternalURL();
556 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
557 $u = new SquidUpdate( $urls );
561 $this->showArticle( $text, wfMsg( "updated" ) );
565 # After we've either updated or inserted the article, update
566 # the link tables and redirect to the new page.
568 function showArticle( $text, $subtitle )
570 global $wgOut, $wgUser, $wgLinkCache;
573 $wgLinkCache = new LinkCache();
575 # Get old version of link table to allow incremental link updates
576 $wgLinkCache->preFill( $this->mTitle
);
577 $wgLinkCache->clear();
579 # Now update the link cache by parsing the text
580 $wgOut = new OutputPage();
581 $wgOut->addWikiText( $text );
583 if( $wgMwRedir->matchStart( $text ) )
587 $wgOut->redirect( $this->mTitle
->getFullURL( $r ) );
590 # Add this page to my watchlist
592 function watch( $add = true )
594 global $wgUser, $wgOut, $wgLang;
595 global $wgDeferredUpdateList;
597 if ( 0 == $wgUser->getID() ) {
598 $wgOut->errorpage( "watchnologin", "watchnologintext" );
601 if ( wfReadOnly() ) {
602 $wgOut->readOnlyPage();
606 $wgUser->addWatch( $this->mTitle
);
608 $wgUser->removeWatch( $this->mTitle
);
610 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
611 $wgOut->setRobotpolicy( "noindex,follow" );
613 $sk = $wgUser->getSkin() ;
614 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
617 $text = wfMsg( "addedwatchtext", $link );
619 $text = wfMsg( "removedwatchtext", $link );
620 $wgOut->addHTML( $text );
622 $up = new UserUpdate();
623 array_push( $wgDeferredUpdateList, $up );
625 $wgOut->returnToMain( false );
630 $this->watch( false );
633 function protect( $limit = "sysop" )
635 global $wgUser, $wgOut;
637 if ( ! $wgUser->isSysop() ) {
638 $wgOut->sysopRequired();
641 if ( wfReadOnly() ) {
642 $wgOut->readOnlyPage();
645 $id = $this->mTitle
->getArticleID();
647 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
650 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
651 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
652 wfQuery( $sql, DB_WRITE
, "Article::protect" );
654 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
655 if ( $limit === "" ) {
656 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
658 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
660 $wgOut->redirect( $this->mTitle
->getFullURL() );
665 return $this->protect( "" );
670 global $wgUser, $wgOut, $wgMessageCache;
671 global $wpConfirm, $wpReason, $image, $oldimage;
672 $fname = "Article::delete";
674 # This code desperately needs to be totally rewritten
676 if ( ( ! $wgUser->isSysop() ) ) {
677 $wgOut->sysopRequired();
680 if ( wfReadOnly() ) {
681 $wgOut->readOnlyPage();
685 # Can't delete MediaWiki namespace
686 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
687 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
691 # Better double-check that it hasn't been deleted yet!
692 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
693 if ( ( "" == trim( $this->mTitle
->getText() ) )
694 or ( $this->mTitle
->getArticleId() == 0 ) ) {
695 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
699 if ( @$_POST["wpConfirm"] ) {
704 # determine whether this page has earlier revisions
705 # and insert a warning if it does
706 # we select the text because it might be useful below
707 $ns = $this->mTitle
->getNamespace();
708 $title = $this->mTitle
->getDBkey();
709 $etitle = wfStrencode( $title );
710 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
711 $res = wfQuery( $sql, DB_READ
, $fname );
712 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
713 $skin=$wgUser->getSkin();
714 $wgOut->addHTML("<B>".wfMsg("historywarning"));
715 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
718 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
719 $res=wfQuery($sql, DB_READ
, $fname);
720 if( ($s=wfFetchObject($res))) {
722 # if this is a mini-text, we can paste part of it into the deletion reason
724 #if this is empty, an earlier revision may contain "useful" text
726 if($s->cur_text
!="") {
730 $text = Article
::getRevisionText( $old );
736 $length=strlen($text);
738 # this should not happen, since it is not possible to store an empty, new
739 # page. Let's insert a standard text in case it does, though
740 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
743 if($length < 500 && !$wpReason) {
745 # comment field=255, let's grep the first 150 to have some user
747 $text=substr($text,0,150);
748 # let's strip out newlines and HTML tags
749 $text=preg_replace("/\"/","'",$text);
750 $text=preg_replace("/\</","<",$text);
751 $text=preg_replace("/\>/",">",$text);
752 $text=preg_replace("/[\n\r]/","",$text);
754 $wpReason=wfMsg("excontent"). " '".$text;
756 $wpReason=wfMsg("exbeforeblank") . " '".$text;
758 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
763 return $this->confirmDelete();
766 function confirmDelete( $par = "" )
771 wfDebug( "Article::confirmDelete\n" );
773 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
774 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
775 $wgOut->setRobotpolicy( "noindex,nofollow" );
776 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
778 $formaction = $this->mTitle
->escapeLocalURL( "action=delete" . $par );
780 $confirm = wfMsg( "confirm" );
781 $check = wfMsg( "confirmcheck" );
782 $delcom = wfMsg( "deletecomment" );
785 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
786 <table border=0><tr><td align=right>
787 {$delcom}:</td><td align=left>
788 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
789 </td></tr><tr><td> </td></tr>
791 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
792 </td><td><label for=\"wpConfirm\">{$check}</label></td>
793 </tr><tr><td> </td><td>
794 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
795 </td></tr></table></form>\n" );
797 $wgOut->returnToMain( false );
802 global $wgOut, $wgUser, $wgLang;
804 $fname = "Article::doDelete";
805 wfDebug( "$fname\n" );
807 if ( $this->doDeleteArticle() ) {
808 $deleted = $this->mTitle
->getPrefixedText();
810 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
811 $wgOut->setRobotpolicy( "noindex,nofollow" );
813 $sk = $wgUser->getSkin();
814 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
815 Namespace::getWikipedia() ) .
816 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
818 $text = wfMsg( "deletedtext", $deleted, $loglink );
820 $wgOut->addHTML( "<p>" . $text );
821 $wgOut->returnToMain( false );
823 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
827 # Delete the article, returns success
828 function doDeleteArticle()
830 global $wgUser, $wgLang, $wgRequest;
831 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
833 $fname = "Article::doDeleteArticle";
834 wfDebug( "$fname\n" );
836 $ns = $this->mTitle
->getNamespace();
837 $t = wfStrencode( $this->mTitle
->getDBkey() );
838 $id = $this->mTitle
->getArticleID();
840 if ( "" == $t ||
$id == 0 ) {
844 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
845 array_push( $wgDeferredUpdateList, $u );
847 $linksTo = $this->mTitle
->getLinksTo();
852 $this->mTitle
->getInternalURL(),
853 $this->mTitle
->getInternalURL( "history" )
855 foreach ( $linksTo as $linkTo ) {
856 $urls[] = $linkTo->getInternalURL();
859 $u = new SquidUpdate( $urls );
860 array_push( $wgDeferredUpdateList, $u );
864 # Client and file cache invalidation
865 Title
::touchArray( $linksTo );
867 # Move article and history to the "archive" table
868 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
869 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
870 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
871 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
872 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
873 wfQuery( $sql, DB_WRITE
, $fname );
875 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
876 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
877 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
878 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
879 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
880 wfQuery( $sql, DB_WRITE
, $fname );
882 # Now that it's safely backed up, delete it
884 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
886 wfQuery( $sql, DB_WRITE
, $fname );
888 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
890 wfQuery( $sql, DB_WRITE
, $fname );
892 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
894 wfQuery( $sql, DB_WRITE
, $fname );
896 # Finally, clean up the link tables
897 $t = wfStrencode( $this->mTitle
->getPrefixedDBkey() );
899 Article
::onArticleDelete( $this->mTitle
);
901 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
904 foreach ( $linksTo as $titleObj ) {
905 if ( ! $first ) { $sql .= ","; }
907 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
908 $linkID = $titleObj->getArticleID();
909 $sql .= "({$linkID},'{$t}')";
912 wfQuery( $sql, DB_WRITE
, $fname );
915 $sql = "DELETE FROM links WHERE l_to={$id}";
916 wfQuery( $sql, DB_WRITE
, $fname );
918 $sql = "DELETE FROM links WHERE l_from={$id}";
919 wfQuery( $sql, DB_WRITE
, $fname );
921 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
922 wfQuery( $sql, DB_WRITE
, $fname );
924 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
925 wfQuery( $sql, DB_WRITE
, $fname );
927 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
928 $art = $this->mTitle
->getPrefixedText();
929 $wpReason = $wgRequest->getText( "wpReason" );
930 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
932 # Clear the cached article id so the interface doesn't act like we exist
933 $this->mTitle
->resetArticleID( 0 );
934 $this->mTitle
->mArticleID
= 0;
940 global $wgUser, $wgLang, $wgOut, $wgRequest;
942 if ( ! $wgUser->isSysop() ) {
943 $wgOut->sysopRequired();
946 if ( wfReadOnly() ) {
947 $wgOut->readOnlyPage( $this->getContent() );
951 # Enhanced rollback, marks edits rc_bot=1
952 $bot = $wgRequest->getBool( 'bot' );
954 # Replace all this user's current edits with the next one down
955 $tt = wfStrencode( $this->mTitle
->getDBKey() );
956 $n = $this->mTitle
->getNamespace();
958 # Get the last editor
959 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
960 $res = wfQuery( $sql, DB_READ
);
961 if( ($x = wfNumRows( $res )) != 1 ) {
963 $wgOut->addHTML( wfMsg( "notanarticle" ) );
966 $s = wfFetchObject( $res );
967 $ut = wfStrencode( $s->cur_user_text
);
971 $from = str_replace( '_', ' ', $wgRequest->getVal( "from" ) );
972 if( $from != $s->cur_user_text
) {
973 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
974 $wgOut->addWikiText( wfMsg( "alreadyrolled",
975 htmlspecialchars( $this->mTitle
->getPrefixedText()),
976 htmlspecialchars( $from ),
977 htmlspecialchars( $s->cur_user_text
) ) );
978 if($s->cur_comment
!= "") {
981 htmlspecialchars( $s->cur_comment
) ) );
986 # Get the last edit not by this guy
987 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
988 FROM old USE INDEX (name_title_timestamp)
989 WHERE old_namespace={$n} AND old_title='{$tt}'
990 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
991 ORDER BY inverse_timestamp LIMIT 1";
992 $res = wfQuery( $sql, DB_READ
);
993 if( wfNumRows( $res ) != 1 ) {
995 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
996 $wgOut->addHTML( wfMsg( "cantrollback" ) );
999 $s = wfFetchObject( $res );
1002 # Mark all reverted edits as bot
1003 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1004 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1005 wfQuery( $sql, DB_WRITE
, $fname );
1009 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
1010 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1011 $wgOut->setRobotpolicy( "noindex,nofollow" );
1012 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1013 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1014 Article
::onArticleEdit( $this->mTitle
);
1015 $wgOut->returnToMain( false );
1019 # Do standard deferred updates after page view
1021 /* private */ function viewUpdates()
1023 global $wgDeferredUpdateList;
1024 if ( 0 != $this->getID() ) {
1025 global $wgDisableCounters;
1026 if( !$wgDisableCounters ) {
1027 Article
::incViewCount( $this->getID() );
1028 $u = new SiteStatsUpdate( 1, 0, 0 );
1029 array_push( $wgDeferredUpdateList, $u );
1031 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1032 $this->mTitle
->getDBkey() );
1033 array_push( $wgDeferredUpdateList, $u );
1037 # Do standard deferred updates after page edit.
1038 # Every 1000th edit, prune the recent changes table.
1040 /* private */ function editUpdates( $text )
1042 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1043 global $wgMessageCache;
1046 if ( 0 == mt_rand( 0, 999 ) ) {
1047 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1048 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1049 wfQuery( $sql, DB_WRITE
);
1051 $id = $this->getID();
1052 $title = $this->mTitle
->getPrefixedDBkey();
1053 $shortTitle = $this->mTitle
->getDBkey();
1055 $adj = $this->mCountAdjustment
;
1058 $u = new LinksUpdate( $id, $title );
1059 array_push( $wgDeferredUpdateList, $u );
1060 $u = new SiteStatsUpdate( 0, 1, $adj );
1061 array_push( $wgDeferredUpdateList, $u );
1062 $u = new SearchUpdate( $id, $title, $text );
1063 array_push( $wgDeferredUpdateList, $u );
1065 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1066 array_push( $wgDeferredUpdateList, $u );
1068 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1069 $wgMessageCache->replace( $shortTitle, $text );
1074 /* private */ function setOldSubtitle()
1076 global $wgLang, $wgOut;
1078 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1079 $r = wfMsg( "revisionasof", $td );
1080 $wgOut->setSubtitle( "({$r})" );
1083 # This function is called right before saving the wikitext,
1084 # so we can do things like signatures and links-in-context.
1086 function preSaveTransform( $text )
1088 global $wgParser, $wgUser;
1089 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1092 /* Caching functions */
1094 # checkLastModified returns true if it has taken care of all
1095 # output to the client that is necessary for this request.
1096 # (that is, it has sent a cached version of the page)
1097 function tryFileCache() {
1098 static $called = false;
1100 wfDebug( " tryFileCache() -- called twice!?\n" );
1104 if($this->isFileCacheable()) {
1105 $touched = $this->mTouched
;
1106 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1107 # Expire the main page quicker
1108 $expire = wfUnix2Timestamp( time() - 3600 );
1109 $touched = max( $expire, $touched );
1111 $cache = new CacheManager( $this->mTitle
);
1112 if($cache->isFileCacheGood( $touched )) {
1114 wfDebug( " tryFileCache() - about to load\n" );
1115 $cache->loadFromFileCache();
1118 wfDebug( " tryFileCache() - starting buffer\n" );
1119 ob_start( array(&$cache, 'saveToFileCache' ) );
1122 wfDebug( " tryFileCache() - not cacheable\n" );
1126 function isFileCacheable() {
1127 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1128 global $action, $oldid, $diff, $redirect, $printable;
1129 return $wgUseFileCache
1130 and (!$wgShowIPinHeader)
1131 and ($this->getID() != 0)
1132 and ($wgUser->getId() == 0)
1133 and (!$wgUser->getNewtalk())
1134 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1135 and ($action == "view")
1136 and (!isset($oldid))
1138 and (!isset($redirect))
1139 and (!isset($printable))
1140 and (!$this->mRedirectedFrom
);
1143 function checkTouched() {
1144 $id = $this->getID();
1145 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1146 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1147 if( $s = wfFetchObject( $res ) ) {
1148 $this->mTouched
= $s->cur_touched
;
1149 return !$s->cur_is_redirect
;
1155 /* static */ function incViewCount( $id )
1157 $id = intval( $id );
1158 global $wgHitcounterUpdateFreq;
1160 if( $wgHitcounterUpdateFreq <= 1 ){ //
1161 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1162 "WHERE cur_id = $id", DB_WRITE
);
1166 # Not important enough to warrant an error page in case of failure
1167 $oldignore = wfIgnoreSQLErrors( true );
1169 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1171 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1172 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1173 # Most of the time (or on SQL errors), skip row count check
1174 wfIgnoreSQLErrors( $oldignore );
1178 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1179 $row = wfFetchObject( $res );
1180 $rown = intval( $row->n
);
1181 if( $rown >= $wgHitcounterUpdateFreq ){
1182 wfProfileIn( "Article::incViewCount-collect" );
1183 $old_user_abort = ignore_user_abort( true );
1185 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1186 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1187 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1188 "GROUP BY hc_id", DB_WRITE
);
1189 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1190 wfQuery("UNLOCK TABLES", DB_WRITE
);
1191 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1192 "WHERE cur_id = hc_id", DB_WRITE
);
1193 wfQuery("DROP TABLE acchits", DB_WRITE
);
1195 ignore_user_abort( $old_user_abort );
1196 wfProfileOut( "Article::incViewCount-collect" );
1198 wfIgnoreSQLErrors( $oldignore );
1201 # The onArticle*() functions are supposed to be a kind of hooks
1202 # which should be called whenever any of the specified actions
1205 # This is a good place to put code to clear caches, for instance.
1207 # This is called on page move and undelete, as well as edit
1208 /* static */ function onArticleCreate($title_obj){
1209 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
1211 $titles = $title_obj->getBrokenLinksTo();
1214 if ( $wgUseSquid ) {
1215 $urls = $title_obj->getSquidURLs();
1216 foreach ( $titles as $linkTitle ) {
1217 $urls[] = $linkTitle->getInternalURL();
1219 $u = new SquidUpdate( $urls );
1220 array_push( $wgDeferredUpdateList, $u );
1223 # Clear persistent link cache
1224 if ( $wgEnablePersistentLC ) {
1225 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1228 # Clear parser cache (not really used)
1229 if ( $wgEnableParserCache ) {
1230 OutputPage
::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1234 /* static */ function onArticleDelete($title_obj){
1235 global $wgEnablePersistentLC, $wgEnableParserCache;
1236 if ( $wgEnablePersistentLC ) {
1237 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1239 if ( $wgEnableParserCache ) {
1240 OutputPage
::parsercacheClearLinksTo( $title_obj->getArticleID() );
1244 /* static */ function onArticleEdit($title_obj){
1245 global $wgEnablePersistentLC, $wgEnableParserCache;
1246 if ( $wgEnablePersistentLC ) {
1247 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1249 if ( $wgEnableParserCache ) {
1250 OutputPage
::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );