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 $fname = "Article::getContent";
39 wfProfileIn( $fname );
41 if ( 0 == $this->getID() ) {
42 if ( "edit" == $action ) {
43 wfProfileOut( $fname );
44 return ""; # was "newarticletext", now moved above the box)
46 wfProfileOut( $fname );
47 return wfMsg( "noarticletext" );
49 $this->loadContent( $noredir );
52 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
53 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
54 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
58 wfProfileOut( $fname );
59 return $this->mContent
. "\n" .wfMsg("anontalkpagetext"); }
64 wfProfileOut( $fname );
68 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
70 PREG_SPLIT_DELIM_CAPTURE
);
72 wfProfileOut( $fname );
73 return trim($secs[0]);
75 wfProfileOut( $fname );
76 return trim($secs[$section*2-1] . $secs[$section*2]);
80 wfProfileOut( $fname );
81 return $this->mContent
;
86 function loadContent( $noredir = false )
88 global $wgOut, $wgMwRedir;
89 global $oldid, $redirect; # From query
91 if ( $this->mContentLoaded
) return;
92 $fname = "Article::loadContent";
94 # Pre-fill content with error message so that if something
95 # fails we'll have something telling us what we intended.
97 $t = $this->mTitle
->getPrefixedText();
98 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
99 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
101 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
103 if ( ! $oldid ) { # Retrieve current version
104 $id = $this->getID();
105 if ( 0 == $id ) return;
108 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
109 "FROM cur WHERE cur_id={$id}";
111 $res = wfQuery( $sql, DB_READ
, $fname );
112 if ( 0 == wfNumRows( $res ) ) {
116 $s = wfFetchObject( $res );
117 # If we got a redirect, follow it (unless we've been told
118 # not to by either the function parameter or the query
119 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
120 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
121 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
122 $s->cur_text
, $m ) ) {
123 $rt = Title
::newFromText( $m[1] );
125 # Gotta hand redirects to special pages differently:
126 # Fill the HTTP response "Location" header and ignore
127 # the rest of the page we're on.
129 if ( $rt->getInterwiki() != "" ) {
130 $wgOut->redirect( $rt->getFullURL() ) ;
133 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
134 $wgOut->redirect( wfLocalUrl(
135 $rt->getPrefixedURL() ) );
138 $rid = $rt->getArticleID();
140 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
141 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
142 $res = wfQuery( $sql, DB_READ
, $fname );
144 if ( 0 != wfNumRows( $res ) ) {
145 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
147 $s = wfFetchObject( $res );
153 $this->mContent
= $s->cur_text
;
154 $this->mUser
= $s->cur_user
;
155 $this->mCounter
= $s->cur_counter
;
156 $this->mTimestamp
= $s->cur_timestamp
;
157 $this->mTouched
= $s->cur_touched
;
158 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
159 $this->mTitle
->mRestrictionsLoaded
= true;
160 wfFreeResult( $res );
161 } else { # oldid set, retrieve historical version
162 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
163 "WHERE old_id={$oldid}";
164 $res = wfQuery( $sql, DB_READ
, $fname );
165 if ( 0 == wfNumRows( $res ) ) { return; }
167 $s = wfFetchObject( $res );
168 $this->mContent
= $s->old_text
;
169 $this->mUser
= $s->old_user
;
171 $this->mTimestamp
= $s->old_timestamp
;
172 wfFreeResult( $res );
174 $this->mContentLoaded
= true;
177 function getID() { return $this->mTitle
->getArticleID(); }
181 if ( -1 == $this->mCounter
) {
182 $id = $this->getID();
183 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
185 return $this->mCounter
;
188 # Would the given text make this article a "good" article (i.e.,
189 # suitable for including in the article count)?
191 function isCountable( $text )
193 global $wgUseCommaCount, $wgMwRedir;
195 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
196 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
197 $token = ($wgUseCommaCount ?
"," : "[[" );
198 if ( false === strstr( $text, $token ) ) { return 0; }
202 # Load the field related to the last edit time of the article.
203 # This isn't necessary for all uses, so it's only done if needed.
205 /* private */ function loadLastEdit()
208 if ( -1 != $this->mUser
) return;
210 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
211 "cur_comment,cur_minor_edit FROM cur WHERE " .
212 "cur_id=" . $this->getID();
213 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
215 if ( wfNumRows( $res ) > 0 ) {
216 $s = wfFetchObject( $res );
217 $this->mUser
= $s->cur_user
;
218 $this->mUserText
= $s->cur_user_text
;
219 $this->mTimestamp
= $s->cur_timestamp
;
220 $this->mComment
= $s->cur_comment
;
221 $this->mMinorEdit
= $s->cur_minor_edit
;
225 function getTimestamp()
227 $this->loadLastEdit();
228 return $this->mTimestamp
;
233 $this->loadLastEdit();
237 function getUserText()
239 $this->loadLastEdit();
240 return $this->mUserText
;
243 function getComment()
245 $this->loadLastEdit();
246 return $this->mComment
;
249 function getMinorEdit()
251 $this->loadLastEdit();
252 return $this->mMinorEdit
;
255 # This is the default action of the script: just view the page of
260 global $wgUser, $wgOut, $wgLang;
261 global $oldid, $diff; # From query
262 global $wgLinkCache, $IP;
263 $fname = "Article::view";
264 wfProfileIn( $fname );
266 $wgOut->setArticleFlag( true );
267 $wgOut->setRobotpolicy( "index,follow" );
269 # If we got diff and oldid in the query, we want to see a
270 # diff page instead of the article.
272 if ( isset( $diff ) ) {
273 include_once( "$IP/DifferenceEngine.php" );
274 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
275 $de = new DifferenceEngine( $oldid, $diff );
277 wfProfileOut( $fname );
280 $text = $this->getContent(); # May change wgTitle!
281 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
282 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
283 " - " . wfMsg( "wikititlesuffix" ) );
285 # We're looking at an old revision
288 $this->setOldSubtitle();
289 $wgOut->setRobotpolicy( "noindex,follow" );
291 if ( "" != $this->mRedirectedFrom
) {
292 $sk = $wgUser->getSkin();
293 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
295 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
296 $wgOut->setSubtitle( $s );
298 $wgOut->checkLastModified( $this->mTouched
);
299 $this->tryFileCache();
300 $wgLinkCache->preFill( $this->mTitle
);
301 $wgOut->addWikiText( $text );
303 $this->viewUpdates();
304 wfProfileOut( $fname );
307 # Theoretically we could defer these whole insert and update
308 # functions for after display, but that's taking a big leap
309 # of faith, and we want to be able to report database
310 # errors at some point.
312 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
314 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
315 $fname = "Article::insertNewArticle";
317 $ns = $this->mTitle
->getNamespace();
318 $ttl = $this->mTitle
->getDBkey();
319 $text = $this->preSaveTransform( $text );
320 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
323 $now = wfTimestampNow();
324 $won = wfInvertTimestamp( $now );
326 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
327 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
328 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
329 "cur_restrictions,cur_user_text,cur_is_redirect," .
330 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
331 wfStrencode( $text ) . "', '" .
332 wfStrencode( $summary ) . "', '" .
333 $wgUser->getID() . "', '{$now}', " .
334 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
335 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
336 $res = wfQuery( $sql, DB_WRITE
, $fname );
338 $newid = wfInsertId();
339 $this->mTitle
->resetArticleID( $newid );
341 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
342 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
343 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
344 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
345 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
346 wfStrencode( $wgUser->getName() ) . "','" .
347 wfStrencode( $summary ) . "',0,0," .
348 ( $wgUser->isBot() ?
1 : 0 ) . ")";
349 wfQuery( $sql, DB_WRITE
, $fname );
351 if(!$this->mTitle
->userIsWatching()) $this->watch();
353 if ( $this->mTitle
->userIsWatching() ) {
358 $this->showArticle( $text, wfMsg( "newarticle" ) );
361 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
363 global $wgOut, $wgUser, $wgLinkCache;
364 global $wgDBtransactions, $wgMwRedir;
365 $fname = "Article::updateArticle";
367 $this->loadLastEdit();
369 // insert updated section into old text if we have only edited part
371 if ($section != "") {
372 $oldtext=$this->getContent();
373 if($section=="new") {
374 if($summary) $subject="== {$summary} ==\n\n";
375 $text=$oldtext."\n\n".$subject.$text;
377 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
378 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
379 $secs[$section*2]=$text."\n\n"; // replace with edited
380 if($section) { $secs[$section*2-1]=""; } // erase old headline
381 $text=join("",$secs);
384 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
385 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
386 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
388 $text = $m[1] . "\n"; # Remove all content but redirect
392 $text = $this->preSaveTransform( $text );
394 # Update article, but only if changed.
396 if( $wgDBtransactions ) {
398 wfQuery( $sql, DB_WRITE
);
400 $oldtext = $this->getContent( true );
402 if ( 0 != strcmp( $text, $oldtext ) ) {
403 $this->mCountAdjustment
= $this->isCountable( $text )
404 - $this->isCountable( $oldtext );
406 $now = wfTimestampNow();
407 $won = wfInvertTimestamp( $now );
408 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
409 "',cur_comment='" . wfStrencode( $summary ) .
410 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
411 ",cur_timestamp='{$now}',cur_user_text='" .
412 wfStrencode( $wgUser->getName() ) .
413 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
414 "WHERE cur_id=" . $this->getID() .
415 " AND cur_timestamp='" . $this->getTimestamp() . "'";
416 $res = wfQuery( $sql, DB_WRITE
, $fname );
418 if( wfAffectedRows() == 0 ) {
419 /* Belated edit conflict! Run away!! */
423 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
424 "old_comment,old_user,old_user_text,old_timestamp," .
425 "old_minor_edit,inverse_timestamp) VALUES (" .
426 $this->mTitle
->getNamespace() . ", '" .
427 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
428 wfStrencode( $oldtext ) . "', '" .
429 wfStrencode( $this->getComment() ) . "', " .
430 $this->getUser() . ", '" .
431 wfStrencode( $this->getUserText() ) . "', '" .
432 $this->getTimestamp() . "', " . $me1 . ", '" .
433 wfInvertTimestamp( $this->getTimestamp() ) . "')";
434 $res = wfQuery( $sql, DB_WRITE
, $fname );
435 $oldid = wfInsertID( $res );
437 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
438 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
439 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
440 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
441 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
442 ( $wgUser->isBot() ?
1 : 0 ) . "," .
443 $this->getID() . "," . $wgUser->getID() . ",'" .
444 wfStrencode( $wgUser->getName() ) . "','" .
445 wfStrencode( $summary ) . "',0,{$oldid})";
446 wfQuery( $sql, DB_WRITE
, $fname );
448 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
449 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
450 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
451 "rc_timestamp='" . $this->getTimestamp() . "'";
452 wfQuery( $sql, DB_WRITE
, $fname );
454 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
455 "WHERE rc_cur_id=" . $this->getID();
456 wfQuery( $sql, DB_WRITE
, $fname );
458 if( $wgDBtransactions ) {
460 wfQuery( $sql, DB_WRITE
);
464 if (!$this->mTitle
->userIsWatching()) $this->watch();
466 if ( $this->mTitle
->userIsWatching() ) {
471 $this->showArticle( $text, wfMsg( "updated" ) );
475 # After we've either updated or inserted the article, update
476 # the link tables and redirect to the new page.
478 function showArticle( $text, $subtitle )
480 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
483 $wgLinkCache = new LinkCache();
485 # Get old version of link table to allow incremental link updates
486 if ( $wgUseBetterLinksUpdate ) {
487 $wgLinkCache->preFill( $this->mTitle
);
488 $wgLinkCache->clear();
491 # Now update the link cache by parsing the text
492 $wgOut = new OutputPage();
493 $wgOut->addWikiText( $text );
495 $this->editUpdates( $text );
496 if( $wgMwRedir->matchStart( $text ) )
500 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
503 # Add this page to my watchlist
505 function watch( $add = true )
507 global $wgUser, $wgOut, $wgLang;
508 global $wgDeferredUpdateList;
510 if ( 0 == $wgUser->getID() ) {
511 $wgOut->errorpage( "watchnologin", "watchnologintext" );
514 if ( wfReadOnly() ) {
515 $wgOut->readOnlyPage();
519 $wgUser->addWatch( $this->mTitle
);
521 $wgUser->removeWatch( $this->mTitle
);
523 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
524 $wgOut->setRobotpolicy( "noindex,follow" );
526 $sk = $wgUser->getSkin() ;
527 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
530 $text = wfMsg( "addedwatchtext", $link );
532 $text = wfMsg( "removedwatchtext", $link );
533 $wgOut->addHTML( $text );
535 $up = new UserUpdate();
536 array_push( $wgDeferredUpdateList, $up );
538 $wgOut->returnToMain( false );
543 $this->watch( false );
546 # This shares a lot of issues (and code) with Recent Changes
550 global $wgUser, $wgOut, $wgLang, $offset, $limit;
552 # If page hasn't changed, client can cache this
554 $wgOut->checkLastModified( $this->getTimestamp() );
555 $fname = "Article::history";
556 wfProfileIn( $fname );
558 $wgOut->setPageTitle( $this->mTitle
->getPRefixedText() );
559 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
560 $wgOut->setArticleFlag( false );
561 $wgOut->setRobotpolicy( "noindex,nofollow" );
563 if( $this->mTitle
->getArticleID() == 0 ) {
564 $wgOut->addHTML( wfMsg( "nohistory" ) );
565 wfProfileOut( $fname );
569 $offset = (int)$offset;
570 $limit = (int)$limit;
571 if( $limit == 0 ) $limit = 50;
572 $namespace = $this->mTitle
->getNamespace();
573 $title = $this->mTitle
->getText();
574 $sql = "SELECT old_id,old_user," .
575 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
576 "FROM old USE INDEX (name_title_timestamp) " .
577 "WHERE old_namespace={$namespace} AND " .
578 "old_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
579 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
580 $res = wfQuery( $sql, DB_READ
, "Article::history" );
582 $revs = wfNumRows( $res );
583 if( $this->mTitle
->getArticleID() == 0 ) {
584 $wgOut->addHTML( wfMsg( "nohistory" ) );
585 wfProfileOut( $fname );
589 $sk = $wgUser->getSkin();
590 $numbar = wfViewPrevNext(
592 $this->mTitle
->getPrefixedText(),
595 $s .= $sk->beginHistoryList();
598 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
599 $this->getUserText(), $namespace,
600 $title, 0, $this->getComment(),
601 ( $this->getMinorEdit() > 0 ) );
603 $revs = wfNumRows( $res );
604 while ( $line = wfFetchObject( $res ) ) {
605 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
606 $line->old_user_text
, $namespace,
607 $title, $line->old_id
,
608 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
610 $s .= $sk->endHistoryList();
612 $wgOut->addHTML( $s );
613 wfProfileOut( $fname );
616 function protect( $limit = "sysop" )
618 global $wgUser, $wgOut;
620 if ( ! $wgUser->isSysop() ) {
621 $wgOut->sysopRequired();
624 if ( wfReadOnly() ) {
625 $wgOut->readOnlyPage();
628 $id = $this->mTitle
->getArticleID();
630 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
633 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
634 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
635 wfQuery( $sql, DB_WRITE
, "Article::protect" );
637 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
642 return $this->protect( "" );
647 global $wgUser, $wgOut;
648 global $wpConfirm, $wpReason, $image, $oldimage;
650 # Anybody can delete old revisions of images; only sysops
651 # can delete articles and current images
653 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
654 $wgOut->sysopRequired();
657 if ( wfReadOnly() ) {
658 $wgOut->readOnlyPage();
662 # Better double-check that it hasn't been deleted yet!
663 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
664 if ( ( "" == trim( $this->mTitle
->getText() ) )
665 or ( $this->mTitle
->getArticleId() == 0 ) ) {
666 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
670 # determine whether this page has earlier revisions
671 # and insert a warning if it does
672 # we select the text because it might be useful below
673 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
674 $res=wfQuery($sql, DB_READ
, $fname);
675 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
676 $skin=$wgUser->getSkin();
677 $wgOut->addHTML("<B>".wfMsg("historywarning"));
678 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
681 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."'";
682 $res=wfQuery($sql, DB_READ
, $fname);
683 if( ($s=wfFetchObject($res))) {
685 # if this is a mini-text, we can paste part of it into the deletion reason
687 #if this is empty, an earlier revision may contain "useful" text
688 if($s->cur_text
!="") {
692 $text=$old->old_text
;
698 $length=strlen($text);
700 # this should not happen, since it is not possible to store an empty, new
701 # page. Let's insert a standard text in case it does, though
702 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
705 if($length < 500 && !$wpReason) {
707 # comment field=255, let's grep the first 150 to have some user
709 $text=substr($text,0,150);
710 # let's strip out newlines and HTML tags
711 $text=preg_replace("/\"/","'",$text);
712 $text=preg_replace("/\</","<",$text);
713 $text=preg_replace("/\>/",">",$text);
714 $text=preg_replace("/[\n\r]/","",$text);
716 $wpReason=wfMsg("excontent"). " '".$text;
718 $wpReason=wfMsg("exbeforeblank") . " '".$text;
720 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
725 return $this->confirmDelete();
728 function confirmDelete( $par = "" )
732 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
733 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
734 $wgOut->setRobotpolicy( "noindex,nofollow" );
735 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
737 $t = $this->mTitle
->getPrefixedURL();
739 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
740 $confirm = wfMsg( "confirm" );
741 $check = wfMsg( "confirmcheck" );
742 $delcom = wfMsg( "deletecomment" );
745 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
746 <table border=0><tr><td align=right>
747 {$delcom}:</td><td align=left>
748 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
749 </td></tr><tr><td> </td></tr>
751 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
752 </td><td><label for=\"wpConfirm\">{$check}</label></td>
753 </tr><tr><td> </td><td>
754 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
755 </td></tr></table></form>\n" );
757 $wgOut->returnToMain( false );
762 global $wgOut, $wgUser, $wgLang;
764 $fname = "Article::doDelete";
766 $this->doDeleteArticle( $this->mTitle
);
767 $deleted = $this->mTitle
->getPrefixedText();
769 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
770 $wgOut->setRobotpolicy( "noindex,nofollow" );
772 $sk = $wgUser->getSkin();
773 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
774 Namespace::getWikipedia() ) .
775 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
777 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
778 $text = str_replace( "$2", $loglink, $text );
780 $wgOut->addHTML( "<p>" . $text );
781 $wgOut->returnToMain( false );
784 function doDeleteArticle( $title )
786 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
788 $fname = "Article::doDeleteArticle";
789 $ns = $title->getNamespace();
790 $t = wfStrencode( $title->getDBkey() );
791 $id = $title->getArticleID();
794 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
798 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
799 array_push( $wgDeferredUpdateList, $u );
801 # Move article and history to the "archive" table
802 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
803 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
804 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
805 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
806 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
807 wfQuery( $sql, DB_WRITE
, $fname );
809 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
810 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
811 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
812 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
813 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
814 wfQuery( $sql, DB_WRITE
, $fname );
816 # Now that it's safely backed up, delete it
818 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
820 wfQuery( $sql, DB_WRITE
, $fname );
822 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
824 wfQuery( $sql, DB_WRITE
, $fname );
826 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
828 wfQuery( $sql, DB_WRITE
, $fname );
830 # Finally, clean up the link tables
833 $t = wfStrencode( $title->getPrefixedDBkey() );
834 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
835 $res = wfQuery( $sql, DB_READ
, $fname );
837 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
838 $now = wfTimestampNow();
839 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
842 while ( $s = wfFetchObject( $res ) ) {
843 $nt = Title
::newFromDBkey( $s->l_from
);
844 $lid = $nt->getArticleID();
846 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
848 $sql .= "({$lid},'{$t}')";
853 wfQuery( $sql, DB_WRITE
, $fname );
854 wfQuery( $sql2, DB_WRITE
, $fname );
856 wfFreeResult( $res );
858 $sql = "DELETE FROM links WHERE l_to={$id}";
859 wfQuery( $sql, DB_WRITE
, $fname );
861 $sql = "DELETE FROM links WHERE l_from='{$t}'";
862 wfQuery( $sql, DB_WRITE
, $fname );
864 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
865 wfQuery( $sql, DB_WRITE
, $fname );
867 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
868 wfQuery( $sql, DB_WRITE
, $fname );
871 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
872 $art = $title->getPrefixedText();
873 $wpReason = wfCleanQueryVar( $wpReason );
874 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
876 # Clear the cached article id so the interface doesn't act like we exist
877 $this->mTitle
->resetArticleID( 0 );
878 $this->mTitle
->mArticleID
= 0;
883 global $wgUser, $wgLang, $wgOut, $from;
885 if ( ! $wgUser->isSysop() ) {
886 $wgOut->sysopRequired();
890 # Replace all this user's current edits with the next one down
891 $tt = wfStrencode( $this->mTitle
->getDBKey() );
892 $n = $this->mTitle
->getNamespace();
894 # Get the last editor
895 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
896 $res = wfQuery( $sql, DB_READ
);
897 if( ($x = wfNumRows( $res )) != 1 ) {
899 $wgOut->addHTML( wfMsg( "notanarticle" ) );
902 $s = wfFetchObject( $res );
903 $ut = wfStrencode( $s->cur_user_text
);
907 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
908 if( $from != $s->cur_user_text
) {
909 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
910 $wgOut->addWikiText( wfMsg( "alreadyrolled",
911 htmlspecialchars( $this->mTitle
->getPrefixedText()),
912 htmlspecialchars( $from ),
913 htmlspecialchars( $s->cur_user_text
) ) );
914 if($s->cur_comment
!= "") {
917 htmlspecialchars( $s->cur_comment
) ) );
922 # Get the last edit not by this guy
923 $sql = "SELECT old_text,old_user,old_user_text
924 FROM old USE INDEX (name_title_timestamp)
925 WHERE old_namespace={$n} AND old_title='{$tt}'
926 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
927 ORDER BY inverse_timestamp LIMIT 1";
928 $res = wfQuery( $sql, DB_READ
);
929 if( wfNumRows( $res ) != 1 ) {
931 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
932 $wgOut->addHTML( wfMsg( "cantrollback" ) );
935 $s = wfFetchObject( $res );
938 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
939 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
940 $wgOut->setRobotpolicy( "noindex,nofollow" );
941 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
942 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching() );
944 $wgOut->returnToMain( false );
948 # Do standard deferred updates after page view
950 /* private */ function viewUpdates()
952 global $wgDeferredUpdateList;
954 if ( 0 != $this->getID() ) {
955 $u = new ViewCountUpdate( $this->getID() );
956 array_push( $wgDeferredUpdateList, $u );
957 $u = new SiteStatsUpdate( 1, 0, 0 );
958 array_push( $wgDeferredUpdateList, $u );
960 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
961 $this->mTitle
->getDBkey() );
962 array_push( $wgDeferredUpdateList, $u );
966 # Do standard deferred updates after page edit.
967 # Every 1000th edit, prune the recent changes table.
969 /* private */ function editUpdates( $text )
971 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
974 if ( 0 == mt_rand( 0, 999 ) ) {
975 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
976 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
977 wfQuery( $sql, DB_WRITE
);
979 $id = $this->getID();
980 $title = $this->mTitle
->getPrefixedDBkey();
981 $adj = $this->mCountAdjustment
;
984 $u = new LinksUpdate( $id, $title );
985 array_push( $wgDeferredUpdateList, $u );
986 $u = new SiteStatsUpdate( 0, 1, $adj );
987 array_push( $wgDeferredUpdateList, $u );
988 $u = new SearchUpdate( $id, $title, $text );
989 array_push( $wgDeferredUpdateList, $u );
991 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(),
992 $this->mTitle
->getDBkey() );
993 array_push( $wgDeferredUpdateList, $u );
995 if ( $this->getNamespace
== NS_MEDIAWIKI
) {
996 $messageCache = $wgMemc->get( "$wgDBname:messages" );
997 if (!$messageCache) {
998 $messageCache = wfLoadAllMessages();
1000 $messageCache[$title] = $text;
1001 $wgMemc->set( "$wgDBname:messages" );
1006 /* private */ function setOldSubtitle()
1008 global $wgLang, $wgOut;
1010 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1011 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1012 $wgOut->setSubtitle( "({$r})" );
1015 # This function is called right before saving the wikitext,
1016 # so we can do things like signatures and links-in-context.
1018 function preSaveTransform( $text )
1021 while ( "" != $text ) {
1022 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1023 $s .= $this->pstPass2( $p[0] );
1025 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1027 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1028 $s .= "<nowiki>{$q[0]}</nowiki>";
1035 /* private */ function pstPass2( $text )
1037 global $wgUser, $wgLang, $wgLocaltimezone;
1041 $n = $wgUser->getName();
1042 $k = $wgUser->getOption( "nickname" );
1043 if ( "" == $k ) { $k = $n; }
1044 if(isset($wgLocaltimezone)) {
1045 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1047 /* Note: this is an ugly timezone hack for the European wikis */
1048 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1049 " (" . date( "T" ) . ")";
1050 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1052 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1053 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1054 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1055 Namespace::getUser() ) . ":$n|$k]]", $text );
1057 # Context links: [[|name]] and [[name (context)|]]
1059 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1060 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1061 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1063 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1064 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1065 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1066 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1067 # [[ns:page (cont)|]]
1069 $t = $this->mTitle
->getText();
1070 if ( preg_match( $conpat, $t, $m ) ) {
1073 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1074 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1075 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1077 if ( "" == $context ) {
1078 $text = preg_replace( $p2, "[[\\1]]", $text );
1080 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1083 # {{SUBST:xxx}} variables
1085 $mw =& MagicWord
::get( MAG_SUBST
);
1086 $text = $mw->substituteCallback( $text, "wfReplaceSubstVar" );
1091 /* Caching functions */
1093 function tryFileCache() {
1094 if($this->isFileCacheable()) {
1095 $touched = $this->mTouched
;
1096 if( strpos( $this->mContent
, "{{" ) !== false ) {
1097 # Expire pages with variable replacements in an hour
1098 $expire = wfUnix2Timestamp( time() - 3600 );
1099 $touched = max( $expire, $touched );
1101 $cache = new CacheManager( $this->mTitle
);
1102 if($cache->isFileCacheGood( $touched )) {
1103 wfDebug( " tryFileCache() - about to load\n" );
1104 $cache->loadFromFileCache();
1107 wfDebug( " tryFileCache() - starting buffer\n" );
1108 if($cache->useGzip() && wfClientAcceptsGzip()) {
1109 /* For some reason, adding this header line over in
1110 CacheManager::saveToFileCache() fails on my test
1111 setup at home, though it works on the live install.
1112 Make double-sure... --brion */
1113 header( "Content-Encoding: gzip" );
1115 ob_start( array(&$cache, 'saveToFileCache' ) );
1118 wfDebug( " tryFileCache() - not cacheable\n" );
1122 function isFileCacheable() {
1123 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1124 global $action, $oldid, $diff, $redirect, $printable;
1125 return $wgUseFileCache
1126 and (!$wgShowIPinHeader)
1127 and ($this->getID() != 0)
1128 and ($wgUser->getId() == 0)
1129 and (!$wgUser->getNewtalk())
1130 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1131 and ($action == "view")
1132 and (!isset($oldid))
1134 and (!isset($redirect))
1135 and (!isset($printable))
1136 and (!$this->mRedirectedFrom
);
1143 function wfReplaceSubstVar( $matches ) {
1144 return wfMsg( $matches[1] );