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