2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
11 * s/\$wgTitle/\$this->mTitle/ performed, many replacements
12 * mTitle variable added to class
15 include_once( "CacheManager.php" );
18 /* private */ var $mContent, $mContentLoaded;
19 /* private */ var $mUser, $mTimestamp, $mUserText;
20 /* private */ var $mCounter, $mComment, $mCountAdjustment;
21 /* private */ var $mMinorEdit, $mRedirectedFrom;
22 /* private */ var $mTouched, $mFileCache, $mTitle;
23 /* private */ var $mId, $mTable;
25 function Article( &$title ) {
26 $this->mTitle
=& $title;
30 /* private */ function clear()
32 $this->mContentLoaded
= false;
33 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
34 $this->mRedirectedFrom
= $this->mUserText
=
35 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
36 $this->mCountAdjustment
= 0;
37 $this->mTouched
= "19700101000000";
40 /* static */ function getRevisionText( $row, $prefix = "old_" ) {
41 # Deal with optional compression of archived pages.
42 # This can be done periodically via maintenance/compressOld.php, and
43 # as pages are saved if $wgCompressRevisions is set.
44 $text = $prefix . "text";
45 $flags = $prefix . "flags";
46 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, "gzip" ) ) ) {
47 return gzinflate( $row->$text );
49 if( isset( $row->$text ) ) {
55 /* static */ function compressRevisionText( &$text ) {
56 global $wgCompressRevisions;
57 if( !$wgCompressRevisions ) {
60 if( !function_exists( "gzdeflate" ) ) {
61 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
64 $text = gzdeflate( $text );
68 # Note that getContent/loadContent may follow redirects if
69 # not told otherwise, and so may cause a change to mTitle.
71 # Return the text of this revision
72 function getContent( $noredir = false )
74 global $action,$section,$count; # From query string
75 $fname = "Article::getContent";
76 wfProfileIn( $fname );
78 if ( 0 == $this->getID() ) {
79 if ( "edit" == $action ) {
80 wfProfileOut( $fname );
81 return ""; # was "newarticletext", now moved above the box)
83 wfProfileOut( $fname );
84 return wfMsg( "noarticletext" );
86 $this->loadContent( $noredir );
89 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
90 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
91 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
95 wfProfileOut( $fname );
96 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
100 if($section=="new") {
101 wfProfileOut( $fname );
105 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
107 PREG_SPLIT_DELIM_CAPTURE
);
109 wfProfileOut( $fname );
110 return trim($secs[0]);
112 wfProfileOut( $fname );
113 return trim($secs[$section*2-1] . $secs[$section*2]);
117 wfProfileOut( $fname );
118 return $this->mContent
;
123 # Load the revision (including cur_text) into this object
124 function loadContent( $noredir = false )
126 global $wgOut, $wgMwRedir;
127 global $oldid, $redirect; # From query
129 if ( $this->mContentLoaded
) return;
130 $fname = "Article::loadContent";
133 if ( ! $oldid ) { # Retrieve current version
134 $id = $this->getID();
135 if ( 0 == $id ) return;
138 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
139 "FROM cur WHERE cur_id={$id}";
141 $res = wfQuery( $sql, DB_READ
, $fname );
142 if ( 0 == wfNumRows( $res ) ) {
146 $s = wfFetchObject( $res );
147 # If we got a redirect, follow it (unless we've been told
148 # not to by either the function parameter or the query
149 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
150 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
151 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
152 $s->cur_text
, $m ) ) {
153 $rt = Title
::newFromText( $m[1] );
155 # Gotta hand redirects to special pages differently:
156 # Fill the HTTP response "Location" header and ignore
157 # the rest of the page we're on.
159 if ( $rt->getInterwiki() != "" ) {
160 $wgOut->redirect( $rt->getFullURL() ) ;
163 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
164 $wgOut->redirect( wfLocalUrl(
165 $rt->getPrefixedURL() ) );
168 $rid = $rt->getArticleID();
170 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
171 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
172 $res = wfQuery( $sql, DB_READ
, $fname );
174 if ( 0 != wfNumRows( $res ) ) {
175 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
177 $s = wfFetchObject( $res );
184 $this->mContent
= $s->cur_text
;
185 $this->mUser
= $s->cur_user
;
186 $this->mCounter
= $s->cur_counter
;
187 $this->mTimestamp
= $s->cur_timestamp
;
188 $this->mTouched
= $s->cur_touched
;
189 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
190 $this->mTitle
->mRestrictionsLoaded
= true;
191 wfFreeResult( $res );
192 } else { # oldid set, retrieve historical version
193 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
194 "WHERE old_id={$oldid}";
195 $res = wfQuery( $sql, DB_READ
, $fname );
196 if ( 0 == wfNumRows( $res ) ) { return; }
198 $s = wfFetchObject( $res );
199 $this->mContent
= Article
::getRevisionText( $s );
200 $this->mUser
= $s->old_user
;
202 $this->mTimestamp
= $s->old_timestamp
;
203 wfFreeResult( $res );
206 # Return error message :P
207 # Horrible, confusing UI and data. I think this should return false on error -- TS
209 $t = $this->mTitle
->getPrefixedText();
210 if ( isset( $oldid ) ) {
211 $oldid = IntVal( $oldid );
212 $t .= ",oldid={$oldid}";
214 if ( isset( $redirect ) ) {
215 $redirect = ($redirect == "no") ?
"no" : "yes";
216 $t .= ",redirect={$redirect}";
218 $this->mContent
= wfMsg( "missingarticle", $t );
221 $this->mContentLoaded
= true;
225 if( $this->mTitle
) {
226 return $this->mTitle
->getArticleID();
234 if ( -1 == $this->mCounter
) {
235 $id = $this->getID();
236 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
238 return $this->mCounter
;
241 # Would the given text make this article a "good" article (i.e.,
242 # suitable for including in the article count)?
244 function isCountable( $text )
246 global $wgUseCommaCount, $wgMwRedir;
248 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
249 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
250 $token = ($wgUseCommaCount ?
"," : "[[" );
251 if ( false === strstr( $text, $token ) ) { return 0; }
255 # Loads everything from cur except cur_text
256 # This isn't necessary for all uses, so it's only done if needed.
258 /* private */ function loadLastEdit()
261 if ( -1 != $this->mUser
) return;
263 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
264 "cur_comment,cur_minor_edit FROM cur WHERE " .
265 "cur_id=" . $this->getID();
266 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
268 if ( wfNumRows( $res ) > 0 ) {
269 $s = wfFetchObject( $res );
270 $this->mUser
= $s->cur_user
;
271 $this->mUserText
= $s->cur_user_text
;
272 $this->mTimestamp
= $s->cur_timestamp
;
273 $this->mComment
= $s->cur_comment
;
274 $this->mMinorEdit
= $s->cur_minor_edit
;
278 function getTimestamp()
280 $this->loadLastEdit();
281 return $this->mTimestamp
;
286 $this->loadLastEdit();
290 function getUserText()
292 $this->loadLastEdit();
293 return $this->mUserText
;
296 function getComment()
298 $this->loadLastEdit();
299 return $this->mComment
;
302 function getMinorEdit()
304 $this->loadLastEdit();
305 return $this->mMinorEdit
;
308 # This is the default action of the script: just view the page of
313 global $wgUser, $wgOut, $wgLang;
314 global $oldid, $diff; # From query
315 global $wgLinkCache, $IP;
316 $fname = "Article::view";
317 wfProfileIn( $fname );
319 $wgOut->setArticleFlag( true );
320 $wgOut->setRobotpolicy( "index,follow" );
322 # If we got diff and oldid in the query, we want to see a
323 # diff page instead of the article.
325 if ( isset( $diff ) ) {
326 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
327 $de = new DifferenceEngine( $oldid, $diff );
329 wfProfileOut( $fname );
333 if ( !isset( $oldid ) and $this->checkTouched() ) {
334 if( $wgOut->checkLastModified( $this->mTouched
) ){
336 } else if ( $this->tryFileCache() ) {
337 # tell wgOut that output is taken care of
339 $this->viewUpdates();
344 $text = $this->getContent(); # May change mTitle
345 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
346 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
347 " - " . wfMsg( "wikititlesuffix" ) );
349 # We're looking at an old revision
352 $this->setOldSubtitle();
353 $wgOut->setRobotpolicy( "noindex,follow" );
355 if ( "" != $this->mRedirectedFrom
) {
356 $sk = $wgUser->getSkin();
357 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
359 $s = wfMsg( "redirectedfrom", $redir );
360 $wgOut->setSubtitle( $s );
363 $wgLinkCache->preFill( $this->mTitle
);
364 $wgOut->addWikiText( $text );
366 $this->viewUpdates();
367 wfProfileOut( $fname );
370 # Theoretically we could defer these whole insert and update
371 # functions for after display, but that's taking a big leap
372 # of faith, and we want to be able to report database
373 # errors at some point.
375 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
377 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
378 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
380 $fname = "Article::insertNewArticle";
382 $this->mCountAdjustment
= $this->isCountable( $text );
384 $ns = $this->mTitle
->getNamespace();
385 $ttl = $this->mTitle
->getDBkey();
386 $text = $this->preSaveTransform( $text );
387 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
390 $now = wfTimestampNow();
391 $won = wfInvertTimestamp( $now );
393 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
394 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
395 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
396 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
397 "cur_restrictions,cur_user_text,cur_is_redirect," .
398 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
399 wfStrencode( $text ) . "', '" .
400 wfStrencode( $summary ) . "', '" .
401 $wgUser->getID() . "', '{$now}', " .
402 $isminor . ", 0, '', '" .
403 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
404 $res = wfQuery( $sql, DB_WRITE
, $fname );
406 $newid = wfInsertId();
407 $this->mTitle
->resetArticleID( $newid );
409 Article
::onArticleCreate( $this->mTitle
);
410 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
413 if(!$this->mTitle
->userIsWatching()) $this->watch();
415 if ( $this->mTitle
->userIsWatching() ) {
420 # The talk page isn't in the regular link tables, so we need to update manually:
421 $talkns = $ns ^
1; # talk -> normal; normal -> talk
422 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
423 wfQuery( $sql, DB_WRITE
);
425 # standard deferred updates
426 $this->editUpdates( $text );
431 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
433 wfPurgeSquidServers($urlArr);
434 /* this needs to be done after LinksUpdate */
435 $u = new SquidUpdate($this->mTitle
);
436 array_push( $wgDeferredUpdateList, $u );
439 $this->showArticle( $text, wfMsg( "newarticle" ) );
442 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
444 global $wgOut, $wgUser, $wgLinkCache;
445 global $wgDBtransactions, $wgMwRedir;
446 global $wgUseSquid, $wgInternalServer;
447 $fname = "Article::updateArticle";
449 $this->loadLastEdit();
451 // insert updated section into old text if we have only edited part
453 if ($section != "") {
454 $oldtext=$this->getContent();
455 if($section=="new") {
456 if($summary) $subject="== {$summary} ==\n\n";
457 $text=$oldtext."\n\n".$subject.$text;
459 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
460 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
461 $secs[$section*2]=$text."\n\n"; // replace with edited
462 if($section) { $secs[$section*2-1]=""; } // erase old headline
463 $text=join("",$secs);
466 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
467 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
468 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
470 $text = $m[1] . "\n"; # Remove all content but redirect
474 $text = $this->preSaveTransform( $text );
476 # Update article, but only if changed.
478 if( $wgDBtransactions ) {
480 wfQuery( $sql, DB_WRITE
);
482 $oldtext = $this->getContent( true );
484 if ( 0 != strcmp( $text, $oldtext ) ) {
485 $this->mCountAdjustment
= $this->isCountable( $text )
486 - $this->isCountable( $oldtext );
488 $now = wfTimestampNow();
489 $won = wfInvertTimestamp( $now );
490 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
491 "',cur_comment='" . wfStrencode( $summary ) .
492 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
493 ",cur_timestamp='{$now}',cur_user_text='" .
494 wfStrencode( $wgUser->getName() ) .
495 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
496 "WHERE cur_id=" . $this->getID() .
497 " AND cur_timestamp='" . $this->getTimestamp() . "'";
498 $res = wfQuery( $sql, DB_WRITE
, $fname );
500 if( wfAffectedRows() == 0 ) {
501 /* Belated edit conflict! Run away!! */
505 # This overwrites $oldtext if revision compression is on
506 $flags = Article
::compressRevisionText( $oldtext );
508 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
509 "old_comment,old_user,old_user_text,old_timestamp," .
510 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
511 $this->mTitle
->getNamespace() . ", '" .
512 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
513 wfStrencode( $oldtext ) . "', '" .
514 wfStrencode( $this->getComment() ) . "', " .
515 $this->getUser() . ", '" .
516 wfStrencode( $this->getUserText() ) . "', '" .
517 $this->getTimestamp() . "', " . $me1 . ", '" .
518 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
519 $res = wfQuery( $sql, DB_WRITE
, $fname );
520 $oldid = wfInsertID( $res );
522 $bot = (int)($wgUser->isBot() ||
$forceBot);
523 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
524 $oldid, $this->getTimestamp(), $bot );
525 Article
::onArticleEdit( $this->mTitle
);
528 if( $wgDBtransactions ) {
530 wfQuery( $sql, DB_WRITE
);
534 if (!$this->mTitle
->userIsWatching()) $this->watch();
536 if ( $this->mTitle
->userIsWatching() ) {
540 # standard deferred updates
541 $this->editUpdates( $text );
547 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
549 wfPurgeSquidServers($urlArr);
552 $this->showArticle( $text, wfMsg( "updated" ) );
556 # After we've either updated or inserted the article, update
557 # the link tables and redirect to the new page.
559 function showArticle( $text, $subtitle )
561 global $wgOut, $wgUser, $wgLinkCache;
564 $wgLinkCache = new LinkCache();
566 # Get old version of link table to allow incremental link updates
567 $wgLinkCache->preFill( $this->mTitle
);
568 $wgLinkCache->clear();
570 # Now update the link cache by parsing the text
571 $wgOut = new OutputPage();
572 $wgOut->addWikiText( $text );
574 if( $wgMwRedir->matchStart( $text ) )
578 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
581 # Add this page to my watchlist
583 function watch( $add = true )
585 global $wgUser, $wgOut, $wgLang;
586 global $wgDeferredUpdateList;
588 if ( 0 == $wgUser->getID() ) {
589 $wgOut->errorpage( "watchnologin", "watchnologintext" );
592 if ( wfReadOnly() ) {
593 $wgOut->readOnlyPage();
597 $wgUser->addWatch( $this->mTitle
);
599 $wgUser->removeWatch( $this->mTitle
);
601 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
602 $wgOut->setRobotpolicy( "noindex,follow" );
604 $sk = $wgUser->getSkin() ;
605 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
608 $text = wfMsg( "addedwatchtext", $link );
610 $text = wfMsg( "removedwatchtext", $link );
611 $wgOut->addHTML( $text );
613 $up = new UserUpdate();
614 array_push( $wgDeferredUpdateList, $up );
616 $wgOut->returnToMain( false );
621 $this->watch( false );
624 function protect( $limit = "sysop" )
626 global $wgUser, $wgOut;
628 if ( ! $wgUser->isSysop() ) {
629 $wgOut->sysopRequired();
632 if ( wfReadOnly() ) {
633 $wgOut->readOnlyPage();
636 $id = $this->mTitle
->getArticleID();
638 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
641 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
642 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
643 wfQuery( $sql, DB_WRITE
, "Article::protect" );
645 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
646 if ( $limit === "" ) {
647 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
649 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
651 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
656 return $this->protect( "" );
661 global $wgUser, $wgOut, $wgMessageCache;
662 global $wpConfirm, $wpReason, $image, $oldimage;
664 # This code desperately needs to be totally rewritten
666 if ( ( ! $wgUser->isSysop() ) ) {
667 $wgOut->sysopRequired();
670 if ( wfReadOnly() ) {
671 $wgOut->readOnlyPage();
675 # Can't delete cached MediaWiki namespace (i.e. vital messages)
676 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
677 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
681 # Better double-check that it hasn't been deleted yet!
682 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
683 if ( ( "" == trim( $this->mTitle
->getText() ) )
684 or ( $this->mTitle
->getArticleId() == 0 ) ) {
685 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
689 if ( $_POST["wpConfirm"] ) {
694 # determine whether this page has earlier revisions
695 # and insert a warning if it does
696 # we select the text because it might be useful below
697 $ns = $this->mTitle
->getNamespace();
698 $title = $this->mTitle
->getDBkey();
699 $etitle = wfStrencode( $title );
700 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
701 $res = wfQuery( $sql, DB_READ
, $fname );
702 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
703 $skin=$wgUser->getSkin();
704 $wgOut->addHTML("<B>".wfMsg("historywarning"));
705 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
708 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
709 $res=wfQuery($sql, DB_READ
, $fname);
710 if( ($s=wfFetchObject($res))) {
712 # if this is a mini-text, we can paste part of it into the deletion reason
714 #if this is empty, an earlier revision may contain "useful" text
715 if($s->cur_text
!="") {
719 $text = Article
::getRevisionText( $old );
725 $length=strlen($text);
727 # this should not happen, since it is not possible to store an empty, new
728 # page. Let's insert a standard text in case it does, though
729 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
732 if($length < 500 && !$wpReason) {
734 # comment field=255, let's grep the first 150 to have some user
736 $text=substr($text,0,150);
737 # let's strip out newlines and HTML tags
738 $text=preg_replace("/\"/","'",$text);
739 $text=preg_replace("/\</","<",$text);
740 $text=preg_replace("/\>/",">",$text);
741 $text=preg_replace("/[\n\r]/","",$text);
743 $wpReason=wfMsg("excontent"). " '".$text;
745 $wpReason=wfMsg("exbeforeblank") . " '".$text;
747 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
752 return $this->confirmDelete();
755 function confirmDelete( $par = "" )
760 wfDebug( "Article::confirmDelete\n" );
762 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
763 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
764 $wgOut->setRobotpolicy( "noindex,nofollow" );
765 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
767 $t = $this->mTitle
->getPrefixedURL();
769 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
770 $confirm = wfMsg( "confirm" );
771 $check = wfMsg( "confirmcheck" );
772 $delcom = wfMsg( "deletecomment" );
775 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
776 <table border=0><tr><td align=right>
777 {$delcom}:</td><td align=left>
778 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
779 </td></tr><tr><td> </td></tr>
781 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
782 </td><td><label for=\"wpConfirm\">{$check}</label></td>
783 </tr><tr><td> </td><td>
784 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
785 </td></tr></table></form>\n" );
787 $wgOut->returnToMain( false );
792 global $wgOut, $wgUser, $wgLang;
794 $fname = "Article::doDelete";
795 wfDebug( "$fname\n" );
797 $this->doDeleteArticle( $this->mTitle
);
798 $deleted = $this->mTitle
->getPrefixedText();
800 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
801 $wgOut->setRobotpolicy( "noindex,nofollow" );
803 $sk = $wgUser->getSkin();
804 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
805 Namespace::getWikipedia() ) .
806 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
808 $text = wfMsg( "deletedtext", $deleted, $loglink );
810 $wgOut->addHTML( "<p>" . $text );
811 $wgOut->returnToMain( false );
814 function doDeleteArticle( $title )
816 global $wgUser, $wgOut, $wgLang, $wpReason;
817 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
819 $fname = "Article::doDeleteArticle";
820 wfDebug( "$fname\n" );
822 $ns = $title->getNamespace();
823 $t = wfStrencode( $title->getDBkey() );
824 $id = $title->getArticleID();
827 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
831 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
832 array_push( $wgDeferredUpdateList, $u );
837 $wgInternalServer.wfLocalUrl( $this->mTitle
->getPrefixedURL())
839 wfPurgeSquidServers($urlArr);
841 /* prepare the list of urls to purge */
842 $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
843 $res = wfQuery ( $sql, DB_READ
) ;
844 while ( $BL = wfFetchObject ( $res ) )
846 $tobj = Title
::newFromDBkey( $BL->l_from
) ;
847 $blurlArr[] = $wgInternalServer.wfLocalUrl( $tobj->getPrefixedURL() );
849 wfFreeResult ( $res ) ;
850 $u = new SquidUpdate( $this->mTitle
, $blurlArr );
851 array_push( $wgDeferredUpdateList, $u );
857 # Move article and history to the "archive" table
858 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
859 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
860 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
861 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
862 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
863 wfQuery( $sql, DB_WRITE
, $fname );
865 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
866 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
867 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
868 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
869 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
870 wfQuery( $sql, DB_WRITE
, $fname );
872 # Now that it's safely backed up, delete it
874 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
876 wfQuery( $sql, DB_WRITE
, $fname );
878 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
880 wfQuery( $sql, DB_WRITE
, $fname );
882 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
884 wfQuery( $sql, DB_WRITE
, $fname );
886 # Finally, clean up the link tables
890 $t = wfStrencode( $title->getPrefixedDBkey() );
892 Article
::onArticleDelete( $title );
894 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
895 $res = wfQuery( $sql, DB_READ
, $fname );
897 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
898 $now = wfTimestampNow();
899 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
902 while ( $s = wfFetchObject( $res ) ) {
903 $nt = Title
::newFromDBkey( $s->l_from
);
904 $lid = $nt->getArticleID();
906 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
908 $sql .= "({$lid},'{$t}')";
913 wfQuery( $sql, DB_WRITE
, $fname );
914 wfQuery( $sql2, DB_WRITE
, $fname );
916 wfFreeResult( $res );
918 $sql = "DELETE FROM links WHERE l_to={$id}";
919 wfQuery( $sql, DB_WRITE
, $fname );
921 $sql = "DELETE FROM links WHERE l_from='{$t}'";
922 wfQuery( $sql, DB_WRITE
, $fname );
924 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
925 wfQuery( $sql, DB_WRITE
, $fname );
927 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
928 wfQuery( $sql, DB_WRITE
, $fname );
931 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
932 $art = $title->getPrefixedText();
933 $wpReason = wfCleanQueryVar( $wpReason );
934 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
936 # Clear the cached article id so the interface doesn't act like we exist
937 $this->mTitle
->resetArticleID( 0 );
938 $this->mTitle
->mArticleID
= 0;
943 global $wgUser, $wgLang, $wgOut, $from;
945 if ( ! $wgUser->isSysop() ) {
946 $wgOut->sysopRequired();
949 if ( wfReadOnly() ) {
950 $wgOut->readOnlyPage( $this->getContent() );
954 # Enhanced rollback, marks edits rc_bot=1
955 $bot = !!$_REQUEST['bot'];
957 # Replace all this user's current edits with the next one down
958 $tt = wfStrencode( $this->mTitle
->getDBKey() );
959 $n = $this->mTitle
->getNamespace();
961 # Get the last editor
962 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
963 $res = wfQuery( $sql, DB_READ
);
964 if( ($x = wfNumRows( $res )) != 1 ) {
966 $wgOut->addHTML( wfMsg( "notanarticle" ) );
969 $s = wfFetchObject( $res );
970 $ut = wfStrencode( $s->cur_user_text
);
974 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
975 if( $from != $s->cur_user_text
) {
976 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
977 $wgOut->addWikiText( wfMsg( "alreadyrolled",
978 htmlspecialchars( $this->mTitle
->getPrefixedText()),
979 htmlspecialchars( $from ),
980 htmlspecialchars( $s->cur_user_text
) ) );
981 if($s->cur_comment
!= "") {
984 htmlspecialchars( $s->cur_comment
) ) );
989 # Get the last edit not by this guy
990 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
991 FROM old USE INDEX (name_title_timestamp)
992 WHERE old_namespace={$n} AND old_title='{$tt}'
993 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
994 ORDER BY inverse_timestamp LIMIT 1";
995 $res = wfQuery( $sql, DB_READ
);
996 if( wfNumRows( $res ) != 1 ) {
998 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
999 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1002 $s = wfFetchObject( $res );
1005 # Mark all reverted edits as bot
1006 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1007 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1008 wfQuery( $sql, DB_WRITE
, $fname );
1012 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
1013 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1014 $wgOut->setRobotpolicy( "noindex,nofollow" );
1015 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1016 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
1018 Article
::onArticleEdit( $this->mTitle
);
1019 $wgOut->returnToMain( false );
1023 # Do standard deferred updates after page view
1025 /* private */ function viewUpdates()
1027 global $wgDeferredUpdateList;
1028 if ( 0 != $this->getID() ) {
1029 global $wgDisableCounters;
1030 if( !$wgDisableCounters ) {
1031 Article
::incViewCount( $this->getID() );
1032 $u = new SiteStatsUpdate( 1, 0, 0 );
1033 array_push( $wgDeferredUpdateList, $u );
1035 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1036 $this->mTitle
->getDBkey() );
1037 array_push( $wgDeferredUpdateList, $u );
1041 # Do standard deferred updates after page edit.
1042 # Every 1000th edit, prune the recent changes table.
1044 /* private */ function editUpdates( $text )
1046 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1047 global $wgMessageCache;
1050 if ( 0 == mt_rand( 0, 999 ) ) {
1051 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1052 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1053 wfQuery( $sql, DB_WRITE
);
1055 $id = $this->getID();
1056 $title = $this->mTitle
->getPrefixedDBkey();
1057 $shortTitle = $this->mTitle
->getDBkey();
1059 $adj = $this->mCountAdjustment
;
1062 $u = new LinksUpdate( $id, $title );
1063 array_push( $wgDeferredUpdateList, $u );
1064 $u = new SiteStatsUpdate( 0, 1, $adj );
1065 array_push( $wgDeferredUpdateList, $u );
1066 $u = new SearchUpdate( $id, $title, $text );
1067 array_push( $wgDeferredUpdateList, $u );
1069 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1070 array_push( $wgDeferredUpdateList, $u );
1072 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1073 $wgMessageCache->replace( $shortTitle, $text );
1078 /* private */ function setOldSubtitle()
1080 global $wgLang, $wgOut;
1082 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1083 $r = wfMsg( "revisionasof", $td );
1084 $wgOut->setSubtitle( "({$r})" );
1087 # This function is called right before saving the wikitext,
1088 # so we can do things like signatures and links-in-context.
1090 function preSaveTransform( $text )
1093 while ( "" != $text ) {
1094 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1095 $s .= $this->pstPass2( $p[0] );
1097 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1099 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1100 $s .= "<nowiki>{$q[0]}</nowiki>";
1107 /* private */ function pstPass2( $text )
1109 global $wgUser, $wgLang, $wgLocaltimezone;
1113 $n = $wgUser->getName();
1114 $k = $wgUser->getOption( "nickname" );
1115 if ( "" == $k ) { $k = $n; }
1116 if(isset($wgLocaltimezone)) {
1117 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1119 /* Note: this is an ugly timezone hack for the European wikis */
1120 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1121 " (" . date( "T" ) . ")";
1122 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1124 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1125 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1126 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1127 Namespace::getUser() ) . ":$n|$k]]", $text );
1129 # Context links: [[|name]] and [[name (context)|]]
1131 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1132 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1133 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1134 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1136 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1137 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1138 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1139 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1140 # [[ns:page (cont)|]]
1142 $t = $this->mTitle
->getText();
1143 if ( preg_match( $conpat, $t, $m ) ) {
1146 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1147 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1148 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1150 if ( "" == $context ) {
1151 $text = preg_replace( $p2, "[[\\1]]", $text );
1153 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1156 # {{SUBST:xxx}} variables
1158 $mw =& MagicWord
::get( MAG_SUBST
);
1159 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1162 # Trim trailing whitespace
1163 # MAG_END (__END__) tag allows for trailing
1164 # whitespace to be deliberately included
1165 $text = rtrim( $text );
1166 $mw =& MagicWord::get( MAG_END );
1167 $mw->matchAndRemove( $text );
1172 /* Caching functions */
1174 # checkLastModified returns true iff it has taken care of all
1175 # output to the client that is necessary for this request.
1176 # (that is, it has sent a cached version of the page)
1177 function tryFileCache() {
1178 static $called = false;
1180 wfDebug( " tryFileCache() -- called twice!?\n" );
1184 if($this->isFileCacheable()) {
1185 $touched = $this->mTouched
;
1186 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1187 # Expire the main page quicker
1188 $expire = wfUnix2Timestamp( time() - 3600 );
1189 $touched = max( $expire, $touched );
1191 $cache = new CacheManager( $this->mTitle
);
1192 if($cache->isFileCacheGood( $touched )) {
1194 wfDebug( " tryFileCache() - about to load\n" );
1195 $cache->loadFromFileCache();
1198 wfDebug( " tryFileCache() - starting buffer\n" );
1199 ob_start( array(&$cache, 'saveToFileCache' ) );
1202 wfDebug( " tryFileCache() - not cacheable\n" );
1206 function isFileCacheable() {
1207 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1208 global $action, $oldid, $diff, $redirect, $printable;
1209 return $wgUseFileCache
1210 and (!$wgShowIPinHeader)
1211 and ($this->getID() != 0)
1212 and ($wgUser->getId() == 0)
1213 and (!$wgUser->getNewtalk())
1214 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1215 and ($action == "view")
1216 and (!isset($oldid))
1218 and (!isset($redirect))
1219 and (!isset($printable))
1220 and (!$this->mRedirectedFrom
);
1223 function checkTouched() {
1224 $id = $this->getID();
1225 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1226 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1227 if( $s = wfFetchObject( $res ) ) {
1228 $this->mTouched
= $s->cur_touched
;
1229 return !$s->cur_is_redirect
;
1235 /* static */ function incViewCount( $id )
1237 $id = intval( $id );
1238 global $wgHitcounterUpdateFreq;
1240 if( $wgHitcounterUpdateFreq <= 1 ){ //
1241 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1242 "WHERE cur_id = $id", DB_WRITE
);
1246 # Not important enough to warrant an error page in case of failure
1247 $oldignore = wfIgnoreSQLErrors( true );
1249 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1251 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1252 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1253 # Most of the time (or on SQL errors), skip row count check
1254 wfIgnoreSQLErrors( $oldignore );
1258 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1259 $row = wfFetchObject( $res );
1260 $rown = intval( $row->n
);
1261 if( $rown >= $wgHitcounterUpdateFreq ){
1262 wfProfileIn( "Article::incViewCount-collect" );
1263 $old_user_abort = ignore_user_abort( true );
1265 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1266 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1267 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1268 "GROUP BY hc_id", DB_WRITE
);
1269 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1270 wfQuery("UNLOCK TABLES", DB_WRITE
);
1271 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1272 "WHERE cur_id = hc_id", DB_WRITE
);
1273 wfQuery("DROP TABLE acchits", DB_WRITE
);
1275 ignore_user_abort( $old_user_abort );
1276 wfProfileOut( "Article::incViewCount-collect" );
1278 wfIgnoreSQLErrors( $oldignore );
1281 # The onArticle*() functions are supposed to be a kind of hooks
1282 # which should be called whenever any of the specified actions
1285 # This is a good place to put code to clear caches, for instance.
1287 /* static */ function onArticleCreate($title_obj){
1288 global $wgEnablePersistentLC, $wgEnableParserCache;
1289 if ( $wgEnablePersistentLC ) {
1290 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1292 if ( $wgEnableParserCache ) {
1293 OutputPage
::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1297 /* static */ function onArticleDelete($title_obj){
1298 global $wgEnablePersistentLC, $wgEnableParserCache;
1299 if ( $wgEnablePersistentLC ) {
1300 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1302 if ( $wgEnableParserCache ) {
1303 OutputPage
::parsercacheClearLinksTo( $title_obj->getArticleID() );
1307 /* static */ function onArticleEdit($title_obj){
1308 global $wgEnablePersistentLC, $wgEnableParserCache;
1309 if ( $wgEnablePersistentLC ) {
1310 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1312 if ( $wgEnableParserCache ) {
1313 OutputPage
::parsercacheClearPage( $title_obj->getArticleID() );
1318 function wfReplaceSubstVar( $matches ) {
1319 return wfMsg( $matches[1] );