Merge "Don't localize parentheses in version number in parserTests.php"
[lhc/web/wiklou.git] / includes / revisiondelete / RevDelList.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup RevisionDelete
20 */
21
22 /**
23 * Abstract base class for a list of deletable items. The list class
24 * needs to be able to make a query from a set of identifiers to pull
25 * relevant rows, to return RevDelItem subclasses wrapping them, and
26 * to wrap bulk update operations.
27 */
28 abstract class RevDelList extends RevisionListBase {
29 function __construct( IContextSource $context, Title $title, array $ids ) {
30 parent::__construct( $context, $title );
31 $this->ids = $ids;
32 }
33
34 /**
35 * Get the DB field name associated with the ID list.
36 * This used to populate the log_search table for finding log entries.
37 * Override this function.
38 * @return string|null
39 */
40 public static function getRelationType() {
41 return null;
42 }
43
44 /**
45 * Get the user right required for this list type
46 * Override this function.
47 * @since 1.22
48 * @return string|null
49 */
50 public static function getRestriction() {
51 return null;
52 }
53
54 /**
55 * Get the revision deletion constant for this list type
56 * Override this function.
57 * @since 1.22
58 * @return int|null
59 */
60 public static function getRevdelConstant() {
61 return null;
62 }
63
64 /**
65 * Suggest a target for the revision deletion
66 * Optionally override this function.
67 * @since 1.22
68 * @param Title|null $target User-supplied target
69 * @param array $ids
70 * @return Title|null
71 */
72 public static function suggestTarget( $target, array $ids ) {
73 return $target;
74 }
75
76 /**
77 * Set the visibility for the revisions in this list. Logging and
78 * transactions are done here.
79 *
80 * @param array $params Associative array of parameters. Members are:
81 * value: The integer value to set the visibility to
82 * comment: The log comment.
83 * perItemStatus: Set if you want per-item status reports
84 * @return Status
85 * @since 1.23 Added 'perItemStatus' param
86 */
87 public function setVisibility( $params ) {
88 $bitPars = $params['value'];
89 $comment = $params['comment'];
90 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
91
92 // CAS-style checks are done on the _deleted fields so the select
93 // does not need to use FOR UPDATE nor be in the atomic section
94 $dbw = wfGetDB( DB_MASTER );
95 $this->res = $this->doQuery( $dbw );
96
97 $dbw->startAtomic( __METHOD__ );
98
99 $status = Status::newGood();
100 $missing = array_flip( $this->ids );
101 $this->clearFileOps();
102 $idsForLog = array();
103 $authorIds = $authorIPs = array();
104
105 if ( $perItemStatus ) {
106 $status->itemStatuses = array();
107 }
108
109 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
110 for ( $this->reset(); $this->current(); $this->next() ) {
111 // @codingStandardsIgnoreEnd
112 $item = $this->current();
113 unset( $missing[$item->getId()] );
114
115 if ( $perItemStatus ) {
116 $itemStatus = Status::newGood();
117 $status->itemStatuses[$item->getId()] = $itemStatus;
118 } else {
119 $itemStatus = $status;
120 }
121
122 $oldBits = $item->getBits();
123 // Build the actual new rev_deleted bitfield
124 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
125
126 if ( $oldBits == $newBits ) {
127 $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
128 $status->failCount++;
129 continue;
130 } elseif ( $oldBits == 0 && $newBits != 0 ) {
131 $opType = 'hide';
132 } elseif ( $oldBits != 0 && $newBits == 0 ) {
133 $opType = 'show';
134 } else {
135 $opType = 'modify';
136 }
137
138 if ( $item->isHideCurrentOp( $newBits ) ) {
139 // Cannot hide current version text
140 $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
141 $status->failCount++;
142 continue;
143 }
144 if ( !$item->canView() ) {
145 // Cannot access this revision
146 $msg = ( $opType == 'show' ) ?
147 'revdelete-show-no-access' : 'revdelete-modify-no-access';
148 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
149 $status->failCount++;
150 continue;
151 }
152 // Cannot just "hide from Sysops" without hiding any fields
153 if ( $newBits == Revision::DELETED_RESTRICTED ) {
154 $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
155 $status->failCount++;
156 continue;
157 }
158
159 // Update the revision
160 $ok = $item->setBits( $newBits );
161
162 if ( $ok ) {
163 $idsForLog[] = $item->getId();
164 $status->successCount++;
165 if ( $item->getAuthorId() > 0 ) {
166 $authorIds[] = $item->getAuthorId();
167 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
168 $authorIPs[] = $item->getAuthorName();
169 }
170 } else {
171 $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
172 $status->failCount++;
173 }
174 }
175
176 // Handle missing revisions
177 foreach ( $missing as $id => $unused ) {
178 if ( $perItemStatus ) {
179 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
180 } else {
181 $status->error( 'revdelete-modify-missing', $id );
182 }
183 $status->failCount++;
184 }
185
186 if ( $status->successCount == 0 ) {
187 $dbw->rollback( __METHOD__ );
188 return $status;
189 }
190
191 // Save success count
192 $successCount = $status->successCount;
193
194 // Move files, if there are any
195 $status->merge( $this->doPreCommitUpdates() );
196 if ( !$status->isOK() ) {
197 // Fatal error, such as no configured archive directory
198 $dbw->rollback( __METHOD__ );
199 return $status;
200 }
201
202 // Log it
203 $this->updateLog( array(
204 'title' => $this->title,
205 'count' => $successCount,
206 'newBits' => $newBits,
207 'oldBits' => $oldBits,
208 'comment' => $comment,
209 'ids' => $idsForLog,
210 'authorIds' => $authorIds,
211 'authorIPs' => $authorIPs
212 ) );
213
214 // Clear caches
215 $that = $this;
216 $dbw->onTransactionIdle( function() use ( $that ) {
217 $that->doPostCommitUpdates();
218 } );
219
220 $dbw->endAtomic( __METHOD__ );
221
222 return $status;
223 }
224
225 /**
226 * Reload the list data from the master DB. This can be done after setVisibility()
227 * to allow $item->getHTML() to show the new data.
228 */
229 function reloadFromMaster() {
230 $dbw = wfGetDB( DB_MASTER );
231 $this->res = $this->doQuery( $dbw );
232 }
233
234 /**
235 * Record a log entry on the action
236 * @param array $params Associative array of parameters:
237 * newBits: The new value of the *_deleted bitfield
238 * oldBits: The old value of the *_deleted bitfield.
239 * title: The target title
240 * ids: The ID list
241 * comment: The log comment
242 * authorsIds: The array of the user IDs of the offenders
243 * authorsIPs: The array of the IP/anon user offenders
244 * @throws MWException
245 */
246 protected function updateLog( $params ) {
247 // Get the URL param's corresponding DB field
248 $field = RevisionDeleter::getRelationType( $this->getType() );
249 if ( !$field ) {
250 throw new MWException( "Bad log URL param type!" );
251 }
252 // Put things hidden from sysops in the oversight log
253 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
254 $logType = 'suppress';
255 } else {
256 $logType = 'delete';
257 }
258 // Add params for affected page and ids
259 $logParams = $this->getLogParams( $params );
260 // Actually add the deletion log entry
261 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
262 $logEntry->setTarget( $params['title'] );
263 $logEntry->setComment( $params['comment'] );
264 $logEntry->setParameters( $logParams );
265 $logEntry->setPerformer( $this->getUser() );
266 // Allow for easy searching of deletion log items for revision/log items
267 $logEntry->setRelations( array(
268 $field => $params['ids'],
269 'target_author_id' => $params['authorIds'],
270 'target_author_ip' => $params['authorIPs'],
271 ) );
272 $logId = $logEntry->insert();
273 $logEntry->publish( $logId );
274 }
275
276 /**
277 * Get the log action for this list type
278 * @return string
279 */
280 public function getLogAction() {
281 return 'revision';
282 }
283
284 /**
285 * Get log parameter array.
286 * @param array $params Associative array of log parameters, same as updateLog()
287 * @return array
288 */
289 public function getLogParams( $params ) {
290 return array(
291 '4::type' => $this->getType(),
292 '5::ids' => $params['ids'],
293 '6::ofield' => $params['oldBits'],
294 '7::nfield' => $params['newBits'],
295 );
296 }
297
298 /**
299 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
300 * STUB
301 */
302 public function clearFileOps() {
303 }
304
305 /**
306 * A hook for setVisibility(): do batch updates pre-commit.
307 * STUB
308 * @return Status
309 */
310 public function doPreCommitUpdates() {
311 return Status::newGood();
312 }
313
314 /**
315 * A hook for setVisibility(): do any necessary updates post-commit.
316 * STUB
317 * @return Status
318 */
319 public function doPostCommitUpdates() {
320 return Status::newGood();
321 }
322
323 /**
324 * Get the integer value of the flag used for suppression
325 */
326 abstract public function getSuppressBit();
327 }