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