correct typo in r102393
[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 $sub = wfMsgExt(
57 'watchlistfor2',
58 array( 'parseinline', 'replaceafter' ),
59 $this->getUser()->getName(),
60 SpecialEditWatchlist::buildTools( null )
61 );
62 $out->setSubtitle( $sub );
63
64 # B/C: $mode used to be waaay down the parameter list, and the first parameter
65 # was $wgUser
66 if( $mode instanceof User ){
67 $args = func_get_args();
68 if( count( $args >= 4 ) ){
69 $mode = $args[3];
70 }
71 }
72 $mode = self::getMode( $this->getRequest(), $mode );
73
74 switch( $mode ) {
75 case self::EDIT_CLEAR:
76 // The "Clear" link scared people too much.
77 // Pass on to the raw editor, from which it's very easy to clear.
78
79 case self::EDIT_RAW:
80 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
81 $form = $this->getRawForm();
82 if( $form->show() ){
83 $out->addHTML( $this->successMessage );
84 $out->returnToMain();
85 }
86 break;
87
88 case self::EDIT_NORMAL:
89 default:
90 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
91 $form = $this->getNormalForm();
92 if( $form->show() ){
93 $out->addHTML( $this->successMessage );
94 $out->returnToMain();
95 } elseif ( $this->toc !== false ) {
96 $out->prependHTML( $this->toc );
97 }
98 break;
99 }
100 }
101
102 /**
103 * Extract a list of titles from a blob of text, returning
104 * (prefixed) strings; unwatchable titles are ignored
105 *
106 * @param $list String
107 * @return array
108 */
109 private function extractTitles( $list ) {
110 $titles = array();
111 $list = explode( "\n", trim( $list ) );
112 if( !is_array( $list ) ) {
113 return array();
114 }
115 foreach( $list as $text ) {
116 $text = trim( $text );
117 if( strlen( $text ) > 0 ) {
118 $title = Title::newFromText( $text );
119 if( $title instanceof Title && $title->isWatchable() ) {
120 $titles[] = $title->getPrefixedText();
121 }
122 }
123 }
124 return array_unique( $titles );
125 }
126
127 public function submitRaw( $data ){
128 $wanted = $this->extractTitles( $data['Titles'] );
129 $current = $this->getWatchlist();
130
131 if( count( $wanted ) > 0 ) {
132 $toWatch = array_diff( $wanted, $current );
133 $toUnwatch = array_diff( $current, $wanted );
134 $this->watchTitles( $toWatch );
135 $this->unwatchTitles( $toUnwatch );
136 $this->getUser()->invalidateCache();
137
138 if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ){
139 $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
140 } else {
141 return false;
142 }
143
144 if( count( $toWatch ) > 0 ) {
145 $this->successMessage .= wfMessage(
146 'watchlistedit-raw-added',
147 $this->getLang()->formatNum( count( $toWatch ) )
148 );
149 $this->showTitles( $toWatch, $this->successMessage );
150 }
151
152 if( count( $toUnwatch ) > 0 ) {
153 $this->successMessage .= wfMessage(
154 'watchlistedit-raw-removed',
155 $this->getLang()->formatNum( count( $toUnwatch ) )
156 );
157 $this->showTitles( $toUnwatch, $this->successMessage );
158 }
159 } else {
160 $this->clearWatchlist();
161 $this->getUser()->invalidateCache();
162 $this->successMessage .= wfMessage(
163 'watchlistedit-raw-removed',
164 $this->getLang()->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, title and
241 * redirect status
242 *
243 * @return array
244 */
245 private function getWatchlistInfo() {
246 $titles = array();
247 $dbr = wfGetDB( DB_MASTER );
248
249 $res = $dbr->select(
250 array( 'watchlist' ),
251 array( 'wl_namespace', 'wl_title' ),
252 array( 'wl_user' => $this->getUser()->getId() ),
253 __METHOD__,
254 array( 'ORDER BY' => 'wl_namespace, wl_title' )
255 );
256
257 $lb = new LinkBatch();
258 foreach ( $res as $row ) {
259 $lb->add( $row->wl_namespace, $row->wl_title );
260 if ( !MWNamespace::isTalk( $row->wl_namespace ) ) {
261 $titles[$row->wl_namespace][$row->wl_title] = false;
262 }
263 }
264
265 $lb->execute();
266 return $titles;
267 }
268
269 /**
270 * Remove all titles from a user's watchlist
271 */
272 private function clearWatchlist() {
273 $dbw = wfGetDB( DB_MASTER );
274 $dbw->delete(
275 'watchlist',
276 array( 'wl_user' => $this->getUser()->getId() ),
277 __METHOD__
278 );
279 }
280
281 /**
282 * Add a list of titles to a user's watchlist
283 *
284 * $titles can be an array of strings or Title objects; the former
285 * is preferred, since Titles are very memory-heavy
286 *
287 * @param $titles Array of strings, or Title objects
288 */
289 private function watchTitles( $titles ) {
290 $dbw = wfGetDB( DB_MASTER );
291 $rows = array();
292 foreach( $titles as $title ) {
293 if( !$title instanceof Title ) {
294 $title = Title::newFromText( $title );
295 }
296 if( $title instanceof Title ) {
297 $rows[] = array(
298 'wl_user' => $this->getUser()->getId(),
299 'wl_namespace' => ( $title->getNamespace() & ~1 ),
300 'wl_title' => $title->getDBkey(),
301 'wl_notificationtimestamp' => null,
302 );
303 $rows[] = array(
304 'wl_user' => $this->getUser()->getId(),
305 'wl_namespace' => ( $title->getNamespace() | 1 ),
306 'wl_title' => $title->getDBkey(),
307 'wl_notificationtimestamp' => null,
308 );
309 }
310 }
311 $dbw->insert( 'watchlist', $rows, __METHOD__, 'IGNORE' );
312 }
313
314 /**
315 * Remove a list of titles from a user's watchlist
316 *
317 * $titles can be an array of strings or Title objects; the former
318 * is preferred, since Titles are very memory-heavy
319 *
320 * @param $titles Array of strings, or Title objects
321 */
322 private function unwatchTitles( $titles ) {
323 $dbw = wfGetDB( DB_MASTER );
324 foreach( $titles as $title ) {
325 if( !$title instanceof Title ) {
326 $title = Title::newFromText( $title );
327 }
328 if( $title instanceof Title ) {
329 $dbw->delete(
330 'watchlist',
331 array(
332 'wl_user' => $this->getUser()->getId(),
333 'wl_namespace' => ( $title->getNamespace() & ~1 ),
334 'wl_title' => $title->getDBkey(),
335 ),
336 __METHOD__
337 );
338 $dbw->delete(
339 'watchlist',
340 array(
341 'wl_user' => $this->getUser()->getId(),
342 'wl_namespace' => ( $title->getNamespace() | 1 ),
343 'wl_title' => $title->getDBkey(),
344 ),
345 __METHOD__
346 );
347 $article = new Article( $title, 0 );
348 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
349 }
350 }
351 }
352
353 public function submitNormal( $data ) {
354 $removed = array();
355
356 foreach( $data as $titles ) {
357 $this->unwatchTitles( $titles );
358 $removed += $titles;
359 }
360
361 if( count( $removed ) > 0 ) {
362 $this->successMessage = wfMessage(
363 'watchlistedit-normal-done',
364 $this->getLang()->formatNum( count( $removed ) )
365 );
366 $this->showTitles( $removed, $this->successMessage );
367 return true;
368 } else {
369 return false;
370 }
371 }
372
373 /**
374 * Get the standard watchlist editing form
375 *
376 * @return HTMLForm
377 */
378 protected function getNormalForm(){
379 global $wgContLang;
380
381 $fields = array();
382 $count = 0;
383
384 $haveInvalidNamespaces = false;
385 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
386 if ( $namespace < 0 ) {
387 $haveInvalidNamespaces = true;
388 continue;
389 }
390
391 $fields['TitlesNs'.$namespace] = array(
392 'class' => 'EditWatchlistCheckboxSeriesField',
393 'options' => array(),
394 'section' => "ns$namespace",
395 );
396
397 foreach( array_keys( $pages ) as $dbkey ){
398 $title = Title::makeTitleSafe( $namespace, $dbkey );
399 $text = $this->buildRemoveLine( $title );
400 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
401 $count++;
402 }
403 }
404 if ( $haveInvalidNamespaces ) {
405 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
406 $this->getContext()->getUser()->cleanupWatchlist();
407 }
408
409 if ( count( $fields ) > 1 && $count > 30 ) {
410 $this->toc = Linker::tocIndent();
411 $tocLength = 0;
412 foreach( $fields as $key => $data ) {
413
414 # strip out the 'ns' prefix from the section name:
415 $ns = substr( $data['section'], 2 );
416
417 $nsText = ($ns == NS_MAIN)
418 ? wfMsgHtml( 'blanknamespace' )
419 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
420 $this->toc .= Linker::tocLine( "mw-htmlform-{$data['section']}", $nsText, ++$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->getLang()->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()->getLang()->getFormattedNsText( $namespace ) );
561 }
562 }
563
564 class EditWatchlistCheckboxSeriesField extends HTMLMultiSelectField {
565 /**
566 * HTMLMultiSelectField throws validation errors if we get input data
567 * that doesn't match the data set in the form setup. This causes
568 * problems if something gets removed from the watchlist while the
569 * form is open (bug 32126), but we know that invalid items will
570 * be harmless so we can override it here.
571 *
572 * @param $value String the value the field was submitted with
573 * @param $alldata Array the data collected from the form
574 * @return Mixed Bool true on success, or String error to display.
575 */
576 function validate( $value, $alldata ) {
577 // Need to call into grandparent to be a good citizen. :)
578 return HTMLFormField::validate( $value, $alldata );
579 }
580 }