4 # Class representing a Wikipedia article and history.
5 # See design.doc for an overview.
7 # Note: edit user interface and cache support functions have been
8 # moved to separate EditPage and CacheManager classes.
10 require_once( 'CacheManager.php' );
13 /* private */ var $mContent, $mContentLoaded;
14 /* private */ var $mUser, $mTimestamp, $mUserText;
15 /* private */ var $mCounter, $mComment, $mCountAdjustment;
16 /* private */ var $mMinorEdit, $mRedirectedFrom;
17 /* private */ var $mTouched, $mFileCache, $mTitle;
18 /* private */ var $mId, $mTable;
20 function Article( &$title ) {
21 $this->mTitle
=& $title;
25 /* private */ function clear()
27 $this->mContentLoaded
= false;
28 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
29 $this->mRedirectedFrom
= $this->mUserText
=
30 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= '';
31 $this->mCountAdjustment
= 0;
32 $this->mTouched
= '19700101000000';
35 /* static */ function getRevisionText( $row, $prefix = 'old_' ) {
36 # Deal with optional compression of archived pages.
37 # This can be done periodically via maintenance/compressOld.php, and
38 # as pages are saved if $wgCompressRevisions is set.
39 $text = $prefix . 'text';
40 $flags = $prefix . 'flags';
41 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, 'gzip' ) ) ) {
42 return gzinflate( $row->$text );
44 if( isset( $row->$text ) ) {
50 /* static */ function compressRevisionText( &$text ) {
51 global $wgCompressRevisions;
52 if( !$wgCompressRevisions ) {
55 if( !function_exists( 'gzdeflate' ) ) {
56 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
59 $text = gzdeflate( $text );
63 # Note that getContent/loadContent may follow redirects if
64 # not told otherwise, and so may cause a change to mTitle.
66 # Return the text of this revision
67 function getContent( $noredir )
71 # Get variables from query string :P
72 $action = $wgRequest->getText( 'action', 'view' );
73 $section = $wgRequest->getText( 'section' );
75 $fname = 'Article::getContent';
76 wfProfileIn( $fname );
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 );
89 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
90 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
91 preg_match('/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/',$this->mTitle
->getText()) &&
95 wfProfileOut( $fname );
96 return $this->mContent
. "\n" .wfMsg('anontalkpagetext'); }
100 if($section=='new') {
101 wfProfileOut( $fname );
105 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
106 # comments to be stripped as well)
107 $rv=$this->getSection($this->mContent
,$section);
108 wfProfileOut( $fname );
112 wfProfileOut( $fname );
113 return $this->mContent
;
118 # This function returns the text of a section, specified by a number ($section).
119 # A section is text under a heading like == Heading == or <h1>Heading</h1>, or
120 # the first section before any such heading (section 0).
122 # If a section contains subsections, these are also returned.
124 function getSection($text,$section) {
126 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
127 # comments to be stripped as well)
129 $parser=new Parser();
130 $parser->mOutputType
=OT_WIKI
;
131 $striptext=$parser->strip($text, $striparray, true);
133 # now that we can be sure that no pseudo-sections are in the source,
134 # split it up by section
137 '/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
139 PREG_SPLIT_DELIM_CAPTURE
);
143 $headline=$secs[$section*2-1];
144 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
147 # translate wiki heading into level
148 if(strpos($hlevel,'=')!==false) {
149 $hlevel=strlen($hlevel);
152 $rv=$headline. $secs[$section*2];
156 while(!empty($secs[$count*2-1]) && !$break) {
158 $subheadline=$secs[$count*2-1];
159 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
160 $subhlevel=$matches[1];
161 if(strpos($subhlevel,'=')!==false) {
162 $subhlevel=strlen($subhlevel);
164 if($subhlevel > $hlevel) {
165 $rv.=$subheadline.$secs[$count*2];
167 if($subhlevel <= $hlevel) {
174 # reinsert stripped tags
175 $rv=$parser->unstrip($rv,$striparray);
176 $rv=$parser->unstripNoWiki($rv,$striparray);
183 # Load the revision (including cur_text) into this object
184 function loadContent( $noredir = false )
186 global $wgOut, $wgMwRedir, $wgRequest, $wgIsPg;
189 $oldid = $wgRequest->getVal( 'oldid' );
190 $redirect = $wgRequest->getVal( 'redirect' );
192 if ( $this->mContentLoaded
) return;
193 $fname = 'Article::loadContent';
195 # Pre-fill content with error message so that if something
196 # fails we'll have something telling us what we intended.
198 $t = $this->mTitle
->getPrefixedText();
199 if ( isset( $oldid ) ) {
200 $oldid = IntVal( $oldid );
201 $t .= ",oldid={$oldid}";
203 if ( isset( $redirect ) ) {
204 $redirect = ($redirect == 'no') ?
'no' : 'yes';
205 $t .= ",redirect={$redirect}";
207 $this->mContent
= wfMsg( 'missingarticle', $t );
209 if ( ! $oldid ) { # Retrieve current version
210 $id = $this->getID();
211 if ( 0 == $id ) return;
214 'cur_text,cur_timestamp,cur_user,cur_user_text,cur_comment,cur_counter,cur_restrictions,cur_touched ' .
215 "FROM cur WHERE cur_id={$id}";
217 $res = wfQuery( $sql, DB_READ
, $fname );
218 if ( 0 == wfNumRows( $res ) ) {
222 $s = wfFetchObject( $res );
223 # If we got a redirect, follow it (unless we've been told
224 # not to by either the function parameter or the query
225 if ( ( 'no' != $redirect ) && ( false == $noredir ) &&
226 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
227 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
228 $s->cur_text
, $m ) ) {
229 $rt = Title
::newFromText( $m[1] );
231 # Gotta hand redirects to special pages differently:
232 # Fill the HTTP response "Location" header and ignore
233 # the rest of the page we're on.
235 if ( $rt->getInterwiki() != '' ) {
236 $wgOut->redirect( $rt->getFullURL() ) ;
239 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
240 $wgOut->redirect( $rt->getFullURL() );
243 $rid = $rt->getArticleID();
245 $sql = 'SELECT cur_text,cur_timestamp,cur_user,cur_user_text,cur_comment,' .
246 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
247 $res = wfQuery( $sql, DB_READ
, $fname );
249 if ( 0 != wfNumRows( $res ) ) {
250 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
252 $s = wfFetchObject( $res );
259 $this->mContent
= $s->cur_text
;
260 $this->mUser
= $s->cur_user
;
261 $this->mUserText
= $s->cur_user_text
;
262 $this->mComment
= $s->cur_comment
;
263 $this->mCounter
= $s->cur_counter
;
264 $this->mTimestamp
= $s->cur_timestamp
;
265 $this->mTouched
= $s->cur_touched
;
266 $this->mTitle
->mRestrictions
= explode( ',', trim( $s->cur_restrictions
) );
267 $this->mTitle
->mRestrictionsLoaded
= true;
268 wfFreeResult( $res );
269 } else { # oldid set, retrieve historical version
270 $oldtable=$wgIsPg?
'"old"':'old';
271 $sql = "SELECT old_namespace,old_title,old_text,old_timestamp,".
272 "old_user,old_user_text,old_comment,old_flags FROM old ".
273 "WHERE old_id={$oldid}";
274 $res = wfQuery( $sql, DB_READ
, $fname );
275 if ( 0 == wfNumRows( $res ) ) {
279 $s = wfFetchObject( $res );
280 if( $this->mTitle
->getNamespace() != $s->old_namespace ||
281 $this->mTitle
->getDBkey() != $s->old_title
) {
282 $oldTitle = Title
::makeTitle( $s->old_namesapce
, $s->old_title
);
283 $this->mTitle
= $oldTitle;
284 $wgTitle = $oldTitle;
286 $this->mContent
= Article
::getRevisionText( $s );
287 $this->mUser
= $s->old_user
;
288 $this->mUserText
= $s->old_user_text
;
289 $this->mComment
= $s->old_comment
;
291 $this->mTimestamp
= $s->old_timestamp
;
292 wfFreeResult( $res );
294 $this->mContentLoaded
= true;
295 return $this->mContent
;
298 # Gets the article text without using so many damn globals
299 # Returns false on error
300 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
301 global $wgMwRedir, $wgIsPg;
303 if ( $this->mContentLoaded
) {
304 return $this->mContent
;
306 $this->mContent
= false;
308 $fname = 'Article::loadContent';
310 if ( ! $oldid ) { # Retrieve current version
311 $id = $this->getID();
317 'cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched ' .
318 "FROM cur WHERE cur_id={$id}";
319 $res = wfQuery( $sql, DB_READ
, $fname );
320 if ( 0 == wfNumRows( $res ) ) {
324 $s = wfFetchObject( $res );
325 # If we got a redirect, follow it (unless we've been told
326 # not to by either the function parameter or the query
327 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text
) ) {
328 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
329 $s->cur_text
, $m ) ) {
330 $rt = Title
::newFromText( $m[1] );
331 if( $rt && $rt->getInterwiki() == '' && $rt->getNamespace() != Namespace::getSpecial() ) {
332 $rid = $rt->getArticleID();
334 $sql = 'SELECT cur_text,cur_timestamp,cur_user,' .
335 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
336 $res = wfQuery( $sql, DB_READ
, $fname );
338 if ( 0 != wfNumRows( $res ) ) {
339 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
341 $s = wfFetchObject( $res );
348 $this->mContent
= $s->cur_text
;
349 $this->mUser
= $s->cur_user
;
350 $this->mCounter
= $s->cur_counter
;
351 $this->mTimestamp
= $s->cur_timestamp
;
352 $this->mTouched
= $s->cur_touched
;
353 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
354 $this->mTitle
->mRestrictionsLoaded
= true;
355 wfFreeResult( $res );
356 } else { # oldid set, retrieve historical version
357 $oldtable=$wgIsPg?
'"old"':'old';
358 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM $oldtable " .
359 "WHERE old_id={$oldid}";
360 $res = wfQuery( $sql, DB_READ
, $fname );
361 if ( 0 == wfNumRows( $res ) ) {
365 $s = wfFetchObject( $res );
366 $this->mContent
= Article
::getRevisionText( $s );
367 $this->mUser
= $s->old_user
;
369 $this->mTimestamp
= $s->old_timestamp
;
370 wfFreeResult( $res );
372 $this->mContentLoaded
= true;
373 return $this->mContent
;
377 if( $this->mTitle
) {
378 return $this->mTitle
->getArticleID();
386 if ( -1 == $this->mCounter
) {
387 $id = $this->getID();
388 $this->mCounter
= wfGetSQL( 'cur', 'cur_counter', "cur_id={$id}" );
390 return $this->mCounter
;
393 # Would the given text make this article a "good" article (i.e.,
394 # suitable for including in the article count)?
396 function isCountable( $text )
398 global $wgUseCommaCount, $wgMwRedir;
400 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
401 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
402 $token = ($wgUseCommaCount ?
',' : '[[' );
403 if ( false === strstr( $text, $token ) ) { return 0; }
407 # Loads everything from cur except cur_text
408 # This isn't necessary for all uses, so it's only done if needed.
410 /* private */ function loadLastEdit()
413 if ( -1 != $this->mUser
) return;
415 $sql = 'SELECT cur_user,cur_user_text,cur_timestamp,' .
416 'cur_comment,cur_minor_edit FROM cur WHERE ' .
417 'cur_id=' . $this->getID();
418 $res = wfQuery( $sql, DB_READ
, 'Article::loadLastEdit' );
420 if ( wfNumRows( $res ) > 0 ) {
421 $s = wfFetchObject( $res );
422 $this->mUser
= $s->cur_user
;
423 $this->mUserText
= $s->cur_user_text
;
424 $this->mTimestamp
= $s->cur_timestamp
;
425 $this->mComment
= $s->cur_comment
;
426 $this->mMinorEdit
= $s->cur_minor_edit
;
430 function getTimestamp()
432 $this->loadLastEdit();
433 return $this->mTimestamp
;
438 $this->loadLastEdit();
442 function getUserText()
444 $this->loadLastEdit();
445 return $this->mUserText
;
448 function getComment()
450 $this->loadLastEdit();
451 return $this->mComment
;
454 function getMinorEdit()
456 $this->loadLastEdit();
457 return $this->mMinorEdit
;
460 function getContributors($limit = 0, $offset = 0)
462 $fname = 'Article::getContributors';
464 # XXX: this is expensive; cache this info somewhere.
466 $title = $this->mTitle
;
470 $sql = 'SELECT old.old_user, old.old_user_text, ' .
471 ' user.user_real_name, MAX(old.old_timestamp) as timestamp' .
473 ' WHERE old.old_user = user.user_id ' .
474 ' AND old.old_namespace = ' . $title->getNamespace() .
475 ' AND old.old_title = "' . $title->getDBkey() . '"' .
476 ' AND old.old_user != 0 ' .
477 ' AND old.old_user != ' . $this->getUser() .
478 ' GROUP BY old.old_user ' .
479 ' ORDER BY timestamp DESC ';
482 $sql .= ' LIMIT '.$limit;
485 $res = wfQuery($sql, DB_READ
, $fname);
487 while ( $line = wfFetchObject( $res ) ) {
488 $contribs[$line->old_user
] =
489 array($line->old_user_text
, $line->user_real_name
);
492 # Count anonymous users
494 $res = wfQuery('SELECT COUNT(*) AS cnt ' .
496 ' WHERE old_namespace = ' . $title->getNamespace() .
497 " AND old_title = '" . $title->getDBkey() . "'" .
498 ' AND old_user = 0 ', DB_READ
, $fname);
500 while ( $line = wfFetchObject( $res ) ) {
501 $contribs[0] = array($line->cnt
, 'Anonymous');
507 # This is the default action of the script: just view the page of
512 global $wgUser, $wgOut, $wgLang, $wgRequest;
513 global $wgLinkCache, $IP, $wgEnableParserCache;
515 $fname = 'Article::view';
516 wfProfileIn( $fname );
518 # Get variables from query string :P
519 $oldid = $wgRequest->getVal( 'oldid' );
520 $diff = $wgRequest->getVal( 'diff' );
522 $wgOut->setArticleFlag( true );
523 $wgOut->setRobotpolicy( 'index,follow' );
525 # If we got diff and oldid in the query, we want to see a
526 # diff page instead of the article.
528 if ( !is_null( $diff ) ) {
529 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
530 $de = new DifferenceEngine( intval($oldid), intval($diff) );
532 wfProfileOut( $fname );
536 if ( !is_null( $oldid ) and $this->checkTouched() ) {
537 if( $wgOut->checkLastModified( $this->mTouched
) ){
539 } else if ( $this->tryFileCache() ) {
540 # tell wgOut that output is taken care of
542 $this->viewUpdates();
547 # Should the parser cache be used?
548 if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
556 if ( $wgOut->tryParserCache( $this, $wgUser ) ) {
561 if ( !$outputDone ) {
562 $text = $this->getContent( false ); # May change mTitle by following a redirect
564 # Another whitelist check in case oldid or redirects are altering the title
565 if ( !$this->mTitle
->userCanRead() ) {
566 $wgOut->loginToUse();
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 );
585 # Can't cache redirects
589 $wgLinkCache->preFill( $this->mTitle
);
591 # wrap user css and user js in pre and don't parse
592 # XXX: use $this->mTitle->usCssJsSubpage() when php is fixed/ a workaround is found
594 $this->mTitle
->getNamespace() == Namespace::getUser() &&
595 preg_match('/\\/[\\w]+\\.(css|js)$/', $this->mTitle
->getDBkey())
597 $wgOut->addWikiText( wfMsg('clearyourcache'));
598 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent
)."\n</pre>" );
599 } else if ( $pcache ) {
600 $wgOut->addWikiText( $text, true, $this );
602 $wgOut->addWikiText( $text );
605 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
607 # Add link titles as META keywords
608 $wgOut->addMetaTags() ;
610 $this->viewUpdates();
611 wfProfileOut( $fname );
614 # Theoretically we could defer these whole insert and update
615 # functions for after display, but that's taking a big leap
616 # of faith, and we want to be able to report database
617 # errors at some point.
619 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
621 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
622 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer, $wgIsPg, $wgIsMySQL;
624 $fname = 'Article::insertNewArticle';
626 $this->mCountAdjustment
= $this->isCountable( $text );
628 $ns = $this->mTitle
->getNamespace();
629 $ttl = $this->mTitle
->getDBkey();
630 $text = $this->preSaveTransform( $text );
631 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
634 $now = wfTimestampNow();
635 $won = wfInvertTimestamp( $now );
637 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
640 $cur_id_column="cur_id,";
641 $cur_id=wfGetSQL(""," nextval('cur_cur_id_seq')");
642 $cur_id_value="{$cur_id},";
649 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
650 $sql = "INSERT INTO cur ({$cur_id_column}cur_namespace,cur_title,cur_text," .
651 'cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter,' .
652 'cur_restrictions,cur_user_text,cur_is_redirect,' .
653 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$cur_id_value}{$ns},'" . wfStrencode( $ttl ) . "', '" .
654 wfStrencode( $text ) . "', '" .
655 wfStrencode( $summary ) . "', '" .
656 $wgUser->getID() . "', '{$now}', " .
657 $isminor . ", 0, '', '" .
658 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
659 $res = wfQuery( $sql, DB_WRITE
, $fname );
661 $newid = $wgIsPg?
$cur_id:wfInsertId();
662 $this->mTitle
->resetArticleID( $newid );
664 Article
::onArticleCreate( $this->mTitle
);
665 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
668 if(!$this->mTitle
->userIsWatching()) $this->watch();
670 if ( $this->mTitle
->userIsWatching() ) {
675 # The talk page isn't in the regular link tables, so we need to update manually:
676 $talkns = $ns ^
1; # talk -> normal; normal -> talk
677 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
678 wfQuery( $sql, DB_WRITE
);
680 # standard deferred updates
681 $this->editUpdates( $text );
683 $this->showArticle( $text, wfMsg( 'newarticle' ) );
687 /* Side effects: loads last edit */
688 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ''){
689 $this->loadLastEdit();
690 $oldtext = $this->getContent( true );
691 if ($section != '') {
692 if($section=='new') {
693 if($summary) $subject="== {$summary} ==\n\n";
694 $text=$oldtext."\n\n".$subject.$text;
697 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
698 # comments to be stripped as well)
700 $parser=new Parser();
701 $parser->mOutputType
=OT_WIKI
;
702 $oldtext=$parser->strip($oldtext, $striparray, true);
704 # now that we can be sure that no pseudo-sections are in the source,
706 # Unfortunately we can't simply do a preg_replace because that might
707 # replace the wrong section, so we have to use the section counter instead
708 $secs=preg_split('/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
709 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
710 $secs[$section*2]=$text."\n\n"; // replace with edited
712 # section 0 is top (intro) section
715 # headline of old section - we need to go through this section
716 # to determine if there are any subsections that now need to
717 # be erased, as the mother section has been replaced with
718 # the text of all subsections.
719 $headline=$secs[$section*2-1];
720 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
723 # determine headline level for wikimarkup headings
724 if(strpos($hlevel,'=')!==false) {
725 $hlevel=strlen($hlevel);
728 $secs[$section*2-1]=''; // erase old headline
731 while(!empty($secs[$count*2-1]) && !$break) {
733 $subheadline=$secs[$count*2-1];
735 '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
736 $subhlevel=$matches[1];
737 if(strpos($subhlevel,'=')!==false) {
738 $subhlevel=strlen($subhlevel);
740 if($subhlevel > $hlevel) {
741 // erase old subsections
742 $secs[$count*2-1]='';
745 if($subhlevel <= $hlevel) {
753 $text=join('',$secs);
754 # reinsert the stuff that we stripped out earlier
755 $text=$parser->unstrip($text,$striparray);
756 $text=$parser->unstripNoWiki($text,$striparray);
763 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' )
765 global $wgOut, $wgUser, $wgLinkCache;
766 global $wgDBtransactions, $wgMwRedir;
767 global $wgUseSquid, $wgInternalServer;
769 $fname = 'Article::updateArticle';
771 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
772 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
773 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ')[^\\n]+)/i', $text, $m ) ) {
775 $text = $m[1] . "\n"; # Remove all content but redirect
779 $text = $this->preSaveTransform( $text );
781 # Update article, but only if changed.
783 if( $wgDBtransactions ) {
785 wfQuery( $sql, DB_WRITE
);
787 $oldtext = $this->getContent( true );
789 if ( 0 != strcmp( $text, $oldtext ) ) {
790 $this->mCountAdjustment
= $this->isCountable( $text )
791 - $this->isCountable( $oldtext );
793 $now = wfTimestampNow();
794 $won = wfInvertTimestamp( $now );
795 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
796 "',cur_comment='" . wfStrencode( $summary ) .
797 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
798 ",cur_timestamp='{$now}',cur_user_text='" .
799 wfStrencode( $wgUser->getName() ) .
800 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
801 "WHERE cur_id=" . $this->getID() .
802 " AND cur_timestamp='" . $this->getTimestamp() . "'";
803 $res = wfQuery( $sql, DB_WRITE
, $fname );
805 if( wfAffectedRows() == 0 ) {
806 /* Belated edit conflict! Run away!! */
810 # This overwrites $oldtext if revision compression is on
811 $flags = Article
::compressRevisionText( $oldtext );
813 $oldtable=$wgIsPg?
'"old"':'old';
816 $old_id_column='old_id,';
817 $old_id=wfGetSQL(""," nextval('old_old_id_seq')");
818 $old_id_value=$old_id.',';
825 $sql = "INSERT INTO $oldtable ({$old_id_column}old_namespace,old_title,old_text," .
826 'old_comment,old_user,old_user_text,old_timestamp,' .
827 'old_minor_edit,inverse_timestamp,old_flags) VALUES (' .
829 $this->mTitle
->getNamespace() . ", '" .
830 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
831 wfStrencode( $oldtext ) . "', '" .
832 wfStrencode( $this->getComment() ) . "', " .
833 $this->getUser() . ", '" .
834 wfStrencode( $this->getUserText() ) . "', '" .
835 $this->getTimestamp() . "', " . $me1 . ", '" .
836 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
837 $res = wfQuery( $sql, DB_WRITE
, $fname );
839 $oldid = $wgIsPg?
$old_id:wfInsertId( $res );
841 $bot = (int)($wgUser->isBot() ||
$forceBot);
842 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
843 $oldid, $this->getTimestamp(), $bot );
844 Article
::onArticleEdit( $this->mTitle
);
847 if( $wgDBtransactions ) {
849 wfQuery( $sql, DB_WRITE
);
853 if (!$this->mTitle
->userIsWatching()) $this->watch();
855 if ( $this->mTitle
->userIsWatching() ) {
859 # standard deferred updates
860 $this->editUpdates( $text );
865 # Purge all articles linking here
866 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
867 $titles = $this->mTitle
->getLinksTo();
868 Title
::touchArray( $titles );
870 foreach ( $titles as $title ) {
871 $urls[] = $title->getInternalURL();
878 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
879 $u = new SquidUpdate( $urls );
883 $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor );
887 # After we've either updated or inserted the article, update
888 # the link tables and redirect to the new page.
890 function showArticle( $text, $subtitle , $sectionanchor = '' )
892 global $wgOut, $wgUser, $wgLinkCache;
895 $wgLinkCache = new LinkCache();
897 # Get old version of link table to allow incremental link updates
898 $wgLinkCache->preFill( $this->mTitle
);
899 $wgLinkCache->clear();
901 # Now update the link cache by parsing the text
902 $wgOut = new OutputPage();
903 $wgOut->addWikiText( $text );
905 if( $wgMwRedir->matchStart( $text ) )
909 $wgOut->redirect( $this->mTitle
->getFullURL( $r ).$sectionanchor );
912 # Add this page to my watchlist
914 function watch( $add = true )
916 global $wgUser, $wgOut, $wgLang;
917 global $wgDeferredUpdateList;
919 if ( 0 == $wgUser->getID() ) {
920 $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
923 if ( wfReadOnly() ) {
924 $wgOut->readOnlyPage();
928 $wgUser->addWatch( $this->mTitle
);
930 $wgUser->removeWatch( $this->mTitle
);
932 $wgOut->setPagetitle( wfMsg( $add ?
'addedwatch' : 'removedwatch' ) );
933 $wgOut->setRobotpolicy( 'noindex,follow' );
935 $sk = $wgUser->getSkin() ;
936 $link = $this->mTitle
->getPrefixedText();
939 $text = wfMsg( 'addedwatchtext', $link );
941 $text = wfMsg( 'removedwatchtext', $link );
942 $wgOut->addWikiText( $text );
944 $up = new UserUpdate();
945 array_push( $wgDeferredUpdateList, $up );
947 $wgOut->returnToMain( false );
952 $this->watch( false );
955 function protect( $limit = 'sysop' )
957 global $wgUser, $wgOut, $wgRequest;
959 if ( ! $wgUser->isSysop() ) {
960 $wgOut->sysopRequired();
963 if ( wfReadOnly() ) {
964 $wgOut->readOnlyPage();
967 $id = $this->mTitle
->getArticleID();
969 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
973 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
974 $reason = $wgRequest->getText( 'wpReasonProtect' );
978 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
979 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
980 wfQuery( $sql, DB_WRITE
, 'Article::protect' );
982 $log = new LogPage( wfMsg( 'protectlogpage' ), wfMsg( 'protectlogtext' ) );
983 if ( $limit === "" ) {
984 $log->addEntry( wfMsg( 'unprotectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
986 $log->addEntry( wfMsg( 'protectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
988 $wgOut->redirect( $this->mTitle
->getFullURL() );
991 $reason = htmlspecialchars( wfMsg( 'protectreason' ) );
992 return $this->confirmProtect( '', $reason, $limit );
996 # Output protection confirmation dialog
997 function confirmProtect( $par, $reason, $limit = 'sysop' )
1001 wfDebug( "Article::confirmProtect\n" );
1003 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1004 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1009 if ( $limit === '' ) {
1010 $wgOut->setSubtitle( wfMsg( 'unprotectsub', $sub ) );
1011 $wgOut->addWikiText( wfMsg( 'confirmunprotecttext' ) );
1012 $check = htmlspecialchars( wfMsg( 'confirmunprotect' ) );
1013 $protcom = htmlspecialchars( wfMsg( 'unprotectcomment' ) );
1014 $formaction = $this->mTitle
->escapeLocalURL( 'action=unprotect' . $par );
1016 $wgOut->setSubtitle( wfMsg( 'protectsub', $sub ) );
1017 $wgOut->addWikiText( wfMsg( 'confirmprotecttext' ) );
1018 $check = htmlspecialchars( wfMsg( 'confirmprotect' ) );
1019 $protcom = htmlspecialchars( wfMsg( 'protectcomment' ) );
1020 $formaction = $this->mTitle
->escapeLocalURL( 'action=protect' . $par );
1023 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1026 <form id='protectconfirm' method='post' action=\"{$formaction}\">
1030 <label for='wpReasonProtect'>{$protcom}:</label>
1033 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1041 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1044 <label for='wpConfirmProtect'>{$check}</label>
1050 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1056 $wgOut->returnToMain( false );
1059 function unprotect()
1061 return $this->protect( '' );
1064 # UI entry point for page deletion
1067 global $wgUser, $wgOut, $wgMessageCache, $wgRequest, $wgIsPg;
1068 $fname = 'Article::delete';
1069 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1070 $reason = $wgRequest->getText( 'wpReason' );
1072 # This code desperately needs to be totally rewritten
1075 if ( ( ! $wgUser->isSysop() ) ) {
1076 $wgOut->sysopRequired();
1079 if ( wfReadOnly() ) {
1080 $wgOut->readOnlyPage();
1084 # Better double-check that it hasn't been deleted yet!
1085 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
1086 if ( ( '' == trim( $this->mTitle
->getText() ) )
1087 or ( $this->mTitle
->getArticleId() == 0 ) ) {
1088 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1093 $this->doDelete( $reason );
1097 # determine whether this page has earlier revisions
1098 # and insert a warning if it does
1099 # we select the text because it might be useful below
1100 $ns = $this->mTitle
->getNamespace();
1101 $title = $this->mTitle
->getDBkey();
1102 $etitle = wfStrencode( $title );
1103 $oldtable=$wgIsPg?
'"old"':'old';
1104 $sql = "SELECT old_text,old_flags FROM $oldtable WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
1105 $res = wfQuery( $sql, DB_READ
, $fname );
1106 if( ($old=wfFetchObject($res)) && !$confirm ) {
1107 $skin=$wgUser->getSkin();
1108 $wgOut->addHTML('<b>'.wfMsg('historywarning'));
1109 $wgOut->addHTML( $skin->historyLink() .'</b>');
1112 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
1113 $res=wfQuery($sql, DB_READ
, $fname);
1114 if( ($s=wfFetchObject($res))) {
1116 # if this is a mini-text, we can paste part of it into the deletion reason
1118 #if this is empty, an earlier revision may contain "useful" text
1120 if($s->cur_text
!="") {
1124 $text = Article
::getRevisionText( $old );
1130 $length=strlen($text);
1132 # this should not happen, since it is not possible to store an empty, new
1133 # page. Let's insert a standard text in case it does, though
1134 if($length == 0 && $reason === '') {
1135 $reason = wfMsg('exblank');
1138 if($length < 500 && $reason === '') {
1140 # comment field=255, let's grep the first 150 to have some user
1142 $text=substr($text,0,150);
1143 # let's strip out newlines and HTML tags
1144 $text=preg_replace('/\"/',"'",$text);
1145 $text=preg_replace('/\</','<',$text);
1146 $text=preg_replace('/\>/','>',$text);
1147 $text=preg_replace("/[\n\r]/",'',$text);
1149 $reason=wfMsg('excontent'). " '".$text;
1151 $reason=wfMsg('exbeforeblank') . " '".$text;
1153 if($length>150) { $reason .= '...'; } # we've only pasted part of the text
1158 return $this->confirmDelete( '', $reason );
1161 # Output deletion confirmation dialog
1162 function confirmDelete( $par, $reason )
1166 wfDebug( "Article::confirmDelete\n" );
1168 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1169 $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
1170 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1171 $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
1173 $formaction = $this->mTitle
->escapeLocalURL( 'action=delete' . $par );
1175 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1176 $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
1177 $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
1180 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1184 <label for='wpReason'>{$delcom}:</label>
1187 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1195 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1198 <label for='wpConfirm'>{$check}</label>
1204 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1210 $wgOut->returnToMain( false );
1213 # Perform a deletion and output success or failure messages
1214 function doDelete( $reason )
1216 global $wgOut, $wgUser, $wgLang;
1217 $fname = 'Article::doDelete';
1218 wfDebug( "$fname\n" );
1220 if ( $this->doDeleteArticle( $reason ) ) {
1221 $deleted = $this->mTitle
->getPrefixedText();
1223 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1224 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1226 $sk = $wgUser->getSkin();
1227 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1228 Namespace::getWikipedia() ) .
1229 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
1231 $text = wfMsg( "deletedtext", $deleted, $loglink );
1233 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
1234 $wgOut->returnToMain( false );
1236 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1240 # Back-end article deletion
1241 # Deletes the article with database consistency, writes logs, purges caches
1243 function doDeleteArticle( $reason )
1245 global $wgUser, $wgLang;
1246 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1248 $fname = 'Article::doDeleteArticle';
1249 wfDebug( $fname."\n" );
1251 $ns = $this->mTitle
->getNamespace();
1252 $t = wfStrencode( $this->mTitle
->getDBkey() );
1253 $id = $this->mTitle
->getArticleID();
1255 if ( '' == $t ||
$id == 0 ) {
1259 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1260 array_push( $wgDeferredUpdateList, $u );
1262 $linksTo = $this->mTitle
->getLinksTo();
1265 if ( $wgUseSquid ) {
1267 $this->mTitle
->getInternalURL(),
1268 $this->mTitle
->getInternalURL( 'history' )
1270 foreach ( $linksTo as $linkTo ) {
1271 $urls[] = $linkTo->getInternalURL();
1274 $u = new SquidUpdate( $urls );
1275 array_push( $wgDeferredUpdateList, $u );
1279 # Client and file cache invalidation
1280 Title
::touchArray( $linksTo );
1282 # Move article and history to the "archive" table
1283 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1284 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1285 'ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment,' .
1286 'cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur ' .
1287 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1288 wfQuery( $sql, DB_WRITE
, $fname );
1290 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1291 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1292 'ar_flags) SELECT old_namespace,old_title,old_text,old_comment,' .
1293 'old_user,old_user_text,old_timestamp,old_minor_edit,old_flags ' .
1294 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1295 wfQuery( $sql, DB_WRITE
, $fname );
1297 # Now that it's safely backed up, delete it
1299 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1301 wfQuery( $sql, DB_WRITE
, $fname );
1303 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1305 wfQuery( $sql, DB_WRITE
, $fname );
1307 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1309 wfQuery( $sql, DB_WRITE
, $fname );
1311 # Finally, clean up the link tables
1312 $t = wfStrencode( $this->mTitle
->getPrefixedDBkey() );
1314 Article
::onArticleDelete( $this->mTitle
);
1316 $sql = 'INSERT INTO brokenlinks (bl_from,bl_to) VALUES ';
1319 foreach ( $linksTo as $titleObj ) {
1320 if ( ! $first ) { $sql .= ','; }
1322 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1323 $linkID = $titleObj->getArticleID();
1324 $sql .= "({$linkID},'{$t}')";
1327 wfQuery( $sql, DB_WRITE
, $fname );
1330 $sql = "DELETE FROM links WHERE l_to={$id}";
1331 wfQuery( $sql, DB_WRITE
, $fname );
1333 $sql = "DELETE FROM links WHERE l_from={$id}";
1334 wfQuery( $sql, DB_WRITE
, $fname );
1336 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
1337 wfQuery( $sql, DB_WRITE
, $fname );
1339 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1340 wfQuery( $sql, DB_WRITE
, $fname );
1342 $sql = "DELETE FROM categorylinks WHERE cl_from={$id}";
1343 wfQuery( $sql, DB_WRITE
, $fname );
1345 $log = new LogPage( wfMsg( 'dellogpage' ), wfMsg( 'dellogpagetext' ) );
1346 $art = $this->mTitle
->getPrefixedText();
1347 $log->addEntry( wfMsg( 'deletedarticle', $art ), $reason );
1349 # Clear the cached article id so the interface doesn't act like we exist
1350 $this->mTitle
->resetArticleID( 0 );
1351 $this->mTitle
->mArticleID
= 0;
1357 global $wgUser, $wgLang, $wgOut, $wgRequest, $wgIsMySQL, $wgIsPg;
1359 if ( ! $wgUser->isSysop() ) {
1360 $wgOut->sysopRequired();
1363 if ( wfReadOnly() ) {
1364 $wgOut->readOnlyPage( $this->getContent( true ) );
1368 # Enhanced rollback, marks edits rc_bot=1
1369 $bot = $wgRequest->getBool( 'bot' );
1371 # Replace all this user's current edits with the next one down
1372 $tt = wfStrencode( $this->mTitle
->getDBKey() );
1373 $n = $this->mTitle
->getNamespace();
1375 # Get the last editor
1376 $sql = 'SELECT cur_id,cur_user,cur_user_text,cur_comment ' .
1377 "FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1378 $res = wfQuery( $sql, DB_READ
);
1379 if( ($x = wfNumRows( $res )) != 1 ) {
1381 $wgOut->addHTML( wfMsg( 'notanarticle' ) );
1384 $s = wfFetchObject( $res );
1385 $ut = wfStrencode( $s->cur_user_text
);
1386 $uid = $s->cur_user
;
1389 $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
1390 if( $from != $s->cur_user_text
) {
1391 $wgOut->setPageTitle(wfmsg('rollbackfailed'));
1392 $wgOut->addWikiText( wfMsg( 'alreadyrolled',
1393 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1394 htmlspecialchars( $from ),
1395 htmlspecialchars( $s->cur_user_text
) ) );
1396 if($s->cur_comment
!= '') {
1398 wfMsg('editcomment',
1399 htmlspecialchars( $s->cur_comment
) ) );
1404 # Get the last edit not by this guy
1406 $use_index=$wgIsMySQL?
"USE INDEX (name_title_timestamp)":"";
1407 $oldtable=$wgIsPg?
'"old"':'old';
1408 $sql = 'SELECT old_text,old_user,old_user_text,old_timestamp,old_flags ' .
1409 "FROM $oldtable {$use_index}" .
1410 "WHERE old_namespace={$n} AND old_title='{$tt}'" .
1411 "AND (old_user <> {$uid} OR old_user_text <> '{$ut}')" .
1412 'ORDER BY inverse_timestamp LIMIT 1';
1413 $res = wfQuery( $sql, DB_READ
);
1414 if( wfNumRows( $res ) != 1 ) {
1416 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
1417 $wgOut->addHTML( wfMsg( 'cantrollback' ) );
1420 $s = wfFetchObject( $res );
1423 # Mark all reverted edits as bot
1424 $sql = 'UPDATE recentchanges SET rc_bot=1 WHERE' .
1425 "rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1426 wfQuery( $sql, DB_WRITE
, $fname );
1430 $newcomment = wfMsg( 'revertpage', $s->old_user_text
, $from );
1431 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1432 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1433 $wgOut->addHTML( '<h2>' . $newcomment . "</h2>\n<hr />\n" );
1434 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1435 Article
::onArticleEdit( $this->mTitle
);
1436 $wgOut->returnToMain( false );
1440 # Do standard deferred updates after page view
1442 /* private */ function viewUpdates()
1444 global $wgDeferredUpdateList;
1445 if ( 0 != $this->getID() ) {
1446 global $wgDisableCounters;
1447 if( !$wgDisableCounters ) {
1448 Article
::incViewCount( $this->getID() );
1449 $u = new SiteStatsUpdate( 1, 0, 0 );
1450 array_push( $wgDeferredUpdateList, $u );
1452 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1453 $this->mTitle
->getDBkey() );
1454 array_push( $wgDeferredUpdateList, $u );
1458 # Do standard deferred updates after page edit.
1459 # Every 1000th edit, prune the recent changes table.
1461 /* private */ function editUpdates( $text )
1463 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1464 global $wgMessageCache;
1467 if ( 0 == mt_rand( 0, 999 ) ) {
1468 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1469 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1470 wfQuery( $sql, DB_WRITE
);
1472 $id = $this->getID();
1473 $title = $this->mTitle
->getPrefixedDBkey();
1474 $shortTitle = $this->mTitle
->getDBkey();
1476 $adj = $this->mCountAdjustment
;
1479 $u = new LinksUpdate( $id, $title );
1480 array_push( $wgDeferredUpdateList, $u );
1481 $u = new SiteStatsUpdate( 0, 1, $adj );
1482 array_push( $wgDeferredUpdateList, $u );
1483 $u = new SearchUpdate( $id, $title, $text );
1484 array_push( $wgDeferredUpdateList, $u );
1486 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1487 array_push( $wgDeferredUpdateList, $u );
1489 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1490 $wgMessageCache->replace( $shortTitle, $text );
1495 /* private */ function setOldSubtitle()
1497 global $wgLang, $wgOut;
1499 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1500 $r = wfMsg( 'revisionasof', $td );
1501 $wgOut->setSubtitle( "({$r})" );
1504 # This function is called right before saving the wikitext,
1505 # so we can do things like signatures and links-in-context.
1507 function preSaveTransform( $text )
1509 global $wgParser, $wgUser;
1510 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1513 /* Caching functions */
1515 # checkLastModified returns true if it has taken care of all
1516 # output to the client that is necessary for this request.
1517 # (that is, it has sent a cached version of the page)
1518 function tryFileCache() {
1519 static $called = false;
1521 wfDebug( " tryFileCache() -- called twice!?\n" );
1525 if($this->isFileCacheable()) {
1526 $touched = $this->mTouched
;
1527 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( 'mainpage' ) ) {
1528 # Expire the main page quicker
1529 $expire = wfUnix2Timestamp( time() - 3600 );
1530 $touched = max( $expire, $touched );
1532 $cache = new CacheManager( $this->mTitle
);
1533 if($cache->isFileCacheGood( $touched )) {
1535 wfDebug( " tryFileCache() - about to load\n" );
1536 $cache->loadFromFileCache();
1539 wfDebug( " tryFileCache() - starting buffer\n" );
1540 ob_start( array(&$cache, 'saveToFileCache' ) );
1543 wfDebug( " tryFileCache() - not cacheable\n" );
1547 function isFileCacheable() {
1548 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1549 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1551 return $wgUseFileCache
1552 and (!$wgShowIPinHeader)
1553 and ($this->getID() != 0)
1554 and ($wgUser->getId() == 0)
1555 and (!$wgUser->getNewtalk())
1556 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1557 and ($action == 'view')
1558 and (!isset($oldid))
1560 and (!isset($redirect))
1561 and (!isset($printable))
1562 and (!$this->mRedirectedFrom
);
1565 # Loads cur_touched and returns a value indicating if it should be used
1566 function checkTouched() {
1567 $id = $this->getID();
1568 $sql = 'SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id='.$id;
1569 $res = wfQuery( $sql, DB_READ
, 'Article::checkTouched' );
1570 if( $s = wfFetchObject( $res ) ) {
1571 $this->mTouched
= $s->cur_touched
;
1572 return !$s->cur_is_redirect
;
1578 # Edit an article without doing all that other stuff
1579 function quickEdit( $text, $comment = '', $minor = 0 ) {
1580 global $wgUser, $wgMwRedir, $wgIsPg;
1581 $fname = 'Article::quickEdit';
1582 wfProfileIn( $fname );
1584 $ns = $this->mTitle
->getNamespace();
1585 $dbkey = $this->mTitle
->getDBkey();
1586 $encDbKey = wfStrencode( $dbkey );
1587 $timestamp = wfTimestampNow();
1590 $oldtable=$wgIsPg?
'"old"':'old';
1591 $sql = "INSERT INTO $oldtable (old_namespace,old_title,old_text,old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp)
1592 SELECT cur_namespace,cur_title,cur_text,cur_comment,cur_user,cur_user_text,cur_timestamp,99999999999999-cur_timestamp
1593 FROM cur WHERE cur_namespace=$ns AND cur_title='$encDbKey'";
1594 wfQuery( $sql, DB_WRITE
);
1596 # Use the affected row count to determine if the article is new
1597 $numRows = wfAffectedRows();
1599 # Make an array of fields to be inserted
1601 'cur_text' => $text,
1602 'cur_timestamp' => $timestamp,
1603 'cur_user' => $wgUser->getID(),
1604 'cur_user_text' => $wgUser->getName(),
1605 'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
1606 'cur_comment' => $comment,
1607 'cur_is_redirect' => $wgMwRedir->matchStart( $text ) ?
1 : 0,
1608 'cur_minor_edit' => intval($minor),
1609 'cur_touched' => $timestamp,
1614 $fields['cur_is_new'] = 0;
1615 wfUpdateArray( 'cur', $fields, array( 'cur_namespace' => $ns, 'cur_title' => $dbkey ), $fname );
1617 # Insert new article
1618 $fields['cur_is_new'] = 1;
1619 $fields['cur_namespace'] = $ns;
1620 $fields['cur_title'] = $dbkey;
1621 $fields['cur_random'] = $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1622 wfInsertArray( 'cur', $fields, $fname );
1624 wfProfileOut( $fname );
1627 /* static */ function incViewCount( $id )
1629 $id = intval( $id );
1630 global $wgHitcounterUpdateFreq;
1632 if( $wgHitcounterUpdateFreq <= 1 ){ //
1633 wfQuery('UPDATE cur SET cur_counter = cur_counter + 1 ' .
1634 'WHERE cur_id = '.$id, DB_WRITE
);
1638 # Not important enough to warrant an error page in case of failure
1639 $oldignore = wfIgnoreSQLErrors( true );
1641 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1643 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1644 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1645 # Most of the time (or on SQL errors), skip row count check
1646 wfIgnoreSQLErrors( $oldignore );
1650 $res = wfQuery('SELECT COUNT(*) as n FROM hitcounter', DB_WRITE
);
1651 $row = wfFetchObject( $res );
1652 $rown = intval( $row->n
);
1653 if( $rown >= $wgHitcounterUpdateFreq ){
1654 wfProfileIn( 'Article::incViewCount-collect' );
1655 $old_user_abort = ignore_user_abort( true );
1657 wfQuery('LOCK TABLES hitcounter WRITE', DB_WRITE
);
1658 wfQuery('CREATE TEMPORARY TABLE acchits TYPE=HEAP '.
1659 'SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter '.
1660 'GROUP BY hc_id', DB_WRITE
);
1661 wfQuery('DELETE FROM hitcounter', DB_WRITE
);
1662 wfQuery('UNLOCK TABLES', DB_WRITE
);
1663 wfQuery('UPDATE cur,acchits SET cur_counter=cur_counter + hc_n '.
1664 'WHERE cur_id = hc_id', DB_WRITE
);
1665 wfQuery('DROP TABLE acchits', DB_WRITE
);
1667 ignore_user_abort( $old_user_abort );
1668 wfProfileOut( 'Article::incViewCount-collect' );
1670 wfIgnoreSQLErrors( $oldignore );
1673 # The onArticle*() functions are supposed to be a kind of hooks
1674 # which should be called whenever any of the specified actions
1677 # This is a good place to put code to clear caches, for instance.
1679 # This is called on page move and undelete, as well as edit
1680 /* static */ function onArticleCreate($title_obj){
1681 global $wgUseSquid, $wgDeferredUpdateList;
1683 $titles = $title_obj->getBrokenLinksTo();
1686 if ( $wgUseSquid ) {
1687 $urls = $title_obj->getSquidURLs();
1688 foreach ( $titles as $linkTitle ) {
1689 $urls[] = $linkTitle->getInternalURL();
1691 $u = new SquidUpdate( $urls );
1692 array_push( $wgDeferredUpdateList, $u );
1695 # Clear persistent link cache
1696 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1699 /* static */ function onArticleDelete($title_obj){
1700 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1703 /* static */ function onArticleEdit($title_obj){
1704 LinkCache
::linksccClearPage( $title_obj->getArticleID() );