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() { $this->clear(); }
19 /* private */ function clear()
21 $this->mContentLoaded
= false;
22 $this->mUser
= $this->mCounter
= -1; # Not loaded
23 $this->mRedirectedFrom
= $this->mUserText
=
24 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
25 $this->mCountAdjustment
= 0;
26 $this->mTouched
= "19700101000000";
29 /* static */ function newFromID( $newid )
31 global $wgOut, $wgTitle, $wgArticle;
33 $n = Article
::nameOf( $newid );
35 $wgTitle = Title
::newFromDBkey( $n );
36 $wgTitle->resetArticleID( $newid );
41 /* static */ function nameOf( $id )
43 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
45 $res = wfQuery( $sql, "Article::nameOf" );
46 if ( 0 == wfNumRows( $res ) ) { return NULL; }
48 $s = wfFetchObject( $res );
49 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
53 # Note that getContent/loadContent may follow redirects if
54 # not told otherwise, and so may cause a change to wgTitle.
56 function getContent( $noredir = false )
58 global $action,$section,$count,$wgTitle; # From query string
59 wfProfileIn( "Article::getContent" );
61 if ( 0 == $this->getID() ) {
62 if ( "edit" == $action ) {
65 return ""; # was "newarticletext", now moved above the box)
70 return wfMsg( "noarticletext" );
72 $this->loadContent( $noredir );
76 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
77 ( $wgTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
78 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$wgTitle->getText()) &&
82 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
86 if($section=="new") { return ""; }
88 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
90 PREG_SPLIT_DELIM_CAPTURE
);
92 return trim($secs[0]);
94 return trim($secs[$section*2-1] . $secs[$section*2]);
98 return $this->mContent
;
103 function loadContent( $noredir = false )
105 global $wgOut, $wgTitle;
106 global $oldid, $redirect; # From query
108 if ( $this->mContentLoaded
) return;
109 $fname = "Article::loadContent";
111 # Pre-fill content with error message so that if something
112 # fails we'll have something telling us what we intended.
114 $t = $wgTitle->getPrefixedText();
115 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
116 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
117 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
119 if ( ! $oldid ) { # Retrieve current version
120 $id = $this->getID();
121 if ( 0 == $id ) return;
124 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
125 "FROM cur WHERE cur_id={$id}";
126 $res = wfQuery( $sql, $fname );
127 if ( 0 == wfNumRows( $res ) ) { return; }
129 $s = wfFetchObject( $res );
131 # If we got a redirect, follow it (unless we've been told
132 # not to by either the function parameter or the query
134 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
135 ( preg_match( "/^#redirect/i", $s->cur_text
) ) ) {
136 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
137 $s->cur_text
, $m ) ) {
138 $rt = Title
::newFromText( $m[1] );
140 # Gotta hand redirects to special pages differently:
141 # Fill the HTTP response "Location" header and ignore
142 # the rest of the page we're on.
144 if ( $rt->getInterwiki() != "" ) {
145 $wgOut->redirect( $rt->getFullURL() ) ;
148 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
149 $wgOut->redirect( wfLocalUrl(
150 $rt->getPrefixedURL() ) );
153 $rid = $rt->getArticleID();
155 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
156 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
157 $res = wfQuery( $sql, $fname );
159 if ( 0 != wfNumRows( $res ) ) {
160 $this->mRedirectedFrom
= $wgTitle->getPrefixedText();
162 $s = wfFetchObject( $res );
167 $this->mContent
= $s->cur_text
;
168 $this->mUser
= $s->cur_user
;
169 $this->mCounter
= $s->cur_counter
;
170 $this->mTimestamp
= $s->cur_timestamp
;
171 $this->mTouched
= $s->cur_touched
;
172 $wgTitle->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
173 $wgTitle->mRestrictionsLoaded
= true;
174 wfFreeResult( $res );
175 } else { # oldid set, retrieve historical version
176 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
177 "WHERE old_id={$oldid}";
178 $res = wfQuery( $sql, $fname );
179 if ( 0 == wfNumRows( $res ) ) { return; }
181 $s = wfFetchObject( $res );
182 $this->mContent
= $s->old_text
;
183 $this->mUser
= $s->old_user
;
185 $this->mTimestamp
= $s->old_timestamp
;
186 wfFreeResult( $res );
188 $this->mContentLoaded
= true;
191 function getID() { global $wgTitle; return $wgTitle->getArticleID(); }
195 if ( -1 == $this->mCounter
) {
196 $id = $this->getID();
197 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
199 return $this->mCounter
;
202 # Would the given text make this article a "good" article (i.e.,
203 # suitable for including in the article count)?
205 function isCountable( $text )
207 global $wgTitle, $wgUseCommaCount;
209 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
210 if ( preg_match( "/^#redirect/i", $text ) ) { return 0; }
211 $token = ($wgUseCommaCount ?
"," : "[[" );
212 if ( false === strstr( $text, $token ) ) { return 0; }
216 # Load the field related to the last edit time of the article.
217 # This isn't necessary for all uses, so it's only done if needed.
219 /* private */ function loadLastEdit()
222 if ( -1 != $this->mUser
) return;
224 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
225 "cur_comment,cur_minor_edit FROM cur WHERE " .
226 "cur_id=" . $this->getID();
227 $res = wfQuery( $sql, "Article::loadLastEdit" );
229 if ( wfNumRows( $res ) > 0 ) {
230 $s = wfFetchObject( $res );
231 $this->mUser
= $s->cur_user
;
232 $this->mUserText
= $s->cur_user_text
;
233 $this->mTimestamp
= $s->cur_timestamp
;
234 $this->mComment
= $s->cur_comment
;
235 $this->mMinorEdit
= $s->cur_minor_edit
;
239 function getTimestamp()
241 $this->loadLastEdit();
242 return $this->mTimestamp
;
247 $this->loadLastEdit();
251 function getUserText()
253 $this->loadLastEdit();
254 return $this->mUserText
;
257 function getComment()
259 $this->loadLastEdit();
260 return $this->mComment
;
263 function getMinorEdit()
265 $this->loadLastEdit();
266 return $this->mMinorEdit
;
269 # This is the default action of the script: just view the page of
274 global $wgUser, $wgOut, $wgTitle, $wgLang;
275 global $oldid, $diff; # From query
277 wfProfileIn( "Article::view" );
279 $wgOut->setArticleFlag( true );
280 $wgOut->setRobotpolicy( "index,follow" );
282 # If we got diff and oldid in the query, we want to see a
283 # diff page instead of the article.
285 if ( isset( $diff ) ) {
286 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
287 $de = new DifferenceEngine( $oldid, $diff );
292 $text = $this->getContent(); # May change wgTitle!
293 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
294 $wgOut->setHTMLTitle( $wgTitle->getPrefixedText() .
295 " - " . wfMsg( "wikititlesuffix" ) );
297 # We're looking at an old revision
300 $this->setOldSubtitle();
301 $wgOut->setRobotpolicy( "noindex,follow" );
303 if ( "" != $this->mRedirectedFrom
) {
304 $sk = $wgUser->getSkin();
305 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
307 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
308 $wgOut->setSubtitle( $s );
310 $wgOut->checkLastModified( $this->mTouched
);
311 $this->tryFileCache();
312 $wgLinkCache->preFill( $wgTitle );
313 $wgOut->addWikiText( $text );
315 # If the article we've just shown is in the "Image" namespace,
316 # follow it with the history list and link list for the image
319 if ( Namespace::getImage() == $wgTitle->getNamespace() ) {
320 $this->imageHistory();
323 $this->viewUpdates();
327 # Theoretically we could defer these whole insert and update
328 # functions for after display, but that's taking a big leap
329 # of faith, and we want to be able to report database
330 # errors at some point.
332 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
334 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
335 $fname = "Article::insertNewArticle";
337 $ns = $wgTitle->getNamespace();
338 $ttl = $wgTitle->getDBkey();
339 $text = $this->preSaveTransform( $text );
340 if ( preg_match( "/^#redirect/i", $text ) ) { $redir = 1; }
343 $now = wfTimestampNow();
344 $won = wfInvertTimestamp( $now );
346 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
347 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
348 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
349 "cur_restrictions,cur_user_text,cur_is_redirect," .
350 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
351 wfStrencode( $text ) . "', '" .
352 wfStrencode( $summary ) . "', '" .
353 $wgUser->getID() . "', '{$now}', " .
354 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
355 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
356 $res = wfQuery( $sql, $fname );
358 $newid = wfInsertId();
359 $wgTitle->resetArticleID( $newid );
361 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
362 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
363 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
364 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
365 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
366 wfStrencode( $wgUser->getName() ) . "','" .
367 wfStrencode( $summary ) . "',0,0," .
368 ( $wgUser->isBot() ?
1 : 0 ) . ")";
369 wfQuery( $sql, $fname );
371 if(!$wgTitle->userIsWatching()) $this->watch();
373 if ( $wgTitle->userIsWatching() ) {
378 $this->showArticle( $text, wfMsg( "newarticle" ) );
381 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
383 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
384 global $wgDBtransactions;
385 $fname = "Article::updateArticle";
387 $this->loadLastEdit();
389 // insert updated section into old text if we have only edited part
391 if ($section != "") {
392 $oldtext=$this->getContent();
393 if($section=="new") {
394 if($summary) $subject="== {$summary} ==\n\n";
395 $text=$oldtext."\n\n".$subject.$text;
397 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
398 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
399 $secs[$section*2]=$text."\n\n"; // replace with edited
400 if($section) { $secs[$section*2-1]=""; } // erase old headline
401 $text=join("",$secs);
404 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
405 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
406 if ( preg_match( "/^(#redirect[^\\n]+)/i", $text, $m ) ) {
408 $text = $m[1] . "\n"; # Remove all content but redirect
412 $text = $this->preSaveTransform( $text );
414 # Update article, but only if changed.
416 if( $wgDBtransactions ) {
420 $oldtext = $this->getContent( true );
422 if ( 0 != strcmp( $text, $oldtext ) ) {
423 $this->mCountAdjustment
= $this->isCountable( $text )
424 - $this->isCountable( $oldtext );
426 $now = wfTimestampNow();
427 $won = wfInvertTimestamp( $now );
428 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
429 "',cur_comment='" . wfStrencode( $summary ) .
430 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
431 ",cur_timestamp='{$now}',cur_user_text='" .
432 wfStrencode( $wgUser->getName() ) .
433 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
434 "WHERE cur_id=" . $this->getID() .
435 " AND cur_timestamp='" . $this->getTimestamp() . "'";
436 $res = wfQuery( $sql, $fname );
438 if( wfAffectedRows() == 0 ) {
439 /* Belated edit conflict! Run away!! */
443 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
444 "old_comment,old_user,old_user_text,old_timestamp," .
445 "old_minor_edit,inverse_timestamp) VALUES (" .
446 $wgTitle->getNamespace() . ", '" .
447 wfStrencode( $wgTitle->getDBkey() ) . "', '" .
448 wfStrencode( $oldtext ) . "', '" .
449 wfStrencode( $this->getComment() ) . "', " .
450 $this->getUser() . ", '" .
451 wfStrencode( $this->getUserText() ) . "', '" .
452 $this->getTimestamp() . "', " . $me1 . ", '" .
453 wfInvertTimestamp( $this->getTimestamp() ) . "')";
454 $res = wfQuery( $sql, $fname );
455 $oldid = wfInsertID( $res );
457 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
458 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
459 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
460 "'{$now}','{$now}'," . $wgTitle->getNamespace() . ",'" .
461 wfStrencode( $wgTitle->getDBkey() ) . "',0,{$me2}," .
462 ( $wgUser->isBot() ?
1 : 0 ) . "," .
463 $this->getID() . "," . $wgUser->getID() . ",'" .
464 wfStrencode( $wgUser->getName() ) . "','" .
465 wfStrencode( $summary ) . "',0,{$oldid})";
466 wfQuery( $sql, $fname );
468 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
469 "WHERE rc_namespace=" . $wgTitle->getNamespace() . " AND " .
470 "rc_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' AND " .
471 "rc_timestamp='" . $this->getTimestamp() . "'";
472 wfQuery( $sql, $fname );
474 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
475 "WHERE rc_cur_id=" . $this->getID();
476 wfQuery( $sql, $fname );
478 if( $wgDBtransactions ) {
484 if (!$wgTitle->userIsWatching()) $this->watch();
486 if ( $wgTitle->userIsWatching() ) {
491 $this->showArticle( $text, wfMsg( "updated" ) );
495 # After we've either updated or inserted the article, update
496 # the link tables and redirect to the new page.
498 function showArticle( $text, $subtitle )
500 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
502 $wgLinkCache = new LinkCache();
504 # Get old version of link table to allow incremental link updates
505 if ( $wgUseBetterLinksUpdate ) {
506 $wgLinkCache->preFill( $wgTitle );
507 $wgLinkCache->clear();
510 # Now update the link cache by parsing the text
511 $wgOut->addWikiText( $text );
513 $this->editUpdates( $text );
514 if( preg_match( "/^#redirect/i", $text ) )
518 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL(), $r ) );
521 # If the page we've just displayed is in the "Image" namespace,
522 # we follow it with an upload history of the image and its usage.
524 function imageHistory()
526 global $wgUser, $wgOut, $wgLang, $wgTitle;
527 $fname = "Article::imageHistory";
529 $sql = "SELECT img_size,img_description,img_user," .
530 "img_user_text,img_timestamp FROM image WHERE " .
531 "img_name='" . wfStrencode( $wgTitle->getDBkey() ) . "'";
532 $res = wfQuery( $sql, $fname );
534 if ( 0 == wfNumRows( $res ) ) { return; }
536 $sk = $wgUser->getSkin();
537 $s = $sk->beginImageHistoryList();
539 $line = wfFetchObject( $res );
540 $s .= $sk->imageHistoryLine( true, $line->img_timestamp
,
541 $wgTitle->getText(), $line->img_user
,
542 $line->img_user_text
, $line->img_size
, $line->img_description
);
544 $sql = "SELECT oi_size,oi_description,oi_user," .
545 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
546 "oi_name='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
547 "ORDER BY oi_timestamp DESC";
548 $res = wfQuery( $sql, $fname );
550 while ( $line = wfFetchObject( $res ) ) {
551 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp
,
552 $line->oi_archive_name
, $line->oi_user
,
553 $line->oi_user_text
, $line->oi_size
, $line->oi_description
);
555 $s .= $sk->endImageHistoryList();
556 $wgOut->addHTML( $s );
559 function imageLinks()
561 global $wgUser, $wgOut, $wgTitle;
563 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
565 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
566 wfStrencode( $wgTitle->getDBkey() ) . "'";
567 $res = wfQuery( $sql, "Article::imageLinks" );
569 if ( 0 == wfNumRows( $res ) ) {
570 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
573 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
575 $sk = $wgUser->getSkin();
576 while ( $s = wfFetchObject( $res ) ) {
578 $link = $sk->makeKnownLink( $name, "" );
579 $wgOut->addHTML( "<li>{$link}</li>\n" );
581 $wgOut->addHTML( "</ul>\n" );
584 # Add this page to my watchlist
588 global $wgUser, $wgTitle, $wgOut, $wgLang;
589 global $wgDeferredUpdateList;
591 if ( 0 == $wgUser->getID() ) {
592 $wgOut->errorpage( "watchnologin", "watchnologintext" );
595 if ( wfReadOnly() ) {
596 $wgOut->readOnlyPage();
599 $wgUser->addWatch( $wgTitle );
601 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
602 $wgOut->setRobotpolicy( "noindex,follow" );
604 $sk = $wgUser->getSkin() ;
605 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
607 $text = str_replace( "$1", $link ,
608 wfMsg( "addedwatchtext" ) );
609 $wgOut->addHTML( $text );
611 $up = new UserUpdate();
612 array_push( $wgDeferredUpdateList, $up );
614 $wgOut->returnToMain( false );
619 global $wgUser, $wgTitle, $wgOut, $wgLang;
620 global $wgDeferredUpdateList;
622 if ( 0 == $wgUser->getID() ) {
623 $wgOut->errorpage( "watchnologin", "watchnologintext" );
626 if ( wfReadOnly() ) {
627 $wgOut->readOnlyPage();
630 $wgUser->removeWatch( $wgTitle );
632 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
633 $wgOut->setRobotpolicy( "noindex,follow" );
635 $sk = $wgUser->getSkin() ;
636 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
638 $text = str_replace( "$1", $link ,
639 wfMsg( "removedwatchtext" ) );
640 $wgOut->addHTML( $text );
642 $up = new UserUpdate();
643 array_push( $wgDeferredUpdateList, $up );
645 $wgOut->returnToMain( false );
648 # This shares a lot of issues (and code) with Recent Changes
652 global $wgUser, $wgOut, $wgLang, $wgTitle, $offset, $limit;
654 # If page hasn't changed, client can cache this
656 $wgOut->checkLastModified( $this->getTimestamp() );
657 wfProfileIn( "Article::history" );
659 $wgOut->setPageTitle( $wgTitle->getPRefixedText() );
660 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
661 $wgOut->setArticleFlag( false );
662 $wgOut->setRobotpolicy( "noindex,nofollow" );
664 if( $wgTitle->getArticleID() == 0 ) {
665 $wgOut->addHTML( wfMsg( "nohistory" ) );
670 $offset = (int)$offset;
671 $limit = (int)$limit;
672 if( $limit == 0 ) $limit = 50;
673 $namespace = $wgTitle->getNamespace();
674 $title = $wgTitle->getText();
675 $sql = "SELECT old_id,old_user," .
676 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
677 "FROM old USE INDEX (name_title_timestamp) " .
678 "WHERE old_namespace={$namespace} AND " .
679 "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
680 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
681 $res = wfQuery( $sql, "Article::history" );
683 $revs = wfNumRows( $res );
684 if( $wgTitle->getArticleID() == 0 ) {
685 $wgOut->addHTML( wfMsg( "nohistory" ) );
690 $sk = $wgUser->getSkin();
691 $numbar = wfViewPrevNext(
693 $wgTitle->getPrefixedText(),
696 $s .= $sk->beginHistoryList();
699 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
700 $this->getUserText(), $namespace,
701 $title, 0, $this->getComment(),
702 ( $this->getMinorEdit() > 0 ) );
704 $revs = wfNumRows( $res );
705 while ( $line = wfFetchObject( $res ) ) {
706 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
707 $line->old_user_text
, $namespace,
708 $title, $line->old_id
,
709 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
711 $s .= $sk->endHistoryList();
713 $wgOut->addHTML( $s );
719 global $wgUser, $wgOut, $wgTitle;
721 if ( ! $wgUser->isSysop() ) {
722 $wgOut->sysopRequired();
725 if ( wfReadOnly() ) {
726 $wgOut->readOnlyPage();
729 $id = $wgTitle->getArticleID();
731 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
734 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
735 "cur_restrictions='sysop' WHERE cur_id={$id}";
736 wfQuery( $sql, "Article::protect" );
738 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
743 global $wgUser, $wgOut, $wgTitle;
745 if ( ! $wgUser->isSysop() ) {
746 $wgOut->sysopRequired();
749 if ( wfReadOnly() ) {
750 $wgOut->readOnlyPage();
753 $id = $wgTitle->getArticleID();
755 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
758 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
759 "cur_restrictions='' WHERE cur_id={$id}";
760 wfQuery( $sql, "Article::unprotect" );
762 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
767 global $wgUser, $wgOut, $wgTitle;
768 global $wpConfirm, $wpReason, $image, $oldimage;
770 # Anybody can delete old revisions of images; only sysops
771 # can delete articles and current images
773 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
774 $wgOut->sysopRequired();
777 if ( wfReadOnly() ) {
778 $wgOut->readOnlyPage();
782 # Better double-check that it hasn't been deleted yet!
783 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
785 if ( "" == trim( $image ) ) {
786 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
789 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
792 if ( ( "" == trim( $wgTitle->getText() ) )
793 or ( $wgTitle->getArticleId() == 0 ) ) {
794 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
797 $sub = str_replace( "$1", $wgTitle->getPrefixedText(),
798 wfMsg( "deletesub" ) );
800 # determine whether this page has earlier revisions
801 # and insert a warning if it does
802 # we select the text because it might be useful below
803 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
804 $res=wfQuery($sql,$fname);
805 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
806 $skin=$wgUser->getSkin();
807 $wgOut->addHTML("<B>".wfMsg("historywarning"));
808 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
811 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."'";
812 $res=wfQuery($sql,$fname);
813 if( ($s=wfFetchObject($res))) {
815 # if this is a mini-text, we can paste part of it into the deletion reason
817 #if this is empty, an earlier revision may contain "useful" text
818 if($s->cur_text
!="") {
822 $text=$old->old_text
;
828 $length=strlen($text);
830 # this should not happen, since it is not possible to store an empty, new
831 # page. Let's insert a standard text in case it does, though
832 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
835 if($length < 500 && !$wpReason) {
837 # comment field=255, let's grep the first 150 to have some user
839 $text=substr($text,0,150);
840 # let's strip out newlines and HTML tags
841 $text=preg_replace("/\"/","'",$text);
842 $text=preg_replace("/\</","<",$text);
843 $text=preg_replace("/\>/",">",$text);
844 $text=preg_replace("/[\n\r]/","",$text);
846 $wpReason=wfMsg("excontent"). " '".$text;
848 $wpReason=wfMsg("exbeforeblank") . " '".$text;
850 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
857 # Likewise, deleting old images doesn't require confirmation
858 if ( $oldimage ||
1 == $wpConfirm ) {
863 $wgOut->setSubtitle( $sub );
864 $wgOut->setRobotpolicy( "noindex,nofollow" );
865 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
867 $t = $wgTitle->getPrefixedURL();
868 $q = "action=delete";
871 $q .= "&image={$image}";
872 } else if ( $oldimage ) {
873 $q .= "&oldimage={$oldimage}";
877 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
878 $confirm = wfMsg( "confirm" );
879 $check = wfMsg( "confirmcheck" );
880 $delcom = wfMsg( "deletecomment" );
883 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
884 <table border=0><tr><td align=right>
885 {$delcom}:</td><td align=left>
886 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
887 </td></tr><tr><td> </td></tr>
889 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
890 </td><td><label for=\"wpConfirm\">{$check}</label></td>
891 </tr><tr><td> </td><td>
892 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
893 </td></tr></table></form>\n" );
895 $wgOut->returnToMain( false );
900 global $wgOut, $wgTitle, $wgUser, $wgLang;
901 global $image, $oldimage, $wpReason;
902 $fname = "Article::doDelete";
905 $dest = wfImageDir( $image );
906 $archive = wfImageDir( $image );
907 if ( ! unlink( "{$dest}/{$image}" ) ) {
908 $wgOut->fileDeleteError( "{$dest}/{$image}" );
911 $sql = "DELETE FROM image WHERE img_name='" .
912 wfStrencode( $image ) . "'";
913 wfQuery( $sql, $fname );
915 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
916 wfStrencode( $image ) . "'";
917 $res = wfQuery( $sql, $fname );
919 while ( $s = wfFetchObject( $res ) ) {
920 $this->doDeleteOldImage( $s->oi_archive_name
);
922 $sql = "DELETE FROM oldimage WHERE oi_name='" .
923 wfStrencode( $image ) . "'";
924 wfQuery( $sql, $fname );
926 # Image itself is now gone, and database is cleaned.
927 # Now we remove the image description page.
929 $nt = Title
::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
930 $this->doDeleteArticle( $nt );
933 } else if ( $oldimage ) {
934 $this->doDeleteOldImage( $oldimage );
935 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
936 wfStrencode( $oldimage ) . "'";
937 wfQuery( $sql, $fname );
939 $deleted = $oldimage;
941 $this->doDeleteArticle( $wgTitle );
942 $deleted = $wgTitle->getPrefixedText();
944 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
945 $wgOut->setRobotpolicy( "noindex,nofollow" );
947 $sk = $wgUser->getSkin();
948 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
949 Namespace::getWikipedia() ) .
950 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
952 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
953 $text = str_replace( "$2", $loglink, $text );
955 $wgOut->addHTML( "<p>" . $text );
956 $wgOut->returnToMain( false );
959 function doDeleteOldImage( $oldimage )
963 $name = substr( $oldimage, 15 );
964 $archive = wfImageArchiveDir( $name );
965 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
966 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
970 function doDeleteArticle( $title )
972 global $wgUser, $wgOut, $wgLang, $wpReason, $wgTitle, $wgDeferredUpdateList;
974 $fname = "Article::doDeleteArticle";
975 $ns = $title->getNamespace();
976 $t = wfStrencode( $title->getDBkey() );
977 $id = $title->getArticleID();
980 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
984 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
985 array_push( $wgDeferredUpdateList, $u );
987 # Move article and history to the "archive" table
988 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
989 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
990 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
991 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
992 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
993 wfQuery( $sql, $fname );
995 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
996 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
997 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
998 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
999 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1000 wfQuery( $sql, $fname );
1002 # Now that it's safely backed up, delete it
1004 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1006 wfQuery( $sql, $fname );
1008 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1010 wfQuery( $sql, $fname );
1012 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1014 wfQuery( $sql, $fname );
1016 # Finally, clean up the link tables
1019 $t = wfStrencode( $title->getPrefixedDBkey() );
1020 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1021 $res = wfQuery( $sql, $fname );
1023 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1024 $now = wfTimestampNow();
1025 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1028 while ( $s = wfFetchObject( $res ) ) {
1029 $nt = Title
::newFromDBkey( $s->l_from
);
1030 $lid = $nt->getArticleID();
1032 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1034 $sql .= "({$lid},'{$t}')";
1039 wfQuery( $sql, $fname );
1040 wfQuery( $sql2, $fname );
1042 wfFreeResult( $res );
1044 $sql = "DELETE FROM links WHERE l_to={$id}";
1045 wfQuery( $sql, $fname );
1047 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1048 wfQuery( $sql, $fname );
1050 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1051 wfQuery( $sql, $fname );
1053 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1054 wfQuery( $sql, $fname );
1057 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1058 $art = $title->getPrefixedText();
1059 $wpReason = wfCleanQueryVar( $wpReason );
1060 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1062 # Clear the cached article id so the interface doesn't act like we exist
1063 $wgTitle->resetArticleID( 0 );
1064 $wgTitle->mArticleID
= 0;
1072 if ( strlen( $oldimage ) < 16 ) {
1073 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1076 if ( wfReadOnly() ) {
1077 $wgOut->readOnlyPage();
1080 $name = substr( $oldimage, 15 );
1082 $dest = wfImageDir( $name );
1083 $archive = wfImageArchiveDir( $name );
1084 $curfile = "{$dest}/{$name}";
1086 if ( ! is_file( $curfile ) ) {
1087 $wgOut->fileNotFoundError( $curfile );
1090 $oldver = wfTimestampNow() . "!{$name}";
1091 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1092 wfStrencode( $oldimage ) . "'" );
1094 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1095 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1098 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1099 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1101 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1103 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1104 $wgOut->setRobotpolicy( "noindex,nofollow" );
1105 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1106 $wgOut->returnToMain( false );
1111 global $wgUser, $wgTitle, $wgLang, $wgOut, $from;
1113 if ( ! $wgUser->isSysop() ) {
1114 $wgOut->sysopRequired();
1118 # Replace all this user's current edits with the next one down
1119 $tt = wfStrencode( $wgTitle->getDBKey() );
1120 $n = $wgTitle->getNamespace();
1122 # Get the last editor
1123 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1124 $res = wfQuery( $sql );
1125 if( ($x = wfNumRows( $res )) != 1 ) {
1127 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1130 $s = wfFetchObject( $res );
1131 $ut = wfStrencode( $s->cur_user_text
);
1132 $uid = $s->cur_user
;
1135 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1136 if( $from != $s->cur_user_text
) {
1137 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1138 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1139 htmlspecialchars( $wgTitle->getPrefixedText()),
1140 htmlspecialchars( $from ),
1141 htmlspecialchars( $s->cur_user_text
) ) );
1142 if($s->cur_comment
!= "") {
1144 wfMsg("editcomment",
1145 htmlspecialchars( $s->cur_comment
) ) );
1150 # Get the last edit not by this guy
1151 $sql = "SELECT old_text,old_user,old_user_text
1152 FROM old USE INDEX (name_title_timestamp)
1153 WHERE old_namespace={$n} AND old_title='{$tt}'
1154 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1155 ORDER BY inverse_timestamp LIMIT 1";
1156 $res = wfQuery( $sql );
1157 if( wfNumRows( $res ) != 1 ) {
1159 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1160 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1163 $s = wfFetchObject( $res );
1166 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
1167 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1168 $wgOut->setRobotpolicy( "noindex,nofollow" );
1169 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1170 $this->updateArticle( $s->old_text
, $newcomment, 1, $wgTitle->userIsWatching() );
1172 $wgOut->returnToMain( false );
1176 # Do standard deferred updates after page view
1178 /* private */ function viewUpdates()
1180 global $wgDeferredUpdateList, $wgTitle;
1182 if ( 0 != $this->getID() ) {
1183 $u = new ViewCountUpdate( $this->getID() );
1184 array_push( $wgDeferredUpdateList, $u );
1185 $u = new SiteStatsUpdate( 1, 0, 0 );
1186 array_push( $wgDeferredUpdateList, $u );
1188 $u = new UserTalkUpdate( 0, $wgTitle->getNamespace(),
1189 $wgTitle->getDBkey() );
1190 array_push( $wgDeferredUpdateList, $u );
1194 # Do standard deferred updates after page edit.
1195 # Every 1000th edit, prune the recent changes table.
1197 /* private */ function editUpdates( $text )
1199 global $wgDeferredUpdateList, $wgTitle;
1202 if ( 0 == mt_rand( 0, 999 ) ) {
1203 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1204 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1207 $id = $this->getID();
1208 $title = $wgTitle->getPrefixedDBkey();
1209 $adj = $this->mCountAdjustment
;
1212 $u = new LinksUpdate( $id, $title );
1213 array_push( $wgDeferredUpdateList, $u );
1214 $u = new SiteStatsUpdate( 0, 1, $adj );
1215 array_push( $wgDeferredUpdateList, $u );
1216 $u = new SearchUpdate( $id, $title, $text );
1217 array_push( $wgDeferredUpdateList, $u );
1219 $u = new UserTalkUpdate( 1, $wgTitle->getNamespace(),
1220 $wgTitle->getDBkey() );
1221 array_push( $wgDeferredUpdateList, $u );
1225 /* private */ function setOldSubtitle()
1227 global $wgLang, $wgOut;
1229 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1230 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1231 $wgOut->setSubtitle( "({$r})" );
1234 # This function is called right before saving the wikitext,
1235 # so we can do things like signatures and links-in-context.
1237 function preSaveTransform( $text )
1240 while ( "" != $text ) {
1241 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1242 $s .= $this->pstPass2( $p[0] );
1244 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1246 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1247 $s .= "<nowiki>{$q[0]}</nowiki>";
1254 /* private */ function pstPass2( $text )
1256 global $wgUser, $wgLang, $wgTitle, $wgLocaltimezone;
1260 $n = $wgUser->getName();
1261 $k = $wgUser->getOption( "nickname" );
1262 if ( "" == $k ) { $k = $n; }
1263 if(isset($wgLocaltimezone)) {
1264 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1266 /* Note: this is an ugly timezone hack for the European wikis */
1267 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1268 " (" . date( "T" ) . ")";
1269 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1271 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1272 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1273 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1274 Namespace::getUser() ) . ":$n|$k]]", $text );
1276 # Context links: [[|name]] and [[name (context)|]]
1278 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1279 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1280 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1282 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1283 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1284 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1285 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1286 # [[ns:page (cont)|]]
1288 $t = $wgTitle->getText();
1289 if ( preg_match( $conpat, $t, $m ) ) {
1292 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1293 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1294 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1296 if ( "" == $context ) {
1297 $text = preg_replace( $p2, "[[\\1]]", $text );
1299 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1301 # Replace local image links with new [[image:]] style
1303 $text = preg_replace(
1304 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1305 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1306 "\\1[[image:\\3.\\4]]", $text );
1307 $text = preg_replace(
1308 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1309 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1310 "\\1[[image:\\3.\\4]]", $text );
1315 /* Caching functions */
1317 function tryFileCache() {
1320 if($this->isFileCacheable()) {
1321 $cache = new CacheManager( $wgTitle );
1322 if($cache->isFileCacheGood( $this->mTouched
)) {
1323 wfDebug( " tryFileCache() - about to load\n" );
1324 $cache->loadFromFileCache();
1327 wfDebug( " tryFileCache() - starting buffer\n" );
1328 if($cache->useGzip() && wfClientAcceptsGzip()) {
1329 /* For some reason, adding this header line over in
1330 CacheManager::saveToFileCache() fails on my test
1331 setup at home, though it works on the live install.
1332 Make double-sure... --brion */
1333 header( "Content-Encoding: gzip" );
1335 ob_start( array(&$cache, 'saveToFileCache' ) );
1338 wfDebug( " tryFileCache() - not cacheable\n" );
1342 function isFileCacheable() {
1343 global $wgUser, $wgTitle, $wgUseFileCache, $wgShowIPinHeader;
1344 global $action, $oldid, $diff, $redirect, $printable;
1345 return $wgUseFileCache
1346 and (!$wgShowIPinHeader)
1347 and ($wgUser->getId() == 0)
1348 and (!$wgUser->getNewtalk())
1349 and ($wgTitle->getNamespace
!= Namespace::getSpecial())
1350 and ($action == "view")
1351 and (!isset($oldid))
1353 and (!isset($redirect))
1354 and (!isset($printable))
1355 and (!$this->mRedirectedFrom
);