logging: Simplify DeleteLogFormatter b/c check
[lhc/web/wiklou.git] / includes / logging / DeleteLogFormatter.php
1 <?php
2 /**
3 * Formatter for delete log entries.
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 * @author Niklas Laxström
22 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
23 * @since 1.22
24 */
25
26 /**
27 * This class formats delete log entries.
28 *
29 * @since 1.19
30 */
31 class DeleteLogFormatter extends LogFormatter {
32 protected function getMessageKey() {
33 $key = parent::getMessageKey();
34 if ( in_array( $this->entry->getSubtype(), [ 'event', 'revision' ] ) ) {
35 if ( count( $this->getMessageParameters() ) < 5 ) {
36 // Messages: logentry-delete-event-legacy, logentry-delete-revision-legacy,
37 // logentry-suppress-event-legacy, logentry-suppress-revision-legacy
38 return "$key-legacy";
39 }
40 }
41
42 return $key;
43 }
44
45 protected function getMessageParameters() {
46 if ( isset( $this->parsedParametersDeleteLog ) ) {
47 return $this->parsedParametersDeleteLog;
48 }
49
50 $params = parent::getMessageParameters();
51 $subtype = $this->entry->getSubtype();
52 if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
53 // $params[3] here is 'revision' or 'archive' for page revisions, 'oldimage' or
54 // 'filearchive' for file versions, or a comma-separated list of log_ids for log
55 // entries. $subtype here is 'revision' for page revisions and file
56 // versions, or 'event' for log entries.
57 if (
58 ( $subtype === 'event' && count( $params ) === 6 )
59 || (
60 $subtype === 'revision' && isset( $params[3] )
61 && in_array( $params[3], [ 'revision', 'archive', 'oldimage', 'filearchive' ] )
62 )
63 ) {
64 $paramStart = $subtype === 'revision' ? 4 : 3;
65
66 $old = $this->parseBitField( $params[$paramStart + 1] );
67 $new = $this->parseBitField( $params[$paramStart + 2] );
68 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
69 $changes = [];
70 // messages used: revdelete-content-hid, revdelete-summary-hid, revdelete-uname-hid
71 foreach ( $hid as $v ) {
72 $changes[] = $this->msg( "$v-hid" )->plain();
73 }
74 // messages used: revdelete-content-unhid, revdelete-summary-unhid,
75 // revdelete-uname-unhid
76 foreach ( $unhid as $v ) {
77 $changes[] = $this->msg( "$v-unhid" )->plain();
78 }
79 foreach ( $extra as $v ) {
80 $changes[] = $this->msg( $v )->plain();
81 }
82 $changeText = $this->context->getLanguage()->listToText( $changes );
83
84 $newParams = array_slice( $params, 0, 3 );
85 $newParams[3] = $changeText;
86 $ids = is_array( $params[$paramStart] )
87 ? $params[$paramStart]
88 : explode( ',', $params[$paramStart] );
89 $newParams[4] = $this->context->getLanguage()->formatNum( count( $ids ) );
90
91 $this->parsedParametersDeleteLog = $newParams;
92 return $this->parsedParametersDeleteLog;
93 } else {
94 $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
95 return $this->parsedParametersDeleteLog;
96 }
97 }
98
99 $this->parsedParametersDeleteLog = $params;
100 return $this->parsedParametersDeleteLog;
101 }
102
103 protected function parseBitField( $string ) {
104 // Input is like ofield=2134 or just the number
105 if ( strpos( $string, 'field=' ) === 1 ) {
106 list( , $field ) = explode( '=', $string );
107
108 return (int)$field;
109 } else {
110 return (int)$string;
111 }
112 }
113
114 public function getActionLinks() {
115 $user = $this->context->getUser();
116 if ( !$user->isAllowed( 'deletedhistory' )
117 || $this->entry->isDeleted( LogPage::DELETED_ACTION )
118 ) {
119 return '';
120 }
121
122 switch ( $this->entry->getSubtype() ) {
123 case 'delete': // Show undelete link
124 if ( $user->isAllowed( 'undelete' ) ) {
125 $message = 'undeletelink';
126 } else {
127 $message = 'undeleteviewlink';
128 }
129 $revert = Linker::linkKnown(
130 SpecialPage::getTitleFor( 'Undelete' ),
131 $this->msg( $message )->escaped(),
132 [],
133 [ 'target' => $this->entry->getTarget()->getPrefixedDBkey() ]
134 );
135
136 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
137
138 case 'revision': // If an edit was hidden from a page give a review link to the history
139 $params = $this->extractParameters();
140 if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
141 return '';
142 }
143
144 // Different revision types use different URL params...
145 $key = $params[3];
146 // This is a array or CSV of the IDs
147 $ids = is_array( $params[4] )
148 ? $params[4]
149 : explode( ',', $params[4] );
150
151 $links = [];
152
153 // If there's only one item, we can show a diff link
154 if ( count( $ids ) == 1 ) {
155 // Live revision diffs...
156 if ( $key == 'oldid' || $key == 'revision' ) {
157 $links[] = Linker::linkKnown(
158 $this->entry->getTarget(),
159 $this->msg( 'diff' )->escaped(),
160 [],
161 [
162 'diff' => intval( $ids[0] ),
163 'unhide' => 1
164 ]
165 );
166 // Deleted revision diffs...
167 } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
168 $links[] = Linker::linkKnown(
169 SpecialPage::getTitleFor( 'Undelete' ),
170 $this->msg( 'diff' )->escaped(),
171 [],
172 [
173 'target' => $this->entry->getTarget()->getPrefixedDBkey(),
174 'diff' => 'prev',
175 'timestamp' => $ids[0]
176 ]
177 );
178 }
179 }
180
181 // View/modify link...
182 $links[] = Linker::linkKnown(
183 SpecialPage::getTitleFor( 'Revisiondelete' ),
184 $this->msg( 'revdel-restore' )->escaped(),
185 [],
186 [
187 'target' => $this->entry->getTarget()->getPrefixedText(),
188 'type' => $key,
189 'ids' => implode( ',', $ids ),
190 ]
191 );
192
193 return $this->msg( 'parentheses' )->rawParams(
194 $this->context->getLanguage()->pipeList( $links ) )->escaped();
195
196 case 'event': // Hidden log items, give review link
197 $params = $this->extractParameters();
198 if ( !isset( $params[3] ) ) {
199 return '';
200 }
201 // This is a CSV of the IDs
202 $query = $params[3];
203 if ( is_array( $query ) ) {
204 $query = implode( ',', $query );
205 }
206 // Link to each hidden object ID, $params[1] is the url param
207 $revert = Linker::linkKnown(
208 SpecialPage::getTitleFor( 'Revisiondelete' ),
209 $this->msg( 'revdel-restore' )->escaped(),
210 [],
211 [
212 'target' => $this->entry->getTarget()->getPrefixedText(),
213 'type' => 'logging',
214 'ids' => $query
215 ]
216 );
217
218 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
219 default:
220 return '';
221 }
222 }
223
224 protected function getParametersForApi() {
225 $entry = $this->entry;
226 $params = [];
227
228 $subtype = $this->entry->getSubtype();
229 if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
230 $rawParams = $entry->getParameters();
231 if ( $subtype === 'event' ) {
232 array_unshift( $rawParams, 'logging' );
233 }
234
235 static $map = [
236 '4::type',
237 '5::ids',
238 '6::ofield',
239 '7::nfield',
240 '4::ids' => '5::ids',
241 '5::ofield' => '6::ofield',
242 '6::nfield' => '7::nfield',
243 ];
244 foreach ( $map as $index => $key ) {
245 if ( isset( $rawParams[$index] ) ) {
246 $rawParams[$key] = $rawParams[$index];
247 unset( $rawParams[$index] );
248 }
249 }
250
251 $old = $this->parseBitField( $rawParams['6::ofield'] );
252 $new = $this->parseBitField( $rawParams['7::nfield'] );
253 if ( !is_array( $rawParams['5::ids'] ) ) {
254 $rawParams['5::ids'] = explode( ',', $rawParams['5::ids'] );
255 }
256
257 $params = [
258 '::type' => $rawParams['4::type'],
259 ':array:ids' => $rawParams['5::ids'],
260 ':assoc:old' => [ 'bitmask' => $old ],
261 ':assoc:new' => [ 'bitmask' => $new ],
262 ];
263
264 static $fields = [
265 Revision::DELETED_TEXT => 'content',
266 Revision::DELETED_COMMENT => 'comment',
267 Revision::DELETED_USER => 'user',
268 Revision::DELETED_RESTRICTED => 'restricted',
269 ];
270 foreach ( $fields as $bit => $key ) {
271 $params[':assoc:old'][$key] = (bool)( $old & $bit );
272 $params[':assoc:new'][$key] = (bool)( $new & $bit );
273 }
274 }
275
276 return $params;
277 }
278
279 public function formatParametersForApi() {
280 $ret = parent::formatParametersForApi();
281 if ( isset( $ret['ids'] ) ) {
282 ApiResult::setIndexedTagName( $ret['ids'], 'id' );
283 }
284 return $ret;
285 }
286 }