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