Remove unused global, fix double defined global
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.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 SpecialEditWatchlist extends UnlistedSpecialPage {
11
12 /**
13 * Editing modes
14 */
15 const EDIT_CLEAR = 1;
16 const EDIT_RAW = 2;
17 const EDIT_NORMAL = 3;
18
19 protected $successMessage;
20
21 public function __construct(){
22 parent::__construct( 'EditWatchlist' );
23 }
24
25 /**
26 * Main execution point
27 *
28 * @param $user User
29 * @param $output OutputPage
30 * @param $request WebRequest
31 * @param $mode int
32 */
33 public function execute( $mode ) {
34 global $wgUser, $wgOut, $wgRequest;
35 if( wfReadOnly() ) {
36 $wgOut->readOnlyPage();
37 return;
38 }
39
40 # Anons don't get a watchlist
41 if( $wgUser->isAnon() ) {
42 $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
43 $llink = $wgUser->getSkin()->linkKnown(
44 SpecialPage::getTitleFor( 'Userlogin' ),
45 wfMsgHtml( 'loginreqlink' ),
46 array(),
47 array( 'returnto' => $this->getTitle()->getPrefixedText() )
48 );
49 $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
50 return;
51 }
52
53 $sub = wfMsgExt(
54 'watchlistfor2',
55 array( 'parseinline', 'replaceafter' ),
56 $wgUser->getName(),
57 SpecialEditWatchlist::buildTools( $wgUser->getSkin() )
58 );
59 $wgOut->setSubtitle( $sub );
60
61 # B/C: $mode used to be waaay down the parameter list, and the first parameter
62 # was $wgUser
63 if( $mode instanceof User ){
64 $args = func_get_args();
65 if( count( $args >= 4 ) ){
66 $mode = $args[3];
67 }
68 }
69 $mode = self::getMode( $wgRequest, $mode );
70
71 switch( $mode ) {
72 case self::EDIT_CLEAR:
73 // The "Clear" link scared people too much.
74 // Pass on to the raw editor, from which it's very easy to clear.
75
76 case self::EDIT_RAW:
77 $wgOut->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
78 $form = $this->getRawForm( $wgUser );
79 if( $form->show() ){
80 $wgOut->addHTML( $this->successMessage );
81 $wgOut->returnToMain();
82 }
83 break;
84
85 case self::EDIT_NORMAL:
86 default:
87 $wgOut->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
88 $form = $this->getNormalForm( $wgUser );
89 if( $form->show() ){
90 $wgOut->addHTML( $this->successMessage );
91 $wgOut->returnToMain();
92 }
93 break;
94 }
95 }
96
97 /**
98 * Extract a list of titles from a blob of text, returning
99 * (prefixed) strings; unwatchable titles are ignored
100 *
101 * @param $list String
102 * @return array
103 */
104 private function extractTitles( $list ) {
105 $titles = array();
106 $list = explode( "\n", trim( $list ) );
107 if( !is_array( $list ) ) {
108 return array();
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 public function submitRaw( $data ){
123 global $wgUser, $wgLang;
124 $wanted = $this->extractTitles( $data['Titles'] );
125 $current = $this->getWatchlist( $wgUser );
126
127 if( count( $wanted ) > 0 ) {
128 $toWatch = array_diff( $wanted, $current );
129 $toUnwatch = array_diff( $current, $wanted );
130 $this->watchTitles( $toWatch, $wgUser );
131 $this->unwatchTitles( $toUnwatch, $wgUser );
132 $wgUser->invalidateCache();
133
134 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ){
135 $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
136 } else {
137 return false;
138 }
139
140 if( count( $toWatch ) > 0 ) {
141 $this->successMessage .= wfMessage(
142 'watchlistedit-raw-added',
143 $wgLang->formatNum( count( $toWatch ) )
144 );
145 $this->showTitles( $toWatch, $this->successMessage, $wgUser->getSkin() );
146 }
147
148 if( count( $toUnwatch ) > 0 ) {
149 $this->successMessage .= wfMessage(
150 'watchlistedit-raw-removed',
151 $wgLang->formatNum( count( $toUnwatch ) )
152 );
153 $this->showTitles( $toUnwatch, $this->successMessage, $wgUser->getSkin() );
154 }
155 } else {
156 $this->clearWatchlist( $wgUser );
157 $wgUser->invalidateCache();
158 $this->successMessage .= wfMessage(
159 'watchlistedit-raw-removed',
160 $wgLang->formatNum( count( $current ) )
161 );
162 $this->showTitles( $current, $this->successMessage, $wgUser->getSkin() );
163 }
164 return true;
165 }
166
167 /**
168 * Print out a list of linked titles
169 *
170 * $titles can be an array of strings or Title objects; the former
171 * is preferred, since Titles are very memory-heavy
172 *
173 * @param $titles array of strings, or Title objects
174 * @param $output String
175 * @param $skin Skin
176 */
177 private function showTitles( $titles, &$output, $skin ) {
178 $talk = wfMsgHtml( 'talkpagelinktext' );
179 // Do a batch existence check
180 $batch = new LinkBatch();
181 foreach( $titles as $title ) {
182 if( !$title instanceof Title ) {
183 $title = Title::newFromText( $title );
184 }
185 if( $title instanceof Title ) {
186 $batch->addObj( $title );
187 $batch->addObj( $title->getTalkPage() );
188 }
189 }
190 $batch->execute();
191 // Print out the list
192 $output .= "<ul>\n";
193 foreach( $titles as $title ) {
194 if( !$title instanceof Title ) {
195 $title = Title::newFromText( $title );
196 }
197 if( $title instanceof Title ) {
198 $output .= "<li>"
199 . $skin->link( $title )
200 . ' (' . $skin->link( $title->getTalkPage(), $talk )
201 . ")</li>\n";
202 }
203 }
204 $output .= "</ul>\n";
205 }
206
207 /**
208 * Count the number of titles on a user's watchlist, excluding talk pages
209 *
210 * @param $user User
211 * @return int
212 */
213 private function countWatchlist( $user ) {
214 $dbr = wfGetDB( DB_MASTER );
215 $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
216 $row = $dbr->fetchObject( $res );
217 return ceil( $row->count / 2 ); // Paranoia
218 }
219
220 /**
221 * Prepare a list of titles on a user's watchlist (excluding talk pages)
222 * and return an array of (prefixed) strings
223 *
224 * @param $user User
225 * @return array
226 */
227 private function getWatchlist( $user ) {
228 $list = array();
229 $dbr = wfGetDB( DB_MASTER );
230 $res = $dbr->select(
231 'watchlist',
232 '*',
233 array(
234 'wl_user' => $user->getId(),
235 ),
236 __METHOD__
237 );
238 if( $res->numRows() > 0 ) {
239 foreach ( $res as $row ) {
240 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
241 if( $title instanceof Title && !$title->isTalkPage() )
242 $list[] = $title->getPrefixedText();
243 }
244 $res->free();
245 }
246 return $list;
247 }
248
249 /**
250 * Get a list of titles on a user's watchlist, excluding talk pages,
251 * and return as a two-dimensional array with namespace, title and
252 * redirect status
253 *
254 * @param $user User
255 * @return array
256 */
257 private function getWatchlistInfo( $user ) {
258 $titles = array();
259 $dbr = wfGetDB( DB_MASTER );
260
261 $res = $dbr->select(
262 array( 'watchlist', 'page' ),
263 array(
264 'wl_namespace',
265 'wl_title',
266 'page_id',
267 'page_len',
268 'page_is_redirect',
269 'page_latest'
270 ),
271 array( 'wl_user' => $user->getId() ),
272 __METHOD__,
273 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
274 array( 'page' => array(
275 'LEFT JOIN',
276 'wl_namespace = page_namespace AND wl_title = page_title'
277 ) )
278 );
279
280 if( $res && $dbr->numRows( $res ) > 0 ) {
281 $cache = LinkCache::singleton();
282 foreach ( $res as $row ) {
283 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
284 if( $title instanceof Title ) {
285 // Update the link cache while we're at it
286 if( $row->page_id ) {
287 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
288 } else {
289 $cache->addBadLinkObj( $title );
290 }
291 // Ignore non-talk
292 if( !$title->isTalkPage() ) {
293 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
294 }
295 }
296 }
297 }
298 return $titles;
299 }
300
301 /**
302 * Show a message indicating the number of items on the user's watchlist,
303 * and return this count for additional checking
304 *
305 * @param $output OutputPage
306 * @param $user User
307 * @return int
308 */
309 private function showItemCount( $output, $user ) {
310 if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
311 $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
312 $GLOBALS['wgLang']->formatNum( $count ) ) );
313 } else {
314 $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
315 }
316 return $count;
317 }
318
319 /**
320 * Remove all titles from a user's watchlist
321 *
322 * @param $user User
323 */
324 private function clearWatchlist( $user ) {
325 $dbw = wfGetDB( DB_MASTER );
326 $dbw->delete(
327 'watchlist',
328 array( 'wl_user' => $user->getId() ),
329 __METHOD__
330 );
331 }
332
333 /**
334 * Add a list of titles to a user's watchlist
335 *
336 * $titles can be an array of strings or Title objects; the former
337 * is preferred, since Titles are very memory-heavy
338 *
339 * @param $titles Array of strings, or Title objects
340 * @param $user User
341 */
342 private function watchTitles( $titles, $user ) {
343 $dbw = wfGetDB( DB_MASTER );
344 $rows = array();
345 foreach( $titles as $title ) {
346 if( !$title instanceof Title ) {
347 $title = Title::newFromText( $title );
348 }
349 if( $title instanceof Title ) {
350 $rows[] = array(
351 'wl_user' => $user->getId(),
352 'wl_namespace' => ( $title->getNamespace() & ~1 ),
353 'wl_title' => $title->getDBkey(),
354 'wl_notificationtimestamp' => null,
355 );
356 $rows[] = array(
357 'wl_user' => $user->getId(),
358 'wl_namespace' => ( $title->getNamespace() | 1 ),
359 'wl_title' => $title->getDBkey(),
360 'wl_notificationtimestamp' => null,
361 );
362 }
363 }
364 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
365 }
366
367 /**
368 * Remove a list of titles from a user's watchlist
369 *
370 * $titles can be an array of strings or Title objects; the former
371 * is preferred, since Titles are very memory-heavy
372 *
373 * @param $titles Array of strings, or Title objects
374 * @param $user User
375 */
376 private function unwatchTitles( $titles, $user ) {
377 $dbw = wfGetDB( DB_MASTER );
378 foreach( $titles as $title ) {
379 if( !$title instanceof Title ) {
380 $title = Title::newFromText( $title );
381 }
382 if( $title instanceof Title ) {
383 $dbw->delete(
384 'watchlist',
385 array(
386 'wl_user' => $user->getId(),
387 'wl_namespace' => ( $title->getNamespace() & ~1 ),
388 'wl_title' => $title->getDBkey(),
389 ),
390 __METHOD__
391 );
392 $dbw->delete(
393 'watchlist',
394 array(
395 'wl_user' => $user->getId(),
396 'wl_namespace' => ( $title->getNamespace() | 1 ),
397 'wl_title' => $title->getDBkey(),
398 ),
399 __METHOD__
400 );
401 $article = new Article($title);
402 wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
403 }
404 }
405 }
406
407 public function submitNormal( $data ) {
408 global $wgUser;
409 $removed = array();
410
411 foreach( $data as $titles ) {
412 $this->unwatchTitles( $titles, $wgUser );
413 $removed += $titles;
414 }
415
416 if( count( $removed ) > 0 ) {
417 global $wgLang;
418 $this->successMessage = wfMessage(
419 'watchlistedit-normal-done',
420 $wgLang->formatNum( count( $removed ) )
421 );
422 $this->showTitles( $removed, $this->successMessage, $wgUser->getSkin() );
423 return true;
424 } else {
425 return false;
426 }
427 }
428
429 /**
430 * Get the standard watchlist editing form
431 *
432 * @param $user User
433 * @return HTMLForm
434 */
435 protected function getNormalForm( $user ){
436 global $wgContLang;
437 $skin = $user->getSkin();
438 $fields = array();
439
440 foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ){
441
442 $namespace == NS_MAIN
443 ? wfMsgHtml( 'blanknamespace' )
444 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
445
446 $fields['TitlesNs'.$namespace] = array(
447 'type' => 'multiselect',
448 'options' => array(),
449 'section' => "ns$namespace",
450 );
451
452 foreach( $pages as $dbkey => $redirect ){
453 $title = Title::makeTitleSafe( $namespace, $dbkey );
454 $text = $this->buildRemoveLine( $title, $redirect, $skin );
455 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
456 }
457 }
458
459 $form = new EditWatchlistNormalHTMLForm( $fields );
460 $form->setTitle( $this->getTitle() );
461 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
462 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
463 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
464 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
465 return $form;
466 }
467
468 /**
469 * Build the label for a checkbox, with a link to the title, and various additional bits
470 *
471 * @param $title Title
472 * @param $redirect bool
473 * @param $skin Skin
474 * @return string
475 */
476 private function buildRemoveLine( $title, $redirect, $skin ) {
477 global $wgLang;
478
479 $link = $skin->link( $title );
480 if( $redirect ) {
481 $link = '<span class="watchlistredir">' . $link . '</span>';
482 }
483 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
484 if( $title->exists() ) {
485 $tools[] = $skin->link(
486 $title,
487 wfMsgHtml( 'history_short' ),
488 array(),
489 array( 'action' => 'history' ),
490 array( 'known', 'noclasses' )
491 );
492 }
493 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
494 $tools[] = $skin->link(
495 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
496 wfMsgHtml( 'contributions' ),
497 array(),
498 array(),
499 array( 'known', 'noclasses' )
500 );
501 }
502
503 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
504
505 return $link . " (" . $wgLang->pipeList( $tools ) . ")";
506 }
507
508 /**
509 * Get a form for editing the watchlist in "raw" mode
510 *
511 * @param $user User
512 * @return HTMLForm
513 */
514 protected function getRawForm( $user ){
515 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist( $user ) ), "\n" );
516 $fields = array(
517 'Titles' => array(
518 'type' => 'textarea',
519 'label-message' => 'watchlistedit-raw-titles',
520 'default' => $titles,
521 ),
522 );
523 $form = new HTMLForm( $fields );
524 $form->setTitle( $this->getTitle( 'raw' ) );
525 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
526 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
527 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
528 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
529 return $form;
530 }
531
532 /**
533 * Determine whether we are editing the watchlist, and if so, what
534 * kind of editing operation
535 *
536 * @param $request WebRequest
537 * @param $par mixed
538 * @return int
539 */
540 public static function getMode( $request, $par ) {
541 $mode = strtolower( $request->getVal( 'action', $par ) );
542 switch( $mode ) {
543 case 'clear':
544 case self::EDIT_CLEAR:
545 return self::EDIT_CLEAR;
546
547 case 'raw':
548 case self::EDIT_RAW:
549 return self::EDIT_RAW;
550
551 case 'edit':
552 case self::EDIT_NORMAL:
553 return self::EDIT_NORMAL;
554
555 default:
556 return false;
557 }
558 }
559
560 /**
561 * Build a set of links for convenient navigation
562 * between watchlist viewing and editing modes
563 *
564 * @param $skin Skin to use
565 * @return string
566 */
567 public static function buildTools( $skin ) {
568 global $wgLang;
569
570 $tools = array();
571 $modes = array(
572 'view' => array( 'Watchlist', false ),
573 'edit' => array( 'EditWatchlist', false ),
574 'raw' => array( 'EditWatchlist', 'raw' ),
575 );
576 foreach( $modes as $mode => $arr ) {
577 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
578 $tools[] = $skin->linkKnown(
579 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
580 wfMsgHtml( "watchlisttools-{$mode}" )
581 );
582 }
583 return Html::rawElement( 'span',
584 array( 'class' => 'mw-watchlist-toollinks' ),
585 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
586 }
587 }
588
589 # B/C since 1.18
590 class WatchlistEditor extends SpecialEditWatchlist {}
591
592 /**
593 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
594 */
595 class EditWatchlistNormalHTMLForm extends HTMLForm {
596 public function getLegend( $namespace ){
597 global $wgLang;
598 $namespace = substr( $namespace, 2 );
599 return $namespace == NS_MAIN
600 ? wfMsgHtml( 'blanknamespace' )
601 : htmlspecialchars( $wgLang->getFormattedNsText( $namespace ) );
602 }
603 }