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 ( !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 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
getResult()
Get the result object.
Definition: ApiBase.php:640
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2078
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1863
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module&#39;s error handler and die wit...
Definition: ApiBase.php:2047
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2006
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks...
Definition: ApiBase.php:710
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:31
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:42
setWatch( $watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1742
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
return true
Definition: router.php:92
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317