summaryrefslogtreecommitdiff
path: root/includes/SpecialRevisiondelete.php
blob: 5c70d5aed0b11dd9e5c8f4db5fc4967ce56875bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php

/**
 * Not quite ready for production use yet; need to fix up the restricted mode,
 * and provide for preservation across delete/undelete of the page.
 *
 * To try this out, set up extra permissions something like:
 * $wgGroupPermissions['sysop']['deleterevision'] = true;
 * $wgGroupPermissions['bureaucrat']['hiderevision'] = true;
 */

function wfSpecialRevisiondelete( $par = null ) {
	global $wgOut, $wgRequest;
	
	$target = $wgRequest->getVal( 'target' );
	$oldid = $wgRequest->getIntArray( 'oldid' );
	
	$page = Title::newFromUrl( $target );
	
	if( is_null( $page ) ) {
		$wgOut->showErrorPage( 'notargettitle', 'notargettext' );
		return;
	}
	
	if( is_null( $oldid ) ) {
		$wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
		return;
	}
	
	$form = new RevisionDeleteForm( $wgRequest );
	if( $wgRequest->wasPosted() ) {
		$form->submit( $wgRequest );
	} else {
		$form->show( $wgRequest );
	}
}

/**
 * Implements the GUI for Revision Deletion.
 * @addtogroup SpecialPage
 */
class RevisionDeleteForm {
	/**
	 * @param Title $page
	 * @param int $oldid
	 */
	function __construct( $request ) {
		global $wgUser;
		
		$target = $request->getVal( 'target' );
		$this->page = Title::newFromUrl( $target );
		
		$this->revisions = $request->getIntArray( 'oldid', array() );
		
		$this->skin = $wgUser->getSkin();
		$this->checks = array(
			array( 'revdelete-hide-text', 'wpHideText', Revision::DELETED_TEXT ),
			array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
			array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER ),
			array( 'revdelete-hide-restricted', 'wpHideRestricted', Revision::DELETED_RESTRICTED ) );
	}
	
	/**
	 * @param WebRequest $request
	 */
	function show( $request ) {
		global $wgOut, $wgUser;

		$wgOut->addWikiText( wfMsg( 'revdelete-selected', $this->page->getPrefixedText() ) );
		
		$wgOut->addHtml( "<ul>" );
		foreach( $this->revisions as $revid ) {
			$rev = Revision::newFromTitle( $this->page, $revid );
			if( !isset( $rev ) ) {
				$wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
				return;
			}
			$wgOut->addHtml( $this->historyLine( $rev ) );
			$bitfields[] = $rev->mDeleted; // FIXME
		}
		$wgOut->addHtml( "</ul>" );
	
		$wgOut->addWikiText( wfMsg( 'revdelete-text' ) );
		
		$items = array(
			wfInputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ),
			wfSubmitButton( wfMsg( 'revdelete-submit' ) ) );
		$hidden = array(
			wfHidden( 'wpEditToken', $wgUser->editToken() ),
			wfHidden( 'target', $this->page->getPrefixedText() ) );
		foreach( $this->revisions as $revid ) {
			$hidden[] = wfHidden( 'oldid[]', $revid );
		}
		
		$special = SpecialPage::getTitleFor( 'Revisiondelete' );
		$wgOut->addHtml( wfElement( 'form', array(
			'method' => 'post',
			'action' => $special->getLocalUrl( 'action=submit' ) ),
			null ) );
		
		$wgOut->addHtml( '<fieldset><legend>' . wfMsgHtml( 'revdelete-legend' ) . '</legend>' );
		foreach( $this->checks as $item ) {
			list( $message, $name, $field ) = $item;
			$wgOut->addHtml( '<div>' .
				wfCheckLabel( wfMsg( $message), $name, $name, $rev->isDeleted( $field ) ) .
				'</div>' );
		}
		$wgOut->addHtml( '</fieldset>' );
		foreach( $items as $item ) {
			$wgOut->addHtml( '<p>' . $item . '</p>' );
		}
		foreach( $hidden as $item ) {
			$wgOut->addHtml( $item );
		}
		
		$wgOut->addHtml( '</form>' );
	}
	
	/**
	 * @param Revision $rev
	 * @returns string
	 */
	function historyLine( $rev ) {
		global $wgContLang;
		$date = $wgContLang->timeanddate( $rev->getTimestamp() );
		return
			"<li>" .
			$this->skin->makeLinkObj( $this->page, $date, 'oldid=' . $rev->getId() ) .
			" " .
			$this->skin->revUserLink( $rev ) .
			" " .
			$this->skin->revComment( $rev ) .
			"</li>";
	}
	
	/**
	 * @param WebRequest $request
	 */
	function submit( $request ) {
		$bitfield = $this->extractBitfield( $request );
		$comment = $request->getText( 'wpReason' );
		if( $this->save( $bitfield, $comment ) ) {
			return $this->success( $request );
		} else {
			return $this->show( $request );
		}
	}
	
	function success( $request ) {
		global $wgOut;
		$wgOut->addWikiText( 'woo' );
	}
	
	/**
	 * Put together a rev_deleted bitfield from the submitted checkboxes
	 * @param WebRequest $request
	 * @return int
	 */
	function extractBitfield( $request ) {
		$bitfield = 0;
		foreach( $this->checks as $item ) {
			list( /* message */ , $name, $field ) = $item;
			if( $request->getCheck( $name ) ) {
				$bitfield |= $field;
			}
		}
		return $bitfield;
	}
	
	function save( $bitfield, $reason ) {
		$dbw = wfGetDB( DB_MASTER );
		$deleter = new RevisionDeleter( $dbw );
		$deleter->setVisibility( $this->revisions, $bitfield, $reason );
	}
}

/**
 * Implements the actions for Revision Deletion.
 * @addtogroup SpecialPage
 */
class RevisionDeleter {
	function __construct( $db ) {
		$this->db = $db;
	}
	
	/**
	 * @param array $items list of revision ID numbers
	 * @param int $bitfield new rev_deleted value
	 * @param string $comment Comment for log records
	 */
	function setVisibility( $items, $bitfield, $comment ) {
		$pages = array();
		
		// To work!
		foreach( $items as $revid ) {
			$rev = Revision::newFromId( $revid );
			if( !isset( $rev ) ) {
				return false;
			}
			$this->updateRevision( $rev, $bitfield );
			$this->updateRecentChanges( $rev, $bitfield );
			
			// For logging, maintain a count of revisions per page
			$pageid = $rev->getPage();
			if( isset( $pages[$pageid] ) ) {
				$pages[$pageid]++;
			} else {
				$pages[$pageid] = 1;
			}
		}
		
		// Clear caches...
		foreach( $pages as $pageid => $count ) {
			$title = Title::newFromId( $pageid );
			$this->updatePage( $title );
			$this->updateLog( $title, $count, $bitfield, $comment );
		}
		
		return true;
	}
	
	/**
	 * Update the revision's rev_deleted field
	 * @param Revision $rev
	 * @param int $bitfield new rev_deleted bitfield value
	 */
	function updateRevision( $rev, $bitfield ) {
		$this->db->update( 'revision',
			array( 'rev_deleted' => $bitfield ),
			array( 'rev_id' => $rev->getId() ),
			'RevisionDeleter::updateRevision' );
	}
	
	/**
	 * Update the revision's recentchanges record if fields have been hidden
	 * @param Revision $rev
	 * @param int $bitfield new rev_deleted bitfield value
	 */
	function updateRecentChanges( $rev, $bitfield ) {
		$this->db->update( 'recentchanges',
			array(
				'rc_user' => ($bitfield & Revision::DELETED_USER) ? 0 : $rev->getUser(),
				'rc_user_text' => ($bitfield & Revision::DELETED_USER) ? wfMsg( 'rev-deleted-user' ) : $rev->getUserText(),
				'rc_comment' => ($bitfield & Revision::DELETED_COMMENT) ? wfMsg( 'rev-deleted-comment' ) : $rev->getComment() ),
			array(
				'rc_this_oldid' => $rev->getId() ),
			'RevisionDeleter::updateRecentChanges' );
	}
	
	/**
	 * Touch the page's cache invalidation timestamp; this forces cached
	 * history views to refresh, so any newly hidden or shown fields will
	 * update properly.
	 * @param Title $title
	 */
	function updatePage( $title ) {
		$title->invalidateCache();
	}
	
	/**
	 * Record a log entry on the action
	 * @param Title $title
	 * @param int $count the number of revisions altered for this page
	 * @param int $bitfield the new rev_deleted value
	 * @param string $comment
	 */
	function updateLog( $title, $count, $bitfield, $comment ) {
		$log = new LogPage( 'delete' );
		$reason = "changed $count revisions to $bitfield";
		$reason .= ": $comment";
		$log->addEntry( 'revision', $title, $reason );
	}
}

?>