MediaWiki  master
ApiUndelete.php
Go to the documentation of this file.
1 <?php
29 
33 class ApiUndelete extends ApiBase {
34 
36 
39 
42 
51  public function __construct(
52  ApiMain $mainModule,
53  $moduleName,
58  ) {
59  parent::__construct( $mainModule, $moduleName );
60 
61  // Variables needed in ApiWatchlistTrait trait
62  $this->watchlistExpiryEnabled = $this->getConfig()->get( 'WatchlistExpiry' );
63  $this->watchlistMaxDuration = $this->getConfig()->get( 'WatchlistExpiryMaxDuration' );
64  $this->watchlistManager = $watchlistManager;
65  $this->userOptionsLookup = $userOptionsLookup;
66  $this->undeletePageFactory = $undeletePageFactory;
67  $this->wikiPageFactory = $wikiPageFactory;
68  }
69 
70  public function execute() {
72 
73  $params = $this->extractRequestParams();
74 
75  $user = $this->getUser();
76  $block = $user->getBlock( Authority::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 
86  // Convert timestamps
87  if ( !isset( $params['timestamps'] ) ) {
88  $params['timestamps'] = [];
89  }
90  if ( !is_array( $params['timestamps'] ) ) {
91  $params['timestamps'] = [ $params['timestamps'] ];
92  }
93  foreach ( $params['timestamps'] as $i => $ts ) {
94  $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
95  }
96 
97  $undeletePage = $this->undeletePageFactory->newUndeletePage(
98  $this->wikiPageFactory->newFromTitle( $titleObj ),
99  $this->getAuthority()
100  );
101  $status = $undeletePage
102  ->setUndeleteOnlyTimestamps( $params['timestamps'] ?? [] )
103  ->setUndeleteOnlyFileVersions( $params['fileids'] ?: [] )
104  ->setTags( $params['tags'] ?: [] )
105  ->undeleteIfAllowed( $params['reason'] );
106  if ( !$status->isGood() ) {
107  $this->dieStatus( $status );
108  }
109 
110  $restoredRevs = $status->getValue()[UndeletePage::REVISIONS_RESTORED];
111  $restoredFiles = $status->getValue()[UndeletePage::FILES_RESTORED];
112 
113  if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
114  // BC for code that predates UndeletePage
115  $this->dieWithError( 'apierror-cantundelete' );
116  }
117 
118  if ( $restoredFiles ) {
119  $this->getHookRunner()->onFileUndeleteComplete(
120  $titleObj, $params['fileids'],
121  $this->getUser(), $params['reason'] );
122  }
123 
124  $watchlistExpiry = $this->getExpiryFromParams( $params );
125  $this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
126 
127  $info = [
128  'title' => $titleObj->getPrefixedText(),
129  'revisions' => $restoredRevs,
130  'fileversions' => $restoredFiles,
131  'reason' => $params['reason']
132  ];
133  $this->getResult()->addValue( null, $this->getModuleName(), $info );
134  }
135 
136  public function mustBePosted() {
137  return true;
138  }
139 
140  public function isWriteMode() {
141  return true;
142  }
143 
144  public function getAllowedParams() {
145  return [
146  'title' => [
147  ApiBase::PARAM_TYPE => 'string',
149  ],
150  'reason' => '',
151  'tags' => [
152  ApiBase::PARAM_TYPE => 'tags',
153  ApiBase::PARAM_ISMULTI => true,
154  ],
155  'timestamps' => [
156  ApiBase::PARAM_TYPE => 'timestamp',
157  ApiBase::PARAM_ISMULTI => true,
158  ],
159  'fileids' => [
160  ApiBase::PARAM_TYPE => 'integer',
161  ApiBase::PARAM_ISMULTI => true,
162  ],
163  ] + $this->getWatchlistParams();
164  }
165 
166  public function needsToken() {
167  return 'csrf';
168  }
169 
170  protected function getExamplesMessages() {
171  return [
172  'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
173  => 'apihelp-undelete-example-page',
174  'action=undelete&title=Main%20Page&token=123ABC' .
175  '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
176  => 'apihelp-undelete-example-revisions',
177  ];
178  }
179 
180  public function getHelpUrls() {
181  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
182  }
183 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:377
getExpiryFromParams
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
Definition: ApiWatchlistTrait.php:164
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:105
ApiUndelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUndelete.php:144
ApiUndelete\$wikiPageFactory
WikiPageFactory $wikiPageFactory
Definition: ApiUndelete.php:41
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
true
return true
Definition: router.php:90
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1649
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:170
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiBase\dieBlocked
dieBlocked(Block $block)
Throw an ApiUsageException, which will (if uncaught) call the main module's error handler and die wit...
Definition: ApiBase.php:1466
Page\UndeletePage
Definition: UndeletePage.php:52
ApiUndelete\$undeletePageFactory
UndeletePageFactory $undeletePageFactory
Definition: ApiUndelete.php:38
MediaWiki\Watchlist\WatchlistManager
WatchlistManager service.
Definition: WatchlistManager.php:52
ApiUndelete\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUndelete.php:166
setWatch
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.
Definition: ApiWatchlistTrait.php:96
Page\WikiPageFactory
Definition: WikiPageFactory.php:19
ApiUndelete\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiUndelete.php:70
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
ApiUndelete
Definition: ApiUndelete.php:33
ApiUndelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUndelete.php:140
Page\UndeletePageFactory
Definition: UndeletePageFactory.php:10
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1293
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1440
MediaWiki\User\UserOptionsLookup
Provides access to user options.
Definition: UserOptionsLookup.php:29
ApiWatchlistTrait
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Definition: ApiWatchlistTrait.php:21
$watchlistManager
WatchlistManager $watchlistManager
Definition: ApiWatchlistTrait.php:30
$userOptionsLookup
UserOptionsLookup $userOptionsLookup
Definition: ApiWatchlistTrait.php:33
getWatchlistParams
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
Definition: ApiWatchlistTrait.php:59
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1499
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:77
ApiUndelete\__construct
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup, UndeletePageFactory $undeletePageFactory, WikiPageFactory $wikiPageFactory)
Definition: ApiUndelete.php:51
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:710
ApiUndelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUndelete.php:180
ApiUndelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUndelete.php:136