Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.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 $this->checkUserRightsAny( 'undelete' );
37
38 $user = $this->getUser();
39 if ( $user->isBlocked() ) {
40 $this->dieBlocked( $user->getBlock() );
41 }
42
43 $titleObj = Title::newFromText( $params['title'] );
44 if ( !$titleObj || $titleObj->isExternal() ) {
45 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
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 ( isset( $params['timestamps'] ) ? $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['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 );
92 }
93
94 public function mustBePosted() {
95 return true;
96 }
97
98 public function isWriteMode() {
99 return true;
100 }
101
102 public function getAllowedParams() {
103 return [
104 'title' => [
105 ApiBase::PARAM_TYPE => 'string',
106 ApiBase::PARAM_REQUIRED => true
107 ],
108 'reason' => '',
109 'tags' => [
110 ApiBase::PARAM_TYPE => 'tags',
111 ApiBase::PARAM_ISMULTI => true,
112 ],
113 'timestamps' => [
114 ApiBase::PARAM_TYPE => 'timestamp',
115 ApiBase::PARAM_ISMULTI => true,
116 ],
117 'fileids' => [
118 ApiBase::PARAM_TYPE => 'integer',
119 ApiBase::PARAM_ISMULTI => true,
120 ],
121 'watchlist' => [
122 ApiBase::PARAM_DFLT => 'preferences',
123 ApiBase::PARAM_TYPE => [
124 'watch',
125 'unwatch',
126 'preferences',
127 'nochange'
128 ],
129 ],
130 ];
131 }
132
133 public function needsToken() {
134 return 'csrf';
135 }
136
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',
144 ];
145 }
146
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/API:Undelete';
149 }
150 }