Move all watchlist editing into WatchlistEditor class, integrating "raw editing"...
[lhc/web/wiklou.git] / includes / WatchlistEditor.php
1 <?php
2
3 /**
4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
6 *
7 * @addtogroup Watchlist
8 * @author Rob Church <robchur@gmail.com>
9 */
10 class WatchlistEditor {
11
12 /**
13 * Editing modes
14 */
15 const EDIT_CLEAR = 1;
16 const EDIT_RAW = 2;
17 const EDIT_NORMAL = 3;
18
19 /**
20 * Main execution point
21 *
22 * @param User $user
23 * @param OutputPage $output
24 * @param WebRequest $request
25 * @param int $mode
26 */
27 public function execute( $user, $output, $request, $mode ) {
28 if( wfReadOnly() ) {
29 $output->readOnlyPage();
30 return;
31 }
32 switch( $mode ) {
33 case self::EDIT_CLEAR:
34 $output->setPageTitle( wfMsg( 'watchlistedit-clear-title' ) );
35 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
36 $this->clearWatchlist( $user );
37 $user->invalidateCache();
38 $output->addHtml( wfMsgExt( 'watchlistedit-clear-done', 'parse' ) );
39 } else {
40 $this->showClearForm( $output, $user );
41 }
42 break;
43 case self::EDIT_RAW:
44 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
45 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
46 $titles = $this->extractTitles( $request->getText( 'titles' ) );
47 $this->clearWatchlist( $user );
48 $this->watchTitles( $titles, $user );
49 $user->invalidateCache();
50 $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
51 }
52 $this->showRawForm( $output, $user );
53 break;
54 case self::EDIT_NORMAL:
55 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
56 if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
57 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
58 $this->unwatchTitles( $titles, $user );
59 $user->invalidateCache();
60 $output->addHtml( wfMsgExt( 'watchlistedit-normal-done', 'parse',
61 $GLOBALS['wgLang']->formatNum( count( $titles ) ) ) );
62 $this->showTitles( $titles, $output, $user->getSkin() );
63 }
64 $this->showNormalForm( $output, $user );
65 }
66 }
67
68 /**
69 * Check the edit token from a form submission
70 *
71 * @param WebRequest $request
72 * @param User $user
73 * @return bool
74 */
75 private function checkToken( $request, $user ) {
76 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
77 }
78
79 /**
80 * Extract a list of titles from a text list; if we're given
81 * an array, convert each item into a Title
82 *
83 * @param mixed $list
84 * @return array
85 */
86 private function extractTitles( $list ) {
87 if( !is_array( $list ) ) {
88 $list = explode( "\n", $list );
89 if( !is_array( $list ) )
90 return array();
91 }
92 for( $i = 0; $i < count( $list ); $i++ ) {
93 $list[$i] = Title::newFromText( $list[$i] );
94 if( !$list[$i] instanceof Title )
95 unset( $list[$i] );
96 }
97 return $list;
98 }
99
100 /**
101 * Print out a list of linked titles
102 *
103 * @param array $titles
104 * @param OutputPage $output
105 * @param Skin $skin
106 */
107 private function showTitles( $titles, $output, $skin ) {
108 $talk = htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) );
109 // Do a batch existence check
110 $batch = new LinkBatch();
111 foreach( $titles as $title ) {
112 $batch->addObj( $title );
113 $batch->addObj( $title->getTalkPage() );
114 }
115 $batch->execute();
116 // Print out the list
117 $output->addHtml( "<ul>\n" );
118 foreach( $titles as $title )
119 $output->addHtml( "<li>" . $skin->makeLinkObj( $title )
120 . ' (' . $skin->makeLinkObj( $title->getTalkPage(), $talk ) . ")</li>\n" );
121 $output->addHtml( "</ul>\n" );
122 }
123
124 /**
125 * Count the number of titles on a user's watchlist, excluding talk pages
126 *
127 * @param User $user
128 * @return int
129 */
130 private function countWatchlist( $user ) {
131 $dbr = wfGetDB( DB_SLAVE );
132 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
133 $row = $dbr->fetchObject( $res );
134 return ceil( $row->count / 2 ); // Paranoia
135 }
136
137 /**
138 * Get a list of titles on a user's watchlist, excluding talk pages,
139 * and return as a two-dimensional array with namespace, title and
140 * redirect status
141 *
142 * @param User $user
143 * @return array
144 */
145 private function getWatchlist( $user ) {
146 $titles = array();
147 $dbr = wfGetDB( DB_SLAVE );
148 $uid = intval( $user->getId() );
149 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
150 $sql = "SELECT wl_namespace, wl_title, page_id, page_is_redirect
151 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
152 AND wl_title = page_title ) WHERE wl_user = {$uid}";
153 $res = $dbr->query( $sql, __METHOD__ );
154 if( $res && $dbr->numRows( $res ) > 0 ) {
155 $cache = LinkCache::singleton();
156 while( $row = $dbr->fetchObject( $res ) ) {
157 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
158 if( $title instanceof Title ) {
159 // Update the link cache while we're at it
160 if( $row->page_id ) {
161 $cache->addGoodLinkObj( $row->page_id, $title );
162 } else {
163 $cache->addBadLinkObj( $title );
164 }
165 // Ignore non-talk
166 if( !$title->isTalkPage() )
167 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
168 }
169 }
170 }
171 return $titles;
172 }
173
174 /**
175 * Show a message indicating the number of items on the user's watchlist,
176 * and return this count for additional checking
177 *
178 * @param OutputPage $output
179 * @param User $user
180 * @return int
181 */
182 private function showItemCount( $output, $user ) {
183 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
184 $output->addHtml( wfMsgExt( 'watchlistedit-numitems', 'parse',
185 $GLOBALS['wgLang']->formatNum( $count ) ) );
186 } else {
187 $output->addHtml( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
188 }
189 return $count;
190 }
191
192 /**
193 * Remove all titles from a user's watchlist
194 *
195 * @param User $user
196 */
197 private function clearWatchlist( $user ) {
198 $dbw = wfGetDB( DB_MASTER );
199 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
200 }
201
202 /**
203 * Add a list of titles to a user's watchlist
204 *
205 * @param array $titles
206 * @param User $user
207 */
208 private function watchTitles( $titles, $user ) {
209 $dbw = wfGetDB( DB_MASTER );
210 $rows = array();
211 foreach( $titles as $title ) {
212 $rows[] = array(
213 'wl_user' => $user->getId(),
214 'wl_namespace' => ( $title->getNamespace() & ~1 ),
215 'wl_title' => $title->getDBkey(),
216 'wl_notificationtimestamp' => null,
217 );
218 $rows[] = array(
219 'wl_user' => $user->getId(),
220 'wl_namespace' => ( $title->getNamespace() | 1 ),
221 'wl_title' => $title->getDBkey(),
222 'wl_notificationtimestamp' => null,
223 );
224 }
225 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
226 }
227
228 /**
229 * Remove a list of titles from a user's watchlist
230 *
231 * @param array $titles
232 * @param User $user
233 */
234 private function unwatchTitles( $titles, $user ) {
235 $dbw = wfGetDB( DB_MASTER );
236 foreach( $titles as $title ) {
237 $dbw->delete(
238 'watchlist',
239 array(
240 'wl_user' => $user->getId(),
241 'wl_namespace' => ( $title->getNamespace() & ~1 ),
242 'wl_title' => $title->getDBkey(),
243 ),
244 __METHOD__
245 );
246 $dbw->delete(
247 'watchlist',
248 array(
249 'wl_user' => $user->getId(),
250 'wl_namespace' => ( $title->getNamespace() | 1 ),
251 'wl_title' => $title->getDBkey(),
252 ),
253 __METHOD__
254 );
255 }
256 }
257
258 /**
259 * Show a confirmation form for users wishing to clear their watchlist
260 *
261 * @param OutputPage $output
262 * @param User $user
263 */
264 private function showClearForm( $output, $user ) {
265 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
266 $self = SpecialPage::getTitleFor( 'Watchlist' );
267 $form = Xml::openElement( 'form', array( 'method' => 'post',
268 'action' => $self->getLocalUrl( 'action=clear' ) ) );
269 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
270 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-clear-legend' ) . '</legend>';
271 $form .= wfMsgExt( 'watchlistedit-clear-confirm', 'parse' );
272 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-clear-submit' ) ) . '</p>';
273 $form .= '</fieldset></form>';
274 $output->addHtml( $form );
275 }
276 }
277
278 /**
279 * Show the standard watchlist editing form
280 *
281 * @param OutputPage $output
282 * @param User $user
283 */
284 private function showNormalForm( $output, $user ) {
285 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
286 $self = SpecialPage::getTitleFor( 'Watchlist' );
287 $form = Xml::openElement( 'form', array( 'method' => 'post',
288 'action' => $self->getLocalUrl( 'action=edit' ) ) );
289 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
290 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
291 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
292 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
293 $form .= '<h2>' . $this->getNamespaceHeading( $namespace ) . '</h2>';
294 $form .= '<ul>';
295 foreach( $pages as $dbkey => $redirect ) {
296 $title = Title::makeTitleSafe( $namespace, $dbkey );
297 $form .= $this->buildRemoveLine( $title, $redirect, $user->getSkin() );
298 }
299 $form .= '</ul>';
300 }
301 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
302 $form .= '</fieldset></form>';
303 $output->addHtml( $form );
304 }
305 }
306
307 /**
308 * Get the correct "heading" for a namespace
309 *
310 * @param int $namespace
311 * @return string
312 */
313 private function getNamespaceHeading( $namespace ) {
314 return $namespace == NS_MAIN
315 ? wfMsgHtml( 'blanknamespace' )
316 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
317 }
318
319 /**
320 * Build a single list item containing a check box selecting a title
321 * and a link to that title, with various additional bits
322 *
323 * @param Title $title
324 * @param bool $redirect
325 * @param Skin $skin
326 * @return string
327 */
328 private function buildRemoveLine( $title, $redirect, $skin ) {
329 $link = $skin->makeLinkObj( $title );
330 if( $redirect )
331 $link = '<span class="watchlistredir">' . $link . '</span>';
332 $tools[] = $skin->makeLinkObj( $title->getTalkPage(),
333 htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) ) );
334 if( $title->exists() )
335 $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
336 return '<li>'
337 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
338 . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
339 }
340
341 /**
342 * Show a form for editing the watchlist in "raw" mode
343 *
344 * @param OutputPage $output
345 * @param User $user
346 */
347 public function showRawForm( $output, $user ) {
348 $this->showItemCount( $output, $user );
349 $self = SpecialPage::getTitleFor( 'Watchlist' );
350 $form = Xml::openElement( 'form', array( 'method' => 'post',
351 'action' => $self->getLocalUrl( 'action=raw' ) ) );
352 $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
353 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
354 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
355 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
356 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
357 'rows' => 6, 'cols' => 80 ) );
358 foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
359 foreach( $pages as $dbkey => $redirect ) {
360 $title = Title::makeTitleSafe( $namespace, $dbkey );
361 if( $title instanceof Title )
362 $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
363 }
364 }
365 $form .= '</textarea>';
366 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
367 $form .= '</fieldset></form>';
368 $output->addHtml( $form );
369 }
370
371 /**
372 * Determine whether we are editing the watchlist, and if so, what
373 * kind of editing operation
374 *
375 * @param WebRequest $request
376 * @param mixed $par
377 * @return int
378 */
379 public static function getMode( $request, $par ) {
380 $mode = strtolower( $request->getVal( 'action', $par ) );
381 switch( $mode ) {
382 case 'clear':
383 return self::EDIT_CLEAR;
384 case 'raw':
385 return self::EDIT_RAW;
386 case 'edit':
387 return self::EDIT_NORMAL;
388 default:
389 return false;
390 }
391 }
392
393 }