fce6c590552627054d065bafdb1e3d56512d91f8
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 # Note that getContent/loadContent may follow redirects if
40 # not told otherwise, and so may cause a change to mTitle.
42 function getContent( $noredir = false )
44 global $action,$section,$count; # From query string
45 $fname = "Article::getContent";
46 wfProfileIn( $fname );
48 if ( 0 == $this->getID() ) {
49 if ( "edit" == $action ) {
50 wfProfileOut( $fname );
51 return ""; # was "newarticletext", now moved above the box)
53 wfProfileOut( $fname );
54 return wfMsg( "noarticletext" );
56 $this->loadContent( $noredir );
59 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
60 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
61 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
65 wfProfileOut( $fname );
66 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
71 wfProfileOut( $fname );
75 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
77 PREG_SPLIT_DELIM_CAPTURE
);
79 wfProfileOut( $fname );
80 return trim($secs[0]);
82 wfProfileOut( $fname );
83 return trim($secs[$section*2-1] . $secs[$section*2]);
87 wfProfileOut( $fname );
88 return $this->mContent
;
93 function loadContent( $noredir = false )
95 global $wgOut, $wgMwRedir;
96 global $oldid, $redirect; # From query
98 if ( $this->mContentLoaded
) return;
99 $fname = "Article::loadContent";
101 # Pre-fill content with error message so that if something
102 # fails we'll have something telling us what we intended.
104 $t = $this->mTitle
->getPrefixedText();
105 if ( isset( $oldid ) ) {
106 $oldid = IntVal( $oldid );
107 $t .= ",oldid={$oldid}";
109 if ( isset( $redirect ) ) {
110 $redirect = ($redirect == "no") ?
"no" : "yes";
111 $t .= ",redirect={$redirect}";
113 $this->mContent
= wfMsg( "missingarticle", $t );
115 if ( ! $oldid ) { # Retrieve current version
116 $id = $this->getID();
117 if ( 0 == $id ) return;
120 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
121 "FROM cur WHERE cur_id={$id}";
123 $res = wfQuery( $sql, DB_READ
, $fname );
124 if ( 0 == wfNumRows( $res ) ) {
128 $s = wfFetchObject( $res );
129 # If we got a redirect, follow it (unless we've been told
130 # not to by either the function parameter or the query
131 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
132 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
133 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
134 $s->cur_text
, $m ) ) {
135 $rt = Title
::newFromText( $m[1] );
137 # Gotta hand redirects to special pages differently:
138 # Fill the HTTP response "Location" header and ignore
139 # the rest of the page we're on.
141 if ( $rt->getInterwiki() != "" ) {
142 $wgOut->redirect( $rt->getFullURL() ) ;
145 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
146 $wgOut->redirect( wfLocalUrl(
147 $rt->getPrefixedURL() ) );
150 $rid = $rt->getArticleID();
152 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
153 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
154 $res = wfQuery( $sql, DB_READ
, $fname );
156 if ( 0 != wfNumRows( $res ) ) {
157 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
159 $s = wfFetchObject( $res );
166 $this->mContent
= $s->cur_text
;
167 $this->mUser
= $s->cur_user
;
168 $this->mCounter
= $s->cur_counter
;
169 $this->mTimestamp
= $s->cur_timestamp
;
170 $this->mTouched
= $s->cur_touched
;
171 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
172 $this->mTitle
->mRestrictionsLoaded
= true;
173 wfFreeResult( $res );
174 } else { # oldid set, retrieve historical version
175 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
176 "WHERE old_id={$oldid}";
177 $res = wfQuery( $sql, DB_READ
, $fname );
178 if ( 0 == wfNumRows( $res ) ) { return; }
180 $s = wfFetchObject( $res );
181 $this->mContent
= $s->old_text
;
182 $this->mUser
= $s->old_user
;
184 $this->mTimestamp
= $s->old_timestamp
;
185 wfFreeResult( $res );
187 $this->mContentLoaded
= true;
191 if( $this->mTitle
) {
192 return $this->mTitle
->getArticleID();
200 if ( -1 == $this->mCounter
) {
201 $id = $this->getID();
202 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
204 return $this->mCounter
;
207 # Would the given text make this article a "good" article (i.e.,
208 # suitable for including in the article count)?
210 function isCountable( $text )
212 global $wgUseCommaCount, $wgMwRedir;
214 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
215 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
216 $token = ($wgUseCommaCount ?
"," : "[[" );
217 if ( false === strstr( $text, $token ) ) { return 0; }
221 # Load the field related to the last edit time of the article.
222 # This isn't necessary for all uses, so it's only done if needed.
224 /* private */ function loadLastEdit()
227 if ( -1 != $this->mUser
) return;
229 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
230 "cur_comment,cur_minor_edit FROM cur WHERE " .
231 "cur_id=" . $this->getID();
232 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
234 if ( wfNumRows( $res ) > 0 ) {
235 $s = wfFetchObject( $res );
236 $this->mUser
= $s->cur_user
;
237 $this->mUserText
= $s->cur_user_text
;
238 $this->mTimestamp
= $s->cur_timestamp
;
239 $this->mComment
= $s->cur_comment
;
240 $this->mMinorEdit
= $s->cur_minor_edit
;
244 function getTimestamp()
246 $this->loadLastEdit();
247 return $this->mTimestamp
;
252 $this->loadLastEdit();
256 function getUserText()
258 $this->loadLastEdit();
259 return $this->mUserText
;
262 function getComment()
264 $this->loadLastEdit();
265 return $this->mComment
;
268 function getMinorEdit()
270 $this->loadLastEdit();
271 return $this->mMinorEdit
;
274 # This is the default action of the script: just view the page of
279 global $wgUser, $wgOut, $wgLang;
280 global $oldid, $diff; # From query
281 global $wgLinkCache, $IP;
282 $fname = "Article::view";
283 wfProfileIn( $fname );
285 $wgOut->setArticleFlag( true );
286 $wgOut->setRobotpolicy( "index,follow" );
288 # If we got diff and oldid in the query, we want to see a
289 # diff page instead of the article.
291 if ( isset( $diff ) ) {
292 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
293 $de = new DifferenceEngine( $oldid, $diff );
295 wfProfileOut( $fname );
299 if ( !isset( $oldid ) and $this->checkTouched() ) {
300 if( $wgOut->checkLastModified( $this->mTouched
) ){
302 } else if ( $this->tryFileCache() ) {
303 # tell wgOut that output is taken care of
305 $this->viewUpdates();
310 $text = $this->getContent(); # May change mTitle
311 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
312 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
313 " - " . wfMsg( "wikititlesuffix" ) );
315 # We're looking at an old revision
318 $this->setOldSubtitle();
319 $wgOut->setRobotpolicy( "noindex,follow" );
321 if ( "" != $this->mRedirectedFrom
) {
322 $sk = $wgUser->getSkin();
323 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
325 $s = wfMsg( "redirectedfrom", $redir );
326 $wgOut->setSubtitle( $s );
329 $wgLinkCache->preFill( $this->mTitle
);
330 $wgOut->addWikiText( $text );
332 $this->viewUpdates();
333 wfProfileOut( $fname );
336 # Theoretically we could defer these whole insert and update
337 # functions for after display, but that's taking a big leap
338 # of faith, and we want to be able to report database
339 # errors at some point.
341 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
343 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
344 global $wgEnablePersistentLC;
346 $fname = "Article::insertNewArticle";
348 $this->mCountAdjustment
= $this->isCountable( $text );
350 $ns = $this->mTitle
->getNamespace();
351 $ttl = $this->mTitle
->getDBkey();
352 $text = $this->preSaveTransform( $text );
353 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
356 $now = wfTimestampNow();
357 $won = wfInvertTimestamp( $now );
359 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
360 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
361 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
362 "cur_restrictions,cur_user_text,cur_is_redirect," .
363 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
364 wfStrencode( $text ) . "', '" .
365 wfStrencode( $summary ) . "', '" .
366 $wgUser->getID() . "', '{$now}', " .
367 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
368 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
369 $res = wfQuery( $sql, DB_WRITE
, $fname );
371 $newid = wfInsertId();
372 $this->mTitle
->resetArticleID( $newid );
374 if ( $wgEnablePersistentLC ) {
375 // Purge related entries in links cache on new page, to heal broken links
376 $ptitle = wfStrencode( $ttl );
377 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
378 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE
);
381 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
382 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
383 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
384 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
385 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
386 wfStrencode( $wgUser->getName() ) . "','" .
387 wfStrencode( $summary ) . "',0,0," .
388 ( $wgUser->isBot() ?
1 : 0 ) . ")";
389 wfQuery( $sql, DB_WRITE
, $fname );
391 if(!$this->mTitle
->userIsWatching()) $this->watch();
393 if ( $this->mTitle
->userIsWatching() ) {
398 # The talk page isn't in the regular link tables, so we need to update manually:
399 $talkns = $ns ^
1; # talk -> normal; normal -> talk
400 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
401 wfQuery( $sql, DB_WRITE
);
403 $this->showArticle( $text, wfMsg( "newarticle" ) );
406 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
408 global $wgOut, $wgUser, $wgLinkCache;
409 global $wgDBtransactions, $wgMwRedir;
410 $fname = "Article::updateArticle";
412 $this->loadLastEdit();
414 // insert updated section into old text if we have only edited part
416 if ($section != "") {
417 $oldtext=$this->getContent();
418 if($section=="new") {
419 if($summary) $subject="== {$summary} ==\n\n";
420 $text=$oldtext."\n\n".$subject.$text;
422 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
423 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
424 $secs[$section*2]=$text."\n\n"; // replace with edited
425 if($section) { $secs[$section*2-1]=""; } // erase old headline
426 $text=join("",$secs);
429 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
430 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
431 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
433 $text = $m[1] . "\n"; # Remove all content but redirect
437 $text = $this->preSaveTransform( $text );
439 # Update article, but only if changed.
441 if( $wgDBtransactions ) {
443 wfQuery( $sql, DB_WRITE
);
445 $oldtext = $this->getContent( true );
447 if ( 0 != strcmp( $text, $oldtext ) ) {
448 $this->mCountAdjustment
= $this->isCountable( $text )
449 - $this->isCountable( $oldtext );
451 $now = wfTimestampNow();
452 $won = wfInvertTimestamp( $now );
453 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
454 "',cur_comment='" . wfStrencode( $summary ) .
455 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
456 ",cur_timestamp='{$now}',cur_user_text='" .
457 wfStrencode( $wgUser->getName() ) .
458 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
459 "WHERE cur_id=" . $this->getID() .
460 " AND cur_timestamp='" . $this->getTimestamp() . "'";
461 $res = wfQuery( $sql, DB_WRITE
, $fname );
463 if( wfAffectedRows() == 0 ) {
464 /* Belated edit conflict! Run away!! */
468 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
469 "old_comment,old_user,old_user_text,old_timestamp," .
470 "old_minor_edit,inverse_timestamp) VALUES (" .
471 $this->mTitle
->getNamespace() . ", '" .
472 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
473 wfStrencode( $oldtext ) . "', '" .
474 wfStrencode( $this->getComment() ) . "', " .
475 $this->getUser() . ", '" .
476 wfStrencode( $this->getUserText() ) . "', '" .
477 $this->getTimestamp() . "', " . $me1 . ", '" .
478 wfInvertTimestamp( $this->getTimestamp() ) . "')";
479 $res = wfQuery( $sql, DB_WRITE
, $fname );
480 $oldid = wfInsertID( $res );
482 $bot = (int)($wgUser->isBot() ||
$forceBot);
484 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
485 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
486 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
487 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
488 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
489 "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
490 wfStrencode( $wgUser->getName() ) . "','" .
491 wfStrencode( $summary ) . "',0,{$oldid})";
492 wfQuery( $sql, DB_WRITE
, $fname );
494 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
495 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
496 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
497 "rc_timestamp='" . $this->getTimestamp() . "'";
498 wfQuery( $sql, DB_WRITE
, $fname );
500 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
501 "WHERE rc_cur_id=" . $this->getID();
502 wfQuery( $sql, DB_WRITE
, $fname );
504 global $wgEnablePersistentLC;
505 if ( $wgEnablePersistentLC ) {
506 // Purge link cache for this page
507 $pageid=$this->getID();
508 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE
);
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, $wgUseBetterLinksUpdate;
537 $wgLinkCache = new LinkCache();
539 # Get old version of link table to allow incremental link updates
540 if ( $wgUseBetterLinksUpdate ) {
541 $wgLinkCache->preFill( $this->mTitle
);
542 $wgLinkCache->clear();
545 # Now update the link cache by parsing the text
546 $wgOut = new OutputPage();
547 $wgOut->addWikiText( $text );
549 $this->editUpdates( $text );
550 if( $wgMwRedir->matchStart( $text ) )
554 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
557 # Add this page to my watchlist
559 function watch( $add = true )
561 global $wgUser, $wgOut, $wgLang;
562 global $wgDeferredUpdateList;
564 if ( 0 == $wgUser->getID() ) {
565 $wgOut->errorpage( "watchnologin", "watchnologintext" );
568 if ( wfReadOnly() ) {
569 $wgOut->readOnlyPage();
573 $wgUser->addWatch( $this->mTitle
);
575 $wgUser->removeWatch( $this->mTitle
);
577 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
578 $wgOut->setRobotpolicy( "noindex,follow" );
580 $sk = $wgUser->getSkin() ;
581 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
584 $text = wfMsg( "addedwatchtext", $link );
586 $text = wfMsg( "removedwatchtext", $link );
587 $wgOut->addHTML( $text );
589 $up = new UserUpdate();
590 array_push( $wgDeferredUpdateList, $up );
592 $wgOut->returnToMain( false );
597 $this->watch( false );
600 # This shares a lot of issues (and code) with Recent Changes
604 global $wgUser, $wgOut, $wgLang, $offset, $limit;
606 # If page hasn't changed, client can cache this
608 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
609 # Client cache fresh and headers sent, nothing more to do.
612 $fname = "Article::history";
613 wfProfileIn( $fname );
615 $wgOut->setPageTitle( $this->mTitle
->getPRefixedText() );
616 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
617 $wgOut->setArticleFlag( false );
618 $wgOut->setRobotpolicy( "noindex,nofollow" );
620 if( $this->mTitle
->getArticleID() == 0 ) {
621 $wgOut->addHTML( wfMsg( "nohistory" ) );
622 wfProfileOut( $fname );
626 $offset = (int)$offset;
627 $limit = (int)$limit;
628 if( $limit == 0 ) $limit = 50;
629 $namespace = $this->mTitle
->getNamespace();
630 $title = $this->mTitle
->getText();
631 $sql = "SELECT old_id,old_user," .
632 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
633 "FROM old USE INDEX (name_title_timestamp) " .
634 "WHERE old_namespace={$namespace} AND " .
635 "old_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
636 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
637 $res = wfQuery( $sql, DB_READ
, "Article::history" );
639 $revs = wfNumRows( $res );
640 if( $this->mTitle
->getArticleID() == 0 ) {
641 $wgOut->addHTML( wfMsg( "nohistory" ) );
642 wfProfileOut( $fname );
646 $sk = $wgUser->getSkin();
647 $numbar = wfViewPrevNext(
649 $this->mTitle
->getPrefixedText(),
652 $s .= $sk->beginHistoryList();
655 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
656 $this->getUserText(), $namespace,
657 $title, 0, $this->getComment(),
658 ( $this->getMinorEdit() > 0 ) );
660 $revs = wfNumRows( $res );
661 while ( $line = wfFetchObject( $res ) ) {
662 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
663 $line->old_user_text
, $namespace,
664 $title, $line->old_id
,
665 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
667 $s .= $sk->endHistoryList();
669 $wgOut->addHTML( $s );
670 wfProfileOut( $fname );
673 function protect( $limit = "sysop" )
675 global $wgUser, $wgOut;
677 if ( ! $wgUser->isSysop() ) {
678 $wgOut->sysopRequired();
681 if ( wfReadOnly() ) {
682 $wgOut->readOnlyPage();
685 $id = $this->mTitle
->getArticleID();
687 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
690 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
691 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
692 wfQuery( $sql, DB_WRITE
, "Article::protect" );
694 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
695 if ( $limit === "" ) {
696 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
698 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
700 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
705 return $this->protect( "" );
710 global $wgUser, $wgOut, $wgMessageCache;
711 global $wpConfirm, $wpReason, $image, $oldimage;
713 # This code desperately needs to be totally rewritten
715 if ( ( ! $wgUser->isSysop() ) ) {
716 $wgOut->sysopRequired();
719 if ( wfReadOnly() ) {
720 $wgOut->readOnlyPage();
724 # Can't delete cached MediaWiki namespace (i.e. vital messages)
725 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
726 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
730 # Better double-check that it hasn't been deleted yet!
731 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
732 if ( ( "" == trim( $this->mTitle
->getText() ) )
733 or ( $this->mTitle
->getArticleId() == 0 ) ) {
734 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
738 if ( $_POST["wpConfirm"] ) {
743 # determine whether this page has earlier revisions
744 # and insert a warning if it does
745 # we select the text because it might be useful below
746 $ns = $this->mTitle
->getNamespace();
747 $title = $this->mTitle
->getDBkey();
748 $etitle = wfStrencode( $title );
749 $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
750 $res = wfQuery( $sql, DB_READ
, $fname );
751 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
752 $skin=$wgUser->getSkin();
753 $wgOut->addHTML("<B>".wfMsg("historywarning"));
754 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
757 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
758 $res=wfQuery($sql, DB_READ
, $fname);
759 if( ($s=wfFetchObject($res))) {
761 # if this is a mini-text, we can paste part of it into the deletion reason
763 #if this is empty, an earlier revision may contain "useful" text
764 if($s->cur_text
!="") {
768 $text=$old->old_text
;
774 $length=strlen($text);
776 # this should not happen, since it is not possible to store an empty, new
777 # page. Let's insert a standard text in case it does, though
778 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
781 if($length < 500 && !$wpReason) {
783 # comment field=255, let's grep the first 150 to have some user
785 $text=substr($text,0,150);
786 # let's strip out newlines and HTML tags
787 $text=preg_replace("/\"/","'",$text);
788 $text=preg_replace("/\</","<",$text);
789 $text=preg_replace("/\>/",">",$text);
790 $text=preg_replace("/[\n\r]/","",$text);
792 $wpReason=wfMsg("excontent"). " '".$text;
794 $wpReason=wfMsg("exbeforeblank") . " '".$text;
796 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
801 return $this->confirmDelete();
804 function confirmDelete( $par = "" )
809 wfDebug( "Article::confirmDelete\n" );
811 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
812 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
813 $wgOut->setRobotpolicy( "noindex,nofollow" );
814 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
816 $t = $this->mTitle
->getPrefixedURL();
818 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
819 $confirm = wfMsg( "confirm" );
820 $check = wfMsg( "confirmcheck" );
821 $delcom = wfMsg( "deletecomment" );
824 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
825 <table border=0><tr><td align=right>
826 {$delcom}:</td><td align=left>
827 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
828 </td></tr><tr><td> </td></tr>
830 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
831 </td><td><label for=\"wpConfirm\">{$check}</label></td>
832 </tr><tr><td> </td><td>
833 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
834 </td></tr></table></form>\n" );
836 $wgOut->returnToMain( false );
841 global $wgOut, $wgUser, $wgLang;
843 $fname = "Article::doDelete";
844 wfDebug( "$fname\n" );
846 $this->doDeleteArticle( $this->mTitle
);
847 $deleted = $this->mTitle
->getPrefixedText();
849 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
850 $wgOut->setRobotpolicy( "noindex,nofollow" );
852 $sk = $wgUser->getSkin();
853 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
854 Namespace::getWikipedia() ) .
855 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
857 $text = wfMsg( "deletedtext", $deleted, $loglink );
859 $wgOut->addHTML( "<p>" . $text );
860 $wgOut->returnToMain( false );
863 function doDeleteArticle( $title )
865 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
866 $wgEnablePersistentLC;
868 $fname = "Article::doDeleteArticle";
869 wfDebug( "$fname\n" );
871 $ns = $title->getNamespace();
872 $t = wfStrencode( $title->getDBkey() );
873 $id = $title->getArticleID();
876 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
880 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
881 array_push( $wgDeferredUpdateList, $u );
883 # Move article and history to the "archive" table
884 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
885 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
886 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
887 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
888 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
889 wfQuery( $sql, DB_WRITE
, $fname );
891 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
892 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
893 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
894 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
895 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
896 wfQuery( $sql, DB_WRITE
, $fname );
898 # Now that it's safely backed up, delete it
900 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
902 wfQuery( $sql, DB_WRITE
, $fname );
904 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
906 wfQuery( $sql, DB_WRITE
, $fname );
908 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
910 wfQuery( $sql, DB_WRITE
, $fname );
912 # Finally, clean up the link tables
916 $t = wfStrencode( $title->getPrefixedDBkey() );
918 if ( $wgEnablePersistentLC ) {
919 // Purge related entries in links cache on delete,
920 wfQuery("DELETE linkscc FROM linkscc,links ".
921 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE
);
922 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE
);
925 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
926 $res = wfQuery( $sql, DB_READ
, $fname );
928 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
929 $now = wfTimestampNow();
930 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
933 while ( $s = wfFetchObject( $res ) ) {
934 $nt = Title
::newFromDBkey( $s->l_from
);
935 $lid = $nt->getArticleID();
937 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
939 $sql .= "({$lid},'{$t}')";
944 wfQuery( $sql, DB_WRITE
, $fname );
945 wfQuery( $sql2, DB_WRITE
, $fname );
947 wfFreeResult( $res );
949 $sql = "DELETE FROM links WHERE l_to={$id}";
950 wfQuery( $sql, DB_WRITE
, $fname );
952 $sql = "DELETE FROM links WHERE l_from='{$t}'";
953 wfQuery( $sql, DB_WRITE
, $fname );
955 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
956 wfQuery( $sql, DB_WRITE
, $fname );
958 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
959 wfQuery( $sql, DB_WRITE
, $fname );
962 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
963 $art = $title->getPrefixedText();
964 $wpReason = wfCleanQueryVar( $wpReason );
965 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
967 # Clear the cached article id so the interface doesn't act like we exist
968 $this->mTitle
->resetArticleID( 0 );
969 $this->mTitle
->mArticleID
= 0;
974 global $wgUser, $wgLang, $wgOut, $from;
976 if ( ! $wgUser->isSysop() ) {
977 $wgOut->sysopRequired();
980 if ( wfReadOnly() ) {
981 $wgOut->readOnlyPage( $this->getContent() );
985 # Secret enhanced rollback, marks edits rc_bot=1
986 $bot = !!$_REQUEST['bot'];
988 # Replace all this user's current edits with the next one down
989 $tt = wfStrencode( $this->mTitle
->getDBKey() );
990 $n = $this->mTitle
->getNamespace();
992 # Get the last editor
993 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
994 $res = wfQuery( $sql, DB_READ
);
995 if( ($x = wfNumRows( $res )) != 1 ) {
997 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1000 $s = wfFetchObject( $res );
1001 $ut = wfStrencode( $s->cur_user_text
);
1002 $uid = $s->cur_user
;
1005 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1006 if( $from != $s->cur_user_text
) {
1007 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1008 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1009 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1010 htmlspecialchars( $from ),
1011 htmlspecialchars( $s->cur_user_text
) ) );
1012 if($s->cur_comment
!= "") {
1014 wfMsg("editcomment",
1015 htmlspecialchars( $s->cur_comment
) ) );
1020 # Get the last edit not by this guy
1021 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp
1022 FROM old USE INDEX (name_title_timestamp)
1023 WHERE old_namespace={$n} AND old_title='{$tt}'
1024 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1025 ORDER BY inverse_timestamp LIMIT 1";
1026 $res = wfQuery( $sql, DB_READ
);
1027 if( wfNumRows( $res ) != 1 ) {
1029 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1030 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1033 $s = wfFetchObject( $res );
1036 # Mark all reverted edits as bot
1037 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1038 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1039 wfQuery( $sql, DB_WRITE
, $fname );
1043 $newcomment = wfMsg( "revertpage", $s->old_user_text
);
1044 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1045 $wgOut->setRobotpolicy( "noindex,nofollow" );
1046 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1047 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
1049 global $wgEnablePersistentLC;
1050 if ( $wgEnablePersistentLC ) {
1051 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
1055 $wgOut->returnToMain( false );
1059 # Do standard deferred updates after page view
1061 /* private */ function viewUpdates()
1063 global $wgDeferredUpdateList;
1064 if ( 0 != $this->getID() ) {
1065 global $wgDisableCounters;
1066 if( !$wgDisableCounters ) {
1067 Article
::incViewCount( $this->getID() );
1068 $u = new SiteStatsUpdate( 1, 0, 0 );
1069 array_push( $wgDeferredUpdateList, $u );
1071 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1072 $this->mTitle
->getDBkey() );
1073 array_push( $wgDeferredUpdateList, $u );
1077 # Do standard deferred updates after page edit.
1078 # Every 1000th edit, prune the recent changes table.
1080 /* private */ function editUpdates( $text )
1082 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1083 global $wgMessageCache;
1086 if ( 0 == mt_rand( 0, 999 ) ) {
1087 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1088 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1089 wfQuery( $sql, DB_WRITE
);
1091 $id = $this->getID();
1092 $title = $this->mTitle
->getPrefixedDBkey();
1093 $shortTitle = $this->mTitle
->getDBkey();
1095 $adj = $this->mCountAdjustment
;
1098 $u = new LinksUpdate( $id, $title );
1099 array_push( $wgDeferredUpdateList, $u );
1100 $u = new SiteStatsUpdate( 0, 1, $adj );
1101 array_push( $wgDeferredUpdateList, $u );
1102 $u = new SearchUpdate( $id, $title, $text );
1103 array_push( $wgDeferredUpdateList, $u );
1105 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1106 array_push( $wgDeferredUpdateList, $u );
1108 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1109 $wgMessageCache->replace( $shortTitle, $text );
1114 /* private */ function setOldSubtitle()
1116 global $wgLang, $wgOut;
1118 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1119 $r = wfMsg( "revisionasof", $td );
1120 $wgOut->setSubtitle( "({$r})" );
1123 # This function is called right before saving the wikitext,
1124 # so we can do things like signatures and links-in-context.
1126 function preSaveTransform( $text )
1129 while ( "" != $text ) {
1130 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1131 $s .= $this->pstPass2( $p[0] );
1133 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1135 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1136 $s .= "<nowiki>{$q[0]}</nowiki>";
1143 /* private */ function pstPass2( $text )
1145 global $wgUser, $wgLang, $wgLocaltimezone;
1149 $n = $wgUser->getName();
1150 $k = $wgUser->getOption( "nickname" );
1151 if ( "" == $k ) { $k = $n; }
1152 if(isset($wgLocaltimezone)) {
1153 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1155 /* Note: this is an ugly timezone hack for the European wikis */
1156 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1157 " (" . date( "T" ) . ")";
1158 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1160 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1161 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1162 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1163 Namespace::getUser() ) . ":$n|$k]]", $text );
1165 # Context links: [[|name]] and [[name (context)|]]
1167 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1168 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1169 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1170 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1172 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1173 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1174 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1175 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1176 # [[ns:page (cont)|]]
1178 $t = $this->mTitle
->getText();
1179 if ( preg_match( $conpat, $t, $m ) ) {
1182 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1183 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1184 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1186 if ( "" == $context ) {
1187 $text = preg_replace( $p2, "[[\\1]]", $text );
1189 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1192 # {{SUBST:xxx}} variables
1194 $mw =& MagicWord
::get( MAG_SUBST
);
1195 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1198 # Trim trailing whitespace
1199 # MAG_END (__END__) tag allows for trailing
1200 # whitespace to be deliberately included
1201 $text = rtrim( $text );
1202 $mw =& MagicWord::get( MAG_END );
1203 $mw->matchAndRemove( $text );
1208 /* Caching functions */
1210 # checkLastModified returns true iff it has taken care of all
1211 # output to the client that is necessary for this request.
1212 # (that is, it has sent a cached version of the page)
1213 function tryFileCache() {
1214 static $called = false;
1216 wfDebug( " tryFileCache() -- called twice!?\n" );
1220 if($this->isFileCacheable()) {
1221 $touched = $this->mTouched
;
1222 if( strpos( $this->mContent
, "{{" ) !== false ) {
1223 # Expire pages with variable replacements in an hour
1224 $expire = wfUnix2Timestamp( time() - 3600 );
1225 $touched = max( $expire, $touched );
1227 $cache = new CacheManager( $this->mTitle
);
1228 if($cache->isFileCacheGood( $touched )) {
1230 wfDebug( " tryFileCache() - about to load\n" );
1231 $cache->loadFromFileCache();
1234 wfDebug( " tryFileCache() - starting buffer\n" );
1235 if($cache->useGzip() && wfClientAcceptsGzip()) {
1236 /* For some reason, adding this header line over in
1237 CacheManager::saveToFileCache() fails on my test
1238 setup at home, though it works on the live install.
1239 Make double-sure... --brion */
1240 header( "Content-Encoding: gzip" );
1242 ob_start( array(&$cache, 'saveToFileCache' ) );
1245 wfDebug( " tryFileCache() - not cacheable\n" );
1249 function isFileCacheable() {
1250 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1251 global $action, $oldid, $diff, $redirect, $printable;
1252 return $wgUseFileCache
1253 and (!$wgShowIPinHeader)
1254 and ($this->getID() != 0)
1255 and ($wgUser->getId() == 0)
1256 and (!$wgUser->getNewtalk())
1257 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1258 and ($action == "view")
1259 and (!isset($oldid))
1261 and (!isset($redirect))
1262 and (!isset($printable))
1263 and (!$this->mRedirectedFrom
);
1266 function checkTouched() {
1267 $id = $this->getID();
1268 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1269 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1270 if( $s = wfFetchObject( $res ) ) {
1271 $this->mTouched
= $s->cur_touched
;
1272 return !$s->cur_is_redirect
;
1278 /* static */ function incViewCount( $id )
1280 $id = intval( $id );
1282 # Not important enough to warrant an error page in case of failure
1283 $oldignore = wfIgnoreSQLErrors( true );
1285 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1287 if( (rand() %
23 != 0) or (wfLastErrno() != 0) ){
1288 # Most of the time (or on SQL errors), skip row count check
1289 wfIgnoreSQLErrors( $oldignore );
1293 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1294 $row = wfFetchObject( $res );
1295 $rown = intval( $row->n
);
1296 if( $rown > 1000 ){ // update counters after ~1000 hits
1297 wfProfileIn( "Article::incViewCount-collect" );
1298 $old_user_abort = ignore_user_abort( true );
1300 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1301 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1302 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1303 "GROUP BY hc_id", DB_WRITE
);
1304 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1305 wfQuery("UNLOCK TABLES", DB_WRITE
);
1306 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1307 "WHERE cur_id = hc_id", DB_WRITE
);
1308 wfQuery("DROP TABLE acchits", DB_WRITE
);
1310 ignore_user_abort( $old_user_abort );
1311 wfProfileOut( "Article::incViewCount-collect" );
1313 wfIgnoreSQLErrors( $oldignore );
1317 function wfReplaceSubstVar( $matches ) {
1318 return wfMsg( $matches[1] );