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