2008-03-03 18:19:52 +00:00
|
|
|
<?php
|
2010-02-22 12:18:42 +00:00
|
|
|
/**
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2007 Iker Labarga "<Firstname><Lastname>@gmail.com"
|
2008-03-03 18:19:52 +00:00
|
|
|
*
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-03-03 18:19:52 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2008-03-03 18:19:52 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-09-25 16:17:29 +00:00
|
|
|
namespace MediaWiki\Api;
|
|
|
|
|
|
|
|
|
|
use Article;
|
|
|
|
|
use ChangeTags;
|
|
|
|
|
use MediaWiki\Content\ContentHandler;
|
2020-01-18 20:25:04 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2024-05-19 18:33:58 +00:00
|
|
|
use MediaWiki\Content\TextContent;
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-02-13 19:56:51 +00:00
|
|
|
use MediaWiki\EditPage\EditPage;
|
2022-04-13 15:28:26 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2019-08-27 02:45:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-06-14 21:24:11 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2021-10-20 15:51:22 +00:00
|
|
|
use MediaWiki\Page\RedirectLookup;
|
2021-07-02 12:35:14 +00:00
|
|
|
use MediaWiki\Page\WikiPageFactory;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\DerivativeRequest;
|
2021-06-30 20:30:28 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2019-09-12 19:40:57 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-03-04 07:23:09 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-11-29 10:21:43 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-08-08 18:24:33 +00:00
|
|
|
use MediaWiki\User\TempUser\TempUserCreator;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-08-08 18:24:33 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2024-05-21 20:19:18 +00:00
|
|
|
use MediaWiki\Watchlist\WatchedItemStoreInterface;
|
2021-07-04 12:47:21 +00:00
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2024-09-25 16:17:29 +00:00
|
|
|
use MWContentSerializationException;
|
2022-06-05 23:18:50 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
2019-07-21 13:18:14 +00:00
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
/**
|
2008-09-04 13:40:33 +00:00
|
|
|
* A module that allows for editing and creating pages.
|
2008-03-03 18:19:52 +00:00
|
|
|
*
|
2008-09-04 13:40:33 +00:00
|
|
|
* Currently, this wraps around the EditPage class in an ugly way,
|
2015-04-05 06:48:02 +00:00
|
|
|
* EditPage.php should be rewritten to provide a cleaner interface,
|
|
|
|
|
* see T20654 if you're inspired to fix this.
|
|
|
|
|
*
|
2020-09-30 16:45:19 +00:00
|
|
|
* WARNING: This class is //not// stable to extend. However, it is
|
|
|
|
|
* currently extended by the ApiThreadAction class in the LiquidThreads
|
|
|
|
|
* extension, which is deployed on WMF servers. Changes that would
|
|
|
|
|
* break LiquidThreads will likely be reverted. See T264200 for context
|
|
|
|
|
* and T264213 for removing LiquidThreads' unsupported extending of this
|
|
|
|
|
* class.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2008-03-03 18:19:52 +00:00
|
|
|
*/
|
|
|
|
|
class ApiEditPage extends ApiBase {
|
2023-10-31 20:10:38 +00:00
|
|
|
use ApiCreateTempUserTrait;
|
2020-06-04 20:16:23 +00:00
|
|
|
use ApiWatchlistTrait;
|
|
|
|
|
|
2023-08-28 15:32:58 +00:00
|
|
|
private IContentHandlerFactory $contentHandlerFactory;
|
|
|
|
|
private RevisionLookup $revisionLookup;
|
|
|
|
|
private WatchedItemStoreInterface $watchedItemStore;
|
|
|
|
|
private WikiPageFactory $wikiPageFactory;
|
|
|
|
|
private RedirectLookup $redirectLookup;
|
|
|
|
|
private TempUserCreator $tempUserCreator;
|
|
|
|
|
private UserFactory $userFactory;
|
2023-08-08 18:24:33 +00:00
|
|
|
|
2021-12-08 22:34:58 +00:00
|
|
|
/**
|
|
|
|
|
* Sends a cookie so anons get talk message notifications, mirroring SubmitAction (T295910)
|
|
|
|
|
*/
|
|
|
|
|
private function persistGlobalSession() {
|
2024-09-25 16:17:29 +00:00
|
|
|
\MediaWiki\Session\SessionManager::getGlobalSession()->persist();
|
2021-12-08 22:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-30 20:30:28 +00:00
|
|
|
public function __construct(
|
|
|
|
|
ApiMain $mainModule,
|
2024-10-14 20:12:27 +00:00
|
|
|
string $moduleName,
|
2024-10-16 18:58:33 +00:00
|
|
|
?IContentHandlerFactory $contentHandlerFactory = null,
|
|
|
|
|
?RevisionLookup $revisionLookup = null,
|
|
|
|
|
?WatchedItemStoreInterface $watchedItemStore = null,
|
|
|
|
|
?WikiPageFactory $wikiPageFactory = null,
|
|
|
|
|
?WatchlistManager $watchlistManager = null,
|
|
|
|
|
?UserOptionsLookup $userOptionsLookup = null,
|
|
|
|
|
?RedirectLookup $redirectLookup = null,
|
|
|
|
|
?TempUserCreator $tempUserCreator = null,
|
|
|
|
|
?UserFactory $userFactory = null
|
2021-06-30 20:30:28 +00:00
|
|
|
) {
|
|
|
|
|
parent::__construct( $mainModule, $moduleName );
|
2020-06-04 20:16:23 +00:00
|
|
|
|
2021-06-30 20:30:28 +00:00
|
|
|
// This class is extended and therefor fallback to global state - T264213
|
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory();
|
|
|
|
|
$this->revisionLookup = $revisionLookup ?? $services->getRevisionLookup();
|
|
|
|
|
$this->watchedItemStore = $watchedItemStore ?? $services->getWatchedItemStore();
|
2021-07-02 12:35:14 +00:00
|
|
|
$this->wikiPageFactory = $wikiPageFactory ?? $services->getWikiPageFactory();
|
2021-07-04 12:47:21 +00:00
|
|
|
|
|
|
|
|
// Variables needed in ApiWatchlistTrait trait
|
2022-04-13 15:28:26 +00:00
|
|
|
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
|
|
|
|
|
$this->watchlistMaxDuration =
|
|
|
|
|
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
|
2021-07-04 12:47:21 +00:00
|
|
|
$this->watchlistManager = $watchlistManager ?? $services->getWatchlistManager();
|
|
|
|
|
$this->userOptionsLookup = $userOptionsLookup ?? $services->getUserOptionsLookup();
|
2021-10-20 15:51:22 +00:00
|
|
|
$this->redirectLookup = $redirectLookup ?? $services->getRedirectLookup();
|
2023-08-08 18:24:33 +00:00
|
|
|
$this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
|
|
|
|
|
$this->userFactory = $userFactory ?? $services->getUserFactory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see EditPage::getUserForPermissions
|
|
|
|
|
* @return User
|
|
|
|
|
*/
|
|
|
|
|
private function getUserForPermissions() {
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
|
|
|
|
|
return $this->userFactory->newUnsavedTempUser(
|
|
|
|
|
$this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $user;
|
2020-06-04 20:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
public function execute() {
|
2015-08-08 00:08:33 +00:00
|
|
|
$this->useTransactionalTimeLimit();
|
|
|
|
|
|
2011-10-26 23:27:01 +00:00
|
|
|
$user = $this->getUser();
|
2008-03-03 18:19:52 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2010-02-22 12:18:42 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->requireAtLeastOneParameter( $params, 'text', 'appendtext', 'prependtext', 'undo' );
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2012-04-28 13:45:37 +00:00
|
|
|
$pageObj = $this->getTitleOrPageId( $params );
|
|
|
|
|
$titleObj = $pageObj->getTitle();
|
2021-05-21 15:44:24 +00:00
|
|
|
$this->getErrorFormatter()->setContextTitle( $titleObj );
|
2011-06-20 22:18:11 +00:00
|
|
|
$apiResult = $this->getResult();
|
2011-06-20 22:13:45 +00:00
|
|
|
|
2010-12-30 17:06:09 +00:00
|
|
|
if ( $params['redirect'] ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
if ( $params['prependtext'] === null
|
|
|
|
|
&& $params['appendtext'] === null
|
2015-03-14 08:35:54 +00:00
|
|
|
&& $params['section'] !== 'new'
|
|
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-redirect-appendonly' );
|
2014-05-18 07:04:36 +00:00
|
|
|
}
|
2010-12-30 17:06:09 +00:00
|
|
|
if ( $titleObj->isRedirect() ) {
|
2021-10-20 15:51:22 +00:00
|
|
|
$oldTarget = $titleObj;
|
|
|
|
|
$redirTarget = $this->redirectLookup->getRedirectTarget( $oldTarget );
|
|
|
|
|
$redirTarget = Title::castFromLinkTarget( $redirTarget );
|
|
|
|
|
|
|
|
|
|
$redirValues = [
|
|
|
|
|
'from' => $titleObj->getPrefixedText(),
|
|
|
|
|
'to' => $redirTarget->getPrefixedText()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// T239428: Check whether the new title is valid
|
|
|
|
|
if ( $redirTarget->isExternal() || !$redirTarget->canExist() ) {
|
|
|
|
|
$redirValues['to'] = $redirTarget->getFullText();
|
|
|
|
|
$this->dieWithError(
|
|
|
|
|
[
|
|
|
|
|
'apierror-edit-invalidredirect',
|
|
|
|
|
Message::plaintextParam( $oldTarget->getPrefixedText() ),
|
|
|
|
|
Message::plaintextParam( $redirTarget->getFullText() ),
|
|
|
|
|
],
|
|
|
|
|
'edit-invalidredirect',
|
|
|
|
|
[ 'redirects' => $redirValues ]
|
|
|
|
|
);
|
2010-10-23 17:48:08 +00:00
|
|
|
}
|
2010-12-30 17:06:09 +00:00
|
|
|
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $redirValues, 'r' );
|
2011-06-20 22:18:11 +00:00
|
|
|
$apiResult->addValue( null, 'redirects', $redirValues );
|
2012-11-12 05:42:26 +00:00
|
|
|
|
2022-05-23 15:06:11 +00:00
|
|
|
// Since the page changed, update $pageObj and $titleObj
|
2021-10-20 15:51:22 +00:00
|
|
|
$pageObj = $this->wikiPageFactory->newFromTitle( $redirTarget );
|
2022-05-23 15:06:11 +00:00
|
|
|
$titleObj = $pageObj->getTitle();
|
|
|
|
|
|
2021-10-20 15:51:22 +00:00
|
|
|
$this->getErrorFormatter()->setContextTitle( $redirTarget );
|
2010-08-08 01:14:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $params['contentmodel'] ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
$contentHandler = $this->contentHandlerFactory->getContentHandler( $params['contentmodel'] );
|
2020-03-05 14:59:37 +00:00
|
|
|
} else {
|
|
|
|
|
$contentHandler = $pageObj->getContentHandler();
|
2012-11-12 05:42:26 +00:00
|
|
|
}
|
2016-09-08 23:43:36 +00:00
|
|
|
$contentModel = $contentHandler->getModelID();
|
2012-11-12 05:42:26 +00:00
|
|
|
|
2015-06-19 22:39:48 +00:00
|
|
|
$name = $titleObj->getPrefixedDBkey();
|
2015-08-13 20:34:45 +00:00
|
|
|
|
2019-08-19 14:49:30 +00:00
|
|
|
if ( $params['undo'] > 0 ) {
|
|
|
|
|
// allow undo via api
|
|
|
|
|
} elseif ( $contentHandler->supportsDirectApiEditing() === false ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
$this->dieWithError( [ 'apierror-no-direct-editing', $contentModel, $name ] );
|
2015-04-15 08:26:22 +00:00
|
|
|
}
|
2012-11-12 05:42:26 +00:00
|
|
|
|
2020-03-05 14:59:37 +00:00
|
|
|
$contentFormat = $params['contentformat'] ?: $contentHandler->getDefaultFormat();
|
2012-11-12 05:42:26 +00:00
|
|
|
|
|
|
|
|
if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
$this->dieWithError( [ 'apierror-badformat', $contentFormat, $contentModel, $name ] );
|
2012-11-12 05:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-22 12:18:42 +00:00
|
|
|
if ( $params['createonly'] && $titleObj->exists() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-articleexists' );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
|
|
|
|
if ( $params['nocreate'] && !$titleObj->exists() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-missingtitle' );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
// Now let's check whether we're even allowed to do this
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->checkTitleUserPermissions(
|
|
|
|
|
$titleObj,
|
2021-06-24 14:10:43 +00:00
|
|
|
'edit',
|
2023-08-08 18:24:33 +00:00
|
|
|
[ 'autoblock' => true, 'user' => $this->getUserForPermissions() ]
|
2016-10-19 16:54:25 +00:00
|
|
|
);
|
2008-03-03 18:19:52 +00:00
|
|
|
|
2008-06-15 19:59:55 +00:00
|
|
|
$toMD5 = $params['text'];
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['appendtext'] !== null || $params['prependtext'] !== null ) {
|
2012-06-25 14:09:08 +00:00
|
|
|
$content = $pageObj->getContent();
|
|
|
|
|
|
|
|
|
|
if ( !$content ) {
|
2020-07-22 17:29:48 +00:00
|
|
|
if ( $titleObj->getNamespace() === NS_MEDIAWIKI ) {
|
2012-10-10 19:30:40 +00:00
|
|
|
# If this is a MediaWiki:x message, then load the messages
|
|
|
|
|
# and return the message value for x.
|
2012-06-25 14:09:08 +00:00
|
|
|
$text = $titleObj->getDefaultMessageText();
|
|
|
|
|
if ( $text === false ) {
|
|
|
|
|
$text = '';
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-21 14:14:18 +00:00
|
|
|
try {
|
2018-03-20 13:25:26 +00:00
|
|
|
$content = ContentHandler::makeContent( $text, $titleObj );
|
2012-08-21 14:14:18 +00:00
|
|
|
} catch ( MWContentSerializationException $ex ) {
|
2016-12-08 18:38:45 +00:00
|
|
|
$this->dieWithException( $ex, [
|
|
|
|
|
'wrap' => ApiMessage::create( 'apierror-contentserializationexception', 'parseerror' )
|
|
|
|
|
] );
|
2012-08-21 14:14:18 +00:00
|
|
|
}
|
2012-10-10 19:30:40 +00:00
|
|
|
} else {
|
|
|
|
|
# Otherwise, make a new empty content.
|
|
|
|
|
$content = $contentHandler->makeEmptyContent();
|
2012-06-25 14:09:08 +00:00
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo Add support for appending/prepending to the Content interface
|
2012-10-10 19:30:40 +00:00
|
|
|
|
|
|
|
|
if ( !( $content instanceof TextContent ) ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
$this->dieWithError( [ 'apierror-appendnotsupported', $contentModel ] );
|
2012-10-10 19:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['section'] !== null ) {
|
2012-06-25 14:09:08 +00:00
|
|
|
if ( !$contentHandler->supportsSections() ) {
|
2021-04-12 05:26:45 +00:00
|
|
|
$this->dieWithError( [ 'apierror-sectionsnotsupported', $contentModel ] );
|
2012-06-25 14:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:26:49 +00:00
|
|
|
if ( $params['section'] == 'new' ) {
|
|
|
|
|
// DWIM if they're trying to prepend/append to a new section.
|
|
|
|
|
$content = null;
|
|
|
|
|
} else {
|
|
|
|
|
// Process the content for section edits
|
2014-05-01 00:10:43 +00:00
|
|
|
$section = $params['section'];
|
2013-08-05 15:26:49 +00:00
|
|
|
$content = $content->getSection( $section );
|
2012-06-25 14:09:08 +00:00
|
|
|
|
2013-08-05 15:26:49 +00:00
|
|
|
if ( !$content ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchsection', wfEscapeWikiText( $section ) ] );
|
2013-08-05 15:26:49 +00:00
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2009-07-14 20:14:47 +00:00
|
|
|
}
|
2012-06-25 14:09:08 +00:00
|
|
|
|
|
|
|
|
if ( !$content ) {
|
|
|
|
|
$text = '';
|
|
|
|
|
} else {
|
|
|
|
|
$text = $content->serialize( $contentFormat );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-05 17:09:41 +00:00
|
|
|
$params['text'] = $params['prependtext'] . $text . $params['appendtext'];
|
2008-06-15 19:59:55 +00:00
|
|
|
$toMD5 = $params['prependtext'] . $params['appendtext'];
|
2008-06-12 13:05:07 +00:00
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
|
|
|
|
|
if ( $params['undo'] > 0 ) {
|
2021-06-30 20:30:28 +00:00
|
|
|
$undoRev = $this->revisionLookup->getRevisionById( $params['undo'] );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $undoRev === null || $undoRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['undo'] ] );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2021-04-12 05:26:45 +00:00
|
|
|
if ( $params['undoafter'] > 0 ) {
|
2021-06-30 20:30:28 +00:00
|
|
|
$undoafterRev = $this->revisionLookup->getRevisionById( $params['undoafter'] );
|
2022-03-26 21:49:54 +00:00
|
|
|
} else {
|
|
|
|
|
// undoafter=0 or null
|
2021-06-30 20:30:28 +00:00
|
|
|
$undoafterRev = $this->revisionLookup->getPreviousRevision( $undoRev );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $undoafterRev === null || $undoafterRev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $params['undoafter'] ] );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2020-03-04 07:23:09 +00:00
|
|
|
if ( $undoRev->getPageId() != $pageObj->getId() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-revwrongpage', $undoRev->getId(),
|
2016-02-17 09:09:32 +00:00
|
|
|
$titleObj->getPrefixedText() ] );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2020-03-04 07:23:09 +00:00
|
|
|
if ( $undoafterRev->getPageId() != $pageObj->getId() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-revwrongpage', $undoafterRev->getId(),
|
2016-02-17 09:09:32 +00:00
|
|
|
$titleObj->getPrefixedText() ] );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-16 19:09:17 +00:00
|
|
|
$newContent = $contentHandler->getUndoContent(
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here
|
2020-03-04 07:23:09 +00:00
|
|
|
$pageObj->getRevisionRecord()->getContent( SlotRecord::MAIN ),
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here
|
2020-03-04 07:23:09 +00:00
|
|
|
$undoRev->getContent( SlotRecord::MAIN ),
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable Content is for public use here
|
2020-03-04 07:23:09 +00:00
|
|
|
$undoafterRev->getContent( SlotRecord::MAIN ),
|
|
|
|
|
$pageObj->getRevisionRecord()->getId() === $undoRev->getId()
|
2013-11-16 19:09:17 +00:00
|
|
|
);
|
2012-08-21 14:14:18 +00:00
|
|
|
|
2012-06-25 14:09:08 +00:00
|
|
|
if ( !$newContent ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'undo-failure', 'undofailure' );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( !$params['contentmodel'] && !$params['contentformat'] ) {
|
2016-09-08 23:43:36 +00:00
|
|
|
// If we are reverting content model, the new content model
|
|
|
|
|
// might not support the current serialization format, in
|
|
|
|
|
// which case go back to the old serialization format,
|
|
|
|
|
// but only if the user hasn't specified a format/model
|
|
|
|
|
// parameter.
|
|
|
|
|
if ( !$newContent->isSupportedFormat( $contentFormat ) ) {
|
2020-03-04 07:23:09 +00:00
|
|
|
$undoafterRevMainSlot = $undoafterRev->getSlot(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
RevisionRecord::RAW
|
|
|
|
|
);
|
|
|
|
|
$contentFormat = $undoafterRevMainSlot->getFormat();
|
|
|
|
|
if ( !$contentFormat ) {
|
|
|
|
|
// fall back to default content format for the model
|
|
|
|
|
// of $undoafterRev
|
2021-04-12 05:26:45 +00:00
|
|
|
$contentFormat = $this->contentHandlerFactory
|
2020-03-04 07:23:09 +00:00
|
|
|
->getContentHandler( $undoafterRevMainSlot->getModel() )
|
|
|
|
|
->getDefaultFormat();
|
|
|
|
|
}
|
2016-09-08 23:43:36 +00:00
|
|
|
}
|
|
|
|
|
// Override content model with model of undid revision.
|
|
|
|
|
$contentModel = $newContent->getModel();
|
2020-06-20 15:21:46 +00:00
|
|
|
$undoContentModel = true;
|
2016-09-08 23:43:36 +00:00
|
|
|
}
|
|
|
|
|
$params['text'] = $newContent->serialize( $contentFormat );
|
2009-01-26 13:51:03 +00:00
|
|
|
// If no summary was given and we only undid one rev,
|
|
|
|
|
// use an autosummary
|
2019-08-27 02:45:33 +00:00
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['summary'] === null ) {
|
2021-06-30 20:30:28 +00:00
|
|
|
$nextRev = $this->revisionLookup->getNextRevision( $undoafterRev );
|
2019-08-27 02:45:33 +00:00
|
|
|
if ( $nextRev && $nextRev->getId() == $params['undo'] ) {
|
2020-03-04 07:23:09 +00:00
|
|
|
$undoRevUser = $undoRev->getUser();
|
2022-08-13 14:21:29 +00:00
|
|
|
$params['summary'] = $this->msg( 'undo-summary' )
|
2020-03-04 07:23:09 +00:00
|
|
|
->params( $params['undo'], $undoRevUser ? $undoRevUser->getName() : '' )
|
2019-08-27 02:45:33 +00:00
|
|
|
->inContentLanguage()->text();
|
|
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2009-01-26 13:51:03 +00:00
|
|
|
}
|
2008-06-12 13:05:07 +00:00
|
|
|
|
2010-01-23 22:52:40 +00:00
|
|
|
// See if the MD5 hash checks out
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['md5'] !== null && md5( $toMD5 ) !== $params['md5'] ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-badmd5' );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-27 13:14:16 +00:00
|
|
|
// EditPage wants to parse its stuff from a WebRequest
|
2008-03-03 18:19:52 +00:00
|
|
|
// That interface kind of sucks, but it's workable
|
2016-02-17 09:09:32 +00:00
|
|
|
$requestArray = [
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2010-02-22 12:18:42 +00:00
|
|
|
'wpTextbox1' => $params['text'],
|
2012-06-25 14:09:08 +00:00
|
|
|
'format' => $contentFormat,
|
2016-09-08 23:43:36 +00:00
|
|
|
'model' => $contentModel,
|
2021-08-05 06:54:11 +00:00
|
|
|
'wpEditToken' => $params['token'],
|
2015-01-20 21:15:36 +00:00
|
|
|
'wpIgnoreBlankSummary' => true,
|
2014-12-10 11:48:13 +00:00
|
|
|
'wpIgnoreBlankArticle' => true,
|
|
|
|
|
'wpIgnoreSelfRedirect' => true,
|
2015-02-06 19:51:09 +00:00
|
|
|
'bot' => $params['bot'],
|
2017-08-28 21:14:44 +00:00
|
|
|
'wpUnicodeCheck' => EditPage::UNICODE_CHECK,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['summary'] !== null ) {
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpSummary'] = $params['summary'];
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2012-09-05 15:50:13 +00:00
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['sectiontitle'] !== null ) {
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpSectionTitle'] = $params['sectiontitle'];
|
2011-12-20 04:15:21 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2012-10-28 23:05:40 +00:00
|
|
|
if ( $params['undo'] > 0 ) {
|
|
|
|
|
$requestArray['wpUndidRevision'] = $params['undo'];
|
|
|
|
|
}
|
2020-07-04 12:46:53 +00:00
|
|
|
if ( $params['undoafter'] > 0 ) {
|
|
|
|
|
$requestArray['wpUndoAfter'] = $params['undoafter'];
|
|
|
|
|
}
|
2012-10-28 23:05:40 +00:00
|
|
|
|
2020-03-05 21:23:07 +00:00
|
|
|
// Skip for baserevid == null or '' or '0' or 0
|
|
|
|
|
if ( !empty( $params['baserevid'] ) ) {
|
|
|
|
|
$requestArray['editRevId'] = $params['baserevid'];
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-07 16:39:55 +00:00
|
|
|
// Watch out for basetimestamp == '' or '0'
|
|
|
|
|
// It gets treated as NOW, almost certainly causing an edit conflict
|
|
|
|
|
if ( $params['basetimestamp'] !== null && (bool)$this->getMain()->getVal( 'basetimestamp' ) ) {
|
|
|
|
|
$requestArray['wpEdittime'] = $params['basetimestamp'];
|
2020-03-05 21:23:07 +00:00
|
|
|
} elseif ( empty( $params['baserevid'] ) ) {
|
|
|
|
|
// Only set if baserevid is not set. Otherwise, conflicts would be ignored,
|
|
|
|
|
// due to the way userWasLastToEdit() works.
|
2012-06-25 14:09:08 +00:00
|
|
|
$requestArray['wpEdittime'] = $pageObj->getTimestamp();
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2015-05-07 16:39:55 +00:00
|
|
|
if ( $params['starttimestamp'] !== null ) {
|
|
|
|
|
$requestArray['wpStarttime'] = $params['starttimestamp'];
|
2010-02-22 12:18:42 +00:00
|
|
|
} else {
|
2013-03-07 16:50:43 +00:00
|
|
|
$requestArray['wpStarttime'] = wfTimestampNow(); // Fake wpStartime
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2021-07-06 21:10:28 +00:00
|
|
|
if ( $params['minor'] || ( !$params['notminor'] &&
|
|
|
|
|
$this->userOptionsLookup->getOption( $user, 'minordefault' ) )
|
|
|
|
|
) {
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpMinoredit'] = '';
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2010-02-22 12:18:42 +00:00
|
|
|
if ( $params['recreate'] ) {
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpRecreate'] = '';
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['section'] !== null ) {
|
2014-05-01 00:10:43 +00:00
|
|
|
$section = $params['section'];
|
|
|
|
|
if ( !preg_match( '/^((T-)?\d+|new)$/', $section ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-invalidsection' );
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2013-09-11 17:54:22 +00:00
|
|
|
$content = $pageObj->getContent();
|
2021-04-12 05:26:45 +00:00
|
|
|
if ( $section !== '0'
|
|
|
|
|
&& $section != 'new'
|
2015-03-14 08:35:54 +00:00
|
|
|
&& ( !$content || !$content->getSection( $section ) )
|
|
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchsection', $section ] );
|
2013-09-11 17:54:22 +00:00
|
|
|
}
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpSection'] = $params['section'];
|
2010-02-22 12:18:42 +00:00
|
|
|
} else {
|
2011-12-20 04:22:06 +00:00
|
|
|
$requestArray['wpSection'] = '';
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2020-09-04 12:31:40 +00:00
|
|
|
$watch = $this->getWatchlistValue( $params['watchlist'], $titleObj, $user );
|
2010-03-25 22:15:08 +00:00
|
|
|
|
2009-07-14 20:48:00 +00:00
|
|
|
// Deprecated parameters
|
2010-02-22 12:18:42 +00:00
|
|
|
if ( $params['watch'] ) {
|
2008-03-03 18:19:52 +00:00
|
|
|
$watch = true;
|
2010-02-22 12:18:42 +00:00
|
|
|
} elseif ( $params['unwatch'] ) {
|
2008-03-03 18:19:52 +00:00
|
|
|
$watch = false;
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
2010-04-14 12:17:39 +00:00
|
|
|
if ( $watch ) {
|
2020-09-30 17:43:39 +00:00
|
|
|
$requestArray['wpWatchthis'] = true;
|
|
|
|
|
$watchlistExpiry = $this->getExpiryFromParams( $params );
|
2020-06-04 20:16:23 +00:00
|
|
|
|
2020-09-30 17:43:39 +00:00
|
|
|
if ( $watchlistExpiry ) {
|
2020-06-04 20:16:23 +00:00
|
|
|
$requestArray['wpWatchlistExpiry'] = $watchlistExpiry;
|
|
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
}
|
2008-03-03 18:19:52 +00:00
|
|
|
|
2015-04-15 01:33:08 +00:00
|
|
|
// Apply change tags
|
2017-12-04 18:36:48 +00:00
|
|
|
if ( $params['tags'] ) {
|
2021-02-23 20:45:00 +00:00
|
|
|
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
|
2016-02-20 20:20:37 +00:00
|
|
|
if ( $tagStatus->isOK() ) {
|
2015-04-15 01:33:08 +00:00
|
|
|
$requestArray['wpChangeTags'] = implode( ',', $params['tags'] );
|
|
|
|
|
} else {
|
2016-01-20 09:51:01 +00:00
|
|
|
$this->dieStatus( $tagStatus );
|
2015-04-15 01:33:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 06:26:37 +00:00
|
|
|
// Pass through anything else we might have been given, to support extensions
|
|
|
|
|
// This is kind of a hack but it's the best we can do to make extensions work
|
|
|
|
|
$requestArray += $this->getRequest()->getValues();
|
|
|
|
|
|
2024-09-17 21:01:43 +00:00
|
|
|
// phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage,MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
|
2011-11-17 10:33:20 +00:00
|
|
|
global $wgTitle, $wgRequest;
|
|
|
|
|
|
2011-12-20 04:22:06 +00:00
|
|
|
$req = new DerivativeRequest( $this->getRequest(), $requestArray, true );
|
2011-11-17 10:33:20 +00:00
|
|
|
|
|
|
|
|
// Some functions depend on $wgTitle == $ep->mTitle
|
|
|
|
|
// TODO: Make them not or check if they still do
|
|
|
|
|
$wgTitle = $titleObj;
|
|
|
|
|
|
2013-02-17 10:59:41 +00:00
|
|
|
$articleContext = new RequestContext;
|
|
|
|
|
$articleContext->setRequest( $req );
|
2013-02-24 10:34:50 +00:00
|
|
|
$articleContext->setWikiPage( $pageObj );
|
|
|
|
|
$articleContext->setUser( $this->getUser() );
|
|
|
|
|
|
2017-09-04 18:05:26 +00:00
|
|
|
/** @var Article $articleObject */
|
2013-02-24 10:34:50 +00:00
|
|
|
$articleObject = Article::newFromWikiPage( $pageObj, $articleContext );
|
2013-02-17 10:59:41 +00:00
|
|
|
|
2012-06-25 14:09:08 +00:00
|
|
|
$ep = new EditPage( $articleObject );
|
2012-03-09 17:23:05 +00:00
|
|
|
|
2015-04-15 08:26:22 +00:00
|
|
|
$ep->setApiEditOverride( true );
|
2011-11-17 10:33:20 +00:00
|
|
|
$ep->setContextTitle( $titleObj );
|
2010-01-11 15:55:52 +00:00
|
|
|
$ep->importFormData( $req );
|
Handle collisions from SerialProvider::acquireIndex
Why:
* When using the TempUserCreator::create or ::acquireAndStashName to
get temporary account when the chosen username already existed, that
temporary account is treated as if it doesn't exist. This causes
confusing "userexists" errors and also causes the user to be logged
into an already existing temporary account.
* Furthermore, because the user existence check in AuthManager::auto
CreateUser only checks the local wiki, if an existing temporary
account exists globally but not on the local wiki then the code
sign a new user into an existing temporary account.
* This is very bad behaviour, though shouldn't happen unless the
serialMapping configuration uses a SerialMapping class that could
provide duplicates and/or the configuration has been changed to
use a different SerialMapping class.
* There is a need to change the SerialMapping class in use to a
different class, which means that the code will attempt to use
temporary account usernames which already exist.
* As such, the code that is generating the temporary account usernames
based on the SerialMapping and SerialProvider should be aware that
it may produce an already existing temporary account username, even
if the SerialMapping class being used is asserted to never provide
duplicates.
* Therefore, the code that generates temporary account usernames
should always attempt to verify that a automatically generated
temporary account name does not already exist on the wiki.
What:
* Update TempUserCreator::acquireName to check to see if the username
it generates already exists centrally using the CentralIdLookup.
If it does, then the method returns null. Otherwise, the username
that hasn't been used yet is returned.
* Create the private method TempUserCreator::attemptAutoCreate that
attempts an autocreate for a temporary account name, and optionally
logs the account in.
* Update TempUserCreator::create to use ::attemptAutoCreate to
first to check if the account can be created and then again once
the account is created to actually login to that temporary account.
This is done to prevent logins to existing temporary accounts on
the local wiki. The second call to actually perform the login is
necessary as there is no other way to login to a temporary account.
* Update TempUserCreator::acquireAndStashName to respond to the changes
to ::acquireName, such that it returns null if ::acquireName returns
null and also does not modify the session.
* Update EditPage::maybeActivateTempUserCreate to return a Status and
return a good status in all cases except when a temporary account
name could not be acquired.
* Add IEditObject::AS_UNABLE_TO_ACQUIRE_TEMP_ACCOUNT, and use it as
the value of the fatal status returned by EditPage
::internalAttemptSave if a temporary account name could not be
acquired. This will cause the display of a useful error to the
user on edit.
* Update ApiEditPage and ApiAcquireTempUserName to die with an error
if a temporary account username was unable to be acquired.
* Provide tests for the untested ApiAcquireTempUserName.php file
including testing the new behaviour.
* Add and update tests for TempUserCreator.php
Bug: T353390
Change-Id: Id3a316ea0eba544d51d4ffcdfb03e35f4b3c54cc
2023-12-13 22:56:29 +00:00
|
|
|
$tempUserCreateStatus = $ep->maybeActivateTempUserCreate( true );
|
|
|
|
|
if ( !$tempUserCreateStatus->isOK() ) {
|
|
|
|
|
$this->dieWithError( 'apierror-tempuseracquirefailed', 'tempuseracquirefailed' );
|
|
|
|
|
}
|
2013-07-26 11:43:08 +00:00
|
|
|
|
2020-06-20 15:21:46 +00:00
|
|
|
// T255700: Ensure content models of the base content
|
|
|
|
|
// and fetched revision remain the same before attempting to save.
|
|
|
|
|
$editRevId = $requestArray['editRevId'] ?? false;
|
2021-06-30 20:30:28 +00:00
|
|
|
$baseRev = $this->revisionLookup->getRevisionByTitle( $titleObj, $editRevId );
|
2021-10-02 20:10:35 +00:00
|
|
|
$baseContentModel = null;
|
|
|
|
|
|
|
|
|
|
if ( $baseRev ) {
|
|
|
|
|
$baseContent = $baseRev->getContent( SlotRecord::MAIN );
|
|
|
|
|
$baseContentModel = $baseContent ? $baseContent->getModel() : null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 23:48:27 +00:00
|
|
|
$baseContentModel ??= $pageObj->getContentModel();
|
2020-06-20 15:21:46 +00:00
|
|
|
|
|
|
|
|
// However, allow the content models to possibly differ if we are intentionally
|
|
|
|
|
// changing them or we are doing an undo edit that is reverting content model change.
|
|
|
|
|
$contentModelsCanDiffer = $params['contentmodel'] || isset( $undoContentModel );
|
|
|
|
|
|
|
|
|
|
if ( !$contentModelsCanDiffer && $contentModel !== $baseContentModel ) {
|
|
|
|
|
$this->dieWithError( [ 'apierror-contentmodel-mismatch', $contentModel, $baseContentModel ] );
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-23 22:52:40 +00:00
|
|
|
// Do the actual save
|
2012-06-25 14:09:08 +00:00
|
|
|
$oldRevId = $articleObject->getRevIdFetched();
|
2011-06-20 22:18:11 +00:00
|
|
|
$result = null;
|
2020-06-20 15:21:46 +00:00
|
|
|
|
2010-01-23 22:52:40 +00:00
|
|
|
// Fake $wgRequest for some hooks inside EditPage
|
2011-05-17 22:03:20 +00:00
|
|
|
// @todo FIXME: This interface SUCKS
|
2024-04-22 10:08:14 +00:00
|
|
|
// phpcs:disable MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
|
2008-05-28 09:14:35 +00:00
|
|
|
$oldRequest = $wgRequest;
|
|
|
|
|
$wgRequest = $req;
|
|
|
|
|
|
2015-02-06 19:51:09 +00:00
|
|
|
$status = $ep->attemptSave( $result );
|
2021-01-15 16:18:40 +00:00
|
|
|
$statusValue = is_int( $status->value ) ? $status->value : 0;
|
2008-05-28 09:14:35 +00:00
|
|
|
$wgRequest = $oldRequest;
|
2024-04-22 10:08:14 +00:00
|
|
|
// phpcs:enable MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
|
2010-08-12 14:25:07 +00:00
|
|
|
|
2019-08-30 17:56:27 +00:00
|
|
|
$r = [];
|
2021-01-15 16:18:40 +00:00
|
|
|
switch ( $statusValue ) {
|
2008-03-03 18:19:52 +00:00
|
|
|
case EditPage::AS_HOOK_ERROR:
|
|
|
|
|
case EditPage::AS_HOOK_ERROR_EXPECTED:
|
2023-11-28 11:54:20 +00:00
|
|
|
if ( $status->statusData !== null ) {
|
|
|
|
|
$r = $status->statusData;
|
API edit: allow ConfirmEdit to use the merged parse
ConfirmEdit was tripling the amount of time it took to save a typical
page via the API, since it was assuming that the APIEditBeforeSave hook
was giving unmerged wikitext, requiring a full parse of the content
before and after the edit in order to check the addurl trigger.
Apparently nobody bothered to update it after Ia59fb6bb.
APIEditBeforeSave is unusable anyway, because it is given the serialized
content, without any information about the content model. It really
needs the Content object in order to do it properly. So instead, adapt
EditFilterMergedContent for the purpose. Incidentally, that avoids the
inelegant defined('MW_API') check in ConfirmEdit's
EditFilterMergedContent handler, since both API and normal edits are
handled by EditFilterMergedContent in the same way.
Unfortunately the interpretation of the 'value' member in the Status
object passed to EditFilterMergedContent is not extensible, being an
integer instead of an associative array. So we add a custom member in
order to get the result array back down the stack.
Another obstacle to this design was the lack of an EditPage object
passed to EditFilterMergedContent. ConfirmEdit was previously directly
calling EditPage::showEditForm() with a $formCallback which outputs the
necessary HTML. Instead, I hacked up runPostMergeFilters() a bit, to
allow the hook to request that the edit page be shown again even if
hookError is not set. Then a new EditPage::showEditForm:fields hook does
the necessary HTML output, instead of $formCallback.
Marked $formCallback as deprecated, since I think it is now unused.
Change-Id: I4b4270dd868a643512d4717927858b6ef0556d8a
2014-12-05 04:25:18 +00:00
|
|
|
$r['result'] = 'Failure';
|
|
|
|
|
$apiResult->addValue( null, $this->getModuleName(), $r );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-04 08:34:04 +00:00
|
|
|
if ( !$status->getMessages() ) {
|
2018-03-20 13:25:26 +00:00
|
|
|
// This appears to be unreachable right now, because all
|
|
|
|
|
// code paths will set an error. Could change, though.
|
2020-05-10 00:09:19 +00:00
|
|
|
$status->fatal( 'hookaborted' ); // @codeCoverageIgnore
|
2016-10-19 16:54:25 +00:00
|
|
|
}
|
|
|
|
|
$this->dieStatus( $status );
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2018-03-20 13:25:26 +00:00
|
|
|
// These two cases will normally have been caught earlier, and will
|
|
|
|
|
// only occur if something blocks the user between the earlier
|
|
|
|
|
// check and the check in EditPage (presumably a hook). It's not
|
|
|
|
|
// obvious that this is even possible.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2008-03-03 18:19:52 +00:00
|
|
|
case EditPage::AS_BLOCKED_PAGE_FOR_USER:
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
|
2018-08-27 01:45:18 +00:00
|
|
|
$this->dieBlocked( $user->getBlock() );
|
2021-04-12 21:22:21 +00:00
|
|
|
// dieBlocked prevents continuation
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
case EditPage::AS_READ_ONLY_PAGE:
|
2009-07-12 12:38:03 +00:00
|
|
|
$this->dieReadOnly();
|
2018-03-20 13:25:26 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
case EditPage::AS_SUCCESS_NEW_ARTICLE:
|
2015-01-16 19:00:07 +00:00
|
|
|
$r['new'] = true;
|
2013-03-11 03:45:51 +00:00
|
|
|
// fall-through
|
2010-07-06 13:15:59 +00:00
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
case EditPage::AS_SUCCESS_UPDATE:
|
2010-02-22 12:18:42 +00:00
|
|
|
$r['result'] = 'Success';
|
2019-02-25 00:38:33 +00:00
|
|
|
$r['pageid'] = (int)$titleObj->getArticleID();
|
2008-03-03 18:19:52 +00:00
|
|
|
$r['title'] = $titleObj->getPrefixedText();
|
2020-03-21 04:04:40 +00:00
|
|
|
$r['contentmodel'] = $articleObject->getPage()->getContentModel();
|
|
|
|
|
$newRevId = $articleObject->getPage()->getLatest();
|
2010-02-22 12:18:42 +00:00
|
|
|
if ( $newRevId == $oldRevId ) {
|
2015-01-16 19:00:07 +00:00
|
|
|
$r['nochange'] = true;
|
2010-02-22 12:18:42 +00:00
|
|
|
} else {
|
2019-02-25 00:38:33 +00:00
|
|
|
$r['oldrevid'] = (int)$oldRevId;
|
|
|
|
|
$r['newrevid'] = (int)$newRevId;
|
2010-01-11 15:55:52 +00:00
|
|
|
$r['newtimestamp'] = wfTimestamp( TS_ISO_8601,
|
2012-06-25 14:09:08 +00:00
|
|
|
$pageObj->getTimestamp() );
|
2008-03-03 18:19:52 +00:00
|
|
|
}
|
2020-06-04 20:16:23 +00:00
|
|
|
|
|
|
|
|
if ( $watch ) {
|
2020-09-30 17:43:39 +00:00
|
|
|
$r['watched'] = true;
|
2020-06-04 20:16:23 +00:00
|
|
|
|
2020-09-30 17:43:39 +00:00
|
|
|
$watchlistExpiry = $this->getWatchlistExpiry(
|
2021-06-30 20:30:28 +00:00
|
|
|
$this->watchedItemStore,
|
2020-09-30 17:43:39 +00:00
|
|
|
$titleObj,
|
|
|
|
|
$user
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $watchlistExpiry ) {
|
|
|
|
|
$r['watchlistexpiry'] = $watchlistExpiry;
|
2020-06-04 20:16:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 22:34:58 +00:00
|
|
|
$this->persistGlobalSession();
|
2023-06-02 23:51:46 +00:00
|
|
|
|
2024-04-19 07:13:09 +00:00
|
|
|
// If the temporary account was created in this request,
|
|
|
|
|
// or if the temporary account has zero edits (implying
|
|
|
|
|
// that the account was created during a failed edit
|
|
|
|
|
// attempt in a previous request), perform the top-level
|
|
|
|
|
// redirect to ensure the account is attached.
|
|
|
|
|
// Note that the temp user could already have performed
|
|
|
|
|
// the top-level redirect if this a first edit on
|
|
|
|
|
// a wiki that is not the user's home wiki.
|
|
|
|
|
$shouldRedirectForTempUser = isset( $result['savedTempUser'] ) ||
|
2024-07-29 15:41:20 +00:00
|
|
|
( $user->isTemp() && ( $user->getEditCount() === 0 ) );
|
2024-04-19 07:13:09 +00:00
|
|
|
if ( $shouldRedirectForTempUser ) {
|
2023-06-02 23:51:46 +00:00
|
|
|
$r['tempusercreated'] = true;
|
2023-10-31 20:10:38 +00:00
|
|
|
$params['returnto'] ??= $titleObj->getPrefixedDBkey();
|
|
|
|
|
$redirectUrl = $this->getTempUserRedirectUrl(
|
|
|
|
|
$params,
|
2024-04-19 07:13:09 +00:00
|
|
|
$result['savedTempUser'] ?? $user
|
2023-06-02 23:51:46 +00:00
|
|
|
);
|
2023-10-31 20:10:38 +00:00
|
|
|
if ( $redirectUrl ) {
|
2023-06-02 23:51:46 +00:00
|
|
|
$r['tempusercreatedredirect'] = $redirectUrl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-03 18:19:52 +00:00
|
|
|
break;
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2012-05-10 04:48:16 +00:00
|
|
|
default:
|
2024-05-04 08:34:04 +00:00
|
|
|
if ( !$status->getMessages() ) {
|
2016-12-19 14:49:59 +00:00
|
|
|
// EditPage sometimes only sets the status code without setting
|
|
|
|
|
// any actual error messages. Supply defaults for those cases.
|
2021-01-15 16:18:40 +00:00
|
|
|
switch ( $statusValue ) {
|
2016-12-19 14:49:59 +00:00
|
|
|
// Currently needed
|
|
|
|
|
case EditPage::AS_IMAGE_REDIRECT_ANON:
|
|
|
|
|
$status->fatal( 'apierror-noimageredirect-anon' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_IMAGE_REDIRECT_LOGGED:
|
2018-03-20 13:25:26 +00:00
|
|
|
$status->fatal( 'apierror-noimageredirect' );
|
2016-12-19 14:49:59 +00:00
|
|
|
break;
|
|
|
|
|
case EditPage::AS_CONTENT_TOO_BIG:
|
|
|
|
|
case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
|
2022-04-13 15:28:26 +00:00
|
|
|
$status->fatal( 'apierror-contenttoobig',
|
|
|
|
|
$this->getConfig()->get( MainConfigNames::MaxArticleSize ) );
|
2016-12-19 14:49:59 +00:00
|
|
|
break;
|
|
|
|
|
case EditPage::AS_READ_ONLY_PAGE_ANON:
|
|
|
|
|
$status->fatal( 'apierror-noedit-anon' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_NO_CHANGE_CONTENT_MODEL:
|
|
|
|
|
$status->fatal( 'apierror-cantchangecontentmodel' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_ARTICLE_WAS_DELETED:
|
|
|
|
|
$status->fatal( 'apierror-pagedeleted' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_CONFLICT_DETECTED:
|
2019-12-13 14:45:24 +00:00
|
|
|
$status->fatal( 'edit-conflict' );
|
2016-12-19 14:49:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Currently shouldn't be needed, but here in case
|
|
|
|
|
// hooks use them without setting appropriate
|
|
|
|
|
// errors on the status.
|
2018-03-20 13:25:26 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-19 14:49:59 +00:00
|
|
|
case EditPage::AS_SPAM_ERROR:
|
2022-10-08 12:12:34 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
|
2016-12-19 14:49:59 +00:00
|
|
|
$status->fatal( 'apierror-spamdetected', $result['spam'] );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_READ_ONLY_PAGE_LOGGED:
|
|
|
|
|
$status->fatal( 'apierror-noedit' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_RATE_LIMITED:
|
|
|
|
|
$status->fatal( 'apierror-ratelimited' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_NO_CREATE_PERMISSION:
|
|
|
|
|
$status->fatal( 'nocreate-loggedin' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_BLANK_ARTICLE:
|
|
|
|
|
$status->fatal( 'apierror-emptypage' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_TEXTBOX_EMPTY:
|
|
|
|
|
$status->fatal( 'apierror-emptynewsection' );
|
|
|
|
|
break;
|
|
|
|
|
case EditPage::AS_SUMMARY_NEEDED:
|
|
|
|
|
$status->fatal( 'apierror-summaryrequired' );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-01-15 16:18:40 +00:00
|
|
|
wfWarn( __METHOD__ . ": Unknown EditPage code $statusValue with no message" );
|
|
|
|
|
$status->fatal( 'apierror-unknownerror-editpage', $statusValue );
|
2016-12-19 14:49:59 +00:00
|
|
|
break;
|
2018-03-20 13:25:26 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-10-19 16:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->dieStatus( $status );
|
2008-03-03 18:19:52 +00:00
|
|
|
}
|
2011-06-20 22:18:11 +00:00
|
|
|
$apiResult->addValue( null, $this->getModuleName(), $r );
|
2008-03-03 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-23 22:02:23 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getAllowedParams() {
|
2020-06-04 20:16:23 +00:00
|
|
|
$params = [
|
2016-02-17 09:09:32 +00:00
|
|
|
'title' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'pageid' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2008-03-12 13:25:12 +00:00
|
|
|
'section' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'sectiontitle' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'text' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2008-03-03 18:19:52 +00:00
|
|
|
'summary' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'tags' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'tags',
|
|
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2008-03-03 18:19:52 +00:00
|
|
|
'minor' => false,
|
|
|
|
|
'notminor' => false,
|
|
|
|
|
'bot' => false,
|
2020-03-05 21:23:07 +00:00
|
|
|
'baserevid' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2020-03-05 21:23:07 +00:00
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
'basetimestamp' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'timestamp',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'starttimestamp' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'timestamp',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2008-03-03 18:19:52 +00:00
|
|
|
'recreate' => false,
|
2008-03-25 21:12:01 +00:00
|
|
|
'createonly' => false,
|
2008-06-15 20:37:28 +00:00
|
|
|
'nocreate' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
'watch' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => false,
|
2022-06-06 01:24:41 +00:00
|
|
|
ParamValidator::PARAM_DEPRECATED => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'unwatch' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => false,
|
2022-06-06 01:24:41 +00:00
|
|
|
ParamValidator::PARAM_DEPRECATED => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2020-06-04 20:16:23 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Params appear in the docs in the order they are defined,
|
|
|
|
|
// which is why this is here and not at the bottom.
|
|
|
|
|
$params += $this->getWatchlistParams();
|
|
|
|
|
|
2023-10-31 20:10:38 +00:00
|
|
|
$params += [
|
2008-05-28 09:22:40 +00:00
|
|
|
'md5' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'prependtext' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'appendtext' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'undo' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
|
IntegerDef::PARAM_MIN => 0,
|
2018-03-20 13:25:26 +00:00
|
|
|
ApiBase::PARAM_RANGE_ENFORCE => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'undoafter' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
|
|
|
|
IntegerDef::PARAM_MIN => 0,
|
2018-03-20 13:25:26 +00:00
|
|
|
ApiBase::PARAM_RANGE_ENFORCE => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'redirect' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'boolean',
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'contentformat' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'contentmodel' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'token' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
// Standard definition automatically inserted
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_APPEND => [ 'apihelp-edit-param-token' ],
|
|
|
|
|
],
|
|
|
|
|
];
|
2023-10-31 20:10:38 +00:00
|
|
|
|
|
|
|
|
$params += $this->getCreateTempUserParams();
|
|
|
|
|
|
|
|
|
|
return $params;
|
2008-03-03 18:19:52 +00:00
|
|
|
}
|
2010-02-22 12:18:42 +00:00
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2008-03-03 18:19:52 +00:00
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=edit&title=Test&summary=test%20summary&' .
|
2020-03-05 21:23:07 +00:00
|
|
|
'text=article%20content&baserevid=1234567&token=123ABC'
|
2014-09-18 17:38:23 +00:00
|
|
|
=> 'apihelp-edit-example-edit',
|
|
|
|
|
'action=edit&title=Test&summary=NOTOC&minor=&' .
|
|
|
|
|
'prependtext=__NOTOC__%0A&basetimestamp=2007-08-24T12:34:54Z&token=123ABC'
|
|
|
|
|
=> 'apihelp-edit-example-prepend',
|
|
|
|
|
'action=edit&title=Test&undo=13585&undoafter=13579&' .
|
|
|
|
|
'basetimestamp=2007-08-24T12:34:54Z&token=123ABC'
|
|
|
|
|
=> 'apihelp-edit-example-undo',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2008-03-03 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Edit';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2008-03-12 13:25:12 +00:00
|
|
|
}
|
2024-09-25 16:17:29 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( ApiEditPage::class, 'ApiEditPage' );
|