bfe325a4674a456db776a34c95b3f2d47596f2c1
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' );
11 include_once ( 'SpecialValidate.php' ) ;
13 $wgArticleCurContentFields = false;
14 $wgArticleOldContentFields = false;
17 /* private */ var $mContent, $mContentLoaded;
18 /* private */ var $mUser, $mTimestamp, $mUserText;
19 /* private */ var $mCounter, $mComment, $mCountAdjustment;
20 /* private */ var $mMinorEdit, $mRedirectedFrom;
21 /* private */ var $mTouched, $mFileCache, $mTitle;
22 /* private */ var $mId, $mTable;
24 function Article( &$title ) {
25 $this->mTitle
=& $title;
29 /* private */ function clear()
31 $this->mContentLoaded
= false;
32 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
33 $this->mRedirectedFrom
= $this->mUserText
=
34 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= '';
35 $this->mCountAdjustment
= 0;
36 $this->mTouched
= '19700101000000';
39 # Get revision text associated with an old or archive row
40 # $row is usually an object from wfFetchRow(), both the flags and the text field must be included
41 /* static */ function getRevisionText( $row, $prefix = 'old_' ) {
43 $textField = $prefix . 'text';
44 $flagsField = $prefix . 'flags';
46 if ( isset( $row->$flagsField ) ) {
47 $flags = explode( ",", $row->$flagsField );
52 if ( isset( $row->$textField ) ) {
53 $text = $row->$textField;
58 if ( in_array( 'link', $flags ) ) {
60 $text = Article
::followLink( $text );
61 } elseif ( in_array( 'gzip', $flags ) ) {
62 # Deal with optional compression of archived pages.
63 # This can be done periodically via maintenance/compressOld.php, and
64 # as pages are saved if $wgCompressRevisions is set.
65 return gzinflate( $text );
70 /* static */ function compressRevisionText( &$text ) {
71 global $wgCompressRevisions;
72 if( !$wgCompressRevisions ) {
75 if( !function_exists( 'gzdeflate' ) ) {
76 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
79 $text = gzdeflate( $text );
83 # Returns the text associated with a "link" type old table row
84 /* static */ function followLink( $link ) {
85 # Split the link into fields and values
86 $lines = explode( '\n', $link );
89 foreach ( $lines as $line ) {
91 if ( $line{0} == '#' ) {
95 if ( preg_match( '/^(.*?)\s*:\s*(.*)$/', $line, $matches ) ) {
96 $field = strtolower($matches[1]);
98 if ( $field == 'hash' ) {
100 } elseif ( $field == 'location' ) {
101 $locations[] = $value;
106 if ( $hash === '' ) {
110 # Look in each specified location for the text
112 foreach ( $locations as $location ) {
113 $text = Article
::fetchFromLocation( $location, $hash );
114 if ( $text !== false ) {
122 /* static */ function fetchFromLocation( $location, $hash ) {
123 global $wgLoadBalancer;
124 $fname = 'fetchFromLocation';
125 wfProfileIn( $fname );
127 $p = strpos( $location, ':' );
128 if ( $p === false ) {
129 wfProfileOut( $fname );
133 $type = substr( $location, 0, $p );
137 # MySQL locations are specified by mysql://<machineID>/<dbname>/<tblname>/<index>
138 # Machine ID 0 is the current connection
139 if ( preg_match( '/^mysql:\/\/(\d+)\/([A-Za-z_]+)\/([A-Za-z_]+)\/([A-Za-z_]+)$/',
140 $location, $matches ) ) {
141 $machineID = $matches[1];
142 $dbName = $matches[2];
143 $tblName = $matches[3];
144 $index = $matches[4];
145 if ( $machineID == 0 ) {
147 $db =& wfGetDB( DB_SLAVE
);
149 # Alternate connection
150 $db =& $wgLoadBalancer->getConnection( $machineID );
152 if ( array_key_exists( $machineId, $wgKnownMysqlServers ) ) {
153 # Try to open, return false on failure
154 $params = $wgKnownDBServers[$machineId];
155 $db = Database
::newFromParams( $params['server'], $params['user'], $params['password'],
156 $dbName, 1, false, true, true );
159 if ( $db->isOpen() ) {
160 $index = $db->strencode( $index );
161 $res = $db->query( "SELECT blob_data FROM $dbName.$tblName WHERE blob_index='$index'", $fname );
162 $row = $db->fetchObject( $res );
163 $text = $row->text_data
;
168 # File locations are of the form file://<filename>, relative to the current directory
169 if ( preg_match( '/^file:\/\/(.*)$', $location, $matches ) )
170 $filename = strstr( $location, 'file://' );
171 $text = @file_get_contents
( $matches[1] );
173 if ( $text !== false ) {
174 # Got text, now we need to interpret it
175 # The first line contains information about how to do this
176 $p = strpos( $text, '\n' );
177 $type = substr( $text, 0, $p );
178 $text = substr( $text, $p +
1 );
183 $text = gzinflate( $text );
186 $object = unserialize( $text );
187 $text = $object->getItem( $hash );
193 wfProfileOut( $fname );
197 # Note that getContent/loadContent may follow redirects if
198 # not told otherwise, and so may cause a change to mTitle.
200 # Return the text of this revision
201 function getContent( $noredir )
205 # Get variables from query string :P
206 $action = $wgRequest->getText( 'action', 'view' );
207 $section = $wgRequest->getText( 'section' );
209 $fname = 'Article::getContent';
210 wfProfileIn( $fname );
212 if ( 0 == $this->getID() ) {
213 if ( 'edit' == $action ) {
214 wfProfileOut( $fname );
215 return ''; # was "newarticletext", now moved above the box)
217 wfProfileOut( $fname );
218 return wfMsg( 'noarticletext' );
220 $this->loadContent( $noredir );
223 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
224 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
225 preg_match('/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/',$this->mTitle
->getText()) &&
229 wfProfileOut( $fname );
230 return $this->mContent
. "\n" .wfMsg('anontalkpagetext'); }
232 if($action=='edit') {
234 if($section=='new') {
235 wfProfileOut( $fname );
239 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
240 # comments to be stripped as well)
241 $rv=$this->getSection($this->mContent
,$section);
242 wfProfileOut( $fname );
246 wfProfileOut( $fname );
247 return $this->mContent
;
252 # This function returns the text of a section, specified by a number ($section).
253 # A section is text under a heading like == Heading == or <h1>Heading</h1>, or
254 # the first section before any such heading (section 0).
256 # If a section contains subsections, these are also returned.
258 function getSection($text,$section) {
260 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
261 # comments to be stripped as well)
263 $parser=new Parser();
264 $parser->mOutputType
=OT_WIKI
;
265 $striptext=$parser->strip($text, $striparray, true);
267 # now that we can be sure that no pseudo-sections are in the source,
268 # split it up by section
271 '/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
273 PREG_SPLIT_DELIM_CAPTURE
);
277 $headline=$secs[$section*2-1];
278 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
281 # translate wiki heading into level
282 if(strpos($hlevel,'=')!==false) {
283 $hlevel=strlen($hlevel);
286 $rv=$headline. $secs[$section*2];
290 while(!empty($secs[$count*2-1]) && !$break) {
292 $subheadline=$secs[$count*2-1];
293 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
294 $subhlevel=$matches[1];
295 if(strpos($subhlevel,'=')!==false) {
296 $subhlevel=strlen($subhlevel);
298 if($subhlevel > $hlevel) {
299 $rv.=$subheadline.$secs[$count*2];
301 if($subhlevel <= $hlevel) {
308 # reinsert stripped tags
309 $rv=$parser->unstrip($rv,$striparray);
310 $rv=$parser->unstripNoWiki($rv,$striparray);
316 # Return an array of the columns of the "cur"-table
317 function &getCurContentFields() {
318 global $wgArticleCurContentFields;
319 if ( !$wgArticleCurContentFields ) {
320 $wgArticleCurContentFields = array( 'cur_text','cur_timestamp','cur_user', 'cur_user_text',
321 'cur_comment','cur_counter','cur_restrictions','cur_touched' );
323 return $wgArticleCurContentFields;
326 # Return an array of the columns of the "old"-table
327 function &getOldContentFields() {
328 global $wgArticleOldContentFields;
329 if ( !$wgArticleOldContentFields ) {
330 $wgArticleOldContentFields = array( 'old_namespace','old_title','old_text','old_timestamp',
331 'old_user','old_user_text','old_comment','old_flags' );
333 return $wgArticleOldContentFields;
336 # Load the revision (including cur_text) into this object
337 function loadContent( $noredir = false )
339 global $wgOut, $wgMwRedir, $wgRequest;
341 $dbr =& wfGetDB( DB_SLAVE
);
343 $oldid = $wgRequest->getVal( 'oldid' );
344 $redirect = $wgRequest->getVal( 'redirect' );
346 if ( $this->mContentLoaded
) return;
347 $fname = 'Article::loadContent';
349 # Pre-fill content with error message so that if something
350 # fails we'll have something telling us what we intended.
352 $t = $this->mTitle
->getPrefixedText();
353 if ( isset( $oldid ) ) {
354 $oldid = IntVal( $oldid );
355 $t .= ",oldid={$oldid}";
357 if ( isset( $redirect ) ) {
358 $redirect = ($redirect == 'no') ?
'no' : 'yes';
359 $t .= ",redirect={$redirect}";
361 $this->mContent
= wfMsg( 'missingarticle', $t );
363 if ( ! $oldid ) { # Retrieve current version
364 $id = $this->getID();
365 if ( 0 == $id ) return;
367 $s = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $id ), $fname );
368 if ( $s === false ) {
372 # If we got a redirect, follow it (unless we've been told
373 # not to by either the function parameter or the query
374 if ( ( 'no' != $redirect ) && ( false == $noredir ) &&
375 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
376 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
377 $s->cur_text
, $m ) ) {
378 $rt = Title
::newFromText( $m[1] );
380 # Gotta hand redirects to special pages differently:
381 # Fill the HTTP response "Location" header and ignore
382 # the rest of the page we're on.
384 if ( $rt->getInterwiki() != '' ) {
385 $wgOut->redirect( $rt->getFullURL() ) ;
388 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
389 $wgOut->redirect( $rt->getFullURL() );
392 $rid = $rt->getArticleID();
394 $redirRow = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $rid ), $fname );
396 if ( $redirRow !== false ) {
397 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
406 $this->mContent
= $s->cur_text
;
407 $this->mUser
= $s->cur_user
;
408 $this->mUserText
= $s->cur_user_text
;
409 $this->mComment
= $s->cur_comment
;
410 $this->mCounter
= $s->cur_counter
;
411 $this->mTimestamp
= $s->cur_timestamp
;
412 $this->mTouched
= $s->cur_touched
;
413 $this->mTitle
->mRestrictions
= explode( ',', trim( $s->cur_restrictions
) );
414 $this->mTitle
->mRestrictionsLoaded
= true;
415 } else { # oldid set, retrieve historical version
416 $s = $dbr->getArray( 'old', $this->getOldContentFields(), array( 'old_id' => $oldid ), $fname );
417 if ( $s === false ) {
421 if( $this->mTitle
->getNamespace() != $s->old_namespace ||
422 $this->mTitle
->getDBkey() != $s->old_title
) {
423 $oldTitle = Title
::makeTitle( $s->old_namesapce
, $s->old_title
);
424 $this->mTitle
= $oldTitle;
425 $wgTitle = $oldTitle;
427 $this->mContent
= Article
::getRevisionText( $s );
428 $this->mUser
= $s->old_user
;
429 $this->mUserText
= $s->old_user_text
;
430 $this->mComment
= $s->old_comment
;
432 $this->mTimestamp
= $s->old_timestamp
;
434 $this->mContentLoaded
= true;
435 return $this->mContent
;
438 # Gets the article text without using so many damn globals
439 # Returns false on error
440 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
443 if ( $this->mContentLoaded
) {
444 return $this->mContent
;
446 $this->mContent
= false;
448 $fname = 'Article::getContentWithout';
449 $dbr =& wfGetDB( DB_SLAVE
);
451 if ( ! $oldid ) { # Retrieve current version
452 $id = $this->getID();
457 $s = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $id ), $fname );
458 if ( $s === false ) {
462 # If we got a redirect, follow it (unless we've been told
463 # not to by either the function parameter or the query
464 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text
) ) {
465 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
466 $s->cur_text
, $m ) ) {
467 $rt = Title
::newFromText( $m[1] );
468 if( $rt && $rt->getInterwiki() == '' && $rt->getNamespace() != Namespace::getSpecial() ) {
469 $rid = $rt->getArticleID();
471 $redirRow = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $rid ), $fname );
473 if ( $redirRow !== false ) {
474 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
483 $this->mContent
= $s->cur_text
;
484 $this->mUser
= $s->cur_user
;
485 $this->mUserText
= $s->cur_user_text
;
486 $this->mComment
= $s->cur_comment
;
487 $this->mCounter
= $s->cur_counter
;
488 $this->mTimestamp
= $s->cur_timestamp
;
489 $this->mTouched
= $s->cur_touched
;
490 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
491 $this->mTitle
->mRestrictionsLoaded
= true;
492 } else { # oldid set, retrieve historical version
493 $s = $dbr->getArray( 'old', $this->getOldContentFields(), array( 'old_id' => $oldid ) );
494 if ( $s === false ) {
497 $this->mContent
= Article
::getRevisionText( $s );
498 $this->mUser
= $s->old_user
;
499 $this->mUserText
= $s->old_user_text
;
500 $this->mComment
= $s->old_comment
;
502 $this->mTimestamp
= $s->old_timestamp
;
504 $this->mContentLoaded
= true;
505 return $this->mContent
;
509 if( $this->mTitle
) {
510 return $this->mTitle
->getArticleID();
518 if ( -1 == $this->mCounter
) {
519 $id = $this->getID();
520 $dbr =& wfGetDB( DB_SLAVE
);
521 $this->mCounter
= $dbr->getField( 'cur', 'cur_counter', "cur_id={$id}" );
523 return $this->mCounter
;
526 # Would the given text make this article a "good" article (i.e.,
527 # suitable for including in the article count)?
529 function isCountable( $text )
531 global $wgUseCommaCount, $wgMwRedir;
533 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
534 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
535 $token = ($wgUseCommaCount ?
',' : '[[' );
536 if ( false === strstr( $text, $token ) ) { return 0; }
540 # Loads everything from cur except cur_text
541 # This isn't necessary for all uses, so it's only done if needed.
543 /* private */ function loadLastEdit()
546 if ( -1 != $this->mUser
) return;
548 $fname = 'Article::loadLastEdit';
550 $dbr =& wfGetDB( DB_SLAVE
);
551 $s = $dbr->getArray( 'cur',
552 array( 'cur_user','cur_user_text','cur_timestamp', 'cur_comment','cur_minor_edit' ),
553 array( 'cur_id' => $this->getID() ), $fname );
555 if ( $s !== false ) {
556 $this->mUser
= $s->cur_user
;
557 $this->mUserText
= $s->cur_user_text
;
558 $this->mTimestamp
= $s->cur_timestamp
;
559 $this->mComment
= $s->cur_comment
;
560 $this->mMinorEdit
= $s->cur_minor_edit
;
564 function getTimestamp()
566 $this->loadLastEdit();
567 return $this->mTimestamp
;
572 $this->loadLastEdit();
576 function getUserText()
578 $this->loadLastEdit();
579 return $this->mUserText
;
582 function getComment()
584 $this->loadLastEdit();
585 return $this->mComment
;
588 function getMinorEdit()
590 $this->loadLastEdit();
591 return $this->mMinorEdit
;
594 function getContributors($limit = 0, $offset = 0)
596 $fname = 'Article::getContributors';
598 # XXX: this is expensive; cache this info somewhere.
600 $title = $this->mTitle
;
602 $dbr =& wfGetDB( DB_SLAVE
);
603 $oldTable = $dbr->tableName( 'old' );
604 $userTable = $dbr->tableName( 'user' );
605 $encDBkey = $dbr->strencode( $title->getDBkey() );
606 $ns = $title->getNamespace();
607 $user = $this->getUser();
609 $sql = "SELECT old_user, old_user_text, user_real_name, MAX(old_timestamp) as timestamp
610 FROM $oldTable LEFT JOIN $userTable ON old_user = user_id
611 WHERE old_namespace = $user
612 AND old_title = $encDBkey
613 AND old_user != $user
615 ORDER BY timestamp DESC";
618 $sql .= ' LIMIT '.$limit;
621 $res = $dbr->query($sql, $fname);
623 while ( $line = $dbr->fetchObject( $res ) ) {
624 $contribs[] = array($line->old_user
, $line->old_user_text
, $line->user_real_name
);
627 $dbr->freeResult($res);
632 # This is the default action of the script: just view the page of
637 global $wgUser, $wgOut, $wgLang, $wgRequest;
638 global $wgLinkCache, $IP, $wgEnableParserCache;
640 $fname = 'Article::view';
641 wfProfileIn( $fname );
643 # Get variables from query string :P
644 $oldid = $wgRequest->getVal( 'oldid' );
645 $diff = $wgRequest->getVal( 'diff' );
647 $wgOut->setArticleFlag( true );
648 $wgOut->setRobotpolicy( 'index,follow' );
650 # If we got diff and oldid in the query, we want to see a
651 # diff page instead of the article.
653 if ( !is_null( $diff ) ) {
654 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
655 $de = new DifferenceEngine( intval($oldid), intval($diff) );
657 wfProfileOut( $fname );
661 if ( empty( $oldid ) && $this->checkTouched() ) {
662 if( $wgOut->checkLastModified( $this->mTouched
) ){
664 } else if ( $this->tryFileCache() ) {
665 # tell wgOut that output is taken care of
667 $this->viewUpdates();
672 # Should the parser cache be used?
673 if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
681 if ( $wgOut->tryParserCache( $this, $wgUser ) ) {
686 if ( !$outputDone ) {
687 $text = $this->getContent( false ); # May change mTitle by following a redirect
689 # Another whitelist check in case oldid or redirects are altering the title
690 if ( !$this->mTitle
->userCanRead() ) {
691 $wgOut->loginToUse();
697 # We're looking at an old revision
699 if ( !empty( $oldid ) ) {
700 $this->setOldSubtitle();
701 $wgOut->setRobotpolicy( 'noindex,follow' );
703 if ( '' != $this->mRedirectedFrom
) {
704 $sk = $wgUser->getSkin();
705 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, '',
707 $s = wfMsg( 'redirectedfrom', $redir );
708 $wgOut->setSubtitle( $s );
710 # Can't cache redirects
714 $wgLinkCache->preFill( $this->mTitle
);
716 # wrap user css and user js in pre and don't parse
717 # XXX: use $this->mTitle->usCssJsSubpage() when php is fixed/ a workaround is found
719 $this->mTitle
->getNamespace() == Namespace::getUser() &&
720 preg_match('/\\/[\\w]+\\.(css|js)$/', $this->mTitle
->getDBkey())
722 $wgOut->addWikiText( wfMsg('clearyourcache'));
723 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent
)."\n</pre>" );
724 } else if ( $pcache ) {
725 $wgOut->addWikiText( $text, true, $this );
727 $wgOut->addWikiText( $text );
730 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
732 # Add link titles as META keywords
733 $wgOut->addMetaTags() ;
735 $this->viewUpdates();
736 wfProfileOut( $fname );
739 # Theoretically we could defer these whole insert and update
740 # functions for after display, but that's taking a big leap
741 # of faith, and we want to be able to report database
742 # errors at some point.
744 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
746 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
747 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
749 $fname = 'Article::insertNewArticle';
751 $this->mCountAdjustment
= $this->isCountable( $text );
753 $ns = $this->mTitle
->getNamespace();
754 $ttl = $this->mTitle
->getDBkey();
755 $text = $this->preSaveTransform( $text );
756 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
759 $now = wfTimestampNow();
760 $won = wfInvertTimestamp( $now );
762 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
763 $dbw =& wfGetDB( DB_MASTER
);
765 $cur_id = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
767 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
769 $dbw->insertArray( 'cur', array(
771 'cur_namespace' => $ns,
774 'cur_comment' => $summary,
775 'cur_user' => $wgUser->getID(),
776 'cur_timestamp' => $now,
777 'cur_minor_edit' => $isminor,
779 'cur_restrictions' => '',
780 'cur_user_text' => $wgUser->getName(),
781 'cur_is_redirect' => $redir,
783 'cur_random' => $rand,
784 'cur_touched' => $now,
785 'inverse_timestamp' => $won,
788 $newid = $dbw->insertId();
789 $this->mTitle
->resetArticleID( $newid );
791 Article
::onArticleCreate( $this->mTitle
);
792 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
795 if(!$this->mTitle
->userIsWatching()) $this->watch();
797 if ( $this->mTitle
->userIsWatching() ) {
802 # The talk page isn't in the regular link tables, so we need to update manually:
803 $talkns = $ns ^
1; # talk -> normal; normal -> talk
804 $dbw->updateArray( 'cur', array( 'cur_touched' => $now ), array( 'cur_namespace' => $talkns, 'cur_title' => $ttl ), $fname );
806 # standard deferred updates
807 $this->editUpdates( $text );
809 $this->showArticle( $text, wfMsg( 'newarticle' ) );
813 /* Side effects: loads last edit */
814 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ''){
815 $this->loadLastEdit();
816 $oldtext = $this->getContent( true );
817 if ($section != '') {
818 if($section=='new') {
819 if($summary) $subject="== {$summary} ==\n\n";
820 $text=$oldtext."\n\n".$subject.$text;
823 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
824 # comments to be stripped as well)
826 $parser=new Parser();
827 $parser->mOutputType
=OT_WIKI
;
828 $oldtext=$parser->strip($oldtext, $striparray, true);
830 # now that we can be sure that no pseudo-sections are in the source,
832 # Unfortunately we can't simply do a preg_replace because that might
833 # replace the wrong section, so we have to use the section counter instead
834 $secs=preg_split('/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
835 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
836 $secs[$section*2]=$text."\n\n"; // replace with edited
838 # section 0 is top (intro) section
841 # headline of old section - we need to go through this section
842 # to determine if there are any subsections that now need to
843 # be erased, as the mother section has been replaced with
844 # the text of all subsections.
845 $headline=$secs[$section*2-1];
846 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
849 # determine headline level for wikimarkup headings
850 if(strpos($hlevel,'=')!==false) {
851 $hlevel=strlen($hlevel);
854 $secs[$section*2-1]=''; // erase old headline
857 while(!empty($secs[$count*2-1]) && !$break) {
859 $subheadline=$secs[$count*2-1];
861 '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
862 $subhlevel=$matches[1];
863 if(strpos($subhlevel,'=')!==false) {
864 $subhlevel=strlen($subhlevel);
866 if($subhlevel > $hlevel) {
867 // erase old subsections
868 $secs[$count*2-1]='';
871 if($subhlevel <= $hlevel) {
879 $text=join('',$secs);
880 # reinsert the stuff that we stripped out earlier
881 $text=$parser->unstrip($text,$striparray);
882 $text=$parser->unstripNoWiki($text,$striparray);
889 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' )
891 global $wgOut, $wgUser, $wgLinkCache;
892 global $wgDBtransactions, $wgMwRedir;
893 global $wgUseSquid, $wgInternalServer;
895 $fname = 'Article::updateArticle';
898 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
899 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
900 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ')[^\\n]+)/i', $text, $m ) ) {
902 $text = $m[1] . "\n"; # Remove all content but redirect
906 $text = $this->preSaveTransform( $text );
907 $dbw =& wfGetDB( DB_MASTER
);
909 # Update article, but only if changed.
911 # It's important that we either rollback or complete, otherwise an attacker could
912 # overwrite cur entries by sending precisely timed user aborts. Random bored users
913 # could conceivably have the same effect, especially if cur is locked for long periods.
914 if( $wgDBtransactions ) {
915 $dbw->query( 'BEGIN', $fname );
917 $userAbort = ignore_user_abort( true );
920 $oldtext = $this->getContent( true );
922 if ( 0 != strcmp( $text, $oldtext ) ) {
923 $this->mCountAdjustment
= $this->isCountable( $text )
924 - $this->isCountable( $oldtext );
925 $now = wfTimestampNow();
926 $won = wfInvertTimestamp( $now );
928 # First update the cur row
929 $dbw->updateArray( 'cur',
932 'cur_comment' => $summary,
933 'cur_minor_edit' => $me2,
934 'cur_user' => $wgUser->getID(),
935 'cur_timestamp' => $now,
936 'cur_user_text' => $wgUser->getName(),
937 'cur_is_redirect' => $redir,
939 'cur_touched' => $now,
940 'inverse_timestamp' => $won
941 ), array( /* WHERE */
942 'cur_id' => $this->getID(),
943 'cur_timestamp' => $this->getTimestamp()
947 if( $dbw->affectedRows() == 0 ) {
948 /* Belated edit conflict! Run away!! */
951 # Now insert the previous revision into old
953 # This overwrites $oldtext if revision compression is on
954 $flags = Article
::compressRevisionText( $oldtext );
956 $dbw->insertArray( 'old',
958 'old_id' => $dbw->nextSequenceValue( 'old_old_id_seq' ),
959 'old_namespace' => $this->mTitle
->getNamespace(),
960 'old_title' => $this->mTitle
->getDBkey(),
961 'old_text' => $oldtext,
962 'old_comment' => $this->getComment(),
963 'old_user' => $this->getUser(),
964 'old_user_text' => $this->getUserText(),
965 'old_timestamp' => $this->getTimestamp(),
966 'old_minor_edit' => $me1,
967 'inverse_timestamp' => wfInvertTimestamp( $this->getTimestamp() ),
968 'old_flags' => $flags,
972 $oldid = $dbw->insertId();
974 $bot = (int)($wgUser->isBot() ||
$forceBot);
975 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
976 $oldid, $this->getTimestamp(), $bot );
977 Article
::onArticleEdit( $this->mTitle
);
981 if( $wgDBtransactions ) {
982 $dbw->query( 'COMMIT', $fname );
984 ignore_user_abort( $userAbort );
989 if (!$this->mTitle
->userIsWatching()) $this->watch();
991 if ( $this->mTitle
->userIsWatching() ) {
995 # standard deferred updates
996 $this->editUpdates( $text );
1000 # Template namespace
1001 # Purge all articles linking here
1002 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
1003 $titles = $this->mTitle
->getLinksTo();
1004 Title
::touchArray( $titles );
1005 if ( $wgUseSquid ) {
1006 foreach ( $titles as $title ) {
1007 $urls[] = $title->getInternalURL();
1013 if ( $wgUseSquid ) {
1014 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
1015 $u = new SquidUpdate( $urls );
1019 $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor );
1024 # After we've either updated or inserted the article, update
1025 # the link tables and redirect to the new page.
1027 function showArticle( $text, $subtitle , $sectionanchor = '' )
1029 global $wgOut, $wgUser, $wgLinkCache;
1032 $wgLinkCache = new LinkCache();
1034 $wgLinkCache->forUpdate( true );
1036 # Get old version of link table to allow incremental link updates
1037 $wgLinkCache->preFill( $this->mTitle
);
1038 $wgLinkCache->clear();
1040 # Now update the link cache by parsing the text
1041 $wgOut = new OutputPage();
1042 $wgOut->addWikiText( $text );
1044 if( $wgMwRedir->matchStart( $text ) )
1048 $wgOut->redirect( $this->mTitle
->getFullURL( $r ).$sectionanchor );
1053 function validate ()
1056 $v = new Validation
;
1057 $html = $v->validate_form ( $this->mTitle
->getDBkey() ) ;
1058 $wgOut->setPagetitle( wfMsg( 'validate' ) . " : " . $this->mTitle
->getText() );
1059 $wgOut->setRobotpolicy( 'noindex,follow' );
1060 $wgOut->addHTML( $html ) ;
1063 # Add this page to my watchlist
1065 function watch( $add = true )
1067 global $wgUser, $wgOut, $wgLang;
1068 global $wgDeferredUpdateList;
1070 if ( 0 == $wgUser->getID() ) {
1071 $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
1074 if ( wfReadOnly() ) {
1075 $wgOut->readOnlyPage();
1079 $wgUser->addWatch( $this->mTitle
);
1081 $wgUser->removeWatch( $this->mTitle
);
1083 $wgOut->setPagetitle( wfMsg( $add ?
'addedwatch' : 'removedwatch' ) );
1084 $wgOut->setRobotpolicy( 'noindex,follow' );
1086 $sk = $wgUser->getSkin() ;
1087 $link = $this->mTitle
->getPrefixedText();
1090 $text = wfMsg( 'addedwatchtext', $link );
1092 $text = wfMsg( 'removedwatchtext', $link );
1093 $wgOut->addWikiText( $text );
1095 $up = new UserUpdate();
1096 array_push( $wgDeferredUpdateList, $up );
1098 $wgOut->returnToMain( false );
1103 $this->watch( false );
1106 function protect( $limit = 'sysop' )
1108 global $wgUser, $wgOut, $wgRequest;
1110 if ( ! $wgUser->isSysop() ) {
1111 $wgOut->sysopRequired();
1114 if ( wfReadOnly() ) {
1115 $wgOut->readOnlyPage();
1118 $id = $this->mTitle
->getArticleID();
1120 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
1124 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
1125 $reason = $wgRequest->getText( 'wpReasonProtect' );
1128 $dbw =& wfGetDB( DB_MASTER
);
1129 $dbw->updateArray( 'cur',
1131 'cur_touched' => wfTimestampNow(),
1132 'cur_restrictions' => (string)$limit
1133 ), array( /* WHERE */
1135 ), 'Article::protect'
1138 $log = new LogPage( wfMsg( 'protectlogpage' ), wfMsg( 'protectlogtext' ) );
1139 if ( $limit === "" ) {
1140 $log->addEntry( wfMsg( 'unprotectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
1142 $log->addEntry( wfMsg( 'protectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
1144 $wgOut->redirect( $this->mTitle
->getFullURL() );
1147 $reason = htmlspecialchars( wfMsg( 'protectreason' ) );
1148 return $this->confirmProtect( '', $reason, $limit );
1152 # Output protection confirmation dialog
1153 function confirmProtect( $par, $reason, $limit = 'sysop' )
1157 wfDebug( "Article::confirmProtect\n" );
1159 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1160 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1165 if ( $limit === '' ) {
1166 $wgOut->setPageTitle( wfMsg( 'confirmunprotect' ) );
1167 $wgOut->setSubtitle( wfMsg( 'unprotectsub', $sub ) );
1168 $wgOut->addWikiText( wfMsg( 'confirmunprotecttext' ) );
1169 $check = htmlspecialchars( wfMsg( 'confirmunprotect' ) );
1170 $protcom = htmlspecialchars( wfMsg( 'unprotectcomment' ) );
1171 $formaction = $this->mTitle
->escapeLocalURL( 'action=unprotect' . $par );
1173 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
1174 $wgOut->setSubtitle( wfMsg( 'protectsub', $sub ) );
1175 $wgOut->addWikiText( wfMsg( 'confirmprotecttext' ) );
1176 $check = htmlspecialchars( wfMsg( 'confirmprotect' ) );
1177 $protcom = htmlspecialchars( wfMsg( 'protectcomment' ) );
1178 $formaction = $this->mTitle
->escapeLocalURL( 'action=protect' . $par );
1181 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1184 <form id='protectconfirm' method='post' action=\"{$formaction}\">
1188 <label for='wpReasonProtect'>{$protcom}:</label>
1191 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1199 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1202 <label for='wpConfirmProtect'>{$check}</label>
1208 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1214 $wgOut->returnToMain( false );
1217 function unprotect()
1219 return $this->protect( '' );
1222 # UI entry point for page deletion
1225 global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
1226 $fname = 'Article::delete';
1227 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1228 $reason = $wgRequest->getText( 'wpReason' );
1230 # This code desperately needs to be totally rewritten
1233 if ( ( ! $wgUser->isSysop() ) ) {
1234 $wgOut->sysopRequired();
1237 if ( wfReadOnly() ) {
1238 $wgOut->readOnlyPage();
1242 # Better double-check that it hasn't been deleted yet!
1243 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
1244 if ( ( '' == trim( $this->mTitle
->getText() ) )
1245 or ( $this->mTitle
->getArticleId() == 0 ) ) {
1246 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1251 $this->doDelete( $reason );
1255 # determine whether this page has earlier revisions
1256 # and insert a warning if it does
1257 # we select the text because it might be useful below
1258 $dbr =& wfGetDB( DB_SLAVE
);
1259 $ns = $this->mTitle
->getNamespace();
1260 $title = $this->mTitle
->getDBkey();
1261 $old = $dbr->getArray( 'old',
1262 array( 'old_text', 'old_flags' ),
1264 'old_namespace' => $ns,
1265 'old_title' => $title,
1266 ), $fname, array( 'ORDER BY' => 'inverse_timestamp' )
1269 if( $old !== false && !$confirm ) {
1270 $skin=$wgUser->getSkin();
1271 $wgOut->addHTML('<b>'.wfMsg('historywarning'));
1272 $wgOut->addHTML( $skin->historyLink() .'</b>');
1276 $s = $dbr->getArray( 'cur',
1277 array( 'cur_text' ),
1279 'cur_namespace' => $ns,
1280 'cur_title' => $title,
1284 if( $s !== false ) {
1285 # if this is a mini-text, we can paste part of it into the deletion reason
1287 #if this is empty, an earlier revision may contain "useful" text
1289 if($s->cur_text
!="") {
1293 $text = Article
::getRevisionText( $old );
1299 $length=strlen($text);
1301 # this should not happen, since it is not possible to store an empty, new
1302 # page. Let's insert a standard text in case it does, though
1303 if($length == 0 && $reason === '') {
1304 $reason = wfMsg('exblank');
1307 if($length < 500 && $reason === '') {
1309 # comment field=255, let's grep the first 150 to have some user
1311 $text=substr($text,0,150);
1312 # let's strip out newlines and HTML tags
1313 $text=preg_replace('/\"/',"'",$text);
1314 $text=preg_replace('/\</','<',$text);
1315 $text=preg_replace('/\>/','>',$text);
1316 $text=preg_replace("/[\n\r]/",'',$text);
1318 $reason=wfMsg('excontent'). " '".$text;
1320 $reason=wfMsg('exbeforeblank') . " '".$text;
1322 if($length>150) { $reason .= '...'; } # we've only pasted part of the text
1327 return $this->confirmDelete( '', $reason );
1330 # Output deletion confirmation dialog
1331 function confirmDelete( $par, $reason )
1335 wfDebug( "Article::confirmDelete\n" );
1337 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1338 $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
1339 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1340 $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
1342 $formaction = $this->mTitle
->escapeLocalURL( 'action=delete' . $par );
1344 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1345 $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
1346 $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
1349 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1353 <label for='wpReason'>{$delcom}:</label>
1356 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1364 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1367 <label for='wpConfirm'>{$check}</label>
1373 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1379 $wgOut->returnToMain( false );
1382 # Perform a deletion and output success or failure messages
1383 function doDelete( $reason )
1385 global $wgOut, $wgUser, $wgLang;
1386 $fname = 'Article::doDelete';
1387 wfDebug( "$fname\n" );
1389 if ( $this->doDeleteArticle( $reason ) ) {
1390 $deleted = $this->mTitle
->getPrefixedText();
1392 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1393 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1395 $sk = $wgUser->getSkin();
1396 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1397 Namespace::getWikipedia() ) .
1398 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
1400 $text = wfMsg( "deletedtext", $deleted, $loglink );
1402 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
1403 $wgOut->returnToMain( false );
1405 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1409 # Back-end article deletion
1410 # Deletes the article with database consistency, writes logs, purges caches
1412 function doDeleteArticle( $reason )
1414 global $wgUser, $wgLang;
1415 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1417 $fname = 'Article::doDeleteArticle';
1418 wfDebug( $fname."\n" );
1420 $dbw =& wfGetDB( DB_MASTER
);
1421 $ns = $this->mTitle
->getNamespace();
1422 $t = $this->mTitle
->getDBkey();
1423 $id = $this->mTitle
->getArticleID();
1425 if ( '' == $t ||
$id == 0 ) {
1429 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1430 array_push( $wgDeferredUpdateList, $u );
1432 $linksTo = $this->mTitle
->getLinksTo();
1435 if ( $wgUseSquid ) {
1437 $this->mTitle
->getInternalURL(),
1438 $this->mTitle
->getInternalURL( 'history' )
1440 foreach ( $linksTo as $linkTo ) {
1441 $urls[] = $linkTo->getInternalURL();
1444 $u = new SquidUpdate( $urls );
1445 array_push( $wgDeferredUpdateList, $u );
1449 # Client and file cache invalidation
1450 Title
::touchArray( $linksTo );
1452 # Move article and history to the "archive" table
1453 $archiveTable = $dbw->tableName( 'archive' );
1454 $oldTable = $dbw->tableName( 'old' );
1455 $curTable = $dbw->tableName( 'cur' );
1456 $recentchangesTable = $dbw->tableName( 'recentchanges' );
1457 $linksTable = $dbw->tableName( 'links' );
1458 $brokenlinksTable = $dbw->tableName( 'brokenlinks' );
1460 $dbw->insertSelect( 'archive', 'cur',
1462 'ar_namespace' => 'cur_namespace',
1463 'ar_title' => 'cur_title',
1464 'ar_text' => 'cur_text',
1465 'ar_comment' => 'cur_comment',
1466 'ar_user' => 'cur_user',
1467 'ar_user_text' => 'cur_user_text',
1468 'ar_timestamp' => 'cur_timestamp',
1469 'ar_minor_edit' => 'cur_minor_edit',
1472 'cur_namespace' => $ns,
1477 $dbw->insertSelect( 'archive', 'old',
1479 'ar_namespace' => 'old_namespace',
1480 'ar_title' => 'old_title',
1481 'ar_text' => 'old_text',
1482 'ar_comment' => 'old_comment',
1483 'ar_user' => 'old_user',
1484 'ar_user_text' => 'old_user_text',
1485 'ar_timestamp' => 'old_timestamp',
1486 'ar_minor_edit' => 'old_minor_edit',
1487 'ar_flags' => 'old_flags'
1489 'old_namespace' => $ns,
1494 # Now that it's safely backed up, delete it
1496 $dbw->delete( 'cur', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname );
1497 $dbw->delete( 'old', array( 'old_namespace' => $ns, 'old_title' => $t ), $fname );
1498 $dbw->delete( 'recentchanges', array( 'rc_namespace' => $ns, 'rc_title' => $t ), $fname );
1500 # Finally, clean up the link tables
1501 $t = $this->mTitle
->getPrefixedDBkey();
1503 Article
::onArticleDelete( $this->mTitle
);
1505 # Insert broken links
1506 $brokenLinks = array();
1507 foreach ( $linksTo as $titleObj ) {
1508 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1509 $linkID = $titleObj->getArticleID();
1510 $brokenLinks[] = array( 'bl_from' => $linkID, 'bl_to' => $t );
1512 $dbw->insertArray( 'brokenlinks', $brokenLinks, $fname );
1515 $dbw->delete( 'links', array( 'l_to' => $id ) );
1516 $dbw->delete( 'links', array( 'l_from' => $id ) );
1517 $dbw->delete( 'imagelinks', array( 'il_from' => $id ) );
1518 $dbw->delete( 'brokenlinks', array( 'bl_from' => $id ) );
1519 $dbw->delete( 'categorylinks', array( 'cl_from' => $id ) );
1522 $log = new LogPage( wfMsg( 'dellogpage' ), wfMsg( 'dellogpagetext' ) );
1523 $art = $this->mTitle
->getPrefixedText();
1524 $log->addEntry( wfMsg( 'deletedarticle', $art ), $reason );
1526 # Clear the cached article id so the interface doesn't act like we exist
1527 $this->mTitle
->resetArticleID( 0 );
1528 $this->mTitle
->mArticleID
= 0;
1534 global $wgUser, $wgLang, $wgOut, $wgRequest;
1535 $fname = "Article::rollback";
1537 if ( ! $wgUser->isSysop() ) {
1538 $wgOut->sysopRequired();
1541 if ( wfReadOnly() ) {
1542 $wgOut->readOnlyPage( $this->getContent( true ) );
1545 $dbw =& wfGetDB( DB_MASTER
);
1547 # Enhanced rollback, marks edits rc_bot=1
1548 $bot = $wgRequest->getBool( 'bot' );
1550 # Replace all this user's current edits with the next one down
1551 $tt = $this->mTitle
->getDBKey();
1552 $n = $this->mTitle
->getNamespace();
1554 # Get the last editor, lock table exclusively
1555 $s = $dbw->getArray( 'cur',
1556 array( 'cur_id','cur_user','cur_user_text','cur_comment' ),
1557 array( 'cur_title' => $tt, 'cur_namespace' => $n ),
1558 $fname, 'FOR UPDATE'
1560 if( $s === false ) {
1562 $wgOut->addHTML( wfMsg( 'notanarticle' ) );
1565 $ut = $dbw->strencode( $s->cur_user_text
);
1566 $uid = $s->cur_user
;
1569 $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
1570 if( $from != $s->cur_user_text
) {
1571 $wgOut->setPageTitle(wfmsg('rollbackfailed'));
1572 $wgOut->addWikiText( wfMsg( 'alreadyrolled',
1573 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1574 htmlspecialchars( $from ),
1575 htmlspecialchars( $s->cur_user_text
) ) );
1576 if($s->cur_comment
!= '') {
1578 wfMsg('editcomment',
1579 htmlspecialchars( $s->cur_comment
) ) );
1584 # Get the last edit not by this guy
1585 $s = $dbw->getArray( 'old',
1586 array( 'old_text','old_user','old_user_text','old_timestamp','old_flags' ),
1588 'old_namespace' => $n,
1590 "old_user <> {$uid} OR old_user_text <> '{$ut}'"
1591 ), $fname, array( 'FOR UPDATE', 'USE INDEX' => 'name_title_timestamp' )
1593 if( $s === false ) {
1595 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
1596 $wgOut->addHTML( wfMsg( 'cantrollback' ) );
1601 # Mark all reverted edits as bot
1602 $dbw->updateArray( 'recentchanges',
1605 ), array( /* WHERE */
1607 "rc_timestamp > '{$s->old_timestamp}'",
1613 $newcomment = wfMsg( 'revertpage', $s->old_user_text
, $from );
1614 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1615 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1616 $wgOut->addHTML( '<h2>' . $newcomment . "</h2>\n<hr />\n" );
1617 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1618 Article
::onArticleEdit( $this->mTitle
);
1619 $wgOut->returnToMain( false );
1623 # Do standard deferred updates after page view
1625 /* private */ function viewUpdates()
1627 global $wgDeferredUpdateList;
1628 if ( 0 != $this->getID() ) {
1629 global $wgDisableCounters;
1630 if( !$wgDisableCounters ) {
1631 Article
::incViewCount( $this->getID() );
1632 $u = new SiteStatsUpdate( 1, 0, 0 );
1633 array_push( $wgDeferredUpdateList, $u );
1635 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1636 $this->mTitle
->getDBkey() );
1637 array_push( $wgDeferredUpdateList, $u );
1641 # Do standard deferred updates after page edit.
1642 # Every 1000th edit, prune the recent changes table.
1644 /* private */ function editUpdates( $text )
1646 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1647 global $wgMessageCache;
1650 if ( 0 == mt_rand( 0, 999 ) ) {
1651 $dbw =& wfGetDB( DB_MASTER
);
1652 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1653 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1654 $dbw->query( $sql );
1656 $id = $this->getID();
1657 $title = $this->mTitle
->getPrefixedDBkey();
1658 $shortTitle = $this->mTitle
->getDBkey();
1660 $adj = $this->mCountAdjustment
;
1663 $u = new LinksUpdate( $id, $title );
1664 array_push( $wgDeferredUpdateList, $u );
1665 $u = new SiteStatsUpdate( 0, 1, $adj );
1666 array_push( $wgDeferredUpdateList, $u );
1667 $u = new SearchUpdate( $id, $title, $text );
1668 array_push( $wgDeferredUpdateList, $u );
1670 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1671 array_push( $wgDeferredUpdateList, $u );
1673 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1674 $wgMessageCache->replace( $shortTitle, $text );
1679 /* private */ function setOldSubtitle()
1681 global $wgLang, $wgOut;
1683 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1684 $r = wfMsg( 'revisionasof', $td );
1685 $wgOut->setSubtitle( "({$r})" );
1688 # This function is called right before saving the wikitext,
1689 # so we can do things like signatures and links-in-context.
1691 function preSaveTransform( $text )
1693 global $wgParser, $wgUser;
1694 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1697 /* Caching functions */
1699 # checkLastModified returns true if it has taken care of all
1700 # output to the client that is necessary for this request.
1701 # (that is, it has sent a cached version of the page)
1702 function tryFileCache() {
1703 static $called = false;
1705 wfDebug( " tryFileCache() -- called twice!?\n" );
1709 if($this->isFileCacheable()) {
1710 $touched = $this->mTouched
;
1711 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( 'mainpage' ) ) {
1712 # Expire the main page quicker
1713 $expire = wfUnix2Timestamp( time() - 3600 );
1714 $touched = max( $expire, $touched );
1716 $cache = new CacheManager( $this->mTitle
);
1717 if($cache->isFileCacheGood( $touched )) {
1719 wfDebug( " tryFileCache() - about to load\n" );
1720 $cache->loadFromFileCache();
1723 wfDebug( " tryFileCache() - starting buffer\n" );
1724 ob_start( array(&$cache, 'saveToFileCache' ) );
1727 wfDebug( " tryFileCache() - not cacheable\n" );
1731 function isFileCacheable() {
1732 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1733 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1735 return $wgUseFileCache
1736 and (!$wgShowIPinHeader)
1737 and ($this->getID() != 0)
1738 and ($wgUser->getId() == 0)
1739 and (!$wgUser->getNewtalk())
1740 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1741 and ($action == 'view' ||
empty( $action ))
1742 and (!isset($oldid))
1744 and (!isset($redirect))
1745 and (!isset($printable))
1746 and (!$this->mRedirectedFrom
);
1749 # Loads cur_touched and returns a value indicating if it should be used
1750 function checkTouched() {
1751 $fname = 'Article::checkTouched';
1753 $id = $this->getID();
1754 $dbr =& wfGetDB( DB_SLAVE
);
1755 $s = $dbr->getArray( 'cur', array( 'cur_touched', 'cur_is_redirect' ),
1756 array( 'cur_id' => $id ), $fname );
1757 if( $s !== false ) {
1758 $this->mTouched
= $s->cur_touched
;
1759 return !$s->cur_is_redirect
;
1765 # Edit an article without doing all that other stuff
1766 function quickEdit( $text, $comment = '', $minor = 0 ) {
1767 global $wgUser, $wgMwRedir;
1768 $fname = 'Article::quickEdit';
1769 wfProfileIn( $fname );
1771 $dbw =& wfGetDB( DB_MASTER
);
1772 $ns = $this->mTitle
->getNamespace();
1773 $dbkey = $this->mTitle
->getDBkey();
1774 $encDbKey = $dbw->strencode( $dbkey );
1775 $timestamp = wfTimestampNow();
1778 $dbw->insertSelect( 'old', 'cur',
1780 'old_namespace' => 'cur_namespace',
1781 'old_title' => 'cur_title',
1782 'old_text' => 'cur_text',
1783 'old_comment' => 'cur_comment',
1784 'old_user' => 'cur_user',
1785 'old_user_text' => 'cur_user_text',
1786 'old_timestamp' => 'cur_timestamp',
1787 'inverse_timestamp' => '99999999999999-cur_timestamp',
1789 'cur_namespace' => $ns,
1790 'cur_title' => $dbkey,
1794 # Use the affected row count to determine if the article is new
1795 $numRows = $dbw->affectedRows();
1797 # Make an array of fields to be inserted
1799 'cur_text' => $text,
1800 'cur_timestamp' => $timestamp,
1801 'cur_user' => $wgUser->getID(),
1802 'cur_user_text' => $wgUser->getName(),
1803 'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
1804 'cur_comment' => $comment,
1805 'cur_is_redirect' => $wgMwRedir->matchStart( $text ) ?
1 : 0,
1806 'cur_minor_edit' => intval($minor),
1807 'cur_touched' => $timestamp,
1812 $fields['cur_is_new'] = 0;
1813 $dbw->updateArray( 'cur', $fields, array( 'cur_namespace' => $ns, 'cur_title' => $dbkey ), $fname );
1815 # Insert new article
1816 $fields['cur_is_new'] = 1;
1817 $fields['cur_namespace'] = $ns;
1818 $fields['cur_title'] = $dbkey;
1819 $fields['cur_random'] = $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1820 $dbw->insertArray( 'cur', $fields, $fname );
1822 wfProfileOut( $fname );
1825 /* static */ function incViewCount( $id )
1827 $id = intval( $id );
1828 global $wgHitcounterUpdateFreq;
1830 $dbw =& wfGetDB( DB_MASTER
);
1831 $curTable = $dbw->tableName( 'cur' );
1832 $hitcounterTable = $dbw->tableName( 'hitcounter' );
1833 $acchitsTable = $dbw->tableName( 'acchits' );
1835 if( $wgHitcounterUpdateFreq <= 1 ){ //
1836 $dbw->query( "UPDATE $curTable SET cur_counter = cur_counter + 1 WHERE cur_id = $id" );
1840 # Not important enough to warrant an error page in case of failure
1841 $oldignore = $dbw->setIgnoreErrors( true );
1843 $dbw->query( "INSERT INTO $hitcounterTable (hc_id) VALUES ({$id})" );
1845 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1846 if( (rand() %
$checkfreq != 0) or ($dbw->lastErrno() != 0) ){
1847 # Most of the time (or on SQL errors), skip row count check
1848 $dbw->setIgnoreErrors( $oldignore );
1852 $res = $dbw->query("SELECT COUNT(*) as n FROM $hitcounterTable");
1853 $row = $dbw->fetchObject( $res );
1854 $rown = intval( $row->n
);
1855 if( $rown >= $wgHitcounterUpdateFreq ){
1856 wfProfileIn( 'Article::incViewCount-collect' );
1857 $old_user_abort = ignore_user_abort( true );
1859 $dbw->query("LOCK TABLES $hitcounterTable WRITE");
1860 $dbw->query("CREATE TEMPORARY TABLE $acchitsTable TYPE=HEAP ".
1861 "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable ".
1863 $dbw->query("DELETE FROM $hitcounterTable");
1864 $dbw->query('UNLOCK TABLES');
1865 $dbw->query("UPDATE $curTable,$acchitsTable SET cur_counter=cur_counter + hc_n ".
1866 'WHERE cur_id = hc_id');
1867 $dbw->query("DROP TABLE $acchitsTable");
1869 ignore_user_abort( $old_user_abort );
1870 wfProfileOut( 'Article::incViewCount-collect' );
1872 $dbw->setIgnoreErrors( $oldignore );
1875 # The onArticle*() functions are supposed to be a kind of hooks
1876 # which should be called whenever any of the specified actions
1879 # This is a good place to put code to clear caches, for instance.
1881 # This is called on page move and undelete, as well as edit
1882 /* static */ function onArticleCreate($title_obj){
1883 global $wgUseSquid, $wgDeferredUpdateList;
1885 $titles = $title_obj->getBrokenLinksTo();
1888 if ( $wgUseSquid ) {
1889 $urls = $title_obj->getSquidURLs();
1890 foreach ( $titles as $linkTitle ) {
1891 $urls[] = $linkTitle->getInternalURL();
1893 $u = new SquidUpdate( $urls );
1894 array_push( $wgDeferredUpdateList, $u );
1897 # Clear persistent link cache
1898 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1901 /* static */ function onArticleDelete($title_obj){
1902 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1905 /* static */ function onArticleEdit($title_obj){
1906 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1909 # Info about this page
1913 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgAllowPageInfo;
1914 $fname = 'Article::info';
1916 if ( !$wgAllowPageInfo ) {
1917 $wgOut->errorpage( "nosuchaction", "nosuchactiontext" );
1921 $dbr =& wfGetDB( DB_SLAVE
);
1923 $basenamespace = $wgTitle->getNamespace() & (~
1);
1924 $cur_clause = array( 'cur_title' => $wgTitle->getDBkey(), 'cur_namespace' => $basenamespace );
1925 $old_clause = array( 'old_title' => $wgTitle->getDBkey(), 'old_namespace' => $basenamespace );
1926 $wl_clause = array( 'wl_title' => $wgTitle->getDBkey(), 'wl_namespace' => $basenamespace );
1927 $fullTitle = $wgTitle->makeName($basenamespace, $wgTitle->getDBKey());
1928 $wgOut->setPagetitle( $fullTitle );
1929 $wgOut->setSubtitle( wfMsg( "infosubtitle" ));
1931 # first, see if the page exists at all.
1932 $exists = $dbr->selectField( 'cur', 'COUNT(*)', $cur_clause, $fname );
1934 $wgOut->addHTML( wfMsg("noarticletext") );
1936 $numwatchers = $dbr->selectField( 'watchlist', 'COUNT(*)', $wl_clause, $fname );
1937 $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers", $numwatchers) . "</li>" );
1938 $old = $dbr->selectField( 'old', 'COUNT(*)', $old_clause, $fname );
1939 $wgOut->addHTML( "<li>" . wfMsg("numedits", $old +
1) . "</li>");
1941 # to find number of distinct authors, we need to do some
1942 # funny stuff because of the cur/old table split:
1943 # - first, find the name of the 'cur' author
1944 # - then, find the number of *other* authors in 'old'
1947 $cur_author = $dbr->selectField( 'cur', 'cur_user_text', $cur_clause, $fname );
1949 # find number of 'old' authors excluding 'cur' author
1950 $authors = $dbr->selectField( 'old', 'COUNT(DISTINCT old_user_text)',
1951 $old_clause +
array( 'old_user_text<>' . $dbr->addQuotes( $cur_author ) ), $fname ) +
1;
1953 # now for the Talk page ...
1954 $cur_clause = array( 'cur_title' => $wgTitle->getDBkey(), 'cur_namespace' => $basenamespace+
1 );
1955 $old_clause = array( 'old_title' => $wgTitle->getDBkey(), 'old_namespace' => $basenamespace+
1 );
1958 $exists = $dbr->selectField( 'cur', 'COUNT(*)', $cur_clause, $fname );
1962 $old = $dbr->selectField( 'old', 'COUNT(*)', $old_clause, $fname );
1963 $wgOut->addHTML( "<li>" . wfMsg("numtalkedits", $old +
1) . "</li>");
1965 $wgOut->addHTML( "<li>" . wfMsg("numauthors", $authors) . "</li>" );
1969 $cur_author = $dbr->selectField( 'cur', 'cur_user_text', $cur_clause, $fname );
1970 $authors = $dbr->selectField( 'cur', 'COUNT(DISTINCT old_user_text)',
1971 $old_clause +
array( 'old_user_text<>' . $dbr->addQuotes( $cur_author ) ), $fname );
1973 $wgOut->addHTML( "<li>" . wfMsg("numtalkauthors", $authors) . "</li></ul>" );