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