MediaWiki  1.28.0
ApiUndelete.php
Go to the documentation of this file.
1 <?php
30 class ApiUndelete extends ApiBase {
31 
32  public function execute() {
34 
35  $params = $this->extractRequestParams();
36  $user = $this->getUser();
37  if ( !$user->isAllowed( 'undelete' ) ) {
38  $this->dieUsageMsg( 'permdenied-undelete' );
39  }
40 
41  if ( $user->isBlocked() ) {
42  $this->dieBlocked( $user->getBlock() );
43  }
44 
45  $titleObj = Title::newFromText( $params['title'] );
46  if ( !$titleObj || $titleObj->isExternal() ) {
47  $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
48  }
49 
50  // Check if user can add tags
51  if ( !is_null( $params['tags'] ) ) {
53  if ( !$ableToTag->isOK() ) {
54  $this->dieStatus( $ableToTag );
55  }
56  }
57 
58  // Convert timestamps
59  if ( !isset( $params['timestamps'] ) ) {
60  $params['timestamps'] = [];
61  }
62  if ( !is_array( $params['timestamps'] ) ) {
63  $params['timestamps'] = [ $params['timestamps'] ];
64  }
65  foreach ( $params['timestamps'] as $i => $ts ) {
66  $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
67  }
68 
69  $pa = new PageArchive( $titleObj, $this->getConfig() );
70  $retval = $pa->undelete(
71  ( isset( $params['timestamps'] ) ? $params['timestamps'] : [] ),
72  $params['reason'],
73  $params['fileids'],
74  false,
75  $user,
76  $params['tags']
77  );
78  if ( !is_array( $retval ) ) {
79  $this->dieUsageMsg( 'cannotundelete' );
80  }
81 
82  if ( $retval[1] ) {
83  Hooks::run( 'FileUndeleteComplete',
84  [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
85  }
86 
87  $this->setWatch( $params['watchlist'], $titleObj );
88 
89  $info['title'] = $titleObj->getPrefixedText();
90  $info['revisions'] = intval( $retval[0] );
91  $info['fileversions'] = intval( $retval[1] );
92  $info['reason'] = $retval[2];
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/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:88
getResult()
Get the result object.
Definition: ApiBase.php:577
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2752
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
setWatch($watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1423
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:112
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1936
getConfig()
Get the Config object.
$params
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:457
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
Used to show archived pages and eventually restore them.
dieBlocked(Block $block)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1591
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
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:392
dieStatus($status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1663
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition: hooks.txt:242
getUser()
Get the User object.
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2192