Merge "Remove convertPlural methods already served by CLDR plural system"
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleteAbstracts.php
1 <?php
2 /**
3 * Interface definition for deletable items.
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 * Abstract base class for a list of deletable items. The list class
26 * needs to be able to make a query from a set of identifiers to pull
27 * relevant rows, to return RevDel_Item subclasses wrapping them, and
28 * to wrap bulk update operations.
29 */
30 abstract class RevDel_List extends RevisionListBase {
31 function __construct( IContextSource $context, Title $title, array $ids ) {
32 parent::__construct( $context, $title );
33 $this->ids = $ids;
34 }
35
36 /**
37 * Get the DB field name associated with the ID list.
38 * This used to populate the log_search table for finding log entries.
39 * Override this function.
40 * @return string|null
41 */
42 public static function getRelationType() {
43 return null;
44 }
45
46 /**
47 * Get the user right required for this list type
48 * Override this function.
49 * @since 1.22
50 * @return string|null
51 */
52 public static function getRestriction() {
53 return null;
54 }
55
56 /**
57 * Get the revision deletion constant for this list type
58 * Override this function.
59 * @since 1.22
60 * @return int|null
61 */
62 public static function getRevdelConstant() {
63 return null;
64 }
65
66 /**
67 * Suggest a target for the revision deletion
68 * Optionally override this function.
69 * @since 1.22
70 * @param Title|null $target User-supplied target
71 * @param array $ids
72 * @return Title|null
73 */
74 public static function suggestTarget( $target, array $ids ) {
75 return $target;
76 }
77
78 /**
79 * Set the visibility for the revisions in this list. Logging and
80 * transactions are done here.
81 *
82 * @param array $params Associative array of parameters. Members are:
83 * value: The integer value to set the visibility to
84 * comment: The log comment.
85 * @return Status
86 */
87 public function setVisibility( $params ) {
88 $bitPars = $params['value'];
89 $comment = $params['comment'];
90
91 $this->res = false;
92 $dbw = wfGetDB( DB_MASTER );
93 $this->doQuery( $dbw );
94 $dbw->begin( __METHOD__ );
95 $status = Status::newGood();
96 $missing = array_flip( $this->ids );
97 $this->clearFileOps();
98 $idsForLog = array();
99 $authorIds = $authorIPs = array();
100
101 for ( $this->reset(); $this->current(); $this->next() ) {
102 $item = $this->current();
103 unset( $missing[$item->getId()] );
104
105 $oldBits = $item->getBits();
106 // Build the actual new rev_deleted bitfield
107 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
108
109 if ( $oldBits == $newBits ) {
110 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
111 $status->failCount++;
112 continue;
113 } elseif ( $oldBits == 0 && $newBits != 0 ) {
114 $opType = 'hide';
115 } elseif ( $oldBits != 0 && $newBits == 0 ) {
116 $opType = 'show';
117 } else {
118 $opType = 'modify';
119 }
120
121 if ( $item->isHideCurrentOp( $newBits ) ) {
122 // Cannot hide current version text
123 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
124 $status->failCount++;
125 continue;
126 }
127 if ( !$item->canView() ) {
128 // Cannot access this revision
129 $msg = ( $opType == 'show' ) ?
130 'revdelete-show-no-access' : 'revdelete-modify-no-access';
131 $status->error( $msg, $item->formatDate(), $item->formatTime() );
132 $status->failCount++;
133 continue;
134 }
135 // Cannot just "hide from Sysops" without hiding any fields
136 if ( $newBits == Revision::DELETED_RESTRICTED ) {
137 $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
138 $status->failCount++;
139 continue;
140 }
141
142 // Update the revision
143 $ok = $item->setBits( $newBits );
144
145 if ( $ok ) {
146 $idsForLog[] = $item->getId();
147 $status->successCount++;
148 if ( $item->getAuthorId() > 0 ) {
149 $authorIds[] = $item->getAuthorId();
150 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
151 $authorIPs[] = $item->getAuthorName();
152 }
153 } else {
154 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
155 $status->failCount++;
156 }
157 }
158
159 // Handle missing revisions
160 foreach ( $missing as $id => $unused ) {
161 $status->error( 'revdelete-modify-missing', $id );
162 $status->failCount++;
163 }
164
165 if ( $status->successCount == 0 ) {
166 $status->ok = false;
167 $dbw->rollback( __METHOD__ );
168 return $status;
169 }
170
171 // Save success count
172 $successCount = $status->successCount;
173
174 // Move files, if there are any
175 $status->merge( $this->doPreCommitUpdates() );
176 if ( !$status->isOK() ) {
177 // Fatal error, such as no configured archive directory
178 $dbw->rollback( __METHOD__ );
179 return $status;
180 }
181
182 // Log it
183 $this->updateLog( array(
184 'title' => $this->title,
185 'count' => $successCount,
186 'newBits' => $newBits,
187 'oldBits' => $oldBits,
188 'comment' => $comment,
189 'ids' => $idsForLog,
190 'authorIds' => $authorIds,
191 'authorIPs' => $authorIPs
192 ) );
193 $dbw->commit( __METHOD__ );
194
195 // Clear caches
196 $status->merge( $this->doPostCommitUpdates() );
197 return $status;
198 }
199
200 /**
201 * Reload the list data from the master DB. This can be done after setVisibility()
202 * to allow $item->getHTML() to show the new data.
203 */
204 function reloadFromMaster() {
205 $dbw = wfGetDB( DB_MASTER );
206 $this->res = $this->doQuery( $dbw );
207 }
208
209 /**
210 * Record a log entry on the action
211 * @param array $params Associative array of parameters:
212 * newBits: The new value of the *_deleted bitfield
213 * oldBits: The old value of the *_deleted bitfield.
214 * title: The target title
215 * ids: The ID list
216 * comment: The log comment
217 * authorsIds: The array of the user IDs of the offenders
218 * authorsIPs: The array of the IP/anon user offenders
219 * @throws MWException
220 */
221 protected function updateLog( $params ) {
222 // Get the URL param's corresponding DB field
223 $field = RevisionDeleter::getRelationType( $this->getType() );
224 if ( !$field ) {
225 throw new MWException( "Bad log URL param type!" );
226 }
227 // Put things hidden from sysops in the oversight log
228 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
229 $logType = 'suppress';
230 } else {
231 $logType = 'delete';
232 }
233 // Add params for effected page and ids
234 $logParams = $this->getLogParams( $params );
235 // Actually add the deletion log entry
236 $log = new LogPage( $logType );
237 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
238 $params['comment'], $logParams, $this->getUser() );
239 // Allow for easy searching of deletion log items for revision/log items
240 $log->addRelations( $field, $params['ids'], $logid );
241 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
242 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
243 }
244
245 /**
246 * Get the log action for this list type
247 * @return string
248 */
249 public function getLogAction() {
250 return 'revision';
251 }
252
253 /**
254 * Get log parameter array.
255 * @param array $params Associative array of log parameters, same as updateLog()
256 * @return array
257 */
258 public function getLogParams( $params ) {
259 return array(
260 $this->getType(),
261 implode( ',', $params['ids'] ),
262 "ofield={$params['oldBits']}",
263 "nfield={$params['newBits']}"
264 );
265 }
266
267 /**
268 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
269 * STUB
270 */
271 public function clearFileOps() {
272 }
273
274 /**
275 * A hook for setVisibility(): do batch updates pre-commit.
276 * STUB
277 * @return Status
278 */
279 public function doPreCommitUpdates() {
280 return Status::newGood();
281 }
282
283 /**
284 * A hook for setVisibility(): do any necessary updates post-commit.
285 * STUB
286 * @return Status
287 */
288 public function doPostCommitUpdates() {
289 return Status::newGood();
290 }
291
292 /**
293 * Get the integer value of the flag used for suppression
294 */
295 abstract public function getSuppressBit();
296 }
297
298 /**
299 * Abstract base class for deletable items
300 */
301 abstract class RevDel_Item extends RevisionItemBase {
302 /**
303 * Returns true if the item is "current", and the operation to set the given
304 * bits can't be executed for that reason
305 * STUB
306 * @return bool
307 */
308 public function isHideCurrentOp( $newBits ) {
309 return false;
310 }
311
312 /**
313 * Get the current deletion bitfield value
314 */
315 abstract public function getBits();
316
317 /**
318 * Set the visibility of the item. This should do any necessary DB queries.
319 *
320 * The DB update query should have a condition which forces it to only update
321 * if the value in the DB matches the value fetched earlier with the SELECT.
322 * If the update fails because it did not match, the function should return
323 * false. This prevents concurrency problems.
324 *
325 * @return boolean success
326 */
327 abstract public function setBits( $newBits );
328 }