MediaWiki  1.30.2
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 
37  $user = $this->getUser();
38  if ( $user->isBlocked() ) {
39  $this->dieBlocked( $user->getBlock() );
40  }
41 
42  $titleObj = Title::newFromText( $params['title'] );
43  if ( !$titleObj || $titleObj->isExternal() ) {
44  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
45  }
46 
47  if ( !$titleObj->userCan( 'undelete', $user, 'secure' ) ) {
48  $this->dieWithError( 'permdenied-undelete' );
49  }
50 
51  // Check if user can add tags
52  if ( !is_null( $params['tags'] ) ) {
54  if ( !$ableToTag->isOK() ) {
55  $this->dieStatus( $ableToTag );
56  }
57  }
58 
59  // Convert timestamps
60  if ( !isset( $params['timestamps'] ) ) {
61  $params['timestamps'] = [];
62  }
63  if ( !is_array( $params['timestamps'] ) ) {
64  $params['timestamps'] = [ $params['timestamps'] ];
65  }
66  foreach ( $params['timestamps'] as $i => $ts ) {
67  $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
68  }
69 
70  $pa = new PageArchive( $titleObj, $this->getConfig() );
71  $retval = $pa->undelete(
72  ( isset( $params['timestamps'] ) ? $params['timestamps'] : [] ),
73  $params['reason'],
74  $params['fileids'],
75  false,
76  $user,
77  $params['tags']
78  );
79  if ( !is_array( $retval ) ) {
80  $this->dieWithError( 'apierror-cantundelete' );
81  }
82 
83  if ( $retval[1] ) {
84  Hooks::run( 'FileUndeleteComplete',
85  [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
86  }
87 
88  $this->setWatch( $params['watchlist'], $titleObj );
89 
90  $info['title'] = $titleObj->getPrefixedText();
91  $info['revisions'] = intval( $retval[0] );
92  $info['fileversions'] = intval( $retval[1] );
93  $info['reason'] = $retval[2];
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()
Get the Config object.
Definition: ContextSource.php:68
$user
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 account $user
Definition: hooks.txt:244
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:268
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:115
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:28
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1855
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:140
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:91
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:632
$params
$params
Definition: styleTest.css.php:40
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:133
php
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
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:41
ApiBase\dieBlocked
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:1895
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:32
ApiUndelete
Definition: ApiUndelete.php:30
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:1616
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:740
$retval
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 account incomplete not yet checked for validity & $retval
Definition: hooks.txt:244
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2631
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1703
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:421
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
as
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
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1920
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:512
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:55
true
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:1965
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
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