Merge "Use local context to get messages and time formatting methods of Language...
[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 $params array 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 $params array 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 */
188 protected function updateLog( $params ) {
189 // Get the URL param's corresponding DB field
190 $field = RevisionDeleter::getRelationType( $this->getType() );
191 if( !$field ) {
192 throw new MWException( "Bad log URL param type!" );
193 }
194 // Put things hidden from sysops in the oversight log
195 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
196 $logType = 'suppress';
197 } else {
198 $logType = 'delete';
199 }
200 // Add params for effected page and ids
201 $logParams = $this->getLogParams( $params );
202 // Actually add the deletion log entry
203 $log = new LogPage( $logType );
204 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
205 $params['comment'], $logParams );
206 // Allow for easy searching of deletion log items for revision/log items
207 $log->addRelations( $field, $params['ids'], $logid );
208 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
209 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
210 }
211
212 /**
213 * Get the log action for this list type
214 * @return string
215 */
216 public function getLogAction() {
217 return 'revision';
218 }
219
220 /**
221 * Get log parameter array.
222 * @param $params array Associative array of log parameters, same as updateLog()
223 * @return array
224 */
225 public function getLogParams( $params ) {
226 return array(
227 $this->getType(),
228 implode( ',', $params['ids'] ),
229 "ofield={$params['oldBits']}",
230 "nfield={$params['newBits']}"
231 );
232 }
233
234 /**
235 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
236 * STUB
237 */
238 public function clearFileOps() {
239 }
240
241 /**
242 * A hook for setVisibility(): do batch updates pre-commit.
243 * STUB
244 * @return Status
245 */
246 public function doPreCommitUpdates() {
247 return Status::newGood();
248 }
249
250 /**
251 * A hook for setVisibility(): do any necessary updates post-commit.
252 * STUB
253 * @return Status
254 */
255 public function doPostCommitUpdates() {
256 return Status::newGood();
257 }
258
259 /**
260 * Get the integer value of the flag used for suppression
261 */
262 abstract public function getSuppressBit();
263 }
264
265 /**
266 * Abstract base class for deletable items
267 */
268 abstract class RevDel_Item extends RevisionItemBase {
269 /**
270 * Returns true if the item is "current", and the operation to set the given
271 * bits can't be executed for that reason
272 * STUB
273 * @return bool
274 */
275 public function isHideCurrentOp( $newBits ) {
276 return false;
277 }
278
279 /**
280 * Get the current deletion bitfield value
281 */
282 abstract public function getBits();
283
284 /**
285 * Set the visibility of the item. This should do any necessary DB queries.
286 *
287 * The DB update query should have a condition which forces it to only update
288 * if the value in the DB matches the value fetched earlier with the SELECT.
289 * If the update fails because it did not match, the function should return
290 * false. This prevents concurrency problems.
291 *
292 * @return boolean success
293 */
294 abstract public function setBits( $newBits );
295 }