Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiMergeHistory.php
blob357698e13c40eba25fce32d29f69e7fbb7674857
1 <?php
2 /**
5 * Created on Dec 29, 2015
7 * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
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.
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.
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
24 * @file
27 /**
28 * API Module to merge page histories
29 * @ingroup API
31 class ApiMergeHistory extends ApiBase {
33 public function execute() {
34 $this->useTransactionalTimeLimit();
36 $params = $this->extractRequestParams();
38 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
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'] ) ] );
47 } elseif ( isset( $params['fromid'] ) ) {
48 $fromTitle = Title::newFromID( $params['fromid'] );
49 if ( !$fromTitle ) {
50 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
54 if ( isset( $params['to'] ) ) {
55 $toTitle = Title::newFromText( $params['to'] );
56 if ( !$toTitle || $toTitle->isExternal() ) {
57 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
59 } elseif ( isset( $params['toid'] ) ) {
60 $toTitle = Title::newFromID( $params['toid'] );
61 if ( !$toTitle ) {
62 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
66 $reason = $params['reason'];
67 $timestamp = $params['timestamp'];
69 // Merge!
70 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
71 if ( !$status->isOK() ) {
72 $this->dieStatus( $status );
75 $r = [
76 'from' => $fromTitle->getPrefixedText(),
77 'to' => $toTitle->getPrefixedText(),
78 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
79 'reason' => $params['reason']
81 $result = $this->getResult();
83 $result->addValue( null, $this->getModuleName(), $r );
86 /**
87 * @param Title $from
88 * @param Title $to
89 * @param string $timestamp
90 * @param string $reason
91 * @return Status
93 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
94 $mh = new MergeHistory( $from, $to, $timestamp );
96 return $mh->merge( $this->getUser(), $reason );
99 public function mustBePosted() {
100 return true;
103 public function isWriteMode() {
104 return true;
107 public function getAllowedParams() {
108 return [
109 'from' => null,
110 'fromid' => [
111 ApiBase::PARAM_TYPE => 'integer'
113 'to' => null,
114 'toid' => [
115 ApiBase::PARAM_TYPE => 'integer'
117 'timestamp' => [
118 ApiBase::PARAM_TYPE => 'timestamp'
120 'reason' => '',
124 public function needsToken() {
125 return 'csrf';
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',
139 public function getHelpUrls() {
140 return 'https://www.mediawiki.org/wiki/API:Mergehistory';