4 * Provides the UI through which users can perform editing
5 * operations on their watchlist
8 * @author Rob Church <robchur@gmail.com>
10 class SpecialEditWatchlist
extends UnlistedSpecialPage
{
17 const EDIT_NORMAL
= 3;
19 protected $successMessage;
21 public function __construct(){
22 parent
::__construct( 'EditWatchlist' );
26 * Main execution point
30 public function execute( $mode ) {
32 throw new ReadOnlyError
;
37 $out = $this->getOutput();
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' ),
46 array( 'returnto' => $this->getTitle()->getPrefixedText() )
48 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
52 $this->outputHeader();
56 array( 'parseinline', 'replaceafter' ),
57 $this->getUser()->getName(),
58 SpecialEditWatchlist
::buildTools( null )
60 $out->setSubtitle( $sub );
62 # B/C: $mode used to be waaay down the parameter list, and the first parameter
64 if( $mode instanceof User
){
65 $args = func_get_args();
66 if( count( $args >= 4 ) ){
70 $mode = self
::getMode( $this->getRequest(), $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.
78 $out->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
79 $form = $this->getRawForm();
81 $out->addHTML( $this->successMessage
);
86 case self
::EDIT_NORMAL
:
88 $out->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
89 $form = $this->getNormalForm();
91 $out->addHTML( $this->successMessage
);
99 * Extract a list of titles from a blob of text, returning
100 * (prefixed) strings; unwatchable titles are ignored
102 * @param $list String
105 private function extractTitles( $list ) {
107 $list = explode( "\n", trim( $list ) );
108 if( !is_array( $list ) ) {
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();
120 return array_unique( $titles );
123 public function submitRaw( $data ){
124 $wanted = $this->extractTitles( $data['Titles'] );
125 $current = $this->getWatchlist();
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();
134 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
135 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
140 if( count( $toWatch ) > 0 ) {
141 $this->successMessage
.= wfMessage(
142 'watchlistedit-raw-added',
143 $this->getLang()->formatNum( count( $toWatch ) )
145 $this->showTitles( $toWatch, $this->successMessage
);
148 if( count( $toUnwatch ) > 0 ) {
149 $this->successMessage
.= wfMessage(
150 'watchlistedit-raw-removed',
151 $this->getLang()->formatNum( count( $toUnwatch ) )
153 $this->showTitles( $toUnwatch, $this->successMessage
);
156 $this->clearWatchlist();
157 $this->getUser()->invalidateCache();
158 $this->successMessage
.= wfMessage(
159 'watchlistedit-raw-removed',
160 $this->getLang()->formatNum( count( $current ) )
162 $this->showTitles( $current, $this->successMessage
);
168 * Print out a list of linked titles
170 * $titles can be an array of strings or Title objects; the former
171 * is preferred, since Titles are very memory-heavy
173 * @param $titles array of strings, or Title objects
174 * @param $output String
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 );
184 if( $title instanceof Title
) {
185 $batch->addObj( $title );
186 $batch->addObj( $title->getTalkPage() );
190 // Print out the list
192 foreach( $titles as $title ) {
193 if( !$title instanceof Title
) {
194 $title = Title
::newFromText( $title );
196 if( $title instanceof Title
) {
198 . Linker
::link( $title )
199 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
203 $output .= "</ul>\n";
207 * Prepare a list of titles on a user's watchlist (excluding talk pages)
208 * and return an array of (prefixed) strings
212 private function getWatchlist() {
214 $dbr = wfGetDB( DB_MASTER
);
219 'wl_user' => $this->getUser()->getId(),
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();
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
241 private function getWatchlistInfo() {
243 $dbr = wfGetDB( DB_MASTER
);
246 array( 'watchlist' ),
247 array( 'wl_namespace', 'wl_title' ),
248 array( 'wl_user' => $this->getUser()->getId() ),
250 array( 'ORDER BY' => 'wl_namespace, wl_title' )
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;
266 * Remove all titles from a user's watchlist
268 private function clearWatchlist() {
269 $dbw = wfGetDB( DB_MASTER
);
272 array( 'wl_user' => $this->getUser()->getId() ),
278 * Add a list of titles to a user's watchlist
280 * $titles can be an array of strings or Title objects; the former
281 * is preferred, since Titles are very memory-heavy
283 * @param $titles Array of strings, or Title objects
285 private function watchTitles( $titles ) {
286 $dbw = wfGetDB( DB_MASTER
);
288 foreach( $titles as $title ) {
289 if( !$title instanceof Title
) {
290 $title = Title
::newFromText( $title );
292 if( $title instanceof Title
) {
294 'wl_user' => $this->getUser()->getId(),
295 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
296 'wl_title' => $title->getDBkey(),
297 'wl_notificationtimestamp' => null,
300 'wl_user' => $this->getUser()->getId(),
301 'wl_namespace' => ( $title->getNamespace() |
1 ),
302 'wl_title' => $title->getDBkey(),
303 'wl_notificationtimestamp' => null,
307 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
311 * Remove a list of titles from a user's watchlist
313 * $titles can be an array of strings or Title objects; the former
314 * is preferred, since Titles are very memory-heavy
316 * @param $titles Array of strings, or Title objects
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 );
324 if( $title instanceof Title
) {
328 'wl_user' => $this->getUser()->getId(),
329 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
330 'wl_title' => $title->getDBkey(),
337 'wl_user' => $this->getUser()->getId(),
338 'wl_namespace' => ( $title->getNamespace() |
1 ),
339 'wl_title' => $title->getDBkey(),
343 $article = new Article( $title, 0 );
344 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
349 public function submitNormal( $data ) {
352 foreach( $data as $titles ) {
353 $this->unwatchTitles( $titles );
357 if( count( $removed ) > 0 ) {
358 $this->successMessage
= wfMessage(
359 'watchlistedit-normal-done',
360 $this->getLang()->formatNum( count( $removed ) )
362 $this->showTitles( $removed, $this->successMessage
);
370 * Get the standard watchlist editing form
374 protected function getNormalForm(){
379 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
381 $namespace == NS_MAIN
382 ?
wfMsgHtml( 'blanknamespace' )
383 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
385 $fields['TitlesNs'.$namespace] = array(
386 'type' => 'multiselect',
387 'options' => array(),
388 'section' => "ns$namespace",
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();
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' ) );
408 * Build the label for a checkbox, with a link to the title, and various additional bits
410 * @param $title Title
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>';
419 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
420 if( $title->exists() ) {
421 $tools[] = Linker
::linkKnown(
423 wfMsgHtml( 'history_short' ),
425 array( 'action' => 'history' )
428 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
429 $tools[] = Linker
::linkKnown(
430 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
431 wfMsgHtml( 'contributions' )
435 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
437 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
441 * Get a form for editing the watchlist in "raw" mode
445 protected function getRawForm(){
446 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist() ), "\n" );
449 'type' => 'textarea',
450 'label-message' => 'watchlistedit-raw-titles',
451 'default' => $titles,
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' ) );
464 * Determine whether we are editing the watchlist, and if so, what
465 * kind of editing operation
467 * @param $request WebRequest
471 public static function getMode( $request, $par ) {
472 $mode = strtolower( $request->getVal( 'action', $par ) );
475 case self
::EDIT_CLEAR
:
476 return self
::EDIT_CLEAR
;
480 return self
::EDIT_RAW
;
483 case self
::EDIT_NORMAL
:
484 return self
::EDIT_NORMAL
;
492 * Build a set of links for convenient navigation
493 * between watchlist viewing and editing modes
495 * @param $unused Unused
498 public static function buildTools( $unused ) {
503 'view' => array( 'Watchlist', false ),
504 'edit' => array( 'EditWatchlist', false ),
505 'raw' => array( 'EditWatchlist', 'raw' ),
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}" )
514 return Html
::rawElement( 'span',
515 array( 'class' => 'mw-watchlist-toollinks' ),
516 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
521 class WatchlistEditor
extends SpecialEditWatchlist
{}
524 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
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 ) );