* Fixed indentation
[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 0 and 1 into message keys by appendin -hid and
41 * and -unhid to they 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 // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
72 public static function getRelationType( $typeName ) {
73 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
74 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
75 }
76 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
77 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
78 return call_user_func( array( $class, 'getRelationType' ) );
79 } else {
80 return null;
81 }
82 }
83
84 /**
85 * Checks if a revision still exists in the revision table.
86 * If it doesn't, returns the corresponding ar_timestamp field
87 * so that this key can be used instead.
88 *
89 * @param $title Title
90 * @param $revid
91 * @return bool|mixed
92 */
93 public static function checkRevisionExistence( $title, $revid ) {
94 $dbr = wfGetDB( DB_SLAVE );
95 $exists = $dbr->selectField( 'revision', '1',
96 array( 'rev_id' => $revid ), __METHOD__ );
97
98 if ( $exists ) {
99 return true;
100 }
101
102 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
103 array( 'ar_namespace' => $title->getNamespace(),
104 'ar_title' => $title->getDBkey(),
105 'ar_rev_id' => $revid ), __METHOD__ );
106
107 return $timestamp;
108 }
109
110 /**
111 * Creates utility links for log entries.
112 *
113 * @param $title Title
114 * @param $paramArray Array
115 * @param $skin Skin
116 * @param $messages
117 * @return String
118 */
119 public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
120 global $wgLang;
121
122 if ( count( $paramArray ) >= 2 ) {
123 // Different revision types use different URL params...
124 $originalKey = $key = $paramArray[0];
125 // $paramArray[1] is a CSV of the IDs
126 $Ids = explode( ',', $paramArray[1] );
127
128 $revert = array();
129
130 // Diff link for single rev deletions
131 if ( count( $Ids ) == 1 ) {
132 // Live revision diffs...
133 if ( in_array( $key, array( 'oldid', 'revision' ) ) ) {
134 $revert[] = $skin->link(
135 $title,
136 $messages['diff'],
137 array(),
138 array(
139 'diff' => intval( $Ids[0] ),
140 'unhide' => 1
141 ),
142 array( 'known', 'noclasses' )
143 );
144 // Deleted revision diffs...
145 } elseif ( in_array( $key, array( 'artimestamp','archive' ) ) ) {
146 $revert[] = $skin->link(
147 SpecialPage::getTitleFor( 'Undelete' ),
148 $messages['diff'],
149 array(),
150 array(
151 'target' => $title->getPrefixedDBKey(),
152 'diff' => 'prev',
153 'timestamp' => $Ids[0]
154 ),
155 array( 'known', 'noclasses' )
156 );
157 }
158 }
159
160 // View/modify link...
161 $revert[] = $skin->link(
162 SpecialPage::getTitleFor( 'Revisiondelete' ),
163 $messages['revdel-restore'],
164 array(),
165 array(
166 'target' => $title->getPrefixedText(),
167 'type' => $key,
168 'ids' => implode(',', $Ids),
169 ),
170 array( 'known', 'noclasses' )
171 );
172
173 // Pipe links
174 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
175 }
176 return '';
177 }
178 }