5 * Created on Jul 3, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class ApiUndelete
extends ApiBase
{
32 public function execute() {
33 $this->useTransactionalTimeLimit();
35 $params = $this->extractRequestParams();
36 $user = $this->getUser();
37 if ( !$user->isAllowed( 'undelete' ) ) {
38 $this->dieUsageMsg( 'permdenied-undelete' );
41 if ( $user->isBlocked() ) {
42 $this->dieBlocked( $user->getBlock() );
45 $titleObj = Title
::newFromText( $params['title'] );
46 if ( !$titleObj ||
$titleObj->isExternal() ) {
47 $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
50 // Check if user can add tags
51 if ( !is_null( $params['tags'] ) ) {
52 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
53 if ( !$ableToTag->isOK() ) {
54 $this->dieStatus( $ableToTag );
59 if ( !isset( $params['timestamps'] ) ) {
60 $params['timestamps'] = [];
62 if ( !is_array( $params['timestamps'] ) ) {
63 $params['timestamps'] = [ $params['timestamps'] ];
65 foreach ( $params['timestamps'] as $i => $ts ) {
66 $params['timestamps'][$i] = wfTimestamp( TS_MW
, $ts );
69 $pa = new PageArchive( $titleObj, $this->getConfig() );
70 $retval = $pa->undelete(
71 ( isset( $params['timestamps'] ) ?
$params['timestamps'] : [] ),
78 if ( !is_array( $retval ) ) {
79 $this->dieUsageMsg( 'cannotundelete' );
83 Hooks
::run( 'FileUndeleteComplete',
84 [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
87 $this->setWatch( $params['watchlist'], $titleObj );
89 $info['title'] = $titleObj->getPrefixedText();
90 $info['revisions'] = intval( $retval[0] );
91 $info['fileversions'] = intval( $retval[1] );
92 $info['reason'] = $retval[2];
93 $this->getResult()->addValue( null, $this->getModuleName(), $info );
96 public function mustBePosted() {
100 public function isWriteMode() {
104 public function getAllowedParams() {
107 ApiBase
::PARAM_TYPE
=> 'string',
108 ApiBase
::PARAM_REQUIRED
=> true
112 ApiBase
::PARAM_TYPE
=> 'tags',
113 ApiBase
::PARAM_ISMULTI
=> true,
116 ApiBase
::PARAM_TYPE
=> 'timestamp',
117 ApiBase
::PARAM_ISMULTI
=> true,
120 ApiBase
::PARAM_TYPE
=> 'integer',
121 ApiBase
::PARAM_ISMULTI
=> true,
124 ApiBase
::PARAM_DFLT
=> 'preferences',
125 ApiBase
::PARAM_TYPE
=> [
135 public function needsToken() {
139 protected function getExamplesMessages() {
141 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
142 => 'apihelp-undelete-example-page',
143 'action=undelete&title=Main%20Page&token=123ABC' .
144 '×tamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
145 => 'apihelp-undelete-example-revisions',
149 public function getHelpUrls() {
150 return 'https://www.mediawiki.org/wiki/API:Undelete';