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
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
;
32 * API Module to merge page histories
35 class ApiMergeHistory
extends ApiBase
{
37 private MergeHistoryFactory
$mergeHistoryFactory;
39 public function __construct(
42 MergeHistoryFactory
$mergeHistoryFactory
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'] );
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'] );
77 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
81 $reason = $params['reason'];
82 $timestamp = $params['timestamp'];
85 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable T240141
86 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
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
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() {
121 public function isWriteMode() {
125 public function getAllowedParams() {
129 ParamValidator
::PARAM_TYPE
=> 'integer'
133 ParamValidator
::PARAM_TYPE
=> 'integer'
136 ParamValidator
::PARAM_TYPE
=> 'timestamp'
142 public function needsToken() {
146 protected function getExamplesMessages() {
148 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
150 => 'apihelp-mergehistory-example-merge',
151 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
152 'reason=Reason×tamp=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' );