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