MediaWiki master
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
16
21class ApiMergeHistory extends ApiBase {
22
23 private MergeHistoryFactory $mergeHistoryFactory;
24
25 public function __construct(
26 ApiMain $mainModule,
27 string $moduleName,
28 MergeHistoryFactory $mergeHistoryFactory
29 ) {
30 parent::__construct( $mainModule, $moduleName );
31 $this->mergeHistoryFactory = $mergeHistoryFactory;
32 }
33
34 public function execute() {
36
37 $params = $this->extractRequestParams();
38
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
40 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
41
42 // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
43 if ( isset( $params['from'] ) ) {
44 $fromTitle = Title::newFromText( $params['from'] );
45 if ( !$fromTitle || $fromTitle->isExternal() ) {
46 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
47 }
48 } elseif ( isset( $params['fromid'] ) ) {
49 $fromTitle = Title::newFromID( $params['fromid'] );
50 if ( !$fromTitle ) {
51 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
52 }
53 } else {
54 self::dieDebug( __METHOD__, 'unknown from parameter' );
55 }
56
57 if ( isset( $params['to'] ) ) {
58 $toTitle = Title::newFromText( $params['to'] );
59 if ( !$toTitle || $toTitle->isExternal() ) {
60 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
61 }
62 } elseif ( isset( $params['toid'] ) ) {
63 $toTitle = Title::newFromID( $params['toid'] );
64 if ( !$toTitle ) {
65 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
66 }
67 } else {
68 self::dieDebug( __METHOD__, 'unknown to parameter' );
69 }
70
71 $reason = $params['reason'];
72 $timestamp = $params['timestamp'] ?? '';
73 $startTimestamp = $params['starttimestamp'] ?? '';
74
75 // Merge!
76 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason, $startTimestamp );
77 if ( !$status->isOK() ) {
78 $this->dieStatus( $status );
79 }
80
81 $r = [
82 'from' => $fromTitle->getPrefixedText(),
83 'to' => $toTitle->getPrefixedText(),
84 'timestamp' => $params['timestamp'],
85 'reason' => $params['reason']
86 ];
87 $result = $this->getResult();
88
89 $result->addValue( null, $this->getModuleName(), $r );
90 }
91
100 protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason, $startTimestamp ) {
101 $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp, $startTimestamp );
102
103 return $mh->merge( $this->getAuthority(), $reason );
104 }
105
107 public function mustBePosted() {
108 return true;
109 }
110
112 public function isWriteMode() {
113 return true;
114 }
115
117 public function getAllowedParams() {
118 return [
119 'from' => null,
120 'fromid' => [
121 ParamValidator::PARAM_TYPE => 'integer'
122 ],
123 'to' => null,
124 'toid' => [
125 ParamValidator::PARAM_TYPE => 'integer'
126 ],
127 // This can either be a timestamp or a timestamp-with-ID pair; don't reject the latter in validation
128 'timestamp' => null,
129 'reason' => '',
130 'starttimestamp' => null
131 ];
132 }
133
135 public function needsToken() {
136 return 'csrf';
137 }
138
140 protected function getExamplesMessages() {
141 return [
142 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
143 'reason=Reason'
144 => 'apihelp-mergehistory-example-merge',
145 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
146 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
147 => 'apihelp-mergehistory-example-merge-timestamp',
148 ];
149 }
150
152 public function getHelpUrls() {
153 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
154 }
155}
156
158class_alias( ApiMergeHistory::class, 'ApiMergeHistory' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1748
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:961
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
API Module to merge page histories.
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
__construct(ApiMain $mainModule, string $moduleName, MergeHistoryFactory $mergeHistoryFactory)
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
merge(PageIdentity $from, PageIdentity $to, $timestamp, $reason, $startTimestamp)
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
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:44
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Service for mergehistory actions.
Interface for objects (potentially) representing an editable wiki page.