MediaWiki  master
ApiUndelete.php
Go to the documentation of this file.
1 <?php
26 class ApiUndelete extends ApiBase {
27 
29 
30  public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) {
31  parent::__construct( $mainModule, $moduleName, $modulePrefix );
32 
33  $this->watchlistExpiryEnabled = $this->getConfig()->get( 'WatchlistExpiry' );
34  $this->watchlistMaxDuration = $this->getConfig()->get( 'WatchlistExpiryMaxDuration' );
35  }
36 
37  public function execute() {
39 
40  $params = $this->extractRequestParams();
41 
42  $user = $this->getUser();
43  $block = $user->getBlock();
44  if ( $block && $block->isSitewide() ) {
45  $this->dieBlocked( $user->getBlock() );
46  }
47 
48  $titleObj = Title::newFromText( $params['title'] );
49  if ( !$titleObj || $titleObj->isExternal() ) {
50  $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
51  }
52 
53  if ( !$this->getPermissionManager()->userCan( 'undelete', $this->getUser(), $titleObj ) ) {
54  $this->dieWithError( 'permdenied-undelete' );
55  }
56 
57  // Check if user can add tags
58  if ( $params['tags'] !== null ) {
59  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
60  if ( !$ableToTag->isOK() ) {
61  $this->dieStatus( $ableToTag );
62  }
63  }
64 
65  // Convert timestamps
66  if ( !isset( $params['timestamps'] ) ) {
67  $params['timestamps'] = [];
68  }
69  if ( !is_array( $params['timestamps'] ) ) {
70  $params['timestamps'] = [ $params['timestamps'] ];
71  }
72  foreach ( $params['timestamps'] as $i => $ts ) {
73  $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
74  }
75 
76  $pa = new PageArchive( $titleObj, $this->getConfig() );
77  $retval = $pa->undeleteAsUser(
78  ( $params['timestamps'] ?? [] ),
79  $user,
80  $params['reason'],
81  $params['fileids'],
82  false,
83  $params['tags']
84  );
85  if ( !is_array( $retval ) ) {
86  $this->dieWithError( 'apierror-cantundelete' );
87  }
88 
89  if ( $retval[1] ) {
90  $this->getHookRunner()->onFileUndeleteComplete(
91  $titleObj, $params['fileids'],
92  $this->getUser(), $params['reason'] );
93  }
94 
95  $watchlistExpiry = $this->getExpiryFromParams( $params );
96  $this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
97 
98  $info = [
99  'title' => $titleObj->getPrefixedText(),
100  'revisions' => (int)$retval[0],
101  'fileversions' => (int)$retval[1],
102  'reason' => $retval[2]
103  ];
104  $this->getResult()->addValue( null, $this->getModuleName(), $info );
105  }
106 
107  public function mustBePosted() {
108  return true;
109  }
110 
111  public function isWriteMode() {
112  return true;
113  }
114 
115  public function getAllowedParams() {
116  return [
117  'title' => [
118  ApiBase::PARAM_TYPE => 'string',
120  ],
121  'reason' => '',
122  'tags' => [
123  ApiBase::PARAM_TYPE => 'tags',
124  ApiBase::PARAM_ISMULTI => true,
125  ],
126  'timestamps' => [
127  ApiBase::PARAM_TYPE => 'timestamp',
128  ApiBase::PARAM_ISMULTI => true,
129  ],
130  'fileids' => [
131  ApiBase::PARAM_TYPE => 'integer',
132  ApiBase::PARAM_ISMULTI => true,
133  ],
134  ] + $this->getWatchlistParams();
135  }
136 
137  public function needsToken() {
138  return 'csrf';
139  }
140 
141  protected function getExamplesMessages() {
142  return [
143  'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
144  => 'apihelp-undelete-example-page',
145  'action=undelete&title=Main%20Page&token=123ABC' .
146  '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
147  => 'apihelp-undelete-example-revisions',
148  ];
149  }
150 
151  public function getHelpUrls() {
152  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
153  }
154 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:48
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:67
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:328
getExpiryFromParams
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
Definition: ApiWatchlistTrait.php:135
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:76
ApiUndelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUndelete.php:115
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:1381
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:1815
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:141
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:564
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:1408
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiUndelete\__construct
__construct(ApiMain $mainModule, $moduleName, $modulePrefix='')
Stable to call.
Definition: ApiUndelete.php:30
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiUndelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUndelete.php:137
setWatch
setWatch(string $watch, Title $title, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiWatchlistTrait.php:72
ApiUndelete\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiUndelete.php:37
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:716
ApiUndelete
Definition: ApiUndelete.php:26
ApiUndelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUndelete.php:111
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:636
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1238
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1494
ApiWatchlistTrait
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Definition: ApiWatchlistTrait.php:17
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:545
getWatchlistParams
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
Definition: ApiWatchlistTrait.php:35
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1439
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:443
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:661
ApiUndelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUndelete.php:151
ApiUndelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiUndelete.php:107