Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / api / ApiMergeHistory.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 29, 2015
6 *
7 * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API Module to merge page histories
29 * @ingroup API
30 */
31 class ApiMergeHistory extends ApiBase {
32
33 public function execute() {
34 $this->useTransactionalTimeLimit();
35
36 $params = $this->extractRequestParams();
37
38 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
40
41 // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
42 if ( isset( $params['from'] ) ) {
43 $fromTitle = Title::newFromText( $params['from'] );
44 if ( !$fromTitle || $fromTitle->isExternal() ) {
45 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
46 }
47 } elseif ( isset( $params['fromid'] ) ) {
48 $fromTitle = Title::newFromID( $params['fromid'] );
49 if ( !$fromTitle ) {
50 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
51 }
52 }
53
54 if ( isset( $params['to'] ) ) {
55 $toTitle = Title::newFromText( $params['to'] );
56 if ( !$toTitle || $toTitle->isExternal() ) {
57 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
58 }
59 } elseif ( isset( $params['toid'] ) ) {
60 $toTitle = Title::newFromID( $params['toid'] );
61 if ( !$toTitle ) {
62 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
63 }
64 }
65
66 $reason = $params['reason'];
67 $timestamp = $params['timestamp'];
68
69 // Merge!
70 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
71 if ( !$status->isOK() ) {
72 $this->dieStatus( $status );
73 }
74
75 $r = [
76 'from' => $fromTitle->getPrefixedText(),
77 'to' => $toTitle->getPrefixedText(),
78 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
79 'reason' => $params['reason']
80 ];
81 $result = $this->getResult();
82
83 $result->addValue( null, $this->getModuleName(), $r );
84 }
85
86 /**
87 * @param Title $from
88 * @param Title $to
89 * @param string $timestamp
90 * @param string $reason
91 * @return Status
92 */
93 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
94 $mh = new MergeHistory( $from, $to, $timestamp );
95
96 return $mh->merge( $this->getUser(), $reason );
97 }
98
99 public function mustBePosted() {
100 return true;
101 }
102
103 public function isWriteMode() {
104 return true;
105 }
106
107 public function getAllowedParams() {
108 return [
109 'from' => null,
110 'fromid' => [
111 ApiBase::PARAM_TYPE => 'integer'
112 ],
113 'to' => null,
114 'toid' => [
115 ApiBase::PARAM_TYPE => 'integer'
116 ],
117 'timestamp' => [
118 ApiBase::PARAM_TYPE => 'timestamp'
119 ],
120 'reason' => '',
121 ];
122 }
123
124 public function needsToken() {
125 return 'csrf';
126 }
127
128 protected function getExamplesMessages() {
129 return [
130 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
131 'reason=Reason'
132 => 'apihelp-mergehistory-example-merge',
133 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
134 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
135 => 'apihelp-mergehistory-example-merge-timestamp',
136 ];
137 }
138
139 public function getHelpUrls() {
140 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
141 }
142 }