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