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 require_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, $mTitle;
16 /* private */ var $mId, $mTable;
18 function Article( &$title ) {
19 $this->mTitle
=& $title;
23 /* private */ function clear()
25 $this->mContentLoaded
= false;
26 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
27 $this->mRedirectedFrom
= $this->mUserText
=
28 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= "";
29 $this->mCountAdjustment
= 0;
30 $this->mTouched
= "19700101000000";
33 /* static */ function getRevisionText( $row, $prefix = "old_" ) {
34 # Deal with optional compression of archived pages.
35 # This can be done periodically via maintenance/compressOld.php, and
36 # as pages are saved if $wgCompressRevisions is set.
37 $text = $prefix . "text";
38 $flags = $prefix . "flags";
39 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, "gzip" ) ) ) {
40 return gzinflate( $row->$text );
42 if( isset( $row->$text ) ) {
48 /* static */ function compressRevisionText( &$text ) {
49 global $wgCompressRevisions;
50 if( !$wgCompressRevisions ) {
53 if( !function_exists( "gzdeflate" ) ) {
54 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
57 $text = gzdeflate( $text );
61 # Note that getContent/loadContent may follow redirects if
62 # not told otherwise, and so may cause a change to mTitle.
64 # Return the text of this revision
65 function getContent( $noredir )
67 global $wgRequest,$wgUser,$wgOut;
69 $fname = "Article::getContent";
70 wfProfileIn( $fname );
72 # Get variables from query string :P
73 $action = $wgRequest->getText( 'action', 'view' );
74 $section = $wgRequest->getText( 'section' );
75 $sectiontitle = $wgRequest->getText( 'sectiontitle' );
76 $oid = $wgRequest->getInt( 'oldid' );
78 if ( 0 == $this->getID() ) {
79 if ( "edit" == $action ) {
80 wfProfileOut( $fname );
81 return ""; # was "newarticletext", now moved above the box)
83 wfProfileOut( $fname );
84 return wfMsg( "noarticletext" );
86 $this->loadContent( $noredir );
88 # size threshold at which pages are automatically collapsed
89 $collapsethreshold=$wgUser->getOption( "collapsethreshold" );
90 if($collapsethreshold && strlen($this->mContent
)>=$collapsethreshold) {
95 #override user pref is expansion is explicitly requested
96 if($wgRequest->getBool('collapse')) { $collapse=true; }
97 if( $wgRequest->getText( 'collapse' ) == 'false') {
100 if($oid) { $collapse=false; }
101 $wgOut->setCollapse($collapse);
103 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
104 # if so, a "This is an anonymous talk page .." message must be appended to the
105 # displayed page (but not during editing!).
107 if( ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
108 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle
->getText()) &&
110 ) { $anontalk=true; }
112 # When a page is viewed in collapsed mode, only the intro section and, for
113 # pages with multiple sections, a table of contents are shown.
114 if($collapse && $action=="view" && $section=="") {
115 $rv=$this->getSection($this->mContent
,0,"");
116 $wgOut->setToc(Parser
::getTocFromSource($this->mContent
));
117 if($anontalk) { $rv = $rv . "\n" . wfMsg("anontalkpage"); }
120 # $section contains a section number and is used for section viewing and editing.
122 if($section=="new" && $action=="edit") {
123 wfProfileOut( $fname );
126 $rv=$this->getSection($this->mContent
,$section,$sectiontitle);
127 if($anontalk) { $rv = $rv . "\n" . wfMsg("anontalkpage"); }
128 wfProfileOut( $fname );
133 if($anontalk) { return $this->mContent
. "\n" . wfMsg("anontalkpage"); }
134 wfProfileOut( $fname );
135 return $this->mContent
;
139 # This function returns the text of a section, specified by a number ($section)
140 # and a section title ($sectiontitle). A section is text under a heading like
141 # == Heading == or <h1>Heading</h1>, or the first section before any such
142 # heading (section 0).
144 # If a section contains subsections, these are also returned.
146 function getSection($text,$section,$sectiontitle) {
148 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
149 # comments to be stripped as well)
151 $parser=new Parser();
152 $parser->mOutputType
=OT_WIKI
;
153 $striptext=$parser->strip($text, $striparray, true);
155 # now that we can be sure that no pseudo-sections are in the source,
156 # split it up by section
159 "/(^=+.*?=+|^<h[1-6].*?" . ">.*?<\/h[1-6].*?" . ">)/mi",
161 PREG_SPLIT_DELIM_CAPTURE
);
165 $headline=$secs[$section*2-1];
166 preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$headline,$matches);
169 # translate wiki heading into level
170 if(strpos($hlevel,"=")!==false) {
171 $hlevel=strlen($hlevel);
174 $rv=$headline. $secs[$section*2];
178 while(!empty($secs[$count*2-1]) && !$break) {
180 $subheadline=$secs[$count*2-1];
181 preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$subheadline,$matches);
182 $subhlevel=$matches[1];
183 if(strpos($subhlevel,"=")!==false) {
184 $subhlevel=strlen($subhlevel);
186 if($subhlevel > $hlevel) {
187 $rv.=$subheadline.$secs[$count*2];
189 if($subhlevel <= $hlevel) {
196 # reinsert stripped tags
197 $rv=$parser->unstrip($rv,$striparray);
204 # Load the revision (including cur_text) into this object
205 function loadContent( $noredir = false )
207 global $wgOut, $wgMwRedir, $wgRequest;
210 $oldid = $wgRequest->getVal( 'oldid' );
211 $redirect = $wgRequest->getVal( 'redirect' );
213 if ( $this->mContentLoaded
) return;
214 $fname = "Article::loadContent";
216 # Pre-fill content with error message so that if something
217 # fails we'll have something telling us what we intended.
219 $t = $this->mTitle
->getPrefixedText();
220 if ( isset( $oldid ) ) {
221 $oldid = IntVal( $oldid );
222 $t .= ",oldid={$oldid}";
224 if ( isset( $redirect ) ) {
225 $redirect = ($redirect == "no") ?
"no" : "yes";
226 $t .= ",redirect={$redirect}";
228 $this->mContent
= wfMsg( "missingarticle", $t );
230 if ( ! $oldid ) { # Retrieve current version
231 $id = $this->getID();
232 if ( 0 == $id ) return;
235 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
236 "FROM cur WHERE cur_id={$id}";
238 $res = wfQuery( $sql, DB_READ
, $fname );
239 if ( 0 == wfNumRows( $res ) ) {
243 $s = wfFetchObject( $res );
244 # If we got a redirect, follow it (unless we've been told
245 # not to by either the function parameter or the query
246 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
247 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
248 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
249 $s->cur_text
, $m ) ) {
250 $rt = Title
::newFromText( $m[1] );
252 # Gotta hand redirects to special pages differently:
253 # Fill the HTTP response "Location" header and ignore
254 # the rest of the page we're on.
256 if ( $rt->getInterwiki() != "" ) {
257 $wgOut->redirect( $rt->getFullURL() ) ;
260 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
261 $wgOut->redirect( $rt->getFullURL() );
264 $rid = $rt->getArticleID();
266 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
267 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
268 $res = wfQuery( $sql, DB_READ
, $fname );
270 if ( 0 != wfNumRows( $res ) ) {
271 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
273 $s = wfFetchObject( $res );
280 $this->mContent
= $s->cur_text
;
281 $this->mUser
= $s->cur_user
;
282 $this->mCounter
= $s->cur_counter
;
283 $this->mTimestamp
= $s->cur_timestamp
;
284 $this->mTouched
= $s->cur_touched
;
285 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
286 $this->mTitle
->mRestrictionsLoaded
= true;
287 wfFreeResult( $res );
288 } else { # oldid set, retrieve historical version
289 $sql = "SELECT old_namespace,old_title,old_text,old_timestamp,old_user,old_flags FROM old " .
290 "WHERE old_id={$oldid}";
291 $res = wfQuery( $sql, DB_READ
, $fname );
292 if ( 0 == wfNumRows( $res ) ) {
296 $s = wfFetchObject( $res );
297 if( $this->mTitle
->getNamespace() != $s->old_namespace ||
298 $this->mTitle
->getDBkey() != $s->old_title
) {
299 $oldTitle = Title
::makeTitle( $s->old_namesapce
, $s->old_title
);
300 $this->mTitle
= $oldTitle;
301 $wgTitle = $oldTitle;
303 $this->mContent
= Article
::getRevisionText( $s );
304 $this->mUser
= $s->old_user
;
306 $this->mTimestamp
= $s->old_timestamp
;
307 wfFreeResult( $res );
309 $this->mContentLoaded
= true;
310 return $this->mContent
;
313 # Gets the article text without using so many damn globals
314 # Returns false on error
315 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
318 if ( $this->mContentLoaded
) {
319 return $this->mContent
;
321 $this->mContent
= false;
323 $fname = "Article::loadContent";
325 if ( ! $oldid ) { # Retrieve current version
326 $id = $this->getID();
332 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
333 "FROM cur WHERE cur_id={$id}";
334 $res = wfQuery( $sql, DB_READ
, $fname );
335 if ( 0 == wfNumRows( $res ) ) {
339 $s = wfFetchObject( $res );
340 # If we got a redirect, follow it (unless we've been told
341 # not to by either the function parameter or the query
342 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text
) ) {
343 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
344 $s->cur_text
, $m ) ) {
345 $rt = Title
::newFromText( $m[1] );
346 if( $rt && $rt->getInterwiki() == "" && $rt->getNamespace() != Namespace::getSpecial() ) {
347 $rid = $rt->getArticleID();
349 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
350 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
351 $res = wfQuery( $sql, DB_READ
, $fname );
353 if ( 0 != wfNumRows( $res ) ) {
354 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
356 $s = wfFetchObject( $res );
363 $this->mContent
= $s->cur_text
;
364 $this->mUser
= $s->cur_user
;
365 $this->mCounter
= $s->cur_counter
;
366 $this->mTimestamp
= $s->cur_timestamp
;
367 $this->mTouched
= $s->cur_touched
;
368 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
369 $this->mTitle
->mRestrictionsLoaded
= true;
370 wfFreeResult( $res );
371 } else { # oldid set, retrieve historical version
372 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
373 "WHERE old_id={$oldid}";
374 $res = wfQuery( $sql, DB_READ
, $fname );
375 if ( 0 == wfNumRows( $res ) ) {
379 $s = wfFetchObject( $res );
380 $this->mContent
= Article
::getRevisionText( $s );
381 $this->mUser
= $s->old_user
;
383 $this->mTimestamp
= $s->old_timestamp
;
384 wfFreeResult( $res );
386 $this->mContentLoaded
= true;
387 return $this->mContent
;
391 if( $this->mTitle
) {
392 return $this->mTitle
->getArticleID();
400 if ( -1 == $this->mCounter
) {
401 $id = $this->getID();
402 $this->mCounter
= wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
404 return $this->mCounter
;
407 # Would the given text make this article a "good" article (i.e.,
408 # suitable for including in the article count)?
410 function isCountable( $text )
412 global $wgUseCommaCount, $wgMwRedir;
414 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
415 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
416 $token = ($wgUseCommaCount ?
"," : "[[" );
417 if ( false === strstr( $text, $token ) ) { return 0; }
421 # Loads everything from cur except cur_text
422 # This isn't necessary for all uses, so it's only done if needed.
424 /* private */ function loadLastEdit()
427 if ( -1 != $this->mUser
) return;
429 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
430 "cur_comment,cur_minor_edit FROM cur WHERE " .
431 "cur_id=" . $this->getID();
432 $res = wfQuery( $sql, DB_READ
, "Article::loadLastEdit" );
434 if ( wfNumRows( $res ) > 0 ) {
435 $s = wfFetchObject( $res );
436 $this->mUser
= $s->cur_user
;
437 $this->mUserText
= $s->cur_user_text
;
438 $this->mTimestamp
= $s->cur_timestamp
;
439 $this->mComment
= $s->cur_comment
;
440 $this->mMinorEdit
= $s->cur_minor_edit
;
444 function getTimestamp()
446 $this->loadLastEdit();
447 return $this->mTimestamp
;
452 $this->loadLastEdit();
456 function getUserText()
458 $this->loadLastEdit();
459 return $this->mUserText
;
462 function getComment()
464 $this->loadLastEdit();
465 return $this->mComment
;
468 function getMinorEdit()
470 $this->loadLastEdit();
471 return $this->mMinorEdit
;
474 function getContributors($limit = 0, $offset = 0)
476 $fname = "Article::getContributors";
478 # XXX: this is expensive; cache this info somewhere.
480 $title = $this->mTitle
;
484 $sql = "SELECT old.old_user, old.old_user_text, " .
485 " user.user_real_name, MAX(old.old_timestamp) as timestamp" .
487 " WHERE old.old_user = user.user_id " .
488 " AND old.old_namespace = " . $title->getNamespace() .
489 " AND old.old_title = \"" . $title->getDBkey() . "\"" .
490 " AND old.old_user != 0 " .
491 " AND old.old_user != " . $this->getUser() .
492 " GROUP BY old.old_user " .
493 " ORDER BY timestamp DESC ";
496 $sql .= " LIMIT $limit";
499 $res = wfQuery($sql, DB_READ
, $fname);
501 while ( $line = wfFetchObject( $res ) ) {
502 $contribs[$line->old_user
] =
503 array($line->old_user_text
, $line->user_real_name
);
506 # Count anonymous users
508 $res = wfQuery("SELECT COUNT(*) AS cnt " .
510 " WHERE old_namespace = " . $title->getNamespace() .
511 " AND old_title = '" . $title->getDBkey() . "'" .
512 " AND old_user = 0 ", DB_READ
, $fname);
514 while ( $line = wfFetchObject( $res ) ) {
515 $contribs[0] = array($line->cnt
, 'Anonymous');
521 # This is the default action of the script: just view the page of
526 global $wgUser, $wgOut, $wgLang, $wgRequest;
527 global $wgLinkCache, $IP, $wgEnableParserCache;
529 $fname = "Article::view";
530 wfProfileIn( $fname );
532 # Get variables from query string :P
533 $oldid = $wgRequest->getVal( 'oldid' );
534 $diff = $wgRequest->getVal( 'diff' );
536 $wgOut->setArticleFlag( true );
537 $wgOut->setRobotpolicy( "index,follow" );
539 # If we got diff and oldid in the query, we want to see a
540 # diff page instead of the article.
542 if ( !is_null( $diff ) ) {
543 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
544 $de = new DifferenceEngine( intval($oldid), intval($diff) );
546 wfProfileOut( $fname );
550 if ( !is_null( $oldid ) and $this->checkTouched() ) {
551 if( $wgOut->checkLastModified( $this->mTouched
) ){
553 } else if ( $this->tryFileCache() ) {
554 # tell wgOut that output is taken care of
556 $this->viewUpdates();
561 $text = $this->getContent( false ); # May change mTitle by following a redirect
563 # Another whitelist check in case oldid or redirects are altering the title
564 if ( !$this->mTitle
->userCanRead() ) {
565 $wgOut->loginToUse();
570 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
572 # We're looking at an old revision
574 if ( !empty( $oldid ) ) {
575 $this->setOldSubtitle();
576 $wgOut->setRobotpolicy( "noindex,follow" );
578 if ( "" != $this->mRedirectedFrom
) {
579 $sk = $wgUser->getSkin();
580 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, "",
582 $s = wfMsg( "redirectedfrom", $redir );
583 $wgOut->setSubtitle( $s );
586 $wgLinkCache->preFill( $this->mTitle
);
588 # wrap user css and user js in pre and don't parse
589 # XXX: use $this->mTitle->usCssJsSubpage() when php is fixed/ a workaround is found
591 $this->mTitle
->getNamespace() == Namespace::getUser() &&
592 preg_match("/\\/[\\w]+\\.(css|js)$/", $this->mTitle
->getDBkey())
594 $wgOut->addWikiText( wfMsg('usercssjs'));
595 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent
)."\n</pre>" );
596 } else if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
597 $wgOut->addWikiText( $text, true, $this );
599 $wgOut->addWikiText( $text );
602 # Add link titles as META keywords
603 $wgOut->addMetaTags() ;
605 $this->viewUpdates();
606 wfProfileOut( $fname );
609 # Theoretically we could defer these whole insert and update
610 # functions for after display, but that's taking a big leap
611 # of faith, and we want to be able to report database
612 # errors at some point.
614 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
616 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
617 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
619 $fname = "Article::insertNewArticle";
621 $this->mCountAdjustment
= $this->isCountable( $text );
623 $ns = $this->mTitle
->getNamespace();
624 $ttl = $this->mTitle
->getDBkey();
625 $text = $this->preSaveTransform( $text );
626 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
629 $now = wfTimestampNow();
630 $won = wfInvertTimestamp( $now );
632 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
633 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
634 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
635 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
636 "cur_restrictions,cur_user_text,cur_is_redirect," .
637 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
638 wfStrencode( $text ) . "', '" .
639 wfStrencode( $summary ) . "', '" .
640 $wgUser->getID() . "', '{$now}', " .
641 $isminor . ", 0, '', '" .
642 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
643 $res = wfQuery( $sql, DB_WRITE
, $fname );
645 $newid = wfInsertId();
646 $this->mTitle
->resetArticleID( $newid );
648 Article
::onArticleCreate( $this->mTitle
);
649 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
652 if(!$this->mTitle
->userIsWatching()) $this->watch();
654 if ( $this->mTitle
->userIsWatching() ) {
659 # The talk page isn't in the regular link tables, so we need to update manually:
660 $talkns = $ns ^
1; # talk -> normal; normal -> talk
661 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
662 wfQuery( $sql, DB_WRITE
);
664 # standard deferred updates
665 $this->editUpdates( $text );
667 $this->showArticle( $text, wfMsg( "newarticle" ) );
671 /* Side effects: loads last edit */
672 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
673 $this->loadLastEdit();
674 $oldtext = $this->getContent( true );
675 if ($section != "") {
676 if($section=="new") {
677 if($summary) $subject="== {$summary} ==\n\n";
678 $text=$oldtext."\n\n".$subject.$text;
681 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
682 # comments to be stripped as well)
684 $parser=new Parser();
685 $parser->mOutputType
=OT_WIKI
;
686 $oldtext=$parser->strip($oldtext, $striparray, true);
688 # now that we can be sure that no pseudo-sections are in the source,
690 # Unfortunately we can't simply do a preg_replace because that might
691 # replace the wrong section, so we have to use the section counter instead
692 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?" . ">.*?<\/h[1-6].*?" . ">)/mi",
693 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
694 $secs[$section*2]=$text."\n\n"; // replace with edited
696 # section 0 is top (intro) section
699 # headline of old section - we need to go through this section
700 # to determine if there are any subsections that now need to
701 # be erased, as the mother section has been replaced with
702 # the text of all subsections.
703 $headline=$secs[$section*2-1];
704 preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$headline,$matches);
707 # determine headline level for wikimarkup headings
708 if(strpos($hlevel,"=")!==false) {
709 $hlevel=strlen($hlevel);
712 $secs[$section*2-1]=""; // erase old headline
715 while(!empty($secs[$count*2-1]) && !$break) {
717 $subheadline=$secs[$count*2-1];
719 "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$subheadline,$matches);
720 $subhlevel=$matches[1];
721 if(strpos($subhlevel,"=")!==false) {
722 $subhlevel=strlen($subhlevel);
724 if($subhlevel > $hlevel) {
725 // erase old subsections
726 $secs[$count*2-1]="";
729 if($subhlevel <= $hlevel) {
737 $text=join("",$secs);
738 # reinsert the stuff that we stripped out earlier
739 $text=$parser->unstrip($text,$striparray);
746 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false )
748 global $wgOut, $wgUser, $wgLinkCache;
749 global $wgDBtransactions, $wgMwRedir;
750 global $wgUseSquid, $wgInternalServer;
751 $fname = "Article::updateArticle";
753 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
754 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
755 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
757 $text = $m[1] . "\n"; # Remove all content but redirect
761 $text = $this->preSaveTransform( $text );
763 # Update article, but only if changed.
765 if( $wgDBtransactions ) {
767 wfQuery( $sql, DB_WRITE
);
769 $oldtext = $this->getContent( true );
771 if ( 0 != strcmp( $text, $oldtext ) ) {
772 $this->mCountAdjustment
= $this->isCountable( $text )
773 - $this->isCountable( $oldtext );
775 $now = wfTimestampNow();
776 $won = wfInvertTimestamp( $now );
777 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
778 "',cur_comment='" . wfStrencode( $summary ) .
779 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
780 ",cur_timestamp='{$now}',cur_user_text='" .
781 wfStrencode( $wgUser->getName() ) .
782 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
783 "WHERE cur_id=" . $this->getID() .
784 " AND cur_timestamp='" . $this->getTimestamp() . "'";
785 $res = wfQuery( $sql, DB_WRITE
, $fname );
787 if( wfAffectedRows() == 0 ) {
788 /* Belated edit conflict! Run away!! */
792 # This overwrites $oldtext if revision compression is on
793 $flags = Article
::compressRevisionText( $oldtext );
795 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
796 "old_comment,old_user,old_user_text,old_timestamp," .
797 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
798 $this->mTitle
->getNamespace() . ", '" .
799 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
800 wfStrencode( $oldtext ) . "', '" .
801 wfStrencode( $this->getComment() ) . "', " .
802 $this->getUser() . ", '" .
803 wfStrencode( $this->getUserText() ) . "', '" .
804 $this->getTimestamp() . "', " . $me1 . ", '" .
805 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
806 $res = wfQuery( $sql, DB_WRITE
, $fname );
807 $oldid = wfInsertID( $res );
809 $bot = (int)($wgUser->isBot() ||
$forceBot);
810 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
811 $oldid, $this->getTimestamp(), $bot );
812 Article
::onArticleEdit( $this->mTitle
);
815 if( $wgDBtransactions ) {
817 wfQuery( $sql, DB_WRITE
);
821 if (!$this->mTitle
->userIsWatching()) $this->watch();
823 if ( $this->mTitle
->userIsWatching() ) {
827 # standard deferred updates
828 $this->editUpdates( $text );
833 # Purge all articles linking here
834 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
835 $titles = $this->mTitle
->getLinksTo();
836 Title
::touchArray( $titles );
838 foreach ( $titles as $title ) {
839 $urls[] = $title->getInternalURL();
846 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
847 $u = new SquidUpdate( $urls );
851 $this->showArticle( $text, wfMsg( "updated" ) );
855 # After we've either updated or inserted the article, update
856 # the link tables and redirect to the new page.
858 function showArticle( $text, $subtitle )
860 global $wgOut, $wgUser, $wgLinkCache;
863 $wgLinkCache = new LinkCache();
865 # Get old version of link table to allow incremental link updates
866 $wgLinkCache->preFill( $this->mTitle
);
867 $wgLinkCache->clear();
869 # Now update the link cache by parsing the text
870 $wgOut = new OutputPage();
871 $wgOut->addWikiText( $text );
873 if( $wgMwRedir->matchStart( $text ) )
877 $wgOut->redirect( $this->mTitle
->getFullURL( $r ) );
880 # Add this page to my watchlist
882 function watch( $add = true )
884 global $wgUser, $wgOut, $wgLang;
885 global $wgDeferredUpdateList;
887 if ( 0 == $wgUser->getID() ) {
888 $wgOut->errorpage( "watchnologin", "watchnologintext" );
891 if ( wfReadOnly() ) {
892 $wgOut->readOnlyPage();
896 $wgUser->addWatch( $this->mTitle
);
898 $wgUser->removeWatch( $this->mTitle
);
900 $wgOut->setPagetitle( wfMsg( $add ?
"addedwatch" : "removedwatch" ) );
901 $wgOut->setRobotpolicy( "noindex,follow" );
903 $sk = $wgUser->getSkin() ;
904 $link = $this->mTitle
->getPrefixedText();
907 $text = wfMsg( "addedwatchtext", $link );
909 $text = wfMsg( "removedwatchtext", $link );
910 $wgOut->addWikiText( $text );
912 $up = new UserUpdate();
913 array_push( $wgDeferredUpdateList, $up );
915 $wgOut->returnToMain( false );
920 $this->watch( false );
923 function protect( $limit = "sysop" )
925 global $wgUser, $wgOut, $wgRequest;
927 if ( ! $wgUser->isSysop() ) {
928 $wgOut->sysopRequired();
931 if ( wfReadOnly() ) {
932 $wgOut->readOnlyPage();
935 $id = $this->mTitle
->getArticleID();
937 $wgOut->fatalError( wfMsg( "badarticleerror" ) );
941 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
942 $reason = $wgRequest->getText( 'wpReasonProtect' );
946 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
947 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
948 wfQuery( $sql, DB_WRITE
, "Article::protect" );
950 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
951 if ( $limit === "" ) {
952 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle
->getPrefixedText() ), $reason );
954 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle
->getPrefixedText() ), $reason );
956 $wgOut->redirect( $this->mTitle
->getFullURL() );
959 $reason = htmlspecialchars( wfMsg( "protectreason" ) );
960 return $this->confirmProtect( "", $reason, $limit );
964 # Output protection confirmation dialog
965 function confirmProtect( $par, $reason, $limit = "sysop" )
969 wfDebug( "Article::confirmProtect\n" );
971 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
972 $wgOut->setRobotpolicy( "noindex,nofollow" );
977 if ( $limit === "" ) {
978 $wgOut->setSubtitle( wfMsg( "unprotectsub", $sub ) );
979 $wgOut->addWikiText( wfMsg( "confirmunprotecttext" ) );
980 $check = htmlspecialchars( wfMsg( "confirmunprotect" ) );
981 $protcom = htmlspecialchars( wfMsg( "unprotectcomment" ) );
982 $formaction = $this->mTitle
->escapeLocalURL( "action=unprotect" . $par );
984 $wgOut->setSubtitle( wfMsg( "protectsub", $sub ) );
985 $wgOut->addWikiText( wfMsg( "confirmprotecttext" ) );
986 $check = htmlspecialchars( wfMsg( "confirmprotect" ) );
987 $protcom = htmlspecialchars( wfMsg( "protectcomment" ) );
988 $formaction = $this->mTitle
->escapeLocalURL( "action=protect" . $par );
991 $confirm = htmlspecialchars( wfMsg( "confirm" ) );
994 <form id='protectconfirm' method='post' action=\"{$formaction}\">
998 <label for='wpReasonProtect'>{$protcom}:</label>
1001 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1009 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1012 <label for='wpConfirmProtect'>{$check}</label>
1018 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1024 $wgOut->returnToMain( false );
1027 function unprotect()
1029 return $this->protect( "" );
1032 # UI entry point for page deletion
1035 global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
1036 $fname = "Article::delete";
1037 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1038 $reason = $wgRequest->getText( 'wpReason' );
1040 # This code desperately needs to be totally rewritten
1043 if ( ( ! $wgUser->isSysop() ) ) {
1044 $wgOut->sysopRequired();
1047 if ( wfReadOnly() ) {
1048 $wgOut->readOnlyPage();
1052 # Better double-check that it hasn't been deleted yet!
1053 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
1054 if ( ( "" == trim( $this->mTitle
->getText() ) )
1055 or ( $this->mTitle
->getArticleId() == 0 ) ) {
1056 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
1061 $this->doDelete( $reason );
1065 # determine whether this page has earlier revisions
1066 # and insert a warning if it does
1067 # we select the text because it might be useful below
1068 $ns = $this->mTitle
->getNamespace();
1069 $title = $this->mTitle
->getDBkey();
1070 $etitle = wfStrencode( $title );
1071 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
1072 $res = wfQuery( $sql, DB_READ
, $fname );
1073 if( ($old=wfFetchObject($res)) && !$confirm ) {
1074 $skin=$wgUser->getSkin();
1075 $wgOut->addHTML("<b>".wfMsg("historywarning"));
1076 $wgOut->addHTML( $skin->historyLink() ."</b>");
1079 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
1080 $res=wfQuery($sql, DB_READ
, $fname);
1081 if( ($s=wfFetchObject($res))) {
1083 # if this is a mini-text, we can paste part of it into the deletion reason
1085 #if this is empty, an earlier revision may contain "useful" text
1087 if($s->cur_text
!="") {
1091 $text = Article
::getRevisionText( $old );
1097 $length=strlen($text);
1099 # this should not happen, since it is not possible to store an empty, new
1100 # page. Let's insert a standard text in case it does, though
1101 if($length == 0 && $reason === "") {
1102 $reason = wfMsg("exblank");
1105 if($length < 500 && $reason === "") {
1107 # comment field=255, let's grep the first 150 to have some user
1109 $text=substr($text,0,150);
1110 # let's strip out newlines and HTML tags
1111 $text=preg_replace("/\"/","'",$text);
1112 $text=preg_replace("/\</","<",$text);
1113 $text=preg_replace("/\>/",">",$text);
1114 $text=preg_replace("/[\n\r]/","",$text);
1116 $reason=wfMsg("excontent"). " '".$text;
1118 $reason=wfMsg("exbeforeblank") . " '".$text;
1120 if($length>150) { $reason .= "..."; } # we've only pasted part of the text
1125 return $this->confirmDelete( "", $reason );
1128 # Output deletion confirmation dialog
1129 function confirmDelete( $par, $reason )
1133 wfDebug( "Article::confirmDelete\n" );
1135 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1136 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
1137 $wgOut->setRobotpolicy( "noindex,nofollow" );
1138 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
1140 $formaction = $this->mTitle
->escapeLocalURL( "action=delete" . $par );
1142 $confirm = htmlspecialchars( wfMsg( "confirm" ) );
1143 $check = htmlspecialchars( wfMsg( "confirmcheck" ) );
1144 $delcom = htmlspecialchars( wfMsg( "deletecomment" ) );
1147 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1151 <label for='wpReason'>{$delcom}:</label>
1154 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1162 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1165 <label for='wpConfirm'>{$check}</label>
1171 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1177 $wgOut->returnToMain( false );
1180 # Perform a deletion and output success or failure messages
1181 function doDelete( $reason )
1183 global $wgOut, $wgUser, $wgLang;
1184 $fname = "Article::doDelete";
1185 wfDebug( "$fname\n" );
1187 if ( $this->doDeleteArticle( $reason ) ) {
1188 $deleted = $this->mTitle
->getPrefixedText();
1190 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1191 $wgOut->setRobotpolicy( "noindex,nofollow" );
1193 $sk = $wgUser->getSkin();
1194 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1195 Namespace::getWikipedia() ) .
1196 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
1198 $text = wfMsg( "deletedtext", $deleted, $loglink );
1200 $wgOut->addHTML( "<p>" . $text . "</p>\n" );
1201 $wgOut->returnToMain( false );
1203 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
1207 # Back-end article deletion
1208 # Deletes the article with database consistency, writes logs, purges caches
1210 function doDeleteArticle( $reason )
1212 global $wgUser, $wgLang;
1213 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1215 $fname = "Article::doDeleteArticle";
1216 wfDebug( "$fname\n" );
1218 $ns = $this->mTitle
->getNamespace();
1219 $t = wfStrencode( $this->mTitle
->getDBkey() );
1220 $id = $this->mTitle
->getArticleID();
1222 if ( "" == $t ||
$id == 0 ) {
1226 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1227 array_push( $wgDeferredUpdateList, $u );
1229 $linksTo = $this->mTitle
->getLinksTo();
1232 if ( $wgUseSquid ) {
1234 $this->mTitle
->getInternalURL(),
1235 $this->mTitle
->getInternalURL( "history" )
1237 foreach ( $linksTo as $linkTo ) {
1238 $urls[] = $linkTo->getInternalURL();
1241 $u = new SquidUpdate( $urls );
1242 array_push( $wgDeferredUpdateList, $u );
1246 # Client and file cache invalidation
1247 Title
::touchArray( $linksTo );
1249 # Move article and history to the "archive" table
1250 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1251 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1252 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
1253 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
1254 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1255 wfQuery( $sql, DB_WRITE
, $fname );
1257 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1258 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1259 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
1260 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
1261 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1262 wfQuery( $sql, DB_WRITE
, $fname );
1264 # Now that it's safely backed up, delete it
1266 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1268 wfQuery( $sql, DB_WRITE
, $fname );
1270 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1272 wfQuery( $sql, DB_WRITE
, $fname );
1274 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1276 wfQuery( $sql, DB_WRITE
, $fname );
1278 # Finally, clean up the link tables
1279 $t = wfStrencode( $this->mTitle
->getPrefixedDBkey() );
1281 Article
::onArticleDelete( $this->mTitle
);
1283 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1286 foreach ( $linksTo as $titleObj ) {
1287 if ( ! $first ) { $sql .= ","; }
1289 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1290 $linkID = $titleObj->getArticleID();
1291 $sql .= "({$linkID},'{$t}')";
1294 wfQuery( $sql, DB_WRITE
, $fname );
1297 $sql = "DELETE FROM links WHERE l_to={$id}";
1298 wfQuery( $sql, DB_WRITE
, $fname );
1300 $sql = "DELETE FROM links WHERE l_from={$id}";
1301 wfQuery( $sql, DB_WRITE
, $fname );
1303 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
1304 wfQuery( $sql, DB_WRITE
, $fname );
1306 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1307 wfQuery( $sql, DB_WRITE
, $fname );
1309 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1310 $art = $this->mTitle
->getPrefixedText();
1311 $log->addEntry( wfMsg( "deletedarticle", $art ), $reason );
1313 # Clear the cached article id so the interface doesn't act like we exist
1314 $this->mTitle
->resetArticleID( 0 );
1315 $this->mTitle
->mArticleID
= 0;
1321 global $wgUser, $wgLang, $wgOut, $wgRequest;
1323 if ( ! $wgUser->isSysop() ) {
1324 $wgOut->sysopRequired();
1327 if ( wfReadOnly() ) {
1328 $wgOut->readOnlyPage( $this->getContent( true ) );
1332 # Enhanced rollback, marks edits rc_bot=1
1333 $bot = $wgRequest->getBool( 'bot' );
1335 # Replace all this user's current edits with the next one down
1336 $tt = wfStrencode( $this->mTitle
->getDBKey() );
1337 $n = $this->mTitle
->getNamespace();
1339 # Get the last editor
1340 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1341 $res = wfQuery( $sql, DB_READ
);
1342 if( ($x = wfNumRows( $res )) != 1 ) {
1344 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1347 $s = wfFetchObject( $res );
1348 $ut = wfStrencode( $s->cur_user_text
);
1349 $uid = $s->cur_user
;
1352 $from = str_replace( '_', ' ', $wgRequest->getVal( "from" ) );
1353 if( $from != $s->cur_user_text
) {
1354 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1355 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1356 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1357 htmlspecialchars( $from ),
1358 htmlspecialchars( $s->cur_user_text
) ) );
1359 if($s->cur_comment
!= "") {
1361 wfMsg("editcomment",
1362 htmlspecialchars( $s->cur_comment
) ) );
1367 # Get the last edit not by this guy
1368 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
1369 FROM old USE INDEX (name_title_timestamp)
1370 WHERE old_namespace={$n} AND old_title='{$tt}'
1371 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1372 ORDER BY inverse_timestamp LIMIT 1";
1373 $res = wfQuery( $sql, DB_READ
);
1374 if( wfNumRows( $res ) != 1 ) {
1376 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1377 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1380 $s = wfFetchObject( $res );
1383 # Mark all reverted edits as bot
1384 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1385 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1386 wfQuery( $sql, DB_WRITE
, $fname );
1390 $newcomment = wfMsg( "revertpage", $s->old_user_text
, $from );
1391 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1392 $wgOut->setRobotpolicy( "noindex,nofollow" );
1393 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr />\n" );
1394 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1395 Article
::onArticleEdit( $this->mTitle
);
1396 $wgOut->returnToMain( false );
1400 # Do standard deferred updates after page view
1402 /* private */ function viewUpdates()
1404 global $wgDeferredUpdateList;
1405 if ( 0 != $this->getID() ) {
1406 global $wgDisableCounters;
1407 if( !$wgDisableCounters ) {
1408 Article
::incViewCount( $this->getID() );
1409 $u = new SiteStatsUpdate( 1, 0, 0 );
1410 array_push( $wgDeferredUpdateList, $u );
1412 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1413 $this->mTitle
->getDBkey() );
1414 array_push( $wgDeferredUpdateList, $u );
1418 # Do standard deferred updates after page edit.
1419 # Every 1000th edit, prune the recent changes table.
1421 /* private */ function editUpdates( $text )
1423 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1424 global $wgMessageCache;
1427 if ( 0 == mt_rand( 0, 999 ) ) {
1428 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1429 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1430 wfQuery( $sql, DB_WRITE
);
1432 $id = $this->getID();
1433 $title = $this->mTitle
->getPrefixedDBkey();
1434 $shortTitle = $this->mTitle
->getDBkey();
1436 $adj = $this->mCountAdjustment
;
1439 $u = new LinksUpdate( $id, $title );
1440 array_push( $wgDeferredUpdateList, $u );
1441 $u = new SiteStatsUpdate( 0, 1, $adj );
1442 array_push( $wgDeferredUpdateList, $u );
1443 $u = new SearchUpdate( $id, $title, $text );
1444 array_push( $wgDeferredUpdateList, $u );
1446 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1447 array_push( $wgDeferredUpdateList, $u );
1449 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1450 $wgMessageCache->replace( $shortTitle, $text );
1455 /* private */ function setOldSubtitle()
1457 global $wgLang, $wgOut;
1459 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1460 $r = wfMsg( "revisionasof", $td );
1461 $wgOut->setSubtitle( "({$r})" );
1464 # This function is called right before saving the wikitext,
1465 # so we can do things like signatures and links-in-context.
1467 function preSaveTransform( $text )
1469 global $wgParser, $wgUser;
1470 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1473 /* Caching functions */
1475 # checkLastModified returns true if it has taken care of all
1476 # output to the client that is necessary for this request.
1477 # (that is, it has sent a cached version of the page)
1478 function tryFileCache() {
1479 static $called = false;
1481 wfDebug( " tryFileCache() -- called twice!?\n" );
1485 if($this->isFileCacheable()) {
1486 $touched = $this->mTouched
;
1487 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1488 # Expire the main page quicker
1489 $expire = wfUnix2Timestamp( time() - 3600 );
1490 $touched = max( $expire, $touched );
1492 $cache = new CacheManager( $this->mTitle
);
1493 if($cache->isFileCacheGood( $touched )) {
1495 wfDebug( " tryFileCache() - about to load\n" );
1496 $cache->loadFromFileCache();
1499 wfDebug( " tryFileCache() - starting buffer\n" );
1500 ob_start( array(&$cache, 'saveToFileCache' ) );
1503 wfDebug( " tryFileCache() - not cacheable\n" );
1507 function isFileCacheable() {
1508 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1509 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1511 return $wgUseFileCache
1512 and (!$wgShowIPinHeader)
1513 and ($this->getID() != 0)
1514 and ($wgUser->getId() == 0)
1515 and (!$wgUser->getNewtalk())
1516 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1517 and ($action == "view")
1518 and (!isset($oldid))
1520 and (!isset($redirect))
1521 and (!isset($printable))
1522 and (!$this->mRedirectedFrom
);
1525 function checkTouched() {
1526 $id = $this->getID();
1527 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1528 $res = wfQuery( $sql, DB_READ
, "Article::checkTouched" );
1529 if( $s = wfFetchObject( $res ) ) {
1530 $this->mTouched
= $s->cur_touched
;
1531 return !$s->cur_is_redirect
;
1537 /* static */ function incViewCount( $id )
1539 $id = intval( $id );
1540 global $wgHitcounterUpdateFreq;
1542 if( $wgHitcounterUpdateFreq <= 1 ){ //
1543 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1544 "WHERE cur_id = $id", DB_WRITE
);
1548 # Not important enough to warrant an error page in case of failure
1549 $oldignore = wfIgnoreSQLErrors( true );
1551 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1553 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1554 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1555 # Most of the time (or on SQL errors), skip row count check
1556 wfIgnoreSQLErrors( $oldignore );
1560 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE
);
1561 $row = wfFetchObject( $res );
1562 $rown = intval( $row->n
);
1563 if( $rown >= $wgHitcounterUpdateFreq ){
1564 wfProfileIn( "Article::incViewCount-collect" );
1565 $old_user_abort = ignore_user_abort( true );
1567 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE
);
1568 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1569 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1570 "GROUP BY hc_id", DB_WRITE
);
1571 wfQuery("DELETE FROM hitcounter", DB_WRITE
);
1572 wfQuery("UNLOCK TABLES", DB_WRITE
);
1573 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1574 "WHERE cur_id = hc_id", DB_WRITE
);
1575 wfQuery("DROP TABLE acchits", DB_WRITE
);
1577 ignore_user_abort( $old_user_abort );
1578 wfProfileOut( "Article::incViewCount-collect" );
1580 wfIgnoreSQLErrors( $oldignore );
1583 # The onArticle*() functions are supposed to be a kind of hooks
1584 # which should be called whenever any of the specified actions
1587 # This is a good place to put code to clear caches, for instance.
1589 # This is called on page move and undelete, as well as edit
1590 /* static */ function onArticleCreate($title_obj){
1591 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
1593 $titles = $title_obj->getBrokenLinksTo();
1596 if ( $wgUseSquid ) {
1597 $urls = $title_obj->getSquidURLs();
1598 foreach ( $titles as $linkTitle ) {
1599 $urls[] = $linkTitle->getInternalURL();
1601 $u = new SquidUpdate( $urls );
1602 array_push( $wgDeferredUpdateList, $u );
1605 # Clear persistent link cache
1606 if ( $wgEnablePersistentLC ) {
1607 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1610 # Clear parser cache (not really used)
1611 if ( $wgEnableParserCache ) {
1612 OutputPage
::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1616 /* static */ function onArticleDelete($title_obj){
1617 global $wgEnablePersistentLC, $wgEnableParserCache;
1618 if ( $wgEnablePersistentLC ) {
1619 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1621 if ( $wgEnableParserCache ) {
1622 OutputPage
::parsercacheClearLinksTo( $title_obj->getArticleID() );
1626 /* static */ function onArticleEdit($title_obj){
1627 global $wgEnablePersistentLC, $wgEnableParserCache;
1628 if ( $wgEnablePersistentLC ) {
1629 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1631 if ( $wgEnableParserCache ) {
1632 OutputPage
::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );