MediaWiki  master
ApiMergeHistory.php
Go to the documentation of this file.
1 <?php
24 
29 class 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 }
ApiMergeHistory
API Module to merge page histories.
Definition: ApiMergeHistory.php:29
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:332
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1806
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:60
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:538
ApiMergeHistory\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiMergeHistory.php:102
ApiMergeHistory\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiMergeHistory.php:127
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:48
ApiMergeHistory\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiMergeHistory.php:123
ApiMergeHistory\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiMergeHistory.php:98
ApiMergeHistory\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiMergeHistory.php:106
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:659
ApiBase\requireOnlyOneParameter
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:796
ApiMergeHistory\merge
merge(Title $from, Title $to, $timestamp, $reason)
Definition: ApiMergeHistory.php:91
ApiMergeHistory\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiMergeHistory.php:138
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1236
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1485
ApiMergeHistory\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiMergeHistory.php:31
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1437
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:418
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:476