Debian Codegraph @

includes/api/ApiMergeHistory.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2015 Geoffrey Mon <[email protected]>
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 namespace MediaWiki\Api;
24
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;
30
31 /**
32 * API Module to merge page histories
33 * @ingroup API
34 */
35 class ApiMergeHistory extends ApiBase {
36
37 private MergeHistoryFactory $mergeHistoryFactory;
38
39 public function __construct(
40 ApiMain $mainModule,
41 string $moduleName,
42 MergeHistoryFactory $mergeHistoryFactory
43 ) {
44 parent::__construct( $mainModule, $moduleName );
45 $this->mergeHistoryFactory = $mergeHistoryFactory;
46 }
47
48 public function execute() {
49 $this->useTransactionalTimeLimit();
50
51 $params = $this->extractRequestParams();
52
53 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
54 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
55
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'] ) ] );
61 }
62 } elseif ( isset( $params['fromid'] ) ) {
63 $fromTitle = Title::newFromID( $params['fromid'] );
64 if ( !$fromTitle ) {
65 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
66 }
67 }
68
69 if ( isset( $params['to'] ) ) {
70 $toTitle = Title::newFromText( $params['to'] );
71 if ( !$toTitle || $toTitle->isExternal() ) {
72 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
73 }
74 } elseif ( isset( $params['toid'] ) ) {
75 $toTitle = Title::newFromID( $params['toid'] );
76 if ( !$toTitle ) {
77 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
78 }
79 }
80
81 $reason = $params['reason'];
82 $timestamp = $params['timestamp'];
83
84 // Merge!
85 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable T240141
86 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
89 }
90
91 $r = [
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']
98 ];
99 $result = $this->getResult();
100
101 $result->addValue( null, $this->getModuleName(), $r );
102 }
103
104 /**
105 * @param PageIdentity $from
106 * @param PageIdentity $to
107 * @param string $timestamp
108 * @param string $reason
109 * @return Status
110 */
111 protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) {
112 $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp );
113
114 return $mh->merge( $this->getAuthority(), $reason );
115 }
116
117 public function mustBePosted() {
118 return true;
119 }
120
121 public function isWriteMode() {
122 return true;
123 }
124
125 public function getAllowedParams() {
126 return [
127 'from' => null,
128 'fromid' => [
129 ParamValidator::PARAM_TYPE => 'integer'
130 ],
131 'to' => null,
132 'toid' => [
133 ParamValidator::PARAM_TYPE => 'integer'
134 ],
135 'timestamp' => [
136 ParamValidator::PARAM_TYPE => 'timestamp'
137 ],
138 'reason' => '',
139 ];
140 }
141
142 public function needsToken() {
143 return 'csrf';
144 }
145
146 protected function getExamplesMessages() {
147 return [
148 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
149 'reason=Reason'
150 => 'apihelp-mergehistory-example-merge',
151 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
152 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
153 => 'apihelp-mergehistory-example-merge-timestamp',
154 ];
155 }
156
157 public function getHelpUrls() {
158 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
159 }
160 }
161
162 /** @deprecated class alias since 1.43 */
163 class_alias( ApiMergeHistory::class, 'ApiMergeHistory' );