MediaWiki  1.34.0
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 ( !is_null( $params['tags'] ) ) {
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->undelete(
69  ( $params['timestamps'] ?? [] ),
70  $params['reason'],
71  $params['fileids'],
72  false,
73  $user,
74  $params['tags']
75  );
76  if ( !is_array( $retval ) ) {
77  $this->dieWithError( 'apierror-cantundelete' );
78  }
79 
80  if ( $retval[1] ) {
81  Hooks::run( 'FileUndeleteComplete',
82  [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
83  }
84 
85  $this->setWatch( $params['watchlist'], $titleObj );
86 
87  $info = [
88  'title' => $titleObj->getPrefixedText(),
89  'revisions' => (int)$retval[0],
90  'fileversions' => (int)$retval[1],
91  'reason' => $retval[2]
92  ];
93  $this->getResult()->addValue( null, $this->getModuleName(), $info );
94  }
95 
96  public function mustBePosted() {
97  return true;
98  }
99 
100  public function isWriteMode() {
101  return true;
102  }
103 
104  public function getAllowedParams() {
105  return [
106  'title' => [
107  ApiBase::PARAM_TYPE => 'string',
109  ],
110  'reason' => '',
111  'tags' => [
112  ApiBase::PARAM_TYPE => 'tags',
113  ApiBase::PARAM_ISMULTI => true,
114  ],
115  'timestamps' => [
116  ApiBase::PARAM_TYPE => 'timestamp',
117  ApiBase::PARAM_ISMULTI => true,
118  ],
119  'fileids' => [
120  ApiBase::PARAM_TYPE => 'integer',
121  ApiBase::PARAM_ISMULTI => true,
122  ],
123  'watchlist' => [
124  ApiBase::PARAM_DFLT => 'preferences',
126  'watch',
127  'unwatch',
128  'preferences',
129  'nochange'
130  ],
131  ],
132  ];
133  }
134 
135  public function needsToken() {
136  return 'csrf';
137  }
138 
139  protected function getExamplesMessages() {
140  return [
141  'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
142  => 'apihelp-undelete-example-page',
143  'action=undelete&title=Main%20Page&token=123ABC' .
144  '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
145  => 'apihelp-undelete-example-revisions',
146  ];
147  }
148 
149  public function getHelpUrls() {
150  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
151  }
152 }
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:316
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiUndelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUndelete.php:104
PageArchive
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:31
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
true
return true
Definition: router.php:92
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:139
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
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:2055
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
ApiUndelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUndelete.php:135
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:761
ApiUndelete
Definition: ApiUndelete.php:26
ApiUndelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUndelete.php:100
ApiBase\setWatch
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1750
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:710
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
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:521
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
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
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ApiUndelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUndelete.php:149
ApiUndelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUndelete.php:96