MediaWiki REL1_37
ApiUndelete.php
Go to the documentation of this file.
1<?php
26
30class ApiUndelete extends ApiBase {
31
33
40 public function __construct(
41 ApiMain $mainModule,
42 $moduleName,
45 ) {
46 parent::__construct( $mainModule, $moduleName );
47
48 // Variables needed in ApiWatchlistTrait trait
49 $this->watchlistExpiryEnabled = $this->getConfig()->get( 'WatchlistExpiry' );
50 $this->watchlistMaxDuration = $this->getConfig()->get( 'WatchlistExpiryMaxDuration' );
51 $this->watchlistManager = $watchlistManager;
52 $this->userOptionsLookup = $userOptionsLookup;
53 }
54
55 public function execute() {
57
58 $params = $this->extractRequestParams();
59
60 $user = $this->getUser();
61 $block = $user->getBlock( Authority::READ_LATEST );
62 if ( $block && $block->isSitewide() ) {
63 $this->dieBlocked( $block );
64 }
65
66 $titleObj = Title::newFromText( $params['title'] );
67 if ( !$titleObj || $titleObj->isExternal() ) {
68 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
69 }
70
71 if ( !$this->getAuthority()->authorizeWrite( 'undelete', $titleObj ) ) {
72 $this->dieWithError( 'permdenied-undelete' );
73 }
74
75 // Check if user can add tags
76 if ( $params['tags'] !== null ) {
77 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $this->getAuthority() );
78 if ( !$ableToTag->isOK() ) {
79 $this->dieStatus( $ableToTag );
80 }
81 }
82
83 // Convert timestamps
84 if ( !isset( $params['timestamps'] ) ) {
85 $params['timestamps'] = [];
86 }
87 if ( !is_array( $params['timestamps'] ) ) {
88 $params['timestamps'] = [ $params['timestamps'] ];
89 }
90 foreach ( $params['timestamps'] as $i => $ts ) {
91 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
92 }
93
94 $pa = new PageArchive( $titleObj, $this->getConfig() );
95 $retval = $pa->undeleteAsUser(
96 ( $params['timestamps'] ?? [] ),
97 $user,
98 $params['reason'],
99 $params['fileids'],
100 false,
101 $params['tags']
102 );
103 if ( !is_array( $retval ) ) {
104 $this->dieWithError( 'apierror-cantundelete' );
105 }
106
107 if ( $retval[1] ) {
108 $this->getHookRunner()->onFileUndeleteComplete(
109 $titleObj, $params['fileids'],
110 $this->getUser(), $params['reason'] );
111 }
112
113 $watchlistExpiry = $this->getExpiryFromParams( $params );
114 $this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
115
116 $info = [
117 'title' => $titleObj->getPrefixedText(),
118 'revisions' => (int)$retval[0],
119 'fileversions' => (int)$retval[1],
120 'reason' => $retval[2]
121 ];
122 $this->getResult()->addValue( null, $this->getModuleName(), $info );
123 }
124
125 public function mustBePosted() {
126 return true;
127 }
128
129 public function isWriteMode() {
130 return true;
131 }
132
133 public function getAllowedParams() {
134 return [
135 'title' => [
136 ApiBase::PARAM_TYPE => 'string',
138 ],
139 'reason' => '',
140 'tags' => [
141 ApiBase::PARAM_TYPE => 'tags',
143 ],
144 'timestamps' => [
145 ApiBase::PARAM_TYPE => 'timestamp',
147 ],
148 'fileids' => [
149 ApiBase::PARAM_TYPE => 'integer',
151 ],
152 ] + $this->getWatchlistParams();
153 }
154
155 public function needsToken() {
156 return 'csrf';
157 }
158
159 protected function getExamplesMessages() {
160 return [
161 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
162 => 'apihelp-undelete-example-page',
163 'action=undelete&title=Main%20Page&token=123ABC' .
164 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
165 => 'apihelp-undelete-example-revisions',
166 ];
167 }
168
169 public function getHelpUrls() {
170 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
171 }
172}
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.
WatchlistManager $watchlistManager
UserOptionsLookup $userOptionsLookup
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:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
const PARAM_TYPE
Definition ApiBase.php:81
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
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:1293
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:710
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition ApiBase.php:1463
const PARAM_ISMULTI
Definition ApiBase.php:77
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
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.
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Provides access to user options.
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...
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