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