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