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;
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
, $text );
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 $this->showArticle( $text, wfMsg( "newarticle" ) );
427 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
429 global $wgOut, $wgUser, $wgLinkCache;
430 global $wgDBtransactions, $wgMwRedir;
431 $fname = "Article::updateArticle";
433 $this->loadLastEdit();
435 // insert updated section into old text if we have only edited part
437 if ($section != "") {
438 $oldtext=$this->getContent();
439 if($section=="new") {
440 if($summary) $subject="== {$summary} ==\n\n";
441 $text=$oldtext."\n\n".$subject.$text;
443 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
444 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
445 $secs[$section*2]=$text."\n\n"; // replace with edited
446 if($section) { $secs[$section*2-1]=""; } // erase old headline
447 $text=join("",$secs);
450 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
451 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
452 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
454 $text = $m[1] . "\n"; # Remove all content but redirect
458 $text = $this->preSaveTransform( $text );
460 # Update article, but only if changed.
462 if( $wgDBtransactions ) {
464 wfQuery( $sql, DB_WRITE
);
466 $oldtext = $this->getContent( true );
468 if ( 0 != strcmp( $text, $oldtext ) ) {
469 $this->mCountAdjustment
= $this->isCountable( $text )
470 - $this->isCountable( $oldtext );
472 $now = wfTimestampNow();
473 $won = wfInvertTimestamp( $now );
474 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
475 "',cur_comment='" . wfStrencode( $summary ) .
476 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
477 ",cur_timestamp='{$now}',cur_user_text='" .
478 wfStrencode( $wgUser->getName() ) .
479 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
480 "WHERE cur_id=" . $this->getID() .
481 " AND cur_timestamp='" . $this->getTimestamp() . "'";
482 $res = wfQuery( $sql, DB_WRITE
, $fname );
484 if( wfAffectedRows() == 0 ) {
485 /* Belated edit conflict! Run away!! */
489 # This overwrites $oldtext if revision compression is on
490 $flags = Article
::compressRevisionText( $oldtext );
492 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
493 "old_comment,old_user,old_user_text,old_timestamp," .
494 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
495 $this->mTitle
->getNamespace() . ", '" .
496 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
497 wfStrencode( $oldtext ) . "', '" .
498 wfStrencode( $this->getComment() ) . "', " .
499 $this->getUser() . ", '" .
500 wfStrencode( $this->getUserText() ) . "', '" .
501 $this->getTimestamp() . "', " . $me1 . ", '" .
502 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
503 $res = wfQuery( $sql, DB_WRITE
, $fname );
504 $oldid = wfInsertID( $res );
506 $bot = (int)($wgUser->isBot() ||
$forceBot);
507 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
508 $oldid, $this->getTimestamp() );
509 Article
::onArticleEdit( $this->mTitle
);
512 if( $wgDBtransactions ) {
514 wfQuery( $sql, DB_WRITE
);
518 if (!$this->mTitle
->userIsWatching()) $this->watch();
520 if ( $this->mTitle
->userIsWatching() ) {
525 $this->showArticle( $text, wfMsg( "updated" ) );
529 # After we've either updated or inserted the article, update
530 # the link tables and redirect to the new page.
532 function showArticle( $text, $subtitle )
534 global $wgOut, $wgUser, $wgLinkCache;
537 $wgLinkCache = new LinkCache();
539 # Get old version of link table to allow incremental link updates
540 $wgLinkCache->preFill( $this->mTitle
);
541 $wgLinkCache->clear();
543 # Now update the link cache by parsing the text
544 $wgOut = new OutputPage();
545 $wgOut->addWikiText( $text );
547 # Every 1000th edit, prune the recent changes table.
549 if ( 0 == mt_rand( 0, 999 ) ) {
550 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
551 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
552 wfQuery( $sql, DB_WRITE
);
555 if( $wgMwRedir->matchStart( $text ) )
559 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
562 # Add this page to my watchlist
564 function watch( $add = true )
566 global $wgUser, $wgOut, $wgLang;
567 global $wgDeferredUpdateList;
569 if ( 0 == $wgUser->getID() ) {
570 $wgOut->errorpage( "watchnologin", "watchnologintext" );
573 if ( wfReadOnly() ) {
574 $wgOut->readOnlyPage();
578 $wgUser->addWatch( $this->mTitle
);
580 $wgUser->removeWatch( $this->mTitle
);
582 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
583 $wgOut->setRobotpolicy( "noindex,follow" );
585 $sk = $wgUser->getSkin() ;
586 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
589 $text = wfMsg( "addedwatchtext", $link );
591 $text = wfMsg( "removedwatchtext", $link );
592 $wgOut->addHTML( $text );
594 $up = new UserUpdate();
595 array_push( $wgDeferredUpdateList, $up );
597 $wgOut->returnToMain( false );
602 $this->watch( false );
605 function protect( $limit = "sysop" )
607 global $wgUser, $wgOut;
609 if ( ! $wgUser->isSysop() ) {
610 $wgOut->sysopRequired();
613 if ( wfReadOnly() ) {
614 $wgOut->readOnlyPage();
617 $id = $this->mTitle
->getArticleID();
619 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
622 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
623 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
624 wfQuery( $sql, DB_WRITE
, "Article::protect" );
626 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
627 if ( $limit === "" ) {
628 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
630 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
632 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
637 return $this->protect( "" );
642 global $wgUser, $wgOut, $wgMessageCache;
643 global $wpConfirm, $wpReason, $image, $oldimage;
645 # This code desperately needs to be totally rewritten
647 if ( ( ! $wgUser->isSysop() ) ) {
648 $wgOut->sysopRequired();
651 if ( wfReadOnly() ) {
652 $wgOut->readOnlyPage();
656 # Can't delete cached MediaWiki namespace (i.e. vital messages)
657 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
658 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
662 # Better double-check that it hasn't been deleted yet!
663 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
664 if ( ( "" == trim( $this->mTitle
->getText() ) )
665 or ( $this->mTitle
->getArticleId() == 0 ) ) {
666 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
670 if ( $_POST["wpConfirm"] ) {
675 # determine whether this page has earlier revisions
676 # and insert a warning if it does
677 # we select the text because it might be useful below
678 $ns = $this->mTitle
->getNamespace();
679 $title = $this->mTitle
->getDBkey();
680 $etitle = wfStrencode( $title );
681 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
682 $res = wfQuery( $sql, DB_READ
, $fname );
683 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
684 $skin=$wgUser->getSkin();
685 $wgOut->addHTML("<B>".wfMsg("historywarning"));
686 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
689 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
690 $res=wfQuery($sql, DB_READ
, $fname);
691 if( ($s=wfFetchObject($res))) {
693 # if this is a mini-text, we can paste part of it into the deletion reason
695 #if this is empty, an earlier revision may contain "useful" text
696 if($s->cur_text
!="") {
700 $text = Article
::getRevisionText( $old );
706 $length=strlen($text);
708 # this should not happen, since it is not possible to store an empty, new
709 # page. Let's insert a standard text in case it does, though
710 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
713 if($length < 500 && !$wpReason) {
715 # comment field=255, let's grep the first 150 to have some user
717 $text=substr($text,0,150);
718 # let's strip out newlines and HTML tags
719 $text=preg_replace("/\"/","'",$text);
720 $text=preg_replace("/\</","<",$text);
721 $text=preg_replace("/\>/",">",$text);
722 $text=preg_replace("/[\n\r]/","",$text);
724 $wpReason=wfMsg("excontent"). " '".$text;
726 $wpReason=wfMsg("exbeforeblank") . " '".$text;
728 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
733 return $this->confirmDelete();
736 function confirmDelete( $par = "" )
741 wfDebug( "Article::confirmDelete\n" );
743 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
744 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
745 $wgOut->setRobotpolicy( "noindex,nofollow" );
746 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
748 $t = $this->mTitle
->getPrefixedURL();
750 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
751 $confirm = wfMsg( "confirm" );
752 $check = wfMsg( "confirmcheck" );
753 $delcom = wfMsg( "deletecomment" );
756 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
757 <table border=0><tr><td align=right>
758 {$delcom}:</td><td align=left>
759 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
760 </td></tr><tr><td> </td></tr>
762 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
763 </td><td><label for=\"wpConfirm\">{$check}</label></td>
764 </tr><tr><td> </td><td>
765 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
766 </td></tr></table></form>\n" );
768 $wgOut->returnToMain( false );
773 global $wgOut, $wgUser, $wgLang;
775 $fname = "Article::doDelete";
776 wfDebug( "$fname\n" );
778 $this->doDeleteArticle( $this->mTitle
);
779 $deleted = $this->mTitle
->getPrefixedText();
781 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
782 $wgOut->setRobotpolicy( "noindex,nofollow" );
784 $sk = $wgUser->getSkin();
785 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
786 Namespace::getWikipedia() ) .
787 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
789 $text = wfMsg( "deletedtext", $deleted, $loglink );
791 $wgOut->addHTML( "<p>" . $text );
792 $wgOut->returnToMain( false );
795 function doDeleteArticle( $title )
797 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
799 $fname = "Article::doDeleteArticle";
800 wfDebug( "$fname\n" );
802 $ns = $title->getNamespace();
803 $t = wfStrencode( $title->getDBkey() );
804 $id = $title->getArticleID();
807 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
811 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
812 array_push( $wgDeferredUpdateList, $u );
814 # Move article and history to the "archive" table
815 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
816 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
817 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
818 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
819 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
820 wfQuery( $sql, DB_WRITE
, $fname );
822 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
823 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
824 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
825 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
826 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
827 wfQuery( $sql, DB_WRITE
, $fname );
829 # Now that it's safely backed up, delete it
831 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
833 wfQuery( $sql, DB_WRITE
, $fname );
835 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
837 wfQuery( $sql, DB_WRITE
, $fname );
839 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
841 wfQuery( $sql, DB_WRITE
, $fname );
843 # Finally, clean up the link tables
847 $t = wfStrencode( $title->getPrefixedDBkey() );
849 Article
::onArticleDelete( $title );
851 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
852 $res = wfQuery( $sql, DB_READ
, $fname );
854 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
855 $now = wfTimestampNow();
856 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
859 while ( $s = wfFetchObject( $res ) ) {
860 $nt = Title
::newFromDBkey( $s->l_from
);
861 $lid = $nt->getArticleID();
863 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
865 $sql .= "({$lid},'{$t}')";
870 wfQuery( $sql, DB_WRITE
, $fname );
871 wfQuery( $sql2, DB_WRITE
, $fname );
873 wfFreeResult( $res );
875 $sql = "DELETE FROM links WHERE l_to={$id}";
876 wfQuery( $sql, DB_WRITE
, $fname );
878 $sql = "DELETE FROM links WHERE l_from='{$t}'";
879 wfQuery( $sql, DB_WRITE
, $fname );
881 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
882 wfQuery( $sql, DB_WRITE
, $fname );
884 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
885 wfQuery( $sql, DB_WRITE
, $fname );
888 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
889 $art = $title->getPrefixedText();
890 $wpReason = wfCleanQueryVar( $wpReason );
891 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
893 # Clear the cached article id so the interface doesn't act like we exist
894 $this->mTitle
->resetArticleID( 0 );
895 $this->mTitle
->mArticleID
= 0;
900 global $wgUser, $wgLang, $wgOut, $from;
902 if ( ! $wgUser->isSysop() ) {
903 $wgOut->sysopRequired();
906 if ( wfReadOnly() ) {
907 $wgOut->readOnlyPage( $this->getContent() );
911 # Enhanced rollback, marks edits rc_bot=1
912 $bot = !!$_REQUEST['bot'];
914 # Replace all this user's current edits with the next one down
915 $tt = wfStrencode( $this->mTitle
->getDBKey() );
916 $n = $this->mTitle
->getNamespace();
918 # Get the last editor
919 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
920 $res = wfQuery( $sql, DB_READ
);
921 if( ($x = wfNumRows( $res )) != 1 ) {
923 $wgOut->addHTML( wfMsg( "notanarticle" ) );
926 $s = wfFetchObject( $res );
927 $ut = wfStrencode( $s->cur_user_text
);
931 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
932 if( $from != $s->cur_user_text
) {
933 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
934 $wgOut->addWikiText( wfMsg( "alreadyrolled",
935 htmlspecialchars( $this->mTitle
->getPrefixedText()),
936 htmlspecialchars( $from ),
937 htmlspecialchars( $s->cur_user_text
) ) );
938 if($s->cur_comment
!= "") {
941 htmlspecialchars( $s->cur_comment
) ) );
946 # Get the last edit not by this guy
947 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
948 FROM old USE INDEX (name_title_timestamp)
949 WHERE old_namespace={$n} AND old_title='{$tt}'
950 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
951 ORDER BY inverse_timestamp LIMIT 1";
952 $res = wfQuery( $sql, DB_READ
);
953 if( wfNumRows( $res ) != 1 ) {
955 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
956 $wgOut->addHTML( wfMsg( "cantrollback" ) );
959 $s = wfFetchObject( $res );
962 # Mark all reverted edits as bot
963 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
964 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
965 wfQuery( $sql, DB_WRITE
, $fname );
969 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
970 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
971 $wgOut->setRobotpolicy( "noindex,nofollow" );
972 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
973 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
975 Article
::onArticleEdit( $this->mTitle
);
976 $wgOut->returnToMain( false );
980 # Do standard deferred updates after page view
982 /* private */ function viewUpdates()
984 global $wgDeferredUpdateList;
985 if ( 0 != $this->getID() ) {
986 global $wgDisableCounters;
987 if( !$wgDisableCounters ) {
988 Article
::incViewCount( $this->getID() );
989 $u = new SiteStatsUpdate( 1, 0, 0 );
990 array_push( $wgDeferredUpdateList, $u );
992 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
993 $this->mTitle
->getDBkey() );
994 array_push( $wgDeferredUpdateList, $u );
1000 /* private */ function setOldSubtitle()
1002 global $wgLang, $wgOut;
1004 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1005 $r = wfMsg( "revisionasof", $td );
1006 $wgOut->setSubtitle( "({$r})" );
1009 # This function is called right before saving the wikitext,
1010 # so we can do things like signatures and links-in-context.
1012 function preSaveTransform( $text )
1015 while ( "" != $text ) {
1016 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1017 $s .= $this->pstPass2( $p[0] );
1019 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1021 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1022 $s .= "<nowiki>{$q[0]}</nowiki>";
1029 /* private */ function pstPass2( $text )
1031 global $wgUser, $wgLang, $wgLocaltimezone;
1035 $n = $wgUser->getName();
1036 $k = $wgUser->getOption( "nickname" );
1037 if ( "" == $k ) { $k = $n; }
1038 if(isset($wgLocaltimezone)) {
1039 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1041 /* Note: this is an ugly timezone hack for the European wikis */
1042 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1043 " (" . date( "T" ) . ")";
1044 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1046 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1047 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1048 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1049 Namespace::getUser() ) . ":$n|$k]]", $text );
1051 # Context links: [[|name]] and [[name (context)|]]
1053 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1054 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1055 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1056 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1058 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1059 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1060 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1061 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1062 # [[ns:page (cont)|]]
1064 $t = $this->mTitle
->getText();
1065 if ( preg_match( $conpat, $t, $m ) ) {
1068 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1069 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1070 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1072 if ( "" == $context ) {
1073 $text = preg_replace( $p2, "[[\\1]]", $text );
1075 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1078 # {{SUBST:xxx}} variables
1080 $mw =& MagicWord
::get( MAG_SUBST
);
1081 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1084 # Trim trailing whitespace
1085 # MAG_END (__END__) tag allows for trailing
1086 # whitespace to be deliberately included
1087 $text = rtrim( $text );
1088 $mw =& MagicWord::get( MAG_END );
1089 $mw->matchAndRemove( $text );
1094 /* Caching functions */
1096 # checkLastModified returns true iff it has taken care of all
1097 # output to the client that is necessary for this request.
1098 # (that is, it has sent a cached version of the page)
1099 function tryFileCache() {
1100 static $called = false;
1102 wfDebug( " tryFileCache() -- called twice!?\n" );
1106 if($this->isFileCacheable()) {
1107 $touched = $this->mTouched
;
1108 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1109 # Expire the main page quicker
1110 $expire = wfUnix2Timestamp( time() - 3600 );
1111 $touched = max( $expire, $touched );
1113 $cache = new CacheManager( $this->mTitle
);
1114 if($cache->isFileCacheGood( $touched )) {
1116 wfDebug( " tryFileCache() - about to load\n" );
1117 $cache->loadFromFileCache();
1120 wfDebug( " tryFileCache() - starting buffer\n" );
1121 ob_start( array(&$cache, 'saveToFileCache' ) );
1124 wfDebug( " tryFileCache() - not cacheable\n" );
1128 function isFileCacheable() {
1129 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1130 global $action, $oldid, $diff, $redirect, $printable;
1131 return $wgUseFileCache
1132 and (!$wgShowIPinHeader)
1133 and ($this->getID() != 0)
1134 and ($wgUser->getId() == 0)
1135 and (!$wgUser->getNewtalk())
1136 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1137 and ($action == "view")
1138 and (!isset($oldid))
1140 and (!isset($redirect))
1141 and (!isset($printable))
1142 and (!$this->mRedirectedFrom
);
1145 function checkTouched() {
1146 $id = $this->getID();
1147 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1148 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1149 if( $s = wfFetchObject( $res ) ) {
1150 $this->mTouched
= $s->cur_touched
;
1151 return !$s->cur_is_redirect
;
1157 /* static */ function incViewCount( $id )
1159 $id = intval( $id );
1160 global $wgHitcounterUpdateFreq;
1162 if( $wgHitcounterUpdateFreq <= 1 ){ //
1163 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1164 "WHERE cur_id = $id", DB_WRITE
);
1168 # Not important enough to warrant an error page in case of failure
1169 $oldignore = wfIgnoreSQLErrors( true );
1171 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1173 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1174 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1175 # Most of the time (or on SQL errors), skip row count check
1176 wfIgnoreSQLErrors( $oldignore );
1180 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1181 $row = wfFetchObject( $res );
1182 $rown = intval( $row->n
);
1183 if( $rown >= $wgHitcounterUpdateFreq ){
1184 wfProfileIn( "Article::incViewCount-collect" );
1185 $old_user_abort = ignore_user_abort( true );
1187 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1188 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1189 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1190 "GROUP BY hc_id", DB_WRITE
);
1191 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1192 wfQuery("UNLOCK TABLES", DB_WRITE
);
1193 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1194 "WHERE cur_id = hc_id", DB_WRITE
);
1195 wfQuery("DROP TABLE acchits", DB_WRITE
);
1197 ignore_user_abort( $old_user_abort );
1198 wfProfileOut( "Article::incViewCount-collect" );
1200 wfIgnoreSQLErrors( $oldignore );
1203 # The onArticle*() functions are supposed to be a kind of hooks
1204 # which should be called whenever any of the specified actions
1207 # This is a good place to put code to clear caches, for instance.
1209 /* static */ function onArticleCreate($title_obj,$text=''){
1210 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid;
1211 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1212 global $wgMessageCache, $wgInternalServer;
1213 # Do standard deferred updates after page edit.
1214 $id = $title_obj->getArticleID();
1215 $title = $title_obj->getPrefixedDBkey();
1216 $shortTitle = $title_obj->getDBkey();
1218 $adj = $this->mCountAdjustment
;
1221 $u = new LinksUpdate( $id, $title );
1222 array_push( $wgDeferredUpdateList, $u );
1223 $u = new SiteStatsUpdate( 0, 1, $adj );
1224 array_push( $wgDeferredUpdateList, $u );
1225 $u = new SearchUpdate( $id, $title, $text );
1226 array_push( $wgDeferredUpdateList, $u );
1228 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1229 array_push( $wgDeferredUpdateList, $u );
1231 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1232 $wgMessageCache->replace( $shortTitle, $text );
1235 if ( $wgEnablePersistentLC ) {
1236 LinkCache
::linksccClearBrokenLinksTo( $title );
1238 if ( $wgEnableParserCache ) {
1239 OutputPage
::parsercacheClearBrokenLinksTo( $title );
1241 if ( $wgUseSquid ) {
1243 $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL())
1245 wfPurgeSquidServers($urlArr);
1246 /* this needs to be done after LinksUpdate */
1247 $u = new SquidUpdate($title_obj);
1248 array_push( $wgDeferredUpdateList, $u );
1252 /* static */ function onArticleDelete($title_obj,$text=''){
1253 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
1254 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1255 global $wgMessageCache, $wgInternalServer;
1257 $id = $title_obj->getArticleID();
1258 $title = $title_obj->getPrefixedDBkey();
1259 $shortTitle = $title_obj->getDBkey();
1261 if ( $wgEnablePersistentLC ) {
1262 LinkCache
::linksccClearLinksTo( $id );
1264 if ( $wgEnableParserCache ) {
1265 OutputPage
::parsercacheClearLinksTo( $id );
1267 if ( $wgUseSquid ) {
1269 $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL())
1271 wfPurgeSquidServers($urlArr);
1273 /* prepare the list of urls to purge */
1274 $sql = "SELECT l_from FROM links WHERE l_to={$id}" ;
1275 $res = wfQuery ( $sql, DB_READ
) ;
1276 while ( $BL = wfFetchObject ( $res ) )
1278 $t = Title
::newFromDBkey( $BL->l_from
) ;
1279 $blurlArr[] = $wgInternalServer.wfLocalUrl( $t->getPrefixedURL() );
1281 wfFreeResult ( $res ) ;
1282 $u = new SquidUpdate( $title_obj, $blurlArr );
1283 array_push( $wgDeferredUpdateList, $u );
1288 /* static */ function onArticleEdit($title_obj,$text=''){
1289 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid;
1290 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1291 global $wgMessageCache, $wgInternalServer;
1293 $id = $title_obj->getArticleID();
1294 $title = $title_obj->getPrefixedDBkey();
1295 $shortTitle = $title_obj->getDBkey();
1297 $adj = $this->mCountAdjustment
;
1300 $u = new LinksUpdate( $id, $title );
1301 array_push( $wgDeferredUpdateList, $u );
1302 $u = new SiteStatsUpdate( 0, 1, $adj );
1303 array_push( $wgDeferredUpdateList, $u );
1304 $u = new SearchUpdate( $id, $title, $text );
1305 array_push( $wgDeferredUpdateList, $u );
1307 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1308 array_push( $wgDeferredUpdateList, $u );
1310 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1311 $wgMessageCache->replace( $shortTitle, $text );
1314 if ( $wgEnablePersistentLC ) {
1315 LinkCache
::linksccClearPage( $id );
1317 if ( $wgEnableParserCache ) {
1318 OutputPage
::parsercacheClearPage( $id );
1320 if ( $wgUseSquid ) {
1322 $wgInternalServer.wfLocalUrl( $title_obj->getPrefixedURL()),
1324 wfPurgeSquidServers($urlArr);
1329 function wfReplaceSubstVar( $matches ) {
1330 return wfMsg( $matches[1] );