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( wfLocalUrl(
158 $rt->getPrefixedURL() ) );
161 $rid = $rt->getArticleID();
163 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
164 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
165 $res = wfQuery( $sql, DB_READ
, $fname );
167 if ( 0 != wfNumRows( $res ) ) {
168 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
170 $s = wfFetchObject( $res );
177 $this->mContent
= $s->cur_text
;
178 $this->mUser
= $s->cur_user
;
179 $this->mCounter
= $s->cur_counter
;
180 $this->mTimestamp
= $s->cur_timestamp
;
181 $this->mTouched
= $s->cur_touched
;
182 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
183 $this->mTitle
->mRestrictionsLoaded
= true;
184 wfFreeResult( $res );
185 } else { # oldid set, retrieve historical version
186 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
187 "WHERE old_id={$oldid}";
188 $res = wfQuery( $sql, DB_READ
, $fname );
189 if ( 0 == wfNumRows( $res ) ) { return; }
191 $s = wfFetchObject( $res );
192 $this->mContent
= Article
::getRevisionText( $s );
193 $this->mUser
= $s->old_user
;
195 $this->mTimestamp
= $s->old_timestamp
;
196 wfFreeResult( $res );
199 # Return error message :P
200 # Horrible, confusing UI and data. I think this should return false on error -- TS
202 $t = $this->mTitle
->getPrefixedText();
203 if ( isset( $oldid ) ) {
204 $oldid = IntVal( $oldid );
205 $t .= ",oldid={$oldid}";
207 if ( isset( $redirect ) ) {
208 $redirect = ($redirect == "no") ?
"no" : "yes";
209 $t .= ",redirect={$redirect}";
211 $this->mContent
= wfMsg( "missingarticle", $t );
214 $this->mContentLoaded
= true;
218 if( $this->mTitle
) {
219 return $this->mTitle
->getArticleID();
227 if ( -1 == $this->mCounter
) {
228 $id = $this->getID();
229 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
231 return $this->mCounter
;
234 # Would the given text make this article a "good" article (i.e.,
235 # suitable for including in the article count)?
237 function isCountable( $text )
239 global $wgUseCommaCount, $wgMwRedir;
241 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
242 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
243 $token = ($wgUseCommaCount ?
"," : "[[" );
244 if ( false === strstr( $text, $token ) ) { return 0; }
248 # Loads everything from cur except cur_text
249 # This isn't necessary for all uses, so it's only done if needed.
251 /* private */ function loadLastEdit()
254 if ( -1 != $this->mUser
) return;
256 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
257 "cur_comment,cur_minor_edit FROM cur WHERE " .
258 "cur_id=" . $this->getID();
259 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
261 if ( wfNumRows( $res ) > 0 ) {
262 $s = wfFetchObject( $res );
263 $this->mUser
= $s->cur_user
;
264 $this->mUserText
= $s->cur_user_text
;
265 $this->mTimestamp
= $s->cur_timestamp
;
266 $this->mComment
= $s->cur_comment
;
267 $this->mMinorEdit
= $s->cur_minor_edit
;
271 function getTimestamp()
273 $this->loadLastEdit();
274 return $this->mTimestamp
;
279 $this->loadLastEdit();
283 function getUserText()
285 $this->loadLastEdit();
286 return $this->mUserText
;
289 function getComment()
291 $this->loadLastEdit();
292 return $this->mComment
;
295 function getMinorEdit()
297 $this->loadLastEdit();
298 return $this->mMinorEdit
;
301 # This is the default action of the script: just view the page of
306 global $wgUser, $wgOut, $wgLang;
307 global $oldid, $diff; # From query
308 global $wgLinkCache, $IP, $wgEnableParserCache;
310 $fname = "Article::view";
311 wfProfileIn( $fname );
313 $wgOut->setArticleFlag( true );
314 $wgOut->setRobotpolicy( "index,follow" );
316 # If we got diff and oldid in the query, we want to see a
317 # diff page instead of the article.
319 if ( isset( $diff ) ) {
320 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
321 $de = new DifferenceEngine( $oldid, $diff );
323 wfProfileOut( $fname );
327 if ( !isset( $oldid ) and $this->checkTouched() ) {
328 if( $wgOut->checkLastModified( $this->mTouched
) ){
330 } else if ( $this->tryFileCache() ) {
331 # tell wgOut that output is taken care of
333 $this->viewUpdates();
338 $text = $this->getContent(); # May change mTitle
339 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
340 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
341 " - " . wfMsg( "wikititlesuffix" ) );
343 # We're looking at an old revision
346 $this->setOldSubtitle();
347 $wgOut->setRobotpolicy( "noindex,follow" );
349 if ( "" != $this->mRedirectedFrom
) {
350 $sk = $wgUser->getSkin();
351 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
353 $s = wfMsg( "redirectedfrom", $redir );
354 $wgOut->setSubtitle( $s );
357 $wgLinkCache->preFill( $this->mTitle
);
359 if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
360 $wgOut->addWikiText( $text, true, $this );
362 $wgOut->addWikiText( $text );
365 $this->viewUpdates();
366 wfProfileOut( $fname );
369 # Theoretically we could defer these whole insert and update
370 # functions for after display, but that's taking a big leap
371 # of faith, and we want to be able to report database
372 # errors at some point.
374 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
376 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
377 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
379 $fname = "Article::insertNewArticle";
381 $this->mCountAdjustment
= $this->isCountable( $text );
383 $ns = $this->mTitle
->getNamespace();
384 $ttl = $this->mTitle
->getDBkey();
385 $text = $this->preSaveTransform( $text );
386 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
389 $now = wfTimestampNow();
390 $won = wfInvertTimestamp( $now );
392 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
393 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
394 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
395 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
396 "cur_restrictions,cur_user_text,cur_is_redirect," .
397 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
398 wfStrencode( $text ) . "', '" .
399 wfStrencode( $summary ) . "', '" .
400 $wgUser->getID() . "', '{$now}', " .
401 $isminor . ", 0, '', '" .
402 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
403 $res = wfQuery( $sql, DB_WRITE
, $fname );
405 $newid = wfInsertId();
406 $this->mTitle
->resetArticleID( $newid );
408 Article
::onArticleCreate( $this->mTitle
);
409 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
412 if(!$this->mTitle
->userIsWatching()) $this->watch();
414 if ( $this->mTitle
->userIsWatching() ) {
419 # The talk page isn't in the regular link tables, so we need to update manually:
420 $talkns = $ns ^
1; # talk -> normal; normal -> talk
421 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
422 wfQuery( $sql, DB_WRITE
);
424 # standard deferred updates
425 $this->editUpdates( $text );
430 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
432 wfPurgeSquidServers($urlArr);
433 /* this needs to be done after LinksUpdate */
434 $u = new SquidUpdate($this->mTitle
);
435 array_push( $wgDeferredUpdateList, $u );
438 $this->showArticle( $text, wfMsg( "newarticle" ) );
441 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
443 global $wgOut, $wgUser, $wgLinkCache;
444 global $wgDBtransactions, $wgMwRedir;
445 global $wgUseSquid, $wgInternalServer;
446 $fname = "Article::updateArticle";
448 $this->loadLastEdit();
450 // insert updated section into old text if we have only edited part
452 if ($section != "") {
453 $oldtext=$this->getContent();
454 if($section=="new") {
455 if($summary) $subject="== {$summary} ==\n\n";
456 $text=$oldtext."\n\n".$subject.$text;
458 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
459 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
460 $secs[$section*2]=$text."\n\n"; // replace with edited
461 if($section) { $secs[$section*2-1]=""; } // erase old headline
462 $text=join("",$secs);
465 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
466 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
467 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
469 $text = $m[1] . "\n"; # Remove all content but redirect
473 $text = $this->preSaveTransform( $text );
475 # Update article, but only if changed.
477 if( $wgDBtransactions ) {
479 wfQuery( $sql, DB_WRITE
);
481 $oldtext = $this->getContent( true );
483 if ( 0 != strcmp( $text, $oldtext ) ) {
484 $this->mCountAdjustment
= $this->isCountable( $text )
485 - $this->isCountable( $oldtext );
487 $now = wfTimestampNow();
488 $won = wfInvertTimestamp( $now );
489 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
490 "',cur_comment='" . wfStrencode( $summary ) .
491 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
492 ",cur_timestamp='{$now}',cur_user_text='" .
493 wfStrencode( $wgUser->getName() ) .
494 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
495 "WHERE cur_id=" . $this->getID() .
496 " AND cur_timestamp='" . $this->getTimestamp() . "'";
497 $res = wfQuery( $sql, DB_WRITE
, $fname );
499 if( wfAffectedRows() == 0 ) {
500 /* Belated edit conflict! Run away!! */
504 # This overwrites $oldtext if revision compression is on
505 $flags = Article
::compressRevisionText( $oldtext );
507 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
508 "old_comment,old_user,old_user_text,old_timestamp," .
509 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
510 $this->mTitle
->getNamespace() . ", '" .
511 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
512 wfStrencode( $oldtext ) . "', '" .
513 wfStrencode( $this->getComment() ) . "', " .
514 $this->getUser() . ", '" .
515 wfStrencode( $this->getUserText() ) . "', '" .
516 $this->getTimestamp() . "', " . $me1 . ", '" .
517 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
518 $res = wfQuery( $sql, DB_WRITE
, $fname );
519 $oldid = wfInsertID( $res );
521 $bot = (int)($wgUser->isBot() ||
$forceBot);
522 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
523 $oldid, $this->getTimestamp(), $bot );
524 Article
::onArticleEdit( $this->mTitle
);
527 if( $wgDBtransactions ) {
529 wfQuery( $sql, DB_WRITE
);
533 if (!$this->mTitle
->userIsWatching()) $this->watch();
535 if ( $this->mTitle
->userIsWatching() ) {
539 # standard deferred updates
540 $this->editUpdates( $text );
546 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
548 wfPurgeSquidServers($urlArr);
551 $this->showArticle( $text, wfMsg( "updated" ) );
555 # After we've either updated or inserted the article, update
556 # the link tables and redirect to the new page.
558 function showArticle( $text, $subtitle )
560 global $wgOut, $wgUser, $wgLinkCache;
563 $wgLinkCache = new LinkCache();
565 # Get old version of link table to allow incremental link updates
566 $wgLinkCache->preFill( $this->mTitle
);
567 $wgLinkCache->clear();
569 # Now update the link cache by parsing the text
570 $wgOut = new OutputPage();
571 $wgOut->addWikiText( $text );
573 if( $wgMwRedir->matchStart( $text ) )
577 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
580 # Add this page to my watchlist
582 function watch( $add = true )
584 global $wgUser, $wgOut, $wgLang;
585 global $wgDeferredUpdateList;
587 if ( 0 == $wgUser->getID() ) {
588 $wgOut->errorpage( "watchnologin", "watchnologintext" );
591 if ( wfReadOnly() ) {
592 $wgOut->readOnlyPage();
596 $wgUser->addWatch( $this->mTitle
);
598 $wgUser->removeWatch( $this->mTitle
);
600 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
601 $wgOut->setRobotpolicy( "noindex,follow" );
603 $sk = $wgUser->getSkin() ;
604 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
607 $text = wfMsg( "addedwatchtext", $link );
609 $text = wfMsg( "removedwatchtext", $link );
610 $wgOut->addHTML( $text );
612 $up = new UserUpdate();
613 array_push( $wgDeferredUpdateList, $up );
615 $wgOut->returnToMain( false );
620 $this->watch( false );
623 function protect( $limit = "sysop" )
625 global $wgUser, $wgOut;
627 if ( ! $wgUser->isSysop() ) {
628 $wgOut->sysopRequired();
631 if ( wfReadOnly() ) {
632 $wgOut->readOnlyPage();
635 $id = $this->mTitle
->getArticleID();
637 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
640 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
641 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
642 wfQuery( $sql, DB_WRITE
, "Article::protect" );
644 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
645 if ( $limit === "" ) {
646 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
648 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
650 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
655 return $this->protect( "" );
660 global $wgUser, $wgOut, $wgMessageCache;
661 global $wpConfirm, $wpReason, $image, $oldimage;
663 # This code desperately needs to be totally rewritten
665 if ( ( ! $wgUser->isSysop() ) ) {
666 $wgOut->sysopRequired();
669 if ( wfReadOnly() ) {
670 $wgOut->readOnlyPage();
674 # Can't delete cached MediaWiki namespace (i.e. vital messages)
675 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
676 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
680 # Better double-check that it hasn't been deleted yet!
681 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
682 if ( ( "" == trim( $this->mTitle
->getText() ) )
683 or ( $this->mTitle
->getArticleId() == 0 ) ) {
684 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
688 if ( $_POST["wpConfirm"] ) {
693 # determine whether this page has earlier revisions
694 # and insert a warning if it does
695 # we select the text because it might be useful below
696 $ns = $this->mTitle
->getNamespace();
697 $title = $this->mTitle
->getDBkey();
698 $etitle = wfStrencode( $title );
699 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
700 $res = wfQuery( $sql, DB_READ
, $fname );
701 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
702 $skin=$wgUser->getSkin();
703 $wgOut->addHTML("<B>".wfMsg("historywarning"));
704 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
707 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
708 $res=wfQuery($sql, DB_READ
, $fname);
709 if( ($s=wfFetchObject($res))) {
711 # if this is a mini-text, we can paste part of it into the deletion reason
713 #if this is empty, an earlier revision may contain "useful" text
714 if($s->cur_text
!="") {
718 $text = Article
::getRevisionText( $old );
724 $length=strlen($text);
726 # this should not happen, since it is not possible to store an empty, new
727 # page. Let's insert a standard text in case it does, though
728 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
731 if($length < 500 && !$wpReason) {
733 # comment field=255, let's grep the first 150 to have some user
735 $text=substr($text,0,150);
736 # let's strip out newlines and HTML tags
737 $text=preg_replace("/\"/","'",$text);
738 $text=preg_replace("/\</","<",$text);
739 $text=preg_replace("/\>/",">",$text);
740 $text=preg_replace("/[\n\r]/","",$text);
742 $wpReason=wfMsg("excontent"). " '".$text;
744 $wpReason=wfMsg("exbeforeblank") . " '".$text;
746 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
751 return $this->confirmDelete();
754 function confirmDelete( $par = "" )
759 wfDebug( "Article::confirmDelete\n" );
761 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
762 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
763 $wgOut->setRobotpolicy( "noindex,nofollow" );
764 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
766 $t = $this->mTitle
->getPrefixedURL();
768 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
769 $confirm = wfMsg( "confirm" );
770 $check = wfMsg( "confirmcheck" );
771 $delcom = wfMsg( "deletecomment" );
774 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
775 <table border=0><tr><td align=right>
776 {$delcom}:</td><td align=left>
777 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
778 </td></tr><tr><td> </td></tr>
780 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
781 </td><td><label for=\"wpConfirm\">{$check}</label></td>
782 </tr><tr><td> </td><td>
783 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
784 </td></tr></table></form>\n" );
786 $wgOut->returnToMain( false );
791 global $wgOut, $wgUser, $wgLang;
793 $fname = "Article::doDelete";
794 wfDebug( "$fname\n" );
796 $this->doDeleteArticle( $this->mTitle
);
797 $deleted = $this->mTitle
->getPrefixedText();
799 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
800 $wgOut->setRobotpolicy( "noindex,nofollow" );
802 $sk = $wgUser->getSkin();
803 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
804 Namespace::getWikipedia() ) .
805 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
807 $text = wfMsg( "deletedtext", $deleted, $loglink );
809 $wgOut->addHTML( "<p>" . $text );
810 $wgOut->returnToMain( false );
813 function doDeleteArticle( $title )
815 global $wgUser, $wgOut, $wgLang, $wpReason;
816 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
818 $fname = "Article::doDeleteArticle";
819 wfDebug( "$fname\n" );
821 $ns = $title->getNamespace();
822 $t = wfStrencode( $title->getDBkey() );
823 $id = $title->getArticleID();
826 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
830 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
831 array_push( $wgDeferredUpdateList, $u );
836 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
838 wfPurgeSquidServers($urlArr);
840 /* prepare the list of urls to purge */
841 $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
842 $res = wfQuery ( $sql, DB_READ
) ;
843 while ( $BL = wfFetchObject ( $res ) )
845 $tobj = Title
::newFromDBkey( $BL->l_from
) ;
846 $blurlArr[] = $wgInternalServer.wfLocalUrl( $tobj->getPrefixedURL() );
848 wfFreeResult ( $res ) ;
849 $u = new SquidUpdate( $this->mTitle
, $blurlArr );
850 array_push( $wgDeferredUpdateList, $u );
856 # Move article and history to the "archive" table
857 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
858 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
859 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
860 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
861 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
862 wfQuery( $sql, DB_WRITE
, $fname );
864 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
865 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
866 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
867 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
868 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
869 wfQuery( $sql, DB_WRITE
, $fname );
871 # Now that it's safely backed up, delete it
873 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
875 wfQuery( $sql, DB_WRITE
, $fname );
877 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
879 wfQuery( $sql, DB_WRITE
, $fname );
881 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
883 wfQuery( $sql, DB_WRITE
, $fname );
885 # Finally, clean up the link tables
889 $t = wfStrencode( $title->getPrefixedDBkey() );
891 Article
::onArticleDelete( $title );
893 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
894 $res = wfQuery( $sql, DB_READ
, $fname );
896 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
897 $now = wfTimestampNow();
898 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
901 while ( $s = wfFetchObject( $res ) ) {
902 $nt = Title
::newFromDBkey( $s->l_from
);
903 $lid = $nt->getArticleID();
905 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
907 $sql .= "({$lid},'{$t}')";
912 wfQuery( $sql, DB_WRITE
, $fname );
913 wfQuery( $sql2, DB_WRITE
, $fname );
915 wfFreeResult( $res );
917 $sql = "DELETE FROM links WHERE l_to={$id}";
918 wfQuery( $sql, DB_WRITE
, $fname );
920 $sql = "DELETE FROM links WHERE l_from='{$t}'";
921 wfQuery( $sql, DB_WRITE
, $fname );
923 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
924 wfQuery( $sql, DB_WRITE
, $fname );
926 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
927 wfQuery( $sql, DB_WRITE
, $fname );
930 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
931 $art = $title->getPrefixedText();
932 $wpReason = wfCleanQueryVar( $wpReason );
933 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
935 # Clear the cached article id so the interface doesn't act like we exist
936 $this->mTitle
->resetArticleID( 0 );
937 $this->mTitle
->mArticleID
= 0;
942 global $wgUser, $wgLang, $wgOut, $from;
944 if ( ! $wgUser->isSysop() ) {
945 $wgOut->sysopRequired();
948 if ( wfReadOnly() ) {
949 $wgOut->readOnlyPage( $this->getContent() );
953 # Enhanced rollback, marks edits rc_bot=1
954 $bot = !!$_REQUEST['bot'];
956 # Replace all this user's current edits with the next one down
957 $tt = wfStrencode( $this->mTitle
->getDBKey() );
958 $n = $this->mTitle
->getNamespace();
960 # Get the last editor
961 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
962 $res = wfQuery( $sql, DB_READ
);
963 if( ($x = wfNumRows( $res )) != 1 ) {
965 $wgOut->addHTML( wfMsg( "notanarticle" ) );
968 $s = wfFetchObject( $res );
969 $ut = wfStrencode( $s->cur_user_text
);
973 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
974 if( $from != $s->cur_user_text
) {
975 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
976 $wgOut->addWikiText( wfMsg( "alreadyrolled",
977 htmlspecialchars( $this->mTitle
->getPrefixedText()),
978 htmlspecialchars( $from ),
979 htmlspecialchars( $s->cur_user_text
) ) );
980 if($s->cur_comment
!= "") {
983 htmlspecialchars( $s->cur_comment
) ) );
988 # Get the last edit not by this guy
989 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
990 FROM old USE INDEX (name_title_timestamp)
991 WHERE old_namespace={$n} AND old_title='{$tt}'
992 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
993 ORDER BY inverse_timestamp LIMIT 1";
994 $res = wfQuery( $sql, DB_READ
);
995 if( wfNumRows( $res ) != 1 ) {
997 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
998 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1001 $s = wfFetchObject( $res );
1004 # Mark all reverted edits as bot
1005 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1006 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1007 wfQuery( $sql, DB_WRITE
, $fname );
1011 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
1012 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1013 $wgOut->setRobotpolicy( "noindex,nofollow" );
1014 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1015 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
1017 Article
::onArticleEdit( $this->mTitle
);
1018 $wgOut->returnToMain( false );
1022 # Do standard deferred updates after page view
1024 /* private */ function viewUpdates()
1026 global $wgDeferredUpdateList;
1027 if ( 0 != $this->getID() ) {
1028 global $wgDisableCounters;
1029 if( !$wgDisableCounters ) {
1030 Article
::incViewCount( $this->getID() );
1031 $u = new SiteStatsUpdate( 1, 0, 0 );
1032 array_push( $wgDeferredUpdateList, $u );
1034 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1035 $this->mTitle
->getDBkey() );
1036 array_push( $wgDeferredUpdateList, $u );
1040 # Do standard deferred updates after page edit.
1041 # Every 1000th edit, prune the recent changes table.
1043 /* private */ function editUpdates( $text )
1045 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1046 global $wgMessageCache;
1049 if ( 0 == mt_rand( 0, 999 ) ) {
1050 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1051 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1052 wfQuery( $sql, DB_WRITE
);
1054 $id = $this->getID();
1055 $title = $this->mTitle
->getPrefixedDBkey();
1056 $shortTitle = $this->mTitle
->getDBkey();
1058 $adj = $this->mCountAdjustment
;
1061 $u = new LinksUpdate( $id, $title );
1062 array_push( $wgDeferredUpdateList, $u );
1063 $u = new SiteStatsUpdate( 0, 1, $adj );
1064 array_push( $wgDeferredUpdateList, $u );
1065 $u = new SearchUpdate( $id, $title, $text );
1066 array_push( $wgDeferredUpdateList, $u );
1068 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1069 array_push( $wgDeferredUpdateList, $u );
1071 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1072 $wgMessageCache->replace( $shortTitle, $text );
1077 /* private */ function setOldSubtitle()
1079 global $wgLang, $wgOut;
1081 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1082 $r = wfMsg( "revisionasof", $td );
1083 $wgOut->setSubtitle( "({$r})" );
1086 # This function is called right before saving the wikitext,
1087 # so we can do things like signatures and links-in-context.
1089 function preSaveTransform( $text )
1092 while ( "" != $text ) {
1093 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1094 $s .= $this->pstPass2( $p[0] );
1096 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1098 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1099 $s .= "<nowiki>{$q[0]}</nowiki>";
1106 /* private */ function pstPass2( $text )
1108 global $wgUser, $wgLang, $wgLocaltimezone;
1112 $n = $wgUser->getName();
1113 $k = $wgUser->getOption( "nickname" );
1114 if ( "" == $k ) { $k = $n; }
1115 if(isset($wgLocaltimezone)) {
1116 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1118 /* Note: this is an ugly timezone hack for the European wikis */
1119 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1120 " (" . date( "T" ) . ")";
1121 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1123 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1124 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1125 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1126 Namespace::getUser() ) . ":$n|$k]]", $text );
1128 # Context links: [[|name]] and [[name (context)|]]
1130 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1131 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1132 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1133 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1135 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1136 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1137 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1138 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1139 # [[ns:page (cont)|]]
1141 $t = $this->mTitle
->getText();
1142 if ( preg_match( $conpat, $t, $m ) ) {
1145 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1146 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1147 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1149 if ( "" == $context ) {
1150 $text = preg_replace( $p2, "[[\\1]]", $text );
1152 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1155 # {{SUBST:xxx}} variables
1157 $mw =& MagicWord
::get( MAG_SUBST
);
1158 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1161 # Trim trailing whitespace
1162 # MAG_END (__END__) tag allows for trailing
1163 # whitespace to be deliberately included
1164 $text = rtrim( $text );
1165 $mw =& MagicWord::get( MAG_END );
1166 $mw->matchAndRemove( $text );
1171 /* Caching functions */
1173 # checkLastModified returns true iff it has taken care of all
1174 # output to the client that is necessary for this request.
1175 # (that is, it has sent a cached version of the page)
1176 function tryFileCache() {
1177 static $called = false;
1179 wfDebug( " tryFileCache() -- called twice!?\n" );
1183 if($this->isFileCacheable()) {
1184 $touched = $this->mTouched
;
1185 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1186 # Expire the main page quicker
1187 $expire = wfUnix2Timestamp( time() - 3600 );
1188 $touched = max( $expire, $touched );
1190 $cache = new CacheManager( $this->mTitle
);
1191 if($cache->isFileCacheGood( $touched )) {
1193 wfDebug( " tryFileCache() - about to load\n" );
1194 $cache->loadFromFileCache();
1197 wfDebug( " tryFileCache() - starting buffer\n" );
1198 ob_start( array(&$cache, 'saveToFileCache' ) );
1201 wfDebug( " tryFileCache() - not cacheable\n" );
1205 function isFileCacheable() {
1206 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1207 global $action, $oldid, $diff, $redirect, $printable;
1208 return $wgUseFileCache
1209 and (!$wgShowIPinHeader)
1210 and ($this->getID() != 0)
1211 and ($wgUser->getId() == 0)
1212 and (!$wgUser->getNewtalk())
1213 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1214 and ($action == "view")
1215 and (!isset($oldid))
1217 and (!isset($redirect))
1218 and (!isset($printable))
1219 and (!$this->mRedirectedFrom
);
1222 function checkTouched() {
1223 $id = $this->getID();
1224 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1225 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1226 if( $s = wfFetchObject( $res ) ) {
1227 $this->mTouched
= $s->cur_touched
;
1228 return !$s->cur_is_redirect
;
1234 /* static */ function incViewCount( $id )
1236 $id = intval( $id );
1237 global $wgHitcounterUpdateFreq;
1239 if( $wgHitcounterUpdateFreq <= 1 ){ //
1240 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1241 "WHERE cur_id = $id", DB_WRITE
);
1245 # Not important enough to warrant an error page in case of failure
1246 $oldignore = wfIgnoreSQLErrors( true );
1248 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1250 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1251 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1252 # Most of the time (or on SQL errors), skip row count check
1253 wfIgnoreSQLErrors( $oldignore );
1257 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1258 $row = wfFetchObject( $res );
1259 $rown = intval( $row->n
);
1260 if( $rown >= $wgHitcounterUpdateFreq ){
1261 wfProfileIn( "Article::incViewCount-collect" );
1262 $old_user_abort = ignore_user_abort( true );
1264 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1265 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1266 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1267 "GROUP BY hc_id", DB_WRITE
);
1268 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1269 wfQuery("UNLOCK TABLES", DB_WRITE
);
1270 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1271 "WHERE cur_id = hc_id", DB_WRITE
);
1272 wfQuery("DROP TABLE acchits", DB_WRITE
);
1274 ignore_user_abort( $old_user_abort );
1275 wfProfileOut( "Article::incViewCount-collect" );
1277 wfIgnoreSQLErrors( $oldignore );
1280 # The onArticle*() functions are supposed to be a kind of hooks
1281 # which should be called whenever any of the specified actions
1284 # This is a good place to put code to clear caches, for instance.
1286 /* static */ function onArticleCreate($title_obj){
1287 global $wgEnablePersistentLC, $wgEnableParserCache;
1288 if ( $wgEnablePersistentLC ) {
1289 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1291 if ( $wgEnableParserCache ) {
1292 OutputPage
::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1296 /* static */ function onArticleDelete($title_obj){
1297 global $wgEnablePersistentLC, $wgEnableParserCache;
1298 if ( $wgEnablePersistentLC ) {
1299 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1301 if ( $wgEnableParserCache ) {
1302 OutputPage
::parsercacheClearLinksTo( $title_obj->getArticleID() );
1306 /* static */ function onArticleEdit($title_obj){
1307 global $wgEnablePersistentLC, $wgEnableParserCache;
1308 if ( $wgEnablePersistentLC ) {
1309 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1311 if ( $wgEnableParserCache ) {
1312 OutputPage
::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );
1317 function wfReplaceSubstVar( $matches ) {
1318 return wfMsg( $matches[1] );