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}"; }
100 $this->mContent
= str_replace( "$1", $t, wfMsg( "missingarticle" ) );
102 if ( ! $oldid ) { # Retrieve current version
103 $id = $this->getID();
104 if ( 0 == $id ) return;
107 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
108 "FROM cur WHERE cur_id={$id}";
109 $res = wfQuery( $sql, DB_READ
, $fname );
110 if ( 0 == wfNumRows( $res ) ) { return; }
112 $s = wfFetchObject( $res );
114 # If we got a redirect, follow it (unless we've been told
115 # not to by either the function parameter or the query
116 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
117 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
118 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
119 $s->cur_text
, $m ) ) {
120 $rt = Title
::newFromText( $m[1] );
122 # Gotta hand redirects to special pages differently:
123 # Fill the HTTP response "Location" header and ignore
124 # the rest of the page we're on.
126 if ( $rt->getInterwiki() != "" ) {
127 $wgOut->redirect( $rt->getFullURL() ) ;
130 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
131 $wgOut->redirect( wfLocalUrl(
132 $rt->getPrefixedURL() ) );
135 $rid = $rt->getArticleID();
137 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
138 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
139 $res = wfQuery( $sql, DB_READ
, $fname );
141 if ( 0 != wfNumRows( $res ) ) {
142 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
144 $s = wfFetchObject( $res );
149 $this->mContent
= $s->cur_text
;
150 $this->mUser
= $s->cur_user
;
151 $this->mCounter
= $s->cur_counter
;
152 $this->mTimestamp
= $s->cur_timestamp
;
153 $this->mTouched
= $s->cur_touched
;
154 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
155 $this->mTitle
->mRestrictionsLoaded
= true;
156 wfFreeResult( $res );
157 } else { # oldid set, retrieve historical version
158 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
159 "WHERE old_id={$oldid}";
160 $res = wfQuery( $sql, DB_READ
, $fname );
161 if ( 0 == wfNumRows( $res ) ) { return; }
163 $s = wfFetchObject( $res );
164 $this->mContent
= $s->old_text
;
165 $this->mUser
= $s->old_user
;
167 $this->mTimestamp
= $s->old_timestamp
;
168 wfFreeResult( $res );
170 $this->mContentLoaded
= true;
173 function getID() { return $this->mTitle
->getArticleID(); }
177 if ( -1 == $this->mCounter
) {
178 $id = $this->getID();
179 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
181 return $this->mCounter
;
184 # Would the given text make this article a "good" article (i.e.,
185 # suitable for including in the article count)?
187 function isCountable( $text )
189 global $wgUseCommaCount, $wgMwRedir;
191 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
192 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
193 $token = ($wgUseCommaCount ?
"," : "[[" );
194 if ( false === strstr( $text, $token ) ) { return 0; }
198 # Load the field related to the last edit time of the article.
199 # This isn't necessary for all uses, so it's only done if needed.
201 /* private */ function loadLastEdit()
204 if ( -1 != $this->mUser
) return;
206 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
207 "cur_comment,cur_minor_edit FROM cur WHERE " .
208 "cur_id=" . $this->getID();
209 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
211 if ( wfNumRows( $res ) > 0 ) {
212 $s = wfFetchObject( $res );
213 $this->mUser
= $s->cur_user
;
214 $this->mUserText
= $s->cur_user_text
;
215 $this->mTimestamp
= $s->cur_timestamp
;
216 $this->mComment
= $s->cur_comment
;
217 $this->mMinorEdit
= $s->cur_minor_edit
;
221 function getTimestamp()
223 $this->loadLastEdit();
224 return $this->mTimestamp
;
229 $this->loadLastEdit();
233 function getUserText()
235 $this->loadLastEdit();
236 return $this->mUserText
;
239 function getComment()
241 $this->loadLastEdit();
242 return $this->mComment
;
245 function getMinorEdit()
247 $this->loadLastEdit();
248 return $this->mMinorEdit
;
251 # This is the default action of the script: just view the page of
256 global $wgUser, $wgOut, $wgLang;
257 global $oldid, $diff; # From query
258 global $wgLinkCache, $IP;
259 $fname = "Article::view";
260 wfProfileIn( $fname );
262 $wgOut->setArticleFlag( true );
263 $wgOut->setRobotpolicy( "index,follow" );
265 # If we got diff and oldid in the query, we want to see a
266 # diff page instead of the article.
268 if ( isset( $diff ) ) {
269 include_once( "$IP/DifferenceEngine.php" );
270 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
271 $de = new DifferenceEngine( $oldid, $diff );
273 wfProfileOut( $fname );
276 $text = $this->getContent(); # May change wgTitle!
277 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
278 $wgOut->setHTMLTitle( $this->mTitle
->getPrefixedText() .
279 " - " . wfMsg( "wikititlesuffix" ) );
281 # We're looking at an old revision
284 $this->setOldSubtitle();
285 $wgOut->setRobotpolicy( "noindex,follow" );
287 if ( "" != $this->mRedirectedFrom
) {
288 $sk = $wgUser->getSkin();
289 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
291 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
292 $wgOut->setSubtitle( $s );
294 $wgOut->checkLastModified( $this->mTouched
);
295 $this->tryFileCache();
296 $wgLinkCache->preFill( $this->mTitle
);
297 $wgOut->addWikiText( $text );
299 $this->viewUpdates();
300 wfProfileOut( $fname );
303 # Theoretically we could defer these whole insert and update
304 # functions for after display, but that's taking a big leap
305 # of faith, and we want to be able to report database
306 # errors at some point.
308 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
310 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
311 $fname = "Article::insertNewArticle";
313 $ns = $this->mTitle
->getNamespace();
314 $ttl = $this->mTitle
->getDBkey();
315 $text = $this->preSaveTransform( $text );
316 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
319 $now = wfTimestampNow();
320 $won = wfInvertTimestamp( $now );
322 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
323 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
324 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
325 "cur_restrictions,cur_user_text,cur_is_redirect," .
326 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
327 wfStrencode( $text ) . "', '" .
328 wfStrencode( $summary ) . "', '" .
329 $wgUser->getID() . "', '{$now}', " .
330 ( $isminor ?
1 : 0 ) . ", 0, '', '" .
331 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
332 $res = wfQuery( $sql, DB_WRITE
, $fname );
334 $newid = wfInsertId();
335 $this->mTitle
->resetArticleID( $newid );
337 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
338 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
339 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
340 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
341 ( $isminor ?
1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
342 wfStrencode( $wgUser->getName() ) . "','" .
343 wfStrencode( $summary ) . "',0,0," .
344 ( $wgUser->isBot() ?
1 : 0 ) . ")";
345 wfQuery( $sql, DB_WRITE
, $fname );
347 if(!$this->mTitle
->userIsWatching()) $this->watch();
349 if ( $this->mTitle
->userIsWatching() ) {
354 $this->showArticle( $text, wfMsg( "newarticle" ) );
357 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
359 global $wgOut, $wgUser, $wgLinkCache;
360 global $wgDBtransactions, $wgMwRedir;
361 $fname = "Article::updateArticle";
363 $this->loadLastEdit();
365 // insert updated section into old text if we have only edited part
367 if ($section != "") {
368 $oldtext=$this->getContent();
369 if($section=="new") {
370 if($summary) $subject="== {$summary} ==\n\n";
371 $text=$oldtext."\n\n".$subject.$text;
373 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
374 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
375 $secs[$section*2]=$text."\n\n"; // replace with edited
376 if($section) { $secs[$section*2-1]=""; } // erase old headline
377 $text=join("",$secs);
380 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
381 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
382 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
384 $text = $m[1] . "\n"; # Remove all content but redirect
388 $text = $this->preSaveTransform( $text );
390 # Update article, but only if changed.
392 if( $wgDBtransactions ) {
394 wfQuery( $sql, DB_WRITE
);
396 $oldtext = $this->getContent( true );
398 if ( 0 != strcmp( $text, $oldtext ) ) {
399 $this->mCountAdjustment
= $this->isCountable( $text )
400 - $this->isCountable( $oldtext );
402 $now = wfTimestampNow();
403 $won = wfInvertTimestamp( $now );
404 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
405 "',cur_comment='" . wfStrencode( $summary ) .
406 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
407 ",cur_timestamp='{$now}',cur_user_text='" .
408 wfStrencode( $wgUser->getName() ) .
409 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
410 "WHERE cur_id=" . $this->getID() .
411 " AND cur_timestamp='" . $this->getTimestamp() . "'";
412 $res = wfQuery( $sql, DB_WRITE
, $fname );
414 if( wfAffectedRows() == 0 ) {
415 /* Belated edit conflict! Run away!! */
419 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
420 "old_comment,old_user,old_user_text,old_timestamp," .
421 "old_minor_edit,inverse_timestamp) VALUES (" .
422 $this->mTitle
->getNamespace() . ", '" .
423 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
424 wfStrencode( $oldtext ) . "', '" .
425 wfStrencode( $this->getComment() ) . "', " .
426 $this->getUser() . ", '" .
427 wfStrencode( $this->getUserText() ) . "', '" .
428 $this->getTimestamp() . "', " . $me1 . ", '" .
429 wfInvertTimestamp( $this->getTimestamp() ) . "')";
430 $res = wfQuery( $sql, DB_WRITE
, $fname );
431 $oldid = wfInsertID( $res );
433 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
434 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
435 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
436 "'{$now}','{$now}'," . $this->mTitle
->getNamespace() . ",'" .
437 wfStrencode( $this->mTitle
->getDBkey() ) . "',0,{$me2}," .
438 ( $wgUser->isBot() ?
1 : 0 ) . "," .
439 $this->getID() . "," . $wgUser->getID() . ",'" .
440 wfStrencode( $wgUser->getName() ) . "','" .
441 wfStrencode( $summary ) . "',0,{$oldid})";
442 wfQuery( $sql, DB_WRITE
, $fname );
444 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
445 "WHERE rc_namespace=" . $this->mTitle
->getNamespace() . " AND " .
446 "rc_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' AND " .
447 "rc_timestamp='" . $this->getTimestamp() . "'";
448 wfQuery( $sql, DB_WRITE
, $fname );
450 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
451 "WHERE rc_cur_id=" . $this->getID();
452 wfQuery( $sql, DB_WRITE
, $fname );
454 if( $wgDBtransactions ) {
456 wfQuery( $sql, DB_WRITE
);
460 if (!$this->mTitle
->userIsWatching()) $this->watch();
462 if ( $this->mTitle
->userIsWatching() ) {
467 $this->showArticle( $text, wfMsg( "updated" ) );
471 # After we've either updated or inserted the article, update
472 # the link tables and redirect to the new page.
474 function showArticle( $text, $subtitle )
476 global $wgOut, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
479 $wgLinkCache = new LinkCache();
481 # Get old version of link table to allow incremental link updates
482 if ( $wgUseBetterLinksUpdate ) {
483 $wgLinkCache->preFill( $this->mTitle
);
484 $wgLinkCache->clear();
487 # Now update the link cache by parsing the text
488 $wgOut = new OutputPage();
489 $wgOut->addWikiText( $text );
491 $this->editUpdates( $text );
492 if( $wgMwRedir->matchStart( $text ) )
496 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL(), $r ) );
499 # Add this page to my watchlist
501 function watch( $add = true )
503 global $wgUser, $wgOut, $wgLang;
504 global $wgDeferredUpdateList;
506 if ( 0 == $wgUser->getID() ) {
507 $wgOut->errorpage( "watchnologin", "watchnologintext" );
510 if ( wfReadOnly() ) {
511 $wgOut->readOnlyPage();
515 $wgUser->addWatch( $this->mTitle
);
517 $wgUser->removeWatch( $this->mTitle
);
519 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
520 $wgOut->setRobotpolicy( "noindex,follow" );
522 $sk = $wgUser->getSkin() ;
523 $link = $sk->makeKnownLink ( $this->mTitle
->getPrefixedText() ) ;
526 $text = wfMsg( "addedwatchtext", $link );
528 $text = wfMsg( "removedwatchtext", $link );
529 $wgOut->addHTML( $text );
531 $up = new UserUpdate();
532 array_push( $wgDeferredUpdateList, $up );
534 $wgOut->returnToMain( false );
539 $this->watch( false );
542 # This shares a lot of issues (and code) with Recent Changes
546 global $wgUser, $wgOut, $wgLang, $offset, $limit;
548 # If page hasn't changed, client can cache this
550 $wgOut->checkLastModified( $this->getTimestamp() );
551 $fname = "Article::history";
552 wfProfileIn( $fname );
554 $wgOut->setPageTitle( $this->mTitle
->getPRefixedText() );
555 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
556 $wgOut->setArticleFlag( false );
557 $wgOut->setRobotpolicy( "noindex,nofollow" );
559 if( $this->mTitle
->getArticleID() == 0 ) {
560 $wgOut->addHTML( wfMsg( "nohistory" ) );
561 wfProfileOut( $fname );
565 $offset = (int)$offset;
566 $limit = (int)$limit;
567 if( $limit == 0 ) $limit = 50;
568 $namespace = $this->mTitle
->getNamespace();
569 $title = $this->mTitle
->getText();
570 $sql = "SELECT old_id,old_user," .
571 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
572 "FROM old USE INDEX (name_title_timestamp) " .
573 "WHERE old_namespace={$namespace} AND " .
574 "old_title='" . wfStrencode( $this->mTitle
->getDBkey() ) . "' " .
575 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
576 $res = wfQuery( $sql, DB_READ
, "Article::history" );
578 $revs = wfNumRows( $res );
579 if( $this->mTitle
->getArticleID() == 0 ) {
580 $wgOut->addHTML( wfMsg( "nohistory" ) );
581 wfProfileOut( $fname );
585 $sk = $wgUser->getSkin();
586 $numbar = wfViewPrevNext(
588 $this->mTitle
->getPrefixedText(),
591 $s .= $sk->beginHistoryList();
594 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
595 $this->getUserText(), $namespace,
596 $title, 0, $this->getComment(),
597 ( $this->getMinorEdit() > 0 ) );
599 $revs = wfNumRows( $res );
600 while ( $line = wfFetchObject( $res ) ) {
601 $s .= $sk->historyLine( $line->old_timestamp
, $line->old_user
,
602 $line->old_user_text
, $namespace,
603 $title, $line->old_id
,
604 $line->old_comment
, ( $line->old_minor_edit
> 0 ) );
606 $s .= $sk->endHistoryList();
608 $wgOut->addHTML( $s );
609 wfProfileOut( $fname );
612 function protect( $limit = "sysop" )
614 global $wgUser, $wgOut;
616 if ( ! $wgUser->isSysop() ) {
617 $wgOut->sysopRequired();
620 if ( wfReadOnly() ) {
621 $wgOut->readOnlyPage();
624 $id = $this->mTitle
->getArticleID();
626 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
629 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
630 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
631 wfQuery( $sql, DB_WRITE
, "Article::protect" );
633 $wgOut->redirect( wfLocalUrl( $this->mTitle
->getPrefixedURL() ) );
638 return $this->protect( "" );
643 global $wgUser, $wgOut;
644 global $wpConfirm, $wpReason, $image, $oldimage;
646 # Anybody can delete old revisions of images; only sysops
647 # can delete articles and current images
649 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
650 $wgOut->sysopRequired();
653 if ( wfReadOnly() ) {
654 $wgOut->readOnlyPage();
658 # Better double-check that it hasn't been deleted yet!
659 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
660 if ( ( "" == trim( $this->mTitle
->getText() ) )
661 or ( $this->mTitle
->getArticleId() == 0 ) ) {
662 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
666 # determine whether this page has earlier revisions
667 # and insert a warning if it does
668 # we select the text because it might be useful below
669 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
670 $res=wfQuery($sql, DB_READ
, $fname);
671 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
672 $skin=$wgUser->getSkin();
673 $wgOut->addHTML("<B>".wfMsg("historywarning"));
674 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
677 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle
->getPrefixedDBkey())."'";
678 $res=wfQuery($sql, DB_READ
, $fname);
679 if( ($s=wfFetchObject($res))) {
681 # if this is a mini-text, we can paste part of it into the deletion reason
683 #if this is empty, an earlier revision may contain "useful" text
684 if($s->cur_text
!="") {
688 $text=$old->old_text
;
694 $length=strlen($text);
696 # this should not happen, since it is not possible to store an empty, new
697 # page. Let's insert a standard text in case it does, though
698 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
701 if($length < 500 && !$wpReason) {
703 # comment field=255, let's grep the first 150 to have some user
705 $text=substr($text,0,150);
706 # let's strip out newlines and HTML tags
707 $text=preg_replace("/\"/","'",$text);
708 $text=preg_replace("/\</","<",$text);
709 $text=preg_replace("/\>/",">",$text);
710 $text=preg_replace("/[\n\r]/","",$text);
712 $wpReason=wfMsg("excontent"). " '".$text;
714 $wpReason=wfMsg("exbeforeblank") . " '".$text;
716 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
721 return $this->confirmDelete();
724 function confirmDelete( $par = "" )
728 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
729 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
730 $wgOut->setRobotpolicy( "noindex,nofollow" );
731 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
733 $t = $this->mTitle
->getPrefixedURL();
735 $formaction = wfEscapeHTML( wfLocalUrl( $t, "action=delete" . $par ) );
736 $confirm = wfMsg( "confirm" );
737 $check = wfMsg( "confirmcheck" );
738 $delcom = wfMsg( "deletecomment" );
741 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
742 <table border=0><tr><td align=right>
743 {$delcom}:</td><td align=left>
744 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
745 </td></tr><tr><td> </td></tr>
747 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
748 </td><td><label for=\"wpConfirm\">{$check}</label></td>
749 </tr><tr><td> </td><td>
750 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
751 </td></tr></table></form>\n" );
753 $wgOut->returnToMain( false );
758 global $wgOut, $wgUser, $wgLang;
760 $fname = "Article::doDelete";
762 $this->doDeleteArticle( $this->mTitle
);
763 $deleted = $this->mTitle
->getPrefixedText();
765 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
766 $wgOut->setRobotpolicy( "noindex,nofollow" );
768 $sk = $wgUser->getSkin();
769 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
770 Namespace::getWikipedia() ) .
771 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
773 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
774 $text = str_replace( "$2", $loglink, $text );
776 $wgOut->addHTML( "<p>" . $text );
777 $wgOut->returnToMain( false );
780 function doDeleteArticle( $title )
782 global $wgUser, $wgOut, $wgLang, $wpReason, $wgDeferredUpdateList;
784 $fname = "Article::doDeleteArticle";
785 $ns = $title->getNamespace();
786 $t = wfStrencode( $title->getDBkey() );
787 $id = $title->getArticleID();
790 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
794 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
795 array_push( $wgDeferredUpdateList, $u );
797 # Move article and history to the "archive" table
798 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
799 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
800 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
801 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
802 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
803 wfQuery( $sql, DB_WRITE
, $fname );
805 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
806 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
807 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
808 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
809 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
810 wfQuery( $sql, DB_WRITE
, $fname );
812 # Now that it's safely backed up, delete it
814 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
816 wfQuery( $sql, DB_WRITE
, $fname );
818 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
820 wfQuery( $sql, DB_WRITE
, $fname );
822 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
824 wfQuery( $sql, DB_WRITE
, $fname );
826 # Finally, clean up the link tables
829 $t = wfStrencode( $title->getPrefixedDBkey() );
830 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
831 $res = wfQuery( $sql, DB_READ
, $fname );
833 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
834 $now = wfTimestampNow();
835 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
838 while ( $s = wfFetchObject( $res ) ) {
839 $nt = Title
::newFromDBkey( $s->l_from
);
840 $lid = $nt->getArticleID();
842 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
844 $sql .= "({$lid},'{$t}')";
849 wfQuery( $sql, DB_WRITE
, $fname );
850 wfQuery( $sql2, DB_WRITE
, $fname );
852 wfFreeResult( $res );
854 $sql = "DELETE FROM links WHERE l_to={$id}";
855 wfQuery( $sql, DB_WRITE
, $fname );
857 $sql = "DELETE FROM links WHERE l_from='{$t}'";
858 wfQuery( $sql, DB_WRITE
, $fname );
860 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
861 wfQuery( $sql, DB_WRITE
, $fname );
863 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
864 wfQuery( $sql, DB_WRITE
, $fname );
867 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
868 $art = $title->getPrefixedText();
869 $wpReason = wfCleanQueryVar( $wpReason );
870 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
872 # Clear the cached article id so the interface doesn't act like we exist
873 $this->mTitle
->resetArticleID( 0 );
874 $this->mTitle
->mArticleID
= 0;
879 global $wgUser, $wgLang, $wgOut, $from;
881 if ( ! $wgUser->isSysop() ) {
882 $wgOut->sysopRequired();
886 # Replace all this user's current edits with the next one down
887 $tt = wfStrencode( $this->mTitle
->getDBKey() );
888 $n = $this->mTitle
->getNamespace();
890 # Get the last editor
891 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
892 $res = wfQuery( $sql, DB_READ
);
893 if( ($x = wfNumRows( $res )) != 1 ) {
895 $wgOut->addHTML( wfMsg( "notanarticle" ) );
898 $s = wfFetchObject( $res );
899 $ut = wfStrencode( $s->cur_user_text
);
903 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
904 if( $from != $s->cur_user_text
) {
905 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
906 $wgOut->addWikiText( wfMsg( "alreadyrolled",
907 htmlspecialchars( $this->mTitle
->getPrefixedText()),
908 htmlspecialchars( $from ),
909 htmlspecialchars( $s->cur_user_text
) ) );
910 if($s->cur_comment
!= "") {
913 htmlspecialchars( $s->cur_comment
) ) );
918 # Get the last edit not by this guy
919 $sql = "SELECT old_text,old_user,old_user_text
920 FROM old USE INDEX (name_title_timestamp)
921 WHERE old_namespace={$n} AND old_title='{$tt}'
922 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
923 ORDER BY inverse_timestamp LIMIT 1";
924 $res = wfQuery( $sql, DB_READ
);
925 if( wfNumRows( $res ) != 1 ) {
927 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
928 $wgOut->addHTML( wfMsg( "cantrollback" ) );
931 $s = wfFetchObject( $res );
934 $newcomment = str_replace( "$1", $s->old_user_text
, wfMsg( "revertpage" ) );
935 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
936 $wgOut->setRobotpolicy( "noindex,nofollow" );
937 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
938 $this->updateArticle( $s->old_text
, $newcomment, 1, $this->mTitle
->userIsWatching() );
940 $wgOut->returnToMain( false );
944 # Do standard deferred updates after page view
946 /* private */ function viewUpdates()
948 global $wgDeferredUpdateList;
950 if ( 0 != $this->getID() ) {
951 $u = new ViewCountUpdate( $this->getID() );
952 array_push( $wgDeferredUpdateList, $u );
953 $u = new SiteStatsUpdate( 1, 0, 0 );
954 array_push( $wgDeferredUpdateList, $u );
956 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
957 $this->mTitle
->getDBkey() );
958 array_push( $wgDeferredUpdateList, $u );
962 # Do standard deferred updates after page edit.
963 # Every 1000th edit, prune the recent changes table.
965 /* private */ function editUpdates( $text )
967 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
970 if ( 0 == mt_rand( 0, 999 ) ) {
971 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
972 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
973 wfQuery( $sql, DB_WRITE
);
975 $id = $this->getID();
976 $title = $this->mTitle
->getPrefixedDBkey();
977 $adj = $this->mCountAdjustment
;
980 $u = new LinksUpdate( $id, $title );
981 array_push( $wgDeferredUpdateList, $u );
982 $u = new SiteStatsUpdate( 0, 1, $adj );
983 array_push( $wgDeferredUpdateList, $u );
984 $u = new SearchUpdate( $id, $title, $text );
985 array_push( $wgDeferredUpdateList, $u );
987 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(),
988 $this->mTitle
->getDBkey() );
989 array_push( $wgDeferredUpdateList, $u );
991 if ( $this->getNamespace
== NS_MEDIAWIKI
) {
992 $wgMemc->delete( "$wgDBname:messages" );
997 /* private */ function setOldSubtitle()
999 global $wgLang, $wgOut;
1001 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1002 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1003 $wgOut->setSubtitle( "({$r})" );
1006 # This function is called right before saving the wikitext,
1007 # so we can do things like signatures and links-in-context.
1009 function preSaveTransform( $text )
1012 while ( "" != $text ) {
1013 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1014 $s .= $this->pstPass2( $p[0] );
1016 if ( ( count( $p ) < 2 ) ||
( "" == $p[1] ) ) { $text = ""; }
1018 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1019 $s .= "<nowiki>{$q[0]}</nowiki>";
1026 /* private */ function pstPass2( $text )
1028 global $wgUser, $wgLang, $wgLocaltimezone;
1032 $n = $wgUser->getName();
1033 $k = $wgUser->getOption( "nickname" );
1034 if ( "" == $k ) { $k = $n; }
1035 if(isset($wgLocaltimezone)) {
1036 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1038 /* Note: this is an ugly timezone hack for the European wikis */
1039 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1040 " (" . date( "T" ) . ")";
1041 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1043 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1044 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1045 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1046 Namespace::getUser() ) . ":$n|$k]]", $text );
1048 # Context links: [[|name]] and [[name (context)|]]
1050 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1051 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1052 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1054 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1055 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1056 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1057 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1058 # [[ns:page (cont)|]]
1060 $t = $this->mTitle
->getText();
1061 if ( preg_match( $conpat, $t, $m ) ) {
1064 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1065 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1066 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1068 if ( "" == $context ) {
1069 $text = preg_replace( $p2, "[[\\1]]", $text );
1071 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1074 # {{SUBST:xxx}} variables
1076 $mw =& MagicWord
::get( MAG_SUBST
);
1077 $text = $mw->substituteCallback( $text, "replaceMsgVar" );
1082 /* Caching functions */
1084 function tryFileCache() {
1085 if($this->isFileCacheable()) {
1086 $touched = $this->mTouched
;
1087 if( strpos( $this->mContent
, "{{" ) !== false ) {
1088 # Expire pages with variable replacements in an hour
1089 $expire = wfUnix2Timestamp( time() - 3600 );
1090 $touched = max( $expire, $touched );
1092 $cache = new CacheManager( $this->mTitle
);
1093 if($cache->isFileCacheGood( $touched )) {
1094 wfDebug( " tryFileCache() - about to load\n" );
1095 $cache->loadFromFileCache();
1098 wfDebug( " tryFileCache() - starting buffer\n" );
1099 if($cache->useGzip() && wfClientAcceptsGzip()) {
1100 /* For some reason, adding this header line over in
1101 CacheManager::saveToFileCache() fails on my test
1102 setup at home, though it works on the live install.
1103 Make double-sure... --brion */
1104 header( "Content-Encoding: gzip" );
1106 ob_start( array(&$cache, 'saveToFileCache' ) );
1109 wfDebug( " tryFileCache() - not cacheable\n" );
1113 function isFileCacheable() {
1114 global $wgUser, $wgUseFileCache, $wgShowIPinHeader;
1115 global $action, $oldid, $diff, $redirect, $printable;
1116 return $wgUseFileCache
1117 and (!$wgShowIPinHeader)
1118 and ($this->getID() != 0)
1119 and ($wgUser->getId() == 0)
1120 and (!$wgUser->getNewtalk())
1121 and ($this->mTitle
->getNamespace
!= Namespace::getSpecial())
1122 and ($action == "view")
1123 and (!isset($oldid))
1125 and (!isset($redirect))
1126 and (!isset($printable))
1127 and (!$this->mRedirectedFrom
);