Merge "Forward port of https://www.mediawiki.org/wiki/Special:Code/MediaWiki/105964"
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleter.php
1 <?php
2 /**
3 * Revision/log/file deletion backend
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup RevisionDelete
22 */
23
24 /**
25 * Temporary b/c interface, collection of static functions.
26 * @ingroup SpecialPage
27 * @ingroup RevisionDelete
28 */
29 class RevisionDeleter {
30 /**
31 * Checks for a change in the bitfield for a certain option and updates the
32 * provided array accordingly.
33 *
34 * @param $desc String: description to add to the array if the option was
35 * enabled / disabled.
36 * @param $field Integer: the bitmask describing the single option.
37 * @param $diff Integer: the xor of the old and new bitfields.
38 * @param $new Integer: the new bitfield
39 * @param $arr Array: the array to update.
40 */
41 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
42 if( $diff & $field ) {
43 $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
44 }
45 }
46
47 /**
48 * Gets an array of message keys describing the changes made to the
49 * visibility of the revision.
50 *
51 * If the resulting array is $arr, then $arr[0] will contain an array of
52 * keys describing the items that were hidden, $arr[1] will contain
53 * an array of keys describing the items that were unhidden, and $arr[2]
54 * will contain an array with a single message key, which can be one of
55 * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression
56 * or null to indicate nothing in particular.
57 * You can turn the keys in $arr[0] and $arr[1] into message keys by
58 * appending -hid and and -unhid to the keys respectively.
59 *
60 * @param $n Integer: the new bitfield.
61 * @param $o Integer: the old bitfield.
62 * @return array An array as described above.
63 * @since 1.19 public
64 */
65 public static function getChanges( $n, $o ) {
66 $diff = $n ^ $o;
67 $ret = array( 0 => array(), 1 => array(), 2 => array() );
68 // Build bitfield changes in language
69 self::checkItem( 'revdelete-content',
70 Revision::DELETED_TEXT, $diff, $n, $ret );
71 self::checkItem( 'revdelete-summary',
72 Revision::DELETED_COMMENT, $diff, $n, $ret );
73 self::checkItem( 'revdelete-uname',
74 Revision::DELETED_USER, $diff, $n, $ret );
75 // Restriction application to sysops
76 if( $diff & Revision::DELETED_RESTRICTED ) {
77 if( $n & Revision::DELETED_RESTRICTED )
78 $ret[2][] = 'revdelete-restricted';
79 else
80 $ret[2][] = 'revdelete-unrestricted';
81 }
82 return $ret;
83 }
84
85 /** Get DB field name for URL param...
86 * Future code for other things may also track
87 * other types of revision-specific changes.
88 * @return string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
89 */
90 public static function getRelationType( $typeName ) {
91 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
92 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
93 }
94 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
95 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
96 return call_user_func( array( $class, 'getRelationType' ) );
97 } else {
98 return null;
99 }
100 }
101
102 /**
103 * Checks if a revision still exists in the revision table.
104 * If it doesn't, returns the corresponding ar_timestamp field
105 * so that this key can be used instead.
106 *
107 * @param $title Title
108 * @param $revid
109 * @return bool|mixed
110 */
111 public static function checkRevisionExistence( $title, $revid ) {
112 $dbr = wfGetDB( DB_SLAVE );
113 $exists = $dbr->selectField( 'revision', '1',
114 array( 'rev_id' => $revid ), __METHOD__ );
115
116 if ( $exists ) {
117 return true;
118 }
119
120 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
121 array( 'ar_namespace' => $title->getNamespace(),
122 'ar_title' => $title->getDBkey(),
123 'ar_rev_id' => $revid ), __METHOD__ );
124
125 return $timestamp;
126 }
127
128 /**
129 * Creates utility links for log entries.
130 *
131 * @param $title Title
132 * @param $paramArray Array
133 * @param $messages
134 * @return String
135 */
136 public static function getLogLinks( $title, $paramArray, $messages ) {
137 global $wgLang;
138
139 if ( count( $paramArray ) >= 2 ) {
140 // Different revision types use different URL params...
141 $key = $paramArray[0];
142 // $paramArray[1] is a CSV of the IDs
143 $Ids = explode( ',', $paramArray[1] );
144
145 $revert = array();
146
147 // Diff link for single rev deletions
148 if ( count( $Ids ) == 1 ) {
149 // Live revision diffs...
150 if ( in_array( $key, array( 'oldid', 'revision' ) ) ) {
151 $revert[] = Linker::linkKnown(
152 $title,
153 $messages['diff'],
154 array(),
155 array(
156 'diff' => intval( $Ids[0] ),
157 'unhide' => 1
158 )
159 );
160 // Deleted revision diffs...
161 } elseif ( in_array( $key, array( 'artimestamp','archive' ) ) ) {
162 $revert[] = Linker::linkKnown(
163 SpecialPage::getTitleFor( 'Undelete' ),
164 $messages['diff'],
165 array(),
166 array(
167 'target' => $title->getPrefixedDBKey(),
168 'diff' => 'prev',
169 'timestamp' => $Ids[0]
170 )
171 );
172 }
173 }
174
175 // View/modify link...
176 $revert[] = Linker::linkKnown(
177 SpecialPage::getTitleFor( 'Revisiondelete' ),
178 $messages['revdel-restore'],
179 array(),
180 array(
181 'target' => $title->getPrefixedText(),
182 'type' => $key,
183 'ids' => implode(',', $Ids),
184 )
185 );
186
187 // Pipe links
188 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
189 }
190 return '';
191 }
192 }