Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiMergeHistory.php
blob74f8353bd319171e155ad303b35e296ef0043e79
1 <?php
2 /**
3 * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Page\MergeHistoryFactory;
26 use MediaWiki\Page\PageIdentity;
27 use MediaWiki\Status\Status;
28 use MediaWiki\Title\Title;
29 use Wikimedia\ParamValidator\ParamValidator;
31 /**
32 * API Module to merge page histories
33 * @ingroup API
35 class ApiMergeHistory extends ApiBase {
37 private MergeHistoryFactory $mergeHistoryFactory;
39 public function __construct(
40 ApiMain $mainModule,
41 string $moduleName,
42 MergeHistoryFactory $mergeHistoryFactory
43 ) {
44 parent::__construct( $mainModule, $moduleName );
45 $this->mergeHistoryFactory = $mergeHistoryFactory;
48 public function execute() {
49 $this->useTransactionalTimeLimit();
51 $params = $this->extractRequestParams();
53 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
54 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
56 // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
57 if ( isset( $params['from'] ) ) {
58 $fromTitle = Title::newFromText( $params['from'] );
59 if ( !$fromTitle || $fromTitle->isExternal() ) {
60 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
62 } elseif ( isset( $params['fromid'] ) ) {
63 $fromTitle = Title::newFromID( $params['fromid'] );
64 if ( !$fromTitle ) {
65 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
69 if ( isset( $params['to'] ) ) {
70 $toTitle = Title::newFromText( $params['to'] );
71 if ( !$toTitle || $toTitle->isExternal() ) {
72 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
74 } elseif ( isset( $params['toid'] ) ) {
75 $toTitle = Title::newFromID( $params['toid'] );
76 if ( !$toTitle ) {
77 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
81 $reason = $params['reason'];
82 $timestamp = $params['timestamp'];
84 // Merge!
85 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable T240141
86 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
91 $r = [
92 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
93 'from' => $fromTitle->getPrefixedText(),
94 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
95 'to' => $toTitle->getPrefixedText(),
96 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
97 'reason' => $params['reason']
99 $result = $this->getResult();
101 $result->addValue( null, $this->getModuleName(), $r );
105 * @param PageIdentity $from
106 * @param PageIdentity $to
107 * @param string $timestamp
108 * @param string $reason
109 * @return Status
111 protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) {
112 $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp );
114 return $mh->merge( $this->getAuthority(), $reason );
117 public function mustBePosted() {
118 return true;
121 public function isWriteMode() {
122 return true;
125 public function getAllowedParams() {
126 return [
127 'from' => null,
128 'fromid' => [
129 ParamValidator::PARAM_TYPE => 'integer'
131 'to' => null,
132 'toid' => [
133 ParamValidator::PARAM_TYPE => 'integer'
135 'timestamp' => [
136 ParamValidator::PARAM_TYPE => 'timestamp'
138 'reason' => '',
142 public function needsToken() {
143 return 'csrf';
146 protected function getExamplesMessages() {
147 return [
148 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
149 'reason=Reason'
150 => 'apihelp-mergehistory-example-merge',
151 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
152 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
153 => 'apihelp-mergehistory-example-merge-timestamp',
157 public function getHelpUrls() {
158 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
162 /** @deprecated class alias since 1.43 */
163 class_alias( ApiMergeHistory::class, 'ApiMergeHistory' );