Followup r77679, 1 more for bug 23332
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleter.php
1 <?php
2 /**
3 * Revision/log/file deletion backend
4 *
5 * @file
6 */
7
8 /**
9 * Temporary b/c interface, collection of static functions.
10 * @ingroup SpecialPage
11 */
12 class RevisionDeleter {
13 /**
14 * Checks for a change in the bitfield for a certain option and updates the
15 * provided array accordingly.
16 *
17 * @param $desc String: description to add to the array if the option was
18 * enabled / disabled.
19 * @param $field Integer: the bitmask describing the single option.
20 * @param $diff Integer: the xor of the old and new bitfields.
21 * @param $new Integer: the new bitfield
22 * @param $arr Array: the array to update.
23 */
24 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
25 if( $diff & $field ) {
26 $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
27 }
28 }
29
30 /**
31 * Gets an array of message keys describing the changes made to the visibility
32 * of the revision. If the resulting array is $arr, then $arr[0] will contain an
33 * array of strings describing the items that were hidden, $arr[2] will contain
34 * an array of strings describing the items that were unhidden, and $arr[3] will
35 * contain an array with a single string, which can be one of "applied
36 * restrictions to sysops", "removed restrictions from sysops", or null.
37 *
38 * @param $n Integer: the new bitfield.
39 * @param $o Integer: the old bitfield.
40 * @return An array as described above.
41 */
42 protected static function getChanges( $n, $o ) {
43 $diff = $n ^ $o;
44 $ret = array( 0 => array(), 1 => array(), 2 => array() );
45 // Build bitfield changes in language
46 self::checkItem( 'revdelete-content',
47 Revision::DELETED_TEXT, $diff, $n, $ret );
48 self::checkItem( 'revdelete-summary',
49 Revision::DELETED_COMMENT, $diff, $n, $ret );
50 self::checkItem( 'revdelete-uname',
51 Revision::DELETED_USER, $diff, $n, $ret );
52 // Restriction application to sysops
53 if( $diff & Revision::DELETED_RESTRICTED ) {
54 if( $n & Revision::DELETED_RESTRICTED )
55 $ret[2][] = 'revdelete-restricted';
56 else
57 $ret[2][] = 'revdelete-unrestricted';
58 }
59 return $ret;
60 }
61
62 /**
63 * Gets a log message to describe the given revision visibility change. This
64 * message will be of the form "[hid {content, edit summary, username}];
65 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
66 *
67 * @param $count Integer: The number of effected revisions.
68 * @param $nbitfield Integer: The new bitfield for the revision.
69 * @param $obitfield Integer: The old bitfield for the revision.
70 * @param $isForLog Boolean
71 * @param $forContent Boolean
72 */
73 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false, $forContent = false ) {
74 global $wgLang, $wgContLang;
75
76 $lang = $forContent ? $wgContLang : $wgLang;
77 $msgFunc = $forContent ? "wfMsgForContent" : "wfMsg";
78
79 $changes = self::getChanges( $nbitfield, $obitfield );
80 array_walk($changes, 'RevisionDeleter::expandMessageArray', $forContent);
81
82 $changesText = array();
83
84 if( count( $changes[0] ) ) {
85 $changesText[] = $msgFunc( 'revdelete-hid', $lang->commaList( $changes[0] ) );
86 }
87 if( count( $changes[1] ) ) {
88 $changesText[] = $msgFunc( 'revdelete-unhid', $lang->commaList( $changes[1] ) );
89 }
90
91 $s = $lang->semicolonList( $changesText );
92 if( count( $changes[2] ) ) {
93 $s .= $s ? ' (' . $changes[2][0] . ')' : ' ' . $changes[2][0];
94 }
95
96 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
97 return wfMsgExt( $msg, $forContent ? array( 'parsemag', 'content' ) : array( 'parsemag' ), $s, $lang->formatNum($count) );
98 }
99
100 private static function expandMessageArray(& $msg, $key, $forContent) {
101 if ( is_array ($msg) ) {
102 array_walk($msg, 'RevisionDeleter::expandMessageArray', $forContent);
103 } else {
104 if ( $forContent ) {
105 $msg = wfMsgForContent($msg);
106 } else {
107 $msg = wfMsg($msg);
108 }
109 }
110 }
111
112 // Get DB field name for URL param...
113 // Future code for other things may also track
114 // other types of revision-specific changes.
115 // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
116 public static function getRelationType( $typeName ) {
117 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
118 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
119 }
120 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
121 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
122 $list = new $class( null, null, null );
123 return $list->getIdField();
124 } else {
125 return null;
126 }
127 }
128
129 // Checks if a revision still exists in the revision table.
130 // If it doesn't, returns the corresponding ar_timestamp field
131 // so that this key can be used instead.
132 public static function checkRevisionExistence( $title, $revid ) {
133 $dbr = wfGetDB( DB_SLAVE );
134 $exists = $dbr->selectField( 'revision', '1',
135 array( 'rev_id' => $revid ), __METHOD__ );
136
137 if ( $exists ) {
138 return true;
139 }
140
141 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
142 array( 'ar_namespace' => $title->getNamespace(),
143 'ar_title' => $title->getDBkey(),
144 'ar_rev_id' => $revid ), __METHOD__ );
145
146 return $timestamp;
147 }
148
149 // Creates utility links for log entries.
150 public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
151 global $wgLang;
152
153 if( count($paramArray) >= 2 ) {
154 // Different revision types use different URL params...
155 $originalKey = $key = $paramArray[0];
156 // $paramArray[1] is a CSV of the IDs
157 $Ids = explode( ',', $paramArray[1] );
158
159 $revert = array();
160
161 // For if undeleted revisions are found amidst deleted ones.
162 $undeletedRevisions = array();
163
164 // This is not going to work if some revs are deleted and some
165 // aren't.
166 if ($key == 'revision') {
167 foreach( $Ids as $k => $id ) {
168 $existResult =
169 self::checkRevisionExistence( $title, $id );
170
171 if ($existResult !== true) {
172 $key = 'archive';
173 $Ids[$k] = $existResult;
174 } else {
175 // Undeleted revision amidst deleted ones
176 unset($Ids[$k]);
177 $undeletedRevisions[] = $id;
178 }
179 }
180
181 if ( $key == $originalKey ) {
182 $Ids = $undeletedRevisions;
183 $undeletedRevisions = array();
184 }
185 }
186
187 // Diff link for single rev deletions
188 if( count($Ids) == 1 && !count($undeletedRevisions) ) {
189 // Live revision diffs...
190 if( in_array( $key, array( 'oldid', 'revision' ) ) ) {
191 $revert[] = $skin->link(
192 $title,
193 $messages['diff'],
194 array(),
195 array(
196 'diff' => intval( $Ids[0] ),
197 'unhide' => 1
198 ),
199 array( 'known', 'noclasses' )
200 );
201 // Deleted revision diffs...
202 } else if( in_array( $key, array( 'artimestamp','archive' ) ) ) {
203 $revert[] = $skin->link(
204 SpecialPage::getTitleFor( 'Undelete' ),
205 $messages['diff'],
206 array(),
207 array(
208 'target' => $title->getPrefixedDBKey(),
209 'diff' => 'prev',
210 'timestamp' => $Ids[0]
211 ),
212 array( 'known', 'noclasses' )
213 );
214 }
215 }
216
217 // View/modify link...
218 if ( count($undeletedRevisions) ) {
219 // FIXME THIS IS A HORRIBLE HORRIBLE HACK AND SHOULD DIE
220 // It's not possible to pass a list of both deleted and
221 // undeleted revisions to SpecialRevisionDelete, so we're
222 // stuck with two links. See bug 23363.
223 $restoreLinks = array();
224
225 $restoreLinks[] = $skin->link(
226 SpecialPage::getTitleFor( 'Revisiondelete' ),
227 $messages['revdel-restore-visible'],
228 array(),
229 array(
230 'target' => $title->getPrefixedText(),
231 'type' => $originalKey,
232 'ids' => implode(',', $undeletedRevisions),
233 ),
234 array( 'known', 'noclasses' )
235 );
236
237 $restoreLinks[] = $skin->link(
238 SpecialPage::getTitleFor( 'Revisiondelete' ),
239 $messages['revdel-restore-deleted'],
240 array(),
241 array(
242 'target' => $title->getPrefixedText(),
243 'type' => $key,
244 'ids' => implode(',', $Ids),
245 ),
246 array( 'known', 'noclasses' )
247 );
248
249 $revert[] = $messages['revdel-restore'] . ' [' .
250 $wgLang->pipeList( $restoreLinks ) . ']';
251 } else {
252 $revert[] = $skin->link(
253 SpecialPage::getTitleFor( 'Revisiondelete' ),
254 $messages['revdel-restore'],
255 array(),
256 array(
257 'target' => $title->getPrefixedText(),
258 'type' => $key,
259 'ids' => implode(',', $Ids),
260 ),
261 array( 'known', 'noclasses' )
262 );
263 }
264
265 // Pipe links
266 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
267 }
268 return '';
269 }
270 }