Braces and spaces
[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 while( $row = $res->fetchObject() ) {
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 $uid = intval( $user->getId() );
211 list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
212 $sql = "SELECT wl_namespace, wl_title, page_id, page_len, page_is_redirect, page_latest
213 FROM {$watchlist} LEFT JOIN {$page} ON ( wl_namespace = page_namespace
214 AND wl_title = page_title ) WHERE wl_user = {$uid}";
215 $res = $dbr->query( $sql, __METHOD__ );
216 if( $res && $dbr->numRows( $res ) > 0 ) {
217 $cache = LinkCache::singleton();
218 while( $row = $dbr->fetchObject( $res ) ) {
219 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
220 if( $title instanceof Title ) {
221 // Update the link cache while we're at it
222 if( $row->page_id ) {
223 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
224 } else {
225 $cache->addBadLinkObj( $title );
226 }
227 // Ignore non-talk
228 if( !$title->isTalkPage() ) {
229 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
230 }
231 }
232 }
233 }
234 return $titles;
235 }
236
237 /**
238 * Show a message indicating the number of items on the user's watchlist,
239 * and return this count for additional checking
240 *
241 * @param $output OutputPage
242 * @param $user User
243 * @return int
244 */
245 private function showItemCount( $output, $user ) {
246 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
247 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
248 $GLOBALS['wgLang']->formatNum( $count ) ) );
249 } else {
250 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
251 }
252 return $count;
253 }
254
255 /**
256 * Remove all titles from a user's watchlist
257 *
258 * @param $user User
259 */
260 private function clearWatchlist( $user ) {
261 $dbw = wfGetDB( DB_MASTER );
262 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__ );
263 }
264
265 /**
266 * Add a list of titles to a user's watchlist
267 *
268 * $titles can be an array of strings or Title objects; the former
269 * is preferred, since Titles are very memory-heavy
270 *
271 * @param $titles An array of strings, or Title objects
272 * @param $user User
273 */
274 private function watchTitles( $titles, $user ) {
275 $dbw = wfGetDB( DB_MASTER );
276 $rows = array();
277 foreach( $titles as $title ) {
278 if( !$title instanceof Title ) {
279 $title = Title::newFromText( $title );
280 }
281 if( $title instanceof Title ) {
282 $rows[] = array(
283 'wl_user' => $user->getId(),
284 'wl_namespace' => ( $title->getNamespace() & ~1 ),
285 'wl_title' => $title->getDBkey(),
286 'wl_notificationtimestamp' => null,
287 );
288 $rows[] = array(
289 'wl_user' => $user->getId(),
290 'wl_namespace' => ( $title->getNamespace() | 1 ),
291 'wl_title' => $title->getDBkey(),
292 'wl_notificationtimestamp' => null,
293 );
294 }
295 }
296 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
297 }
298
299 /**
300 * Remove a list of titles from a user's watchlist
301 *
302 * $titles can be an array of strings or Title objects; the former
303 * is preferred, since Titles are very memory-heavy
304 *
305 * @param $titles An array of strings, or Title objects
306 * @param $user User
307 */
308 private function unwatchTitles( $titles, $user ) {
309 $dbw = wfGetDB( DB_MASTER );
310 foreach( $titles as $title ) {
311 if( !$title instanceof Title ) {
312 $title = Title::newFromText( $title );
313 }
314 if( $title instanceof Title ) {
315 $dbw->delete(
316 'watchlist',
317 array(
318 'wl_user' => $user->getId(),
319 'wl_namespace' => ( $title->getNamespace() & ~1 ),
320 'wl_title' => $title->getDBkey(),
321 ),
322 __METHOD__
323 );
324 $dbw->delete(
325 'watchlist',
326 array(
327 'wl_user' => $user->getId(),
328 'wl_namespace' => ( $title->getNamespace() | 1 ),
329 'wl_title' => $title->getDBkey(),
330 ),
331 __METHOD__
332 );
333 $article = new Article($title);
334 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
335 }
336 }
337 }
338
339 /**
340 * Show the standard watchlist editing form
341 *
342 * @param $output OutputPage
343 * @param $user User
344 */
345 private function showNormalForm( $output, $user ) {
346 global $wgUser;
347 if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
348 $self = SpecialPage::getTitleFor( 'Watchlist' );
349 $form = Xml::openElement( 'form', array( 'method' => 'post',
350 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
351 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
352 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'watchlistedit-normal-legend' ) . "</legend>";
353 $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
354 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
355 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-normal-submit' ) ) . '</p>';
356 $form .= '</fieldset></form>';
357 $output->addHTML( $form );
358 }
359 }
360
361 /**
362 * Build the part of the standard watchlist editing form with the actual
363 * title selection checkboxes and stuff. Also generates a table of
364 * contents if there's more than one heading.
365 *
366 * @param $user User
367 * @param $skin Skin (really, Linker)
368 */
369 private function buildRemoveList( $user, $skin ) {
370 $list = "";
371 $toc = $skin->tocIndent();
372 $tocLength = 0;
373 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
374 $tocLength++;
375 $heading = htmlspecialchars( $this->getNamespaceHeading( $namespace ) );
376 $anchor = "editwatchlist-ns" . $namespace;
377
378 $list .= $skin->makeHeadLine( 2, ">", $anchor, $heading, "" );
379 $toc .= $skin->tocLine( $anchor, $heading, $tocLength, 1 ) . $skin->tocLineEnd();
380
381 $list .= "<ul>\n";
382 foreach( $pages as $dbkey => $redirect ) {
383 $title = Title::makeTitleSafe( $namespace, $dbkey );
384 $list .= $this->buildRemoveLine( $title, $redirect, $skin );
385 }
386 $list .= "</ul>\n";
387 }
388 // ISSUE: omit the TOC if the total number of titles is low?
389 if( $tocLength > 1 ) {
390 $list = $skin->tocList( $toc ) . $list;
391 }
392 return $list;
393 }
394
395 /**
396 * Get the correct "heading" for a namespace
397 *
398 * @param $namespace int
399 * @return string
400 */
401 private function getNamespaceHeading( $namespace ) {
402 return $namespace == NS_MAIN
403 ? wfMsgHtml( 'blanknamespace' )
404 : htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( $namespace ) );
405 }
406
407 /**
408 * Build a single list item containing a check box selecting a title
409 * and a link to that title, with various additional bits
410 *
411 * @param $title Title
412 * @param $redirect bool
413 * @param $skin Skin
414 * @return string
415 */
416 private function buildRemoveLine( $title, $redirect, $skin ) {
417 global $wgLang;
418
419 $link = $skin->link( $title );
420 if( $redirect ) {
421 $link = '<span class="watchlistredir">' . $link . '</span>';
422 }
423 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
424 if( $title->exists() ) {
425 $tools[] = $skin->link(
426 $title,
427 wfMsgHtml( 'history_short' ),
428 array(),
429 array( 'action' => 'history' ),
430 array( 'known', 'noclasses' )
431 );
432 }
433 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
434 $tools[] = $skin->link(
435 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
436 wfMsgHtml( 'contributions' ),
437 array(),
438 array(),
439 array( 'known', 'noclasses' )
440 );
441 }
442
443 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
444
445 return "<li>"
446 . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
447 . $link . " (" . $wgLang->pipeList( $tools ) . ")" . "</li>\n";
448 }
449
450 /**
451 * Show a form for editing the watchlist in "raw" mode
452 *
453 * @param $output OutputPage
454 * @param $user User
455 */
456 public function showRawForm( $output, $user ) {
457 global $wgUser;
458 $this->showItemCount( $output, $user );
459 $self = SpecialPage::getTitleFor( 'Watchlist' );
460 $form = Xml::openElement( 'form', array( 'method' => 'post',
461 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
462 $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
463 $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
464 $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
465 $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
466 $form .= "<br />\n";
467 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
468 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
469 $titles = $this->getWatchlist( $user );
470 foreach( $titles as $title ) {
471 $form .= htmlspecialchars( $title ) . "\n";
472 }
473 $form .= '</textarea>';
474 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
475 $form .= '</fieldset></form>';
476 $output->addHTML( $form );
477 }
478
479 /**
480 * Determine whether we are editing the watchlist, and if so, what
481 * kind of editing operation
482 *
483 * @param $request WebRequest
484 * @param $par mixed
485 * @return int
486 */
487 public static function getMode( $request, $par ) {
488 $mode = strtolower( $request->getVal( 'action', $par ) );
489 switch( $mode ) {
490 case 'clear':
491 return self::EDIT_CLEAR;
492 case 'raw':
493 return self::EDIT_RAW;
494 case 'edit':
495 return self::EDIT_NORMAL;
496 default:
497 return false;
498 }
499 }
500
501 /**
502 * Build a set of links for convenient navigation
503 * between watchlist viewing and editing modes
504 *
505 * @param $skin Skin to use
506 * @return string
507 */
508 public static function buildTools( $skin ) {
509 global $wgLang;
510
511 $tools = array();
512 $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
513 foreach( $modes as $mode => $subpage ) {
514 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
515 $tools[] = $skin->linkKnown(
516 SpecialPage::getTitleFor( 'Watchlist', $subpage ),
517 wfMsgHtml( "watchlisttools-{$mode}" )
518 );
519 }
520 return Html::rawElement( 'span',
521 array( 'class' => 'mw-watchlist-toollinks' ),
522 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
523 }
524 }