Perform edit stashing when the edit preview or diff form is requested
[lhc/web/wiklou.git] / includes / api / ApiStashEdit.php
index 3539eed..67939a0 100644 (file)
@@ -41,11 +41,16 @@ class ApiStashEdit extends ApiBase {
        const ERROR_UNCACHEABLE = 'uncacheable';
 
        const PRESUME_FRESH_TTL_SEC = 30;
+       const MAX_CACHE_TTL = 300; // 5 minutes
 
        public function execute() {
                $user = $this->getUser();
                $params = $this->extractRequestParams();
 
+               if ( $user->isBot() ) { // sanity
+                       $this->dieUsage( 'This interface is not supported for bots', 'botsnotsupported' );
+               }
+
                $page = $this->getTitleOrPageId( $params );
                $title = $page->getTitle();
 
@@ -117,36 +122,41 @@ class ApiStashEdit extends ApiBase {
                if ( $user->pingLimiter( 'stashedit' ) ) {
                        $status = 'ratelimited';
                } elseif ( $dbw->lock( $key, __METHOD__, 1 ) ) {
-                       $status = self::parseAndStash( $page, $content, $user );
+                       $status = self::parseAndStash( $page, $content, $user, $params['summary'] );
                        $dbw->unlock( $key, __METHOD__ );
                } else {
                        $status = 'busy';
                }
 
+               $this->getStats()->increment( "editstash.cache_stores.$status" );
+
                $this->getResult()->addValue( null, $this->getModuleName(), [ 'status' => $status ] );
        }
 
        /**
         * @param WikiPage $page
-        * @param Content $content
+        * @param Content $content Edit content
         * @param User $user
+        * @param string $summary Edit summary
         * @return integer ApiStashEdit::ERROR_* constant
         * @since 1.25
         */
-       public static function parseAndStash( WikiPage $page, Content $content, User $user ) {
+       public static function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
                $cache = ObjectCache::getLocalClusterInstance();
                $logger = LoggerFactory::getInstance( 'StashEdit' );
 
                $format = $content->getDefaultFormat();
                $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
+               $title = $page->getTitle();
 
                if ( $editInfo && $editInfo->output ) {
-                       $key = self::getStashKey( $page->getTitle(), $content, $user );
+                       $key = self::getStashKey( $title, $content, $user );
 
                        // Let extensions add ParserOutput metadata or warm other caches
-                       Hooks::run( 'ParserOutputStashForEdit', [ $page, $content, $editInfo->output ] );
+                       Hooks::run( 'ParserOutputStashForEdit',
+                               [ $page, $content, $editInfo->output, $summary, $user ] );
 
-                       list( $stashInfo, $ttl ) = self::buildStashValue(
+                       list( $stashInfo, $ttl, $code ) = self::buildStashValue(
                                $editInfo->pstContent,
                                $editInfo->output,
                                $editInfo->timestamp,
@@ -156,14 +166,14 @@ class ApiStashEdit extends ApiBase {
                        if ( $stashInfo ) {
                                $ok = $cache->set( $key, $stashInfo, $ttl );
                                if ( $ok ) {
-                                       $logger->debug( "Cached parser output for key '$key'." );
+                                       $logger->debug( "Cached parser output for key '$key' ('$title')." );
                                        return self::ERROR_NONE;
                                } else {
-                                       $logger->error( "Failed to cache parser output for key '$key'." );
+                                       $logger->error( "Failed to cache parser output for key '$key' ('$title')." );
                                        return self::ERROR_CACHE;
                                }
                        } else {
-                               $logger->info( "Uncacheable parser output for key '$key'." );
+                               $logger->info( "Uncacheable parser output for key '$key' ('$title') [$code]." );
                                return self::ERROR_UNCACHEABLE;
                        }
                }
@@ -171,76 +181,6 @@ class ApiStashEdit extends ApiBase {
                return self::ERROR_PARSE;
        }
 
-       /**
-        * Attempt to cache PST content and corresponding parser output in passing
-        *
-        * This method can be called when the output was already generated for other
-        * reasons. Parsing should not be done just to call this method, however.
-        * $pstOpts must be that of the user doing the edit preview. If $pOpts does
-        * not match the options of WikiPage::makeParserOptions( 'canonical' ), this
-        * will do nothing. Provided the values are cacheable, they will be stored
-        * in memcached so that final edit submission might make use of them.
-        *
-        * @param Page|Article|WikiPage $page Page title
-        * @param Content $content Proposed page content
-        * @param Content $pstContent The result of preSaveTransform() on $content
-        * @param ParserOutput $pOut The result of getParserOutput() on $pstContent
-        * @param ParserOptions $pstOpts Options for $pstContent (MUST be for prospective author)
-        * @param ParserOptions $pOpts Options for $pOut
-        * @param string $timestamp TS_MW timestamp of parser output generation
-        * @return bool Success
-        */
-       public static function stashEditFromPreview(
-               Page $page, Content $content, Content $pstContent, ParserOutput $pOut,
-               ParserOptions $pstOpts, ParserOptions $pOpts, $timestamp
-       ) {
-               $cache = ObjectCache::getLocalClusterInstance();
-               $logger = LoggerFactory::getInstance( 'StashEdit' );
-
-               // getIsPreview() controls parser function behavior that references things
-               // like user/revision that don't exists yet. The user/text should already
-               // be set correctly by callers, just double check the preview flag.
-               if ( !$pOpts->getIsPreview() ) {
-                       return false; // sanity
-               } elseif ( $pOpts->getIsSectionPreview() ) {
-                       return false; // short-circuit (need the full content)
-               }
-
-               // PST parser options are for the user (handles signatures, etc...)
-               $user = $pstOpts->getUser();
-               // Get a key based on the source text, format, and user preferences
-               $key = self::getStashKey( $page->getTitle(), $content, $user );
-
-               // Parser output options must match cannonical options.
-               // Treat some options as matching that are different but don't matter.
-               $canonicalPOpts = $page->makeParserOptions( 'canonical' );
-               $canonicalPOpts->setIsPreview( true ); // force match
-               $canonicalPOpts->setTimestamp( $pOpts->getTimestamp() ); // force match
-               if ( !$pOpts->matches( $canonicalPOpts ) ) {
-                       $logger->info( "Uncacheable preview output for key '$key' (options)." );
-                       return false;
-               }
-
-               // Set the time the output was generated
-               $pOut->setCacheTime( wfTimestampNow() );
-
-               // Build a value to cache with a proper TTL
-               list( $stashInfo, $ttl ) = self::buildStashValue( $pstContent, $pOut, $timestamp, $user );
-               if ( !$stashInfo ) {
-                       $logger->info( "Uncacheable parser output for key '$key' (rev/TTL)." );
-                       return false;
-               }
-
-               $ok = $cache->set( $key, $stashInfo, $ttl );
-               if ( !$ok ) {
-                       $logger->error( "Failed to cache preview parser output for key '$key'." );
-               } else {
-                       $logger->debug( "Cached preview output for key '$key'." );
-               }
-
-               return $ok;
-       }
-
        /**
         * Check that a prepared edit is in cache and still up-to-date
         *
@@ -259,6 +199,10 @@ class ApiStashEdit extends ApiBase {
         * @return stdClass|bool Returns false on cache miss
         */
        public static function checkCache( Title $title, Content $content, User $user ) {
+               if ( $user->isBot() ) {
+                       return false; // bots never stash - don't pollute stats
+               }
+
                $cache = ObjectCache::getLocalClusterInstance();
                $logger = LoggerFactory::getInstance( 'StashEdit' );
                $stats = RequestContext::getMain()->getStats();
@@ -284,7 +228,7 @@ class ApiStashEdit extends ApiBase {
 
                if ( !is_object( $editInfo ) || !$editInfo->output ) {
                        $stats->increment( 'editstash.cache_misses.no_stash' );
-                       $logger->debug( "No cache value for key '$key'." );
+                       $logger->debug( "Empty cache for key '$key' ('$title'); user '{$user->getName()}'." );
                        return false;
                }
 
@@ -294,69 +238,38 @@ class ApiStashEdit extends ApiBase {
                        $logger->debug( "Timestamp-based cache hit for key '$key' (age: $age sec)." );
                        return $editInfo; // assume nothing changed
                } elseif ( isset( $editInfo->edits ) && $editInfo->edits === $user->getEditCount() ) {
+                       // Logged-in user made no local upload/template edits in the meantime
                        $stats->increment( 'editstash.cache_hits.presumed_fresh' );
                        $logger->debug( "Edit count based cache hit for key '$key' (age: $age sec)." );
-                       return $editInfo; // use made no local upload/template edits in the meantime
-               }
-
-               $dbr = wfGetDB( DB_SLAVE );
-
-               $templates = []; // conditions to find changes/creations
-               $templateUses = 0; // expected existing templates
-               foreach ( $editInfo->output->getTemplateIds() as $ns => $stuff ) {
-                       foreach ( $stuff as $dbkey => $revId ) {
-                               $templates[(string)$ns][$dbkey] = (int)$revId;
-                               ++$templateUses;
-                       }
-               }
-               // Check that no templates used in the output changed...
-               if ( count( $templates ) ) {
-                       $res = $dbr->select(
-                               'page',
-                               [ 'ns' => 'page_namespace', 'dbk' => 'page_title', 'page_latest' ],
-                               $dbr->makeWhereFrom2d( $templates, 'page_namespace', 'page_title' ),
-                               __METHOD__
-                       );
-                       $changed = false;
-                       foreach ( $res as $row ) {
-                               $changed = $changed || ( $row->page_latest != $templates[$row->ns][$row->dbk] );
-                       }
-
-                       if ( $changed || $res->numRows() != $templateUses ) {
-                               $stats->increment( 'editstash.cache_misses.proven_stale' );
-                               $logger->info( "Stale cache for key '$key'; template changed. (age: $age sec)" );
-                               return false;
-                       }
-               }
-
-               $files = []; // conditions to find changes/creations
-               foreach ( $editInfo->output->getFileSearchOptions() as $name => $options ) {
-                       $files[$name] = (string)$options['sha1'];
+                       return $editInfo;
+               } elseif ( $user->isAnon()
+                       && self::lastEditTime( $user ) < $editInfo->output->getCacheTime()
+               ) {
+                       // Logged-out user made no local upload/template edits in the meantime
+                       $stats->increment( 'editstash.cache_hits.presumed_fresh' );
+                       $logger->debug( "Edit check based cache hit for key '$key' (age: $age sec)." );
+                       return $editInfo;
                }
-               // Check that no files used in the output changed...
-               if ( count( $files ) ) {
-                       $res = $dbr->select(
-                               'image',
-                               [ 'name' => 'img_name', 'img_sha1' ],
-                               [ 'img_name' => array_keys( $files ) ],
-                               __METHOD__
-                       );
-                       $changed = false;
-                       foreach ( $res as $row ) {
-                               $changed = $changed || ( $row->img_sha1 != $files[$row->name] );
-                       }
 
-                       if ( $changed || $res->numRows() != count( $files ) ) {
-                               $stats->increment( 'editstash.cache_misses.proven_stale' );
-                               $logger->info( "Stale cache for key '$key'; file changed. (age: $age sec)" );
-                               return false;
-                       }
-               }
+               $stats->increment( 'editstash.cache_misses.proven_stale' );
+               $logger->info( "Stale cache for key '$key'; old key with outside edits. (age: $age sec)" );
 
-               $stats->increment( 'editstash.cache_hits.proven_fresh' );
-               $logger->debug( "Verified cache hit for key '$key' (age: $age sec)." );
+               return false;
+       }
 
-               return $editInfo;
+       /**
+        * @param User $user
+        * @return string|null TS_MW timestamp or null
+        */
+       private static function lastEditTime( User $user ) {
+               $time = wfGetDB( DB_SLAVE )->selectField(
+                       'recentchanges',
+                       'MAX(rc_timestamp)',
+                       [ 'rc_user_text' => $user->getName() ],
+                       __METHOD__
+               );
+
+               return wfTimestampOrNull( TS_MW, $time );
        }
 
        /**
@@ -371,13 +284,15 @@ class ApiStashEdit extends ApiBase {
         * @param User $user User to get parser options from
         * @return string
         */
-       protected static function getStashKey( Title $title, Content $content, User $user ) {
+       private static function getStashKey( Title $title, Content $content, User $user ) {
                $hash = sha1( implode( ':', [
+                       // Account for the edit model/text
                        $content->getModel(),
                        $content->getDefaultFormat(),
                        sha1( $content->serialize( $content->getDefaultFormat() ) ),
-                       $user->getId() ?: md5( $user->getName() ), // account for user parser options
-                       $user->getId() ? $user->getDBTouched() : '-' // handle preference change races
+                       // Account for user name related variables like signatures
+                       $user->getId(),
+                       md5( $user->getName() )
                ] ) );
 
                return wfMemcKey( 'prepared-edit', md5( $title->getPrefixedDBkey() ), $hash );
@@ -392,28 +307,30 @@ class ApiStashEdit extends ApiBase {
         * @param ParserOutput $parserOutput
         * @param string $timestamp TS_MW
         * @param User $user
-        * @return array (stash info array, TTL in seconds) or (null, 0)
+        * @return array (stash info array, TTL in seconds, info code) or (null, 0, info code)
         */
-       protected static function buildStashValue(
+       private static function buildStashValue(
                Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user
        ) {
                // If an item is renewed, mind the cache TTL determined by config and parser functions.
                // Put an upper limit on the TTL for sanity to avoid extreme template/file staleness.
                $since = time() - wfTimestamp( TS_UNIX, $parserOutput->getTimestamp() );
-               $ttl = min( $parserOutput->getCacheExpiry() - $since, 5 * 60 );
-
-               if ( $ttl > 0 && !$parserOutput->getFlag( 'vary-revision' ) ) {
-                       // Only store what is actually needed
-                       $stashInfo = (object)[
-                               'pstContent' => $pstContent,
-                               'output'     => $parserOutput,
-                               'timestamp'  => $timestamp,
-                               'edits'      => $user->getEditCount()
-                       ];
-                       return [ $stashInfo, $ttl ];
+               $ttl = min( $parserOutput->getCacheExpiry() - $since, self::MAX_CACHE_TTL );
+               if ( $ttl <= 0 ) {
+                       return [ null, 0, 'no_ttl' ];
+               } elseif ( $parserOutput->getFlag( 'vary-revision' ) ) {
+                       return [ null, 0, 'vary_revision' ];
                }
 
-               return [ null, 0 ];
+               // Only store what is actually needed
+               $stashInfo = (object)[
+                       'pstContent' => $pstContent,
+                       'output'     => $parserOutput,
+                       'timestamp'  => $timestamp,
+                       'edits'      => $user->getEditCount()
+               ];
+
+               return [ $stashInfo, $ttl, 'ok' ];
        }
 
        public function getAllowedParams() {
@@ -432,6 +349,9 @@ class ApiStashEdit extends ApiBase {
                                ApiBase::PARAM_TYPE => 'text',
                                ApiBase::PARAM_REQUIRED => true
                        ],
+                       'summary' => [
+                               ApiBase::PARAM_TYPE => 'string',
+                       ],
                        'contentmodel' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
                                ApiBase::PARAM_REQUIRED => true