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' );
12 $wgArticleCurContentFields = false;
13 $wgArticleOldContentFields = false;
16 /* private */ var $mContent, $mContentLoaded;
17 /* private */ var $mUser, $mTimestamp, $mUserText;
18 /* private */ var $mCounter, $mComment, $mCountAdjustment;
19 /* private */ var $mMinorEdit, $mRedirectedFrom;
20 /* private */ var $mTouched, $mFileCache, $mTitle;
21 /* private */ var $mId, $mTable;
23 function Article( &$title ) {
24 $this->mTitle
=& $title;
28 /* private */ function clear()
30 $this->mContentLoaded
= false;
31 $this->mCurID
= $this->mUser
= $this->mCounter
= -1; # Not loaded
32 $this->mRedirectedFrom
= $this->mUserText
=
33 $this->mTimestamp
= $this->mComment
= $this->mFileCache
= '';
34 $this->mCountAdjustment
= 0;
35 $this->mTouched
= '19700101000000';
38 # Get revision text associated with an old or archive row
39 # $row is usually an object from wfFetchRow(), both the flags and the text field must be included
40 /* static */ function getRevisionText( $row, $prefix = 'old_' ) {
42 $textField = $prefix . 'text';
43 $flagsField = $prefix . 'flags';
45 if ( isset( $row->$flagsField ) ) {
46 $flags = explode( ",", $row->$flagsField );
51 if ( isset( $row->$textField ) ) {
52 $text = $row->$textField;
57 if ( in_array( 'link', $flags ) ) {
59 $text = Article
::followLink( $text );
60 } elseif ( in_array( 'gzip', $flags ) ) {
61 # Deal with optional compression of archived pages.
62 # This can be done periodically via maintenance/compressOld.php, and
63 # as pages are saved if $wgCompressRevisions is set.
64 return gzinflate( $text );
69 /* static */ function compressRevisionText( &$text ) {
70 global $wgCompressRevisions;
71 if( !$wgCompressRevisions ) {
74 if( !function_exists( 'gzdeflate' ) ) {
75 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
78 $text = gzdeflate( $text );
82 # Returns the text associated with a "link" type old table row
83 /* static */ function followLink( $link ) {
84 # Split the link into fields and values
85 $lines = explode( '\n', $link );
88 foreach ( $lines as $line ) {
90 if ( $line{0} == '#' ) {
94 if ( preg_match( '/^(.*?)\s*:\s*(.*)$/', $line, $matches ) ) {
95 $field = strtolower($matches[1]);
97 if ( $field == 'hash' ) {
99 } elseif ( $field == 'location' ) {
100 $locations[] = $value;
105 if ( $hash === '' ) {
109 # Look in each specified location for the text
111 foreach ( $locations as $location ) {
112 $text = Article
::fetchFromLocation( $location, $hash );
113 if ( $text !== false ) {
121 /* static */ function fetchFromLocation( $location, $hash ) {
122 global $wgLoadBalancer;
123 $fname = 'fetchFromLocation';
124 wfProfileIn( $fname );
126 $p = strpos( $location, ':' );
127 if ( $p === false ) {
128 wfProfileOut( $fname );
132 $type = substr( $location, 0, $p );
136 # MySQL locations are specified by mysql://<machineID>/<dbname>/<tblname>/<index>
137 # Machine ID 0 is the current connection
138 if ( preg_match( '/^mysql:\/\/(\d+)\/([A-Za-z_]+)\/([A-Za-z_]+)\/([A-Za-z_]+)$/',
139 $location, $matches ) ) {
140 $machineID = $matches[1];
141 $dbName = $matches[2];
142 $tblName = $matches[3];
143 $index = $matches[4];
144 if ( $machineID == 0 ) {
146 $db =& wfGetDB( DB_READ
);
148 # Alternate connection
149 $db =& $wgLoadBalancer->getConnection( $machineID );
151 if ( array_key_exists( $machineId, $wgKnownMysqlServers ) ) {
152 # Try to open, return false on failure
153 $params = $wgKnownDBServers[$machineId];
154 $db = Database
::newFromParams( $params['server'], $params['user'], $params['password'],
155 $dbName, 1, false, true, true );
158 if ( $db->isOpen() ) {
159 $index = $db->strencode( $index );
160 $res = $db->query( "SELECT blob_data FROM $dbName.$tblName WHERE blob_index='$index'", $fname );
161 $row = $db->fetchObject( $res );
162 $text = $row->text_data
;
167 # File locations are of the form file://<filename>, relative to the current directory
168 if ( preg_match( '/^file:\/\/(.*)$', $location, $matches ) )
169 $filename = strstr( $location, 'file://' );
170 $text = @file_get_contents
( $matches[1] );
172 if ( $text !== false ) {
173 # Got text, now we need to interpret it
174 # The first line contains information about how to do this
175 $p = strpos( $text, '\n' );
176 $type = substr( $text, 0, $p );
177 $text = substr( $text, $p +
1 );
182 $text = gzinflate( $text );
185 $object = unserialize( $text );
186 $text = $object->getItem( $hash );
192 wfProfileOut( $fname );
196 # Note that getContent/loadContent may follow redirects if
197 # not told otherwise, and so may cause a change to mTitle.
199 # Return the text of this revision
200 function getContent( $noredir )
204 # Get variables from query string :P
205 $action = $wgRequest->getText( 'action', 'view' );
206 $section = $wgRequest->getText( 'section' );
208 $fname = 'Article::getContent';
209 wfProfileIn( $fname );
211 if ( 0 == $this->getID() ) {
212 if ( 'edit' == $action ) {
213 wfProfileOut( $fname );
214 return ''; # was "newarticletext", now moved above the box)
216 wfProfileOut( $fname );
217 return wfMsg( 'noarticletext' );
219 $this->loadContent( $noredir );
222 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
223 ( $this->mTitle
->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
224 preg_match('/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/',$this->mTitle
->getText()) &&
228 wfProfileOut( $fname );
229 return $this->mContent
. "\n" .wfMsg('anontalkpagetext'); }
231 if($action=='edit') {
233 if($section=='new') {
234 wfProfileOut( $fname );
238 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
239 # comments to be stripped as well)
240 $rv=$this->getSection($this->mContent
,$section);
241 wfProfileOut( $fname );
245 wfProfileOut( $fname );
246 return $this->mContent
;
251 # This function returns the text of a section, specified by a number ($section).
252 # A section is text under a heading like == Heading == or <h1>Heading</h1>, or
253 # the first section before any such heading (section 0).
255 # If a section contains subsections, these are also returned.
257 function getSection($text,$section) {
259 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
260 # comments to be stripped as well)
262 $parser=new Parser();
263 $parser->mOutputType
=OT_WIKI
;
264 $striptext=$parser->strip($text, $striparray, true);
266 # now that we can be sure that no pseudo-sections are in the source,
267 # split it up by section
270 '/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
272 PREG_SPLIT_DELIM_CAPTURE
);
276 $headline=$secs[$section*2-1];
277 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
280 # translate wiki heading into level
281 if(strpos($hlevel,'=')!==false) {
282 $hlevel=strlen($hlevel);
285 $rv=$headline. $secs[$section*2];
289 while(!empty($secs[$count*2-1]) && !$break) {
291 $subheadline=$secs[$count*2-1];
292 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
293 $subhlevel=$matches[1];
294 if(strpos($subhlevel,'=')!==false) {
295 $subhlevel=strlen($subhlevel);
297 if($subhlevel > $hlevel) {
298 $rv.=$subheadline.$secs[$count*2];
300 if($subhlevel <= $hlevel) {
307 # reinsert stripped tags
308 $rv=$parser->unstrip($rv,$striparray);
309 $rv=$parser->unstripNoWiki($rv,$striparray);
315 function &getCurContentFields() {
316 global $wgArticleCurContentFields;
317 if ( !$wgArticleCurContentFields ) {
318 $wgArticleCurContentFields = array( 'cur_text','cur_timestamp','cur_user', 'cur_user_text',
319 'cur_comment','cur_counter','cur_restrictions','cur_touched' );
321 return $wgArticleCurContentFields;
324 function &getOldContentFields() {
325 global $wgArticleOldContentFields;
326 if ( !$wgArticleOldContentFields ) {
327 $wgArticleOldContentFields = array( 'old_namespace','old_title','old_text','old_timestamp',
328 'old_user','old_user_text','old_comment','old_flags' );
330 return $wgArticleOldContentFields;
333 # Load the revision (including cur_text) into this object
334 function loadContent( $noredir = false )
336 global $wgOut, $wgMwRedir, $wgRequest;
338 $dbr =& wfGetDB( DB_READ
);
340 $oldid = $wgRequest->getVal( 'oldid' );
341 $redirect = $wgRequest->getVal( 'redirect' );
343 if ( $this->mContentLoaded
) return;
344 $fname = 'Article::loadContent';
346 # Pre-fill content with error message so that if something
347 # fails we'll have something telling us what we intended.
349 $t = $this->mTitle
->getPrefixedText();
350 if ( isset( $oldid ) ) {
351 $oldid = IntVal( $oldid );
352 $t .= ",oldid={$oldid}";
354 if ( isset( $redirect ) ) {
355 $redirect = ($redirect == 'no') ?
'no' : 'yes';
356 $t .= ",redirect={$redirect}";
358 $this->mContent
= wfMsg( 'missingarticle', $t );
360 if ( ! $oldid ) { # Retrieve current version
361 $id = $this->getID();
362 if ( 0 == $id ) return;
364 $s = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $id ), $fname );
365 if ( $s === false ) {
369 # If we got a redirect, follow it (unless we've been told
370 # not to by either the function parameter or the query
371 if ( ( 'no' != $redirect ) && ( false == $noredir ) &&
372 ( $wgMwRedir->matchStart( $s->cur_text
) ) ) {
373 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
374 $s->cur_text
, $m ) ) {
375 $rt = Title
::newFromText( $m[1] );
377 # Gotta hand redirects to special pages differently:
378 # Fill the HTTP response "Location" header and ignore
379 # the rest of the page we're on.
381 if ( $rt->getInterwiki() != '' ) {
382 $wgOut->redirect( $rt->getFullURL() ) ;
385 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
386 $wgOut->redirect( $rt->getFullURL() );
389 $rid = $rt->getArticleID();
391 $redirRow = $dbr->getArray( 'cur', $this->getContentFields(), array( 'cur_id' => $rid ), $fname );
393 if ( $redirRow !== false ) {
394 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
403 $this->mContent
= $s->cur_text
;
404 $this->mUser
= $s->cur_user
;
405 $this->mUserText
= $s->cur_user_text
;
406 $this->mComment
= $s->cur_comment
;
407 $this->mCounter
= $s->cur_counter
;
408 $this->mTimestamp
= $s->cur_timestamp
;
409 $this->mTouched
= $s->cur_touched
;
410 $this->mTitle
->mRestrictions
= explode( ',', trim( $s->cur_restrictions
) );
411 $this->mTitle
->mRestrictionsLoaded
= true;
412 } else { # oldid set, retrieve historical version
413 $s = $dbr->getArray( 'old', $this->getOldContentFields(), array( 'old_id' => $oldid ), $fname );
414 if ( $s === false ) {
418 if( $this->mTitle
->getNamespace() != $s->old_namespace ||
419 $this->mTitle
->getDBkey() != $s->old_title
) {
420 $oldTitle = Title
::makeTitle( $s->old_namesapce
, $s->old_title
);
421 $this->mTitle
= $oldTitle;
422 $wgTitle = $oldTitle;
424 $this->mContent
= Article
::getRevisionText( $s );
425 $this->mUser
= $s->old_user
;
426 $this->mUserText
= $s->old_user_text
;
427 $this->mComment
= $s->old_comment
;
429 $this->mTimestamp
= $s->old_timestamp
;
431 $this->mContentLoaded
= true;
432 return $this->mContent
;
435 # Gets the article text without using so many damn globals
436 # Returns false on error
437 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
440 if ( $this->mContentLoaded
) {
441 return $this->mContent
;
443 $this->mContent
= false;
445 $fname = 'Article::getContentWithout';
446 $dbr =& wfGetDB( DB_READ
);
448 if ( ! $oldid ) { # Retrieve current version
449 $id = $this->getID();
454 $s = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $id ), $fname );
455 if ( $s === false ) {
459 # If we got a redirect, follow it (unless we've been told
460 # not to by either the function parameter or the query
461 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text
) ) {
462 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/',
463 $s->cur_text
, $m ) ) {
464 $rt = Title
::newFromText( $m[1] );
465 if( $rt && $rt->getInterwiki() == '' && $rt->getNamespace() != Namespace::getSpecial() ) {
466 $rid = $rt->getArticleID();
468 $redirRow = $dbr->getArray( 'cur', $this->getCurContentFields(), array( 'cur_id' => $rid ), $fname );
470 if ( $redirRow !== false ) {
471 $this->mRedirectedFrom
= $this->mTitle
->getPrefixedText();
480 $this->mContent
= $s->cur_text
;
481 $this->mUser
= $s->cur_user
;
482 $this->mUserText
= $s->cur_user_text
;
483 $this->mComment
= $s->cur_comment
;
484 $this->mCounter
= $s->cur_counter
;
485 $this->mTimestamp
= $s->cur_timestamp
;
486 $this->mTouched
= $s->cur_touched
;
487 $this->mTitle
->mRestrictions
= explode( ",", trim( $s->cur_restrictions
) );
488 $this->mTitle
->mRestrictionsLoaded
= true;
489 } else { # oldid set, retrieve historical version
490 $s = $dbr->getArray( 'old', $this->getOldContentFields(), array( 'old_id' => $oldid ) );
491 if ( $s === false ) {
494 $this->mContent
= Article
::getRevisionText( $s );
495 $this->mUser
= $s->old_user
;
496 $this->mUserText
= $s->old_user_text
;
497 $this->mComment
= $s->old_comment
;
499 $this->mTimestamp
= $s->old_timestamp
;
501 $this->mContentLoaded
= true;
502 return $this->mContent
;
506 if( $this->mTitle
) {
507 return $this->mTitle
->getArticleID();
515 if ( -1 == $this->mCounter
) {
516 $id = $this->getID();
517 $dbr =& wfGetDB( DB_READ
);
518 $this->mCounter
= $dbr->getField( 'cur', 'cur_counter', "cur_id={$id}" );
520 return $this->mCounter
;
523 # Would the given text make this article a "good" article (i.e.,
524 # suitable for including in the article count)?
526 function isCountable( $text )
528 global $wgUseCommaCount, $wgMwRedir;
530 if ( 0 != $this->mTitle
->getNamespace() ) { return 0; }
531 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
532 $token = ($wgUseCommaCount ?
',' : '[[' );
533 if ( false === strstr( $text, $token ) ) { return 0; }
537 # Loads everything from cur except cur_text
538 # This isn't necessary for all uses, so it's only done if needed.
540 /* private */ function loadLastEdit()
543 if ( -1 != $this->mUser
) return;
545 $fname = 'Article::loadLastEdit';
547 $dbr =& wfGetDB( DB_READ
);
548 $s = $dbr->getArray( 'cur',
549 array( 'cur_user','cur_user_text','cur_timestamp', 'cur_comment','cur_minor_edit' ),
550 array( 'cur_id' => $this->getID() ), $fname );
552 if ( $s !== false ) {
553 $this->mUser
= $s->cur_user
;
554 $this->mUserText
= $s->cur_user_text
;
555 $this->mTimestamp
= $s->cur_timestamp
;
556 $this->mComment
= $s->cur_comment
;
557 $this->mMinorEdit
= $s->cur_minor_edit
;
561 function getTimestamp()
563 $this->loadLastEdit();
564 return $this->mTimestamp
;
569 $this->loadLastEdit();
573 function getUserText()
575 $this->loadLastEdit();
576 return $this->mUserText
;
579 function getComment()
581 $this->loadLastEdit();
582 return $this->mComment
;
585 function getMinorEdit()
587 $this->loadLastEdit();
588 return $this->mMinorEdit
;
591 function getContributors($limit = 0, $offset = 0)
593 $fname = 'Article::getContributors';
595 # XXX: this is expensive; cache this info somewhere.
597 $title = $this->mTitle
;
599 $dbr = wfGetDB( DB_READ
);
600 $oldTable = $dbr->tableName( 'old' );
601 $userTable = $dbr->tableName( 'user' );
602 $encDBkey = $dbr->strencode( $title->getDBkey() );
603 $ns = $title->getNamespace();
604 $user = $this->getUser();
606 $sql = "SELECT old_user, old_user_text, user_real_name, MAX(old_timestamp) as timestamp
607 FROM $oldTable LEFT JOIN $userTable ON old_user = user_id
608 WHERE old_namespace = $user
609 AND old_title = $encDBkey
610 AND old_user != $user
612 ORDER BY timestamp DESC";
615 $sql .= ' LIMIT '.$limit;
618 $res = $dbr->query($sql, $fname);
620 while ( $line = $dbr->fetchObject( $res ) ) {
621 $contribs[] = array($line->old_user
, $line->old_user_text
, $line->user_real_name
);
624 $dbr->freeResult($res);
629 # This is the default action of the script: just view the page of
634 global $wgUser, $wgOut, $wgLang, $wgRequest;
635 global $wgLinkCache, $IP, $wgEnableParserCache;
637 $fname = 'Article::view';
638 wfProfileIn( $fname );
640 # Get variables from query string :P
641 $oldid = $wgRequest->getVal( 'oldid' );
642 $diff = $wgRequest->getVal( 'diff' );
644 $wgOut->setArticleFlag( true );
645 $wgOut->setRobotpolicy( 'index,follow' );
647 # If we got diff and oldid in the query, we want to see a
648 # diff page instead of the article.
650 if ( !is_null( $diff ) ) {
651 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
652 $de = new DifferenceEngine( intval($oldid), intval($diff) );
654 wfProfileOut( $fname );
658 if ( !is_null( $oldid ) and $this->checkTouched() ) {
659 if( $wgOut->checkLastModified( $this->mTouched
) ){
661 } else if ( $this->tryFileCache() ) {
662 # tell wgOut that output is taken care of
664 $this->viewUpdates();
669 # Should the parser cache be used?
670 if ( $wgEnableParserCache && intval($wgUser->getOption( 'stubthreshold' )) == 0 && empty( $oldid ) ) {
678 if ( $wgOut->tryParserCache( $this, $wgUser ) ) {
683 if ( !$outputDone ) {
684 $text = $this->getContent( false ); # May change mTitle by following a redirect
686 # Another whitelist check in case oldid or redirects are altering the title
687 if ( !$this->mTitle
->userCanRead() ) {
688 $wgOut->loginToUse();
694 # We're looking at an old revision
696 if ( !empty( $oldid ) ) {
697 $this->setOldSubtitle();
698 $wgOut->setRobotpolicy( 'noindex,follow' );
700 if ( '' != $this->mRedirectedFrom
) {
701 $sk = $wgUser->getSkin();
702 $redir = $sk->makeKnownLink( $this->mRedirectedFrom
, '',
704 $s = wfMsg( 'redirectedfrom', $redir );
705 $wgOut->setSubtitle( $s );
707 # Can't cache redirects
711 $wgLinkCache->preFill( $this->mTitle
);
713 # wrap user css and user js in pre and don't parse
714 # XXX: use $this->mTitle->usCssJsSubpage() when php is fixed/ a workaround is found
716 $this->mTitle
->getNamespace() == Namespace::getUser() &&
717 preg_match('/\\/[\\w]+\\.(css|js)$/', $this->mTitle
->getDBkey())
719 $wgOut->addWikiText( wfMsg('clearyourcache'));
720 $wgOut->addHTML( '<pre>'.htmlspecialchars($this->mContent
)."\n</pre>" );
721 } else if ( $pcache ) {
722 $wgOut->addWikiText( $text, true, $this );
724 $wgOut->addWikiText( $text );
727 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
729 # Add link titles as META keywords
730 $wgOut->addMetaTags() ;
732 $this->viewUpdates();
733 wfProfileOut( $fname );
736 # Theoretically we could defer these whole insert and update
737 # functions for after display, but that's taking a big leap
738 # of faith, and we want to be able to report database
739 # errors at some point.
741 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
743 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
744 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
746 $fname = 'Article::insertNewArticle';
748 $this->mCountAdjustment
= $this->isCountable( $text );
750 $ns = $this->mTitle
->getNamespace();
751 $ttl = $this->mTitle
->getDBkey();
752 $text = $this->preSaveTransform( $text );
753 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
756 $now = wfTimestampNow();
757 $won = wfInvertTimestamp( $now );
759 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
760 $dbw =& wfGetDB( DB_WRITE
);
762 $cur_id = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
764 $isminor = ( $isminor && $wgUser->getID() ) ?
1 : 0;
766 $dbw->insertArray( 'cur', array(
768 'cur_namespace' => $ns,
771 'cur_comment' => $summary,
772 'cur_user' => $wgUser->getID(),
773 'cur_timestamp' => $now,
774 'cur_minor_edit' => $isminor,
776 'cur_restrictions' => '',
777 'cur_user_text' => $wgUser->getName(),
778 'cur_is_redirect' => $redir,
780 'cur_random' => $rand,
781 'cur_touched' => $now,
782 'inverse_timestamp' => $won,
785 $newid = $dbw->insertId();
786 $this->mTitle
->resetArticleID( $newid );
788 Article
::onArticleCreate( $this->mTitle
);
789 RecentChange
::notifyNew( $now, $this->mTitle
, $isminor, $wgUser, $summary );
792 if(!$this->mTitle
->userIsWatching()) $this->watch();
794 if ( $this->mTitle
->userIsWatching() ) {
799 # The talk page isn't in the regular link tables, so we need to update manually:
800 $talkns = $ns ^
1; # talk -> normal; normal -> talk
801 $dbw->updateArray( 'cur', array( 'cur_touched' => $now ), array( 'cur_namespace' => $talkns, 'cur_title' => $ttl ), $fname );
803 # standard deferred updates
804 $this->editUpdates( $text );
806 $this->showArticle( $text, wfMsg( 'newarticle' ) );
810 /* Side effects: loads last edit */
811 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ''){
812 $this->loadLastEdit();
813 $oldtext = $this->getContent( true );
814 if ($section != '') {
815 if($section=='new') {
816 if($summary) $subject="== {$summary} ==\n\n";
817 $text=$oldtext."\n\n".$subject.$text;
820 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
821 # comments to be stripped as well)
823 $parser=new Parser();
824 $parser->mOutputType
=OT_WIKI
;
825 $oldtext=$parser->strip($oldtext, $striparray, true);
827 # now that we can be sure that no pseudo-sections are in the source,
829 # Unfortunately we can't simply do a preg_replace because that might
830 # replace the wrong section, so we have to use the section counter instead
831 $secs=preg_split('/(^=+.*?=+|^<h[1-6].*?' . '>.*?<\/h[1-6].*?' . '>)/mi',
832 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE
);
833 $secs[$section*2]=$text."\n\n"; // replace with edited
835 # section 0 is top (intro) section
838 # headline of old section - we need to go through this section
839 # to determine if there are any subsections that now need to
840 # be erased, as the mother section has been replaced with
841 # the text of all subsections.
842 $headline=$secs[$section*2-1];
843 preg_match( '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$headline,$matches);
846 # determine headline level for wikimarkup headings
847 if(strpos($hlevel,'=')!==false) {
848 $hlevel=strlen($hlevel);
851 $secs[$section*2-1]=''; // erase old headline
854 while(!empty($secs[$count*2-1]) && !$break) {
856 $subheadline=$secs[$count*2-1];
858 '/^(=+).*?=+|^<h([1-6]).*?' . '>.*?<\/h[1-6].*?' . '>/mi',$subheadline,$matches);
859 $subhlevel=$matches[1];
860 if(strpos($subhlevel,'=')!==false) {
861 $subhlevel=strlen($subhlevel);
863 if($subhlevel > $hlevel) {
864 // erase old subsections
865 $secs[$count*2-1]='';
868 if($subhlevel <= $hlevel) {
876 $text=join('',$secs);
877 # reinsert the stuff that we stripped out earlier
878 $text=$parser->unstrip($text,$striparray);
879 $text=$parser->unstripNoWiki($text,$striparray);
886 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false, $sectionanchor = '' )
888 global $wgOut, $wgUser, $wgLinkCache;
889 global $wgDBtransactions, $wgMwRedir;
890 global $wgUseSquid, $wgInternalServer;
892 $fname = 'Article::updateArticle';
895 if ( $this->mMinorEdit
) { $me1 = 1; } else { $me1 = 0; }
896 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
897 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ')[^\\n]+)/i', $text, $m ) ) {
899 $text = $m[1] . "\n"; # Remove all content but redirect
903 $text = $this->preSaveTransform( $text );
904 $dbw =& wfGetDB( DB_WRITE
);
906 # Update article, but only if changed.
908 # It's important that we either rollback or complete, otherwise an attacker could
909 # overwrite cur entries by sending precisely timed user aborts. Random bored users
910 # could conceivably have the same effect, especially if cur is locked for long periods.
911 if( $wgDBtransactions ) {
912 $dbw->query( 'BEGIN', $fname );
914 $userAbort = ignore_user_abort( true );
917 $oldtext = $this->getContent( true );
919 if ( 0 != strcmp( $text, $oldtext ) ) {
920 $this->mCountAdjustment
= $this->isCountable( $text )
921 - $this->isCountable( $oldtext );
922 $now = wfTimestampNow();
923 $won = wfInvertTimestamp( $now );
925 # First update the cur row
926 $dbw->updateArray( 'cur',
929 'cur_comment' => $summary,
930 'cur_minor_edit' => $me2,
931 'cur_user' => $wgUser->getID(),
932 'cur_timestamp' => $now,
933 'cur_user_text' => $wgUser->getName(),
934 'cur_is_redirect' => $redir,
936 'cur_touched' => $now,
937 'inverse_timestamp' => $won
938 ), array( /* WHERE */
939 'cur_id' => $this->getID(),
940 'cur_timestamp' => $this->getTimestamp()
944 if( $dbw->affectedRows() == 0 ) {
945 /* Belated edit conflict! Run away!! */
948 # Now insert the previous revision into old
950 # This overwrites $oldtext if revision compression is on
951 $flags = Article
::compressRevisionText( $oldtext );
953 $dbw->insertArray( 'old',
955 'old_id' => $dbw->nextSequenceValue( 'old_old_id_seq' ),
956 'old_namespace' => $this->mTitle
->getNamespace(),
957 'old_title' => $this->mTitle
->getDBkey(),
958 'old_text' => $oldtext,
959 'old_comment' => $this->getComment(),
960 'old_user' => $this->getUser(),
961 'old_user_text' => $this->getUserText(),
962 'old_timestamp' => $this->getTimestamp(),
963 'old_minor_edit' => $me1,
964 'inverse_timestamp' => wfInvertTimestamp( $this->getTimestamp() ),
965 'old_flags' => $flags,
969 $oldid = $dbw->insertId();
971 $bot = (int)($wgUser->isBot() ||
$forceBot);
972 RecentChange
::notifyEdit( $now, $this->mTitle
, $me2, $wgUser, $summary,
973 $oldid, $this->getTimestamp(), $bot );
974 Article
::onArticleEdit( $this->mTitle
);
978 if( $wgDBtransactions ) {
979 $dbw->query( 'COMMIT', $fname );
981 ignore_user_abort( $userAbort );
986 if (!$this->mTitle
->userIsWatching()) $this->watch();
988 if ( $this->mTitle
->userIsWatching() ) {
992 # standard deferred updates
993 $this->editUpdates( $text );
998 # Purge all articles linking here
999 if ( $this->mTitle
->getNamespace() == NS_TEMPLATE
) {
1000 $titles = $this->mTitle
->getLinksTo();
1001 Title
::touchArray( $titles );
1002 if ( $wgUseSquid ) {
1003 foreach ( $titles as $title ) {
1004 $urls[] = $title->getInternalURL();
1010 if ( $wgUseSquid ) {
1011 $urls = array_merge( $urls, $this->mTitle
->getSquidURLs() );
1012 $u = new SquidUpdate( $urls );
1016 $this->showArticle( $text, wfMsg( 'updated' ), $sectionanchor );
1021 # After we've either updated or inserted the article, update
1022 # the link tables and redirect to the new page.
1024 function showArticle( $text, $subtitle , $sectionanchor = '' )
1026 global $wgOut, $wgUser, $wgLinkCache;
1029 $wgLinkCache = new LinkCache();
1031 # Get old version of link table to allow incremental link updates
1032 $wgLinkCache->preFill( $this->mTitle
);
1033 $wgLinkCache->clear();
1035 # Now update the link cache by parsing the text
1036 $wgOut = new OutputPage();
1037 $wgOut->addWikiText( $text );
1039 if( $wgMwRedir->matchStart( $text ) )
1043 $wgOut->redirect( $this->mTitle
->getFullURL( $r ).$sectionanchor );
1046 # Add this page to my watchlist
1048 function watch( $add = true )
1050 global $wgUser, $wgOut, $wgLang;
1051 global $wgDeferredUpdateList;
1053 if ( 0 == $wgUser->getID() ) {
1054 $wgOut->errorpage( 'watchnologin', 'watchnologintext' );
1057 if ( wfReadOnly() ) {
1058 $wgOut->readOnlyPage();
1062 $wgUser->addWatch( $this->mTitle
);
1064 $wgUser->removeWatch( $this->mTitle
);
1066 $wgOut->setPagetitle( wfMsg( $add ?
'addedwatch' : 'removedwatch' ) );
1067 $wgOut->setRobotpolicy( 'noindex,follow' );
1069 $sk = $wgUser->getSkin() ;
1070 $link = $this->mTitle
->getPrefixedText();
1073 $text = wfMsg( 'addedwatchtext', $link );
1075 $text = wfMsg( 'removedwatchtext', $link );
1076 $wgOut->addWikiText( $text );
1078 $up = new UserUpdate();
1079 array_push( $wgDeferredUpdateList, $up );
1081 $wgOut->returnToMain( false );
1086 $this->watch( false );
1089 function protect( $limit = 'sysop' )
1091 global $wgUser, $wgOut, $wgRequest;
1093 if ( ! $wgUser->isSysop() ) {
1094 $wgOut->sysopRequired();
1097 if ( wfReadOnly() ) {
1098 $wgOut->readOnlyPage();
1101 $id = $this->mTitle
->getArticleID();
1103 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
1107 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
1108 $reason = $wgRequest->getText( 'wpReasonProtect' );
1111 $dbw =& wfGetDB( DB_WRITE
);
1112 $dbw->updateArray( 'cur',
1114 'cur_touched' => wfTimestampNow(),
1115 'cur_restrictions' => (string)$limit
1116 ), array( /* WHERE */
1118 ), 'Article::protect'
1121 $log = new LogPage( wfMsg( 'protectlogpage' ), wfMsg( 'protectlogtext' ) );
1122 if ( $limit === "" ) {
1123 $log->addEntry( wfMsg( 'unprotectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
1125 $log->addEntry( wfMsg( 'protectedarticle', $this->mTitle
->getPrefixedText() ), $reason );
1127 $wgOut->redirect( $this->mTitle
->getFullURL() );
1130 $reason = htmlspecialchars( wfMsg( 'protectreason' ) );
1131 return $this->confirmProtect( '', $reason, $limit );
1135 # Output protection confirmation dialog
1136 function confirmProtect( $par, $reason, $limit = 'sysop' )
1140 wfDebug( "Article::confirmProtect\n" );
1142 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1143 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1148 if ( $limit === '' ) {
1149 $wgOut->setPageTitle( wfMsg( 'confirmunprotect' ) );
1150 $wgOut->setSubtitle( wfMsg( 'unprotectsub', $sub ) );
1151 $wgOut->addWikiText( wfMsg( 'confirmunprotecttext' ) );
1152 $check = htmlspecialchars( wfMsg( 'confirmunprotect' ) );
1153 $protcom = htmlspecialchars( wfMsg( 'unprotectcomment' ) );
1154 $formaction = $this->mTitle
->escapeLocalURL( 'action=unprotect' . $par );
1156 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
1157 $wgOut->setSubtitle( wfMsg( 'protectsub', $sub ) );
1158 $wgOut->addWikiText( wfMsg( 'confirmprotecttext' ) );
1159 $check = htmlspecialchars( wfMsg( 'confirmprotect' ) );
1160 $protcom = htmlspecialchars( wfMsg( 'protectcomment' ) );
1161 $formaction = $this->mTitle
->escapeLocalURL( 'action=protect' . $par );
1164 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1167 <form id='protectconfirm' method='post' action=\"{$formaction}\">
1171 <label for='wpReasonProtect'>{$protcom}:</label>
1174 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
1182 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
1185 <label for='wpConfirmProtect'>{$check}</label>
1191 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
1197 $wgOut->returnToMain( false );
1200 function unprotect()
1202 return $this->protect( '' );
1205 # UI entry point for page deletion
1208 global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
1209 $fname = 'Article::delete';
1210 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
1211 $reason = $wgRequest->getText( 'wpReason' );
1213 # This code desperately needs to be totally rewritten
1216 if ( ( ! $wgUser->isSysop() ) ) {
1217 $wgOut->sysopRequired();
1220 if ( wfReadOnly() ) {
1221 $wgOut->readOnlyPage();
1225 # Better double-check that it hasn't been deleted yet!
1226 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
1227 if ( ( '' == trim( $this->mTitle
->getText() ) )
1228 or ( $this->mTitle
->getArticleId() == 0 ) ) {
1229 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1234 $this->doDelete( $reason );
1238 # determine whether this page has earlier revisions
1239 # and insert a warning if it does
1240 # we select the text because it might be useful below
1241 $dbr =& wfGetDB( DB_READ
);
1242 $ns = $this->mTitle
->getNamespace();
1243 $title = $this->mTitle
->getDBkey();
1244 $old = $dbr->getArray( 'old',
1245 array( 'old_text', 'old_flags' ),
1247 'old_namespace' => $ns,
1248 'old_title' => $title,
1249 ), $fname, array( 'ORDER BY' => 'inverse_timestamp' )
1252 if( $old !== false && !$confirm ) {
1253 $skin=$wgUser->getSkin();
1254 $wgOut->addHTML('<b>'.wfMsg('historywarning'));
1255 $wgOut->addHTML( $skin->historyLink() .'</b>');
1259 $s = $dbr->getArray( 'cur',
1260 array( 'cur_text' ),
1262 'cur_namespace' => $ns,
1263 'cur_title' => $title,
1267 if( $s !== false ) {
1268 # if this is a mini-text, we can paste part of it into the deletion reason
1270 #if this is empty, an earlier revision may contain "useful" text
1272 if($s->cur_text
!="") {
1276 $text = Article
::getRevisionText( $old );
1282 $length=strlen($text);
1284 # this should not happen, since it is not possible to store an empty, new
1285 # page. Let's insert a standard text in case it does, though
1286 if($length == 0 && $reason === '') {
1287 $reason = wfMsg('exblank');
1290 if($length < 500 && $reason === '') {
1292 # comment field=255, let's grep the first 150 to have some user
1294 $text=substr($text,0,150);
1295 # let's strip out newlines and HTML tags
1296 $text=preg_replace('/\"/',"'",$text);
1297 $text=preg_replace('/\</','<',$text);
1298 $text=preg_replace('/\>/','>',$text);
1299 $text=preg_replace("/[\n\r]/",'',$text);
1301 $reason=wfMsg('excontent'). " '".$text;
1303 $reason=wfMsg('exbeforeblank') . " '".$text;
1305 if($length>150) { $reason .= '...'; } # we've only pasted part of the text
1310 return $this->confirmDelete( '', $reason );
1313 # Output deletion confirmation dialog
1314 function confirmDelete( $par, $reason )
1318 wfDebug( "Article::confirmDelete\n" );
1320 $sub = htmlspecialchars( $this->mTitle
->getPrefixedText() );
1321 $wgOut->setSubtitle( wfMsg( 'deletesub', $sub ) );
1322 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1323 $wgOut->addWikiText( wfMsg( 'confirmdeletetext' ) );
1325 $formaction = $this->mTitle
->escapeLocalURL( 'action=delete' . $par );
1327 $confirm = htmlspecialchars( wfMsg( 'confirm' ) );
1328 $check = htmlspecialchars( wfMsg( 'confirmcheck' ) );
1329 $delcom = htmlspecialchars( wfMsg( 'deletecomment' ) );
1332 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1336 <label for='wpReason'>{$delcom}:</label>
1339 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1347 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1350 <label for='wpConfirm'>{$check}</label>
1356 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1362 $wgOut->returnToMain( false );
1365 # Perform a deletion and output success or failure messages
1366 function doDelete( $reason )
1368 global $wgOut, $wgUser, $wgLang;
1369 $fname = 'Article::doDelete';
1370 wfDebug( "$fname\n" );
1372 if ( $this->doDeleteArticle( $reason ) ) {
1373 $deleted = $this->mTitle
->getPrefixedText();
1375 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1376 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1378 $sk = $wgUser->getSkin();
1379 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1380 Namespace::getWikipedia() ) .
1381 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
1383 $text = wfMsg( "deletedtext", $deleted, $loglink );
1385 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
1386 $wgOut->returnToMain( false );
1388 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
1392 # Back-end article deletion
1393 # Deletes the article with database consistency, writes logs, purges caches
1395 function doDeleteArticle( $reason )
1397 global $wgUser, $wgLang;
1398 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1400 $fname = 'Article::doDeleteArticle';
1401 wfDebug( $fname."\n" );
1403 $dbw =& wfGetDB( DB_WRITE
);
1404 $ns = $this->mTitle
->getNamespace();
1405 $t = $this->mTitle
->getDBkey();
1406 $id = $this->mTitle
->getArticleID();
1408 if ( '' == $t ||
$id == 0 ) {
1412 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1413 array_push( $wgDeferredUpdateList, $u );
1415 $linksTo = $this->mTitle
->getLinksTo();
1418 if ( $wgUseSquid ) {
1420 $this->mTitle
->getInternalURL(),
1421 $this->mTitle
->getInternalURL( 'history' )
1423 foreach ( $linksTo as $linkTo ) {
1424 $urls[] = $linkTo->getInternalURL();
1427 $u = new SquidUpdate( $urls );
1428 array_push( $wgDeferredUpdateList, $u );
1432 # Client and file cache invalidation
1433 Title
::touchArray( $linksTo );
1435 # Move article and history to the "archive" table
1436 $archiveTable = $dbw->tableName( 'archive' );
1437 $oldTable = $dbw->tableName( 'old' );
1438 $curTable = $dbw->tableName( 'cur' );
1439 $recentchangesTable = $dbw->tableName( 'recentchanges' );
1440 $linksTable = $dbw->tableName( 'links' );
1441 $brokenlinksTable = $dbw->tableName( 'brokenlinks' );
1443 $dbw->insertSelect( 'archive', 'cur',
1445 'ar_namespace' => 'cur_namespace',
1446 'ar_title' => 'cur_title',
1447 'ar_text' => 'cur_text',
1448 'ar_comment' => 'cur_comment',
1449 'ar_user' => 'cur_user',
1450 'ar_user_text' => 'cur_user_text',
1451 'ar_timestamp' => 'cur_timestamp',
1452 'ar_minor_edit' => 'cur_minor_edit',
1455 'cur_namespace' => $ns,
1460 $dbw->insertSelect( 'archive', 'old',
1462 'ar_namespace' => 'old_namespace',
1463 'ar_title' => 'old_title',
1464 'ar_text' => 'old_text',
1465 'ar_comment' => 'old_comment',
1466 'ar_user' => 'old_user',
1467 'ar_user_text' => 'old_user_text',
1468 'ar_timestamp' => 'old_timestamp',
1469 'ar_minor_edit' => 'old_minor_edit',
1470 'ar_flags' => 'old_flags'
1472 'old_namespace' => $ns,
1477 # Now that it's safely backed up, delete it
1479 $dbw->delete( 'cur', array( 'cur_namespace' => $ns, 'cur_title' => $t ), $fname );
1480 $dbw->delete( 'old', array( 'old_namespace' => $ns, 'old_title' => $t ), $fname );
1481 $dbw->delete( 'recentchanges', array( 'rc_namespace' => $ns, 'rc_title' => $t ), $fname );
1483 # Finally, clean up the link tables
1484 $t = $this->mTitle
->getPrefixedDBkey();
1486 Article
::onArticleDelete( $this->mTitle
);
1488 # Insert broken links
1489 $brokenLinks = array();
1490 foreach ( $linksTo as $titleObj ) {
1491 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1492 $linkID = $titleObj->getArticleID();
1493 $brokenLinks[] = array( 'bl_from' => $linkID, 'bl_to' => $t );
1495 $dbw->insertArray( 'brokenlinks', $brokenLinks, $fname );
1498 $dbw->delete( 'links', array( 'l_to' => $id ) );
1499 $dbw->delete( 'links', array( 'l_from' => $id ) );
1500 $dbw->delete( 'imagelinks', array( 'il_from' => $id ) );
1501 $dbw->delete( 'brokenlinks', array( 'bl_from' => $id ) );
1502 $dbw->delete( 'categorylinks', array( 'cl_from' => $id ) );
1505 $log = new LogPage( wfMsg( 'dellogpage' ), wfMsg( 'dellogpagetext' ) );
1506 $art = $this->mTitle
->getPrefixedText();
1507 $log->addEntry( wfMsg( 'deletedarticle', $art ), $reason );
1509 # Clear the cached article id so the interface doesn't act like we exist
1510 $this->mTitle
->resetArticleID( 0 );
1511 $this->mTitle
->mArticleID
= 0;
1517 global $wgUser, $wgLang, $wgOut, $wgRequest;
1518 $fname = "Article::rollback";
1520 if ( ! $wgUser->isSysop() ) {
1521 $wgOut->sysopRequired();
1524 if ( wfReadOnly() ) {
1525 $wgOut->readOnlyPage( $this->getContent( true ) );
1528 $dbw =& wfGetDB( DB_WRITE
);
1530 # Enhanced rollback, marks edits rc_bot=1
1531 $bot = $wgRequest->getBool( 'bot' );
1533 # Replace all this user's current edits with the next one down
1534 $tt = $this->mTitle
->getDBKey();
1535 $n = $this->mTitle
->getNamespace();
1537 # Get the last editor, lock table exclusively
1538 $s = $dbw->getArray( 'cur',
1539 array( 'cur_id','cur_user','cur_user_text','cur_comment' ),
1540 array( 'cur_title' => $tt, 'cur_namespace' => $n ),
1541 $fname, 'FOR UPDATE'
1543 if( $s === false ) {
1545 $wgOut->addHTML( wfMsg( 'notanarticle' ) );
1548 $ut = $dbw->strencode( $s->cur_user_text
);
1549 $uid = $s->cur_user
;
1552 $from = str_replace( '_', ' ', $wgRequest->getVal( 'from' ) );
1553 if( $from != $s->cur_user_text
) {
1554 $wgOut->setPageTitle(wfmsg('rollbackfailed'));
1555 $wgOut->addWikiText( wfMsg( 'alreadyrolled',
1556 htmlspecialchars( $this->mTitle
->getPrefixedText()),
1557 htmlspecialchars( $from ),
1558 htmlspecialchars( $s->cur_user_text
) ) );
1559 if($s->cur_comment
!= '') {
1561 wfMsg('editcomment',
1562 htmlspecialchars( $s->cur_comment
) ) );
1567 # Get the last edit not by this guy
1568 $s = $dbw->getArray( 'old',
1569 array( 'old_text','old_user','old_user_text','old_timestamp','old_flags' ),
1571 'old_namespace' => $n,
1573 "old_user <> {$uid} OR old_user_text <> '{$ut}'"
1574 ), $fname, array( 'FOR UPDATE', 'USE INDEX' => 'name_title_timestamp' )
1576 if( $s === false ) {
1578 $wgOut->setPageTitle(wfMsg('rollbackfailed'));
1579 $wgOut->addHTML( wfMsg( 'cantrollback' ) );
1584 # Mark all reverted edits as bot
1585 $dbw->updateArray( 'recentchanges',
1588 ), array( /* WHERE */
1590 "rc_timestamp > '{$s->old_timestamp}'",
1596 $newcomment = wfMsg( 'revertpage', $s->old_user_text
, $from );
1597 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
1598 $wgOut->setRobotpolicy( 'noindex,nofollow' );
1599 $wgOut->addHTML( '<h2>' . $newcomment . "</h2>\n<hr />\n" );
1600 $this->updateArticle( Article
::getRevisionText( $s ), $newcomment, 1, $this->mTitle
->userIsWatching(), $bot );
1601 Article
::onArticleEdit( $this->mTitle
);
1602 $wgOut->returnToMain( false );
1606 # Do standard deferred updates after page view
1608 /* private */ function viewUpdates()
1610 global $wgDeferredUpdateList;
1611 if ( 0 != $this->getID() ) {
1612 global $wgDisableCounters;
1613 if( !$wgDisableCounters ) {
1614 Article
::incViewCount( $this->getID() );
1615 $u = new SiteStatsUpdate( 1, 0, 0 );
1616 array_push( $wgDeferredUpdateList, $u );
1618 $u = new UserTalkUpdate( 0, $this->mTitle
->getNamespace(),
1619 $this->mTitle
->getDBkey() );
1620 array_push( $wgDeferredUpdateList, $u );
1624 # Do standard deferred updates after page edit.
1625 # Every 1000th edit, prune the recent changes table.
1627 /* private */ function editUpdates( $text )
1629 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1630 global $wgMessageCache;
1633 if ( 0 == mt_rand( 0, 999 ) ) {
1634 $dbw =& wfGetDB( DB_WRITE
);
1635 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1636 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1637 $dbw->query( $sql );
1639 $id = $this->getID();
1640 $title = $this->mTitle
->getPrefixedDBkey();
1641 $shortTitle = $this->mTitle
->getDBkey();
1643 $adj = $this->mCountAdjustment
;
1646 $u = new LinksUpdate( $id, $title );
1647 array_push( $wgDeferredUpdateList, $u );
1648 $u = new SiteStatsUpdate( 0, 1, $adj );
1649 array_push( $wgDeferredUpdateList, $u );
1650 $u = new SearchUpdate( $id, $title, $text );
1651 array_push( $wgDeferredUpdateList, $u );
1653 $u = new UserTalkUpdate( 1, $this->mTitle
->getNamespace(), $shortTitle );
1654 array_push( $wgDeferredUpdateList, $u );
1656 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
1657 $wgMessageCache->replace( $shortTitle, $text );
1662 /* private */ function setOldSubtitle()
1664 global $wgLang, $wgOut;
1666 $td = $wgLang->timeanddate( $this->mTimestamp
, true );
1667 $r = wfMsg( 'revisionasof', $td );
1668 $wgOut->setSubtitle( "({$r})" );
1671 # This function is called right before saving the wikitext,
1672 # so we can do things like signatures and links-in-context.
1674 function preSaveTransform( $text )
1676 global $wgParser, $wgUser;
1677 return $wgParser->preSaveTransform( $text, $this->mTitle
, $wgUser, ParserOptions
::newFromUser( $wgUser ) );
1680 /* Caching functions */
1682 # checkLastModified returns true if it has taken care of all
1683 # output to the client that is necessary for this request.
1684 # (that is, it has sent a cached version of the page)
1685 function tryFileCache() {
1686 static $called = false;
1688 wfDebug( " tryFileCache() -- called twice!?\n" );
1692 if($this->isFileCacheable()) {
1693 $touched = $this->mTouched
;
1694 if( $this->mTitle
->getPrefixedDBkey() == wfMsg( 'mainpage' ) ) {
1695 # Expire the main page quicker
1696 $expire = wfUnix2Timestamp( time() - 3600 );
1697 $touched = max( $expire, $touched );
1699 $cache = new CacheManager( $this->mTitle
);
1700 if($cache->isFileCacheGood( $touched )) {
1702 wfDebug( " tryFileCache() - about to load\n" );
1703 $cache->loadFromFileCache();
1706 wfDebug( " tryFileCache() - starting buffer\n" );
1707 ob_start( array(&$cache, 'saveToFileCache' ) );
1710 wfDebug( " tryFileCache() - not cacheable\n" );
1714 function isFileCacheable() {
1715 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1716 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1718 return $wgUseFileCache
1719 and (!$wgShowIPinHeader)
1720 and ($this->getID() != 0)
1721 and ($wgUser->getId() == 0)
1722 and (!$wgUser->getNewtalk())
1723 and ($this->mTitle
->getNamespace() != Namespace::getSpecial())
1724 and ($action == 'view')
1725 and (!isset($oldid))
1727 and (!isset($redirect))
1728 and (!isset($printable))
1729 and (!$this->mRedirectedFrom
);
1732 # Loads cur_touched and returns a value indicating if it should be used
1733 function checkTouched() {
1734 $id = $this->getID();
1735 $dbr =& wfGetDB( DB_READ
);
1736 $s = $dbr->getArray( 'cur', array( 'cur_touched', 'cur_is_redirect' ),
1737 array( 'cur_id' => $id ), $fname );
1738 if( $s !== false ) {
1739 $this->mTouched
= $s->cur_touched
;
1740 return !$s->cur_is_redirect
;
1746 # Edit an article without doing all that other stuff
1747 function quickEdit( $text, $comment = '', $minor = 0 ) {
1748 global $wgUser, $wgMwRedir;
1749 $fname = 'Article::quickEdit';
1750 wfProfileIn( $fname );
1752 $dbw =& wfGetDB( DB_WRITE
);
1753 $ns = $this->mTitle
->getNamespace();
1754 $dbkey = $this->mTitle
->getDBkey();
1755 $encDbKey = $dbw->strencode( $dbkey );
1756 $timestamp = wfTimestampNow();
1759 $dbw->insertSelect( 'old', 'cur',
1761 'old_namespace' => 'cur_namespace',
1762 'old_title' => 'cur_title',
1763 'old_text' => 'cur_text',
1764 'old_comment' => 'cur_comment',
1765 'old_user' => 'cur_user',
1766 'old_user_text' => 'cur_user_text',
1767 'old_timestamp' => 'cur_timestamp',
1768 'inverse_timestamp' => '99999999999999-cur_timestamp',
1770 'cur_namespace' => $ns,
1771 'cur_title' => $dbkey,
1775 # Use the affected row count to determine if the article is new
1776 $numRows = $dbw->affectedRows();
1778 # Make an array of fields to be inserted
1780 'cur_text' => $text,
1781 'cur_timestamp' => $timestamp,
1782 'cur_user' => $wgUser->getID(),
1783 'cur_user_text' => $wgUser->getName(),
1784 'inverse_timestamp' => wfInvertTimestamp( $timestamp ),
1785 'cur_comment' => $comment,
1786 'cur_is_redirect' => $wgMwRedir->matchStart( $text ) ?
1 : 0,
1787 'cur_minor_edit' => intval($minor),
1788 'cur_touched' => $timestamp,
1793 $fields['cur_is_new'] = 0;
1794 $dbw->updateArray( 'cur', $fields, array( 'cur_namespace' => $ns, 'cur_title' => $dbkey ), $fname );
1796 # Insert new article
1797 $fields['cur_is_new'] = 1;
1798 $fields['cur_namespace'] = $ns;
1799 $fields['cur_title'] = $dbkey;
1800 $fields['cur_random'] = $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1801 $dbw->insertArray( 'cur', $fields, $fname );
1803 wfProfileOut( $fname );
1806 /* static */ function incViewCount( $id )
1808 $id = intval( $id );
1809 global $wgHitcounterUpdateFreq;
1811 $dbw =& wfGetDB( DB_WRITE
);
1812 $curTable = $dbw->tableName( 'cur' );
1813 $hitcounterTable = $dbw->tableName( 'hitcounter' );
1814 $acchitsTable = $dbw->tableName( 'acchits' );
1816 if( $wgHitcounterUpdateFreq <= 1 ){ //
1817 $dbw->query( "UPDATE $curTable SET cur_counter = cur_counter + 1 WHERE cur_id = $id" );
1821 # Not important enough to warrant an error page in case of failure
1822 $oldignore = $dbw->setIgnoreErrors( true );
1824 $dbw->query( "INSERT INTO $hitcounterTable (hc_id) VALUES ({$id})" );
1826 $checkfreq = intval( $wgHitcounterUpdateFreq/25 +
1 );
1827 if( (rand() %
$checkfreq != 0) or ($dbw->lastErrno() != 0) ){
1828 # Most of the time (or on SQL errors), skip row count check
1829 $dbw->setIgnoreErrors( $oldignore );
1833 $res = $dbw->query("SELECT COUNT(*) as n FROM $hitcounterTable");
1834 $row = $dbw->fetchObject( $res );
1835 $rown = intval( $row->n
);
1836 if( $rown >= $wgHitcounterUpdateFreq ){
1837 wfProfileIn( 'Article::incViewCount-collect' );
1838 $old_user_abort = ignore_user_abort( true );
1840 $dbw->query("LOCK TABLES $hitcounterTable WRITE");
1841 $dbw->query("CREATE TEMPORARY TABLE $acchitsTable TYPE=HEAP ".
1842 "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable ".
1844 $dbw->query("DELETE FROM $hitcounterTable");
1845 $dbw->query('UNLOCK TABLES');
1846 $dbw->query("UPDATE $curTable,$acchitsTable SET cur_counter=cur_counter + hc_n ".
1847 'WHERE cur_id = hc_id');
1848 $dbw->query("DROP TABLE $acchitsTable");
1850 ignore_user_abort( $old_user_abort );
1851 wfProfileOut( 'Article::incViewCount-collect' );
1853 $dbw->setIgnoreErrors( $oldignore );
1856 # The onArticle*() functions are supposed to be a kind of hooks
1857 # which should be called whenever any of the specified actions
1860 # This is a good place to put code to clear caches, for instance.
1862 # This is called on page move and undelete, as well as edit
1863 /* static */ function onArticleCreate($title_obj){
1864 global $wgUseSquid, $wgDeferredUpdateList;
1866 $titles = $title_obj->getBrokenLinksTo();
1869 if ( $wgUseSquid ) {
1870 $urls = $title_obj->getSquidURLs();
1871 foreach ( $titles as $linkTitle ) {
1872 $urls[] = $linkTitle->getInternalURL();
1874 $u = new SquidUpdate( $urls );
1875 array_push( $wgDeferredUpdateList, $u );
1878 # Clear persistent link cache
1879 LinkCache
::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1882 /* static */ function onArticleDelete($title_obj){
1883 LinkCache
::linksccClearLinksTo( $title_obj->getArticleID() );
1886 /* static */ function onArticleEdit($title_obj){
1887 LinkCache
::linksccClearPage( $title_obj->getArticleID() );
1890 # Info about this page
1894 global $wgUser, $wgTitle, $wgOut, $wgLang, $wgAllowPageInfo;
1896 if ( !$wgAllowPageInfo ) {
1897 $wgOut->errorpage( "nosuchaction", "nosuchactiontext" );
1901 $basenamespace = $wgTitle->getNamespace() & (~
1);
1902 $cur_clause = "cur_title='".$wgTitle->getDBkey()."' AND cur_namespace=".$basenamespace;
1903 $old_clause = "old_title='".$wgTitle->getDBkey()."' AND old_namespace=".$basenamespace;
1904 $wl_clause = "wl_title='".$wgTitle->getDBkey()."' AND wl_namespace=".$basenamespace;
1905 $fullTitle = $wgTitle->makeName($basenamespace, $wgTitle->getDBKey());
1906 $wgOut->setPagetitle( $fullTitle );
1907 $wgOut->setSubtitle( wfMsg( "infosubtitle" ));
1909 # first, see if the page exists at all.
1910 $sql = "SELECT COUNT(*) FROM cur WHERE ".$cur_clause;
1911 $exists = wfSingleQuery( $sql , DB_READ
);
1913 $wgOut->addHTML( wfMsg("noarticletext") );
1915 $sql = "SELECT COUNT(*) FROM watchlist WHERE ".$wl_clause;
1916 $wgOut->addHTML( "<ul><li>" . wfMsg("numwatchers") . wfSingleQuery( $sql, DB_READ
) . "</li>" );
1917 $sql = "SELECT COUNT(*) FROM old WHERE ".$old_clause;
1918 $old = wfSingleQuery( $sql, DB_READ
);
1919 $wgOut->addHTML( "<li>" . wfMsg("numedits") . ($old +
1) . "</li>");
1921 # to find number of distinct authors, we need to do some
1922 # funny stuff because of the cur/old table split:
1923 # - first, find the name of the 'cur' author
1924 # - then, find the number of *other* authors in 'old'
1927 $sql = "SELECT cur_user_text FROM cur WHERE ".$cur_clause;
1928 $cur_author = wfSingleQuery( $sql, DB_READ
);
1930 # find number of 'old' authors excluding 'cur' author
1931 $sql = "SELECT COUNT(DISTINCT old_user_text) FROM old WHERE ".$old_clause
1932 ." AND old_user_text<>'" . $cur_author . "'";
1933 $authors = wfSingleQuery( $sql, DB_READ
) +
1;
1935 # now for the Talk page ...
1936 $cur_clause = "cur_title='".$wgTitle->getDBkey()."' AND cur_namespace=".($basenamespace+
1);
1937 $old_clause = "old_title='".$wgTitle->getDBkey()."' AND old_namespace=".($basenamespace+
1);
1940 $sql = "SELECT COUNT(*) FROM cur WHERE ".$cur_clause;
1941 $exists = wfSingleQuery( $sql , DB_READ
);
1945 $sql = "SELECT COUNT(*) FROM old WHERE ".$old_clause;
1946 $old = wfSingleQuery( $sql, DB_READ
);
1947 $wgOut->addHTML( "<li>" . wfMsg("numtalkedits") . ($old +
1) . "</li>");
1949 $wgOut->addHTML( "<li>" . wfMsg("numauthors") . $authors . "</li>" );
1953 $sql = "SELECT cur_user_text FROM cur WHERE ".$cur_clause;
1954 $cur_author = wfSingleQuery( $sql, DB_READ
);
1956 $sql = "SELECT COUNT(DISTINCT old_user_text) FROM old WHERE "
1957 .$old_clause." AND old_user_text<>'" . $cur_author . "'";
1958 $authors = wfSingleQuery( $sql, DB_READ
) +
1;
1960 $wgOut->addHTML( "<li>" . wfMsg("numtalkauthors") . $authors . "</li></ul>" );