Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / includes / api / ApiMergeHistory.php
1 <?php
2 /**
3 * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * API Module to merge page histories
25 * @ingroup API
26 */
27 class ApiMergeHistory extends ApiBase {
28
29 public function execute() {
30 $this->useTransactionalTimeLimit();
31
32 $params = $this->extractRequestParams();
33
34 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
35 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
36
37 // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
38 if ( isset( $params['from'] ) ) {
39 $fromTitle = Title::newFromText( $params['from'] );
40 if ( !$fromTitle || $fromTitle->isExternal() ) {
41 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
42 }
43 } elseif ( isset( $params['fromid'] ) ) {
44 $fromTitle = Title::newFromID( $params['fromid'] );
45 if ( !$fromTitle ) {
46 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
47 }
48 }
49
50 if ( isset( $params['to'] ) ) {
51 $toTitle = Title::newFromText( $params['to'] );
52 if ( !$toTitle || $toTitle->isExternal() ) {
53 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
54 }
55 } elseif ( isset( $params['toid'] ) ) {
56 $toTitle = Title::newFromID( $params['toid'] );
57 if ( !$toTitle ) {
58 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
59 }
60 }
61
62 $reason = $params['reason'];
63 $timestamp = $params['timestamp'];
64
65 // Merge!
66 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
67 if ( !$status->isOK() ) {
68 $this->dieStatus( $status );
69 }
70
71 $r = [
72 'from' => $fromTitle->getPrefixedText(),
73 'to' => $toTitle->getPrefixedText(),
74 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
75 'reason' => $params['reason']
76 ];
77 $result = $this->getResult();
78
79 $result->addValue( null, $this->getModuleName(), $r );
80 }
81
82 /**
83 * @param Title $from
84 * @param Title $to
85 * @param string $timestamp
86 * @param string $reason
87 * @return Status
88 */
89 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
90 $mh = new MergeHistory( $from, $to, $timestamp );
91
92 return $mh->merge( $this->getUser(), $reason );
93 }
94
95 public function mustBePosted() {
96 return true;
97 }
98
99 public function isWriteMode() {
100 return true;
101 }
102
103 public function getAllowedParams() {
104 return [
105 'from' => null,
106 'fromid' => [
107 ApiBase::PARAM_TYPE => 'integer'
108 ],
109 'to' => null,
110 'toid' => [
111 ApiBase::PARAM_TYPE => 'integer'
112 ],
113 'timestamp' => [
114 ApiBase::PARAM_TYPE => 'timestamp'
115 ],
116 'reason' => '',
117 ];
118 }
119
120 public function needsToken() {
121 return 'csrf';
122 }
123
124 protected function getExamplesMessages() {
125 return [
126 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
127 'reason=Reason'
128 => 'apihelp-mergehistory-example-merge',
129 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
130 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
131 => 'apihelp-mergehistory-example-merge-timestamp',
132 ];
133 }
134
135 public function getHelpUrls() {
136 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
137 }
138 }