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