MediaWiki master
ApiUndelete.php
Go to the documentation of this file.
1<?php
31
35class ApiUndelete extends ApiBase {
36
38
39 private UndeletePageFactory $undeletePageFactory;
40 private WikiPageFactory $wikiPageFactory;
41
50 public function __construct(
51 ApiMain $mainModule,
52 $moduleName,
53 WatchlistManager $watchlistManager,
54 UserOptionsLookup $userOptionsLookup,
55 UndeletePageFactory $undeletePageFactory,
56 WikiPageFactory $wikiPageFactory
57 ) {
58 parent::__construct( $mainModule, $moduleName );
59
60 // Variables needed in ApiWatchlistTrait trait
61 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
62 $this->watchlistMaxDuration =
63 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
64 $this->watchlistManager = $watchlistManager;
65 $this->userOptionsLookup = $userOptionsLookup;
66 $this->undeletePageFactory = $undeletePageFactory;
67 $this->wikiPageFactory = $wikiPageFactory;
68 }
69
70 public function execute() {
72
74
75 $user = $this->getUser();
76 $block = $user->getBlock( IDBAccessObject::READ_LATEST );
77 if ( $block && $block->isSitewide() ) {
78 $this->dieBlocked( $block );
79 }
80
81 $titleObj = Title::newFromText( $params['title'] );
82 if ( !$titleObj || $titleObj->isExternal() ) {
83 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
84 }
85 if ( !$titleObj->canExist() ) {
86 $this->dieWithError( 'apierror-pagecannotexist' );
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.
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1539
addMessagesFromStatus(StatusValue $status, $types=[ 'warning', 'error'], array $filter=[])
Add warnings and/or errors from a Status.
Definition ApiBase.php:1517
getResult()
Get the result object.
Definition ApiBase.php:680
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:820
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:541
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1595
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1387
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:765
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1568
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
__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:78
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.