1e47445a6a3a1729364bf442632f2b4266ec8f25
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;
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 $sql = 'SELECT old_namespace,old_title,old_text,old_timestamp,old_user,old_user_text,old_comment,old_flags FROM old ' .
271 "WHERE old_id={$oldid}";
272 $res = wfQuery( $sql, DB_READ
, $fname );
273 if ( 0 == wfNumRows( $res ) ) {
277 $s = wfFetchObject( $res );
278 if( $this->mTitle
->getNamespace() != $s->old_namespace ||
279 $this->mTitle
->getDBkey() != $s->old_title
) {
280 $oldTitle = Title
::makeTitle( $s->old_namesapce
, $s->old_title
);
281 $this->mTitle
= $oldTitle;
282 $wgTitle = $oldTitle;
284 $this->mContent
= Article
::getRevisionText( $s );
285 $this->mUser
= $s->old_user
;
286 $this->mUserText
= $s->old_user_text
;
287 $this->mComment
= $s->old_comment
;
289 $this->mTimestamp
= $s->old_timestamp
;
290 wfFreeResult( $res );
292 $this->mContentLoaded
= true;
293 return $this->mContent
;
296 # Gets the article text without using so many damn globals
297 # Returns false on error
298 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
301 if ( $this->mContentLoaded
) {
302 return $this->mContent
;
304 $this->mContent
= false;
306 $fname = 'Article::loadContent';
308 if ( ! $oldid ) { # Retrieve current version
309 $id = $this->getID();
315 'cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched ' .
316 "FROM cur WHERE cur_id={$id}";
317 $res = wfQuery( $sql, DB_READ
, $fname );
318 if ( 0 == wfNumRows( $res ) ) {
322 $s = wfFetchObject( $res );
323 # If we got a redirect, follow it (unless we've been told
324 # not to by either the function parameter or the query
325 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text
) ) {
326 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
327 $s->cur_text
, $m ) ) {
328 $rt = Title
::newFromText( $m[1] );
329 if( $rt && $rt->getInterwiki() == '' && $rt->getNamespace() != Namespace::getSpecial() ) {
330 $rid = $rt->getArticleID();
332 $sql = 'SELECT cur_text,cur_timestamp,cur_user,' .
333 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
334 $res = wfQuery( $sql, DB_READ
, $fname );
336 if ( 0 != wfNumRows( $res ) ) {
337 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
339 $s = wfFetchObject( $res );
346 $this->mContent
= $s->cur_text
;
347 $this->mUser
= $s->cur_user
;
348 $this->mCounter
= $s->cur_counter
;
349 $this->mTimestamp
= $s->cur_timestamp
;
350 $this->mTouched
= $s->cur_touched
;
351 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
352 $this->mTitle
->mRestrictionsLoaded
= true;
353 wfFreeResult( $res );
354 } else { # oldid set, retrieve historical version
355 $sql = 'SELECT old_text,old_timestamp,old_user,old_flags FROM old ' .
356 "WHERE old_id={$oldid}";
357 $res = wfQuery( $sql, DB_READ
, $fname );
358 if ( 0 == wfNumRows( $res ) ) {
362 $s = wfFetchObject( $res );
363 $this->mContent
= Article
::getRevisionText( $s );
364 $this->mUser
= $s->old_user
;
366 $this->mTimestamp
= $s->old_timestamp
;
367 wfFreeResult( $res );
369 $this->mContentLoaded
= true;
370 return $this->mContent
;
374 if( $this->mTitle
) {
375 return $this->mTitle
->getArticleID();
383 if ( -1 == $this->mCounter
) {
384 $id = $this->getID();
385 $this->mCounter
= wfGetSQL( 'cur', 'cur_counter', "cur_id={$id}" );
387 return $this->mCounter
;
390 # Would the given text make this article a "good" article (i.e.,
391 # suitable for including in the article count)?
393 function isCountable( $text )
395 global $wgUseCommaCount, $wgMwRedir;
397 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
398 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
399 $token = ($wgUseCommaCount ?
',' : '[[' );
400 if ( false === strstr( $text, $token ) ) { return 0; }
404 # Loads everything from cur except cur_text
405 # This isn't necessary for all uses, so it's only done if needed.
407 /* private */ function loadLastEdit()
410 if ( -1 != $this->mUser
) return;
412 $sql = 'SELECT cur_user,cur_user_text,cur_timestamp,' .
413 'cur_comment,cur_minor_edit FROM cur WHERE ' .
414 'cur_id=' . $this->getID();
415 $res = wfQuery( $sql, DB_READ
, 'Article::loadLastEdit' );
417 if ( wfNumRows( $res ) > 0 ) {
418 $s = wfFetchObject( $res );
419 $this->mUser
= $s->cur_user
;
420 $this->mUserText
= $s->cur_user_text
;
421 $this->mTimestamp
= $s->cur_timestamp
;
422 $this->mComment
= $s->cur_comment
;
423 $this->mMinorEdit
= $s->cur_minor_edit
;
427 function getTimestamp()
429 $this->loadLastEdit();
430 return $this->mTimestamp
;
435 $this->loadLastEdit();
439 function getUserText()
441 $this->loadLastEdit();
442 return $this->mUserText
;
445 function getComment()
447 $this->loadLastEdit();
448 return $this->mComment
;
451 function getMinorEdit()
453 $this->loadLastEdit();
454 return $this->mMinorEdit
;
457 function getContributors($limit = 0, $offset = 0)
459 $fname = 'Article::getContributors';
461 # XXX: this is expensive; cache this info somewhere.
463 $title = $this->mTitle
;
467 $sql = 'SELECT old.old_user, old.old_user_text, ' .
468 ' user.user_real_name, MAX(old.old_timestamp) as timestamp' .
470 ' WHERE old.old_user = user.user_id ' .
471 ' AND old.old_namespace = ' . $title->getNamespace() .
472 ' AND old.old_title = "' . $title->getDBkey() . '"' .
473 ' AND old.old_user != 0 ' .
474 ' AND old.old_user != ' . $this->getUser() .
475 ' GROUP BY old.old_user ' .
476 ' ORDER BY timestamp DESC ';
479 $sql .= ' LIMIT '.$limit;
482 $res = wfQuery($sql, DB_READ
, $fname);
484 while ( $line = wfFetchObject( $res ) ) {
485 $contribs[$line->old_user
] =
486 array($line->old_user_text
, $line->user_real_name
);
489 # Count anonymous users
491 $res = wfQuery('SELECT COUNT(*) AS cnt ' .
493 ' WHERE old_namespace = ' . $title->getNamespace() .
494 " AND old_title = '" . $title->getDBkey() . "'" .
495 ' AND old_user = 0 ', DB_READ
, $fname);
497 while ( $line = wfFetchObject( $res ) ) {
498 $contribs[0] = array($line->cnt
, 'Anonymous');
504 # This is the default action of the script: just view the page of
509 global $wgUser, $wgOut, $wgLang, $wgRequest;
510 global $wgLinkCache, $IP, $wgEnableParserCache;
512 $fname = 'Article::view';
513 wfProfileIn( $fname );
515 # Get variables from query string :P
516 $oldid = $wgRequest->getVal( 'oldid' );
517 $diff = $wgRequest->getVal( 'diff' );
519 $wgOut->setArticleFlag( true );
520 $wgOut->setRobotpolicy( 'index,follow' );
522 # If we got diff and oldid in the query, we want to see a
523 # diff page instead of the article.
525 if ( !is_null( $diff ) ) {
526 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
527 $de = new DifferenceEngine( intval($oldid), intval($diff) );
529 wfProfileOut( $fname );
533 if ( !is_null( $oldid ) and $this->checkTouched() ) {
534 if( $wgOut->checkLastModified( $this->mTouched
) ){
536 } else if ( $this->tryFileCache() ) {
537 # tell wgOut that output is taken care of
539 $this->viewUpdates();
544 # Should the parser cache be used?
545 if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
553 if ( $wgOut->tryParserCache( $this, $wgUser ) ) {
558 if ( !$outputDone ) {
559 $text = $this->getContent( false ); # May change mTitle by following a redirect
561 # Another whitelist check in case oldid or redirects are altering the title
562 if ( !$this->mTitle
->userCanRead() ) {
563 $wgOut->loginToUse();
569 # We're looking at an old revision
571 if ( !empty( $oldid ) ) {
572 $this->setOldSubtitle();
573 $wgOut->setRobotpolicy( 'noindex,follow' );
575 if ( '' != $this->mRedirectedFrom
) {
576 $sk = $wgUser->getSkin();
577 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, '',
579 $s = wfMsg( 'redirectedfrom', $redir );
580 $wgOut->setSubtitle( $s );
582 # Can't cache redirects
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('clearyourcache'));
595 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent
)."\n</pre>" );
596 } else if ( $pcache ) {
597 $wgOut->addWikiText( $text, true, $this );
599 $wgOut->addWikiText( $text );
602 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
604 # Add link titles as META keywords
605 $wgOut->addMetaTags() ;
607 $this->viewUpdates();
608 wfProfileOut( $fname );
611 # Theoretically we could defer these whole insert and update
612 # functions for after display, but that's taking a big leap
613 # of faith, and we want to be able to report database
614 # errors at some point.
616 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
618 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
619 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
621 $fname = 'Article::insertNewArticle';
623 $this->mCountAdjustment
= $this->isCountable( $text );
625 $ns = $this->mTitle
->getNamespace();
626 $ttl = $this->mTitle
->getDBkey();
627 $text = $this->preSaveTransform( $text );
628 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
631 $now = wfTimestampNow();
632 $won = wfInvertTimestamp( $now );
634 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
635 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
636 $sql = 'INSERT INTO cur (cur_namespace,cur_title,cur_text,' .
637 'cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter,' .
638 'cur_restrictions,cur_user_text,cur_is_redirect,' .
639 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
640 wfStrencode( $text ) . "', '" .
641 wfStrencode( $summary ) . "', '" .
642 $wgUser->getID() . "', '{$now}', " .
643 $isminor . ", 0, '', '" .
644 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
645 $res = wfQuery( $sql, DB_WRITE
, $fname );
647 $newid = wfInsertId();
648 $this->mTitle
->resetArticleID( $newid );
650 Article
::onArticleCreate( $this->mTitle
);
651 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
654 if(!$this->mTitle
->userIsWatching()) $this->watch();
656 if ( $this->mTitle
->userIsWatching() ) {
661 # The talk page isn't in the regular link tables, so we need to update manually:
662 $talkns = $ns ^
1; # talk -> normal; normal -> talk
663 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
664 wfQuery( $sql, DB_WRITE
);
666 # standard deferred updates
667 $this->editUpdates( $text );
669 $this->showArticle( $text, wfMsg( 'newarticle' ) );
673 /* Side effects: loads last edit */
674 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ''){
675 $this->loadLastEdit();
676 $oldtext = $this->getContent( true );
677 if ($section != '') {
678 if($section=='new') {
679 if($summary) $subject="== {$summary} ==\n\n";
680 $text=$oldtext."\n\n".$subject.$text;
683 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
684 # comments to be stripped as well)
686 $parser=new Parser();
687 $parser->mOutputType
=OT_WIKI
;
688 $oldtext=$parser->strip($oldtext, $striparray, true);
690 # now that we can be sure that no pseudo-sections are in the source,
692 # Unfortunately we can't simply do a preg_replace because that might
693 # replace the wrong section, so we have to use the section counter instead
694 $secs=preg_split('/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
695 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
696 $secs[$section*2]=$text."\n\n"; // replace with edited
698 # section 0 is top (intro) section
701 # headline of old section - we need to go through this section
702 # to determine if there are any subsections that now need to
703 # be erased, as the mother section has been replaced with
704 # the text of all subsections.
705 $headline=$secs[$section*2-1];
706 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
709 # determine headline level for wikimarkup headings
710 if(strpos($hlevel,'=')!==false) {
711 $hlevel=strlen($hlevel);
714 $secs[$section*2-1]=''; // erase old headline
717 while(!empty($secs[$count*2-1]) && !$break) {
719 $subheadline=$secs[$count*2-1];
721 '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
722 $subhlevel=$matches[1];
723 if(strpos($subhlevel,'=')!==false) {
724 $subhlevel=strlen($subhlevel);
726 if($subhlevel > $hlevel) {
727 // erase old subsections
728 $secs[$count*2-1]='';
731 if($subhlevel <= $hlevel) {
739 $text=join('',$secs);
740 # reinsert the stuff that we stripped out earlier
741 $text=$parser->unstrip($text,$striparray);
742 $text=$parser->unstripNoWiki($text,$striparray);
749 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' )
751 global $wgOut, $wgUser, $wgLinkCache;
752 global $wgDBtransactions, $wgMwRedir;
753 global $wgUseSquid, $wgInternalServer;
754 $fname = 'Article::updateArticle';
756 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
757 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
758 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ')[^\\n]+)/i', $text, $m ) ) {
760 $text = $m[1] . "\n"; # Remove all content but redirect
764 $text = $this->preSaveTransform( $text );
766 # Update article, but only if changed.
768 if( $wgDBtransactions ) {
770 wfQuery( $sql, DB_WRITE
);
772 $oldtext = $this->getContent( true );
774 if ( 0 != strcmp( $text, $oldtext ) ) {
775 $this->mCountAdjustment
= $this->isCountable( $text )
776 - $this->isCountable( $oldtext );
778 $now = wfTimestampNow();
779 $won = wfInvertTimestamp( $now );
780 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
781 "',cur_comment='" . wfStrencode( $summary ) .
782 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
783 ",cur_timestamp='{$now}',cur_user_text='" .
784 wfStrencode( $wgUser->getName() ) .
785 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
786 "WHERE cur_id=" . $this->getID() .
787 " AND cur_timestamp='" . $this->getTimestamp() . "'";
788 $res = wfQuery( $sql, DB_WRITE
, $fname );
790 if( wfAffectedRows() == 0 ) {
791 /* Belated edit conflict! Run away!! */
795 # This overwrites $oldtext if revision compression is on
796 $flags = Article
::compressRevisionText( $oldtext );
798 $sql = 'INSERT INTO old (old_namespace,old_title,old_text,' .
799 'old_comment,old_user,old_user_text,old_timestamp,' .
800 'old_minor_edit,inverse_timestamp,old_flags) VALUES (' .
801 $this->mTitle
->getNamespace() . ", '" .
802 wfStrencode( $this->mTitle
->getDBkey() ) . "', '" .
803 wfStrencode( $oldtext ) . "', '" .
804 wfStrencode( $this->getComment() ) . "', " .
805 $this->getUser() . ", '" .
806 wfStrencode( $this->getUserText() ) . "', '" .
807 $this->getTimestamp() . "', " . $me1 . ", '" .
808 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
809 $res = wfQuery( $sql, DB_WRITE
, $fname );
810 $oldid = wfInsertID( $res );
812 $bot = (int)($wgUser->isBot() ||
$forceBot);
813 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
814 $oldid, $this->getTimestamp(), $bot );
815 Article
::onArticleEdit( $this->mTitle
);
818 if( $wgDBtransactions ) {
820 wfQuery( $sql, DB_WRITE
);
824 if (!$this->mTitle
->userIsWatching()) $this->watch();
826 if ( $this->mTitle
->userIsWatching() ) {
830 # standard deferred updates
831 $this->editUpdates( $text );
836 # Purge all articles linking here
837 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
838 $titles = $this->mTitle
->getLinksTo();
839 Title
::touchArray( $titles );
841 foreach ( $titles as $title ) {
842 $urls[] = $title->getInternalURL();
849 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
850 $u = new SquidUpdate( $urls );
854 $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor );
858 # After we've either updated or inserted the article, update
859 # the link tables and redirect to the new page.
861 function showArticle( $text, $subtitle , $sectionanchor = '' )
863 global $wgOut, $wgUser, $wgLinkCache;
866 $wgLinkCache = new LinkCache();
868 # Get old version of link table to allow incremental link updates
869 $wgLinkCache->preFill( $this->mTitle
);
870 $wgLinkCache->clear();
872 # Now update the link cache by parsing the text
873 $wgOut = new OutputPage();
874 $wgOut->addWikiText( $text );
876 if( $wgMwRedir->matchStart( $text ) )
880 $wgOut->redirect( $this->mTitle
->getFullURL( $r ).$sectionanchor );
883 # Add this page to my watchlist
885 function watch( $add = true )
887 global $wgUser, $wgOut, $wgLang;
888 global $wgDeferredUpdateList;
890 if ( 0 == $wgUser->getID() ) {
891 $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
894 if ( wfReadOnly() ) {
895 $wgOut->readOnlyPage();
899 $wgUser->addWatch( $this->mTitle
);
901 $wgUser->removeWatch( $this->mTitle
);
903 $wgOut->setPagetitle( wfMsg( $add ?
'addedwatch' : 'removedwatch' ) );
904 $wgOut->setRobotpolicy( 'noindex,follow' );
906 $sk = $wgUser->getSkin() ;
907 $link = $this->mTitle
->getPrefixedText();
910 $text = wfMsg( 'addedwatchtext', $link );
912 $text = wfMsg( 'removedwatchtext', $link );
913 $wgOut->addWikiText( $text );
915 $up = new UserUpdate();
916 array_push( $wgDeferredUpdateList, $up );
918 $wgOut->returnToMain( false );
923 $this->watch( false );
926 function protect( $limit = 'sysop' )
928 global $wgUser, $wgOut, $wgRequest;
930 if ( ! $wgUser->isSysop() ) {
931 $wgOut->sysopRequired();
934 if ( wfReadOnly() ) {
935 $wgOut->readOnlyPage();
938 $id = $this->mTitle
->getArticleID();
940 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
944 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
945 $reason = $wgRequest->getText( 'wpReasonProtect' );
949 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
950 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
951 wfQuery( $sql, DB_WRITE
, 'Article::protect' );
953 $log = new LogPage( wfMsg( 'protectlogpage' ), wfMsg( 'protectlogtext' ) );
954 if ( $limit === "" ) {
955 $log->addEntry( wfMsg( 'unprotectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
957 $log->addEntry( wfMsg( 'protectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
959 $wgOut->redirect( $this->mTitle
->getFullURL() );
962 $reason = htmlspecialchars( wfMsg( 'protectreason' ) );
963 return $this->confirmProtect( '', $reason, $limit );
967 # Output protection confirmation dialog
968 function confirmProtect( $par, $reason, $limit = 'sysop' )
972 wfDebug( "Article::confirmProtect\n" );
974 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
975 $wgOut->setRobotpolicy( 'noindex,nofollow' );
980 if ( $limit === '' ) {
981 $wgOut->setSubtitle( wfMsg( 'unprotectsub', $sub ) );
982 $wgOut->addWikiText( wfMsg( 'confirmunprotecttext' ) );
983 $check = htmlspecialchars( wfMsg( 'confirmunprotect' ) );
984 $protcom = htmlspecialchars( wfMsg( 'unprotectcomment' ) );
985 $formaction = $this->mTitle
->escapeLocalURL( 'action=unprotect' . $par );
987 $wgOut->setSubtitle( wfMsg( 'protectsub', $sub ) );
988 $wgOut->addWikiText( wfMsg( 'confirmprotecttext' ) );
989 $check = htmlspecialchars( wfMsg( 'confirmprotect' ) );
990 $protcom = htmlspecialchars( wfMsg( 'protectcomment' ) );
991 $formaction = $this->mTitle
->escapeLocalURL( 'action=protect' . $par );
994 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
997 <form id='protectconfirm' method='post' action=\"{$formaction}\">
1001 <label for='wpReasonProtect'>{$protcom}:</label>
1004 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1012 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1015 <label for='wpConfirmProtect'>{$check}</label>
1021 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1027 $wgOut->returnToMain( false );
1030 function unprotect()
1032 return $this->protect( '' );
1035 # UI entry point for page deletion
1038 global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
1039 $fname = 'Article::delete';
1040 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1041 $reason = $wgRequest->getText( 'wpReason' );
1043 # This code desperately needs to be totally rewritten
1046 if ( ( ! $wgUser->isSysop() ) ) {
1047 $wgOut->sysopRequired();
1050 if ( wfReadOnly() ) {
1051 $wgOut->readOnlyPage();
1055 # Better double-check that it hasn't been deleted yet!
1056 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
1057 if ( ( '' == trim( $this->mTitle
->getText() ) )
1058 or ( $this->mTitle
->getArticleId() == 0 ) ) {
1059 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1064 $this->doDelete( $reason );
1068 # determine whether this page has earlier revisions
1069 # and insert a warning if it does
1070 # we select the text because it might be useful below
1071 $ns = $this->mTitle
->getNamespace();
1072 $title = $this->mTitle
->getDBkey();
1073 $etitle = wfStrencode( $title );
1074 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
1075 $res = wfQuery( $sql, DB_READ
, $fname );
1076 if( ($old=wfFetchObject($res)) && !$confirm ) {
1077 $skin=$wgUser->getSkin();
1078 $wgOut->addHTML('<b>'.wfMsg('historywarning'));
1079 $wgOut->addHTML( $skin->historyLink() .'</b>');
1082 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
1083 $res=wfQuery($sql, DB_READ
, $fname);
1084 if( ($s=wfFetchObject($res))) {
1086 # if this is a mini-text, we can paste part of it into the deletion reason
1088 #if this is empty, an earlier revision may contain "useful" text
1090 if($s->cur_text
!="") {
1094 $text = Article
::getRevisionText( $old );
1100 $length=strlen($text);
1102 # this should not happen, since it is not possible to store an empty, new
1103 # page. Let's insert a standard text in case it does, though
1104 if($length == 0 && $reason === '') {
1105 $reason = wfMsg('exblank');
1108 if($length < 500 && $reason === '') {
1110 # comment field=255, let's grep the first 150 to have some user
1112 $text=substr($text,0,150);
1113 # let's strip out newlines and HTML tags
1114 $text=preg_replace('/\"/',"'",$text);
1115 $text=preg_replace('/\</','<',$text);
1116 $text=preg_replace('/\>/','>',$text);
1117 $text=preg_replace("/[\n\r]/",'',$text);
1119 $reason=wfMsg('excontent'). " '".$text;
1121 $reason=wfMsg('exbeforeblank') . " '".$text;
1123 if($length>150) { $reason .= '...'; } # we've only pasted part of the text
1128 return $this->confirmDelete( '', $reason );
1131 # Output deletion confirmation dialog
1132 function confirmDelete( $par, $reason )
1136 wfDebug( "Article::confirmDelete\n" );
1138 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1139 $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
1140 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1141 $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
1143 $formaction = $this->mTitle
->escapeLocalURL( 'action=delete' . $par );
1145 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1146 $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
1147 $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
1150 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1154 <label for='wpReason'>{$delcom}:</label>
1157 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1165 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1168 <label for='wpConfirm'>{$check}</label>
1174 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1180 $wgOut->returnToMain( false );
1183 # Perform a deletion and output success or failure messages
1184 function doDelete( $reason )
1186 global $wgOut, $wgUser, $wgLang;
1187 $fname = 'Article::doDelete';
1188 wfDebug( "$fname\n" );
1190 if ( $this->doDeleteArticle( $reason ) ) {
1191 $deleted = $this->mTitle
->getPrefixedText();
1193 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1194 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1196 $sk = $wgUser->getSkin();
1197 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1198 Namespace::getWikipedia() ) .
1199 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
1201 $text = wfMsg( "deletedtext", $deleted, $loglink );
1203 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
1204 $wgOut->returnToMain( false );
1206 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1210 # Back-end article deletion
1211 # Deletes the article with database consistency, writes logs, purges caches
1213 function doDeleteArticle( $reason )
1215 global $wgUser, $wgLang;
1216 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1218 $fname = 'Article::doDeleteArticle';
1219 wfDebug( $fname."\n" );
1221 $ns = $this->mTitle
->getNamespace();
1222 $t = wfStrencode( $this->mTitle
->getDBkey() );
1223 $id = $this->mTitle
->getArticleID();
1225 if ( '' == $t ||
$id == 0 ) {
1229 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1230 array_push( $wgDeferredUpdateList, $u );
1232 $linksTo = $this->mTitle
->getLinksTo();
1235 if ( $wgUseSquid ) {
1237 $this->mTitle
->getInternalURL(),
1238 $this->mTitle
->getInternalURL( 'history' )
1240 foreach ( $linksTo as $linkTo ) {
1241 $urls[] = $linkTo->getInternalURL();
1244 $u = new SquidUpdate( $urls );
1245 array_push( $wgDeferredUpdateList, $u );
1249 # Client and file cache invalidation
1250 Title
::touchArray( $linksTo );
1252 # Move article and history to the "archive" table
1253 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1254 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1255 'ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment,' .
1256 'cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur ' .
1257 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1258 wfQuery( $sql, DB_WRITE
, $fname );
1260 $sql = 'INSERT INTO archive (ar_namespace,ar_title,ar_text,' .
1261 'ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,' .
1262 'ar_flags) SELECT old_namespace,old_title,old_text,old_comment,' .
1263 'old_user,old_user_text,old_timestamp,old_minor_edit,old_flags ' .
1264 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1265 wfQuery( $sql, DB_WRITE
, $fname );
1267 # Now that it's safely backed up, delete it
1269 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1271 wfQuery( $sql, DB_WRITE
, $fname );
1273 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1275 wfQuery( $sql, DB_WRITE
, $fname );
1277 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1279 wfQuery( $sql, DB_WRITE
, $fname );
1281 # Finally, clean up the link tables
1282 $t = wfStrencode( $this->mTitle
->getPrefixedDBkey() );
1284 Article
::onArticleDelete( $this->mTitle
);
1286 $sql = 'INSERT INTO brokenlinks (bl_from,bl_to) VALUES ';
1289 foreach ( $linksTo as $titleObj ) {
1290 if ( ! $first ) { $sql .= ','; }
1292 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1293 $linkID = $titleObj->getArticleID();
1294 $sql .= "({$linkID},'{$t}')";
1297 wfQuery( $sql, DB_WRITE
, $fname );
1300 $sql = "DELETE FROM links WHERE l_to={$id}";
1301 wfQuery( $sql, DB_WRITE
, $fname );
1303 $sql = "DELETE FROM links WHERE l_from={$id}";
1304 wfQuery( $sql, DB_WRITE
, $fname );
1306 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
1307 wfQuery( $sql, DB_WRITE
, $fname );
1309 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1310 wfQuery( $sql, DB_WRITE
, $fname );
1312 $sql = "DELETE FROM categorylinks WHERE cl_from={$id}";
1313 wfQuery( $sql, DB_WRITE
, $fname );
1315 $log = new LogPage( wfMsg( 'dellogpage' ), wfMsg( 'dellogpagetext' ) );
1316 $art = $this->mTitle
->getPrefixedText();
1317 $log->addEntry( wfMsg( 'deletedarticle', $art ), $reason );
1319 # Clear the cached article id so the interface doesn't act like we exist
1320 $this->mTitle
->resetArticleID( 0 );
1321 $this->mTitle
->mArticleID
= 0;
1327 global $wgUser, $wgLang, $wgOut, $wgRequest, $wgIsMySQL;
1329 if ( ! $wgUser->isSysop() ) {
1330 $wgOut->sysopRequired();
1333 if ( wfReadOnly() ) {
1334 $wgOut->readOnlyPage( $this->getContent( true ) );
1338 # Enhanced rollback, marks edits rc_bot=1
1339 $bot = $wgRequest->getBool( 'bot' );
1341 # Replace all this user's current edits with the next one down
1342 $tt = wfStrencode( $this->mTitle
->getDBKey() );
1343 $n = $this->mTitle
->getNamespace();
1345 # Get the last editor
1346 $sql = 'SELECT cur_id,cur_user,cur_user_text,cur_comment ' .
1347 "FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1348 $res = wfQuery( $sql, DB_READ
);
1349 if( ($x = wfNumRows( $res )) != 1 ) {
1351 $wgOut->addHTML( wfMsg( 'notanarticle' ) );
1354 $s = wfFetchObject( $res );
1355 $ut = wfStrencode( $s->cur_user_text
);
1356 $uid = $s->cur_user
;
1359 $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
1360 if( $from != $s->cur_user_text
) {
1361 $wgOut->setPageTitle(wfmsg('rollbackfailed'));
1362 $wgOut->addWikiText( wfMsg( 'alreadyrolled',
1363 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1364 htmlspecialchars( $from ),
1365 htmlspecialchars( $s->cur_user_text
) ) );
1366 if($s->cur_comment
!= '') {
1368 wfMsg('editcomment',
1369 htmlspecialchars( $s->cur_comment
) ) );
1374 # Get the last edit not by this guy
1376 $use_index=$wgIsMySQL?
"USE INDEX (name_title_timestamp)":"";
1377 $sql = 'SELECT old_text,old_user,old_user_text,old_timestamp,old_flags ' .
1378 'FROM old {$use_index}' .
1379 "WHERE old_namespace={$n} AND old_title='{$tt}'" .
1380 "AND (old_user <> {$uid} OR old_user_text <> '{$ut}')" .
1381 'ORDER BY inverse_timestamp LIMIT 1';
1382 $res = wfQuery( $sql, DB_READ
);
1383 if( wfNumRows( $res ) != 1 ) {
1385 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
1386 $wgOut->addHTML( wfMsg( 'cantrollback' ) );
1389 $s = wfFetchObject( $res );
1392 # Mark all reverted edits as bot
1393 $sql = 'UPDATE recentchanges SET rc_bot=1 WHERE' .
1394 "rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1395 wfQuery( $sql, DB_WRITE
, $fname );
1399 $newcomment = wfMsg( 'revertpage', $s->old_user_text
, $from );
1400 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1401 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1402 $wgOut->addHTML( '<h2>' . $newcomment . "</h2>\n<hr />\n" );
1403 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1404 Article
::onArticleEdit( $this->mTitle
);
1405 $wgOut->returnToMain( false );
1409 # Do standard deferred updates after page view
1411 /* private */ function viewUpdates()
1413 global $wgDeferredUpdateList;
1414 if ( 0 != $this->getID() ) {
1415 global $wgDisableCounters;
1416 if( !$wgDisableCounters ) {
1417 Article
::incViewCount( $this->getID() );
1418 $u = new SiteStatsUpdate( 1, 0, 0 );
1419 array_push( $wgDeferredUpdateList, $u );
1421 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1422 $this->mTitle
->getDBkey() );
1423 array_push( $wgDeferredUpdateList, $u );
1427 # Do standard deferred updates after page edit.
1428 # Every 1000th edit, prune the recent changes table.
1430 /* private */ function editUpdates( $text )
1432 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1433 global $wgMessageCache;
1436 if ( 0 == mt_rand( 0, 999 ) ) {
1437 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1438 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1439 wfQuery( $sql, DB_WRITE
);
1441 $id = $this->getID();
1442 $title = $this->mTitle
->getPrefixedDBkey();
1443 $shortTitle = $this->mTitle
->getDBkey();
1445 $adj = $this->mCountAdjustment
;
1448 $u = new LinksUpdate( $id, $title );
1449 array_push( $wgDeferredUpdateList, $u );
1450 $u = new SiteStatsUpdate( 0, 1, $adj );
1451 array_push( $wgDeferredUpdateList, $u );
1452 $u = new SearchUpdate( $id, $title, $text );
1453 array_push( $wgDeferredUpdateList, $u );
1455 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1456 array_push( $wgDeferredUpdateList, $u );
1458 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1459 $wgMessageCache->replace( $shortTitle, $text );
1464 /* private */ function setOldSubtitle()
1466 global $wgLang, $wgOut;
1468 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1469 $r = wfMsg( 'revisionasof', $td );
1470 $wgOut->setSubtitle( "({$r})" );
1473 # This function is called right before saving the wikitext,
1474 # so we can do things like signatures and links-in-context.
1476 function preSaveTransform( $text )
1478 global $wgParser, $wgUser;
1479 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1482 /* Caching functions */
1484 # checkLastModified returns true if it has taken care of all
1485 # output to the client that is necessary for this request.
1486 # (that is, it has sent a cached version of the page)
1487 function tryFileCache() {
1488 static $called = false;
1490 wfDebug( " tryFileCache() -- called twice!?\n" );
1494 if($this->isFileCacheable()) {
1495 $touched = $this->mTouched
;
1496 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( 'mainpage' ) ) {
1497 # Expire the main page quicker
1498 $expire = wfUnix2Timestamp( time() - 3600 );
1499 $touched = max( $expire, $touched );
1501 $cache = new CacheManager( $this->mTitle
);
1502 if($cache->isFileCacheGood( $touched )) {
1504 wfDebug( " tryFileCache() - about to load\n" );
1505 $cache->loadFromFileCache();
1508 wfDebug( " tryFileCache() - starting buffer\n" );
1509 ob_start( array(&$cache, 'saveToFileCache' ) );
1512 wfDebug( " tryFileCache() - not cacheable\n" );
1516 function isFileCacheable() {
1517 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1518 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1520 return $wgUseFileCache
1521 and (!$wgShowIPinHeader)
1522 and ($this->getID() != 0)
1523 and ($wgUser->getId() == 0)
1524 and (!$wgUser->getNewtalk())
1525 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1526 and ($action == 'view')
1527 and (!isset($oldid))
1529 and (!isset($redirect))
1530 and (!isset($printable))
1531 and (!$this->mRedirectedFrom
);
1534 # Loads cur_touched and returns a value indicating if it should be used
1535 function checkTouched() {
1536 $id = $this->getID();
1537 $sql = 'SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id='.$id;
1538 $res = wfQuery( $sql, DB_READ
, 'Article::checkTouched' );
1539 if( $s = wfFetchObject( $res ) ) {
1540 $this->mTouched
= $s->cur_touched
;
1541 return !$s->cur_is_redirect
;
1547 # Edit an article without doing all that other stuff
1548 function quickEdit( $text, $comment = '', $minor = 0 ) {
1549 global $wgUser, $wgMwRedir;
1550 $fname = 'Article::quickEdit';
1551 wfProfileIn( $fname );
1553 $ns = $this->mTitle
->getNamespace();
1554 $dbkey = $this->mTitle
->getDBkey();
1555 $encDbKey = wfStrencode( $dbkey );
1556 $timestamp = wfTimestampNow();
1559 $sql = 'INSERT INTO old (old_namespace,old_title,old_text,old_comment,old_user,old_user_text,old_timestamp,inverse_timestamp)' .
1560 'SELECT cur_namespace,cur_title,cur_text,cur_comment,cur_user,cur_user_text,cur_timestamp,99999999999999-cur_timestamp' .
1561 "FROM cur WHERE cur_namespace=$ns AND cur_title='$encDbKey'";
1562 wfQuery( $sql, DB_WRITE
);
1564 # Use the affected row count to determine if the article is new
1565 $numRows = wfAffectedRows();
1567 # Make an array of fields to be inserted
1569 'cur_text' => $text,
1570 'cur_timestamp' => $timestamp,
1571 'cur_user' => $wgUser->getID(),
1572 'cur_user_text' => $wgUser->getName(),
1573 'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
1574 'cur_comment' => $comment,
1575 'cur_is_redirect' => $wgMwRedir->matchStart( $text ) ?
1 : 0,
1576 'cur_minor_edit' => intval($minor),
1577 'cur_touched' => $timestamp,
1582 $fields['cur_is_new'] = 0;
1583 wfUpdateArray( 'cur', $fields, array( 'cur_namespace' => $ns, 'cur_title' => $dbkey ), $fname );
1585 # Insert new article
1586 $fields['cur_is_new'] = 1;
1587 $fields['cur_namespace'] = $ns;
1588 $fields['cur_title'] = $dbkey;
1589 $fields['cur_random'] = $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1590 wfInsertArray( 'cur', $fields, $fname );
1592 wfProfileOut( $fname );
1595 /* static */ function incViewCount( $id )
1597 $id = intval( $id );
1598 global $wgHitcounterUpdateFreq;
1600 if( $wgHitcounterUpdateFreq <= 1 ){ //
1601 wfQuery('UPDATE cur SET cur_counter = cur_counter + 1 ' .
1602 'WHERE cur_id = '.$id, DB_WRITE
);
1606 # Not important enough to warrant an error page in case of failure
1607 $oldignore = wfIgnoreSQLErrors( true );
1609 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE
);
1611 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1612 if( (rand() %
$checkfreq != 0) or (wfLastErrno() != 0) ){
1613 # Most of the time (or on SQL errors), skip row count check
1614 wfIgnoreSQLErrors( $oldignore );
1618 $res = wfQuery('SELECT COUNT(*) as n FROM hitcounter', DB_WRITE
);
1619 $row = wfFetchObject( $res );
1620 $rown = intval( $row->n
);
1621 if( $rown >= $wgHitcounterUpdateFreq ){
1622 wfProfileIn( 'Article::incViewCount-collect' );
1623 $old_user_abort = ignore_user_abort( true );
1625 wfQuery('LOCK TABLES hitcounter WRITE', DB_WRITE
);
1626 wfQuery('CREATE TEMPORARY TABLE acchits TYPE=HEAP '.
1627 'SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter '.
1628 'GROUP BY hc_id', DB_WRITE
);
1629 wfQuery('DELETE FROM hitcounter', DB_WRITE
);
1630 wfQuery('UNLOCK TABLES', DB_WRITE
);
1631 wfQuery('UPDATE cur,acchits SET cur_counter=cur_counter + hc_n '.
1632 'WHERE cur_id = hc_id', DB_WRITE
);
1633 wfQuery('DROP TABLE acchits', DB_WRITE
);
1635 ignore_user_abort( $old_user_abort );
1636 wfProfileOut( 'Article::incViewCount-collect' );
1638 wfIgnoreSQLErrors( $oldignore );
1641 # The onArticle*() functions are supposed to be a kind of hooks
1642 # which should be called whenever any of the specified actions
1645 # This is a good place to put code to clear caches, for instance.
1647 # This is called on page move and undelete, as well as edit
1648 /* static */ function onArticleCreate($title_obj){
1649 global $wgUseSquid, $wgDeferredUpdateList;
1651 $titles = $title_obj->getBrokenLinksTo();
1654 if ( $wgUseSquid ) {
1655 $urls = $title_obj->getSquidURLs();
1656 foreach ( $titles as $linkTitle ) {
1657 $urls[] = $linkTitle->getInternalURL();
1659 $u = new SquidUpdate( $urls );
1660 array_push( $wgDeferredUpdateList, $u );
1663 # Clear persistent link cache
1664 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1667 /* static */ function onArticleDelete($title_obj){
1668 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1671 /* static */ function onArticleEdit($title_obj){
1672 LinkCache
::linksccClearPage( $title_obj->getArticleID() );