873326cb72b606eb7133fc8755d5931156212bce
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.
8 include_once( "CacheManager.php" );
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache;
17 function Article( &$title ) {
18 $this->mTitle
=& $title;
22 /* private */ function clear()
24 $this->mContentLoaded
= false;
25 $this->mUser
= $this->mCounter
= -1; # Not loaded
26 $this->mRedirectedFrom
= $this->mUserText
=
27 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
28 $this->mCountAdjustment
= 0;
29 $this->mTouched
= "19700101000000";
32 # Note that getContent/loadContent may follow redirects if
33 # not told otherwise, and so may cause a change to wgTitle.
35 function getContent( $noredir = false )
37 global $action,$section,$count; # From query string
38 wfProfileIn( "Article::getContent" );
40 if ( 0 == $this->getID() ) {
41 if ( "edit" == $action ) {
42 return ""; # was "newarticletext", now moved above the box)
45 return wfMsg( "noarticletext" );
47 $this->loadContent( $noredir );
51 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
52 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
53 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
57 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
61 if($section=="new") { return ""; }
63 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
65 PREG_SPLIT_DELIM_CAPTURE
);
67 return trim($secs[0]);
69 return trim($secs[$section*2-1] . $secs[$section*2]);
73 return $this->mContent
;
78 function loadContent( $noredir = false )
80 global $wgOut, $wgMwRedir;
81 global $oldid, $redirect; # From query
83 if ( $this->mContentLoaded
) return;
84 $fname = "Article::loadContent";
86 # Pre-fill content with error message so that if something
87 # fails we'll have something telling us what we intended.
89 $t = $this->mTitle
->getPrefixedText();
90 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
91 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
92 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
94 if ( ! $oldid ) { # Retrieve current version
96 if ( 0 == $id ) return;
99 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
100 "FROM cur WHERE cur_id={$id}";
101 $res = wfQuery( $sql, $fname );
102 if ( 0 == wfNumRows( $res ) ) { return; }
104 $s = wfFetchObject( $res );
106 # If we got a redirect, follow it (unless we've been told
107 # not to by either the function parameter or the query
108 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
109 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
110 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
111 $s->cur_text
, $m ) ) {
112 $rt = Title
::newFromText( $m[1] );
114 # Gotta hand redirects to special pages differently:
115 # Fill the HTTP response "Location" header and ignore
116 # the rest of the page we're on.
118 if ( $rt->getInterwiki() != "" ) {
119 $wgOut->redirect( $rt->getFullURL() ) ;
122 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
123 $wgOut->redirect( wfLocalUrl(
124 $rt->getPrefixedURL() ) );
127 $rid = $rt->getArticleID();
129 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
130 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
131 $res = wfQuery( $sql, $fname );
133 if ( 0 != wfNumRows( $res ) ) {
134 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
136 $s = wfFetchObject( $res );
141 $this->mContent
= $s->cur_text
;
142 $this->mUser
= $s->cur_user
;
143 $this->mCounter
= $s->cur_counter
;
144 $this->mTimestamp
= $s->cur_timestamp
;
145 $this->mTouched
= $s->cur_touched
;
146 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
147 $this->mTitle
->mRestrictionsLoaded
= true;
148 wfFreeResult( $res );
149 } else { # oldid set, retrieve historical version
150 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
151 "WHERE old_id={$oldid}";
152 $res = wfQuery( $sql, $fname );
153 if ( 0 == wfNumRows( $res ) ) { return; }
155 $s = wfFetchObject( $res );
156 $this->mContent
= $s->old_text
;
157 $this->mUser
= $s->old_user
;
159 $this->mTimestamp
= $s->old_timestamp
;
160 wfFreeResult( $res );
162 $this->mContentLoaded
= true;
165 function getID() { return $this->mTitle
->getArticleID(); }
169 if ( -1 == $this->mCounter
) {
170 $id = $this->getID();
171 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
173 return $this->mCounter
;
176 # Would the given text make this article a "good" article (i.e.,
177 # suitable for including in the article count)?
179 function isCountable( $text )
181 global $wgUseCommaCount, $wgMwRedir;
183 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
184 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
185 $token = ($wgUseCommaCount ?
"," : "[[" );
186 if ( false === strstr( $text, $token ) ) { return 0; }
190 # Load the field related to the last edit time of the article.
191 # This isn't necessary for all uses, so it's only done if needed.
193 /* private */ function loadLastEdit()
196 if ( -1 != $this->mUser
) return;
198 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
199 "cur_comment,cur_minor_edit FROM cur WHERE " .
200 "cur_id=" . $this->getID();
201 $res = wfQuery( $sql, "Article::loadLastEdit" );
203 if ( wfNumRows( $res ) > 0 ) {
204 $s = wfFetchObject( $res );
205 $this->mUser
= $s->cur_user
;
206 $this->mUserText
= $s->cur_user_text
;
207 $this->mTimestamp
= $s->cur_timestamp
;
208 $this->mComment
= $s->cur_comment
;
209 $this->mMinorEdit
= $s->cur_minor_edit
;
213 function getTimestamp()
215 $this->loadLastEdit();
216 return $this->mTimestamp
;
221 $this->loadLastEdit();
225 function getUserText()
227 $this->loadLastEdit();
228 return $this->mUserText
;
231 function getComment()
233 $this->loadLastEdit();
234 return $this->mComment
;
237 function getMinorEdit()
239 $this->loadLastEdit();
240 return $this->mMinorEdit
;
243 # This is the default action of the script: just view the page of
248 global $wgUser, $wgOut, $wgLang;
249 global $oldid, $diff; # From query
251 wfProfileIn( "Article::view" );
253 $wgOut->setArticleFlag( true );
254 $wgOut->setRobotpolicy( "index,follow" );
256 # If we got diff and oldid in the query, we want to see a
257 # diff page instead of the article.
259 if ( isset( $diff ) ) {
260 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
261 $de = new DifferenceEngine( $oldid, $diff );
266 $text = $this->getContent(); # May change wgTitle!
267 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
268 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
269 " - " . wfMsg( "wikititlesuffix" ) );
271 # We're looking at an old revision
274 $this->setOldSubtitle();
275 $wgOut->setRobotpolicy( "noindex,follow" );
277 if ( "" != $this->mRedirectedFrom
) {
278 $sk = $wgUser->getSkin();
279 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
281 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
282 $wgOut->setSubtitle( $s );
284 $wgOut->checkLastModified( $this->mTouched
);
285 $this->tryFileCache();
286 $wgLinkCache->preFill( $this->mTitle
);
287 $wgOut->addWikiText( $text );
289 # If the article we've just shown is in the "Image" namespace,
290 # follow it with the history list and link list for the image
293 if ( Namespace::getImage() == $this->mTitle
->getNamespace() ) {
294 $this->imageHistory();
297 $this->viewUpdates();
301 # Theoretically we could defer these whole insert and update
302 # functions for after display, but that's taking a big leap
303 # of faith, and we want to be able to report database
304 # errors at some point.
306 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
308 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
309 $fname = "Article::insertNewArticle";
311 $ns = $this->mTitle
->getNamespace();
312 $ttl = $this->mTitle
->getDBkey();
313 $text = $this->preSaveTransform( $text );
314 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
317 $now = wfTimestampNow();
318 $won = wfInvertTimestamp( $now );
320 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
321 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
322 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
323 "cur_restrictions,cur_user_text,cur_is_redirect," .
324 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
325 wfStrencode( $text ) . "', '" .
326 wfStrencode( $summary ) . "', '" .
327 $wgUser->getID() . "', '{$now}', " .
328 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
329 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
330 $res = wfQuery( $sql, $fname );
332 $newid = wfInsertId();
333 $this->mTitle
->resetArticleID( $newid );
335 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
336 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
337 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
338 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
339 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
340 wfStrencode( $wgUser->getName() ) . "','" .
341 wfStrencode( $summary ) . "',0,0," .
342 ( $wgUser->isBot() ?
1 : 0 ) . ")";
343 wfQuery( $sql, $fname );
345 if(!$this->mTitle
->userIsWatching()) $this->watch();
347 if ( $this->mTitle
->userIsWatching() ) {
352 $this->showArticle( $text, wfMsg( "newarticle" ) );
355 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
357 global $wgOut, $wgUser, $wgLinkCache;
358 global $wgDBtransactions, $wgMwRedir;
359 $fname = "Article::updateArticle";
361 $this->loadLastEdit();
363 // insert updated section into old text if we have only edited part
365 if ($section != "") {
366 $oldtext=$this->getContent();
367 if($section=="new") {
368 if($summary) $subject="== {$summary} ==\n\n";
369 $text=$oldtext."\n\n".$subject.$text;
371 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
372 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
373 $secs[$section*2]=$text."\n\n"; // replace with edited
374 if($section) { $secs[$section*2-1]=""; } // erase old headline
375 $text=join("",$secs);
378 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
379 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
380 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
382 $text = $m[1] . "\n"; # Remove all content but redirect
386 $text = $this->preSaveTransform( $text );
388 # Update article, but only if changed.
390 if( $wgDBtransactions ) {
394 $oldtext = $this->getContent( true );
396 if ( 0 != strcmp( $text, $oldtext ) ) {
397 $this->mCountAdjustment
= $this->isCountable( $text )
398 - $this->isCountable( $oldtext );
400 $now = wfTimestampNow();
401 $won = wfInvertTimestamp( $now );
402 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
403 "',cur_comment='" . wfStrencode( $summary ) .
404 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
405 ",cur_timestamp='{$now}',cur_user_text='" .
406 wfStrencode( $wgUser->getName() ) .
407 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
408 "WHERE cur_id=" . $this->getID() .
409 " AND cur_timestamp='" . $this->getTimestamp() . "'";
410 $res = wfQuery( $sql, $fname );
412 if( wfAffectedRows() == 0 ) {
413 /* Belated edit conflict! Run away!! */
417 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
418 "old_comment,old_user,old_user_text,old_timestamp," .
419 "old_minor_edit,inverse_timestamp) VALUES (" .
420 $this->mTitle
->getNamespace() . ", '" .
421 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
422 wfStrencode( $oldtext ) . "', '" .
423 wfStrencode( $this->getComment() ) . "', " .
424 $this->getUser() . ", '" .
425 wfStrencode( $this->getUserText() ) . "', '" .
426 $this->getTimestamp() . "', " . $me1 . ", '" .
427 wfInvertTimestamp( $this->getTimestamp() ) . "')";
428 $res = wfQuery( $sql, $fname );
429 $oldid = wfInsertID( $res );
431 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
432 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
433 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
434 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
435 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
436 ( $wgUser->isBot() ?
1 : 0 ) . "," .
437 $this->getID() . "," . $wgUser->getID() . ",'" .
438 wfStrencode( $wgUser->getName() ) . "','" .
439 wfStrencode( $summary ) . "',0,{$oldid})";
440 wfQuery( $sql, $fname );
442 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
443 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
444 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
445 "rc_timestamp='" . $this->getTimestamp() . "'";
446 wfQuery( $sql, $fname );
448 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
449 "WHERE rc_cur_id=" . $this->getID();
450 wfQuery( $sql, $fname );
452 if( $wgDBtransactions ) {
458 if (!$this->mTitle
->userIsWatching()) $this->watch();
460 if ( $this->mTitle
->userIsWatching() ) {
465 $this->showArticle( $text, wfMsg( "updated" ) );
469 # After we've either updated or inserted the article, update
470 # the link tables and redirect to the new page.
472 function showArticle( $text, $subtitle )
474 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
477 $wgLinkCache = new LinkCache();
479 # Get old version of link table to allow incremental link updates
480 if ( $wgUseBetterLinksUpdate ) {
481 $wgLinkCache->preFill( $this->mTitle
);
482 $wgLinkCache->clear();
485 # Now update the link cache by parsing the text
486 $wgOut = new OutputPage();
487 $wgOut->addWikiText( $text );
489 $this->editUpdates( $text );
490 if( $wgMwRedir->matchStart( $text ) )
494 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
497 # If the page we've just displayed is in the "Image" namespace,
498 # we follow it with an upload history of the image and its usage.
500 function imageHistory()
502 global $wgUser, $wgOut, $wgLang;
503 $fname = "Article::imageHistory";
505 $sql = "SELECT img_size,img_description,img_user," .
506 "img_user_text,img_timestamp FROM image WHERE " .
507 "img_name='" . wfStrencode( $this->mTitle
->getDBkey() ) . "'";
508 $res = wfQuery( $sql, $fname );
510 if ( 0 == wfNumRows( $res ) ) { return; }
512 $sk = $wgUser->getSkin();
513 $s = $sk->beginImageHistoryList();
515 $line = wfFetchObject( $res );
516 $s .= $sk->imageHistoryLine( true, $line->img_timestamp
,
517 $this->mTitle
->getText(), $line->img_user
,
518 $line->img_user_text
, $line->img_size
, $line->img_description
);
520 $sql = "SELECT oi_size,oi_description,oi_user," .
521 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
522 "oi_name='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
523 "ORDER BY oi_timestamp DESC";
524 $res = wfQuery( $sql, $fname );
526 while ( $line = wfFetchObject( $res ) ) {
527 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp
,
528 $line->oi_archive_name
, $line->oi_user
,
529 $line->oi_user_text
, $line->oi_size
, $line->oi_description
);
531 $s .= $sk->endImageHistoryList();
532 $wgOut->addHTML( $s );
535 function imageLinks()
537 global $wgUser, $wgOut;
539 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
541 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
542 wfStrencode( $this->mTitle
->getDBkey() ) . "'";
543 $res = wfQuery( $sql, "Article::imageLinks" );
545 if ( 0 == wfNumRows( $res ) ) {
546 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
549 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
551 $sk = $wgUser->getSkin();
552 while ( $s = wfFetchObject( $res ) ) {
554 $link = $sk->makeKnownLink( $name, "" );
555 $wgOut->addHTML( "<li>{$link}</li>\n" );
557 $wgOut->addHTML( "</ul>\n" );
560 # Add this page to my watchlist
564 global $wgUser, $wgOut, $wgLang;
565 global $wgDeferredUpdateList;
567 if ( 0 == $wgUser->getID() ) {
568 $wgOut->errorpage( "watchnologin", "watchnologintext" );
571 if ( wfReadOnly() ) {
572 $wgOut->readOnlyPage();
575 $wgUser->addWatch( $this->mTitle
);
577 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
578 $wgOut->setRobotpolicy( "noindex,follow" );
580 $sk = $wgUser->getSkin() ;
581 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
583 $text = str_replace( "$1", $link ,
584 wfMsg( "addedwatchtext" ) );
585 $wgOut->addHTML( $text );
587 $up = new UserUpdate();
588 array_push( $wgDeferredUpdateList, $up );
590 $wgOut->returnToMain( false );
595 global $wgUser, $wgOut, $wgLang;
596 global $wgDeferredUpdateList;
598 if ( 0 == $wgUser->getID() ) {
599 $wgOut->errorpage( "watchnologin", "watchnologintext" );
602 if ( wfReadOnly() ) {
603 $wgOut->readOnlyPage();
606 $wgUser->removeWatch( $this->mTitle
);
608 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
609 $wgOut->setRobotpolicy( "noindex,follow" );
611 $sk = $wgUser->getSkin() ;
612 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
614 $text = str_replace( "$1", $link ,
615 wfMsg( "removedwatchtext" ) );
616 $wgOut->addHTML( $text );
618 $up = new UserUpdate();
619 array_push( $wgDeferredUpdateList, $up );
621 $wgOut->returnToMain( false );
624 # This shares a lot of issues (and code) with Recent Changes
628 global $wgUser, $wgOut, $wgLang, $offset, $limit;
630 # If page hasn't changed, client can cache this
632 $wgOut->checkLastModified( $this->getTimestamp() );
633 wfProfileIn( "Article::history" );
635 $wgOut->setPageTitle( $this->mTitle
->getPRefixedText() );
636 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
637 $wgOut->setArticleFlag( false );
638 $wgOut->setRobotpolicy( "noindex,nofollow" );
640 if( $this->mTitle
->getArticleID() == 0 ) {
641 $wgOut->addHTML( wfMsg( "nohistory" ) );
646 $offset = (int)$offset;
647 $limit = (int)$limit;
648 if( $limit == 0 ) $limit = 50;
649 $namespace = $this->mTitle
->getNamespace();
650 $title = $this->mTitle
->getText();
651 $sql = "SELECT old_id,old_user," .
652 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
653 "FROM old USE INDEX (name_title_timestamp) " .
654 "WHERE old_namespace={$namespace} AND " .
655 "old_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
656 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
657 $res = wfQuery( $sql, "Article::history" );
659 $revs = wfNumRows( $res );
660 if( $this->mTitle
->getArticleID() == 0 ) {
661 $wgOut->addHTML( wfMsg( "nohistory" ) );
666 $sk = $wgUser->getSkin();
667 $numbar = wfViewPrevNext(
669 $this->mTitle
->getPrefixedText(),
672 $s .= $sk->beginHistoryList();
675 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
676 $this->getUserText(), $namespace,
677 $title, 0, $this->getComment(),
678 ( $this->getMinorEdit() > 0 ) );
680 $revs = wfNumRows( $res );
681 while ( $line = wfFetchObject( $res ) ) {
682 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
683 $line->old_user_text
, $namespace,
684 $title, $line->old_id
,
685 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
687 $s .= $sk->endHistoryList();
689 $wgOut->addHTML( $s );
695 global $wgUser, $wgOut;
697 if ( ! $wgUser->isSysop() ) {
698 $wgOut->sysopRequired();
701 if ( wfReadOnly() ) {
702 $wgOut->readOnlyPage();
705 $id = $this->mTitle
->getArticleID();
707 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
710 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
711 "cur_restrictions='sysop' WHERE cur_id={$id}";
712 wfQuery( $sql, "Article::protect" );
714 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
719 global $wgUser, $wgOut;
721 if ( ! $wgUser->isSysop() ) {
722 $wgOut->sysopRequired();
725 if ( wfReadOnly() ) {
726 $wgOut->readOnlyPage();
729 $id = $this->mTitle
->getArticleID();
731 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
734 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
735 "cur_restrictions='' WHERE cur_id={$id}";
736 wfQuery( $sql, "Article::unprotect" );
738 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
743 global $wgUser, $wgOut;
744 global $wpConfirm, $wpReason, $image, $oldimage;
746 # Anybody can delete old revisions of images; only sysops
747 # can delete articles and current images
749 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
750 $wgOut->sysopRequired();
753 if ( wfReadOnly() ) {
754 $wgOut->readOnlyPage();
758 # Better double-check that it hasn't been deleted yet!
759 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
761 if ( "" == trim( $image ) ) {
762 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
765 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
768 if ( ( "" == trim( $this->mTitle
->getText() ) )
769 or ( $this->mTitle
->getArticleId() == 0 ) ) {
770 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
773 $sub = str_replace( "$1", $this->mTitle
->getPrefixedText(),
774 wfMsg( "deletesub" ) );
776 # determine whether this page has earlier revisions
777 # and insert a warning if it does
778 # we select the text because it might be useful below
779 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
780 $res=wfQuery($sql,$fname);
781 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
782 $skin=$wgUser->getSkin();
783 $wgOut->addHTML("<B>".wfMsg("historywarning"));
784 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
787 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."'";
788 $res=wfQuery($sql,$fname);
789 if( ($s=wfFetchObject($res))) {
791 # if this is a mini-text, we can paste part of it into the deletion reason
793 #if this is empty, an earlier revision may contain "useful" text
794 if($s->cur_text
!="") {
798 $text=$old->old_text
;
804 $length=strlen($text);
806 # this should not happen, since it is not possible to store an empty, new
807 # page. Let's insert a standard text in case it does, though
808 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
811 if($length < 500 && !$wpReason) {
813 # comment field=255, let's grep the first 150 to have some user
815 $text=substr($text,0,150);
816 # let's strip out newlines and HTML tags
817 $text=preg_replace("/\"/","'",$text);
818 $text=preg_replace("/\</","<",$text);
819 $text=preg_replace("/\>/",">",$text);
820 $text=preg_replace("/[\n\r]/","",$text);
822 $wpReason=wfMsg("excontent"). " '".$text;
824 $wpReason=wfMsg("exbeforeblank") . " '".$text;
826 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
833 # Likewise, deleting old images doesn't require confirmation
834 if ( $oldimage ||
1 == $wpConfirm ) {
839 $wgOut->setSubtitle( $sub );
840 $wgOut->setRobotpolicy( "noindex,nofollow" );
841 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
843 $t = $this->mTitle
->getPrefixedURL();
844 $q = "action=delete";
847 $q .= "&image={$image}";
848 } else if ( $oldimage ) {
849 $q .= "&oldimage={$oldimage}";
853 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
854 $confirm = wfMsg( "confirm" );
855 $check = wfMsg( "confirmcheck" );
856 $delcom = wfMsg( "deletecomment" );
859 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
860 <table border=0><tr><td align=right>
861 {$delcom}:</td><td align=left>
862 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
863 </td></tr><tr><td> </td></tr>
865 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
866 </td><td><label for=\"wpConfirm\">{$check}</label></td>
867 </tr><tr><td> </td><td>
868 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
869 </td></tr></table></form>\n" );
871 $wgOut->returnToMain( false );
876 global $wgOut, $wgUser, $wgLang;
877 global $image, $oldimage, $wpReason;
878 $fname = "Article::doDelete";
881 $dest = wfImageDir( $image );
882 $archive = wfImageDir( $image );
883 if ( ! unlink( "{$dest}/{$image}" ) ) {
884 $wgOut->fileDeleteError( "{$dest}/{$image}" );
887 $sql = "DELETE FROM image WHERE img_name='" .
888 wfStrencode( $image ) . "'";
889 wfQuery( $sql, $fname );
891 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
892 wfStrencode( $image ) . "'";
893 $res = wfQuery( $sql, $fname );
895 while ( $s = wfFetchObject( $res ) ) {
896 $this->doDeleteOldImage( $s->oi_archive_name
);
898 $sql = "DELETE FROM oldimage WHERE oi_name='" .
899 wfStrencode( $image ) . "'";
900 wfQuery( $sql, $fname );
902 # Image itself is now gone, and database is cleaned.
903 # Now we remove the image description page.
905 $nt = Title
::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
906 $this->doDeleteArticle( $nt );
909 } else if ( $oldimage ) {
910 $this->doDeleteOldImage( $oldimage );
911 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
912 wfStrencode( $oldimage ) . "'";
913 wfQuery( $sql, $fname );
915 $deleted = $oldimage;
917 $this->doDeleteArticle( $this->mTitle
);
918 $deleted = $this->mTitle
->getPrefixedText();
920 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
921 $wgOut->setRobotpolicy( "noindex,nofollow" );
923 $sk = $wgUser->getSkin();
924 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
925 Namespace::getWikipedia() ) .
926 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
928 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
929 $text = str_replace( "$2", $loglink, $text );
931 $wgOut->addHTML( "<p>" . $text );
932 $wgOut->returnToMain( false );
935 function doDeleteOldImage( $oldimage )
939 $name = substr( $oldimage, 15 );
940 $archive = wfImageArchiveDir( $name );
941 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
942 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
946 function doDeleteArticle( $title )
948 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
950 $fname = "Article::doDeleteArticle";
951 $ns = $title->getNamespace();
952 $t = wfStrencode( $title->getDBkey() );
953 $id = $title->getArticleID();
956 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
960 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
961 array_push( $wgDeferredUpdateList, $u );
963 # Move article and history to the "archive" table
964 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
965 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
966 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
967 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
968 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
969 wfQuery( $sql, $fname );
971 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
972 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
973 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
974 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
975 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
976 wfQuery( $sql, $fname );
978 # Now that it's safely backed up, delete it
980 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
982 wfQuery( $sql, $fname );
984 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
986 wfQuery( $sql, $fname );
988 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
990 wfQuery( $sql, $fname );
992 # Finally, clean up the link tables
995 $t = wfStrencode( $title->getPrefixedDBkey() );
996 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
997 $res = wfQuery( $sql, $fname );
999 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1000 $now = wfTimestampNow();
1001 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1004 while ( $s = wfFetchObject( $res ) ) {
1005 $nt = Title
::newFromDBkey( $s->l_from
);
1006 $lid = $nt->getArticleID();
1008 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1010 $sql .= "({$lid},'{$t}')";
1015 wfQuery( $sql, $fname );
1016 wfQuery( $sql2, $fname );
1018 wfFreeResult( $res );
1020 $sql = "DELETE FROM links WHERE l_to={$id}";
1021 wfQuery( $sql, $fname );
1023 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1024 wfQuery( $sql, $fname );
1026 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1027 wfQuery( $sql, $fname );
1029 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1030 wfQuery( $sql, $fname );
1033 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1034 $art = $title->getPrefixedText();
1035 $wpReason = wfCleanQueryVar( $wpReason );
1036 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1038 # Clear the cached article id so the interface doesn't act like we exist
1039 $this->mTitle
->resetArticleID( 0 );
1040 $this->mTitle
->mArticleID
= 0;
1048 if ( strlen( $oldimage ) < 16 ) {
1049 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1052 if ( wfReadOnly() ) {
1053 $wgOut->readOnlyPage();
1056 $name = substr( $oldimage, 15 );
1058 $dest = wfImageDir( $name );
1059 $archive = wfImageArchiveDir( $name );
1060 $curfile = "{$dest}/{$name}";
1062 if ( ! is_file( $curfile ) ) {
1063 $wgOut->fileNotFoundError( $curfile );
1066 $oldver = wfTimestampNow() . "!{$name}";
1067 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1068 wfStrencode( $oldimage ) . "'" );
1070 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1071 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1074 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1075 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1077 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1079 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1080 $wgOut->setRobotpolicy( "noindex,nofollow" );
1081 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1082 $wgOut->returnToMain( false );
1087 global $wgUser, $wgLang, $wgOut, $from;
1089 if ( ! $wgUser->isSysop() ) {
1090 $wgOut->sysopRequired();
1094 # Replace all this user's current edits with the next one down
1095 $tt = wfStrencode( $this->mTitle
->getDBKey() );
1096 $n = $this->mTitle
->getNamespace();
1098 # Get the last editor
1099 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1100 $res = wfQuery( $sql );
1101 if( ($x = wfNumRows( $res )) != 1 ) {
1103 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1106 $s = wfFetchObject( $res );
1107 $ut = wfStrencode( $s->cur_user_text
);
1108 $uid = $s->cur_user
;
1111 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1112 if( $from != $s->cur_user_text
) {
1113 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1114 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1115 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1116 htmlspecialchars( $from ),
1117 htmlspecialchars( $s->cur_user_text
) ) );
1118 if($s->cur_comment
!= "") {
1120 wfMsg("editcomment",
1121 htmlspecialchars( $s->cur_comment
) ) );
1126 # Get the last edit not by this guy
1127 $sql = "SELECT old_text,old_user,old_user_text
1128 FROM old USE INDEX (name_title_timestamp)
1129 WHERE old_namespace={$n} AND old_title='{$tt}'
1130 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1131 ORDER BY inverse_timestamp LIMIT 1";
1132 $res = wfQuery( $sql );
1133 if( wfNumRows( $res ) != 1 ) {
1135 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1136 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1139 $s = wfFetchObject( $res );
1142 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
1143 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1144 $wgOut->setRobotpolicy( "noindex,nofollow" );
1145 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1146 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching() );
1148 $wgOut->returnToMain( false );
1152 # Do standard deferred updates after page view
1154 /* private */ function viewUpdates()
1156 global $wgDeferredUpdateList;
1158 if ( 0 != $this->getID() ) {
1159 $u = new ViewCountUpdate( $this->getID() );
1160 array_push( $wgDeferredUpdateList, $u );
1161 $u = new SiteStatsUpdate( 1, 0, 0 );
1162 array_push( $wgDeferredUpdateList, $u );
1164 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1165 $this->mTitle
->getDBkey() );
1166 array_push( $wgDeferredUpdateList, $u );
1170 # Do standard deferred updates after page edit.
1171 # Every 1000th edit, prune the recent changes table.
1173 /* private */ function editUpdates( $text )
1175 global $wgDeferredUpdateList;
1178 if ( 0 == mt_rand( 0, 999 ) ) {
1179 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1180 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1183 $id = $this->getID();
1184 $title = $this->mTitle
->getPrefixedDBkey();
1185 $adj = $this->mCountAdjustment
;
1188 $u = new LinksUpdate( $id, $title );
1189 array_push( $wgDeferredUpdateList, $u );
1190 $u = new SiteStatsUpdate( 0, 1, $adj );
1191 array_push( $wgDeferredUpdateList, $u );
1192 $u = new SearchUpdate( $id, $title, $text );
1193 array_push( $wgDeferredUpdateList, $u );
1195 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(),
1196 $this->mTitle
->getDBkey() );
1197 array_push( $wgDeferredUpdateList, $u );
1201 /* private */ function setOldSubtitle()
1203 global $wgLang, $wgOut;
1205 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1206 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1207 $wgOut->setSubtitle( "({$r})" );
1210 # This function is called right before saving the wikitext,
1211 # so we can do things like signatures and links-in-context.
1213 function preSaveTransform( $text )
1216 while ( "" != $text ) {
1217 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1218 $s .= $this->pstPass2( $p[0] );
1220 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1222 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1223 $s .= "<nowiki>{$q[0]}</nowiki>";
1230 /* private */ function pstPass2( $text )
1232 global $wgUser, $wgLang, $wgLocaltimezone;
1236 $n = $wgUser->getName();
1237 $k = $wgUser->getOption( "nickname" );
1238 if ( "" == $k ) { $k = $n; }
1239 if(isset($wgLocaltimezone)) {
1240 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1242 /* Note: this is an ugly timezone hack for the European wikis */
1243 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1244 " (" . date( "T" ) . ")";
1245 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1247 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1248 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1249 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1250 Namespace::getUser() ) . ":$n|$k]]", $text );
1252 # Context links: [[|name]] and [[name (context)|]]
1254 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1255 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1256 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1258 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1259 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1260 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1261 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1262 # [[ns:page (cont)|]]
1264 $t = $this->mTitle
->getText();
1265 if ( preg_match( $conpat, $t, $m ) ) {
1268 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1269 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1270 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1272 if ( "" == $context ) {
1273 $text = preg_replace( $p2, "[[\\1]]", $text );
1275 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1277 # Replace local image links with new [[image:]] style
1279 $text = preg_replace(
1280 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1281 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1282 "\\1[[image:\\3.\\4]]", $text );
1283 $text = preg_replace(
1284 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1285 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1286 "\\1[[image:\\3.\\4]]", $text );
1291 /* Caching functions */
1293 function tryFileCache() {
1294 if($this->isFileCacheable()) {
1295 $cache = new CacheManager( $this->mTitle
);
1296 if($cache->isFileCacheGood( $this->mTouched
)) {
1297 wfDebug( " tryFileCache() - about to load\n" );
1298 $cache->loadFromFileCache();
1301 wfDebug( " tryFileCache() - starting buffer\n" );
1302 if($cache->useGzip() && wfClientAcceptsGzip()) {
1303 /* For some reason, adding this header line over in
1304 CacheManager::saveToFileCache() fails on my test
1305 setup at home, though it works on the live install.
1306 Make double-sure... --brion */
1307 header( "Content-Encoding: gzip" );
1309 ob_start( array(&$cache, 'saveToFileCache' ) );
1312 wfDebug( " tryFileCache() - not cacheable\n" );
1316 function isFileCacheable() {
1317 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1318 global $action, $oldid, $diff, $redirect, $printable;
1319 return $wgUseFileCache
1320 and (!$wgShowIPinHeader)
1321 and ($wgUser->getId() == 0)
1322 and (!$wgUser->getNewtalk())
1323 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1324 and ($action == "view")
1325 and (!isset($oldid))
1327 and (!isset($redirect))
1328 and (!isset($printable))
1329 and (!$this->mRedirectedFrom
);