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