MediaWiki  master
ApiUndelete.php
Go to the documentation of this file.
1 <?php
26 
30 class 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 );
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',
142  ApiBase::PARAM_ISMULTI => true,
143  ],
144  'timestamps' => [
145  ApiBase::PARAM_TYPE => 'timestamp',
146  ApiBase::PARAM_ISMULTI => true,
147  ],
148  'fileids' => [
149  ApiBase::PARAM_TYPE => 'integer',
150  ApiBase::PARAM_ISMULTI => true,
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 }
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:382
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:78
ApiUndelete\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUndelete.php:133
PageArchive
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:34
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
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:1668
ApiUndelete\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUndelete.php:159
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiUndelete\__construct
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
Definition: ApiUndelete.php:40
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:1409
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:155
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
ApiUndelete\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiUndelete.php:55
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiUndelete
Definition: ApiUndelete.php:30
ApiUndelete\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUndelete.php:129
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
ChangeTags\canAddTagsAccompanyingChange
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...
Definition: ChangeTags.php:625
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1236
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1459
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:1442
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiBase\getHookRunner
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:653
ApiUndelete\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUndelete.php:169
ApiUndelete\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUndelete.php:125