Documentation
[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
32 * visibility of the revision.
33 *
34 * If the resulting array is $arr, then $arr[0] will contain an array of
35 * keys describing the items that were hidden, $arr[1] will contain
36 * an array of keys describing the items that were unhidden, and $arr[2]
37 * will contain an array with a single message key, which can be one of
38 * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression
39 * or null to indicate nothing in particular.
40 * You can turn the keys in $arr[0] and $arr[1] into message keys by
41 * appending -hid and and -unhid to the keys respectively.
42 *
43 * @param $n Integer: the new bitfield.
44 * @param $o Integer: the old bitfield.
45 * @return An array as described above.
46 * @since 1.19 public
47 */
48 public static function getChanges( $n, $o ) {
49 $diff = $n ^ $o;
50 $ret = array( 0 => array(), 1 => array(), 2 => array() );
51 // Build bitfield changes in language
52 self::checkItem( 'revdelete-content',
53 Revision::DELETED_TEXT, $diff, $n, $ret );
54 self::checkItem( 'revdelete-summary',
55 Revision::DELETED_COMMENT, $diff, $n, $ret );
56 self::checkItem( 'revdelete-uname',
57 Revision::DELETED_USER, $diff, $n, $ret );
58 // Restriction application to sysops
59 if( $diff & Revision::DELETED_RESTRICTED ) {
60 if( $n & Revision::DELETED_RESTRICTED )
61 $ret[2][] = 'revdelete-restricted';
62 else
63 $ret[2][] = 'revdelete-unrestricted';
64 }
65 return $ret;
66 }
67
68 /** Get DB field name for URL param...
69 * Future code for other things may also track
70 * other types of revision-specific changes.
71 * @return string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
72 */
73 public static function getRelationType( $typeName ) {
74 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
75 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
76 }
77 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
78 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
79 return call_user_func( array( $class, 'getRelationType' ) );
80 } else {
81 return null;
82 }
83 }
84
85 /**
86 * Checks if a revision still exists in the revision table.
87 * If it doesn't, returns the corresponding ar_timestamp field
88 * so that this key can be used instead.
89 *
90 * @param $title Title
91 * @param $revid
92 * @return bool|mixed
93 */
94 public static function checkRevisionExistence( $title, $revid ) {
95 $dbr = wfGetDB( DB_SLAVE );
96 $exists = $dbr->selectField( 'revision', '1',
97 array( 'rev_id' => $revid ), __METHOD__ );
98
99 if ( $exists ) {
100 return true;
101 }
102
103 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
104 array( 'ar_namespace' => $title->getNamespace(),
105 'ar_title' => $title->getDBkey(),
106 'ar_rev_id' => $revid ), __METHOD__ );
107
108 return $timestamp;
109 }
110
111 /**
112 * Creates utility links for log entries.
113 *
114 * @param $title Title
115 * @param $paramArray Array
116 * @param $skin Skin
117 * @param $messages
118 * @return String
119 */
120 public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
121 global $wgLang;
122
123 if ( count( $paramArray ) >= 2 ) {
124 // Different revision types use different URL params...
125 $originalKey = $key = $paramArray[0];
126 // $paramArray[1] is a CSV of the IDs
127 $Ids = explode( ',', $paramArray[1] );
128
129 $revert = array();
130
131 // Diff link for single rev deletions
132 if ( count( $Ids ) == 1 ) {
133 // Live revision diffs...
134 if ( in_array( $key, array( 'oldid', 'revision' ) ) ) {
135 $revert[] = $skin->link(
136 $title,
137 $messages['diff'],
138 array(),
139 array(
140 'diff' => intval( $Ids[0] ),
141 'unhide' => 1
142 ),
143 array( 'known', 'noclasses' )
144 );
145 // Deleted revision diffs...
146 } elseif ( in_array( $key, array( 'artimestamp','archive' ) ) ) {
147 $revert[] = $skin->link(
148 SpecialPage::getTitleFor( 'Undelete' ),
149 $messages['diff'],
150 array(),
151 array(
152 'target' => $title->getPrefixedDBKey(),
153 'diff' => 'prev',
154 'timestamp' => $Ids[0]
155 ),
156 array( 'known', 'noclasses' )
157 );
158 }
159 }
160
161 // View/modify link...
162 $revert[] = $skin->link(
163 SpecialPage::getTitleFor( 'Revisiondelete' ),
164 $messages['revdel-restore'],
165 array(),
166 array(
167 'target' => $title->getPrefixedText(),
168 'type' => $key,
169 'ids' => implode(',', $Ids),
170 ),
171 array( 'known', 'noclasses' )
172 );
173
174 // Pipe links
175 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
176 }
177 return '';
178 }
179 }