MediaWiki REL1_34
ApiMergeHistory.php
Go to the documentation of this file.
1<?php
27class ApiMergeHistory extends ApiBase {
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 (nonexistant 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 }
49
50 if ( isset( $params['to'] ) ) {
51 $toTitle = Title::newFromText( $params['to'] );
52 if ( !$toTitle || $toTitle->isExternal() ) {
53 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
54 }
55 } elseif ( isset( $params['toid'] ) ) {
56 $toTitle = Title::newFromID( $params['toid'] );
57 if ( !$toTitle ) {
58 $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
59 }
60 }
61
62 $reason = $params['reason'];
63 $timestamp = $params['timestamp'];
64
65 // Merge!
66 $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
67 if ( !$status->isOK() ) {
68 $this->dieStatus( $status );
69 }
70
71 $r = [
72 'from' => $fromTitle->getPrefixedText(),
73 'to' => $toTitle->getPrefixedText(),
74 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
75 'reason' => $params['reason']
76 ];
77 $result = $this->getResult();
78
79 $result->addValue( null, $this->getModuleName(), $r );
80 }
81
89 protected function merge( Title $from, Title $to, $timestamp, $reason ) {
90 $mh = new MergeHistory( $from, $to, $timestamp );
91
92 return $mh->merge( $this->getUser(), $reason );
93 }
94
95 public function mustBePosted() {
96 return true;
97 }
98
99 public function isWriteMode() {
100 return true;
101 }
102
103 public function getAllowedParams() {
104 return [
105 'from' => null,
106 'fromid' => [
107 ApiBase::PARAM_TYPE => 'integer'
108 ],
109 'to' => null,
110 'toid' => [
111 ApiBase::PARAM_TYPE => 'integer'
112 ],
113 'timestamp' => [
114 ApiBase::PARAM_TYPE => 'timestamp'
115 ],
116 'reason' => '',
117 ];
118 }
119
120 public function needsToken() {
121 return 'csrf';
122 }
123
124 protected function getExamplesMessages() {
125 return [
126 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
127 'reason=Reason'
128 => 'apihelp-mergehistory-example-merge',
129 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
130 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
131 => 'apihelp-mergehistory-example-merge-timestamp',
132 ];
133 }
134
135 public function getHelpUrls() {
136 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
137 }
138}
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:42
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:2086
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1871
requireOnlyOneParameter( $params, $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:893
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.
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...
Handles the backend logic of merging the histories of two pages.
Represents a title within MediaWiki.
Definition Title.php:42