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;
24 function Article( &$title ) {
25 $this->mTitle
=& $title;
29 /* private */ function clear()
31 $this->mContentLoaded
= false;
32 $this->mUser
= $this->mCounter
= -1; # Not loaded
33 $this->mRedirectedFrom
= $this->mUserText
=
34 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
35 $this->mCountAdjustment
= 0;
36 $this->mTouched
= "19700101000000";
39 /* static */ function getRevisionText( $row, $prefix = "old_" ) {
40 # Deal with optional compression of archived pages.
41 # This can be done periodically via maintenance/compressOld.php, and
42 # as pages are saved if $wgCompressRevisions is set.
43 $text = $prefix . "text";
44 $flags = $prefix . "flags";
45 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, "gzip" ) ) ) {
46 return gzinflate( $row->$text );
48 if( isset( $row->$text ) ) {
54 /* static */ function compressRevisionText( &$text ) {
55 global $wgCompressRevisions;
56 if( !$wgCompressRevisions ) {
59 if( !function_exists( "gzdeflate" ) ) {
60 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
63 $text = gzdeflate( $text );
67 # Note that getContent/loadContent may follow redirects if
68 # not told otherwise, and so may cause a change to mTitle.
70 function getContent( $noredir = false )
72 global $action,$section,$count; # From query string
73 $fname = "Article::getContent";
74 wfProfileIn( $fname );
76 if ( 0 == $this->getID() ) {
77 if ( "edit" == $action ) {
78 wfProfileOut( $fname );
79 return ""; # was "newarticletext", now moved above the box)
81 wfProfileOut( $fname );
82 return wfMsg( "noarticletext" );
84 $this->loadContent( $noredir );
87 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
88 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
89 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
93 wfProfileOut( $fname );
94 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
99 wfProfileOut( $fname );
103 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
105 PREG_SPLIT_DELIM_CAPTURE
);
107 wfProfileOut( $fname );
108 return trim($secs[0]);
110 wfProfileOut( $fname );
111 return trim($secs[$section*2-1] . $secs[$section*2]);
115 wfProfileOut( $fname );
116 return $this->mContent
;
121 function loadContent( $noredir = false )
123 global $wgOut, $wgMwRedir;
124 global $oldid, $redirect; # From query
126 if ( $this->mContentLoaded
) return;
127 $fname = "Article::loadContent";
129 # Pre-fill content with error message so that if something
130 # fails we'll have something telling us what we intended.
132 $t = $this->mTitle
->getPrefixedText();
133 if ( isset( $oldid ) ) {
134 $oldid = IntVal( $oldid );
135 $t .= ",oldid={$oldid}";
137 if ( isset( $redirect ) ) {
138 $redirect = ($redirect == "no") ?
"no" : "yes";
139 $t .= ",redirect={$redirect}";
141 $this->mContent
= wfMsg( "missingarticle", $t );
143 if ( ! $oldid ) { # Retrieve current version
144 $id = $this->getID();
145 if ( 0 == $id ) return;
148 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
149 "FROM cur WHERE cur_id={$id}";
151 $res = wfQuery( $sql, DB_READ
, $fname );
152 if ( 0 == wfNumRows( $res ) ) {
156 $s = wfFetchObject( $res );
157 # If we got a redirect, follow it (unless we've been told
158 # not to by either the function parameter or the query
159 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
160 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
161 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
162 $s->cur_text
, $m ) ) {
163 $rt = Title
::newFromText( $m[1] );
165 # Gotta hand redirects to special pages differently:
166 # Fill the HTTP response "Location" header and ignore
167 # the rest of the page we're on.
169 if ( $rt->getInterwiki() != "" ) {
170 $wgOut->redirect( $rt->getFullURL() ) ;
173 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
174 $wgOut->redirect( wfLocalUrl(
175 $rt->getPrefixedURL() ) );
178 $rid = $rt->getArticleID();
180 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
181 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
182 $res = wfQuery( $sql, DB_READ
, $fname );
184 if ( 0 != wfNumRows( $res ) ) {
185 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
187 $s = wfFetchObject( $res );
194 $this->mContent
= $s->cur_text
;
195 $this->mUser
= $s->cur_user
;
196 $this->mCounter
= $s->cur_counter
;
197 $this->mTimestamp
= $s->cur_timestamp
;
198 $this->mTouched
= $s->cur_touched
;
199 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
200 $this->mTitle
->mRestrictionsLoaded
= true;
201 wfFreeResult( $res );
202 } else { # oldid set, retrieve historical version
203 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
204 "WHERE old_id={$oldid}";
205 $res = wfQuery( $sql, DB_READ
, $fname );
206 if ( 0 == wfNumRows( $res ) ) { return; }
208 $s = wfFetchObject( $res );
209 $this->mContent
= Article
::getRevisionText( $s );
210 $this->mUser
= $s->old_user
;
212 $this->mTimestamp
= $s->old_timestamp
;
213 wfFreeResult( $res );
215 $this->mContentLoaded
= true;
219 if( $this->mTitle
) {
220 return $this->mTitle
->getArticleID();
228 if ( -1 == $this->mCounter
) {
229 $id = $this->getID();
230 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
232 return $this->mCounter
;
235 # Would the given text make this article a "good" article (i.e.,
236 # suitable for including in the article count)?
238 function isCountable( $text )
240 global $wgUseCommaCount, $wgMwRedir;
242 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
243 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
244 $token = ($wgUseCommaCount ?
"," : "[[" );
245 if ( false === strstr( $text, $token ) ) { return 0; }
249 # Load the field related to the last edit time of the article.
250 # This isn't necessary for all uses, so it's only done if needed.
252 /* private */ function loadLastEdit()
255 if ( -1 != $this->mUser
) return;
257 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
258 "cur_comment,cur_minor_edit FROM cur WHERE " .
259 "cur_id=" . $this->getID();
260 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
262 if ( wfNumRows( $res ) > 0 ) {
263 $s = wfFetchObject( $res );
264 $this->mUser
= $s->cur_user
;
265 $this->mUserText
= $s->cur_user_text
;
266 $this->mTimestamp
= $s->cur_timestamp
;
267 $this->mComment
= $s->cur_comment
;
268 $this->mMinorEdit
= $s->cur_minor_edit
;
272 function getTimestamp()
274 $this->loadLastEdit();
275 return $this->mTimestamp
;
280 $this->loadLastEdit();
284 function getUserText()
286 $this->loadLastEdit();
287 return $this->mUserText
;
290 function getComment()
292 $this->loadLastEdit();
293 return $this->mComment
;
296 function getMinorEdit()
298 $this->loadLastEdit();
299 return $this->mMinorEdit
;
302 # This is the default action of the script: just view the page of
307 global $wgUser, $wgOut, $wgLang;
308 global $oldid, $diff; # From query
309 global $wgLinkCache, $IP;
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
);
358 $wgOut->addWikiText( $text );
360 $this->viewUpdates();
361 wfProfileOut( $fname );
364 # Theoretically we could defer these whole insert and update
365 # functions for after display, but that's taking a big leap
366 # of faith, and we want to be able to report database
367 # errors at some point.
369 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
371 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
372 global $wgEnablePersistentLC;
374 $fname = "Article::insertNewArticle";
376 $this->mCountAdjustment
= $this->isCountable( $text );
378 $ns = $this->mTitle
->getNamespace();
379 $ttl = $this->mTitle
->getDBkey();
380 $text = $this->preSaveTransform( $text );
381 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
384 $now = wfTimestampNow();
385 $won = wfInvertTimestamp( $now );
387 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
388 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
389 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
390 "cur_restrictions,cur_user_text,cur_is_redirect," .
391 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
392 wfStrencode( $text ) . "', '" .
393 wfStrencode( $summary ) . "', '" .
394 $wgUser->getID() . "', '{$now}', " .
395 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
396 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
397 $res = wfQuery( $sql, DB_WRITE
, $fname );
399 $newid = wfInsertId();
400 $this->mTitle
->resetArticleID( $newid );
402 if ( $wgEnablePersistentLC ) {
403 // Purge related entries in links cache on new page, to heal broken links
404 $ptitle = wfStrencode( $ttl );
405 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
406 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE
);
409 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
410 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
411 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
412 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
413 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
414 wfStrencode( $wgUser->getName() ) . "','" .
415 wfStrencode( $summary ) . "',0,0," .
416 ( $wgUser->isBot() ?
1 : 0 ) . ")";
417 wfQuery( $sql, DB_WRITE
, $fname );
419 if(!$this->mTitle
->userIsWatching()) $this->watch();
421 if ( $this->mTitle
->userIsWatching() ) {
426 # The talk page isn't in the regular link tables, so we need to update manually:
427 $talkns = $ns ^
1; # talk -> normal; normal -> talk
428 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
429 wfQuery( $sql, DB_WRITE
);
431 $this->showArticle( $text, wfMsg( "newarticle" ) );
434 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
436 global $wgOut, $wgUser, $wgLinkCache;
437 global $wgDBtransactions, $wgMwRedir;
438 $fname = "Article::updateArticle";
440 $this->loadLastEdit();
442 // insert updated section into old text if we have only edited part
444 if ($section != "") {
445 $oldtext=$this->getContent();
446 if($section=="new") {
447 if($summary) $subject="== {$summary} ==\n\n";
448 $text=$oldtext."\n\n".$subject.$text;
450 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
451 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
452 $secs[$section*2]=$text."\n\n"; // replace with edited
453 if($section) { $secs[$section*2-1]=""; } // erase old headline
454 $text=join("",$secs);
457 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
458 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
459 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
461 $text = $m[1] . "\n"; # Remove all content but redirect
465 $text = $this->preSaveTransform( $text );
467 # Update article, but only if changed.
469 if( $wgDBtransactions ) {
471 wfQuery( $sql, DB_WRITE
);
473 $oldtext = $this->getContent( true );
475 if ( 0 != strcmp( $text, $oldtext ) ) {
476 $this->mCountAdjustment
= $this->isCountable( $text )
477 - $this->isCountable( $oldtext );
479 $now = wfTimestampNow();
480 $won = wfInvertTimestamp( $now );
481 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
482 "',cur_comment='" . wfStrencode( $summary ) .
483 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
484 ",cur_timestamp='{$now}',cur_user_text='" .
485 wfStrencode( $wgUser->getName() ) .
486 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
487 "WHERE cur_id=" . $this->getID() .
488 " AND cur_timestamp='" . $this->getTimestamp() . "'";
489 $res = wfQuery( $sql, DB_WRITE
, $fname );
491 if( wfAffectedRows() == 0 ) {
492 /* Belated edit conflict! Run away!! */
496 # This overwrites $oldtext if revision compression is on
497 $flags = Article
::compressRevisionText( $oldtext );
499 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
500 "old_comment,old_user,old_user_text,old_timestamp," .
501 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
502 $this->mTitle
->getNamespace() . ", '" .
503 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
504 wfStrencode( $oldtext ) . "', '" .
505 wfStrencode( $this->getComment() ) . "', " .
506 $this->getUser() . ", '" .
507 wfStrencode( $this->getUserText() ) . "', '" .
508 $this->getTimestamp() . "', " . $me1 . ", '" .
509 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
510 $res = wfQuery( $sql, DB_WRITE
, $fname );
511 $oldid = wfInsertID( $res );
513 $bot = (int)($wgUser->isBot() ||
$forceBot);
515 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
516 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
517 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
518 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
519 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
520 "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
521 wfStrencode( $wgUser->getName() ) . "','" .
522 wfStrencode( $summary ) . "',0,{$oldid})";
523 wfQuery( $sql, DB_WRITE
, $fname );
525 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
526 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
527 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
528 "rc_timestamp='" . $this->getTimestamp() . "'";
529 wfQuery( $sql, DB_WRITE
, $fname );
531 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
532 "WHERE rc_cur_id=" . $this->getID();
533 wfQuery( $sql, DB_WRITE
, $fname );
535 global $wgEnablePersistentLC;
536 if ( $wgEnablePersistentLC ) {
537 // Purge link cache for this page
538 $pageid=$this->getID();
539 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE
);
543 if( $wgDBtransactions ) {
545 wfQuery( $sql, DB_WRITE
);
549 if (!$this->mTitle
->userIsWatching()) $this->watch();
551 if ( $this->mTitle
->userIsWatching() ) {
556 $this->showArticle( $text, wfMsg( "updated" ) );
560 # After we've either updated or inserted the article, update
561 # the link tables and redirect to the new page.
563 function showArticle( $text, $subtitle )
565 global $wgOut, $wgUser, $wgLinkCache;
568 $wgLinkCache = new LinkCache();
570 # Get old version of link table to allow incremental link updates
571 $wgLinkCache->preFill( $this->mTitle
);
572 $wgLinkCache->clear();
574 # Now update the link cache by parsing the text
575 $wgOut = new OutputPage();
576 $wgOut->addWikiText( $text );
578 $this->editUpdates( $text );
579 if( $wgMwRedir->matchStart( $text ) )
583 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
586 # Add this page to my watchlist
588 function watch( $add = true )
590 global $wgUser, $wgOut, $wgLang;
591 global $wgDeferredUpdateList;
593 if ( 0 == $wgUser->getID() ) {
594 $wgOut->errorpage( "watchnologin", "watchnologintext" );
597 if ( wfReadOnly() ) {
598 $wgOut->readOnlyPage();
602 $wgUser->addWatch( $this->mTitle
);
604 $wgUser->removeWatch( $this->mTitle
);
606 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
607 $wgOut->setRobotpolicy( "noindex,follow" );
609 $sk = $wgUser->getSkin() ;
610 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
613 $text = wfMsg( "addedwatchtext", $link );
615 $text = wfMsg( "removedwatchtext", $link );
616 $wgOut->addHTML( $text );
618 $up = new UserUpdate();
619 array_push( $wgDeferredUpdateList, $up );
621 $wgOut->returnToMain( false );
626 $this->watch( false );
629 function protect( $limit = "sysop" )
631 global $wgUser, $wgOut;
633 if ( ! $wgUser->isSysop() ) {
634 $wgOut->sysopRequired();
637 if ( wfReadOnly() ) {
638 $wgOut->readOnlyPage();
641 $id = $this->mTitle
->getArticleID();
643 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
646 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
647 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
648 wfQuery( $sql, DB_WRITE
, "Article::protect" );
650 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
651 if ( $limit === "" ) {
652 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
654 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
656 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
661 return $this->protect( "" );
666 global $wgUser, $wgOut, $wgMessageCache;
667 global $wpConfirm, $wpReason, $image, $oldimage;
669 # This code desperately needs to be totally rewritten
671 if ( ( ! $wgUser->isSysop() ) ) {
672 $wgOut->sysopRequired();
675 if ( wfReadOnly() ) {
676 $wgOut->readOnlyPage();
680 # Can't delete cached MediaWiki namespace (i.e. vital messages)
681 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
682 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
686 # Better double-check that it hasn't been deleted yet!
687 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
688 if ( ( "" == trim( $this->mTitle
->getText() ) )
689 or ( $this->mTitle
->getArticleId() == 0 ) ) {
690 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
694 if ( $_POST["wpConfirm"] ) {
699 # determine whether this page has earlier revisions
700 # and insert a warning if it does
701 # we select the text because it might be useful below
702 $ns = $this->mTitle
->getNamespace();
703 $title = $this->mTitle
->getDBkey();
704 $etitle = wfStrencode( $title );
705 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
706 $res = wfQuery( $sql, DB_READ
, $fname );
707 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
708 $skin=$wgUser->getSkin();
709 $wgOut->addHTML("<B>".wfMsg("historywarning"));
710 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
713 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
714 $res=wfQuery($sql, DB_READ
, $fname);
715 if( ($s=wfFetchObject($res))) {
717 # if this is a mini-text, we can paste part of it into the deletion reason
719 #if this is empty, an earlier revision may contain "useful" text
720 if($s->cur_text
!="") {
724 $text = Article
::getRevisionText( $old );
730 $length=strlen($text);
732 # this should not happen, since it is not possible to store an empty, new
733 # page. Let's insert a standard text in case it does, though
734 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
737 if($length < 500 && !$wpReason) {
739 # comment field=255, let's grep the first 150 to have some user
741 $text=substr($text,0,150);
742 # let's strip out newlines and HTML tags
743 $text=preg_replace("/\"/","'",$text);
744 $text=preg_replace("/\</","<",$text);
745 $text=preg_replace("/\>/",">",$text);
746 $text=preg_replace("/[\n\r]/","",$text);
748 $wpReason=wfMsg("excontent"). " '".$text;
750 $wpReason=wfMsg("exbeforeblank") . " '".$text;
752 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
757 return $this->confirmDelete();
760 function confirmDelete( $par = "" )
765 wfDebug( "Article::confirmDelete\n" );
767 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
768 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
769 $wgOut->setRobotpolicy( "noindex,nofollow" );
770 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
772 $t = $this->mTitle
->getPrefixedURL();
774 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
775 $confirm = wfMsg( "confirm" );
776 $check = wfMsg( "confirmcheck" );
777 $delcom = wfMsg( "deletecomment" );
780 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
781 <table border=0><tr><td align=right>
782 {$delcom}:</td><td align=left>
783 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
784 </td></tr><tr><td> </td></tr>
786 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
787 </td><td><label for=\"wpConfirm\">{$check}</label></td>
788 </tr><tr><td> </td><td>
789 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
790 </td></tr></table></form>\n" );
792 $wgOut->returnToMain( false );
797 global $wgOut, $wgUser, $wgLang;
799 $fname = "Article::doDelete";
800 wfDebug( "$fname\n" );
802 $this->doDeleteArticle( $this->mTitle
);
803 $deleted = $this->mTitle
->getPrefixedText();
805 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
806 $wgOut->setRobotpolicy( "noindex,nofollow" );
808 $sk = $wgUser->getSkin();
809 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
810 Namespace::getWikipedia() ) .
811 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
813 $text = wfMsg( "deletedtext", $deleted, $loglink );
815 $wgOut->addHTML( "<p>" . $text );
816 $wgOut->returnToMain( false );
819 function doDeleteArticle( $title )
821 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
822 $wgEnablePersistentLC;
824 $fname = "Article::doDeleteArticle";
825 wfDebug( "$fname\n" );
827 $ns = $title->getNamespace();
828 $t = wfStrencode( $title->getDBkey() );
829 $id = $title->getArticleID();
832 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
836 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
837 array_push( $wgDeferredUpdateList, $u );
839 # Move article and history to the "archive" table
840 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
841 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
842 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
843 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
844 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
845 wfQuery( $sql, DB_WRITE
, $fname );
847 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
848 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
849 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
850 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
851 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
852 wfQuery( $sql, DB_WRITE
, $fname );
854 # Now that it's safely backed up, delete it
856 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
858 wfQuery( $sql, DB_WRITE
, $fname );
860 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
862 wfQuery( $sql, DB_WRITE
, $fname );
864 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
866 wfQuery( $sql, DB_WRITE
, $fname );
868 # Finally, clean up the link tables
872 $t = wfStrencode( $title->getPrefixedDBkey() );
874 if ( $wgEnablePersistentLC ) {
875 // Purge related entries in links cache on delete,
876 wfQuery("DELETE linkscc FROM linkscc,links ".
877 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE
);
878 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE
);
881 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
882 $res = wfQuery( $sql, DB_READ
, $fname );
884 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
885 $now = wfTimestampNow();
886 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
889 while ( $s = wfFetchObject( $res ) ) {
890 $nt = Title
::newFromDBkey( $s->l_from
);
891 $lid = $nt->getArticleID();
893 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
895 $sql .= "({$lid},'{$t}')";
900 wfQuery( $sql, DB_WRITE
, $fname );
901 wfQuery( $sql2, DB_WRITE
, $fname );
903 wfFreeResult( $res );
905 $sql = "DELETE FROM links WHERE l_to={$id}";
906 wfQuery( $sql, DB_WRITE
, $fname );
908 $sql = "DELETE FROM links WHERE l_from='{$t}'";
909 wfQuery( $sql, DB_WRITE
, $fname );
911 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
912 wfQuery( $sql, DB_WRITE
, $fname );
914 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
915 wfQuery( $sql, DB_WRITE
, $fname );
918 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
919 $art = $title->getPrefixedText();
920 $wpReason = wfCleanQueryVar( $wpReason );
921 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
923 # Clear the cached article id so the interface doesn't act like we exist
924 $this->mTitle
->resetArticleID( 0 );
925 $this->mTitle
->mArticleID
= 0;
930 global $wgUser, $wgLang, $wgOut, $from;
932 if ( ! $wgUser->isSysop() ) {
933 $wgOut->sysopRequired();
936 if ( wfReadOnly() ) {
937 $wgOut->readOnlyPage( $this->getContent() );
941 # Secret enhanced rollback, marks edits rc_bot=1
942 $bot = !!$_REQUEST['bot'];
944 # Replace all this user's current edits with the next one down
945 $tt = wfStrencode( $this->mTitle
->getDBKey() );
946 $n = $this->mTitle
->getNamespace();
948 # Get the last editor
949 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
950 $res = wfQuery( $sql, DB_READ
);
951 if( ($x = wfNumRows( $res )) != 1 ) {
953 $wgOut->addHTML( wfMsg( "notanarticle" ) );
956 $s = wfFetchObject( $res );
957 $ut = wfStrencode( $s->cur_user_text
);
961 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
962 if( $from != $s->cur_user_text
) {
963 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
964 $wgOut->addWikiText( wfMsg( "alreadyrolled",
965 htmlspecialchars( $this->mTitle
->getPrefixedText()),
966 htmlspecialchars( $from ),
967 htmlspecialchars( $s->cur_user_text
) ) );
968 if($s->cur_comment
!= "") {
971 htmlspecialchars( $s->cur_comment
) ) );
976 # Get the last edit not by this guy
977 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
978 FROM old USE INDEX (name_title_timestamp)
979 WHERE old_namespace={$n} AND old_title='{$tt}'
980 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
981 ORDER BY inverse_timestamp LIMIT 1";
982 $res = wfQuery( $sql, DB_READ
);
983 if( wfNumRows( $res ) != 1 ) {
985 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
986 $wgOut->addHTML( wfMsg( "cantrollback" ) );
989 $s = wfFetchObject( $res );
992 # Mark all reverted edits as bot
993 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
994 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
995 wfQuery( $sql, DB_WRITE
, $fname );
999 $newcomment = wfMsg( "revertpage", $s->old_user_text
);
1000 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1001 $wgOut->setRobotpolicy( "noindex,nofollow" );
1002 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1003 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
1005 global $wgEnablePersistentLC;
1006 if ( $wgEnablePersistentLC ) {
1007 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
1011 $wgOut->returnToMain( false );
1015 # Do standard deferred updates after page view
1017 /* private */ function viewUpdates()
1019 global $wgDeferredUpdateList;
1020 if ( 0 != $this->getID() ) {
1021 global $wgDisableCounters;
1022 if( !$wgDisableCounters ) {
1023 Article
::incViewCount( $this->getID() );
1024 $u = new SiteStatsUpdate( 1, 0, 0 );
1025 array_push( $wgDeferredUpdateList, $u );
1027 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1028 $this->mTitle
->getDBkey() );
1029 array_push( $wgDeferredUpdateList, $u );
1033 # Do standard deferred updates after page edit.
1034 # Every 1000th edit, prune the recent changes table.
1036 /* private */ function editUpdates( $text )
1038 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1039 global $wgMessageCache;
1042 if ( 0 == mt_rand( 0, 999 ) ) {
1043 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1044 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1045 wfQuery( $sql, DB_WRITE
);
1047 $id = $this->getID();
1048 $title = $this->mTitle
->getPrefixedDBkey();
1049 $shortTitle = $this->mTitle
->getDBkey();
1051 $adj = $this->mCountAdjustment
;
1054 $u = new LinksUpdate( $id, $title );
1055 array_push( $wgDeferredUpdateList, $u );
1056 $u = new SiteStatsUpdate( 0, 1, $adj );
1057 array_push( $wgDeferredUpdateList, $u );
1058 $u = new SearchUpdate( $id, $title, $text );
1059 array_push( $wgDeferredUpdateList, $u );
1061 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1062 array_push( $wgDeferredUpdateList, $u );
1064 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1065 $wgMessageCache->replace( $shortTitle, $text );
1070 /* private */ function setOldSubtitle()
1072 global $wgLang, $wgOut;
1074 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1075 $r = wfMsg( "revisionasof", $td );
1076 $wgOut->setSubtitle( "({$r})" );
1079 # This function is called right before saving the wikitext,
1080 # so we can do things like signatures and links-in-context.
1082 function preSaveTransform( $text )
1085 while ( "" != $text ) {
1086 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1087 $s .= $this->pstPass2( $p[0] );
1089 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1091 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1092 $s .= "<nowiki>{$q[0]}</nowiki>";
1099 /* private */ function pstPass2( $text )
1101 global $wgUser, $wgLang, $wgLocaltimezone;
1105 $n = $wgUser->getName();
1106 $k = $wgUser->getOption( "nickname" );
1107 if ( "" == $k ) { $k = $n; }
1108 if(isset($wgLocaltimezone)) {
1109 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1111 /* Note: this is an ugly timezone hack for the European wikis */
1112 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1113 " (" . date( "T" ) . ")";
1114 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1116 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1117 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1118 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1119 Namespace::getUser() ) . ":$n|$k]]", $text );
1121 # Context links: [[|name]] and [[name (context)|]]
1123 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1124 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1125 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1126 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1128 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1129 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1130 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1131 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1132 # [[ns:page (cont)|]]
1134 $t = $this->mTitle
->getText();
1135 if ( preg_match( $conpat, $t, $m ) ) {
1138 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1139 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1140 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1142 if ( "" == $context ) {
1143 $text = preg_replace( $p2, "[[\\1]]", $text );
1145 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1148 # {{SUBST:xxx}} variables
1150 $mw =& MagicWord
::get( MAG_SUBST
);
1151 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1154 # Trim trailing whitespace
1155 # MAG_END (__END__) tag allows for trailing
1156 # whitespace to be deliberately included
1157 $text = rtrim( $text );
1158 $mw =& MagicWord::get( MAG_END );
1159 $mw->matchAndRemove( $text );
1164 /* Caching functions */
1166 # checkLastModified returns true iff it has taken care of all
1167 # output to the client that is necessary for this request.
1168 # (that is, it has sent a cached version of the page)
1169 function tryFileCache() {
1170 static $called = false;
1172 wfDebug( " tryFileCache() -- called twice!?\n" );
1176 if($this->isFileCacheable()) {
1177 $touched = $this->mTouched
;
1178 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1179 # Expire the main page quicker
1180 $expire = wfUnix2Timestamp( time() - 3600 );
1181 $touched = max( $expire, $touched );
1183 $cache = new CacheManager( $this->mTitle
);
1184 if($cache->isFileCacheGood( $touched )) {
1186 wfDebug( " tryFileCache() - about to load\n" );
1187 $cache->loadFromFileCache();
1190 wfDebug( " tryFileCache() - starting buffer\n" );
1191 if($cache->useGzip() && wfClientAcceptsGzip()) {
1192 /* For some reason, adding this header line over in
1193 CacheManager::saveToFileCache() fails on my test
1194 setup at home, though it works on the live install.
1195 Make double-sure... --brion */
1196 header( "Content-Encoding: gzip" );
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 );
1238 # Not important enough to warrant an error page in case of failure
1239 $oldignore = wfIgnoreSQLErrors( true );
1241 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1243 if( (rand() %
23 != 0) or (wfLastErrno() != 0) ){
1244 # Most of the time (or on SQL errors), skip row count check
1245 wfIgnoreSQLErrors( $oldignore );
1249 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1250 $row = wfFetchObject( $res );
1251 $rown = intval( $row->n
);
1252 if( $rown > 1000 ){ // update counters after ~1000 hits
1253 wfProfileIn( "Article::incViewCount-collect" );
1254 $old_user_abort = ignore_user_abort( true );
1256 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1257 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1258 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1259 "GROUP BY hc_id", DB_WRITE
);
1260 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1261 wfQuery("UNLOCK TABLES", DB_WRITE
);
1262 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1263 "WHERE cur_id = hc_id", DB_WRITE
);
1264 wfQuery("DROP TABLE acchits", DB_WRITE
);
1266 ignore_user_abort( $old_user_abort );
1267 wfProfileOut( "Article::incViewCount-collect" );
1269 wfIgnoreSQLErrors( $oldignore );
1273 function wfReplaceSubstVar( $matches ) {
1274 return wfMsg( $matches[1] );