544c6240250db0195c7af823dee6b21de9544996
[lhc/web/wiklou.git] / includes / SpecialRevisiondelete.php
1 <?php
2
3 /**
4 * Not quite ready for production use yet; need to fix up the restricted mode,
5 * and provide for preservation across delete/undelete of the page.
6 *
7 * To try this out, set up extra permissions something like:
8 * $wgGroupPermissions['sysop']['deleterevision'] = true;
9 * $wgGroupPermissions['bureaucrat']['hiderevision'] = true;
10 */
11
12 function wfSpecialRevisiondelete( $par = null ) {
13 global $wgOut, $wgRequest;
14
15 $target = $wgRequest->getVal( 'target' );
16 $oldid = $wgRequest->getIntArray( 'oldid' );
17
18 $page = Title::newFromUrl( $target );
19
20 if( is_null( $page ) ) {
21 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
22 return;
23 }
24
25 if( is_null( $oldid ) ) {
26 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
27 return;
28 }
29
30 $form = new RevisionDeleteForm( $wgRequest );
31 if( $wgRequest->wasPosted() ) {
32 $form->submit( $wgRequest );
33 } else {
34 $form->show( $wgRequest );
35 }
36 }
37
38 class RevisionDeleteForm {
39 /**
40 * @param Title $page
41 * @param int $oldid
42 */
43 function __construct( $request ) {
44 global $wgUser;
45
46 $target = $request->getVal( 'target' );
47 $this->page = Title::newFromUrl( $target );
48
49 $this->revisions = $request->getIntArray( 'oldid', array() );
50
51 $this->checks = array(
52 array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT ),
53 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
54 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER ),
55 array( 'revdelete-hide-restricted', 'wpHideRestricted', Revision::DELETED_RESTRICTED ) );
56 }
57
58 /**
59 * @param WebRequest $request
60 */
61 function show( $request ) {
62 global $wgOut, $wgUser;
63
64 $wgOut->addWikiText( wfMsg( 'revdelete-selected', $this->page->getPrefixedText() ) );
65
66 $wgOut->addHtml( "<ul>" );
67 foreach( $this->revisions as $revid ) {
68 $rev = Revision::newFromTitle( $this->page, $revid );
69 if( !isset( $rev ) ) {
70 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
71 return;
72 }
73 $wgOut->addHtml( $this->historyLine( $rev ) );
74 $bitfields[] = $rev->mDeleted; // FIXME
75 }
76 $wgOut->addHtml( "</ul>" );
77
78 $wgOut->addWikiText( wfMsg( 'revdelete-text' ) );
79
80 $items = array(
81 wfInputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
82 wfSubmitButton( wfMsg( 'revdelete-submit' ) ) );
83 $hidden = array(
84 wfHidden( 'wpEditToken', $wgUser->editToken() ),
85 wfHidden( 'target', $this->page->getPrefixedText() ) );
86 foreach( $this->revisions as $revid ) {
87 $hidden[] = wfHidden( 'oldid[]', $revid );
88 }
89
90 $special = SpecialPage::getTitleFor( 'Revisiondelete' );
91 $wgOut->addHtml( wfElement( 'form', array(
92 'method' => 'post',
93 'action' => $special->getLocalUrl( 'action=submit' ) ),
94 null ) );
95
96 $wgOut->addHtml( '<fieldset><legend>' . wfMsgHtml( 'revdelete-legend' ) . '</legend>' );
97 foreach( $this->checks as $item ) {
98 list( $message, $name, $field ) = $item;
99 $wgOut->addHtml( '<div>' .
100 wfCheckLabel( wfMsg( $message), $name, $name, $rev->isDeleted( $field ) ) .
101 '</div>' );
102 }
103 $wgOut->addHtml( '</fieldset>' );
104 foreach( $items as $item ) {
105 $wgOut->addHtml( '<p>' . $item . '</p>' );
106 }
107 foreach( $hidden as $item ) {
108 $wgOut->addHtml( $item );
109 }
110
111 $wgOut->addHtml( '</form>' );
112 }
113
114 /**
115 * @param Revision $rev
116 * @returns string
117 */
118 function historyLine( $rev ) {
119 global $wgContLang;
120 $date = $wgContLang->timeanddate( $rev->getTimestamp() );
121 return
122 "<li>" .
123 Linker::makeLinkObj( $this->page, $date, 'oldid=' . $rev->getId() ) .
124 " " .
125 Linker::revUserLink( $rev ) .
126 " " .
127 Linker::revComment( $rev ) .
128 "</li>";
129 }
130
131 /**
132 * @param WebRequest $request
133 */
134 function submit( $request ) {
135 $bitfield = $this->extractBitfield( $request );
136 $comment = $request->getText( 'wpReason' );
137 if( $this->save( $bitfield, $comment ) ) {
138 return $this->success( $request );
139 } else {
140 return $this->show( $request );
141 }
142 }
143
144 function success( $request ) {
145 global $wgOut;
146 $wgOut->addWikiText( 'woo' );
147 }
148
149 /**
150 * Put together a rev_deleted bitfield from the submitted checkboxes
151 * @param WebRequest $request
152 * @return int
153 */
154 function extractBitfield( $request ) {
155 $bitfield = 0;
156 foreach( $this->checks as $item ) {
157 list( $message, $name, $field ) = $item;
158 if( $request->getCheck( $name ) ) {
159 $bitfield |= $field;
160 }
161 }
162 return $bitfield;
163 }
164
165 function save( $bitfield, $reason ) {
166 $dbw = wfGetDB( DB_MASTER );
167 $deleter = new RevisionDeleter( $dbw );
168 $ok = $deleter->setVisibility( $this->revisions, $bitfield, $reason );
169 }
170 }
171
172
173 class RevisionDeleter {
174 function __construct( $db ) {
175 $this->db = $db;
176 }
177
178 /**
179 * @param array $items list of revision ID numbers
180 * @param int $bitfield new rev_deleted value
181 * @param string $comment Comment for log records
182 */
183 function setVisibility( $items, $bitfield, $comment ) {
184 $pages = array();
185
186 // To work!
187 foreach( $items as $revid ) {
188 $rev = Revision::newFromId( $revid );
189 if( !isset( $rev ) ) {
190 return false;
191 }
192 $this->updateRevision( $rev, $bitfield );
193 $this->updateRecentChanges( $rev, $bitfield );
194
195 // For logging, maintain a count of revisions per page
196 $pageid = $rev->getPage();
197 if( isset( $pages[$pageid] ) ) {
198 $pages[$pageid]++;
199 } else {
200 $pages[$pageid] = 1;
201 }
202 }
203
204 // Clear caches...
205 foreach( $pages as $pageid => $count ) {
206 $title = Title::newFromId( $pageid );
207 $this->updatePage( $title );
208 $this->updateLog( $title, $count, $bitfield, $comment );
209 }
210
211 return true;
212 }
213
214 /**
215 * Update the revision's rev_deleted field
216 * @param Revision $rev
217 * @param int $bitfield new rev_deleted bitfield value
218 */
219 function updateRevision( $rev, $bitfield ) {
220 $this->db->update( 'revision',
221 array( 'rev_deleted' => $bitfield ),
222 array( 'rev_id' => $rev->getId() ),
223 'RevisionDeleter::updateRevision' );
224 }
225
226 /**
227 * Update the revision's recentchanges record if fields have been hidden
228 * @param Revision $rev
229 * @param int $bitfield new rev_deleted bitfield value
230 */
231 function updateRecentChanges( $rev, $bitfield ) {
232 $this->db->update( 'recentchanges',
233 array(
234 'rc_user' => ($bitfield & Revision::DELETED_USER) ? 0 : $rev->getUser(),
235 'rc_user_text' => ($bitfield & Revision::DELETED_USER) ? wfMsg( 'rev-deleted-user' ) : $rev->getUserText(),
236 'rc_comment' => ($bitfield & Revision::DELETED_COMMENT) ? wfMsg( 'rev-deleted-comment' ) : $rev->getComment() ),
237 array(
238 'rc_this_oldid' => $rev->getId() ),
239 'RevisionDeleter::updateRecentChanges' );
240 }
241
242 /**
243 * Touch the page's cache invalidation timestamp; this forces cached
244 * history views to refresh, so any newly hidden or shown fields will
245 * update properly.
246 * @param Title $title
247 */
248 function updatePage( $title ) {
249 $title->invalidateCache();
250 }
251
252 /**
253 * Record a log entry on the action
254 * @param Title $title
255 * @param int $count the number of revisions altered for this page
256 * @param int $bitfield the new rev_deleted value
257 * @param string $comment
258 */
259 function updateLog( $title, $count, $bitfield, $comment ) {
260 $log = new LogPage( 'delete' );
261 $reason = "changed $count revisions to $bitfield";
262 $reason .= ": $comment";
263 $log->addEntry( 'revision', $title, $reason );
264 }
265 }
266
267 ?>