Merge "Get file from WikiFilePage, instead of ImagePage"
[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 $user = $this->getUser();
37 $params = $this->extractRequestParams();
38
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
40 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
41
42 // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
43 if ( isset( $params['from'] ) ) {
44 $fromTitle = Title::newFromText( $params['from'] );
45 if ( !$fromTitle || $fromTitle->isExternal() ) {
46 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
47 }
48 } elseif ( isset( $params['fromid'] ) ) {
49 $fromTitle = Title::newFromID( $params['fromid'] );
50 if ( !$fromTitle ) {
51 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
52 }
53 }
54
55 if ( isset( $params['to'] ) ) {
56 $toTitle = Title::newFromText( $params['to'] );
57 if ( !$toTitle || $toTitle->isExternal() ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
59 }
60 } elseif ( isset( $params['toid'] ) ) {
61 $toTitle = Title::newFromID( $params['toid'] );
62 if ( !$toTitle ) {
63 $this->dieUsageMsg( array( 'nosuchpageid', $params['toid'] ) );
64 }
65 }
66
67 $reason = $params['reason'];
68 $timestamp = $params['timestamp'];
69
70 // Merge!
71 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
72 if ( !$status->isOK() ) {
73 $this->dieStatus( $status );
74 }
75
76 $r = array(
77 'from' => $fromTitle->getPrefixedText(),
78 'to' => $toTitle->getPrefixedText(),
79 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
80 'reason' => $params['reason']
81 );
82 $result = $this->getResult();
83
84 $result->addValue( null, $this->getModuleName(), $r );
85 }
86
87 /**
88 * @param Title $from
89 * @param Title $to
90 * @param string $timestamp
91 * @param string $reason
92 * @return Status
93 */
94 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
95 $mh = new MergeHistory( $from, $to, $timestamp );
96
97 return $mh->merge( $this->getUser(), $reason );
98 }
99
100 public function mustBePosted() {
101 return true;
102 }
103
104 public function isWriteMode() {
105 return true;
106 }
107
108 public function getAllowedParams() {
109 return array(
110 'from' => null,
111 'fromid' => array(
112 ApiBase::PARAM_TYPE => 'integer'
113 ),
114 'to' => null,
115 'toid' => array(
116 ApiBase::PARAM_TYPE => 'integer'
117 ),
118 'timestamp' => array(
119 ApiBase::PARAM_TYPE => 'timestamp'
120 ),
121 'reason' => '',
122 );
123 }
124
125 public function needsToken() {
126 return 'csrf';
127 }
128
129 protected function getExamplesMessages() {
130 return array(
131 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
132 'reason=Reason'
133 => 'apihelp-mergehistory-example-merge',
134 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
135 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
136 => 'apihelp-mergehistory-example-merge-timestamp',
137 );
138 }
139
140 public function getHelpUrls() {
141 return 'https://www.mediawiki.org/wiki/API:Mergehistory';
142 }
143 }