3 * Copyright © 2007 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\Page\UndeletePage
;
27 use MediaWiki\Page\UndeletePageFactory
;
28 use MediaWiki\Page\WikiPageFactory
;
29 use MediaWiki\Title\Title
;
30 use MediaWiki\User\Options\UserOptionsLookup
;
31 use MediaWiki\Watchlist\WatchlistManager
;
32 use Wikimedia\ParamValidator\ParamValidator
;
33 use Wikimedia\Rdbms\IDBAccessObject
;
38 class ApiUndelete
extends ApiBase
{
40 use ApiWatchlistTrait
;
42 private UndeletePageFactory
$undeletePageFactory;
43 private WikiPageFactory
$wikiPageFactory;
45 public function __construct(
48 WatchlistManager
$watchlistManager,
49 UserOptionsLookup
$userOptionsLookup,
50 UndeletePageFactory
$undeletePageFactory,
51 WikiPageFactory
$wikiPageFactory
53 parent
::__construct( $mainModule, $moduleName );
55 // Variables needed in ApiWatchlistTrait trait
56 $this->watchlistExpiryEnabled
= $this->getConfig()->get( MainConfigNames
::WatchlistExpiry
);
57 $this->watchlistMaxDuration
=
58 $this->getConfig()->get( MainConfigNames
::WatchlistExpiryMaxDuration
);
59 $this->watchlistManager
= $watchlistManager;
60 $this->userOptionsLookup
= $userOptionsLookup;
61 $this->undeletePageFactory
= $undeletePageFactory;
62 $this->wikiPageFactory
= $wikiPageFactory;
65 public function execute() {
66 $this->useTransactionalTimeLimit();
68 $params = $this->extractRequestParams();
70 $user = $this->getUser();
71 $block = $user->getBlock( IDBAccessObject
::READ_LATEST
);
72 if ( $block && $block->isSitewide() ) {
73 $this->dieBlocked( $block );
76 $titleObj = Title
::newFromText( $params['title'] );
77 if ( !$titleObj ||
$titleObj->isExternal() ) {
78 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
80 if ( !$titleObj->canExist() ) {
81 $this->dieWithError( 'apierror-pagecannotexist' );
85 if ( !isset( $params['timestamps'] ) ) {
86 $params['timestamps'] = [];
88 if ( !is_array( $params['timestamps'] ) ) {
89 $params['timestamps'] = [ $params['timestamps'] ];
91 foreach ( $params['timestamps'] as $i => $ts ) {
92 $params['timestamps'][$i] = wfTimestamp( TS_MW
, $ts );
95 $undeletePage = $this->undeletePageFactory
->newUndeletePage(
96 $this->wikiPageFactory
->newFromTitle( $titleObj ),
99 ->setUndeleteOnlyTimestamps( $params['timestamps'] ??
[] )
100 ->setUndeleteOnlyFileVersions( $params['fileids'] ?
: [] )
101 ->setTags( $params['tags'] ?
: [] );
103 if ( $params['undeletetalk'] ) {
104 $undeletePage->setUndeleteAssociatedTalk( true );
107 $status = $undeletePage->undeleteIfAllowed( $params['reason'] );
108 if ( $status->isOK() ) {
109 // in case there are warnings
110 $this->addMessagesFromStatus( $status );
112 $this->dieStatus( $status );
115 $restoredRevs = $status->getValue()[UndeletePage
::REVISIONS_RESTORED
];
116 $restoredFiles = $status->getValue()[UndeletePage
::FILES_RESTORED
];
118 if ( $restoredRevs === 0 && $restoredFiles === 0 ) {
119 // BC for code that predates UndeletePage
120 $this->dieWithError( 'apierror-cantundelete' );
123 if ( $restoredFiles ) {
124 $this->getHookRunner()->onFileUndeleteComplete(
125 $titleObj, $params['fileids'],
126 $this->getUser(), $params['reason'] );
129 $watchlistExpiry = $this->getExpiryFromParams( $params );
130 $this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
133 'title' => $titleObj->getPrefixedText(),
134 'revisions' => $restoredRevs,
135 'fileversions' => $restoredFiles,
136 'reason' => $params['reason']
138 $this->getResult()->addValue( null, $this->getModuleName(), $info );
141 public function mustBePosted() {
145 public function isWriteMode() {
149 public function getAllowedParams() {
152 ParamValidator
::PARAM_TYPE
=> 'string',
153 ParamValidator
::PARAM_REQUIRED
=> true
157 ParamValidator
::PARAM_TYPE
=> 'tags',
158 ParamValidator
::PARAM_ISMULTI
=> true,
161 ParamValidator
::PARAM_TYPE
=> 'timestamp',
162 ParamValidator
::PARAM_ISMULTI
=> true,
165 ParamValidator
::PARAM_TYPE
=> 'integer',
166 ParamValidator
::PARAM_ISMULTI
=> true,
168 'undeletetalk' => false,
169 ] +
$this->getWatchlistParams();
172 public function needsToken() {
176 protected function getExamplesMessages() {
177 $title = Title
::newMainPage()->getPrefixedText();
178 $mp = rawurlencode( $title );
181 "action=undelete&title={$mp}&token=123ABC&reason=Restoring%20{$mp}"
182 => 'apihelp-undelete-example-page',
183 "action=undelete&title={$mp}&token=123ABC" .
184 '×tamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
185 => 'apihelp-undelete-example-revisions',
189 public function getHelpUrls() {
190 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
194 /** @deprecated class alias since 1.43 */
195 class_alias( ApiUndelete
::class, 'ApiUndelete' );