MediaWiki REL1_39
ApiUndelete.php
Go to the documentation of this file.
1<?php
31
35class ApiUndelete extends ApiBase {
36
38
40 private $undeletePageFactory;
41
43 private $wikiPageFactory;
44
53 public function __construct(
54 ApiMain $mainModule,
55 $moduleName,
56 WatchlistManager $watchlistManager,
57 UserOptionsLookup $userOptionsLookup,
58 UndeletePageFactory $undeletePageFactory,
59 WikiPageFactory $wikiPageFactory
60 ) {
61 parent::__construct( $mainModule, $moduleName );
62
63 // Variables needed in ApiWatchlistTrait trait
64 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
65 $this->watchlistMaxDuration =
66 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
67 $this->watchlistManager = $watchlistManager;
68 $this->userOptionsLookup = $userOptionsLookup;
69 $this->undeletePageFactory = $undeletePageFactory;
70 $this->wikiPageFactory = $wikiPageFactory;
71 }
72
73 public function execute() {
75
76 $params = $this->extractRequestParams();
77
78 $user = $this->getUser();
79 $block = $user->getBlock( Authority::READ_LATEST );
80 if ( $block && $block->isSitewide() ) {
81 $this->dieBlocked( $block );
82 }
83
84 $titleObj = Title::newFromText( $params['title'] );
85 if ( !$titleObj || $titleObj->isExternal() ) {
86 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
87 }
88
89 // Convert timestamps
90 if ( !isset( $params['timestamps'] ) ) {
91 $params['timestamps'] = [];
92 }
93 if ( !is_array( $params['timestamps'] ) ) {
94 $params['timestamps'] = [ $params['timestamps'] ];
95 }
96 foreach ( $params['timestamps'] as $i => $ts ) {
97 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
98 }
99
100 $undeletePage = $this->undeletePageFactory->newUndeletePage(
101 $this->wikiPageFactory->newFromTitle( $titleObj ),
102 $this->getAuthority()
103 )
104 ->setUndeleteOnlyTimestamps( $params['timestamps'] ?? [] )
105 ->setUndeleteOnlyFileVersions( $params['fileids'] ?: [] )
106 ->setTags( $params['tags'] ?: [] );
107
108 if ( $params['undeletetalk'] ) {
109 $undeletePage->setUndeleteAssociatedTalk( true );
110 }
111
112 $status = $undeletePage->undeleteIfAllowed( $params['reason'] );
113 if ( $status->isOK() ) {
114 // in case there are warnings
115 $this->addMessagesFromStatus( $status );
116 } else {
117 $this->dieStatus( $status );
118 }
119
120 $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
121 $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
122
123 if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
124 // BC for code that predates UndeletePage
125 $this->dieWithError( 'apierror-cantundelete' );
126 }
127
128 if ( $restoredFiles ) {
129 $this->getHookRunner()->onFileUndeleteComplete(
130 $titleObj, $params['fileids'],
131 $this->getUser(), $params['reason'] );
132 }
133
134 $watchlistExpiry = $this->getExpiryFromParams( $params );
135 $this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
136
137 $info = [
138 'title' => $titleObj->getPrefixedText(),
139 'revisions' => $restoredRevs,
140 'fileversions' => $restoredFiles,
141 'reason' => $params['reason']
142 ];
143 $this->getResult()->addValue( null, $this->getModuleName(), $info );
144 }
145
146 public function mustBePosted() {
147 return true;
148 }
149
150 public function isWriteMode() {
151 return true;
152 }
153
154 public function getAllowedParams() {
155 return [
156 'title' => [
157 ParamValidator::PARAM_TYPE => 'string',
158 ParamValidator::PARAM_REQUIRED => true
159 ],
160 'reason' => '',
161 'tags' => [
162 ParamValidator::PARAM_TYPE => 'tags',
163 ParamValidator::PARAM_ISMULTI => true,
164 ],
165 'timestamps' => [
166 ParamValidator::PARAM_TYPE => 'timestamp',
167 ParamValidator::PARAM_ISMULTI => true,
168 ],
169 'fileids' => [
170 ParamValidator::PARAM_TYPE => 'integer',
171 ParamValidator::PARAM_ISMULTI => true,
172 ],
173 'undeletetalk' => false,
174 ] + $this->getWatchlistParams();
175 }
176
177 public function needsToken() {
178 return 'csrf';
179 }
180
181 protected function getExamplesMessages() {
182 $title = Title::newMainPage()->getPrefixedText();
183 $mp = rawurlencode( $title );
184
185 return [
186 "action=undelete&title={$mp}&token=123ABC&reason=Restoring%20{$mp}"
187 => 'apihelp-undelete-example-page',
188 "action=undelete&title={$mp}&token=123ABC" .
189 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
190 => 'apihelp-undelete-example-revisions',
191 ];
192 }
193
194 public function getHelpUrls() {
195 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
196 }
197}
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:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
addMessagesFromStatus(StatusValue $status, $types=[ 'warning', 'error'], array $filter=[])
Add warnings and/or errors from a Status.
Definition ApiBase.php:1432
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1515
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1299
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:711
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1483
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup, UndeletePageFactory $undeletePageFactory, WikiPageFactory $wikiPageFactory)
needsToken()
Returns the token type this module requires in order to execute.
mustBePosted()
Indicates whether this module must be called with a POST request.
getHelpUrls()
Return links to more detailed help pages about the module.
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.
A class containing constants representing the names of configuration variables.
Backend logic for performing a page undelete action.
Service for creating WikiPage objects.
Provides access to user options.
Service for formatting and validating API parameters.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Service for page undelete actions.
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
return true
Definition router.php:92