MediaWiki  master
ApiUndelete.php
Go to the documentation of this file.
1 <?php
26 class ApiUndelete extends ApiBase {
27 
28  public function execute() {
30 
31  $params = $this->extractRequestParams();
32 
33  $user = $this->getUser();
34  $block = $user->getBlock();
35  if ( $block && $block->isSitewide() ) {
36  $this->dieBlocked( $user->getBlock() );
37  }
38 
39  $titleObj = Title::newFromText( $params['title'] );
40  if ( !$titleObj || $titleObj->isExternal() ) {
41  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
42  }
43 
44  if ( !$this->getPermissionManager()->userCan( 'undelete', $this->getUser(), $titleObj ) ) {
45  $this->dieWithError( 'permdenied-undelete' );
46  }
47 
48  // Check if user can add tags
49  if ( $params['tags'] !== null ) {
50  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
51  if ( !$ableToTag->isOK() ) {
52  $this->dieStatus( $ableToTag );
53  }
54  }
55 
56  // Convert timestamps
57  if ( !isset( $params['timestamps'] ) ) {
58  $params['timestamps'] = [];
59  }
60  if ( !is_array( $params['timestamps'] ) ) {
61  $params['timestamps'] = [ $params['timestamps'] ];
62  }
63  foreach ( $params['timestamps'] as $i => $ts ) {
64  $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
65  }
66 
67  $pa = new PageArchive( $titleObj, $this->getConfig() );
68  $retval = $pa->undeleteAsUser(
69  ( $params['timestamps'] ?? [] ),
70  $user,
71  $params['reason'],
72  $params['fileids'],
73  false,
74  $params['tags']
75  );
76  if ( !is_array( $retval ) ) {
77  $this->dieWithError( 'apierror-cantundelete' );
78  }
79 
80  if ( $retval[1] ) {
81  $this->getHookRunner()->onFileUndeleteComplete(
82  $titleObj, $params['fileids'],
83  $this->getUser(), $params['reason'] );
84  }
85 
86  $this->setWatch( $params['watchlist'], $titleObj );
87 
88  $info = [
89  'title' => $titleObj->getPrefixedText(),
90  'revisions' => (int)$retval[0],
91  'fileversions' => (int)$retval[1],
92  'reason' => $retval[2]
93  ];
94  $this->getResult()->addValue( null, $this->getModuleName(), $info );
95  }
96 
97  public function mustBePosted() {
98  return true;
99  }
100 
101  public function isWriteMode() {
102  return true;
103  }
104 
105  public function getAllowedParams() {
106  return [
107  'title' => [
108  ApiBase::PARAM_TYPE => 'string',
110  ],
111  'reason' => '',
112  'tags' => [
113  ApiBase::PARAM_TYPE => 'tags',
114  ApiBase::PARAM_ISMULTI => true,
115  ],
116  'timestamps' => [
117  ApiBase::PARAM_TYPE => 'timestamp',
118  ApiBase::PARAM_ISMULTI => true,
119  ],
120  'fileids' => [
121  ApiBase::PARAM_TYPE => 'integer',
122  ApiBase::PARAM_ISMULTI => true,
123  ],
124  'watchlist' => [
125  ApiBase::PARAM_DFLT => 'preferences',
127  'watch',
128  'unwatch',
129  'preferences',
130  'nochange'
131  ],
132  ],
133  ];
134  }
135 
136  public function needsToken() {
137  return 'csrf';
138  }
139 
140  protected function getExamplesMessages() {
141  return [
142  'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
143  => 'apihelp-undelete-example-page',
144  'action=undelete&title=Main%20Page&token=123ABC' .
145  '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
146  => 'apihelp-undelete-example-revisions',
147  ];
148  }
149 
150  public function getHelpUrls() {
151  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
152  }
153 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
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:329
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
ApiUndelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUndelete.php:105
PageArchive
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:32
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
true
return true
Definition: router.php:90
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1811
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:140
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
ApiBase\dieBlocked
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:1442
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:50
ApiUndelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUndelete.php:136
ApiUndelete\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiUndelete.php:28
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
ApiUndelete
Definition: ApiUndelete.php:26
ApiUndelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUndelete.php:101
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1151
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:616
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1272
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1490
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:531
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:66
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1473
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:67
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:641
ApiUndelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUndelete.php:150
ApiUndelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUndelete.php:97