2014-11-20 00:33:51 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-04 22:18:07 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2016-09-07 19:00:52 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-10-12 05:36:03 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2015-12-04 22:18:07 +00:00
|
|
|
|
2014-11-20 00:33:51 +00:00
|
|
|
/**
|
2015-09-21 14:31:01 +00:00
|
|
|
* Prepare an edit in shared cache so that it can be reused on edit
|
2014-11-20 00:33:51 +00:00
|
|
|
*
|
|
|
|
|
* This endpoint can be called via AJAX as the user focuses on the edit
|
|
|
|
|
* summary box. By the time of submission, the parse may have already
|
|
|
|
|
* finished, and can be immediately used on page save. Certain parser
|
|
|
|
|
* functions like {{REVISIONID}} or {{CURRENTTIME}} may cause the cache
|
|
|
|
|
* to not be used on edit. Template and files used are check for changes
|
|
|
|
|
* since the output was generated. The cache TTL is also kept low for sanity.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
class ApiStashEdit extends ApiBase {
|
2015-03-26 21:19:39 +00:00
|
|
|
const ERROR_NONE = 'stashed';
|
|
|
|
|
const ERROR_PARSE = 'error_parse';
|
|
|
|
|
const ERROR_CACHE = 'error_cache';
|
|
|
|
|
const ERROR_UNCACHEABLE = 'uncacheable';
|
2016-07-30 06:42:05 +00:00
|
|
|
const ERROR_BUSY = 'busy';
|
2015-03-26 21:19:39 +00:00
|
|
|
|
2016-05-06 12:33:54 +00:00
|
|
|
const PRESUME_FRESH_TTL_SEC = 30;
|
2016-06-08 10:23:39 +00:00
|
|
|
const MAX_CACHE_TTL = 300; // 5 minutes
|
2017-07-06 23:23:32 +00:00
|
|
|
const MAX_SIGNATURE_TTL = 60;
|
2016-04-27 22:43:38 +00:00
|
|
|
|
2019-03-26 05:26:13 +00:00
|
|
|
const MAX_CACHE_RECENT = 2;
|
|
|
|
|
|
2014-11-20 00:33:51 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2016-05-26 19:04:22 +00:00
|
|
|
if ( $user->isBot() ) { // sanity
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-botsnotsupported' );
|
2016-05-26 19:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-30 06:42:05 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
2014-11-20 00:33:51 +00:00
|
|
|
$page = $this->getTitleOrPageId( $params );
|
|
|
|
|
$title = $page->getTitle();
|
|
|
|
|
|
|
|
|
|
if ( !ContentHandler::getForModelID( $params['contentmodel'] )
|
|
|
|
|
->isSupportedFormat( $params['contentformat'] )
|
|
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-badformat-generic', $params['contentformat'], $params['contentmodel'] ],
|
|
|
|
|
'badmodelformat'
|
|
|
|
|
);
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:53:45 +00:00
|
|
|
$this->requireOnlyOneParameter( $params, 'stashedtexthash', 'text' );
|
2016-10-19 16:54:25 +00:00
|
|
|
|
2016-09-07 19:00:52 +00:00
|
|
|
$text = null;
|
|
|
|
|
$textHash = null;
|
2018-08-06 16:53:45 +00:00
|
|
|
if ( $params['stashedtexthash'] !== null ) {
|
2016-07-30 06:42:05 +00:00
|
|
|
// Load from cache since the client indicates the text is the same as last stash
|
|
|
|
|
$textHash = $params['stashedtexthash'];
|
2017-08-31 21:35:17 +00:00
|
|
|
if ( !preg_match( '/^[0-9a-f]{40}$/', $textHash ) ) {
|
|
|
|
|
$this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
|
|
|
|
|
}
|
2016-07-30 06:42:05 +00:00
|
|
|
$textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
|
|
|
|
|
$text = $cache->get( $textKey );
|
|
|
|
|
if ( !is_string( $text ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-stashedit-missingtext', 'missingtext' );
|
2016-07-30 06:42:05 +00:00
|
|
|
}
|
2018-08-06 16:53:45 +00:00
|
|
|
} else {
|
|
|
|
|
// 'text' was passed. Trim and fix newlines so the key SHA1's
|
|
|
|
|
// match (see WebRequest::getText())
|
2016-07-30 06:42:05 +00:00
|
|
|
$text = rtrim( str_replace( "\r\n", "\n", $params['text'] ) );
|
|
|
|
|
$textHash = sha1( $text );
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 00:33:51 +00:00
|
|
|
$textContent = ContentHandler::makeContent(
|
|
|
|
|
$text, $title, $params['contentmodel'], $params['contentformat'] );
|
|
|
|
|
|
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
if ( $page->exists() ) {
|
|
|
|
|
// Page exists: get the merged content with the proposed change
|
|
|
|
|
$baseRev = Revision::newFromPageId( $page->getId(), $params['baserevid'] );
|
|
|
|
|
if ( !$baseRev ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['baserevid'] ] );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
$currentRev = $page->getRevision();
|
|
|
|
|
if ( !$currentRev ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-missingrev-pageid', $page->getId() ], 'missingrev' );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
// Merge in the new version of the section to get the proposed version
|
|
|
|
|
$editContent = $page->replaceSectionAtRev(
|
|
|
|
|
$params['section'],
|
|
|
|
|
$textContent,
|
|
|
|
|
$params['sectiontitle'],
|
|
|
|
|
$baseRev->getId()
|
|
|
|
|
);
|
|
|
|
|
if ( !$editContent ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-sectionreplacefailed', 'replacefailed' );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
if ( $currentRev->getId() == $baseRev->getId() ) {
|
|
|
|
|
// Base revision was still the latest; nothing to merge
|
|
|
|
|
$content = $editContent;
|
|
|
|
|
} else {
|
|
|
|
|
// Merge the edit into the current version
|
|
|
|
|
$baseContent = $baseRev->getContent();
|
|
|
|
|
$currentContent = $currentRev->getContent();
|
|
|
|
|
if ( !$baseContent || !$currentContent ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ], 'missingrev' );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
$handler = ContentHandler::getForModelID( $baseContent->getModel() );
|
|
|
|
|
$content = $handler->merge3( $baseContent, $editContent, $currentContent );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// New pages: use the user-provided content model
|
|
|
|
|
$content = $textContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$content ) { // merge3() failed
|
|
|
|
|
$this->getResult()->addValue( null,
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getModuleName(), [ 'status' => 'editconflict' ] );
|
2014-11-20 00:33:51 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The user will abort the AJAX request by pressing "save", so ignore that
|
|
|
|
|
ignore_user_abort( true );
|
|
|
|
|
|
|
|
|
|
if ( $user->pingLimiter( 'stashedit' ) ) {
|
|
|
|
|
$status = 'ratelimited';
|
|
|
|
|
} else {
|
2016-07-30 06:42:05 +00:00
|
|
|
$status = self::parseAndStash( $page, $content, $user, $params['summary'] );
|
|
|
|
|
$textKey = $cache->makeKey( 'stashedit', 'text', $textHash );
|
|
|
|
|
$cache->set( $textKey, $text, self::MAX_CACHE_TTL );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:00:52 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
$stats->increment( "editstash.cache_stores.$status" );
|
2016-05-26 19:46:32 +00:00
|
|
|
|
2018-08-06 16:54:04 +00:00
|
|
|
$ret = [ 'status' => $status ];
|
|
|
|
|
// If we were rate-limited, we still return the pre-existing valid hash if one was passed
|
|
|
|
|
if ( $status !== 'ratelimited' || $params['stashedtexthash'] !== null ) {
|
|
|
|
|
$ret['texthash'] = $textHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $ret );
|
2015-03-26 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param WikiPage $page
|
2016-06-13 10:09:45 +00:00
|
|
|
* @param Content $content Edit content
|
2015-03-26 21:19:39 +00:00
|
|
|
* @param User $user
|
2016-06-13 10:09:45 +00:00
|
|
|
* @param string $summary Edit summary
|
2016-12-08 05:04:53 +00:00
|
|
|
* @return string ApiStashEdit::ERROR_* constant
|
2015-03-26 21:19:39 +00:00
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
2016-06-13 10:09:45 +00:00
|
|
|
public static function parseAndStash( WikiPage $page, Content $content, User $user, $summary ) {
|
2015-12-04 22:18:07 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'StashEdit' );
|
2015-03-26 21:19:39 +00:00
|
|
|
|
2016-06-06 22:38:03 +00:00
|
|
|
$title = $page->getTitle();
|
2016-07-30 06:42:05 +00:00
|
|
|
$key = self::getStashKey( $title, self::getContentHash( $content ), $user );
|
2018-09-30 14:01:23 +00:00
|
|
|
$fname = __METHOD__;
|
2015-03-26 21:19:39 +00:00
|
|
|
|
2017-11-21 02:58:05 +00:00
|
|
|
// Use the master DB to allow for fast blocking locks on the "save path" where this
|
|
|
|
|
// value might actually be used to complete a page edit. If the edit submission request
|
|
|
|
|
// happens before this edit stash requests finishes, then the submission will block until
|
|
|
|
|
// the stash request finishes parsing. For the lock acquisition below, there is not much
|
|
|
|
|
// need to duplicate parsing of the same content/user/summary bundle, so try to avoid
|
|
|
|
|
// blocking at all here.
|
2016-07-30 06:42:05 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2018-09-30 14:01:23 +00:00
|
|
|
if ( !$dbw->lock( $key, $fname, 0 ) ) {
|
2016-07-30 06:42:05 +00:00
|
|
|
// De-duplicate requests on the same key
|
|
|
|
|
return self::ERROR_BUSY;
|
|
|
|
|
}
|
2016-09-07 19:00:52 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2018-09-30 14:01:23 +00:00
|
|
|
$unlocker = new ScopedCallback( function () use ( $dbw, $key, $fname ) {
|
|
|
|
|
$dbw->unlock( $key, $fname );
|
2016-07-30 06:42:05 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$cutoffTime = time() - self::PRESUME_FRESH_TTL_SEC;
|
|
|
|
|
|
|
|
|
|
// Reuse any freshly build matching edit stash cache
|
2019-03-02 02:30:29 +00:00
|
|
|
$editInfo = self::getStashValue( $key );
|
2016-07-30 06:42:05 +00:00
|
|
|
if ( $editInfo && wfTimestamp( TS_UNIX, $editInfo->timestamp ) >= $cutoffTime ) {
|
|
|
|
|
$alreadyCached = true;
|
|
|
|
|
} else {
|
|
|
|
|
$format = $content->getDefaultFormat();
|
|
|
|
|
$editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
|
|
|
|
|
$alreadyCached = false;
|
|
|
|
|
}
|
2015-03-26 21:19:39 +00:00
|
|
|
|
2016-07-30 06:42:05 +00:00
|
|
|
if ( $editInfo && $editInfo->output ) {
|
2016-01-27 01:23:53 +00:00
|
|
|
// Let extensions add ParserOutput metadata or warm other caches
|
2016-06-13 10:09:45 +00:00
|
|
|
Hooks::run( 'ParserOutputStashForEdit',
|
|
|
|
|
[ $page, $content, $editInfo->output, $summary, $user ] );
|
2016-01-27 01:23:53 +00:00
|
|
|
|
2017-11-08 18:31:02 +00:00
|
|
|
$titleStr = (string)$title;
|
2016-07-30 06:42:05 +00:00
|
|
|
if ( $alreadyCached ) {
|
2017-11-08 18:31:02 +00:00
|
|
|
$logger->debug( "Already cached parser output for key '{cachekey}' ('{title}').",
|
|
|
|
|
[ 'cachekey' => $key, 'title' => $titleStr ] );
|
2016-07-30 06:42:05 +00:00
|
|
|
return self::ERROR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
$code = self::storeStashValue(
|
|
|
|
|
$key,
|
2016-05-13 20:49:56 +00:00
|
|
|
$editInfo->pstContent,
|
|
|
|
|
$editInfo->output,
|
|
|
|
|
$editInfo->timestamp,
|
|
|
|
|
$user
|
2014-12-04 09:42:20 +00:00
|
|
|
);
|
2015-03-26 21:19:39 +00:00
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
if ( $code === true ) {
|
|
|
|
|
$logger->debug( "Cached parser output for key '{cachekey}' ('{title}').",
|
|
|
|
|
[ 'cachekey' => $key, 'title' => $titleStr ] );
|
|
|
|
|
return self::ERROR_NONE;
|
|
|
|
|
} elseif ( $code === 'uncacheable' ) {
|
|
|
|
|
$logger->info(
|
|
|
|
|
"Uncacheable parser output for key '{cachekey}' ('{title}') [{code}].",
|
2017-11-08 18:31:02 +00:00
|
|
|
[ 'cachekey' => $key, 'title' => $titleStr, 'code' => $code ] );
|
2015-03-26 21:19:39 +00:00
|
|
|
return self::ERROR_UNCACHEABLE;
|
2019-03-02 02:30:29 +00:00
|
|
|
} else {
|
|
|
|
|
$logger->error( "Failed to cache parser output for key '{cachekey}' ('{title}').",
|
|
|
|
|
[ 'cachekey' => $key, 'title' => $titleStr, 'code' => $code ] );
|
|
|
|
|
return self::ERROR_CACHE;
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 21:19:39 +00:00
|
|
|
return self::ERROR_PARSE;
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that a prepared edit is in cache and still up-to-date
|
|
|
|
|
*
|
|
|
|
|
* This method blocks if the prepared edit is already being rendered,
|
|
|
|
|
* waiting until rendering finishes before doing final validity checks.
|
|
|
|
|
*
|
|
|
|
|
* The cache is rejected if template or file changes are detected.
|
|
|
|
|
* Note that foreign template or file transclusions are not checked.
|
|
|
|
|
*
|
|
|
|
|
* The result is a map (pstContent,output,timestamp) with fields
|
|
|
|
|
* extracted directly from WikiPage::prepareContentForEdit().
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @param User $user User to get parser options from
|
|
|
|
|
* @return stdClass|bool Returns false on cache miss
|
|
|
|
|
*/
|
|
|
|
|
public static function checkCache( Title $title, Content $content, User $user ) {
|
2016-05-26 18:36:40 +00:00
|
|
|
if ( $user->isBot() ) {
|
|
|
|
|
return false; // bots never stash - don't pollute stats
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 22:18:07 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'StashEdit' );
|
2016-09-07 19:00:52 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2014-11-20 00:33:51 +00:00
|
|
|
|
2016-07-30 06:42:05 +00:00
|
|
|
$key = self::getStashKey( $title, self::getContentHash( $content ), $user );
|
2019-03-02 02:30:29 +00:00
|
|
|
$editInfo = self::getStashValue( $key );
|
2014-11-20 00:33:51 +00:00
|
|
|
if ( !is_object( $editInfo ) ) {
|
|
|
|
|
$start = microtime( true );
|
|
|
|
|
// We ignore user aborts and keep parsing. Block on any prior parsing
|
2015-12-10 21:56:11 +00:00
|
|
|
// so as to use its results and make use of the time spent parsing.
|
|
|
|
|
// Skip this logic if there no master connection in case this method
|
|
|
|
|
// is called on an HTTP GET request for some reason.
|
2016-09-07 19:00:52 +00:00
|
|
|
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
|
2015-12-10 21:56:11 +00:00
|
|
|
$dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
|
|
|
|
|
if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
|
2019-03-02 02:30:29 +00:00
|
|
|
$editInfo = self::getStashValue( $key );
|
2015-12-10 21:56:11 +00:00
|
|
|
$dbw->unlock( $key, __METHOD__ );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
2015-12-16 19:42:12 +00:00
|
|
|
|
|
|
|
|
$timeMs = 1000 * max( 0, microtime( true ) - $start );
|
2016-04-19 01:13:08 +00:00
|
|
|
$stats->timing( 'editstash.lock_wait_time', $timeMs );
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_object( $editInfo ) || !$editInfo->output ) {
|
2016-04-19 01:13:08 +00:00
|
|
|
$stats->increment( 'editstash.cache_misses.no_stash' );
|
2016-06-06 22:38:03 +00:00
|
|
|
$logger->debug( "Empty cache for key '$key' ('$title'); user '{$user->getName()}'." );
|
2014-11-20 00:33:51 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-27 22:43:38 +00:00
|
|
|
$age = time() - wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() );
|
|
|
|
|
if ( $age <= self::PRESUME_FRESH_TTL_SEC ) {
|
2016-06-17 23:16:27 +00:00
|
|
|
// Assume nothing changed in this time
|
2016-04-19 01:13:08 +00:00
|
|
|
$stats->increment( 'editstash.cache_hits.presumed_fresh' );
|
2016-04-27 22:43:38 +00:00
|
|
|
$logger->debug( "Timestamp-based cache hit for key '$key' (age: $age sec)." );
|
2016-05-13 20:49:56 +00:00
|
|
|
} elseif ( isset( $editInfo->edits ) && $editInfo->edits === $user->getEditCount() ) {
|
2016-05-14 10:54:05 +00:00
|
|
|
// Logged-in user made no local upload/template edits in the meantime
|
2016-05-13 20:49:56 +00:00
|
|
|
$stats->increment( 'editstash.cache_hits.presumed_fresh' );
|
|
|
|
|
$logger->debug( "Edit count based cache hit for key '$key' (age: $age sec)." );
|
2016-05-14 10:54:05 +00:00
|
|
|
} 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)." );
|
2016-06-17 23:16:27 +00:00
|
|
|
} else {
|
|
|
|
|
// User may have changed included content
|
|
|
|
|
$editInfo = false;
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-17 23:16:27 +00:00
|
|
|
if ( !$editInfo ) {
|
|
|
|
|
$stats->increment( 'editstash.cache_misses.proven_stale' );
|
|
|
|
|
$logger->info( "Stale cache for key '$key'; old key with outside edits. (age: $age sec)" );
|
|
|
|
|
} elseif ( $editInfo->output->getFlag( 'vary-revision' ) ) {
|
|
|
|
|
// This can be used for the initial parse, e.g. for filters or doEditContent(),
|
|
|
|
|
// but a second parse will be triggered in doEditUpdates(). This is not optimal.
|
2016-06-19 05:30:21 +00:00
|
|
|
$logger->info( "Cache for key '$key' ('$title') has vary_revision." );
|
|
|
|
|
} elseif ( $editInfo->output->getFlag( 'vary-revision-id' ) ) {
|
|
|
|
|
// Similar to the above if we didn't guess the ID correctly.
|
|
|
|
|
$logger->info( "Cache for key '$key' ('$title') has vary_revision_id." );
|
2016-06-17 23:16:27 +00:00
|
|
|
}
|
2015-07-13 19:11:16 +00:00
|
|
|
|
2016-06-17 23:16:27 +00:00
|
|
|
return $editInfo;
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-14 10:54:05 +00:00
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @return string|null TS_MW timestamp or null
|
|
|
|
|
*/
|
|
|
|
|
private static function lastEditTime( User $user ) {
|
2017-09-12 17:12:29 +00:00
|
|
|
$db = wfGetDB( DB_REPLICA );
|
|
|
|
|
$actorQuery = ActorMigration::newMigration()->getWhere( $db, 'rc_user', $user, false );
|
|
|
|
|
$time = $db->selectField(
|
|
|
|
|
[ 'recentchanges' ] + $actorQuery['tables'],
|
2016-05-14 10:54:05 +00:00
|
|
|
'MAX(rc_timestamp)',
|
2017-09-12 17:12:29 +00:00
|
|
|
[ $actorQuery['conds'] ],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
[],
|
|
|
|
|
$actorQuery['joins']
|
2016-05-14 10:54:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return wfTimestampOrNull( TS_MW, $time );
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 06:42:05 +00:00
|
|
|
/**
|
|
|
|
|
* Get hash of the content, factoring in model/format
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function getContentHash( Content $content ) {
|
|
|
|
|
return sha1( implode( "\n", [
|
|
|
|
|
$content->getModel(),
|
|
|
|
|
$content->getDefaultFormat(),
|
|
|
|
|
$content->serialize( $content->getDefaultFormat() )
|
|
|
|
|
] ) );
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 09:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Get the temporary prepared edit stash key for a user
|
|
|
|
|
*
|
|
|
|
|
* This key can be used for caching prepared edits provided:
|
|
|
|
|
* - a) The $user was used for PST options
|
|
|
|
|
* - b) The parser output was made from the PST using cannonical matching options
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
2016-07-30 06:42:05 +00:00
|
|
|
* @param string $contentHash Result of getContentHash()
|
2014-12-04 09:42:20 +00:00
|
|
|
* @param User $user User to get parser options from
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2016-07-30 06:42:05 +00:00
|
|
|
private static function getStashKey( Title $title, $contentHash, User $user ) {
|
|
|
|
|
return ObjectCache::getLocalClusterInstance()->makeKey(
|
2019-03-02 02:30:29 +00:00
|
|
|
'stashed-edit-info',
|
2016-07-30 06:42:05 +00:00
|
|
|
md5( $title->getPrefixedDBkey() ),
|
2016-06-06 21:34:14 +00:00
|
|
|
// Account for the edit model/text
|
2016-07-30 06:42:05 +00:00
|
|
|
$contentHash,
|
2016-06-06 21:34:14 +00:00
|
|
|
// Account for user name related variables like signatures
|
2016-07-30 06:42:05 +00:00
|
|
|
md5( $user->getId() . "\n" . $user->getName() )
|
|
|
|
|
);
|
2014-12-04 09:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $uuid
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function getStashParserOutputKey( $uuid ) {
|
|
|
|
|
return ObjectCache::getLocalClusterInstance()->makeKey( 'stashed-edit-output', $uuid );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return stdClass|bool Object map (pstContent,output,outputID,timestamp,edits) or false
|
|
|
|
|
*/
|
|
|
|
|
private static function getStashValue( $key ) {
|
|
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
|
|
|
|
|
|
$stashInfo = $cache->get( $key );
|
|
|
|
|
if ( !is_object( $stashInfo ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$parserOutputKey = self::getStashParserOutputKey( $stashInfo->outputID );
|
|
|
|
|
$parserOutput = $cache->get( $parserOutputKey );
|
|
|
|
|
if ( $parserOutput instanceof ParserOutput ) {
|
|
|
|
|
$stashInfo->output = $parserOutput;
|
|
|
|
|
|
|
|
|
|
return $stashInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 09:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* Build a value to store in memcached based on the PST content and parser output
|
|
|
|
|
*
|
|
|
|
|
* This makes a simple version of WikiPage::prepareContentForEdit() as stash info
|
|
|
|
|
*
|
2019-03-02 02:30:29 +00:00
|
|
|
* @param string $key
|
2016-07-30 06:42:05 +00:00
|
|
|
* @param Content $pstContent Pre-Save transformed content
|
2014-12-04 09:42:20 +00:00
|
|
|
* @param ParserOutput $parserOutput
|
|
|
|
|
* @param string $timestamp TS_MW
|
2016-05-13 20:49:56 +00:00
|
|
|
* @param User $user
|
2019-03-02 02:30:29 +00:00
|
|
|
* @return string|bool True or an error code
|
2014-12-04 09:42:20 +00:00
|
|
|
*/
|
2019-03-02 02:30:29 +00:00
|
|
|
private static function storeStashValue(
|
|
|
|
|
$key, Content $pstContent, ParserOutput $parserOutput, $timestamp, User $user
|
2014-12-04 09:42:20 +00:00
|
|
|
) {
|
2016-05-13 20:49:56 +00:00
|
|
|
// 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.
|
2019-03-02 02:30:29 +00:00
|
|
|
$age = time() - wfTimestamp( TS_UNIX, $parserOutput->getCacheTime() );
|
|
|
|
|
$ttl = min( $parserOutput->getCacheExpiry() - $age, self::MAX_CACHE_TTL );
|
2017-07-06 23:23:32 +00:00
|
|
|
// Avoid extremely stale user signature timestamps (T84843)
|
|
|
|
|
if ( $parserOutput->getFlag( 'user-signature' ) ) {
|
|
|
|
|
$ttl = min( $ttl, self::MAX_SIGNATURE_TTL );
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 04:03:59 +00:00
|
|
|
if ( $ttl <= 0 ) {
|
2019-03-02 02:30:29 +00:00
|
|
|
return 'uncacheable'; // low TTL due to a tag, magic word, or signature?
|
2014-12-04 09:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
// Store what is actually needed and split the output into another key (T204742)
|
|
|
|
|
$parseroutputID = md5( $key );
|
2016-06-10 04:03:59 +00:00
|
|
|
$stashInfo = (object)[
|
|
|
|
|
'pstContent' => $pstContent,
|
2019-03-02 02:30:29 +00:00
|
|
|
'outputID' => $parseroutputID,
|
2016-06-10 04:03:59 +00:00
|
|
|
'timestamp' => $timestamp,
|
|
|
|
|
'edits' => $user->getEditCount()
|
|
|
|
|
];
|
|
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
|
$ok = $cache->set( $key, $stashInfo, $ttl );
|
|
|
|
|
if ( $ok ) {
|
|
|
|
|
$ok = $cache->set(
|
|
|
|
|
self::getStashParserOutputKey( $parseroutputID ),
|
|
|
|
|
$parserOutput,
|
|
|
|
|
$ttl
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-26 05:26:13 +00:00
|
|
|
if ( $ok ) {
|
|
|
|
|
// These blobs can waste slots in low cardinality memcached slabs
|
|
|
|
|
self::pruneExcessStashedEntries( $cache, $user, $key );
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 02:30:29 +00:00
|
|
|
return $ok ? true : 'store_error';
|
2014-12-04 09:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-26 05:26:13 +00:00
|
|
|
/**
|
|
|
|
|
* @param BagOStuff $cache
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param string $newKey
|
|
|
|
|
*/
|
|
|
|
|
private static function pruneExcessStashedEntries( BagOStuff $cache, User $user, $newKey ) {
|
2019-03-28 01:25:46 +00:00
|
|
|
$key = $cache->makeKey( 'stash-edit-recent', sha1( $user->getName() ) );
|
2019-03-26 05:26:13 +00:00
|
|
|
|
|
|
|
|
$keyList = $cache->get( $key ) ?: [];
|
|
|
|
|
if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
|
|
|
|
|
$oldestKey = array_shift( $keyList );
|
|
|
|
|
$cache->delete( $oldestKey );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$keyList[] = $newKey;
|
|
|
|
|
$cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-20 00:33:51 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'title' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'section' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'sectiontitle' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'text' => [
|
2015-05-07 16:39:55 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'text',
|
2016-07-30 06:42:05 +00:00
|
|
|
ApiBase::PARAM_DFLT => null
|
|
|
|
|
],
|
|
|
|
|
'stashedtexthash' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_DFLT => null
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2016-06-13 10:09:45 +00:00
|
|
|
'summary' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
'contentmodel' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'contentformat' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'baserevid' => [
|
2014-11-20 00:33:51 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
];
|
2014-11-20 00:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 07:46:02 +00:00
|
|
|
public function needsToken() {
|
2014-11-20 00:33:51 +00:00
|
|
|
return 'csrf';
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 07:46:02 +00:00
|
|
|
public function mustBePosted() {
|
2014-11-20 00:33:51 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 07:46:02 +00:00
|
|
|
public function isWriteMode() {
|
2015-12-10 21:56:11 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 07:46:02 +00:00
|
|
|
public function isInternal() {
|
2014-11-20 00:33:51 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|