MediaWiki master
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
14
19class ApiMergeHistory extends ApiBase {
20
21 public function __construct(
22 ApiMain $mainModule,
23 string $moduleName,
24 private readonly MergeHistoryFactory $mergeHistoryFactory,
25 ) {
26 parent::__construct( $mainModule, $moduleName );
27 }
28
29 public function execute() {
31
32 $params = $this->extractRequestParams();
33
34 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
35 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
36
37 // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
38 if ( isset( $params['from'] ) ) {
39 $fromTitle = Title::newFromText( $params['from'] );
40 if ( !$fromTitle || $fromTitle->isExternal() ) {
41 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
42 }
43 } elseif ( isset( $params['fromid'] ) ) {
44 $fromTitle = Title::newFromID( $params['fromid'] );
45 if ( !$fromTitle ) {
46 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
47 }
48 } else {
49 self::dieDebug( __METHOD__, 'unknown from parameter' );
50 }
51
52 if ( isset( $params['to'] ) ) {
53 $toTitle = Title::newFromText( $params['to'] );
54 if ( !$toTitle || $toTitle->isExternal() ) {
55 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
56 }
57 } elseif ( isset( $params['toid'] ) ) {
58 $toTitle = Title::newFromID( $params['toid'] );
59 if ( !$toTitle ) {
60 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
61 }
62 } else {
63 self::dieDebug( __METHOD__, 'unknown to parameter' );
64 }
65
66 $status = $this->mergeHistoryFactory->newMergeHistory(
67 $fromTitle,
68 $toTitle,
69 $params['timestamp'],
70 $params['starttimestamp']
71 )->merge( $this->getAuthority(), $params['reason'] );
72 if ( !$status->isOK() ) {
73 $this->dieStatus( $status );
74 }
75
76 $r = [
77 'from' => $fromTitle->getPrefixedText(),
78 'to' => $toTitle->getPrefixedText(),
79 'timestamp' => $params['timestamp'],
80 'reason' => $params['reason']
81 ];
82 $result = $this->getResult();
83
84 $result->addValue( null, $this->getModuleName(), $r );
85 }
86
88 public function mustBePosted() {
89 return true;
90 }
91
93 public function isWriteMode() {
94 return true;
95 }
96
98 public function getAllowedParams() {
99 return [
100 'from' => null,
101 'fromid' => [
102 ParamValidator::PARAM_TYPE => 'integer'
103 ],
104 'to' => null,
105 'toid' => [
106 ParamValidator::PARAM_TYPE => 'integer'
107 ],
108 // This can either be a timestamp or a timestamp-with-ID pair; don't reject the latter in validation
109 'timestamp' => null,
110 'reason' => '',
111 'starttimestamp' => null
112 ];
113 }
114
116 public function needsToken() {
117 return 'csrf';
118 }
119
121 protected function getExamplesMessages() {
122 return [
123 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
124 'reason=Reason'
125 => 'apihelp-mergehistory-example-merge',
126 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
127 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
128 => 'apihelp-mergehistory-example-merge-timestamp',
129 ];
130 }
131
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
135 }
136}
137
139class_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:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1369
getResult()
Get the result object.
Definition ApiBase.php:696
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1759
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:975
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...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
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.
__construct(ApiMain $mainModule, string $moduleName, private readonly MergeHistoryFactory $mergeHistoryFactory,)
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.
Service for mergehistory actions.