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.
7 * To try this out, set up extra permissions something like:
8 * $wgGroupPermissions['sysop']['deleterevision'] = true;
9 * $wgGroupPermissions['bureaucrat']['hiderevision'] = true;
12 function wfSpecialRevisiondelete( $par = null ) {
13 global $wgOut, $wgRequest;
15 $target = $wgRequest->getVal( 'target' );
16 $oldid = $wgRequest->getIntArray( 'oldid' );
18 $page = Title
::newFromUrl( $target );
20 if( is_null( $page ) ) {
21 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
25 if( is_null( $oldid ) ) {
26 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
30 $form = new RevisionDeleteForm( $wgRequest );
31 if( $wgRequest->wasPosted() ) {
32 $form->submit( $wgRequest );
34 $form->show( $wgRequest );
39 * Implements the GUI for Revision Deletion.
40 * @addtogroup SpecialPage
42 class RevisionDeleteForm
{
44 * @param WebRequest $request
46 function __construct( $request ) {
49 $target = $request->getVal( 'target' );
50 $this->page
= Title
::newFromUrl( $target );
52 $this->revisions
= $request->getIntArray( 'oldid', array() );
54 $this->skin
= $wgUser->getSkin();
55 $this->checks
= array(
56 array( 'revdelete-hide-text', 'wpHideText', Revision
::DELETED_TEXT
),
57 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
58 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
),
59 array( 'revdelete-hide-restricted', 'wpHideRestricted', Revision
::DELETED_RESTRICTED
) );
63 * @param WebRequest $request
65 function show( $request ) {
66 global $wgOut, $wgUser;
68 $wgOut->addWikiMsg( 'revdelete-selected', $this->page
->getPrefixedText() );
70 $wgOut->addHtml( "<ul>" );
71 foreach( $this->revisions
as $revid ) {
72 $rev = Revision
::newFromTitle( $this->page
, $revid );
73 if( !isset( $rev ) ) {
74 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
77 $wgOut->addHtml( $this->historyLine( $rev ) );
78 $bitfields[] = $rev->mDeleted
; // FIXME
80 $wgOut->addHtml( "</ul>" );
82 $wgOut->addWikiMsg( 'revdelete-text' );
85 Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
86 Xml
::submitButton( wfMsg( 'revdelete-submit' ) ) );
88 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ),
89 Xml
::hidden( 'target', $this->page
->getPrefixedText() ) );
90 foreach( $this->revisions
as $revid ) {
91 $hidden[] = Xml
::hidden( 'oldid[]', $revid );
94 $special = SpecialPage
::getTitleFor( 'Revisiondelete' );
95 $wgOut->addHtml( Xml
::element( 'form', array(
97 'action' => $special->getLocalUrl( 'action=submit' ) ),
100 $wgOut->addHtml( '<fieldset><legend>' . wfMsgHtml( 'revdelete-legend' ) . '</legend>' );
101 foreach( $this->checks
as $item ) {
102 list( $message, $name, $field ) = $item;
103 $wgOut->addHtml( '<div>' .
104 Xml
::checkLabel( wfMsg( $message), $name, $name, $rev->isDeleted( $field ) ) .
107 $wgOut->addHtml( '</fieldset>' );
108 foreach( $items as $item ) {
109 $wgOut->addHtml( '<p>' . $item . '</p>' );
111 foreach( $hidden as $item ) {
112 $wgOut->addHtml( $item );
115 $wgOut->addHtml( '</form>' );
119 * @param Revision $rev
122 function historyLine( $rev ) {
124 $date = $wgContLang->timeanddate( $rev->getTimestamp() );
127 $this->skin
->makeLinkObj( $this->page
, $date, 'oldid=' . $rev->getId() ) .
129 $this->skin
->revUserLink( $rev ) .
131 $this->skin
->revComment( $rev ) .
136 * @param WebRequest $request
138 function submit( $request ) {
139 $bitfield = $this->extractBitfield( $request );
140 $comment = $request->getText( 'wpReason' );
141 if( $this->save( $bitfield, $comment ) ) {
142 return $this->success( $request );
144 return $this->show( $request );
148 function success( $request ) {
150 $wgOut->addWikiText( 'woo' );
154 * Put together a rev_deleted bitfield from the submitted checkboxes
155 * @param WebRequest $request
158 function extractBitfield( $request ) {
160 foreach( $this->checks
as $item ) {
161 list( /* message */ , $name, $field ) = $item;
162 if( $request->getCheck( $name ) ) {
169 function save( $bitfield, $reason ) {
170 $dbw = wfGetDB( DB_MASTER
);
171 $deleter = new RevisionDeleter( $dbw );
172 $deleter->setVisibility( $this->revisions
, $bitfield, $reason );
177 * Implements the actions for Revision Deletion.
178 * @addtogroup SpecialPage
180 class RevisionDeleter
{
181 function __construct( $db ) {
186 * @param array $items list of revision ID numbers
187 * @param int $bitfield new rev_deleted value
188 * @param string $comment Comment for log records
190 function setVisibility( $items, $bitfield, $comment ) {
194 foreach( $items as $revid ) {
195 $rev = Revision
::newFromId( $revid );
196 if( !isset( $rev ) ) {
199 $this->updateRevision( $rev, $bitfield );
200 $this->updateRecentChanges( $rev, $bitfield );
202 // For logging, maintain a count of revisions per page
203 $pageid = $rev->getPage();
204 if( isset( $pages[$pageid] ) ) {
212 foreach( $pages as $pageid => $count ) {
213 $title = Title
::newFromId( $pageid );
214 $this->updatePage( $title );
215 $this->updateLog( $title, $count, $bitfield, $comment );
222 * Update the revision's rev_deleted field
223 * @param Revision $rev
224 * @param int $bitfield new rev_deleted bitfield value
226 function updateRevision( $rev, $bitfield ) {
227 $this->db
->update( 'revision',
228 array( 'rev_deleted' => $bitfield ),
229 array( 'rev_id' => $rev->getId() ),
234 * Update the revision's recentchanges record if fields have been hidden
235 * @param Revision $rev
236 * @param int $bitfield new rev_deleted bitfield value
238 function updateRecentChanges( $rev, $bitfield ) {
239 $this->db
->update( 'recentchanges',
241 'rc_user' => ($bitfield & Revision
::DELETED_USER
) ?
0 : $rev->getUser(),
242 'rc_user_text' => ($bitfield & Revision
::DELETED_USER
) ?
wfMsg( 'rev-deleted-user' ) : $rev->getUserText(),
243 'rc_comment' => ($bitfield & Revision
::DELETED_COMMENT
) ?
wfMsg( 'rev-deleted-comment' ) : $rev->getComment() ),
245 'rc_this_oldid' => $rev->getId() ),
250 * Touch the page's cache invalidation timestamp; this forces cached
251 * history views to refresh, so any newly hidden or shown fields will
253 * @param Title $title
255 function updatePage( $title ) {
256 $title->invalidateCache();
260 * Record a log entry on the action
261 * @param Title $title
262 * @param int $count the number of revisions altered for this page
263 * @param int $bitfield the new rev_deleted value
264 * @param string $comment
266 function updateLog( $title, $count, $bitfield, $comment ) {
267 $log = new LogPage( 'delete' );
268 $reason = "changed $count revisions to $bitfield";
269 $reason .= ": $comment";
270 $log->addEntry( 'revision', $title, $reason );