* Changed dynamic calls to Linker methods into static ones
[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 $language Language object to use
71 * @param $isForLog Boolean
72 */
73 public static function getLogMessage( $count, $nbitfield, $obitfield, $language, $isForLog = false ) {
74 $changes = self::getChanges( $nbitfield, $obitfield );
75 array_walk( $changes, array( __CLASS__, 'expandMessageArray' ), $language );
76
77 $changesText = array();
78
79 if( count( $changes[0] ) ) {
80 $changesText[] = wfMsgExt( 'revdelete-hid', array( 'parsemag', 'language' => $language ), $language->commaList( $changes[0] ) );
81 }
82 if( count( $changes[1] ) ) {
83 $changesText[] = wfMsgExt( 'revdelete-unhid', array( 'parsemag', 'language' => $language ), $language->commaList( $changes[1] ) );
84 }
85
86 $s = $language->semicolonList( $changesText );
87 if( count( $changes[2] ) ) {
88 $s .= $s ? ' (' . $changes[2][0] . ')' : ' ' . $changes[2][0];
89 }
90
91 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
92 return wfMsgExt( $msg, array( 'parsemag', 'language' => $language ), $s, $language->formatNum($count) );
93 }
94
95 private static function expandMessageArray( &$msg, $key, $language ) {
96 if ( is_array ( $msg ) ) {
97 array_walk( $msg, array( __CLASS__, 'expandMessageArray' ), $language );
98 } else {
99 $msg = wfMsgExt( $msg, array( 'parsemag', 'language' => $language ) );
100 }
101 }
102
103 // Get DB field name for URL param...
104 // Future code for other things may also track
105 // other types of revision-specific changes.
106 // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
107 public static function getRelationType( $typeName ) {
108 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
109 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
110 }
111 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
112 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
113 return call_user_func( array( $class, 'getRelationType' ) );
114 } else {
115 return null;
116 }
117 }
118
119 /**
120 * Checks if a revision still exists in the revision table.
121 * If it doesn't, returns the corresponding ar_timestamp field
122 * so that this key can be used instead.
123 *
124 * @param $title Title
125 * @param $revid
126 * @return bool|mixed
127 */
128 public static function checkRevisionExistence( $title, $revid ) {
129 $dbr = wfGetDB( DB_SLAVE );
130 $exists = $dbr->selectField( 'revision', '1',
131 array( 'rev_id' => $revid ), __METHOD__ );
132
133 if ( $exists ) {
134 return true;
135 }
136
137 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
138 array( 'ar_namespace' => $title->getNamespace(),
139 'ar_title' => $title->getDBkey(),
140 'ar_rev_id' => $revid ), __METHOD__ );
141
142 return $timestamp;
143 }
144
145 /**
146 * Creates utility links for log entries.
147 *
148 * @param $title Title
149 * @param $paramArray Array
150 * @param $skin Skin
151 * @param $messages
152 * @return String
153 */
154 public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
155 global $wgLang;
156
157 if ( count( $paramArray ) >= 2 ) {
158 // Different revision types use different URL params...
159 $originalKey = $key = $paramArray[0];
160 // $paramArray[1] is a CSV of the IDs
161 $Ids = explode( ',', $paramArray[1] );
162
163 $revert = array();
164
165 // Diff link for single rev deletions
166 if ( count( $Ids ) == 1 ) {
167 // Live revision diffs...
168 if ( in_array( $key, array( 'oldid', 'revision' ) ) ) {
169 $revert[] = $skin->link(
170 $title,
171 $messages['diff'],
172 array(),
173 array(
174 'diff' => intval( $Ids[0] ),
175 'unhide' => 1
176 ),
177 array( 'known', 'noclasses' )
178 );
179 // Deleted revision diffs...
180 } elseif ( in_array( $key, array( 'artimestamp','archive' ) ) ) {
181 $revert[] = $skin->link(
182 SpecialPage::getTitleFor( 'Undelete' ),
183 $messages['diff'],
184 array(),
185 array(
186 'target' => $title->getPrefixedDBKey(),
187 'diff' => 'prev',
188 'timestamp' => $Ids[0]
189 ),
190 array( 'known', 'noclasses' )
191 );
192 }
193 }
194
195 // View/modify link...
196 $revert[] = $skin->link(
197 SpecialPage::getTitleFor( 'Revisiondelete' ),
198 $messages['revdel-restore'],
199 array(),
200 array(
201 'target' => $title->getPrefixedText(),
202 'type' => $key,
203 'ids' => implode(',', $Ids),
204 ),
205 array( 'known', 'noclasses' )
206 );
207
208 // Pipe links
209 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
210 }
211 return '';
212 }
213 }