MediaWiki master
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
28
33class ApiMergeHistory extends ApiBase {
34
35 private MergeHistoryFactory $mergeHistoryFactory;
36
42 public function __construct(
43 ApiMain $mainModule,
44 $moduleName,
45 MergeHistoryFactory $mergeHistoryFactory
46 ) {
47 parent::__construct( $mainModule, $moduleName );
48 $this->mergeHistoryFactory = $mergeHistoryFactory;
49 }
50
51 public function execute() {
53
55
56 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
57 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
58
59 // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
60 if ( isset( $params['from'] ) ) {
61 $fromTitle = Title::newFromText( $params['from'] );
62 if ( !$fromTitle || $fromTitle->isExternal() ) {
63 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
64 }
65 } elseif ( isset( $params['fromid'] ) ) {
66 $fromTitle = Title::newFromID( $params['fromid'] );
67 if ( !$fromTitle ) {
68 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
69 }
70 }
71
72 if ( isset( $params['to'] ) ) {
73 $toTitle = Title::newFromText( $params['to'] );
74 if ( !$toTitle || $toTitle->isExternal() ) {
75 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
76 }
77 } elseif ( isset( $params['toid'] ) ) {
78 $toTitle = Title::newFromID( $params['toid'] );
79 if ( !$toTitle ) {
80 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
81 }
82 }
83
84 $reason = $params['reason'];
85 $timestamp = $params['timestamp'];
86
87 // Merge!
88 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable T240141
89 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
90 if ( !$status->isOK() ) {
91 $this->dieStatus( $status );
92 }
93
94 $r = [
95 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
96 'from' => $fromTitle->getPrefixedText(),
97 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
98 'to' => $toTitle->getPrefixedText(),
99 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
100 'reason' => $params['reason']
101 ];
102 $result = $this->getResult();
103
104 $result->addValue( null, $this->getModuleName(), $r );
105 }
106
114 protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) {
115 $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp );
116
117 return $mh->merge( $this->getAuthority(), $reason );
118 }
119
120 public function mustBePosted() {
121 return true;
122 }
123
124 public function isWriteMode() {
125 return true;
126 }
127
128 public function getAllowedParams() {
129 return [
130 'from' => null,
131 'fromid' => [
132 ParamValidator::PARAM_TYPE => 'integer'
133 ],
134 'to' => null,
135 'toid' => [
136 ParamValidator::PARAM_TYPE => 'integer'
137 ],
138 'timestamp' => [
139 ParamValidator::PARAM_TYPE => 'timestamp'
140 ],
141 'reason' => '',
142 ];
143 }
144
145 public function needsToken() {
146 return 'csrf';
147 }
148
149 protected function getExamplesMessages() {
150 return [
151 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
152 'reason=Reason'
153 => 'apihelp-mergehistory-example-merge',
154 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
155 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
156 => 'apihelp-mergehistory-example-merge-timestamp',
157 ];
158 }
159
160 public function getHelpUrls() {
161 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
162 }
163}
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:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1533
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:950
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1589
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1381
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
API Module to merge page histories.
__construct(ApiMain $mainModule, $moduleName, MergeHistoryFactory $mergeHistoryFactory)
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
isWriteMode()
Indicates whether this module requires write mode.
needsToken()
Returns the token type this module requires in order to execute.
mustBePosted()
Indicates whether this module must be called with a POST request.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
merge(PageIdentity $from, PageIdentity $to, $timestamp, $reason)
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.