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 );
165 $this->mContent
= $s->cur_text
;
166 $this->mUser
= $s->cur_user
;
167 $this->mCounter
= $s->cur_counter
;
168 $this->mTimestamp
= $s->cur_timestamp
;
169 $this->mTouched
= $s->cur_touched
;
170 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
171 $this->mTitle
->mRestrictionsLoaded
= true;
172 wfFreeResult( $res );
173 } else { # oldid set, retrieve historical version
174 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
175 "WHERE old_id={$oldid}";
176 $res = wfQuery( $sql, DB_READ
, $fname );
177 if ( 0 == wfNumRows( $res ) ) { return; }
179 $s = wfFetchObject( $res );
180 $this->mContent
= $s->old_text
;
181 $this->mUser
= $s->old_user
;
183 $this->mTimestamp
= $s->old_timestamp
;
184 wfFreeResult( $res );
186 $this->mContentLoaded
= true;
190 if( $this->mTitle
) {
191 return $this->mTitle
->getArticleID();
199 if ( -1 == $this->mCounter
) {
200 $id = $this->getID();
201 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
203 return $this->mCounter
;
206 # Would the given text make this article a "good" article (i.e.,
207 # suitable for including in the article count)?
209 function isCountable( $text )
211 global $wgUseCommaCount, $wgMwRedir;
213 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
214 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
215 $token = ($wgUseCommaCount ?
"," : "[[" );
216 if ( false === strstr( $text, $token ) ) { return 0; }
220 # Load the field related to the last edit time of the article.
221 # This isn't necessary for all uses, so it's only done if needed.
223 /* private */ function loadLastEdit()
226 if ( -1 != $this->mUser
) return;
228 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
229 "cur_comment,cur_minor_edit FROM cur WHERE " .
230 "cur_id=" . $this->getID();
231 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
233 if ( wfNumRows( $res ) > 0 ) {
234 $s = wfFetchObject( $res );
235 $this->mUser
= $s->cur_user
;
236 $this->mUserText
= $s->cur_user_text
;
237 $this->mTimestamp
= $s->cur_timestamp
;
238 $this->mComment
= $s->cur_comment
;
239 $this->mMinorEdit
= $s->cur_minor_edit
;
243 function getTimestamp()
245 $this->loadLastEdit();
246 return $this->mTimestamp
;
251 $this->loadLastEdit();
255 function getUserText()
257 $this->loadLastEdit();
258 return $this->mUserText
;
261 function getComment()
263 $this->loadLastEdit();
264 return $this->mComment
;
267 function getMinorEdit()
269 $this->loadLastEdit();
270 return $this->mMinorEdit
;
273 # This is the default action of the script: just view the page of
278 global $wgUser, $wgOut, $wgLang;
279 global $oldid, $diff; # From query
280 global $wgLinkCache, $IP;
281 $fname = "Article::view";
282 wfProfileIn( $fname );
284 $wgOut->setArticleFlag( true );
285 $wgOut->setRobotpolicy( "index,follow" );
287 # If we got diff and oldid in the query, we want to see a
288 # diff page instead of the article.
290 if ( isset( $diff ) ) {
291 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
292 $de = new DifferenceEngine( $oldid, $diff );
294 wfProfileOut( $fname );
298 if ( !isset( $oldid ) and $this->checkTouched() ) {
299 if( $wgOut->checkLastModified( $this->mTouched
) ){
301 } else if ( $this->tryFileCache() ) {
302 # tell wgOut that output is taken care of
304 $this->viewUpdates();
309 $text = $this->getContent(); # May change mTitle
310 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
311 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
312 " - " . wfMsg( "wikititlesuffix" ) );
314 # We're looking at an old revision
317 $this->setOldSubtitle();
318 $wgOut->setRobotpolicy( "noindex,follow" );
320 if ( "" != $this->mRedirectedFrom
) {
321 $sk = $wgUser->getSkin();
322 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
324 $s = wfMsg( "redirectedfrom", $redir );
325 $wgOut->setSubtitle( $s );
328 $wgLinkCache->preFill( $this->mTitle
);
329 $wgOut->addWikiText( $text );
331 $this->viewUpdates();
332 wfProfileOut( $fname );
335 # Theoretically we could defer these whole insert and update
336 # functions for after display, but that's taking a big leap
337 # of faith, and we want to be able to report database
338 # errors at some point.
340 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
342 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
343 global $wgEnablePersistentLC;
345 $fname = "Article::insertNewArticle";
347 $this->mCountAdjustment
= $this->isCountable( $text );
349 $ns = $this->mTitle
->getNamespace();
350 $ttl = $this->mTitle
->getDBkey();
351 $text = $this->preSaveTransform( $text );
352 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
355 $now = wfTimestampNow();
356 $won = wfInvertTimestamp( $now );
358 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
359 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
360 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
361 "cur_restrictions,cur_user_text,cur_is_redirect," .
362 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
363 wfStrencode( $text ) . "', '" .
364 wfStrencode( $summary ) . "', '" .
365 $wgUser->getID() . "', '{$now}', " .
366 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
367 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
368 $res = wfQuery( $sql, DB_WRITE
, $fname );
370 $newid = wfInsertId();
371 $this->mTitle
->resetArticleID( $newid );
373 if ( $wgEnablePersistentLC ) {
374 // Purge related entries in links cache on new page, to heal broken links
375 $ptitle = wfStrencode( $ttl );
376 wfQuery("DELETE linkscc FROM linkscc,brokenlinks ".
377 "WHERE lcc_pageid=bl_from AND bl_to='{$ptitle}'", DB_WRITE
);
380 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
381 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
382 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
383 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
384 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
385 wfStrencode( $wgUser->getName() ) . "','" .
386 wfStrencode( $summary ) . "',0,0," .
387 ( $wgUser->isBot() ?
1 : 0 ) . ")";
388 wfQuery( $sql, DB_WRITE
, $fname );
390 if(!$this->mTitle
->userIsWatching()) $this->watch();
392 if ( $this->mTitle
->userIsWatching() ) {
397 # The talk page isn't in the regular link tables, so we need to update manually:
398 $talkns = $ns ^
1; # talk -> normal; normal -> talk
399 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
400 wfQuery( $sql, DB_WRITE
);
402 $this->showArticle( $text, wfMsg( "newarticle" ) );
405 function updateArticle( $text, $summary, $minor, $watchthis, $section = "", $forceBot = false )
407 global $wgOut, $wgUser, $wgLinkCache;
408 global $wgDBtransactions, $wgMwRedir;
409 $fname = "Article::updateArticle";
411 $this->loadLastEdit();
413 // insert updated section into old text if we have only edited part
415 if ($section != "") {
416 $oldtext=$this->getContent();
417 if($section=="new") {
418 if($summary) $subject="== {$summary} ==\n\n";
419 $text=$oldtext."\n\n".$subject.$text;
421 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
422 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
423 $secs[$section*2]=$text."\n\n"; // replace with edited
424 if($section) { $secs[$section*2-1]=""; } // erase old headline
425 $text=join("",$secs);
428 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
429 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
430 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
432 $text = $m[1] . "\n"; # Remove all content but redirect
436 $text = $this->preSaveTransform( $text );
438 # Update article, but only if changed.
440 if( $wgDBtransactions ) {
442 wfQuery( $sql, DB_WRITE
);
444 $oldtext = $this->getContent( true );
446 if ( 0 != strcmp( $text, $oldtext ) ) {
447 $this->mCountAdjustment
= $this->isCountable( $text )
448 - $this->isCountable( $oldtext );
450 $now = wfTimestampNow();
451 $won = wfInvertTimestamp( $now );
452 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
453 "',cur_comment='" . wfStrencode( $summary ) .
454 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
455 ",cur_timestamp='{$now}',cur_user_text='" .
456 wfStrencode( $wgUser->getName() ) .
457 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
458 "WHERE cur_id=" . $this->getID() .
459 " AND cur_timestamp='" . $this->getTimestamp() . "'";
460 $res = wfQuery( $sql, DB_WRITE
, $fname );
462 if( wfAffectedRows() == 0 ) {
463 /* Belated edit conflict! Run away!! */
467 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
468 "old_comment,old_user,old_user_text,old_timestamp," .
469 "old_minor_edit,inverse_timestamp) VALUES (" .
470 $this->mTitle
->getNamespace() . ", '" .
471 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
472 wfStrencode( $oldtext ) . "', '" .
473 wfStrencode( $this->getComment() ) . "', " .
474 $this->getUser() . ", '" .
475 wfStrencode( $this->getUserText() ) . "', '" .
476 $this->getTimestamp() . "', " . $me1 . ", '" .
477 wfInvertTimestamp( $this->getTimestamp() ) . "')";
478 $res = wfQuery( $sql, DB_WRITE
, $fname );
479 $oldid = wfInsertID( $res );
481 $bot = (int)($wgUser->isBot() ||
$forceBot);
483 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
484 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
485 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
486 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
487 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
488 "$bot," . $this->getID() . "," . $wgUser->getID() . ",'" .
489 wfStrencode( $wgUser->getName() ) . "','" .
490 wfStrencode( $summary ) . "',0,{$oldid})";
491 wfQuery( $sql, DB_WRITE
, $fname );
493 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
494 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
495 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
496 "rc_timestamp='" . $this->getTimestamp() . "'";
497 wfQuery( $sql, DB_WRITE
, $fname );
499 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
500 "WHERE rc_cur_id=" . $this->getID();
501 wfQuery( $sql, DB_WRITE
, $fname );
503 global $wgEnablePersistentLC;
504 if ( $wgEnablePersistentLC ) {
505 // Purge link cache for this page
506 $pageid=$this->getID();
507 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pageid}'", DB_WRITE
);
511 if( $wgDBtransactions ) {
513 wfQuery( $sql, DB_WRITE
);
517 if (!$this->mTitle
->userIsWatching()) $this->watch();
519 if ( $this->mTitle
->userIsWatching() ) {
524 $this->showArticle( $text, wfMsg( "updated" ) );
528 # After we've either updated or inserted the article, update
529 # the link tables and redirect to the new page.
531 function showArticle( $text, $subtitle )
533 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
536 $wgLinkCache = new LinkCache();
538 # Get old version of link table to allow incremental link updates
539 if ( $wgUseBetterLinksUpdate ) {
540 $wgLinkCache->preFill( $this->mTitle
);
541 $wgLinkCache->clear();
544 # Now update the link cache by parsing the text
545 $wgOut = new OutputPage();
546 $wgOut->addWikiText( $text );
548 $this->editUpdates( $text );
549 if( $wgMwRedir->matchStart( $text ) )
553 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
556 # Add this page to my watchlist
558 function watch( $add = true )
560 global $wgUser, $wgOut, $wgLang;
561 global $wgDeferredUpdateList;
563 if ( 0 == $wgUser->getID() ) {
564 $wgOut->errorpage( "watchnologin", "watchnologintext" );
567 if ( wfReadOnly() ) {
568 $wgOut->readOnlyPage();
572 $wgUser->addWatch( $this->mTitle
);
574 $wgUser->removeWatch( $this->mTitle
);
576 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
577 $wgOut->setRobotpolicy( "noindex,follow" );
579 $sk = $wgUser->getSkin() ;
580 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
583 $text = wfMsg( "addedwatchtext", $link );
585 $text = wfMsg( "removedwatchtext", $link );
586 $wgOut->addHTML( $text );
588 $up = new UserUpdate();
589 array_push( $wgDeferredUpdateList, $up );
591 $wgOut->returnToMain( false );
596 $this->watch( false );
599 # This shares a lot of issues (and code) with Recent Changes
603 global $wgUser, $wgOut, $wgLang, $offset, $limit;
605 # If page hasn't changed, client can cache this
607 if( $wgOut->checkLastModified( $this->getTimestamp() ) ){
608 # Client cache fresh and headers sent, nothing more to do.
611 $fname = "Article::history";
612 wfProfileIn( $fname );
614 $wgOut->setPageTitle( $this->mTitle
->getPRefixedText() );
615 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
616 $wgOut->setArticleFlag( false );
617 $wgOut->setRobotpolicy( "noindex,nofollow" );
619 if( $this->mTitle
->getArticleID() == 0 ) {
620 $wgOut->addHTML( wfMsg( "nohistory" ) );
621 wfProfileOut( $fname );
625 $offset = (int)$offset;
626 $limit = (int)$limit;
627 if( $limit == 0 ) $limit = 50;
628 $namespace = $this->mTitle
->getNamespace();
629 $title = $this->mTitle
->getText();
630 $sql = "SELECT old_id,old_user," .
631 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
632 "FROM old USE INDEX (name_title_timestamp) " .
633 "WHERE old_namespace={$namespace} AND " .
634 "old_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
635 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
636 $res = wfQuery( $sql, DB_READ
, "Article::history" );
638 $revs = wfNumRows( $res );
639 if( $this->mTitle
->getArticleID() == 0 ) {
640 $wgOut->addHTML( wfMsg( "nohistory" ) );
641 wfProfileOut( $fname );
645 $sk = $wgUser->getSkin();
646 $numbar = wfViewPrevNext(
648 $this->mTitle
->getPrefixedText(),
651 $s .= $sk->beginHistoryList();
654 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
655 $this->getUserText(), $namespace,
656 $title, 0, $this->getComment(),
657 ( $this->getMinorEdit() > 0 ) );
659 $revs = wfNumRows( $res );
660 while ( $line = wfFetchObject( $res ) ) {
661 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
662 $line->old_user_text
, $namespace,
663 $title, $line->old_id
,
664 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
666 $s .= $sk->endHistoryList();
668 $wgOut->addHTML( $s );
669 wfProfileOut( $fname );
672 function protect( $limit = "sysop" )
674 global $wgUser, $wgOut;
676 if ( ! $wgUser->isSysop() ) {
677 $wgOut->sysopRequired();
680 if ( wfReadOnly() ) {
681 $wgOut->readOnlyPage();
684 $id = $this->mTitle
->getArticleID();
686 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
689 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
690 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
691 wfQuery( $sql, DB_WRITE
, "Article::protect" );
693 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
694 if ( $limit === "" ) {
695 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), "" );
697 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), "" );
699 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
704 return $this->protect( "" );
709 global $wgUser, $wgOut, $wgMessageCache;
710 global $wpConfirm, $wpReason, $image, $oldimage;
712 # This code desperately needs to be totally rewritten
714 if ( ( ! $wgUser->isSysop() ) ) {
715 $wgOut->sysopRequired();
718 if ( wfReadOnly() ) {
719 $wgOut->readOnlyPage();
723 # Can't delete cached MediaWiki namespace (i.e. vital messages)
724 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& $wgMessageCache->isCacheable( $this->mTitle
->getDBkey() ) ) {
725 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
729 # Better double-check that it hasn't been deleted yet!
730 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
731 if ( ( "" == trim( $this->mTitle
->getText() ) )
732 or ( $this->mTitle
->getArticleId() == 0 ) ) {
733 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
737 if ( $_POST["wpConfirm"] ) {
742 # determine whether this page has earlier revisions
743 # and insert a warning if it does
744 # we select the text because it might be useful below
745 $ns = $this->mTitle
->getNamespace();
746 $title = $this->mTitle
->getDBkey();
747 $etitle = wfStrencode( $title );
748 $sql = "SELECT old_text FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
749 $res = wfQuery( $sql, DB_READ
, $fname );
750 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
751 $skin=$wgUser->getSkin();
752 $wgOut->addHTML("<B>".wfMsg("historywarning"));
753 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
756 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
757 $res=wfQuery($sql, DB_READ
, $fname);
758 if( ($s=wfFetchObject($res))) {
760 # if this is a mini-text, we can paste part of it into the deletion reason
762 #if this is empty, an earlier revision may contain "useful" text
763 if($s->cur_text
!="") {
767 $text=$old->old_text
;
773 $length=strlen($text);
775 # this should not happen, since it is not possible to store an empty, new
776 # page. Let's insert a standard text in case it does, though
777 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
780 if($length < 500 && !$wpReason) {
782 # comment field=255, let's grep the first 150 to have some user
784 $text=substr($text,0,150);
785 # let's strip out newlines and HTML tags
786 $text=preg_replace("/\"/","'",$text);
787 $text=preg_replace("/\</","<",$text);
788 $text=preg_replace("/\>/",">",$text);
789 $text=preg_replace("/[\n\r]/","",$text);
791 $wpReason=wfMsg("excontent"). " '".$text;
793 $wpReason=wfMsg("exbeforeblank") . " '".$text;
795 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
800 return $this->confirmDelete();
803 function confirmDelete( $par = "" )
808 wfDebug( "Article::confirmDelete\n" );
810 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
811 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
812 $wgOut->setRobotpolicy( "noindex,nofollow" );
813 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
815 $t = $this->mTitle
->getPrefixedURL();
817 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
818 $confirm = wfMsg( "confirm" );
819 $check = wfMsg( "confirmcheck" );
820 $delcom = wfMsg( "deletecomment" );
823 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
824 <table border=0><tr><td align=right>
825 {$delcom}:</td><td align=left>
826 <input type=text size=60 name=\"wpReason\" value=\"" . htmlspecialchars( $wpReason ) . "\">
827 </td></tr><tr><td> </td></tr>
829 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
830 </td><td><label for=\"wpConfirm\">{$check}</label></td>
831 </tr><tr><td> </td><td>
832 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
833 </td></tr></table></form>\n" );
835 $wgOut->returnToMain( false );
840 global $wgOut, $wgUser, $wgLang;
842 $fname = "Article::doDelete";
843 wfDebug( "$fname\n" );
845 $this->doDeleteArticle( $this->mTitle
);
846 $deleted = $this->mTitle
->getPrefixedText();
848 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
849 $wgOut->setRobotpolicy( "noindex,nofollow" );
851 $sk = $wgUser->getSkin();
852 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
853 Namespace::getWikipedia() ) .
854 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
856 $text = wfMsg( "deletedtext", $deleted, $loglink );
858 $wgOut->addHTML( "<p>" . $text );
859 $wgOut->returnToMain( false );
862 function doDeleteArticle( $title )
864 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList,
865 $wgEnablePersistentLC;
867 $fname = "Article::doDeleteArticle";
868 wfDebug( "$fname\n" );
870 $ns = $title->getNamespace();
871 $t = wfStrencode( $title->getDBkey() );
872 $id = $title->getArticleID();
875 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
879 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
880 array_push( $wgDeferredUpdateList, $u );
882 # Move article and history to the "archive" table
883 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
884 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
885 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
886 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
887 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
888 wfQuery( $sql, DB_WRITE
, $fname );
890 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
891 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
892 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
893 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
894 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
895 wfQuery( $sql, DB_WRITE
, $fname );
897 # Now that it's safely backed up, delete it
899 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
901 wfQuery( $sql, DB_WRITE
, $fname );
903 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
905 wfQuery( $sql, DB_WRITE
, $fname );
907 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
909 wfQuery( $sql, DB_WRITE
, $fname );
911 # Finally, clean up the link tables
915 $t = wfStrencode( $title->getPrefixedDBkey() );
917 if ( $wgEnablePersistentLC ) {
918 // Purge related entries in links cache on delete,
919 wfQuery("DELETE linkscc FROM linkscc,links ".
920 "WHERE lcc_title=links.l_from AND l_to={$id}", DB_WRITE
);
921 wfQuery("DELETE FROM linkscc WHERE lcc_title='{$t}'", DB_WRITE
);
924 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
925 $res = wfQuery( $sql, DB_READ
, $fname );
927 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
928 $now = wfTimestampNow();
929 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
932 while ( $s = wfFetchObject( $res ) ) {
933 $nt = Title
::newFromDBkey( $s->l_from
);
934 $lid = $nt->getArticleID();
936 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
938 $sql .= "({$lid},'{$t}')";
943 wfQuery( $sql, DB_WRITE
, $fname );
944 wfQuery( $sql2, DB_WRITE
, $fname );
946 wfFreeResult( $res );
948 $sql = "DELETE FROM links WHERE l_to={$id}";
949 wfQuery( $sql, DB_WRITE
, $fname );
951 $sql = "DELETE FROM links WHERE l_from='{$t}'";
952 wfQuery( $sql, DB_WRITE
, $fname );
954 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
955 wfQuery( $sql, DB_WRITE
, $fname );
957 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
958 wfQuery( $sql, DB_WRITE
, $fname );
961 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
962 $art = $title->getPrefixedText();
963 $wpReason = wfCleanQueryVar( $wpReason );
964 $log->addEntry( wfMsg( "deletedarticle", $art ), $wpReason );
966 # Clear the cached article id so the interface doesn't act like we exist
967 $this->mTitle
->resetArticleID( 0 );
968 $this->mTitle
->mArticleID
= 0;
973 global $wgUser, $wgLang, $wgOut, $from;
975 if ( ! $wgUser->isSysop() ) {
976 $wgOut->sysopRequired();
979 if ( wfReadOnly() ) {
980 $wgOut->readOnlyPage( $this->getContent() );
984 # Secret enhanced rollback, marks edits rc_bot=1
985 $bot = !!$_REQUEST['bot'];
987 # Replace all this user's current edits with the next one down
988 $tt = wfStrencode( $this->mTitle
->getDBKey() );
989 $n = $this->mTitle
->getNamespace();
991 # Get the last editor
992 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
993 $res = wfQuery( $sql, DB_READ
);
994 if( ($x = wfNumRows( $res )) != 1 ) {
996 $wgOut->addHTML( wfMsg( "notanarticle" ) );
999 $s = wfFetchObject( $res );
1000 $ut = wfStrencode( $s->cur_user_text
);
1001 $uid = $s->cur_user
;
1004 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1005 if( $from != $s->cur_user_text
) {
1006 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1007 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1008 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1009 htmlspecialchars( $from ),
1010 htmlspecialchars( $s->cur_user_text
) ) );
1011 if($s->cur_comment
!= "") {
1013 wfMsg("editcomment",
1014 htmlspecialchars( $s->cur_comment
) ) );
1019 # Get the last edit not by this guy
1020 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp
1021 FROM old USE INDEX (name_title_timestamp)
1022 WHERE old_namespace={$n} AND old_title='{$tt}'
1023 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1024 ORDER BY inverse_timestamp LIMIT 1";
1025 $res = wfQuery( $sql, DB_READ
);
1026 if( wfNumRows( $res ) != 1 ) {
1028 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1029 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1032 $s = wfFetchObject( $res );
1035 # Mark all reverted edits as bot
1036 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1037 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1038 wfQuery( $sql, DB_WRITE
, $fname );
1042 $newcomment = wfMsg( "revertpage", $s->old_user_text
);
1043 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1044 $wgOut->setRobotpolicy( "noindex,nofollow" );
1045 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1046 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching(), "", $bot );
1048 global $wgEnablePersistentLC;
1049 if ( $wgEnablePersistentLC ) {
1050 wfQuery("DELETE FROM linkscc WHERE lcc_pageid='{$pid}'", DB_WRITE
);
1054 $wgOut->returnToMain( false );
1058 # Do standard deferred updates after page view
1060 /* private */ function viewUpdates()
1062 global $wgDeferredUpdateList;
1063 if ( 0 != $this->getID() ) {
1064 global $wgDisableCounters;
1065 if( !$wgDisableCounters ) {
1066 Article
::incViewCount( $this->getID() );
1067 $u = new SiteStatsUpdate( 1, 0, 0 );
1068 array_push( $wgDeferredUpdateList, $u );
1070 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1071 $this->mTitle
->getDBkey() );
1072 array_push( $wgDeferredUpdateList, $u );
1076 # Do standard deferred updates after page edit.
1077 # Every 1000th edit, prune the recent changes table.
1079 /* private */ function editUpdates( $text )
1081 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1082 global $wgMessageCache;
1085 if ( 0 == mt_rand( 0, 999 ) ) {
1086 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1087 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1088 wfQuery( $sql, DB_WRITE
);
1090 $id = $this->getID();
1091 $title = $this->mTitle
->getPrefixedDBkey();
1092 $shortTitle = $this->mTitle
->getDBkey();
1094 $adj = $this->mCountAdjustment
;
1097 $u = new LinksUpdate( $id, $title );
1098 array_push( $wgDeferredUpdateList, $u );
1099 $u = new SiteStatsUpdate( 0, 1, $adj );
1100 array_push( $wgDeferredUpdateList, $u );
1101 $u = new SearchUpdate( $id, $title, $text );
1102 array_push( $wgDeferredUpdateList, $u );
1104 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1105 array_push( $wgDeferredUpdateList, $u );
1107 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1108 $wgMessageCache->replace( $shortTitle, $text );
1113 /* private */ function setOldSubtitle()
1115 global $wgLang, $wgOut;
1117 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1118 $r = wfMsg( "revisionasof", $td );
1119 $wgOut->setSubtitle( "({$r})" );
1122 # This function is called right before saving the wikitext,
1123 # so we can do things like signatures and links-in-context.
1125 function preSaveTransform( $text )
1128 while ( "" != $text ) {
1129 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1130 $s .= $this->pstPass2( $p[0] );
1132 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1134 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1135 $s .= "<nowiki>{$q[0]}</nowiki>";
1142 /* private */ function pstPass2( $text )
1144 global $wgUser, $wgLang, $wgLocaltimezone;
1148 $n = $wgUser->getName();
1149 $k = $wgUser->getOption( "nickname" );
1150 if ( "" == $k ) { $k = $n; }
1151 if(isset($wgLocaltimezone)) {
1152 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1154 /* Note: this is an ugly timezone hack for the European wikis */
1155 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1156 " (" . date( "T" ) . ")";
1157 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1159 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1160 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1161 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1162 Namespace::getUser() ) . ":$n|$k]]", $text );
1164 # Context links: [[|name]] and [[name (context)|]]
1166 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1167 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1168 $namespacechar = '[ _0-9A-Za-z\x80-\xff]'; # Namespaces can use non-ascii!
1169 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1171 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1172 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1173 $p3 = "/\[\[($namespacechar+):({$np}+)\\|]]/"; # [[namespace:page|]]
1174 $p4 = "/\[\[($namespacechar+):({$np}+) \\(({$np}+)\\)\\|]]/";
1175 # [[ns:page (cont)|]]
1177 $t = $this->mTitle
->getText();
1178 if ( preg_match( $conpat, $t, $m ) ) {
1181 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1182 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1183 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1185 if ( "" == $context ) {
1186 $text = preg_replace( $p2, "[[\\1]]", $text );
1188 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1191 # {{SUBST:xxx}} variables
1193 $mw =& MagicWord
::get( MAG_SUBST
);
1194 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1197 # Trim trailing whitespace
1198 # MAG_END (__END__) tag allows for trailing
1199 # whitespace to be deliberately included
1200 $text = rtrim( $text );
1201 $mw =& MagicWord::get( MAG_END );
1202 $mw->matchAndRemove( $text );
1207 /* Caching functions */
1209 # checkLastModified returns true iff it has taken care of all
1210 # output to the client that is necessary for this request.
1211 # (that is, it has sent a cached version of the page)
1212 function tryFileCache() {
1213 static $called = false;
1215 wfDebug( " tryFileCache() -- called twice!?\n" );
1219 if($this->isFileCacheable()) {
1220 $touched = $this->mTouched
;
1221 if( strpos( $this->mContent
, "{{" ) !== false ) {
1222 # Expire pages with variable replacements in an hour
1223 $expire = wfUnix2Timestamp( time() - 3600 );
1224 $touched = max( $expire, $touched );
1226 $cache = new CacheManager( $this->mTitle
);
1227 if($cache->isFileCacheGood( $touched )) {
1229 wfDebug( " tryFileCache() - about to load\n" );
1230 $cache->loadFromFileCache();
1233 wfDebug( " tryFileCache() - starting buffer\n" );
1234 if($cache->useGzip() && wfClientAcceptsGzip()) {
1235 /* For some reason, adding this header line over in
1236 CacheManager::saveToFileCache() fails on my test
1237 setup at home, though it works on the live install.
1238 Make double-sure... --brion */
1239 header( "Content-Encoding: gzip" );
1241 ob_start( array(&$cache, 'saveToFileCache' ) );
1244 wfDebug( " tryFileCache() - not cacheable\n" );
1248 function isFileCacheable() {
1249 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1250 global $action, $oldid, $diff, $redirect, $printable;
1251 return $wgUseFileCache
1252 and (!$wgShowIPinHeader)
1253 and ($this->getID() != 0)
1254 and ($wgUser->getId() == 0)
1255 and (!$wgUser->getNewtalk())
1256 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1257 and ($action == "view")
1258 and (!isset($oldid))
1260 and (!isset($redirect))
1261 and (!isset($printable))
1262 and (!$this->mRedirectedFrom
);
1265 function checkTouched() {
1266 $id = $this->getID();
1267 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1268 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1269 if( $s = wfFetchObject( $res ) ) {
1270 $this->mTouched
= $s->cur_touched
;
1271 return !$s->cur_is_redirect
;
1277 /* static */ function incViewCount( $id )
1279 $id = intval( $id );
1281 # Not important enough to warrant an error page in case of failure
1282 $oldignore = wfIgnoreSQLErrors( true );
1284 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1286 if( (rand() %
23 != 0) or (wfLastErrno() != 0) ){
1287 # Most of the time (or on SQL errors), skip row count check
1288 wfIgnoreSQLErrors( $oldignore );
1292 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1293 $row = wfFetchObject( $res );
1294 $rown = intval( $row->n
);
1295 if( $rown > 1000 ){ // update counters after ~1000 hits
1296 wfProfileIn( "Article::incViewCount-collect" );
1297 $old_user_abort = ignore_user_abort( true );
1299 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1300 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1301 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1302 "GROUP BY hc_id", DB_WRITE
);
1303 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1304 wfQuery("UNLOCK TABLES", DB_WRITE
);
1305 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1306 "WHERE cur_id = hc_id", DB_WRITE
);
1307 wfQuery("DROP TABLE acchits", DB_WRITE
);
1309 ignore_user_abort( $old_user_abort );
1310 wfProfileOut( "Article::incViewCount-collect" );
1312 wfIgnoreSQLErrors( $oldignore );
1316 function wfReplaceSubstVar( $matches ) {
1317 return wfMsg( $matches[1] );