Added missing calls to SpecialPage::setHeaders() and SpecialPage::outputHeader()
[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 $this->setHeaders();
36
37 $out = $this->getOutput();
38
39 # Anons don't get a watchlist
40 if( $this->getUser()->isAnon() ) {
41 $out->setPageTitle( wfMsg( 'watchnologin' ) );
42 $llink = Linker::linkKnown(
43 SpecialPage::getTitleFor( 'Userlogin' ),
44 wfMsgHtml( 'loginreqlink' ),
45 array(),
46 array( 'returnto' => $this->getTitle()->getPrefixedText() )
47 );
48 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
49 return;
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 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
380
381 $namespace == NS_MAIN
382 ? wfMsgHtml( 'blanknamespace' )
383 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
384
385 $fields['TitlesNs'.$namespace] = array(
386 'type' => 'multiselect',
387 'options' => array(),
388 'section' => "ns$namespace",
389 );
390
391 foreach( array_keys( $pages ) as $dbkey ){
392 $title = Title::makeTitleSafe( $namespace, $dbkey );
393 $text = $this->buildRemoveLine( $title );
394 $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
395 }
396 }
397
398 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
399 $form->setTitle( $this->getTitle() );
400 $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
401 $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
402 $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
403 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
404 return $form;
405 }
406
407 /**
408 * Build the label for a checkbox, with a link to the title, and various additional bits
409 *
410 * @param $title Title
411 * @return string
412 */
413 private function buildRemoveLine( $title ) {
414 $link = Linker::link( $title );
415 if( $title->isRedirect() ) {
416 // Linker already makes class mw-redirect, so this is redundant
417 $link = '<span class="watchlistredir">' . $link . '</span>';
418 }
419 $tools[] = Linker::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
420 if( $title->exists() ) {
421 $tools[] = Linker::linkKnown(
422 $title,
423 wfMsgHtml( 'history_short' ),
424 array(),
425 array( 'action' => 'history' )
426 );
427 }
428 if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
429 $tools[] = Linker::linkKnown(
430 SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
431 wfMsgHtml( 'contributions' )
432 );
433 }
434
435 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
436
437 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
438 }
439
440 /**
441 * Get a form for editing the watchlist in "raw" mode
442 *
443 * @return HTMLForm
444 */
445 protected function getRawForm(){
446 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist() ), "\n" );
447 $fields = array(
448 'Titles' => array(
449 'type' => 'textarea',
450 'label-message' => 'watchlistedit-raw-titles',
451 'default' => $titles,
452 ),
453 );
454 $form = new HTMLForm( $fields, $this->getContext() );
455 $form->setTitle( $this->getTitle( 'raw' ) );
456 $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
457 $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
458 $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
459 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
460 return $form;
461 }
462
463 /**
464 * Determine whether we are editing the watchlist, and if so, what
465 * kind of editing operation
466 *
467 * @param $request WebRequest
468 * @param $par mixed
469 * @return int
470 */
471 public static function getMode( $request, $par ) {
472 $mode = strtolower( $request->getVal( 'action', $par ) );
473 switch( $mode ) {
474 case 'clear':
475 case self::EDIT_CLEAR:
476 return self::EDIT_CLEAR;
477
478 case 'raw':
479 case self::EDIT_RAW:
480 return self::EDIT_RAW;
481
482 case 'edit':
483 case self::EDIT_NORMAL:
484 return self::EDIT_NORMAL;
485
486 default:
487 return false;
488 }
489 }
490
491 /**
492 * Build a set of links for convenient navigation
493 * between watchlist viewing and editing modes
494 *
495 * @param $unused Unused
496 * @return string
497 */
498 public static function buildTools( $unused ) {
499 global $wgLang;
500
501 $tools = array();
502 $modes = array(
503 'view' => array( 'Watchlist', false ),
504 'edit' => array( 'EditWatchlist', false ),
505 'raw' => array( 'EditWatchlist', 'raw' ),
506 );
507 foreach( $modes as $mode => $arr ) {
508 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
509 $tools[] = Linker::linkKnown(
510 SpecialPage::getTitleFor( $arr[0], $arr[1] ),
511 wfMsgHtml( "watchlisttools-{$mode}" )
512 );
513 }
514 return Html::rawElement( 'span',
515 array( 'class' => 'mw-watchlist-toollinks' ),
516 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
517 }
518 }
519
520 # B/C since 1.18
521 class WatchlistEditor extends SpecialEditWatchlist {}
522
523 /**
524 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
525 */
526 class EditWatchlistNormalHTMLForm extends HTMLForm {
527 public function getLegend( $namespace ){
528 $namespace = substr( $namespace, 2 );
529 return $namespace == NS_MAIN
530 ? wfMsgHtml( 'blanknamespace' )
531 : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
532 }
533 }