Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / api / ApiUndelete.php
blob7fda1ea01a400b1d278a25e7993500c10613c9e8
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiUndelete extends ApiBase {
32 public function execute() {
33 $this->useTransactionalTimeLimit();
35 $params = $this->extractRequestParams();
36 $this->checkUserRightsAny( 'undelete' );
38 $user = $this->getUser();
39 if ( $user->isBlocked() ) {
40 $this->dieBlocked( $user->getBlock() );
43 $titleObj = Title::newFromText( $params['title'] );
44 if ( !$titleObj || $titleObj->isExternal() ) {
45 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
48 // Check if user can add tags
49 if ( !is_null( $params['tags'] ) ) {
50 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
51 if ( !$ableToTag->isOK() ) {
52 $this->dieStatus( $ableToTag );
56 // Convert timestamps
57 if ( !isset( $params['timestamps'] ) ) {
58 $params['timestamps'] = [];
60 if ( !is_array( $params['timestamps'] ) ) {
61 $params['timestamps'] = [ $params['timestamps'] ];
63 foreach ( $params['timestamps'] as $i => $ts ) {
64 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
67 $pa = new PageArchive( $titleObj, $this->getConfig() );
68 $retval = $pa->undelete(
69 ( isset( $params['timestamps'] ) ? $params['timestamps'] : [] ),
70 $params['reason'],
71 $params['fileids'],
72 false,
73 $user,
74 $params['tags']
76 if ( !is_array( $retval ) ) {
77 $this->dieWithError( 'apierror-cantundelete' );
80 if ( $retval[1] ) {
81 Hooks::run( 'FileUndeleteComplete',
82 [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
85 $this->setWatch( $params['watchlist'], $titleObj );
87 $info['title'] = $titleObj->getPrefixedText();
88 $info['revisions'] = intval( $retval[0] );
89 $info['fileversions'] = intval( $retval[1] );
90 $info['reason'] = $retval[2];
91 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 public function mustBePosted() {
95 return true;
98 public function isWriteMode() {
99 return true;
102 public function getAllowedParams() {
103 return [
104 'title' => [
105 ApiBase::PARAM_TYPE => 'string',
106 ApiBase::PARAM_REQUIRED => true
108 'reason' => '',
109 'tags' => [
110 ApiBase::PARAM_TYPE => 'tags',
111 ApiBase::PARAM_ISMULTI => true,
113 'timestamps' => [
114 ApiBase::PARAM_TYPE => 'timestamp',
115 ApiBase::PARAM_ISMULTI => true,
117 'fileids' => [
118 ApiBase::PARAM_TYPE => 'integer',
119 ApiBase::PARAM_ISMULTI => true,
121 'watchlist' => [
122 ApiBase::PARAM_DFLT => 'preferences',
123 ApiBase::PARAM_TYPE => [
124 'watch',
125 'unwatch',
126 'preferences',
127 'nochange'
133 public function needsToken() {
134 return 'csrf';
137 protected function getExamplesMessages() {
138 return [
139 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
140 => 'apihelp-undelete-example-page',
141 'action=undelete&title=Main%20Page&token=123ABC' .
142 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
143 => 'apihelp-undelete-example-revisions',
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/API:Undelete';