MediaWiki REL1_35
ApiUndelete.php
Go to the documentation of this file.
1<?php
26class 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',
125 ],
126 'timestamps' => [
127 ApiBase::PARAM_TYPE => 'timestamp',
129 ],
130 'fileids' => [
131 ApiBase::PARAM_TYPE => 'integer',
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}
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
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.
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
const PARAM_TYPE
Definition ApiBase.php:78
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:692
dieBlocked(AbstractBlock $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1464
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1294
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:717
const PARAM_ISMULTI
Definition ApiBase.php:74
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:47
needsToken()
Returns the token type this module requires in order to execute.
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $mainModule, $moduleName, $modulePrefix='')
Stable to call.
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isWriteMode()
Indicates whether this module requires write mode.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
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...
getUser()
Stable to override.
Used to show archived pages and eventually restore them.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
return true
Definition router.php:92