MediaWiki  master
ApiMergeHistory.php
Go to the documentation of this file.
1 <?php
26 
31 class ApiMergeHistory extends ApiBase {
32 
34  private $mergeHistoryFactory;
35 
41  public function __construct(
42  ApiMain $mainModule,
43  $moduleName,
44  MergeHistoryFactory $mergeHistoryFactory
45  ) {
46  parent::__construct( $mainModule, $moduleName );
47  $this->mergeHistoryFactory = $mergeHistoryFactory;
48  }
49 
50  public function execute() {
52 
53  $params = $this->extractRequestParams();
54 
55  $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
56  $this->requireOnlyOneParameter( $params, 'to', 'toid' );
57 
58  // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
59  if ( isset( $params['from'] ) ) {
60  $fromTitle = Title::newFromText( $params['from'] );
61  if ( !$fromTitle || $fromTitle->isExternal() ) {
62  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
63  }
64  } elseif ( isset( $params['fromid'] ) ) {
65  $fromTitle = Title::newFromID( $params['fromid'] );
66  if ( !$fromTitle ) {
67  $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
68  }
69  }
70 
71  if ( isset( $params['to'] ) ) {
72  $toTitle = Title::newFromText( $params['to'] );
73  if ( !$toTitle || $toTitle->isExternal() ) {
74  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
75  }
76  } elseif ( isset( $params['toid'] ) ) {
77  $toTitle = Title::newFromID( $params['toid'] );
78  if ( !$toTitle ) {
79  $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
80  }
81  }
82 
83  $reason = $params['reason'];
84  $timestamp = $params['timestamp'];
85 
86  // Merge!
87  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable,PhanPossiblyUndeclaredVariable T240141
88  $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
89  if ( !$status->isOK() ) {
90  $this->dieStatus( $status );
91  }
92 
93  $r = [
94  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
95  'from' => $fromTitle->getPrefixedText(),
96  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
97  'to' => $toTitle->getPrefixedText(),
98  'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
99  'reason' => $params['reason']
100  ];
101  $result = $this->getResult();
102 
103  $result->addValue( null, $this->getModuleName(), $r );
104  }
105 
113  protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) {
114  $mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp );
115 
116  return $mh->merge( $this->getAuthority(), $reason );
117  }
118 
119  public function mustBePosted() {
120  return true;
121  }
122 
123  public function isWriteMode() {
124  return true;
125  }
126 
127  public function getAllowedParams() {
128  return [
129  'from' => null,
130  'fromid' => [
131  ParamValidator::PARAM_TYPE => 'integer'
132  ],
133  'to' => null,
134  'toid' => [
135  ParamValidator::PARAM_TYPE => 'integer'
136  ],
137  'timestamp' => [
138  ParamValidator::PARAM_TYPE => 'timestamp'
139  ],
140  'reason' => '',
141  ];
142  }
143 
144  public function needsToken() {
145  return 'csrf';
146  }
147 
148  protected function getExamplesMessages() {
149  return [
150  'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
151  'reason=Reason'
152  => 'apihelp-mergehistory-example-merge',
153  'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
154  'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
155  => 'apihelp-mergehistory-example-merge-timestamp',
156  ];
157  }
158 
159  public function getHelpUrls() {
160  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
161  }
162 }
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:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1453
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:903
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1516
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1298
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:52
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)
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:518
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
Service for formatting and validating API parameters.
Service for mergehistory actions.
Interface for objects (potentially) representing an editable wiki page.