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