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