Merge "Moved RecentChange::purgeExpiredChanges to a job"
[lhc/web/wiklou.git] / includes / page / WikiPage.php
index a822945..2f900f9 100644 (file)
@@ -1145,7 +1145,6 @@ class WikiPage implements Page, IDBAccessObject {
                $pool = new PoolWorkArticleView( $this, $parserOptions, $oldid, $useParserCache );
                $pool->execute();
 
-
                return $pool->getParserOutput();
        }
 
@@ -1264,7 +1263,6 @@ class WikiPage implements Page, IDBAccessObject {
        ) {
                global $wgContentHandlerUseDB;
 
-
                $content = $revision->getContent();
                $len = $content ? $content->getSize() : 0;
                $rt = $content ? $content->getUltimateRedirectTarget() : null;
@@ -1702,7 +1700,6 @@ class WikiPage implements Page, IDBAccessObject {
                        throw new MWException( 'Something is trying to edit an article with an empty title' );
                }
 
-
                if ( !$content->getContentHandler()->canBeUsedOn( $this->getTitle() ) ) {
                        return Status::newFatal( 'content-not-allowed-here',
                                ContentHandler::getLocalizedName( $content->getModel() ),
@@ -1847,7 +1844,7 @@ class WikiPage implements Page, IDBAccessObject {
                                                }
                                        }
                                        $user->incEditCount();
-                               } catch ( MWException $e ) {
+                               } catch ( Exception $e ) {
                                        $dbw->rollback( __METHOD__ );
                                        // Question: Would it perhaps be better if this method turned all
                                        // exceptions into $status's?
@@ -1950,7 +1947,7 @@ class WikiPage implements Page, IDBAccessObject {
                                }
                                $user->incEditCount();
 
-                       } catch ( MWException $e ) {
+                       } catch ( Exception $e ) {
                                $dbw->rollback( __METHOD__ );
                                throw $e;
                        }
@@ -2032,7 +2029,8 @@ class WikiPage implements Page, IDBAccessObject {
         * Returns a stdClass with source, pst and output members
         *
         * @param Content $content
-        * @param int|null $revid
+        * @param Revision|int|null $revision Revision object. For backwards compatibility, a
+        *        revision ID is also accepted, but this is deprecated.
         * @param User|null $user
         * @param string|null $serialFormat
         * @param bool $useCache Check shared prepared edit cache
@@ -2042,9 +2040,22 @@ class WikiPage implements Page, IDBAccessObject {
         * @since 1.21
         */
        public function prepareContentForEdit(
-               Content $content, $revid = null, User $user = null, $serialFormat = null, $useCache = true
+               Content $content, $revision = null, User $user = null, $serialFormat = null, $useCache = true
        ) {
-               global $wgContLang, $wgUser;
+               global $wgContLang, $wgUser, $wgAjaxEditStash;
+
+               if ( is_object( $revision ) ) {
+                       $revid = $revision->getId();
+               } else {
+                       $revid = $revision;
+                       // This code path is deprecated, and nothing is known to
+                       // use it, so performance here shouldn't be a worry.
+                       if ( $revid !== null ) {
+                               $revision = Revision::newFromId( $revid, Revision::READ_LATEST );
+                       } else {
+                               $revision = null;
+                       }
+               }
 
                $user = is_null( $user ) ? $wgUser : $user;
                //XXX: check $user->getId() here???
@@ -2066,7 +2077,7 @@ class WikiPage implements Page, IDBAccessObject {
                }
 
                // The edit may have already been prepared via api.php?action=stashedit
-               $cachedEdit = $useCache
+               $cachedEdit = $useCache && $wgAjaxEditStash
                        ? ApiStashEdit::checkCache( $this->getTitle(), $content, $user )
                        : false;
 
@@ -2095,6 +2106,25 @@ class WikiPage implements Page, IDBAccessObject {
                if ( $cachedEdit ) {
                        $edit->output = $cachedEdit->output;
                } else {
+                       if ( $revision ) {
+                               // We get here if vary-revision is set. This means that this page references
+                               // itself (such as via self-transclusion). In this case, we need to make sure
+                               // that any such self-references refer to the newly-saved revision, and not
+                               // to the previous one, which could otherwise happen due to slave lag.
+                               $oldCallback = $edit->popts->setCurrentRevisionCallback(
+                                       function ( $title, $parser = false ) use ( $revision, &$oldCallback ) {
+                                               if ( $title->equals( $revision->getTitle() ) ) {
+                                                       return $revision;
+                                               } else {
+                                                       return call_user_func(
+                                                               $oldCallback,
+                                                               $title,
+                                                               $parser
+                                                       );
+                                               }
+                                       }
+                               );
+                       }
                        $edit->output = $edit->pstContent
                                ? $edit->pstContent->getParserOutput( $this->mTitle, $revid, $edit->popts )
                                : null;
@@ -2132,7 +2162,6 @@ class WikiPage implements Page, IDBAccessObject {
        public function doEditUpdates( Revision $revision, User $user, array $options = array() ) {
                global $wgEnableParserCache;
 
-
                $options += array(
                        'changed' => true,
                        'created' => false,
@@ -2146,7 +2175,7 @@ class WikiPage implements Page, IDBAccessObject {
                // already pre-save transformed once.
                if ( !$this->mPreparedEdit || $this->mPreparedEdit->output->getFlag( 'vary-revision' ) ) {
                        wfDebug( __METHOD__ . ": No prepared edit or vary-revision is set...\n" );
-                       $editInfo = $this->prepareContentForEdit( $content, $revision->getId(), $user );
+                       $editInfo = $this->prepareContentForEdit( $content, $revision, $user );
                } else {
                        wfDebug( __METHOD__ . ": No vary-revision, using prepared edit...\n" );
                        $editInfo = $this->mPreparedEdit;
@@ -2171,11 +2200,8 @@ class WikiPage implements Page, IDBAccessObject {
                Hooks::run( 'ArticleEditUpdates', array( &$this, &$editInfo, $options['changed'] ) );
 
                if ( Hooks::run( 'ArticleEditUpdatesDeleteFromRecentchanges', array( &$this ) ) ) {
-                       if ( 0 == mt_rand( 0, 99 ) ) {
-                               // Flush old entries from the `recentchanges` table; we do this on
-                               // random requests so as to avoid an increase in writes for no good reason
-                               RecentChange::purgeExpiredChanges();
-                       }
+                       // Flush old entries from the `recentchanges` table
+                       JobQueueGroup::singleton()->push( RecentChangesUpdateJob::newPurgeJob() );
                }
 
                if ( !$this->exists() ) {
@@ -2987,8 +3013,8 @@ class WikiPage implements Page, IDBAccessObject {
 
                // Get the last edit not by this guy...
                // Note: these may not be public values
-               $user = intval( $current->getRawUser() );
-               $user_text = $dbw->addQuotes( $current->getRawUserText() );
+               $user = intval( $current->getUser( Revision::RAW ) );
+               $user_text = $dbw->addQuotes( $current->getUserText( Revision::RAW ) );
                $s = $dbw->selectRow( 'revision',
                        array( 'rev_id', 'rev_timestamp', 'rev_deleted' ),
                        array( 'rev_page' => $current->getPage(),