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
;
35 $out = $this->getOutput();
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' ),
44 array( 'returnto' => $this->getTitle()->getPrefixedText() )
46 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
52 array( 'parseinline', 'replaceafter' ),
53 $this->getUser()->getName(),
54 SpecialEditWatchlist
::buildTools( null )
56 $out->setSubtitle( $sub );
58 # B/C: $mode used to be waaay down the parameter list, and the first parameter
60 if( $mode instanceof User
){
61 $args = func_get_args();
62 if( count( $args >= 4 ) ){
66 $mode = self
::getMode( $this->getRequest(), $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.
74 $out->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
75 $form = $this->getRawForm();
77 $out->addHTML( $this->successMessage
);
82 case self
::EDIT_NORMAL
:
84 $out->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
85 $form = $this->getNormalForm();
87 $out->addHTML( $this->successMessage
);
95 * Extract a list of titles from a blob of text, returning
96 * (prefixed) strings; unwatchable titles are ignored
101 private function extractTitles( $list ) {
103 $list = explode( "\n", trim( $list ) );
104 if( !is_array( $list ) ) {
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();
116 return array_unique( $titles );
119 public function submitRaw( $data ){
120 $wanted = $this->extractTitles( $data['Titles'] );
121 $current = $this->getWatchlist();
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();
130 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
131 $this->successMessage
= wfMessage( 'watchlistedit-raw-done' )->parse();
136 if( count( $toWatch ) > 0 ) {
137 $this->successMessage
.= wfMessage(
138 'watchlistedit-raw-added',
139 $this->getLang()->formatNum( count( $toWatch ) )
141 $this->showTitles( $toWatch, $this->successMessage
);
144 if( count( $toUnwatch ) > 0 ) {
145 $this->successMessage
.= wfMessage(
146 'watchlistedit-raw-removed',
147 $this->getLang()->formatNum( count( $toUnwatch ) )
149 $this->showTitles( $toUnwatch, $this->successMessage
);
152 $this->clearWatchlist();
153 $this->getUser()->invalidateCache();
154 $this->successMessage
.= wfMessage(
155 'watchlistedit-raw-removed',
156 $this->getLang()->formatNum( count( $current ) )
158 $this->showTitles( $current, $this->successMessage
);
164 * Print out a list of linked titles
166 * $titles can be an array of strings or Title objects; the former
167 * is preferred, since Titles are very memory-heavy
169 * @param $titles array of strings, or Title objects
170 * @param $output String
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 );
180 if( $title instanceof Title
) {
181 $batch->addObj( $title );
182 $batch->addObj( $title->getTalkPage() );
186 // Print out the list
188 foreach( $titles as $title ) {
189 if( !$title instanceof Title
) {
190 $title = Title
::newFromText( $title );
192 if( $title instanceof Title
) {
194 . Linker
::link( $title )
195 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
199 $output .= "</ul>\n";
203 * Prepare a list of titles on a user's watchlist (excluding talk pages)
204 * and return an array of (prefixed) strings
208 private function getWatchlist() {
210 $dbr = wfGetDB( DB_MASTER
);
215 'wl_user' => $this->getUser()->getId(),
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();
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
237 private function getWatchlistInfo() {
239 $dbr = wfGetDB( DB_MASTER
);
242 array( 'watchlist', 'page' ),
251 array( 'wl_user' => $this->getUser()->getId() ),
253 array( 'ORDER BY' => 'wl_namespace, wl_title' ),
254 array( 'page' => array(
256 'wl_namespace = page_namespace AND wl_title = page_title'
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
);
269 $cache->addBadLinkObj( $title );
272 if( !$title->isTalkPage() ) {
273 $titles[$row->wl_namespace
][$row->wl_title
] = $row->page_is_redirect
;
282 * Remove all titles from a user's watchlist
284 private function clearWatchlist() {
285 $dbw = wfGetDB( DB_MASTER
);
288 array( 'wl_user' => $this->getUser()->getId() ),
294 * Add a list of titles to a user's watchlist
296 * $titles can be an array of strings or Title objects; the former
297 * is preferred, since Titles are very memory-heavy
299 * @param $titles Array of strings, or Title objects
301 private function watchTitles( $titles ) {
302 $dbw = wfGetDB( DB_MASTER
);
304 foreach( $titles as $title ) {
305 if( !$title instanceof Title
) {
306 $title = Title
::newFromText( $title );
308 if( $title instanceof Title
) {
310 'wl_user' => $this->getUser()->getId(),
311 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
312 'wl_title' => $title->getDBkey(),
313 'wl_notificationtimestamp' => null,
316 'wl_user' => $this->getUser()->getId(),
317 'wl_namespace' => ( $title->getNamespace() |
1 ),
318 'wl_title' => $title->getDBkey(),
319 'wl_notificationtimestamp' => null,
323 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
327 * Remove a list of titles from a user's watchlist
329 * $titles can be an array of strings or Title objects; the former
330 * is preferred, since Titles are very memory-heavy
332 * @param $titles Array of strings, or Title objects
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 );
340 if( $title instanceof Title
) {
344 'wl_user' => $this->getUser()->getId(),
345 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
346 'wl_title' => $title->getDBkey(),
353 'wl_user' => $this->getUser()->getId(),
354 'wl_namespace' => ( $title->getNamespace() |
1 ),
355 'wl_title' => $title->getDBkey(),
359 $article = new Article( $title, 0 );
360 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
365 public function submitNormal( $data ) {
368 foreach( $data as $titles ) {
369 $this->unwatchTitles( $titles );
373 if( count( $removed ) > 0 ) {
374 $this->successMessage
= wfMessage(
375 'watchlistedit-normal-done',
376 $this->getLang()->formatNum( count( $removed ) )
378 $this->showTitles( $removed, $this->successMessage
);
386 * Get the standard watchlist editing form
390 protected function getNormalForm(){
395 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
397 $namespace == NS_MAIN
398 ?
wfMsgHtml( 'blanknamespace' )
399 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
401 $fields['TitlesNs'.$namespace] = array(
402 'type' => 'multiselect',
403 'options' => array(),
404 'section' => "ns$namespace",
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();
414 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
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' ) );
424 * Build the label for a checkbox, with a link to the title, and various additional bits
426 * @param $title Title
427 * @param $redirect bool
430 private function buildRemoveLine( $title, $redirect ) {
431 $link = Linker
::link( $title );
433 $link = '<span class="watchlistredir">' . $link . '</span>';
435 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
436 if( $title->exists() ) {
437 $tools[] = Linker
::linkKnown(
439 wfMsgHtml( 'history_short' ),
441 array( 'action' => 'history' )
444 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
445 $tools[] = Linker
::linkKnown(
446 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
447 wfMsgHtml( 'contributions' )
451 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $this->getSkin() ) );
453 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
457 * Get a form for editing the watchlist in "raw" mode
461 protected function getRawForm(){
462 $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist() ), "\n" );
465 'type' => 'textarea',
466 'label-message' => 'watchlistedit-raw-titles',
467 'default' => $titles,
470 $form = new HTMLForm( $fields, $this->getContext() );
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' ) );
480 * Determine whether we are editing the watchlist, and if so, what
481 * kind of editing operation
483 * @param $request WebRequest
487 public static function getMode( $request, $par ) {
488 $mode = strtolower( $request->getVal( 'action', $par ) );
491 case self
::EDIT_CLEAR
:
492 return self
::EDIT_CLEAR
;
496 return self
::EDIT_RAW
;
499 case self
::EDIT_NORMAL
:
500 return self
::EDIT_NORMAL
;
508 * Build a set of links for convenient navigation
509 * between watchlist viewing and editing modes
511 * @param $unused Unused
514 public static function buildTools( $unused ) {
519 'view' => array( 'Watchlist', false ),
520 'edit' => array( 'EditWatchlist', false ),
521 'raw' => array( 'EditWatchlist', 'raw' ),
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}" )
530 return Html
::rawElement( 'span',
531 array( 'class' => 'mw-watchlist-toollinks' ),
532 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
537 class WatchlistEditor
extends SpecialEditWatchlist
{}
540 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
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 ) );