MediaWiki REL1_35
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
24
29class ApiMergeHistory extends ApiBase {
30
31 public function execute() {
33
34 $params = $this->extractRequestParams();
35
36 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
37 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
38
39 // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
40 if ( isset( $params['from'] ) ) {
41 $fromTitle = Title::newFromText( $params['from'] );
42 if ( !$fromTitle || $fromTitle->isExternal() ) {
43 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
44 }
45 } elseif ( isset( $params['fromid'] ) ) {
46 $fromTitle = Title::newFromID( $params['fromid'] );
47 if ( !$fromTitle ) {
48 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
49 }
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 }
63
64 $reason = $params['reason'];
65 $timestamp = $params['timestamp'];
66
67 // Merge!
68 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
69 if ( !$status->isOK() ) {
70 $this->dieStatus( $status );
71 }
72
73 $r = [
74 'from' => $fromTitle->getPrefixedText(),
75 'to' => $toTitle->getPrefixedText(),
76 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
77 'reason' => $params['reason']
78 ];
79 $result = $this->getResult();
80
81 $result->addValue( null, $this->getModuleName(), $r );
82 }
83
91 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
92 $factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
93 $mh = $factory->newMergeHistory( $from, $to, $timestamp );
94
95 return $mh->merge( $this->getUser(), $reason );
96 }
97
98 public function mustBePosted() {
99 return true;
100 }
101
102 public function isWriteMode() {
103 return true;
104 }
105
106 public function getAllowedParams() {
107 return [
108 'from' => null,
109 'fromid' => [
110 ApiBase::PARAM_TYPE => 'integer'
111 ],
112 'to' => null,
113 'toid' => [
114 ApiBase::PARAM_TYPE => 'integer'
115 ],
116 'timestamp' => [
117 ApiBase::PARAM_TYPE => 'timestamp'
118 ],
119 'reason' => '',
120 ];
121 }
122
123 public function needsToken() {
124 return 'csrf';
125 }
126
127 protected function getExamplesMessages() {
128 return [
129 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
130 'reason=Reason'
131 => 'apihelp-mergehistory-example-merge',
132 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
133 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
134 => 'apihelp-mergehistory-example-merge-timestamp',
135 ];
136 }
137
138 public function getHelpUrls() {
139 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
140 }
141}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfEscapeWikiText( $text)
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:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_TYPE
Definition ApiBase.php:78
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:909
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1294
API Module to merge page histories.
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
merge(Title $from, Title $to, $timestamp, $reason)
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 Stable to override.
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...
getUser()
Stable to override.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42