Followup r79549, only try and filter by group (or right) if there are some groups...
[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 * @ingroup 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 $output OutputPage
24 * @param $request WebRequest
25 * @param $mode int
26 */
27 public function execute( $user, $output, $request, $mode ) {
28 global $wgUser, $wgLang;
29 if( wfReadOnly() ) {
30 $output->readOnlyPage();
31 return;
32 }
33 switch( $mode ) {
34 case self::EDIT_CLEAR:
35 // The "Clear" link scared people too much.
36 // Pass on to the raw editor, from which it's very easy to clear.
37 case self::EDIT_RAW:
38 $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
39 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
40 $wanted = $this->extractTitles( $request->getText( 'titles' ) );
41 $current = $this->getWatchlist( $user );
42 if( count( $wanted ) > 0 ) {
43 $toWatch = array_diff( $wanted, $current );
44 $toUnwatch = array_diff( $current, $wanted );
45 $this->watchTitles( $toWatch, $user );
46 $this->unwatchTitles( $toUnwatch, $user );
47 $user->invalidateCache();
48 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
49 $output->addHTML( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
50 if( ( $count = count( $toWatch ) ) > 0 ) {
51 $output->addHTML( wfMsgExt( 'watchlistedit-raw-added', 'parse',
52 $wgLang->formatNum( $count ) ) );
53 $this->showTitles( $toWatch, $output, $wgUser->getSkin() );
54 }
55 if( ( $count = count( $toUnwatch ) ) > 0 ) {
56 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse',
57 $wgLang->formatNum( $count ) ) );
58 $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
59 }
60 } else {
61 $this->clearWatchlist( $user );
62 $user->invalidateCache();
63 $output->addHTML( wfMsgExt( 'watchlistedit-raw-removed', 'parse',
64 $wgLang->formatNum( count( $current ) ) ) );
65 $this->showTitles( $current, $output, $wgUser->getSkin() );
66 }
67 }
68 $this->showRawForm( $output, $user );
69 break;
70 case self::EDIT_NORMAL:
71 $output->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
72 if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
73 $titles = $this->extractTitles( $request->getArray( 'titles' ) );
74 $this->unwatchTitles( $titles, $user );
75 $user->invalidateCache();
76 $output->addHTML( wfMsgExt( 'watchlistedit-normal-done', 'parse',
77 $wgLang->formatNum( count( $titles ) ) ) );
78 $this->showTitles( $titles, $output, $wgUser->getSkin() );
79 }
80 $this->showNormalForm( $output, $user );
81 }
82 }
83
84 /**
85 * Check the edit token from a form submission
86 *
87 * @param $request WebRequest
88 * @param $user User
89 * @return bool
90 */
91 private function checkToken( $request, $user ) {
92 return $user->matchEditToken( $request->getVal( 'token' ), 'watchlistedit' );
93 }
94
95 /**
96 * Extract a list of titles from a blob of text, returning
97 * (prefixed) strings; unwatchable titles are ignored
98 *
99 * @param $list mixed
100 * @return array
101 */
102 private function extractTitles( $list ) {
103 $titles = array();
104 if( !is_array( $list ) ) {
105 $list = explode( "\n", trim( $list ) );
106 if( !is_array( $list ) ) {
107 return array();
108 }
109 }
110 foreach( $list as $text ) {
111 $text = trim( $text );
112 if( strlen( $text ) > 0 ) {
113 $title = Title::newFromText( $text );
114 if( $title instanceof Title && $title->isWatchable() ) {
115 $titles[] = $title->getPrefixedText();
116 }
117 }
118 }
119 return array_unique( $titles );
120 }
121
122 /**
123 * Print out a list of linked titles
124 *
125 * $titles can be an array of strings or Title objects; the former
126 * is preferred, since Titles are very memory-heavy
127 *
128 * @param $titles An array of strings, or Title objects
129 * @param $output OutputPage
130 * @param $skin Skin
131 */
132 private function showTitles( $titles, $output, $skin ) {
133 $talk = wfMsgHtml( 'talkpagelinktext' );
134 // Do a batch existence check
135 $batch = new LinkBatch();
136 foreach( $titles as $title ) {
137 if( !$title instanceof Title ) {
138 $title = Title::newFromText( $title );
139 }
140 if( $title instanceof Title ) {
141 $batch->addObj( $title );
142 $batch->addObj( $title->getTalkPage() );
143 }
144 }
145 $batch->execute();
146 // Print out the list
147 $output->addHTML( "<ul>\n" );
148 foreach( $titles as $title ) {
149 if( !$title instanceof Title ) {
150 $title = Title::newFromText( $title );
151 }
152 if( $title instanceof Title ) {
153 $output->addHTML( "<li>" . $skin->link( $title )
154 . ' (' . $skin->link( $title->getTalkPage(), $talk ) . ")</li>\n" );
155 }
156 }
157 $output->addHTML( "</ul>\n" );
158 }
159
160 /**
161 * Count the number of titles on a user's watchlist, excluding talk pages
162 *
163 * @param $user User
164 * @return int
165 */
166 private function countWatchlist( $user ) {
167 $dbr = wfGetDB( DB_MASTER );
168 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
169 $row = $dbr->fetchObject( $res );
170 return ceil( $row->count / 2 ); // Paranoia
171 }
172
173 /**
174 * Prepare a list of titles on a user's watchlist (excluding talk pages)
175 * and return an array of (prefixed) strings
176 *
177 * @param $user User
178 * @return array
179 */
180 private function getWatchlist( $user ) {
181 $list = array();
182 $dbr = wfGetDB( DB_MASTER );
183 $res = $dbr->select(
184 'watchlist',
185 '*',
186 array(
187 'wl_user' => $user->getId(),
188 ),
189 __METHOD__
190 );
191 if( $res->numRows() > 0 ) {
192 foreach ( $res as $row ) {
193 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
194 if( $title instanceof Title && !$title->isTalkPage() )
195 $list[] = $title->getPrefixedText();
196 }
197 $res->free();
198 }
199 return $list;
200 }
201
202 /**
203 * Get a list of titles on a user's watchlist, excluding talk pages,
204 * and return as a two-dimensional array with namespace, title and
205 * redirect status
206 *
207 * @param $user User
208 * @return array
209 */
210 private function getWatchlistInfo( $user ) {
211 $titles = array();
212 $dbr = wfGetDB( DB_MASTER );
213
214 $res = $dbr->select(
215 array( 'watchlist', 'page' ),
216 array(
217 'wl_namespace',
218 'wl_title',
219 'page_id',
220 'page_len',
221 'page_is_redirect',
222 'page_latest'
223 ),
224 array( 'wl_user' => $user->getId() ),
225 __METHOD__,
226 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
227 array( 'page' => array(
228 'LEFT JOIN',
229 'wl_namespace = page_namespace AND wl_title = page_title'
230 ) )
231 );
232
233 if( $res && $dbr->numRows( $res ) > 0 ) {
234 $cache = LinkCache::singleton();
235 foreach ( $res as $row ) {
236 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
237 if( $title instanceof Title ) {
238 // Update the link cache while we're at it
239 if( $row->page_id ) {
240 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
241 } else {
242 $cache->addBadLinkObj( $title );
243 }
244 // Ignore non-talk
245 if( !$title->isTalkPage() ) {
246 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
247 }
248 }
249 }
250 }
251 return $titles;
252 }
253
254 /**
255 * Show a message indicating the number of items on the user's watchlist,
256 * and return this count for additional checking
257 *
258 * @param $output OutputPage
259 * @param $user User
260 * @return int
261 */
262 private function showItemCount( $output, $user ) {
263 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
264 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
265 $GLOBALS['wgLang']->formatNum( $count ) ) );
266 } else {
267 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
268 }
269 return $count;
270 }
271
272 /**
273 * Remove all titles from a user's watchlist
274 *
275 * @param $user User
276 */
277 private function clearWatchlist( $user ) {
278 $dbw = wfGetDB( DB_MASTER );
279 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
280 }
281
282 /**
283 * Add a list of titles to a user's watchlist
284 *
285 * $titles can be an array of strings or Title objects; the former
286 * is preferred, since Titles are very memory-heavy
287 *
288 * @param $titles An array of strings, or Title objects
289 * @param $user User
290 */
291 private function watchTitles( $titles, $user ) {
292 $dbw = wfGetDB( DB_MASTER );
293 $rows = array();
294 foreach( $titles as $title ) {
295 if( !$title instanceof Title ) {
296 $title = Title::newFromText( $title );
297 }
298 if( $title instanceof Title ) {
299 $rows[] = array(
300 'wl_user' => $user->getId(),
301 'wl_namespace' => ( $title->getNamespace() & ~1 ),
302 'wl_title' => $title->getDBkey(),
303 'wl_notificationtimestamp' => null,
304 );
305 $rows[] = array(
306 'wl_user' => $user->getId(),
307 'wl_namespace' => ( $title->getNamespace() | 1 ),
308 'wl_title' => $title->getDBkey(),
309 'wl_notificationtimestamp' => null,
310 );
311 }
312 }
313 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
314 }
315
316 /**
317 * Remove a list of titles from a user's watchlist
318 *
319 * $titles can be an array of strings or Title objects; the former
320 * is preferred, since Titles are very memory-heavy
321 *
322 * @param $titles An array of strings, or Title objects
323 * @param $user User
324 */
325 private function unwatchTitles( $titles, $user ) {
326 $dbw = wfGetDB( DB_MASTER );
327 foreach( $titles as $title ) {
328 if( !$title instanceof Title ) {
329 $title = Title::newFromText( $title );
330 }
331 if( $title instanceof Title ) {
332 $dbw->delete(
333 'watchlist',
334 array(
335 'wl_user' => $user->getId(),
336 'wl_namespace' => ( $title->getNamespace() & ~1 ),
337 'wl_title' => $title->getDBkey(),
338 ),
339 __METHOD__
340 );
341 $dbw->delete(
342 'watchlist',
343 array(
344 'wl_user' => $user->getId(),
345 'wl_namespace' => ( $title->getNamespace() | 1 ),
346 'wl_title' => $title->getDBkey(),
347 ),
348 __METHOD__
349 );
350 $article = new Article($title);
351 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
352 }
353 }
354 }
355
356 /**
357 * Show the standard watchlist editing form
358 *
359 * @param $output OutputPage
360 * @param $user User
361 */
362 private function showNormalForm( $output, $user ) {
363 global $wgUser;
364 $count = $this->showItemCount( $output, $user );
365 if( $count > 0 ) {
366 $self = SpecialPage::getTitleFor( 'Watchlist' );
367 $form = Xml::openElement( 'form', array( 'method' => 'post',
368 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
369 $form .= Html::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
370 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
371 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
372 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
373 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
374 $form .= '</fieldset></form>';
375 $output->addHTML( $form );
376 }
377 }
378
379 /**
380 * Build the part of the standard watchlist editing form with the actual
381 * title selection checkboxes and stuff. Also generates a table of
382 * contents if there's more than one heading.
383 *
384 * @param $user User
385 * @param $skin Skin (really, Linker)
386 */
387 private function buildRemoveList( $user, $skin ) {
388 $list = "";
389 $toc = $skin->tocIndent();
390 $tocLength = 0;
391 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
392 $tocLength++;
393 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
394 $anchor = "editwatchlist-ns" . $namespace;
395
396 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
397 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
398
399 $list .= "<ul>\n";
400 foreach( $pages as $dbkey => $redirect ) {
401 $title = Title::makeTitleSafe( $namespace, $dbkey );
402 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
403 }
404 $list .= "</ul>\n";
405 }
406 // ISSUE: omit the TOC if the total number of titles is low?
407 if( $tocLength > 1 ) {
408 $list = $skin->tocList( $toc ) . $list;
409 }
410 return $list;
411 }
412
413 /**
414 * Get the correct "heading" for a namespace
415 *
416 * @param $namespace int
417 * @return string
418 */
419 private function getNamespaceHeading( $namespace ) {
420 return $namespace == NS_MAIN
421 ? wfMsgHtml( 'blanknamespace' )
422 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
423 }
424
425 /**
426 * Build a single list item containing a check box selecting a title
427 * and a link to that title, with various additional bits
428 *
429 * @param $title Title
430 * @param $redirect bool
431 * @param $skin Skin
432 * @return string
433 */
434 private function buildRemoveLine( $title, $redirect, $skin ) {
435 global $wgLang;
436
437 $link = $skin->link( $title );
438 if( $redirect ) {
439 $link = '<span class="watchlistredir">' . $link . '</span>';
440 }
441 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
442 if( $title->exists() ) {
443 $tools[] = $skin->link(
444 $title,
445 wfMsgHtml( 'history_short' ),
446 array(),
447 array( 'action' => 'history' ),
448 array( 'known', 'noclasses' )
449 );
450 }
451 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
452 $tools[] = $skin->link(
453 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
454 wfMsgHtml( 'contributions' ),
455 array(),
456 array(),
457 array( 'known', 'noclasses' )
458 );
459 }
460
461 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
462
463 return "<li>"
464 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
465 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
466 }
467
468 /**
469 * Show a form for editing the watchlist in "raw" mode
470 *
471 * @param $output OutputPage
472 * @param $user User
473 */
474 public function showRawForm( $output, $user ) {
475 global $wgUser;
476 $this->showItemCount( $output, $user );
477 $self = SpecialPage::getTitleFor( 'Watchlist' );
478 $form = Xml::openElement( 'form', array( 'method' => 'post',
479 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
480 $form .= Html::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
481 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
482 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
483 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
484 $form .= "<br />\n";
485 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
486 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
487 $titles = $this->getWatchlist( $user );
488 foreach( $titles as $title ) {
489 $form .= htmlspecialchars( $title ) . "\n";
490 }
491 $form .= '</textarea>';
492 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
493 $form .= '</fieldset></form>';
494 $output->addHTML( $form );
495 }
496
497 /**
498 * Determine whether we are editing the watchlist, and if so, what
499 * kind of editing operation
500 *
501 * @param $request WebRequest
502 * @param $par mixed
503 * @return int
504 */
505 public static function getMode( $request, $par ) {
506 $mode = strtolower( $request->getVal( 'action', $par ) );
507 switch( $mode ) {
508 case 'clear':
509 return self::EDIT_CLEAR;
510 case 'raw':
511 return self::EDIT_RAW;
512 case 'edit':
513 return self::EDIT_NORMAL;
514 default:
515 return false;
516 }
517 }
518
519 /**
520 * Build a set of links for convenient navigation
521 * between watchlist viewing and editing modes
522 *
523 * @param $skin Skin to use
524 * @return string
525 */
526 public static function buildTools( $skin ) {
527 global $wgLang;
528
529 $tools = array();
530 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
531 foreach( $modes as $mode => $subpage ) {
532 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
533 $tools[] = $skin->linkKnown(
534 SpecialPage::getTitleFor( 'Watchlist', $subpage ),
535 wfMsgHtml( "watchlisttools-{$mode}" )
536 );
537 }
538 return Html::rawElement( 'span',
539 array( 'class' => 'mw-watchlist-toollinks' ),
540 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
541 }
542 }