b3905635f5e1a91d55f8d4b280006d8c6522dc3f
[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(), array( 'event', 'revision' ) ) ) {
35 if ( count( $this->getMessageParameters() ) < 5 ) {
36 return "$key-legacy";
37 }
38 }
39
40 return $key;
41 }
42
43 protected function getMessageParameters() {
44 if ( isset( $this->parsedParametersDeleteLog ) ) {
45 return $this->parsedParametersDeleteLog;
46 }
47
48 $params = parent::getMessageParameters();
49 $subtype = $this->entry->getSubtype();
50 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
51 // $params[3] here is 'revision' or 'archive' for page revisions, 'oldimage' or
52 // 'filearchive' for file versions, or a comma-separated list of log_ids for log
53 // entries. $subtype here is 'revision' for page revisions and file
54 // versions, or 'event' for log entries.
55 if ( ( $subtype === 'event' && count( $params ) === 6 )
56 || ( $subtype === 'revision' && isset( $params[3] )
57 && ( $params[3] === 'revision' || $params[3] === 'oldimage'
58 || $params[3] === 'archive' || $params[3] === 'filearchive' )
59 )
60 ) {
61 $paramStart = $subtype === 'revision' ? 4 : 3;
62
63 $old = $this->parseBitField( $params[$paramStart + 1] );
64 $new = $this->parseBitField( $params[$paramStart + 2] );
65 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
66 $changes = array();
67 foreach ( $hid as $v ) {
68 $changes[] = $this->msg( "$v-hid" )->plain();
69 }
70 foreach ( $unhid as $v ) {
71 $changes[] = $this->msg( "$v-unhid" )->plain();
72 }
73 foreach ( $extra as $v ) {
74 $changes[] = $this->msg( $v )->plain();
75 }
76 $changeText = $this->context->getLanguage()->listToText( $changes );
77
78 $newParams = array_slice( $params, 0, 3 );
79 $newParams[3] = $changeText;
80 $count = count( explode( ',', $params[$paramStart] ) );
81 $newParams[4] = $this->context->getLanguage()->formatNum( $count );
82
83 $this->parsedParametersDeleteLog = $newParams;
84 return $this->parsedParametersDeleteLog;
85 } else {
86 $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
87 return $this->parsedParametersDeleteLog;
88 }
89 }
90
91 $this->parsedParametersDeleteLog = $params;
92 return $this->parsedParametersDeleteLog;
93 }
94
95 protected function parseBitField( $string ) {
96 // Input is like ofield=2134 or just the number
97 if ( strpos( $string, 'field=' ) === 1 ) {
98 list( , $field ) = explode( '=', $string );
99
100 return (int)$field;
101 } else {
102 return (int)$string;
103 }
104 }
105
106 public function getActionLinks() {
107 $user = $this->context->getUser();
108 if ( !$user->isAllowed( 'deletedhistory' )
109 || $this->entry->isDeleted( LogPage::DELETED_ACTION )
110 ) {
111 return '';
112 }
113
114 switch ( $this->entry->getSubtype() ) {
115 case 'delete': // Show undelete link
116 if ( $user->isAllowed( 'undelete' ) ) {
117 $message = 'undeletelink';
118 } else {
119 $message = 'undeleteviewlink';
120 }
121 $revert = Linker::linkKnown(
122 SpecialPage::getTitleFor( 'Undelete' ),
123 $this->msg( $message )->escaped(),
124 array(),
125 array( 'target' => $this->entry->getTarget()->getPrefixedDBkey() )
126 );
127
128 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
129
130 case 'revision': // If an edit was hidden from a page give a review link to the history
131 $params = $this->extractParameters();
132 if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
133 return '';
134 }
135
136 // Different revision types use different URL params...
137 $key = $params[3];
138 // This is a CSV of the IDs
139 $ids = explode( ',', $params[4] );
140
141 $links = array();
142
143 // If there's only one item, we can show a diff link
144 if ( count( $ids ) == 1 ) {
145 // Live revision diffs...
146 if ( $key == 'oldid' || $key == 'revision' ) {
147 $links[] = Linker::linkKnown(
148 $this->entry->getTarget(),
149 $this->msg( 'diff' )->escaped(),
150 array(),
151 array(
152 'diff' => intval( $ids[0] ),
153 'unhide' => 1
154 )
155 );
156 // Deleted revision diffs...
157 } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
158 $links[] = Linker::linkKnown(
159 SpecialPage::getTitleFor( 'Undelete' ),
160 $this->msg( 'diff' )->escaped(),
161 array(),
162 array(
163 'target' => $this->entry->getTarget()->getPrefixedDBkey(),
164 'diff' => 'prev',
165 'timestamp' => $ids[0]
166 )
167 );
168 }
169 }
170
171 // View/modify link...
172 $links[] = Linker::linkKnown(
173 SpecialPage::getTitleFor( 'Revisiondelete' ),
174 $this->msg( 'revdel-restore' )->escaped(),
175 array(),
176 array(
177 'target' => $this->entry->getTarget()->getPrefixedText(),
178 'type' => $key,
179 'ids' => implode( ',', $ids ),
180 )
181 );
182
183 return $this->msg( 'parentheses' )->rawParams(
184 $this->context->getLanguage()->pipeList( $links ) )->escaped();
185
186 case 'event': // Hidden log items, give review link
187 $params = $this->extractParameters();
188 if ( !isset( $params[3] ) ) {
189 return '';
190 }
191 // This is a CSV of the IDs
192 $query = $params[3];
193 // Link to each hidden object ID, $params[1] is the url param
194 $revert = Linker::linkKnown(
195 SpecialPage::getTitleFor( 'Revisiondelete' ),
196 $this->msg( 'revdel-restore' )->escaped(),
197 array(),
198 array(
199 'target' => $this->entry->getTarget()->getPrefixedText(),
200 'type' => 'logging',
201 'ids' => $query
202 )
203 );
204
205 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
206 default:
207 return '';
208 }
209 }
210 }