Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / logging / DeleteLogFormatter.php
blob05973df32543d3ed78f73c5937f00165a64e4732
1 <?php
2 /**
3 * Formatter for delete log entries.
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.
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.
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
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
26 use MediaWiki\MediaWikiServices;
28 /**
29 * This class formats delete log entries.
31 * @since 1.19
33 class DeleteLogFormatter extends LogFormatter {
34 protected function getMessageKey() {
35 $key = parent::getMessageKey();
36 if ( in_array( $this->entry->getSubtype(), [ 'event', 'revision' ] ) ) {
37 if ( count( $this->getMessageParameters() ) < 5 ) {
38 // Messages: logentry-delete-event-legacy, logentry-delete-revision-legacy,
39 // logentry-suppress-event-legacy, logentry-suppress-revision-legacy
40 return "$key-legacy";
44 return $key;
47 protected function getMessageParameters() {
48 if ( isset( $this->parsedParametersDeleteLog ) ) {
49 return $this->parsedParametersDeleteLog;
52 $params = parent::getMessageParameters();
53 $subtype = $this->entry->getSubtype();
54 if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
55 // $params[3] here is 'revision' or 'archive' for page revisions, 'oldimage' or
56 // 'filearchive' for file versions, or a comma-separated list of log_ids for log
57 // entries. $subtype here is 'revision' for page revisions and file
58 // versions, or 'event' for log entries.
59 if (
60 ( $subtype === 'event' && count( $params ) === 6 )
61 || (
62 $subtype === 'revision' && isset( $params[3] )
63 && in_array( $params[3], [ 'revision', 'archive', 'oldimage', 'filearchive' ] )
65 ) {
66 // See RevDelList::getLogParams()/RevDelLogList::getLogParams()
67 $paramStart = $subtype === 'revision' ? 4 : 3;
69 $old = $this->parseBitField( $params[$paramStart + 1] );
70 $new = $this->parseBitField( $params[$paramStart + 2] );
71 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
72 $changes = [];
73 // messages used: revdelete-content-hid, revdelete-summary-hid, revdelete-uname-hid
74 foreach ( $hid as $v ) {
75 $changes[] = $this->msg( "$v-hid" )->plain();
77 // messages used: revdelete-content-unhid, revdelete-summary-unhid,
78 // revdelete-uname-unhid
79 foreach ( $unhid as $v ) {
80 $changes[] = $this->msg( "$v-unhid" )->plain();
82 foreach ( $extra as $v ) {
83 $changes[] = $this->msg( $v )->plain();
85 $changeText = $this->context->getLanguage()->listToText( $changes );
87 $newParams = array_slice( $params, 0, 3 );
88 $newParams[3] = $changeText;
89 $ids = is_array( $params[$paramStart] )
90 ? $params[$paramStart]
91 : explode( ',', $params[$paramStart] );
92 $newParams[4] = $this->context->getLanguage()->formatNum( count( $ids ) );
94 $this->parsedParametersDeleteLog = $newParams;
95 return $this->parsedParametersDeleteLog;
96 } else {
97 $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
98 return $this->parsedParametersDeleteLog;
102 $this->parsedParametersDeleteLog = $params;
103 return $this->parsedParametersDeleteLog;
106 protected function parseBitField( $string ) {
107 // Input is like ofield=2134 or just the number
108 if ( strpos( $string, 'field=' ) === 1 ) {
109 list( , $field ) = explode( '=', $string );
111 return (int)$field;
112 } else {
113 return (int)$string;
117 public function getActionLinks() {
118 $user = $this->context->getUser();
119 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
120 if ( !$user->isAllowed( 'deletedhistory' )
121 || $this->entry->isDeleted( LogPage::DELETED_ACTION )
123 return '';
126 switch ( $this->entry->getSubtype() ) {
127 case 'delete': // Show undelete link
128 case 'delete_redir':
129 if ( $user->isAllowed( 'undelete' ) ) {
130 $message = 'undeletelink';
131 } else {
132 $message = 'undeleteviewlink';
134 $revert = $linkRenderer->makeKnownLink(
135 SpecialPage::getTitleFor( 'Undelete' ),
136 $this->msg( $message )->text(),
138 [ 'target' => $this->entry->getTarget()->getPrefixedDBkey() ]
141 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
143 case 'revision': // If an edit was hidden from a page give a review link to the history
144 $params = $this->extractParameters();
145 if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
146 return '';
149 // Different revision types use different URL params...
150 $key = $params[3];
151 // This is a array or CSV of the IDs
152 $ids = is_array( $params[4] )
153 ? $params[4]
154 : explode( ',', $params[4] );
156 $links = [];
158 // If there's only one item, we can show a diff link
159 if ( count( $ids ) == 1 ) {
160 // Live revision diffs...
161 if ( $key == 'oldid' || $key == 'revision' ) {
162 $links[] = $linkRenderer->makeKnownLink(
163 $this->entry->getTarget(),
164 $this->msg( 'diff' )->text(),
167 'diff' => intval( $ids[0] ),
168 'unhide' => 1
171 // Deleted revision diffs...
172 } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
173 $links[] = $linkRenderer->makeKnownLink(
174 SpecialPage::getTitleFor( 'Undelete' ),
175 $this->msg( 'diff' )->text(),
178 'target' => $this->entry->getTarget()->getPrefixedDBkey(),
179 'diff' => 'prev',
180 'timestamp' => $ids[0]
186 // View/modify link...
187 $links[] = $linkRenderer->makeKnownLink(
188 SpecialPage::getTitleFor( 'Revisiondelete' ),
189 $this->msg( 'revdel-restore' )->text(),
192 'target' => $this->entry->getTarget()->getPrefixedText(),
193 'type' => $key,
194 'ids' => implode( ',', $ids ),
198 return $this->msg( 'parentheses' )->rawParams(
199 $this->context->getLanguage()->pipeList( $links ) )->escaped();
201 case 'event': // Hidden log items, give review link
202 $params = $this->extractParameters();
203 if ( !isset( $params[3] ) ) {
204 return '';
206 // This is a CSV of the IDs
207 $query = $params[3];
208 if ( is_array( $query ) ) {
209 $query = implode( ',', $query );
211 // Link to each hidden object ID, $params[1] is the url param
212 $revert = $linkRenderer->makeKnownLink(
213 SpecialPage::getTitleFor( 'Revisiondelete' ),
214 $this->msg( 'revdel-restore' )->text(),
217 'target' => $this->entry->getTarget()->getPrefixedText(),
218 'type' => 'logging',
219 'ids' => $query
223 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
224 default:
225 return '';
229 protected function getParametersForApi() {
230 $entry = $this->entry;
231 $params = [];
233 $subtype = $this->entry->getSubtype();
234 if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
235 $rawParams = $entry->getParameters();
236 if ( $subtype === 'event' ) {
237 array_unshift( $rawParams, 'logging' );
240 static $map = [
241 '4::type',
242 '5::ids',
243 '6::ofield',
244 '7::nfield',
245 '4::ids' => '5::ids',
246 '5::ofield' => '6::ofield',
247 '6::nfield' => '7::nfield',
249 foreach ( $map as $index => $key ) {
250 if ( isset( $rawParams[$index] ) ) {
251 $rawParams[$key] = $rawParams[$index];
252 unset( $rawParams[$index] );
256 $old = $this->parseBitField( $rawParams['6::ofield'] );
257 $new = $this->parseBitField( $rawParams['7::nfield'] );
258 if ( !is_array( $rawParams['5::ids'] ) ) {
259 $rawParams['5::ids'] = explode( ',', $rawParams['5::ids'] );
262 $params = [
263 '::type' => $rawParams['4::type'],
264 ':array:ids' => $rawParams['5::ids'],
265 ':assoc:old' => [ 'bitmask' => $old ],
266 ':assoc:new' => [ 'bitmask' => $new ],
269 static $fields = [
270 Revision::DELETED_TEXT => 'content',
271 Revision::DELETED_COMMENT => 'comment',
272 Revision::DELETED_USER => 'user',
273 Revision::DELETED_RESTRICTED => 'restricted',
275 foreach ( $fields as $bit => $key ) {
276 $params[':assoc:old'][$key] = (bool)( $old & $bit );
277 $params[':assoc:new'][$key] = (bool)( $new & $bit );
281 return $params;
284 public function formatParametersForApi() {
285 $ret = parent::formatParametersForApi();
286 if ( isset( $ret['ids'] ) ) {
287 ApiResult::setIndexedTagName( $ret['ids'], 'id' );
289 return $ret;