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.
9 /* private */ var $mContent, $mContentLoaded;
10 /* private */ var $mUser, $mTimestamp, $mUserText;
11 /* private */ var $mCounter, $mComment, $mCountAdjustment;
12 /* private */ var $mMinorEdit, $mRedirectedFrom;
13 /* private */ var $mTouched, $mFileCache;
15 function Article() { $this->clear(); }
17 /* private */ function clear()
19 $this->mContentLoaded
= false;
20 $this->mUser
= $this->mCounter
= -1; # Not loaded
21 $this->mRedirectedFrom
= $this->mUserText
=
22 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
23 $this->mCountAdjustment
= 0;
24 $this->mTouched
= "19700101000000";
27 /* static */ function newFromID( $newid )
29 global $wgOut, $wgTitle, $wgArticle;
31 $n = Article
::nameOf( $newid );
33 $wgTitle = Title
::newFromDBkey( $n );
34 $wgTitle->resetArticleID( $newid );
39 /* static */ function nameOf( $id )
41 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
43 $res = wfQuery( $sql, "Article::nameOf" );
44 if ( 0 == wfNumRows( $res ) ) { return NULL; }
46 $s = wfFetchObject( $res );
47 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
51 # Note that getContent/loadContent may follow redirects if
52 # not told otherwise, and so may cause a change to wgTitle.
54 function getContent( $noredir = false )
56 global $action,$section,$count,$wgTitle; # From query string
57 wfProfileIn( "Article::getContent" );
59 if ( 0 == $this->getID() ) {
60 if ( "edit" == $action ) {
63 return ""; # was "newarticletext", now moved above the box)
68 return wfMsg( "noarticletext" );
70 $this->loadContent( $noredir );
74 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
75 ( $wgTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
76 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$wgTitle->getText()) &&
80 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
84 if($section=="new") { return ""; }
86 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
88 PREG_SPLIT_DELIM_CAPTURE
);
90 return trim($secs[0]);
92 return trim($secs[$section*2-1] . $secs[$section*2]);
96 return $this->mContent
;
101 function loadContent( $noredir = false )
103 global $wgOut, $wgTitle;
104 global $oldid, $redirect; # From query
106 if ( $this->mContentLoaded
) return;
107 $fname = "Article::loadContent";
109 # Pre-fill content with error message so that if something
110 # fails we'll have something telling us what we intended.
112 $t = $wgTitle->getPrefixedText();
113 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
114 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
115 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
117 if ( ! $oldid ) { # Retrieve current version
118 $id = $this->getID();
119 if ( 0 == $id ) return;
122 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
123 "FROM cur WHERE cur_id={$id}";
124 $res = wfQuery( $sql, $fname );
125 if ( 0 == wfNumRows( $res ) ) { return; }
127 $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
132 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
133 ( preg_match( "/^#redirect/i", $s->cur_text
) ) ) {
134 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
135 $s->cur_text
, $m ) ) {
136 $rt = Title
::newFromText( $m[1] );
138 # Gotta hand redirects to special pages differently:
139 # Fill the HTTP response "Location" header and ignore
140 # the rest of the page we're on.
142 if ( $rt->getInterwiki() != "" ) {
143 $wgOut->redirect( $rt->getFullURL() ) ;
146 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
147 $wgOut->redirect( wfLocalUrl(
148 $rt->getPrefixedURL() ) );
151 $rid = $rt->getArticleID();
153 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
154 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
155 $res = wfQuery( $sql, $fname );
157 if ( 0 != wfNumRows( $res ) ) {
158 $this->mRedirectedFrom
= $wgTitle->getPrefixedText();
160 $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 $wgTitle->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
171 $wgTitle->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, $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;
189 function getID() { global $wgTitle; return $wgTitle->getArticleID(); }
193 if ( -1 == $this->mCounter
) {
194 $id = $this->getID();
195 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
197 return $this->mCounter
;
200 # Would the given text make this article a "good" article (i.e.,
201 # suitable for including in the article count)?
203 function isCountable( $text )
205 global $wgTitle, $wgUseCommaCount;
207 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
208 if ( preg_match( "/^#redirect/i", $text ) ) { return 0; }
209 $token = ($wgUseCommaCount ?
"," : "[[" );
210 if ( false === strstr( $text, $token ) ) { return 0; }
214 # Load the field related to the last edit time of the article.
215 # This isn't necessary for all uses, so it's only done if needed.
217 /* private */ function loadLastEdit()
220 if ( -1 != $this->mUser
) return;
222 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
223 "cur_comment,cur_minor_edit FROM cur WHERE " .
224 "cur_id=" . $this->getID();
225 $res = wfQuery( $sql, "Article::loadLastEdit" );
227 if ( wfNumRows( $res ) > 0 ) {
228 $s = wfFetchObject( $res );
229 $this->mUser
= $s->cur_user
;
230 $this->mUserText
= $s->cur_user_text
;
231 $this->mTimestamp
= $s->cur_timestamp
;
232 $this->mComment
= $s->cur_comment
;
233 $this->mMinorEdit
= $s->cur_minor_edit
;
237 function getTimestamp()
239 $this->loadLastEdit();
240 return $this->mTimestamp
;
245 $this->loadLastEdit();
249 function getUserText()
251 $this->loadLastEdit();
252 return $this->mUserText
;
255 function getComment()
257 $this->loadLastEdit();
258 return $this->mComment
;
261 function getMinorEdit()
263 $this->loadLastEdit();
264 return $this->mMinorEdit
;
267 # This is the default action of the script: just view the page of
272 global $wgUser, $wgOut, $wgTitle, $wgLang;
273 global $oldid, $diff; # From query
275 wfProfileIn( "Article::view" );
277 $wgOut->setArticleFlag( true );
278 $wgOut->setRobotpolicy( "index,follow" );
280 # If we got diff and oldid in the query, we want to see a
281 # diff page instead of the article.
283 if ( isset( $diff ) ) {
284 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
285 $de = new DifferenceEngine( $oldid, $diff );
290 $text = $this->getContent(); # May change wgTitle!
291 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
292 $wgOut->setHTMLTitle( $wgTitle->getPrefixedText() .
293 " - " . wfMsg( "wikititlesuffix" ) );
295 # We're looking at an old revision
298 $this->setOldSubtitle();
299 $wgOut->setRobotpolicy( "noindex,follow" );
301 if ( "" != $this->mRedirectedFrom
) {
302 $sk = $wgUser->getSkin();
303 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
305 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
306 $wgOut->setSubtitle( $s );
308 $wgOut->checkLastModified( $this->mTouched
);
309 $this->tryFileCache();
310 $wgLinkCache->preFill( $wgTitle );
311 $wgOut->addWikiText( $text );
313 # If the article we've just shown is in the "Image" namespace,
314 # follow it with the history list and link list for the image
317 if ( Namespace::getImage() == $wgTitle->getNamespace() ) {
318 $this->imageHistory();
321 $this->viewUpdates();
325 # Theoretically we could defer these whole insert and update
326 # functions for after display, but that's taking a big leap
327 # of faith, and we want to be able to report database
328 # errors at some point.
330 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
332 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
333 $fname = "Article::insertNewArticle";
335 $ns = $wgTitle->getNamespace();
336 $ttl = $wgTitle->getDBkey();
337 $text = $this->preSaveTransform( $text );
338 if ( preg_match( "/^#redirect/i", $text ) ) { $redir = 1; }
341 $now = wfTimestampNow();
342 $won = wfInvertTimestamp( $now );
344 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
345 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
346 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
347 "cur_restrictions,cur_user_text,cur_is_redirect," .
348 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
349 wfStrencode( $text ) . "', '" .
350 wfStrencode( $summary ) . "', '" .
351 $wgUser->getID() . "', '{$now}', " .
352 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
353 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
354 $res = wfQuery( $sql, $fname );
356 $newid = wfInsertId();
357 $wgTitle->resetArticleID( $newid );
359 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
360 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
361 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
362 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
363 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
364 wfStrencode( $wgUser->getName() ) . "','" .
365 wfStrencode( $summary ) . "',0,0," .
366 ( $wgUser->isBot() ?
1 : 0 ) . ")";
367 wfQuery( $sql, $fname );
369 if(!$wgTitle->userIsWatching()) $this->watch();
371 if ( $wgTitle->userIsWatching() ) {
376 $this->showArticle( $text, wfMsg( "newarticle" ) );
379 function updateArticle( $text, $summary, $minor, $watchthis, $section )
381 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
382 global $wgDBtransactions;
383 $fname = "Article::updateArticle";
384 // insert updated section into old text if we have only edited part
386 if ($section != "") {
387 $oldtext=$this->getContent();
388 if($section=="new") {
389 if($summary) $subject="== {$summary} ==\n\n";
390 $text=$oldtext."\n\n".$subject.$text;
392 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
393 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
394 $secs[$section*2]=$text."\n\n"; // replace with edited
395 if($section) { $secs[$section*2-1]=""; } // erase old headline
396 $text=join("",$secs);
399 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
400 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
401 if ( preg_match( "/^(#redirect[^\\n]+)/i", $text, $m ) ) {
403 $text = $m[1] . "\n"; # Remove all content but redirect
406 $this->loadLastEdit();
408 $text = $this->preSaveTransform( $text );
410 # Update article, but only if changed.
412 if( $wgDBtransactions ) {
416 $oldtext = $this->getContent( true );
418 if ( 0 != strcmp( $text, $oldtext ) ) {
419 $this->mCountAdjustment
= $this->isCountable( $text )
420 - $this->isCountable( $oldtext );
422 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
423 "old_comment,old_user,old_user_text,old_timestamp," .
424 "old_minor_edit,inverse_timestamp) VALUES (" .
425 $wgTitle->getNamespace() . ", '" .
426 wfStrencode( $wgTitle->getDBkey() ) . "', '" .
427 wfStrencode( $oldtext ) . "', '" .
428 wfStrencode( $this->getComment() ) . "', " .
429 $this->getUser() . ", '" .
430 wfStrencode( $this->getUserText() ) . "', '" .
431 $this->getTimestamp() . "', " . $me1 . ", '" .
432 wfInvertTimestamp( $this->getTimestamp() ) . "')";
433 $res = wfQuery( $sql, $fname );
434 $oldid = wfInsertID( $res );
436 $now = wfTimestampNow();
437 $won = wfInvertTimestamp( $now );
438 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
439 "',cur_comment='" . wfStrencode( $summary ) .
440 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
441 ",cur_timestamp='{$now}',cur_user_text='" .
442 wfStrencode( $wgUser->getName() ) .
443 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
444 "WHERE cur_id=" . $this->getID();
445 wfQuery( $sql, $fname );
447 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
448 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
449 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
450 "'{$now}','{$now}'," . $wgTitle->getNamespace() . ",'" .
451 wfStrencode( $wgTitle->getDBkey() ) . "',0,{$me2}," .
452 ( $wgUser->isBot() ?
1 : 0 ) . "," .
453 $this->getID() . "," . $wgUser->getID() . ",'" .
454 wfStrencode( $wgUser->getName() ) . "','" .
455 wfStrencode( $summary ) . "',0,{$oldid})";
456 wfQuery( $sql, $fname );
458 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
459 "WHERE rc_namespace=" . $wgTitle->getNamespace() . " AND " .
460 "rc_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' AND " .
461 "rc_timestamp='" . $this->getTimestamp() . "'";
462 wfQuery( $sql, $fname );
464 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
465 "WHERE rc_cur_id=" . $this->getID();
466 wfQuery( $sql, $fname );
468 if( $wgDBtransactions ) {
474 if (!$wgTitle->userIsWatching()) $this->watch();
476 if ( $wgTitle->userIsWatching() ) {
481 $this->showArticle( $text, wfMsg( "updated" ) );
484 # After we've either updated or inserted the article, update
485 # the link tables and redirect to the new page.
487 function showArticle( $text, $subtitle )
489 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
491 $wgLinkCache = new LinkCache();
493 # Get old version of link table to allow incremental link updates
494 if ( $wgUseBetterLinksUpdate ) {
495 $wgLinkCache->preFill( $wgTitle );
496 $wgLinkCache->clear();
499 # Now update the link cache by parsing the text
500 $wgOut->addWikiText( $text );
502 $this->editUpdates( $text );
503 if( preg_match( "/^#redirect/i", $text ) )
507 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL(), $r ) );
510 # If the page we've just displayed is in the "Image" namespace,
511 # we follow it with an upload history of the image and its usage.
513 function imageHistory()
515 global $wgUser, $wgOut, $wgLang, $wgTitle;
516 $fname = "Article::imageHistory";
518 $sql = "SELECT img_size,img_description,img_user," .
519 "img_user_text,img_timestamp FROM image WHERE " .
520 "img_name='" . wfStrencode( $wgTitle->getDBkey() ) . "'";
521 $res = wfQuery( $sql, $fname );
523 if ( 0 == wfNumRows( $res ) ) { return; }
525 $sk = $wgUser->getSkin();
526 $s = $sk->beginImageHistoryList();
528 $line = wfFetchObject( $res );
529 $s .= $sk->imageHistoryLine( true, $line->img_timestamp
,
530 $wgTitle->getText(), $line->img_user
,
531 $line->img_user_text
, $line->img_size
, $line->img_description
);
533 $sql = "SELECT oi_size,oi_description,oi_user," .
534 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
535 "oi_name='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
536 "ORDER BY oi_timestamp DESC";
537 $res = wfQuery( $sql, $fname );
539 while ( $line = wfFetchObject( $res ) ) {
540 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp
,
541 $line->oi_archive_name
, $line->oi_user
,
542 $line->oi_user_text
, $line->oi_size
, $line->oi_description
);
544 $s .= $sk->endImageHistoryList();
545 $wgOut->addHTML( $s );
548 function imageLinks()
550 global $wgUser, $wgOut, $wgTitle;
552 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
554 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
555 wfStrencode( $wgTitle->getDBkey() ) . "'";
556 $res = wfQuery( $sql, "Article::imageLinks" );
558 if ( 0 == wfNumRows( $res ) ) {
559 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
562 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
564 $sk = $wgUser->getSkin();
565 while ( $s = wfFetchObject( $res ) ) {
567 $link = $sk->makeKnownLink( $name, "" );
568 $wgOut->addHTML( "<li>{$link}</li>\n" );
570 $wgOut->addHTML( "</ul>\n" );
573 # Add this page to my watchlist
577 global $wgUser, $wgTitle, $wgOut, $wgLang;
578 global $wgDeferredUpdateList;
580 if ( 0 == $wgUser->getID() ) {
581 $wgOut->errorpage( "watchnologin", "watchnologintext" );
584 if ( wfReadOnly() ) {
585 $wgOut->readOnlyPage();
588 $wgUser->addWatch( $wgTitle );
590 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
591 $wgOut->setRobotpolicy( "noindex,follow" );
593 $sk = $wgUser->getSkin() ;
594 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
596 $text = str_replace( "$1", $link ,
597 wfMsg( "addedwatchtext" ) );
598 $wgOut->addHTML( $text );
600 $up = new UserUpdate();
601 array_push( $wgDeferredUpdateList, $up );
603 $wgOut->returnToMain( false );
608 global $wgUser, $wgTitle, $wgOut, $wgLang;
609 global $wgDeferredUpdateList;
611 if ( 0 == $wgUser->getID() ) {
612 $wgOut->errorpage( "watchnologin", "watchnologintext" );
615 if ( wfReadOnly() ) {
616 $wgOut->readOnlyPage();
619 $wgUser->removeWatch( $wgTitle );
621 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
622 $wgOut->setRobotpolicy( "noindex,follow" );
624 $sk = $wgUser->getSkin() ;
625 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
627 $text = str_replace( "$1", $link ,
628 wfMsg( "removedwatchtext" ) );
629 $wgOut->addHTML( $text );
631 $up = new UserUpdate();
632 array_push( $wgDeferredUpdateList, $up );
634 $wgOut->returnToMain( false );
637 # This shares a lot of issues (and code) with Recent Changes
641 global $wgUser, $wgOut, $wgLang, $wgTitle, $offset, $limit;
643 # If page hasn't changed, client can cache this
645 $wgOut->checkLastModified( $this->getTimestamp() );
646 wfProfileIn( "Article::history" );
648 $wgOut->setPageTitle( $wgTitle->getPRefixedText() );
649 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
650 $wgOut->setArticleFlag( false );
651 $wgOut->setRobotpolicy( "noindex,nofollow" );
653 if( $wgTitle->getArticleID() == 0 ) {
654 $wgOut->addHTML( wfMsg( "nohistory" ) );
659 $offset = (int)$offset;
660 $limit = (int)$limit;
661 if( $limit == 0 ) $limit = 50;
662 $namespace = $wgTitle->getNamespace();
663 $title = $wgTitle->getText();
664 $sql = "SELECT old_id,old_user," .
665 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
666 "FROM old USE INDEX (name_title_timestamp) " .
667 "WHERE old_namespace={$namespace} AND " .
668 "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
669 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
670 $res = wfQuery( $sql, "Article::history" );
672 $revs = wfNumRows( $res );
673 if( $wgTitle->getArticleID() == 0 ) {
674 $wgOut->addHTML( wfMsg( "nohistory" ) );
679 $sk = $wgUser->getSkin();
680 $numbar = wfViewPrevNext(
682 $wgTitle->getPrefixedText(),
685 $s .= $sk->beginHistoryList();
688 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
689 $this->getUserText(), $namespace,
690 $title, 0, $this->getComment(),
691 ( $this->getMinorEdit() > 0 ) );
693 $revs = wfNumRows( $res );
694 while ( $line = wfFetchObject( $res ) ) {
695 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
696 $line->old_user_text
, $namespace,
697 $title, $line->old_id
,
698 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
700 $s .= $sk->endHistoryList();
702 $wgOut->addHTML( $s );
708 global $wgUser, $wgOut, $wgTitle;
710 if ( ! $wgUser->isSysop() ) {
711 $wgOut->sysopRequired();
714 if ( wfReadOnly() ) {
715 $wgOut->readOnlyPage();
718 $id = $wgTitle->getArticleID();
720 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
723 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
724 "cur_restrictions='sysop' WHERE cur_id={$id}";
725 wfQuery( $sql, "Article::protect" );
727 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
732 global $wgUser, $wgOut, $wgTitle;
734 if ( ! $wgUser->isSysop() ) {
735 $wgOut->sysopRequired();
738 if ( wfReadOnly() ) {
739 $wgOut->readOnlyPage();
742 $id = $wgTitle->getArticleID();
744 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
747 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
748 "cur_restrictions='' WHERE cur_id={$id}";
749 wfQuery( $sql, "Article::unprotect" );
751 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
756 global $wgUser, $wgOut, $wgTitle;
757 global $wpConfirm, $wpReason, $image, $oldimage;
759 # Anybody can delete old revisions of images; only sysops
760 # can delete articles and current images
762 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
763 $wgOut->sysopRequired();
766 if ( wfReadOnly() ) {
767 $wgOut->readOnlyPage();
771 # Better double-check that it hasn't been deleted yet!
772 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
774 if ( "" == trim( $image ) ) {
775 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
778 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
781 if ( ( "" == trim( $wgTitle->getText() ) )
782 or ( $wgTitle->getArticleId() == 0 ) ) {
783 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
786 $sub = str_replace( "$1", $wgTitle->getPrefixedText(),
787 wfMsg( "deletesub" ) );
789 # determine whether this page has earlier revisions
790 # and insert a warning if it does
791 # we select the text because it might be useful below
792 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
793 $res=wfQuery($sql,$fname);
794 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
795 $skin=$wgUser->getSkin();
796 $wgOut->addHTML("<B>".wfMsg("historywarning"));
797 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
800 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."'";
801 $res=wfQuery($sql,$fname);
802 if( ($s=wfFetchObject($res))) {
804 # if this is a mini-text, we can paste part of it into the deletion reason
806 #if this is empty, an earlier revision may contain "useful" text
807 if($s->cur_text
!="") {
811 $text=$old->old_text
;
817 $length=strlen($text);
819 # this should not happen, since it is not possible to store an empty, new
820 # page. Let's insert a standard text in case it does, though
821 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
824 if($length < 500 && !$wpReason) {
826 # comment field=255, let's grep the first 150 to have some user
828 $text=substr($text,0,150);
829 # let's strip out newlines and HTML tags
830 $text=preg_replace("/\"/","'",$text);
831 $text=preg_replace("/\</","<",$text);
832 $text=preg_replace("/\>/",">",$text);
833 $text=preg_replace("/[\n\r]/","",$text);
835 $wpReason=wfMsg("excontent"). " '".$text;
837 $wpReason=wfMsg("exbeforeblank") . " '".$text;
839 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
846 # Likewise, deleting old images doesn't require confirmation
847 if ( $oldimage ||
1 == $wpConfirm ) {
852 $wgOut->setSubtitle( $sub );
853 $wgOut->setRobotpolicy( "noindex,nofollow" );
854 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
856 $t = $wgTitle->getPrefixedURL();
857 $q = "action=delete";
860 $q .= "&image={$image}";
861 } else if ( $oldimage ) {
862 $q .= "&oldimage={$oldimage}";
866 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
867 $confirm = wfMsg( "confirm" );
868 $check = wfMsg( "confirmcheck" );
869 $delcom = wfMsg( "deletecomment" );
872 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
873 <table border=0><tr><td align=right>
874 {$delcom}:</td><td align=left>
875 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
876 </td></tr><tr><td> </td></tr>
878 <input type=checkbox name=\"wpConfirm\" value='1'>
879 </td><td>{$check}</td>
880 </tr><tr><td> </td><td>
881 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
882 </td></tr></table></form>\n" );
884 $wgOut->returnToMain( false );
889 global $wgOut, $wgTitle, $wgUser, $wgLang;
890 global $image, $oldimage, $wpReason;
891 $fname = "Article::doDelete";
894 $dest = wfImageDir( $image );
895 $archive = wfImageDir( $image );
896 if ( ! unlink( "{$dest}/{$image}" ) ) {
897 $wgOut->fileDeleteError( "{$dest}/{$image}" );
900 $sql = "DELETE FROM image WHERE img_name='" .
901 wfStrencode( $image ) . "'";
902 wfQuery( $sql, $fname );
904 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
905 wfStrencode( $image ) . "'";
906 $res = wfQuery( $sql, $fname );
908 while ( $s = wfFetchObject( $res ) ) {
909 $this->doDeleteOldImage( $s->oi_archive_name
);
911 $sql = "DELETE FROM oldimage WHERE oi_name='" .
912 wfStrencode( $image ) . "'";
913 wfQuery( $sql, $fname );
915 # Image itself is now gone, and database is cleaned.
916 # Now we remove the image description page.
918 $nt = Title
::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
919 $this->doDeleteArticle( $nt );
922 } else if ( $oldimage ) {
923 $this->doDeleteOldImage( $oldimage );
924 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
925 wfStrencode( $oldimage ) . "'";
926 wfQuery( $sql, $fname );
928 $deleted = $oldimage;
930 $this->doDeleteArticle( $wgTitle );
931 $deleted = $wgTitle->getPrefixedText();
933 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
934 $wgOut->setRobotpolicy( "noindex,nofollow" );
936 $sk = $wgUser->getSkin();
937 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
938 Namespace::getWikipedia() ) .
939 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
941 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
942 $text = str_replace( "$2", $loglink, $text );
944 $wgOut->addHTML( "<p>" . $text );
945 $wgOut->returnToMain( false );
948 function doDeleteOldImage( $oldimage )
952 $name = substr( $oldimage, 15 );
953 $archive = wfImageArchiveDir( $name );
954 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
955 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
959 function doDeleteArticle( $title )
961 global $wgUser, $wgOut, $wgLang, $wpReason, $wgTitle, $wgDeferredUpdateList;
963 $fname = "Article::doDeleteArticle";
964 $ns = $title->getNamespace();
965 $t = wfStrencode( $title->getDBkey() );
966 $id = $title->getArticleID();
969 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
973 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
974 array_push( $wgDeferredUpdateList, $u );
976 # Move article and history to the "archive" table
977 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
978 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
979 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
980 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
981 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
982 wfQuery( $sql, $fname );
984 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
985 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
986 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
987 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
988 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
989 wfQuery( $sql, $fname );
991 # Now that it's safely backed up, delete it
993 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
995 wfQuery( $sql, $fname );
997 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
999 wfQuery( $sql, $fname );
1001 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1003 wfQuery( $sql, $fname );
1005 # Finally, clean up the link tables
1008 $t = wfStrencode( $title->getPrefixedDBkey() );
1009 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1010 $res = wfQuery( $sql, $fname );
1012 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1013 $now = wfTimestampNow();
1014 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1017 while ( $s = wfFetchObject( $res ) ) {
1018 $nt = Title
::newFromDBkey( $s->l_from
);
1019 $lid = $nt->getArticleID();
1021 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1023 $sql .= "({$lid},'{$t}')";
1028 wfQuery( $sql, $fname );
1029 wfQuery( $sql2, $fname );
1031 wfFreeResult( $res );
1033 $sql = "DELETE FROM links WHERE l_to={$id}";
1034 wfQuery( $sql, $fname );
1036 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1037 wfQuery( $sql, $fname );
1039 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1040 wfQuery( $sql, $fname );
1042 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1043 wfQuery( $sql, $fname );
1046 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1047 $art = $title->getPrefixedText();
1048 $wpReason = wfCleanQueryVar( $wpReason );
1049 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1051 # Clear the cached article id so the interface doesn't act like we exist
1052 $wgTitle->resetArticleID( 0 );
1053 $wgTitle->mArticleID
= 0;
1061 if ( strlen( $oldimage ) < 16 ) {
1062 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1065 if ( wfReadOnly() ) {
1066 $wgOut->readOnlyPage();
1069 $name = substr( $oldimage, 15 );
1071 $dest = wfImageDir( $name );
1072 $archive = wfImageArchiveDir( $name );
1073 $curfile = "{$dest}/{$name}";
1075 if ( ! is_file( $curfile ) ) {
1076 $wgOut->fileNotFoundError( $curfile );
1079 $oldver = wfTimestampNow() . "!{$name}";
1080 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1081 wfStrencode( $oldimage ) . "'" );
1083 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1084 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1087 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1088 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1090 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1092 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1093 $wgOut->setRobotpolicy( "noindex,nofollow" );
1094 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1095 $wgOut->returnToMain( false );
1100 global $wgUser, $wgTitle, $wgLang, $wgOut, $from;
1102 if ( ! $wgUser->isSysop() ) {
1103 $wgOut->sysopRequired();
1107 # Replace all this user's current edits with the next one down
1108 $tt = wfStrencode( $wgTitle->getDBKey() );
1109 $n = $wgTitle->getNamespace();
1111 # Get the last editor
1112 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1113 $res = wfQuery( $sql );
1114 if( ($x = wfNumRows( $res )) != 1 ) {
1116 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1119 $s = wfFetchObject( $res );
1120 $ut = wfStrencode( $s->cur_user_text
);
1121 $uid = $s->cur_user
;
1124 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1125 if( $from != $s->cur_user_text
) {
1126 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1127 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1128 htmlspecialchars( $wgTitle->getPrefixedText()),
1129 htmlspecialchars( $from ),
1130 htmlspecialchars( $s->cur_user_text
) ) );
1131 if($s->cur_comment
!= "") {
1133 wfMsg("editcomment",
1134 htmlspecialchars( $s->cur_comment
) ) );
1139 # Get the last edit not by this guy
1140 $sql = "SELECT old_text,old_user,old_user_text
1141 FROM old USE INDEX (name_title_timestamp)
1142 WHERE old_namespace={$n} AND old_title='{$tt}'
1143 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1144 ORDER BY inverse_timestamp LIMIT 1";
1145 $res = wfQuery( $sql );
1146 if( wfNumRows( $res ) != 1 ) {
1148 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1149 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1152 $s = wfFetchObject( $res );
1155 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
1156 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1157 $wgOut->setRobotpolicy( "noindex,nofollow" );
1158 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1159 $this->updateArticle( $s->old_text
, $newcomment, 1, $wgTitle->userIsWatching() );
1161 $wgOut->returnToMain( false );
1165 # Do standard deferred updates after page view
1167 /* private */ function viewUpdates()
1169 global $wgDeferredUpdateList, $wgTitle;
1171 if ( 0 != $this->getID() ) {
1172 $u = new ViewCountUpdate( $this->getID() );
1173 array_push( $wgDeferredUpdateList, $u );
1174 $u = new SiteStatsUpdate( 1, 0, 0 );
1175 array_push( $wgDeferredUpdateList, $u );
1177 $u = new UserTalkUpdate( 0, $wgTitle->getNamespace(),
1178 $wgTitle->getDBkey() );
1179 array_push( $wgDeferredUpdateList, $u );
1183 # Do standard deferred updates after page edit.
1184 # Every 1000th edit, prune the recent changes table.
1186 /* private */ function editUpdates( $text )
1188 global $wgDeferredUpdateList, $wgTitle;
1191 if ( 0 == mt_rand( 0, 999 ) ) {
1192 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1193 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1196 $id = $this->getID();
1197 $title = $wgTitle->getPrefixedDBkey();
1198 $adj = $this->mCountAdjustment
;
1201 $u = new LinksUpdate( $id, $title );
1202 array_push( $wgDeferredUpdateList, $u );
1203 $u = new SiteStatsUpdate( 0, 1, $adj );
1204 array_push( $wgDeferredUpdateList, $u );
1205 $u = new SearchUpdate( $id, $title, $text );
1206 array_push( $wgDeferredUpdateList, $u );
1208 $u = new UserTalkUpdate( 1, $wgTitle->getNamespace(),
1209 $wgTitle->getDBkey() );
1210 array_push( $wgDeferredUpdateList, $u );
1214 /* private */ function setOldSubtitle()
1216 global $wgLang, $wgOut;
1218 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1219 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1220 $wgOut->setSubtitle( "({$r})" );
1223 # This function is called right before saving the wikitext,
1224 # so we can do things like signatures and links-in-context.
1226 function preSaveTransform( $text )
1229 while ( "" != $text ) {
1230 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1231 $s .= $this->pstPass2( $p[0] );
1233 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1235 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1236 $s .= "<nowiki>{$q[0]}</nowiki>";
1243 /* private */ function pstPass2( $text )
1245 global $wgUser, $wgLang, $wgTitle, $wgLocaltimezone;
1249 $n = $wgUser->getName();
1250 $k = $wgUser->getOption( "nickname" );
1251 if ( "" == $k ) { $k = $n; }
1252 if(isset($wgLocaltimezone)) {
1253 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1255 /* Note: this is an ugly timezone hack for the European wikis */
1256 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1257 " (" . date( "T" ) . ")";
1258 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1260 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1261 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1262 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1263 Namespace::getUser() ) . ":$n|$k]]", $text );
1265 # Context links: [[|name]] and [[name (context)|]]
1267 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1268 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1269 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1271 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1272 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1273 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1274 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1275 # [[ns:page (cont)|]]
1277 $t = $wgTitle->getText();
1278 if ( preg_match( $conpat, $t, $m ) ) {
1281 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1282 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1283 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1285 if ( "" == $context ) {
1286 $text = preg_replace( $p2, "[[\\1]]", $text );
1288 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1290 # Replace local image links with new [[image:]] style
1292 $text = preg_replace(
1293 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1294 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1295 "\\1[[image:\\3.\\4]]", $text );
1296 $text = preg_replace(
1297 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1298 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1299 "\\1[[image:\\3.\\4]]", $text );
1304 /* Caching functions */
1306 function tryFileCache() {
1309 if($this->isFileCacheable()) {
1310 $cache = new CacheManager( $wgTitle );
1311 if($cache->isFileCacheGood( $this->mTouched
)) {
1312 wfDebug( " tryFileCache() - about to load\n" );
1313 $cache->loadFromFileCache();
1316 wfDebug( " tryFileCache() - starting buffer\n" );
1317 if($cache->useGzip()) {
1318 /* For some reason, adding this header line over in
1319 CacheManager::saveToFileCache() fails on my test
1320 setup at home, though it works on the live install.
1321 Make double-sure... --brion */
1322 header( "Content-Encoding: gzip" );
1324 ob_start( array(&$cache, 'saveToFileCache' ) );
1327 wfDebug( " tryFileCache() - not cacheable\n" );
1331 function isFileCacheable() {
1332 global $wgUser, $wgTitle, $wgUseFileCache, $wgShowIPinHeader;
1333 global $action, $oldid, $diff, $redirect, $printable;
1334 return $wgUseFileCache
1335 and (!$wgShowIPinHeader)
1336 and ($wgUser->getId() == 0)
1337 and (!$wgUser->getNewtalk())
1338 and ($wgTitle->getNamespace
!= Namespace::getSpecial())
1339 and ($action == "view")
1340 and (!isset($oldid))
1342 and (!isset($redirect))
1343 and (!isset($printable))
1344 and (!$this->mRedirectedFrom
);