MediaWiki  master
ApiMergeHistory.php
Go to the documentation of this file.
1 <?php
25 
30 class 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 }
ApiMergeHistory
API Module to merge page histories.
Definition: ApiMergeHistory.php:30
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
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:383
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:1668
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiMergeHistory\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiMergeHistory.php:119
ApiMergeHistory\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiMergeHistory.php:144
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiMergeHistory\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiMergeHistory.php:140
Page\MergeHistoryFactory
Definition: MergeHistoryFactory.php:30
ApiMergeHistory\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiMergeHistory.php:115
ApiMergeHistory\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiMergeHistory.php:123
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiMergeHistory\merge
merge(PageIdentity $from, PageIdentity $to, $timestamp, $reason)
Definition: ApiMergeHistory.php:109
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:844
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiMergeHistory\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiMergeHistory.php:155
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:1459
ApiMergeHistory\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiMergeHistory.php:49
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1442
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiMergeHistory\$mergeHistoryFactory
MergeHistoryFactory $mergeHistoryFactory
Definition: ApiMergeHistory.php:33
ApiMergeHistory\__construct
__construct(ApiMain $mainModule, $moduleName, MergeHistoryFactory $mergeHistoryFactory)
Definition: ApiMergeHistory.php:40
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:529