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";
125 # Pre-fill content with error message so that if something
126 # fails we'll have something telling us what we intended.
128 $t = $this->mTitle
->getPrefixedText();
129 if ( isset( $oldid ) ) {
130 $oldid = IntVal( $oldid );
131 $t .= ",oldid={$oldid}";
133 if ( isset( $redirect ) ) {
134 $redirect = ($redirect == "no") ?
"no" : "yes";
135 $t .= ",redirect={$redirect}";
137 $this->mContent
= wfMsg( "missingarticle", $t );
139 if ( ! $oldid ) { # Retrieve current version
140 $id = $this->getID();
141 if ( 0 == $id ) return;
144 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
145 "FROM cur WHERE cur_id={$id}";
147 $res = wfQuery( $sql, DB_READ
, $fname );
148 if ( 0 == wfNumRows( $res ) ) {
152 $s = wfFetchObject( $res );
153 # If we got a redirect, follow it (unless we've been told
154 # not to by either the function parameter or the query
155 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
156 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
157 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
158 $s->cur_text
, $m ) ) {
159 $rt = Title
::newFromText( $m[1] );
161 # Gotta hand redirects to special pages differently:
162 # Fill the HTTP response "Location" header and ignore
163 # the rest of the page we're on.
165 if ( $rt->getInterwiki() != "" ) {
166 $wgOut->redirect( $rt->getFullURL() ) ;
169 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
170 $wgOut->redirect( $rt->getFullURL() );
173 $rid = $rt->getArticleID();
175 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
176 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
177 $res = wfQuery( $sql, DB_READ
, $fname );
179 if ( 0 != wfNumRows( $res ) ) {
180 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
182 $s = wfFetchObject( $res );
189 $this->mContent
= $s->cur_text
;
190 $this->mUser
= $s->cur_user
;
191 $this->mCounter
= $s->cur_counter
;
192 $this->mTimestamp
= $s->cur_timestamp
;
193 $this->mTouched
= $s->cur_touched
;
194 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
195 $this->mTitle
->mRestrictionsLoaded
= true;
196 wfFreeResult( $res );
197 } else { # oldid set, retrieve historical version
198 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
199 "WHERE old_id={$oldid}";
200 $res = wfQuery( $sql, DB_READ
, $fname );
201 if ( 0 == wfNumRows( $res ) ) { return; }
203 $s = wfFetchObject( $res );
204 $this->mContent
= Article
::getRevisionText( $s );
205 $this->mUser
= $s->old_user
;
207 $this->mTimestamp
= $s->old_timestamp
;
208 wfFreeResult( $res );
210 $this->mContentLoaded
= true;
214 if( $this->mTitle
) {
215 return $this->mTitle
->getArticleID();
223 if ( -1 == $this->mCounter
) {
224 $id = $this->getID();
225 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
227 return $this->mCounter
;
230 # Would the given text make this article a "good" article (i.e.,
231 # suitable for including in the article count)?
233 function isCountable( $text )
235 global $wgUseCommaCount, $wgMwRedir;
237 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
238 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
239 $token = ($wgUseCommaCount ?
"," : "[[" );
240 if ( false === strstr( $text, $token ) ) { return 0; }
244 # Loads everything from cur except cur_text
245 # This isn't necessary for all uses, so it's only done if needed.
247 /* private */ function loadLastEdit()
250 if ( -1 != $this->mUser
) return;
252 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
253 "cur_comment,cur_minor_edit FROM cur WHERE " .
254 "cur_id=" . $this->getID();
255 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
257 if ( wfNumRows( $res ) > 0 ) {
258 $s = wfFetchObject( $res );
259 $this->mUser
= $s->cur_user
;
260 $this->mUserText
= $s->cur_user_text
;
261 $this->mTimestamp
= $s->cur_timestamp
;
262 $this->mComment
= $s->cur_comment
;
263 $this->mMinorEdit
= $s->cur_minor_edit
;
267 function getTimestamp()
269 $this->loadLastEdit();
270 return $this->mTimestamp
;
275 $this->loadLastEdit();
279 function getUserText()
281 $this->loadLastEdit();
282 return $this->mUserText
;
285 function getComment()
287 $this->loadLastEdit();
288 return $this->mComment
;
291 function getMinorEdit()
293 $this->loadLastEdit();
294 return $this->mMinorEdit
;
297 # This is the default action of the script: just view the page of
302 global $wgUser, $wgOut, $wgLang;
303 global $oldid, $diff; # From query
304 global $wgLinkCache, $IP, $wgEnableParserCache;
306 $fname = "Article::view";
307 wfProfileIn( $fname );
309 $wgOut->setArticleFlag( true );
310 $wgOut->setRobotpolicy( "index,follow" );
312 # If we got diff and oldid in the query, we want to see a
313 # diff page instead of the article.
315 if ( isset( $diff ) ) {
316 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
317 $de = new DifferenceEngine( $oldid, $diff );
319 wfProfileOut( $fname );
323 if ( !isset( $oldid ) and $this->checkTouched() ) {
324 if( $wgOut->checkLastModified( $this->mTouched
) ){
326 } else if ( $this->tryFileCache() ) {
327 # tell wgOut that output is taken care of
329 $this->viewUpdates();
334 $text = $this->getContent(); # May change mTitle
335 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
336 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
337 " - " . wfMsg( "wikititlesuffix" ) );
339 # We're looking at an old revision
342 $this->setOldSubtitle();
343 $wgOut->setRobotpolicy( "noindex,follow" );
345 if ( "" != $this->mRedirectedFrom
) {
346 $sk = $wgUser->getSkin();
347 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
349 $s = wfMsg( "redirectedfrom", $redir );
350 $wgOut->setSubtitle( $s );
353 $wgLinkCache->preFill( $this->mTitle
);
355 if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
356 $wgOut->addWikiText( $text, true, $this );
358 $wgOut->addWikiText( $text );
361 # Add link titles as META keywords
362 $wgOut->addMetaTags() ;
364 $this->viewUpdates();
365 wfProfileOut( $fname );
368 # Theoretically we could defer these whole insert and update
369 # functions for after display, but that's taking a big leap
370 # of faith, and we want to be able to report database
371 # errors at some point.
373 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
375 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
376 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
378 $fname = "Article::insertNewArticle";
380 $this->mCountAdjustment
= $this->isCountable( $text );
382 $ns = $this->mTitle
->getNamespace();
383 $ttl = $this->mTitle
->getDBkey();
384 $text = $this->preSaveTransform( $text );
385 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
388 $now = wfTimestampNow();
389 $won = wfInvertTimestamp( $now );
391 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
392 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
393 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
394 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
395 "cur_restrictions,cur_user_text,cur_is_redirect," .
396 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
397 wfStrencode( $text ) . "', '" .
398 wfStrencode( $summary ) . "', '" .
399 $wgUser->getID() . "', '{$now}', " .
400 $isminor . ", 0, '', '" .
401 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
402 $res = wfQuery( $sql, DB_WRITE
, $fname );
404 $newid = wfInsertId();
405 $this->mTitle
->resetArticleID( $newid );
407 Article
::onArticleCreate( $this->mTitle
);
408 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
411 if(!$this->mTitle
->userIsWatching()) $this->watch();
413 if ( $this->mTitle
->userIsWatching() ) {
418 # The talk page isn't in the regular link tables, so we need to update manually:
419 $talkns = $ns ^
1; # talk -> normal; normal -> talk
420 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
421 wfQuery( $sql, DB_WRITE
);
423 # standard deferred updates
424 $this->editUpdates( $text );
426 $this->showArticle( $text, wfMsg( "newarticle" ) );
430 /* Side effects: loads last edit */
431 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
432 $this->loadLastEdit();
433 $oldtext = $this->getContent();
434 if ($section != "") {
435 if($section=="new") {
436 if($summary) $subject="== {$summary} ==\n\n";
437 $text=$oldtext."\n\n".$subject.$text;
439 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
440 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
441 $secs[$section*2]=$text."\n\n"; // replace with edited
442 if($section) { $secs[$section*2-1]=""; } // erase old headline
443 $text=join("",$secs);
449 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false )
451 global $wgOut, $wgUser, $wgLinkCache;
452 global $wgDBtransactions, $wgMwRedir;
453 global $wgUseSquid, $wgInternalServer;
454 $fname = "Article::updateArticle";
456 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
457 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
458 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
460 $text = $m[1] . "\n"; # Remove all content but redirect
464 $text = $this->preSaveTransform( $text );
466 # Update article, but only if changed.
468 if( $wgDBtransactions ) {
470 wfQuery( $sql, DB_WRITE
);
472 $oldtext = $this->getContent( true );
474 if ( 0 != strcmp( $text, $oldtext ) ) {
475 $this->mCountAdjustment
= $this->isCountable( $text )
476 - $this->isCountable( $oldtext );
478 $now = wfTimestampNow();
479 $won = wfInvertTimestamp( $now );
480 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
481 "',cur_comment='" . wfStrencode( $summary ) .
482 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
483 ",cur_timestamp='{$now}',cur_user_text='" .
484 wfStrencode( $wgUser->getName() ) .
485 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
486 "WHERE cur_id=" . $this->getID() .
487 " AND cur_timestamp='" . $this->getTimestamp() . "'";
488 $res = wfQuery( $sql, DB_WRITE
, $fname );
490 if( wfAffectedRows() == 0 ) {
491 /* Belated edit conflict! Run away!! */
495 # This overwrites $oldtext if revision compression is on
496 $flags = Article
::compressRevisionText( $oldtext );
498 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
499 "old_comment,old_user,old_user_text,old_timestamp," .
500 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
501 $this->mTitle
->getNamespace() . ", '" .
502 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
503 wfStrencode( $oldtext ) . "', '" .
504 wfStrencode( $this->getComment() ) . "', " .
505 $this->getUser() . ", '" .
506 wfStrencode( $this->getUserText() ) . "', '" .
507 $this->getTimestamp() . "', " . $me1 . ", '" .
508 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
509 $res = wfQuery( $sql, DB_WRITE
, $fname );
510 $oldid = wfInsertID( $res );
512 $bot = (int)($wgUser->isBot() ||
$forceBot);
513 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
514 $oldid, $this->getTimestamp(), $bot );
515 Article
::onArticleEdit( $this->mTitle
);
518 if( $wgDBtransactions ) {
520 wfQuery( $sql, DB_WRITE
);
524 if (!$this->mTitle
->userIsWatching()) $this->watch();
526 if ( $this->mTitle
->userIsWatching() ) {
530 # standard deferred updates
531 $this->editUpdates( $text );
536 # Purge all articles linking here
537 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
538 $titles = $this->mTitle
->getLinksTo();
539 Title
::touchArray( $titles );
541 foreach ( $titles as $title ) {
542 $urls[] = $title->getInternalURL();
549 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
550 $u = new SquidUpdate( $urls );
554 $this->showArticle( $text, wfMsg( "updated" ) );
558 # After we've either updated or inserted the article, update
559 # the link tables and redirect to the new page.
561 function showArticle( $text, $subtitle )
563 global $wgOut, $wgUser, $wgLinkCache;
566 $wgLinkCache = new LinkCache();
568 # Get old version of link table to allow incremental link updates
569 $wgLinkCache->preFill( $this->mTitle
);
570 $wgLinkCache->clear();
572 # Now update the link cache by parsing the text
573 $wgOut = new OutputPage();
574 $wgOut->addWikiText( $text );
576 if( $wgMwRedir->matchStart( $text ) )
580 $wgOut->redirect( $this->mTitle
->getFullURL( $r ) );
583 # Add this page to my watchlist
585 function watch( $add = true )
587 global $wgUser, $wgOut, $wgLang;
588 global $wgDeferredUpdateList;
590 if ( 0 == $wgUser->getID() ) {
591 $wgOut->errorpage( "watchnologin", "watchnologintext" );
594 if ( wfReadOnly() ) {
595 $wgOut->readOnlyPage();
599 $wgUser->addWatch( $this->mTitle
);
601 $wgUser->removeWatch( $this->mTitle
);
603 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
604 $wgOut->setRobotpolicy( "noindex,follow" );
606 $sk = $wgUser->getSkin() ;
607 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
610 $text = wfMsg( "addedwatchtext", $link );
612 $text = wfMsg( "removedwatchtext", $link );
613 $wgOut->addHTML( $text );
615 $up = new UserUpdate();
616 array_push( $wgDeferredUpdateList, $up );
618 $wgOut->returnToMain( false );
623 $this->watch( false );
626 function protect( $limit = "sysop" )
628 global $wgUser, $wgOut;
630 if ( ! $wgUser->isSysop() ) {
631 $wgOut->sysopRequired();
634 if ( wfReadOnly() ) {
635 $wgOut->readOnlyPage();
638 $id = $this->mTitle
->getArticleID();
640 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
643 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
644 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
645 wfQuery( $sql, DB_WRITE
, "Article::protect" );
647 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
648 if ( $limit === "" ) {
649 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
651 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
653 $wgOut->redirect( $this->mTitle
->getFullURL() );
658 return $this->protect( "" );
663 global $wgUser, $wgOut, $wgMessageCache;
664 global $wpConfirm, $wpReason, $image, $oldimage;
665 $fname = "Article::delete";
667 # This code desperately needs to be totally rewritten
669 if ( ( ! $wgUser->isSysop() ) ) {
670 $wgOut->sysopRequired();
673 if ( wfReadOnly() ) {
674 $wgOut->readOnlyPage();
678 # Can't delete MediaWiki namespace
679 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
680 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
684 # Better double-check that it hasn't been deleted yet!
685 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
686 if ( ( "" == trim( $this->mTitle
->getText() ) )
687 or ( $this->mTitle
->getArticleId() == 0 ) ) {
688 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
692 if ( @$_POST["wpConfirm"] ) {
697 # determine whether this page has earlier revisions
698 # and insert a warning if it does
699 # we select the text because it might be useful below
700 $ns = $this->mTitle
->getNamespace();
701 $title = $this->mTitle
->getDBkey();
702 $etitle = wfStrencode( $title );
703 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
704 $res = wfQuery( $sql, DB_READ
, $fname );
705 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
706 $skin=$wgUser->getSkin();
707 $wgOut->addHTML("<B>".wfMsg("historywarning"));
708 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
711 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
712 $res=wfQuery($sql, DB_READ
, $fname);
713 if( ($s=wfFetchObject($res))) {
715 # if this is a mini-text, we can paste part of it into the deletion reason
717 #if this is empty, an earlier revision may contain "useful" text
719 if($s->cur_text
!="") {
723 $text = Article
::getRevisionText( $old );
729 $length=strlen($text);
731 # this should not happen, since it is not possible to store an empty, new
732 # page. Let's insert a standard text in case it does, though
733 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
736 if($length < 500 && !$wpReason) {
738 # comment field=255, let's grep the first 150 to have some user
740 $text=substr($text,0,150);
741 # let's strip out newlines and HTML tags
742 $text=preg_replace("/\"/","'",$text);
743 $text=preg_replace("/\</","<",$text);
744 $text=preg_replace("/\>/",">",$text);
745 $text=preg_replace("/[\n\r]/","",$text);
747 $wpReason=wfMsg("excontent"). " '".$text;
749 $wpReason=wfMsg("exbeforeblank") . " '".$text;
751 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
756 return $this->confirmDelete();
759 function confirmDelete( $par = "" )
764 wfDebug( "Article::confirmDelete\n" );
766 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
767 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
768 $wgOut->setRobotpolicy( "noindex,nofollow" );
769 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
771 $formaction = $this->mTitle
->escapeLocalURL( "action=delete" . $par );
773 $confirm = wfMsg( "confirm" );
774 $check = wfMsg( "confirmcheck" );
775 $delcom = wfMsg( "deletecomment" );
778 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
779 <table border=0><tr><td align=right>
780 {$delcom}:</td><td align=left>
781 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
782 </td></tr><tr><td> </td></tr>
784 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
785 </td><td><label for=\"wpConfirm\">{$check}</label></td>
786 </tr><tr><td> </td><td>
787 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
788 </td></tr></table></form>\n" );
790 $wgOut->returnToMain( false );
795 global $wgOut, $wgUser, $wgLang;
797 $fname = "Article::doDelete";
798 wfDebug( "$fname\n" );
800 if ( $this->doDeleteArticle() ) {
801 $deleted = $this->mTitle
->getPrefixedText();
803 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
804 $wgOut->setRobotpolicy( "noindex,nofollow" );
806 $sk = $wgUser->getSkin();
807 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
808 Namespace::getWikipedia() ) .
809 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
811 $text = wfMsg( "deletedtext", $deleted, $loglink );
813 $wgOut->addHTML( "<p>" . $text );
814 $wgOut->returnToMain( false );
816 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
820 # Delete the article, returns success
821 function doDeleteArticle()
823 global $wgUser, $wgLang, $wgRequest;
824 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
826 $fname = "Article::doDeleteArticle";
827 wfDebug( "$fname\n" );
829 $ns = $this->mTitle
->getNamespace();
830 $t = wfStrencode( $this->mTitle
->getDBkey() );
831 $id = $this->mTitle
->getArticleID();
833 if ( "" == $t ||
$id == 0 ) {
837 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
838 array_push( $wgDeferredUpdateList, $u );
840 $linksTo = $this->mTitle
->getLinksTo();
845 $this->mTitle
->getInternalURL(),
846 $this->mTitle
->getInternalURL( "history" )
848 foreach ( $linksTo as $linkTo ) {
849 $urls[] = $linkTo->getInternalURL();
852 $u = new SquidUpdate( $urls );
853 array_push( $wgDeferredUpdateList, $u );
857 # Client and file cache invalidation
858 Title
::touchArray( $linksTo );
860 # Move article and history to the "archive" table
861 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
862 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
863 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
864 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
865 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
866 wfQuery( $sql, DB_WRITE
, $fname );
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 old_namespace,old_title,old_text,old_comment," .
871 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
872 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
873 wfQuery( $sql, DB_WRITE
, $fname );
875 # Now that it's safely backed up, delete it
877 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
879 wfQuery( $sql, DB_WRITE
, $fname );
881 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
883 wfQuery( $sql, DB_WRITE
, $fname );
885 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
887 wfQuery( $sql, DB_WRITE
, $fname );
889 # Finally, clean up the link tables
890 $t = wfStrencode( $this->mTitle
->getPrefixedDBkey() );
892 Article
::onArticleDelete( $this->mTitle
);
894 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
897 foreach ( $linksTo as $titleObj ) {
898 if ( ! $first ) { $sql .= ","; }
900 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
901 $linkID = $titleObj->getArticleID();
902 $sql .= "({$linkID},'{$t}')";
905 wfQuery( $sql, DB_WRITE
, $fname );
908 $sql = "DELETE FROM links WHERE l_to={$id}";
909 wfQuery( $sql, DB_WRITE
, $fname );
911 $sql = "DELETE FROM links WHERE l_from={$id}";
912 wfQuery( $sql, DB_WRITE
, $fname );
914 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
915 wfQuery( $sql, DB_WRITE
, $fname );
917 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
918 wfQuery( $sql, DB_WRITE
, $fname );
920 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
921 $art = $this->mTitle
->getPrefixedText();
922 $wpReason = $wgRequest->getText( "wpReason" );
923 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
925 # Clear the cached article id so the interface doesn't act like we exist
926 $this->mTitle
->resetArticleID( 0 );
927 $this->mTitle
->mArticleID
= 0;
933 global $wgUser, $wgLang, $wgOut, $wgRequest;
935 if ( ! $wgUser->isSysop() ) {
936 $wgOut->sysopRequired();
939 if ( wfReadOnly() ) {
940 $wgOut->readOnlyPage( $this->getContent() );
944 # Enhanced rollback, marks edits rc_bot=1
945 $bot = $wgRequest->getBool( 'bot' );
947 # Replace all this user's current edits with the next one down
948 $tt = wfStrencode( $this->mTitle
->getDBKey() );
949 $n = $this->mTitle
->getNamespace();
951 # Get the last editor
952 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
953 $res = wfQuery( $sql, DB_READ
);
954 if( ($x = wfNumRows( $res )) != 1 ) {
956 $wgOut->addHTML( wfMsg( "notanarticle" ) );
959 $s = wfFetchObject( $res );
960 $ut = wfStrencode( $s->cur_user_text
);
964 $from = str_replace( '_', ' ', $wgRequest->getVal( "from" ) );
965 if( $from != $s->cur_user_text
) {
966 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
967 $wgOut->addWikiText( wfMsg( "alreadyrolled",
968 htmlspecialchars( $this->mTitle
->getPrefixedText()),
969 htmlspecialchars( $from ),
970 htmlspecialchars( $s->cur_user_text
) ) );
971 if($s->cur_comment
!= "") {
974 htmlspecialchars( $s->cur_comment
) ) );
979 # Get the last edit not by this guy
980 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
981 FROM old USE INDEX (name_title_timestamp)
982 WHERE old_namespace={$n} AND old_title='{$tt}'
983 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
984 ORDER BY inverse_timestamp LIMIT 1";
985 $res = wfQuery( $sql, DB_READ
);
986 if( wfNumRows( $res ) != 1 ) {
988 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
989 $wgOut->addHTML( wfMsg( "cantrollback" ) );
992 $s = wfFetchObject( $res );
995 # Mark all reverted edits as bot
996 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
997 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
998 wfQuery( $sql, DB_WRITE
, $fname );
1002 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
1003 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1004 $wgOut->setRobotpolicy( "noindex,nofollow" );
1005 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1006 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1007 Article
::onArticleEdit( $this->mTitle
);
1008 $wgOut->returnToMain( false );
1012 # Do standard deferred updates after page view
1014 /* private */ function viewUpdates()
1016 global $wgDeferredUpdateList;
1017 if ( 0 != $this->getID() ) {
1018 global $wgDisableCounters;
1019 if( !$wgDisableCounters ) {
1020 Article
::incViewCount( $this->getID() );
1021 $u = new SiteStatsUpdate( 1, 0, 0 );
1022 array_push( $wgDeferredUpdateList, $u );
1024 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1025 $this->mTitle
->getDBkey() );
1026 array_push( $wgDeferredUpdateList, $u );
1030 # Do standard deferred updates after page edit.
1031 # Every 1000th edit, prune the recent changes table.
1033 /* private */ function editUpdates( $text )
1035 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1036 global $wgMessageCache;
1039 if ( 0 == mt_rand( 0, 999 ) ) {
1040 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1041 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1042 wfQuery( $sql, DB_WRITE
);
1044 $id = $this->getID();
1045 $title = $this->mTitle
->getPrefixedDBkey();
1046 $shortTitle = $this->mTitle
->getDBkey();
1048 $adj = $this->mCountAdjustment
;
1051 $u = new LinksUpdate( $id, $title );
1052 array_push( $wgDeferredUpdateList, $u );
1053 $u = new SiteStatsUpdate( 0, 1, $adj );
1054 array_push( $wgDeferredUpdateList, $u );
1055 $u = new SearchUpdate( $id, $title, $text );
1056 array_push( $wgDeferredUpdateList, $u );
1058 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1059 array_push( $wgDeferredUpdateList, $u );
1061 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1062 $wgMessageCache->replace( $shortTitle, $text );
1067 /* private */ function setOldSubtitle()
1069 global $wgLang, $wgOut;
1071 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1072 $r = wfMsg( "revisionasof", $td );
1073 $wgOut->setSubtitle( "({$r})" );
1076 # This function is called right before saving the wikitext,
1077 # so we can do things like signatures and links-in-context.
1079 function preSaveTransform( $text )
1081 global $wgParser, $wgUser;
1082 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1085 /* Caching functions */
1087 # checkLastModified returns true if it has taken care of all
1088 # output to the client that is necessary for this request.
1089 # (that is, it has sent a cached version of the page)
1090 function tryFileCache() {
1091 static $called = false;
1093 wfDebug( " tryFileCache() -- called twice!?\n" );
1097 if($this->isFileCacheable()) {
1098 $touched = $this->mTouched
;
1099 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1100 # Expire the main page quicker
1101 $expire = wfUnix2Timestamp( time() - 3600 );
1102 $touched = max( $expire, $touched );
1104 $cache = new CacheManager( $this->mTitle
);
1105 if($cache->isFileCacheGood( $touched )) {
1107 wfDebug( " tryFileCache() - about to load\n" );
1108 $cache->loadFromFileCache();
1111 wfDebug( " tryFileCache() - starting buffer\n" );
1112 ob_start( array(&$cache, 'saveToFileCache' ) );
1115 wfDebug( " tryFileCache() - not cacheable\n" );
1119 function isFileCacheable() {
1120 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1121 global $action, $oldid, $diff, $redirect, $printable;
1122 return $wgUseFileCache
1123 and (!$wgShowIPinHeader)
1124 and ($this->getID() != 0)
1125 and ($wgUser->getId() == 0)
1126 and (!$wgUser->getNewtalk())
1127 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1128 and ($action == "view")
1129 and (!isset($oldid))
1131 and (!isset($redirect))
1132 and (!isset($printable))
1133 and (!$this->mRedirectedFrom
);
1136 function checkTouched() {
1137 $id = $this->getID();
1138 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1139 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1140 if( $s = wfFetchObject( $res ) ) {
1141 $this->mTouched
= $s->cur_touched
;
1142 return !$s->cur_is_redirect
;
1148 /* static */ function incViewCount( $id )
1150 $id = intval( $id );
1151 global $wgHitcounterUpdateFreq;
1153 if( $wgHitcounterUpdateFreq <= 1 ){ //
1154 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1155 "WHERE cur_id = $id", DB_WRITE
);
1159 # Not important enough to warrant an error page in case of failure
1160 $oldignore = wfIgnoreSQLErrors( true );
1162 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1164 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1165 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1166 # Most of the time (or on SQL errors), skip row count check
1167 wfIgnoreSQLErrors( $oldignore );
1171 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1172 $row = wfFetchObject( $res );
1173 $rown = intval( $row->n
);
1174 if( $rown >= $wgHitcounterUpdateFreq ){
1175 wfProfileIn( "Article::incViewCount-collect" );
1176 $old_user_abort = ignore_user_abort( true );
1178 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1179 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1180 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1181 "GROUP BY hc_id", DB_WRITE
);
1182 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1183 wfQuery("UNLOCK TABLES", DB_WRITE
);
1184 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1185 "WHERE cur_id = hc_id", DB_WRITE
);
1186 wfQuery("DROP TABLE acchits", DB_WRITE
);
1188 ignore_user_abort( $old_user_abort );
1189 wfProfileOut( "Article::incViewCount-collect" );
1191 wfIgnoreSQLErrors( $oldignore );
1194 # The onArticle*() functions are supposed to be a kind of hooks
1195 # which should be called whenever any of the specified actions
1198 # This is a good place to put code to clear caches, for instance.
1200 # This is called on page move and undelete, as well as edit
1201 /* static */ function onArticleCreate($title_obj){
1202 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
1204 $titles = $title_obj->getBrokenLinksTo();
1207 if ( $wgUseSquid ) {
1208 $urls = $title_obj->getSquidURLs();
1209 foreach ( $titles as $linkTitle ) {
1210 $urls[] = $linkTitle->getInternalURL();
1212 $u = new SquidUpdate( $urls );
1213 array_push( $wgDeferredUpdateList, $u );
1216 # Clear persistent link cache
1217 if ( $wgEnablePersistentLC ) {
1218 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1221 # Clear parser cache (not really used)
1222 if ( $wgEnableParserCache ) {
1223 OutputPage
::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1227 /* static */ function onArticleDelete($title_obj){
1228 global $wgEnablePersistentLC, $wgEnableParserCache;
1229 if ( $wgEnablePersistentLC ) {
1230 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1232 if ( $wgEnableParserCache ) {
1233 OutputPage
::parsercacheClearLinksTo( $title_obj->getArticleID() );
1237 /* static */ function onArticleEdit($title_obj){
1238 global $wgEnablePersistentLC, $wgEnableParserCache;
1239 if ( $wgEnablePersistentLC ) {
1240 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1242 if ( $wgEnableParserCache ) {
1243 OutputPage
::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );