MediaWiki REL1_37
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
25
30class ApiMergeHistory extends ApiBase {
31
34
40 public function __construct(
41 ApiMain $mainModule,
42 $moduleName,
44 ) {
45 parent::__construct( $mainModule, $moduleName );
46 $this->mergeHistoryFactory = $mergeHistoryFactory;
47 }
48
49 public function execute() {
51
52 $params = $this->extractRequestParams();
53
54 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
55 $this->requireOnlyOneParameter( $params, 'to', 'toid' );
56
57 // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
58 if ( isset( $params['from'] ) ) {
59 $fromTitle = Title::newFromText( $params['from'] );
60 if ( !$fromTitle || $fromTitle->isExternal() ) {
61 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
62 }
63 } elseif ( isset( $params['fromid'] ) ) {
64 $fromTitle = Title::newFromID( $params['fromid'] );
65 if ( !$fromTitle ) {
66 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
67 }
68 }
69
70 if ( isset( $params['to'] ) ) {
71 $toTitle = Title::newFromText( $params['to'] );
72 if ( !$toTitle || $toTitle->isExternal() ) {
73 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
74 }
75 } elseif ( isset( $params['toid'] ) ) {
76 $toTitle = Title::newFromID( $params['toid'] );
77 if ( !$toTitle ) {
78 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
79 }
80 }
81
82 $reason = $params['reason'];
83 $timestamp = $params['timestamp'];
84
85 // Merge!
86 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
89 }
90
91 $r = [
92 'from' => $fromTitle->getPrefixedText(),
93 'to' => $toTitle->getPrefixedText(),
94 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
95 'reason' => $params['reason']
96 ];
97 $result = $this->getResult();
98
99 $result->addValue( null, $this->getModuleName(), $r );
100 }
101
109 protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) {
110 $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp );
111
112 return $mh->merge( $this->getAuthority(), $reason );
113 }
114
115 public function mustBePosted() {
116 return true;
117 }
118
119 public function isWriteMode() {
120 return true;
121 }
122
123 public function getAllowedParams() {
124 return [
125 'from' => null,
126 'fromid' => [
127 ApiBase::PARAM_TYPE => 'integer'
128 ],
129 'to' => null,
130 'toid' => [
131 ApiBase::PARAM_TYPE => 'integer'
132 ],
133 'timestamp' => [
134 ApiBase::PARAM_TYPE => 'timestamp'
135 ],
136 'reason' => '',
137 ];
138 }
139
140 public function needsToken() {
141 return 'csrf';
142 }
143
144 protected function getExamplesMessages() {
145 return [
146 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
147 'reason=Reason'
148 => 'apihelp-mergehistory-example-merge',
149 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
150 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
151 => 'apihelp-mergehistory-example-merge-timestamp',
152 ];
153 }
154
155 public function getHelpUrls() {
156 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
157 }
158}
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:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_TYPE
Definition ApiBase.php:81
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:901
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
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:1293
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
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)
MergeHistoryFactory $mergeHistoryFactory
Interface for objects (potentially) representing an editable wiki page.