MediaWiki  1.34.0
ApiMergeHistory.php
Go to the documentation of this file.
1 <?php
27 class 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 }
ApiMergeHistory
API Module to merge page histories.
Definition: ApiMergeHistory.php:27
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:316
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiMergeHistory\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiMergeHistory.php:99
ApiMergeHistory\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiMergeHistory.php:124
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:42
ApiMergeHistory\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiMergeHistory.php:120
ApiMergeHistory\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiMergeHistory.php:95
ApiMergeHistory\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiMergeHistory.php:103
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiMergeHistory\merge
merge(Title $from, Title $to, $timestamp, $reason)
Definition: ApiMergeHistory.php:89
ApiMergeHistory\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiMergeHistory.php:135
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
ApiMergeHistory\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiMergeHistory.php:29
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:893
MergeHistory
Handles the backend logic of merging the histories of two pages.
Definition: MergeHistory.php:33
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:467