Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / api / ApiUndelete.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiUndelete extends ApiBase {
27
28 public function execute() {
29 $this->useTransactionalTimeLimit();
30
31 $params = $this->extractRequestParams();
32
33 $user = $this->getUser();
34 if ( $user->isBlocked() ) {
35 $this->dieBlocked( $user->getBlock() );
36 }
37
38 $titleObj = Title::newFromText( $params['title'] );
39 if ( !$titleObj || $titleObj->isExternal() ) {
40 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
41 }
42
43 if ( !$titleObj->userCan( 'undelete', $user, 'secure' ) ) {
44 $this->dieWithError( 'permdenied-undelete' );
45 }
46
47 // Check if user can add tags
48 if ( !is_null( $params['tags'] ) ) {
49 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
52 }
53 }
54
55 // Convert timestamps
56 if ( !isset( $params['timestamps'] ) ) {
57 $params['timestamps'] = [];
58 }
59 if ( !is_array( $params['timestamps'] ) ) {
60 $params['timestamps'] = [ $params['timestamps'] ];
61 }
62 foreach ( $params['timestamps'] as $i => $ts ) {
63 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
64 }
65
66 $pa = new PageArchive( $titleObj, $this->getConfig() );
67 $retval = $pa->undelete(
68 ( isset( $params['timestamps'] ) ? $params['timestamps'] : [] ),
69 $params['reason'],
70 $params['fileids'],
71 false,
72 $user,
73 $params['tags']
74 );
75 if ( !is_array( $retval ) ) {
76 $this->dieWithError( 'apierror-cantundelete' );
77 }
78
79 if ( $retval[1] ) {
80 Hooks::run( 'FileUndeleteComplete',
81 [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
82 }
83
84 $this->setWatch( $params['watchlist'], $titleObj );
85
86 $info['title'] = $titleObj->getPrefixedText();
87 $info['revisions'] = intval( $retval[0] );
88 $info['fileversions'] = intval( $retval[1] );
89 $info['reason'] = $retval[2];
90 $this->getResult()->addValue( null, $this->getModuleName(), $info );
91 }
92
93 public function mustBePosted() {
94 return true;
95 }
96
97 public function isWriteMode() {
98 return true;
99 }
100
101 public function getAllowedParams() {
102 return [
103 'title' => [
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true
106 ],
107 'reason' => '',
108 'tags' => [
109 ApiBase::PARAM_TYPE => 'tags',
110 ApiBase::PARAM_ISMULTI => true,
111 ],
112 'timestamps' => [
113 ApiBase::PARAM_TYPE => 'timestamp',
114 ApiBase::PARAM_ISMULTI => true,
115 ],
116 'fileids' => [
117 ApiBase::PARAM_TYPE => 'integer',
118 ApiBase::PARAM_ISMULTI => true,
119 ],
120 'watchlist' => [
121 ApiBase::PARAM_DFLT => 'preferences',
122 ApiBase::PARAM_TYPE => [
123 'watch',
124 'unwatch',
125 'preferences',
126 'nochange'
127 ],
128 ],
129 ];
130 }
131
132 public function needsToken() {
133 return 'csrf';
134 }
135
136 protected function getExamplesMessages() {
137 return [
138 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
139 => 'apihelp-undelete-example-page',
140 'action=undelete&title=Main%20Page&token=123ABC' .
141 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
142 => 'apihelp-undelete-example-revisions',
143 ];
144 }
145
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
148 }
149 }