MediaWiki master
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
30
35class 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() {
50
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
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
163class_alias( ApiMergeHistory::class, 'ApiMergeHistory' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1425
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:990
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
API Module to merge page histories.
isWriteMode()
Indicates whether this module requires write access to the wiki.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiMain $mainModule, string $moduleName, MergeHistoryFactory $mergeHistoryFactory)
getHelpUrls()
Return links to more detailed help pages about the module.
mustBePosted()
Indicates whether this module must be called with a POST request.
merge(PageIdentity $from, PageIdentity $to, $timestamp, $reason)
needsToken()
Returns the token type this module requires in order to execute.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Service for mergehistory actions.
Interface for objects (potentially) representing an editable wiki page.