Use protocol-relative URL for link to MediaWiki.org (as with the footer logo on each...
[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 protected $toc;
22
23 public function __construct(){
24 parent::__construct( 'EditWatchlist' );
25 }
26
27 /**
28 * Main execution point
29 *
30 * @param $mode int
31 */
32 public function execute( $mode ) {
33 $this->setHeaders();
34
35 $out = $this->getOutput();
36
37 # Anons don't get a watchlist
38 if( $this->getUser()->isAnon() ) {
39 $out->setPageTitle( $this->msg( 'watchnologin' ) );
40 $llink = Linker::linkKnown(
41 SpecialPage::getTitleFor( 'Userlogin' ),
42 wfMsgHtml( 'loginreqlink' ),
43 array(),
44 array( 'returnto' => $this->getTitle()->getPrefixedText() )
45 );
46 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
47 return;
48 }
49
50 $this->checkPermissions();
51
52 $this->outputHeader();
53
54 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
55 )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
56
57 # B/C: $mode used to be waaay down the parameter list, and the first parameter
58 # was $wgUser
59 if( $mode instanceof User ){
60 $args = func_get_args();
61 if( count( $args >= 4 ) ){
62 $mode = $args[3];
63 }
64 }
65 $mode = self::getMode( $this->getRequest(), $mode );
66
67 switch( $mode ) {
68 case self::EDIT_CLEAR:
69 // The "Clear" link scared people too much.
70 // Pass on to the raw editor, from which it's very easy to clear.
71
72 case self::EDIT_RAW:
73 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
74 $form = $this->getRawForm();
75 if( $form->show() ){
76 $out->addHTML( $this->successMessage );
77 $out->returnToMain();
78 }
79 break;
80
81 case self::EDIT_NORMAL:
82 default:
83 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
84 $form = $this->getNormalForm();
85 if( $form->show() ){
86 $out->addHTML( $this->successMessage );
87 $out->returnToMain();
88 } elseif ( $this->toc !== false ) {
89 $out->prependHTML( $this->toc );
90 }
91 break;
92 }
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 String
100 * @return array
101 */
102 private function extractTitles( $list ) {
103 $titles = array();
104 $list = explode( "\n", trim( $list ) );
105 if( !is_array( $list ) ) {
106 return array();
107 }
108 foreach( $list as $text ) {
109 $text = trim( $text );
110 if( strlen( $text ) > 0 ) {
111 $title = Title::newFromText( $text );
112 if( $title instanceof Title && $title->isWatchable() ) {
113 $titles[] = $title->getPrefixedText();
114 }
115 }
116 }
117 return array_unique( $titles );
118 }
119
120 public function submitRaw( $data ){
121 $wanted = $this->extractTitles( $data['Titles'] );
122 $current = $this->getWatchlist();
123
124 if( count( $wanted ) > 0 ) {
125 $toWatch = array_diff( $wanted, $current );
126 $toUnwatch = array_diff( $current, $wanted );
127 $this->watchTitles( $toWatch );
128 $this->unwatchTitles( $toUnwatch );
129 $this->getUser()->invalidateCache();
130
131 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ){
132 $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
133 } else {
134 return false;
135 }
136
137 if( count( $toWatch ) > 0 ) {
138 $this->successMessage .= ' ' . wfMessage(
139 'watchlistedit-raw-added',
140 $this->getLanguage()->formatNum( count( $toWatch ) )
141 );
142 $this->showTitles( $toWatch, $this->successMessage );
143 }
144
145 if( count( $toUnwatch ) > 0 ) {
146 $this->successMessage .= ' ' . wfMessage(
147 'watchlistedit-raw-removed',
148 $this->getLanguage()->formatNum( count( $toUnwatch ) )
149 );
150 $this->showTitles( $toUnwatch, $this->successMessage );
151 }
152 } else {
153 $this->clearWatchlist();
154 $this->getUser()->invalidateCache();
155
156 if( count( $current ) > 0 ){
157 $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
158 } else {
159 return false;
160 }
161
162 $this->successMessage .= ' ' . wfMessage(
163 'watchlistedit-raw-removed',
164 $this->getLanguage()->formatNum( count( $current ) )
165 );
166 $this->showTitles( $current, $this->successMessage );
167 }
168 return true;
169 }
170
171 /**
172 * Print out a list of linked titles
173 *
174 * $titles can be an array of strings or Title objects; the former
175 * is preferred, since Titles are very memory-heavy
176 *
177 * @param $titles array of strings, or Title objects
178 * @param $output String
179 */
180 private function showTitles( $titles, &$output ) {
181 $talk = wfMsgHtml( 'talkpagelinktext' );
182 // Do a batch existence check
183 $batch = new LinkBatch();
184 foreach( $titles as $title ) {
185 if( !$title instanceof Title ) {
186 $title = Title::newFromText( $title );
187 }
188 if( $title instanceof Title ) {
189 $batch->addObj( $title );
190 $batch->addObj( $title->getTalkPage() );
191 }
192 }
193 $batch->execute();
194 // Print out the list
195 $output .= "<ul>\n";
196 foreach( $titles as $title ) {
197 if( !$title instanceof Title ) {
198 $title = Title::newFromText( $title );
199 }
200 if( $title instanceof Title ) {
201 $output .= "<li>"
202 . Linker::link( $title )
203 . ' (' . Linker::link( $title->getTalkPage(), $talk )
204 . ")</li>\n";
205 }
206 }
207 $output .= "</ul>\n";
208 }
209
210 /**
211 * Prepare a list of titles on a user's watchlist (excluding talk pages)
212 * and return an array of (prefixed) strings
213 *
214 * @return array
215 */
216 private function getWatchlist() {
217 $list = array();
218 $dbr = wfGetDB( DB_MASTER );
219 $res = $dbr->select(
220 'watchlist',
221 '*',
222 array(
223 'wl_user' => $this->getUser()->getId(),
224 ),
225 __METHOD__
226 );
227 if( $res->numRows() > 0 ) {
228 foreach ( $res as $row ) {
229 $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
230 if( $title instanceof Title && !$title->isTalkPage() )
231 $list[] = $title->getPrefixedText();
232 }
233 $res->free();
234 }
235 return $list;
236 }
237
238 /**
239 * Get a list of titles on a user's watchlist, excluding talk pages,
240 * and return as a two-dimensional array with namespace and title.
241 *
242 * @return array
243 */
244 private function getWatchlistInfo() {
245 $titles = array();
246 $dbr = wfGetDB( DB_MASTER );
247
248 $res = $dbr->select(
249 array( 'watchlist' ),
250 array( 'wl_namespace', 'wl_title' ),
251 array( 'wl_user' => $this->getUser()->getId() ),
252 __METHOD__,
253 array( 'ORDER BY' => 'wl_namespace, wl_title' )
254 );
255
256 $lb = new LinkBatch();
257 foreach ( $res as $row ) {
258 $lb->add( $row->wl_namespace, $row->wl_title );
259 if ( !MWNamespace::isTalk( $row->wl_namespace ) ) {
260 $titles[$row->wl_namespace][$row->wl_title] = 1;
261 }
262 }
263
264 $lb->execute();
265 return $titles;
266 }
267
268 /**
269 * Remove all titles from a user's watchlist
270 */
271 private function clearWatchlist() {
272 $dbw = wfGetDB( DB_MASTER );
273 $dbw->delete(
274 'watchlist',
275 array( 'wl_user' => $this->getUser()->getId() ),
276 __METHOD__
277 );
278 }
279
280 /**
281 * Add a list of titles to a user's watchlist
282 *
283 * $titles can be an array of strings or Title objects; the former
284 * is preferred, since Titles are very memory-heavy
285 *
286 * @param $titles Array of strings, or Title objects
287 */
288 private function watchTitles( $titles ) {
289 $dbw = wfGetDB( DB_MASTER );
290 $rows = array();
291 foreach( $titles as $title ) {
292 if( !$title instanceof Title ) {
293 $title = Title::newFromText( $title );
294 }
295 if( $title instanceof Title ) {
296 $rows[] = array(
297 'wl_user' => $this->getUser()->getId(),
298 'wl_namespace' => ( $title->getNamespace() & ~1 ),
299 'wl_title' => $title->getDBkey(),
300 'wl_notificationtimestamp' => null,
301 );
302 $rows[] = array(
303 'wl_user' => $this->getUser()->getId(),
304 'wl_namespace' => ( $title->getNamespace() | 1 ),
305 'wl_title' => $title->getDBkey(),
306 'wl_notificationtimestamp' => null,
307 );
308 }
309 }
310 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
311 }
312
313 /**
314 * Remove a list of titles from a user's watchlist
315 *
316 * $titles can be an array of strings or Title objects; the former
317 * is preferred, since Titles are very memory-heavy
318 *
319 * @param $titles Array of strings, or Title objects
320 */
321 private function unwatchTitles( $titles ) {
322 $dbw = wfGetDB( DB_MASTER );
323 foreach( $titles as $title ) {
324 if( !$title instanceof Title ) {
325 $title = Title::newFromText( $title );
326 }
327 if( $title instanceof Title ) {
328 $dbw->delete(
329 'watchlist',
330 array(
331 'wl_user' => $this->getUser()->getId(),
332 'wl_namespace' => ( $title->getNamespace() & ~1 ),
333 'wl_title' => $title->getDBkey(),
334 ),
335 __METHOD__
336 );
337 $dbw->delete(
338 'watchlist',
339 array(
340 'wl_user' => $this->getUser()->getId(),
341 'wl_namespace' => ( $title->getNamespace() | 1 ),
342 'wl_title' => $title->getDBkey(),
343 ),
344 __METHOD__
345 );
346 $article = new Article( $title, 0 );
347 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
348 }
349 }
350 }
351
352 public function submitNormal( $data ) {
353 $removed = array();
354
355 foreach( $data as $titles ) {
356 $this->unwatchTitles( $titles );
357 $removed += $titles;
358 }
359
360 if( count( $removed ) > 0 ) {
361 $this->successMessage = wfMessage(
362 'watchlistedit-normal-done',
363 $this->getLanguage()->formatNum( count( $removed ) )
364 );
365 $this->showTitles( $removed, $this->successMessage );
366 return true;
367 } else {
368 return false;
369 }
370 }
371
372 /**
373 * Get the standard watchlist editing form
374 *
375 * @return HTMLForm
376 */
377 protected function getNormalForm(){
378 global $wgContLang;
379
380 $fields = array();
381 $count = 0;
382
383 $haveInvalidNamespaces = false;
384 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
385 if ( $namespace < 0 ) {
386 $haveInvalidNamespaces = true;
387 continue;
388 }
389
390 $fields['TitlesNs'.$namespace] = array(
391 'class' => 'EditWatchlistCheckboxSeriesField',
392 'options' => array(),
393 'section' => "ns$namespace",
394 );
395
396 foreach( array_keys( $pages ) as $dbkey ){
397 $title = Title::makeTitleSafe( $namespace, $dbkey );
398 $text = $this->buildRemoveLine( $title );
399 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
400 $count++;
401 }
402 }
403 if ( $haveInvalidNamespaces ) {
404 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
405 $this->getContext()->getUser()->cleanupWatchlist();
406 }
407
408 if ( count( $fields ) > 1 && $count > 30 ) {
409 $this->toc = Linker::tocIndent();
410 $tocLength = 0;
411 foreach( $fields as $key => $data ) {
412
413 # strip out the 'ns' prefix from the section name:
414 $ns = substr( $data['section'], 2 );
415
416 $nsText = ($ns == NS_MAIN)
417 ? wfMsgHtml( 'blanknamespace' )
418 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
419 $this->toc .= Linker::tocLine( "editwatchlist-{$data['section']}", $nsText,
420 $this->getLanguage()->formatNum( ++$tocLength ), 1 ) . Linker::tocLineEnd();
421 }
422 $this->toc = Linker::tocList( $this->toc );
423 } else {
424 $this->toc = false;
425 }
426
427 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
428 $form->setTitle( $this->getTitle() );
429 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
430 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
431 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
432 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
433 return $form;
434 }
435
436 /**
437 * Build the label for a checkbox, with a link to the title, and various additional bits
438 *
439 * @param $title Title
440 * @return string
441 */
442 private function buildRemoveLine( $title ) {
443 $link = Linker::link( $title );
444 if( $title->isRedirect() ) {
445 // Linker already makes class mw-redirect, so this is redundant
446 $link = '<span class="watchlistredir">' . $link . '</span>';
447 }
448 $tools[] = Linker::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
449 if( $title->exists() ) {
450 $tools[] = Linker::linkKnown(
451 $title,
452 wfMsgHtml( 'history_short' ),
453 array(),
454 array( 'action' => 'history' )
455 );
456 }
457 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
458 $tools[] = Linker::linkKnown(
459 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
460 wfMsgHtml( 'contributions' )
461 );
462 }
463
464 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
465
466 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
467 }
468
469 /**
470 * Get a form for editing the watchlist in "raw" mode
471 *
472 * @return HTMLForm
473 */
474 protected function getRawForm(){
475 $titles = implode( $this->getWatchlist(), "\n" );
476 $fields = array(
477 'Titles' => array(
478 'type' => 'textarea',
479 'label-message' => 'watchlistedit-raw-titles',
480 'default' => $titles,
481 ),
482 );
483 $form = new HTMLForm( $fields, $this->getContext() );
484 $form->setTitle( $this->getTitle( 'raw' ) );
485 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
486 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
487 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
488 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
489 return $form;
490 }
491
492 /**
493 * Determine whether we are editing the watchlist, and if so, what
494 * kind of editing operation
495 *
496 * @param $request WebRequest
497 * @param $par mixed
498 * @return int
499 */
500 public static function getMode( $request, $par ) {
501 $mode = strtolower( $request->getVal( 'action', $par ) );
502 switch( $mode ) {
503 case 'clear':
504 case self::EDIT_CLEAR:
505 return self::EDIT_CLEAR;
506
507 case 'raw':
508 case self::EDIT_RAW:
509 return self::EDIT_RAW;
510
511 case 'edit':
512 case self::EDIT_NORMAL:
513 return self::EDIT_NORMAL;
514
515 default:
516 return false;
517 }
518 }
519
520 /**
521 * Build a set of links for convenient navigation
522 * between watchlist viewing and editing modes
523 *
524 * @param $unused Unused
525 * @return string
526 */
527 public static function buildTools( $unused ) {
528 global $wgLang;
529
530 $tools = array();
531 $modes = array(
532 'view' => array( 'Watchlist', false ),
533 'edit' => array( 'EditWatchlist', false ),
534 'raw' => array( 'EditWatchlist', 'raw' ),
535 );
536 foreach( $modes as $mode => $arr ) {
537 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
538 $tools[] = Linker::linkKnown(
539 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
540 wfMsgHtml( "watchlisttools-{$mode}" )
541 );
542 }
543 return Html::rawElement( 'span',
544 array( 'class' => 'mw-watchlist-toollinks' ),
545 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
546 }
547 }
548
549 # B/C since 1.18
550 class WatchlistEditor extends SpecialEditWatchlist {}
551
552 /**
553 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
554 */
555 class EditWatchlistNormalHTMLForm extends HTMLForm {
556 public function getLegend( $namespace ){
557 $namespace = substr( $namespace, 2 );
558 return $namespace == NS_MAIN
559 ? wfMsgHtml( 'blanknamespace' )
560 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
561 }
562 public function getBody() {
563 return $this->displaySection( $this->mFieldTree, '', 'editwatchlist-' );
564 }
565 }
566
567 class EditWatchlistCheckboxSeriesField extends HTMLMultiSelectField {
568 /**
569 * HTMLMultiSelectField throws validation errors if we get input data
570 * that doesn't match the data set in the form setup. This causes
571 * problems if something gets removed from the watchlist while the
572 * form is open (bug 32126), but we know that invalid items will
573 * be harmless so we can override it here.
574 *
575 * @param $value String the value the field was submitted with
576 * @param $alldata Array the data collected from the form
577 * @return Mixed Bool true on success, or String error to display.
578 */
579 function validate( $value, $alldata ) {
580 // Need to call into grandparent to be a good citizen. :)
581 return HTMLFormField::validate( $value, $alldata );
582 }
583 }