Merge "Add .pipeline/ with dev image variant"
[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 $block = $user->getBlock();
35 if ( $block && $block->isSitewide() ) {
36 $this->dieBlocked( $user->getBlock() );
37 }
38
39 $titleObj = Title::newFromText( $params['title'] );
40 if ( !$titleObj || $titleObj->isExternal() ) {
41 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
42 }
43
44 if ( !$this->getPermissionManager()->userCan( 'undelete', $this->getUser(), $titleObj ) ) {
45 $this->dieWithError( 'permdenied-undelete' );
46 }
47
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 );
53 }
54 }
55
56 // Convert timestamps
57 if ( !isset( $params['timestamps'] ) ) {
58 $params['timestamps'] = [];
59 }
60 if ( !is_array( $params['timestamps'] ) ) {
61 $params['timestamps'] = [ $params['timestamps'] ];
62 }
63 foreach ( $params['timestamps'] as $i => $ts ) {
64 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
65 }
66
67 $pa = new PageArchive( $titleObj, $this->getConfig() );
68 $retval = $pa->undelete(
69 ( $params['timestamps'] ?? [] ),
70 $params['reason'],
71 $params['fileids'],
72 false,
73 $user,
74 $params['tags']
75 );
76 if ( !is_array( $retval ) ) {
77 $this->dieWithError( 'apierror-cantundelete' );
78 }
79
80 if ( $retval[1] ) {
81 Hooks::run( 'FileUndeleteComplete',
82 [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
83 }
84
85 $this->setWatch( $params['watchlist'], $titleObj );
86
87 $info = [
88 'title' => $titleObj->getPrefixedText(),
89 'revisions' => (int)$retval[0],
90 'fileversions' => (int)$retval[1],
91 'reason' => $retval[2]
92 ];
93 $this->getResult()->addValue( null, $this->getModuleName(), $info );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return [
106 'title' => [
107 ApiBase::PARAM_TYPE => 'string',
108 ApiBase::PARAM_REQUIRED => true
109 ],
110 'reason' => '',
111 'tags' => [
112 ApiBase::PARAM_TYPE => 'tags',
113 ApiBase::PARAM_ISMULTI => true,
114 ],
115 'timestamps' => [
116 ApiBase::PARAM_TYPE => 'timestamp',
117 ApiBase::PARAM_ISMULTI => true,
118 ],
119 'fileids' => [
120 ApiBase::PARAM_TYPE => 'integer',
121 ApiBase::PARAM_ISMULTI => true,
122 ],
123 'watchlist' => [
124 ApiBase::PARAM_DFLT => 'preferences',
125 ApiBase::PARAM_TYPE => [
126 'watch',
127 'unwatch',
128 'preferences',
129 'nochange'
130 ],
131 ],
132 ];
133 }
134
135 public function needsToken() {
136 return 'csrf';
137 }
138
139 protected function getExamplesMessages() {
140 return [
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 '&timestamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
145 => 'apihelp-undelete-example-revisions',
146 ];
147 }
148
149 public function getHelpUrls() {
150 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';
151 }
152 }