Merge "Http::getProxy() method to get proxy configuration"
[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 * Indicate whether any item in this list is suppressed
78 * @since 1.25
79 * @return bool
80 */
81 public function areAnySuppressed() {
82 $bit = $this->getSuppressBit();
83
84 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
85 for ( $this->reset(); $this->current(); $this->next() ) {
86 // @codingStandardsIgnoreEnd
87 $item = $this->current();
88 if ( $item->getBits() & $bit ) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 /**
96 * Set the visibility for the revisions in this list. Logging and
97 * transactions are done here.
98 *
99 * @param array $params Associative array of parameters. Members are:
100 * value: ExtractBitParams() bitfield array
101 * comment: The log comment.
102 * perItemStatus: Set if you want per-item status reports
103 * @return Status
104 * @since 1.23 Added 'perItemStatus' param
105 */
106 public function setVisibility( $params ) {
107 $bitPars = $params['value'];
108 $comment = $params['comment'];
109 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
110
111 // CAS-style checks are done on the _deleted fields so the select
112 // does not need to use FOR UPDATE nor be in the atomic section
113 $dbw = wfGetDB( DB_MASTER );
114 $this->res = $this->doQuery( $dbw );
115
116 $dbw->startAtomic( __METHOD__ );
117
118 $status = Status::newGood();
119 $missing = array_flip( $this->ids );
120 $this->clearFileOps();
121 $idsForLog = [];
122 $authorIds = $authorIPs = [];
123
124 if ( $perItemStatus ) {
125 $status->itemStatuses = [];
126 }
127
128 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
129 for ( $this->reset(); $this->current(); $this->next() ) {
130 // @codingStandardsIgnoreEnd
131 /** @var $item RevDelItem */
132 $item = $this->current();
133 unset( $missing[$item->getId()] );
134
135 if ( $perItemStatus ) {
136 $itemStatus = Status::newGood();
137 $status->itemStatuses[$item->getId()] = $itemStatus;
138 } else {
139 $itemStatus = $status;
140 }
141
142 $oldBits = $item->getBits();
143 // Build the actual new rev_deleted bitfield
144 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
145
146 if ( $oldBits == $newBits ) {
147 $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
148 $status->failCount++;
149 continue;
150 } elseif ( $oldBits == 0 && $newBits != 0 ) {
151 $opType = 'hide';
152 } elseif ( $oldBits != 0 && $newBits == 0 ) {
153 $opType = 'show';
154 } else {
155 $opType = 'modify';
156 }
157
158 if ( $item->isHideCurrentOp( $newBits ) ) {
159 // Cannot hide current version text
160 $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
161 $status->failCount++;
162 continue;
163 }
164 if ( !$item->canView() ) {
165 // Cannot access this revision
166 $msg = ( $opType == 'show' ) ?
167 'revdelete-show-no-access' : 'revdelete-modify-no-access';
168 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
169 $status->failCount++;
170 continue;
171 }
172 // Cannot just "hide from Sysops" without hiding any fields
173 if ( $newBits == Revision::DELETED_RESTRICTED ) {
174 $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
175 $status->failCount++;
176 continue;
177 }
178
179 // Update the revision
180 $ok = $item->setBits( $newBits );
181
182 if ( $ok ) {
183 $idsForLog[] = $item->getId();
184 $status->successCount++;
185 if ( $item->getAuthorId() > 0 ) {
186 $authorIds[] = $item->getAuthorId();
187 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
188 $authorIPs[] = $item->getAuthorName();
189 }
190 } else {
191 $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
192 $status->failCount++;
193 }
194 }
195
196 // Handle missing revisions
197 foreach ( $missing as $id => $unused ) {
198 if ( $perItemStatus ) {
199 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
200 } else {
201 $status->error( 'revdelete-modify-missing', $id );
202 }
203 $status->failCount++;
204 }
205
206 if ( $status->successCount == 0 ) {
207 $dbw->rollback( __METHOD__ );
208 return $status;
209 }
210
211 // Save success count
212 $successCount = $status->successCount;
213
214 // Move files, if there are any
215 $status->merge( $this->doPreCommitUpdates() );
216 if ( !$status->isOK() ) {
217 // Fatal error, such as no configured archive directory
218 $dbw->rollback( __METHOD__ );
219 return $status;
220 }
221
222 // Log it
223 // @FIXME: $newBits/$oldBits set in for loop, makes IDE warnings too
224 $this->updateLog( [
225 'title' => $this->title,
226 'count' => $successCount,
227 'newBits' => $newBits,
228 'oldBits' => $oldBits,
229 'comment' => $comment,
230 'ids' => $idsForLog,
231 'authorIds' => $authorIds,
232 'authorIPs' => $authorIPs
233 ] );
234
235 // Clear caches
236 $that = $this;
237 $dbw->onTransactionIdle( function() use ( $that ) {
238 $that->doPostCommitUpdates();
239 } );
240
241 $dbw->endAtomic( __METHOD__ );
242
243 return $status;
244 }
245
246 /**
247 * Reload the list data from the master DB. This can be done after setVisibility()
248 * to allow $item->getHTML() to show the new data.
249 */
250 function reloadFromMaster() {
251 $dbw = wfGetDB( DB_MASTER );
252 $this->res = $this->doQuery( $dbw );
253 }
254
255 /**
256 * Record a log entry on the action
257 * @param array $params Associative array of parameters:
258 * newBits: The new value of the *_deleted bitfield
259 * oldBits: The old value of the *_deleted bitfield.
260 * title: The target title
261 * ids: The ID list
262 * comment: The log comment
263 * authorsIds: The array of the user IDs of the offenders
264 * authorsIPs: The array of the IP/anon user offenders
265 * @throws MWException
266 */
267 protected function updateLog( $params ) {
268 // Get the URL param's corresponding DB field
269 $field = RevisionDeleter::getRelationType( $this->getType() );
270 if ( !$field ) {
271 throw new MWException( "Bad log URL param type!" );
272 }
273 // Put things hidden from sysops in the suppression log
274 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
275 $logType = 'suppress';
276 } else {
277 $logType = 'delete';
278 }
279 // Add params for affected page and ids
280 $logParams = $this->getLogParams( $params );
281 // Actually add the deletion log entry
282 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
283 $logEntry->setTarget( $params['title'] );
284 $logEntry->setComment( $params['comment'] );
285 $logEntry->setParameters( $logParams );
286 $logEntry->setPerformer( $this->getUser() );
287 // Allow for easy searching of deletion log items for revision/log items
288 $logEntry->setRelations( [
289 $field => $params['ids'],
290 'target_author_id' => $params['authorIds'],
291 'target_author_ip' => $params['authorIPs'],
292 ] );
293 $logId = $logEntry->insert();
294 $logEntry->publish( $logId );
295 }
296
297 /**
298 * Get the log action for this list type
299 * @return string
300 */
301 public function getLogAction() {
302 return 'revision';
303 }
304
305 /**
306 * Get log parameter array.
307 * @param array $params Associative array of log parameters, same as updateLog()
308 * @return array
309 */
310 public function getLogParams( $params ) {
311 return [
312 '4::type' => $this->getType(),
313 '5::ids' => $params['ids'],
314 '6::ofield' => $params['oldBits'],
315 '7::nfield' => $params['newBits'],
316 ];
317 }
318
319 /**
320 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
321 * STUB
322 */
323 public function clearFileOps() {
324 }
325
326 /**
327 * A hook for setVisibility(): do batch updates pre-commit.
328 * STUB
329 * @return Status
330 */
331 public function doPreCommitUpdates() {
332 return Status::newGood();
333 }
334
335 /**
336 * A hook for setVisibility(): do any necessary updates post-commit.
337 * STUB
338 * @return Status
339 */
340 public function doPostCommitUpdates() {
341 return Status::newGood();
342 }
343
344 /**
345 * Get the integer value of the flag used for suppression
346 */
347 abstract public function getSuppressBit();
348 }