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 ) {
33 $out = $this->getOutput();
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' ),
42 array( 'returnto' => $this->getTitle()->getPrefixedText() )
44 $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
49 throw new ReadOnlyError
;
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 $haveInvalidNamespaces = false;
380 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
381 if ( $namespace < 0 ) {
382 $haveInvalidNamespaces = true;
386 $namespace == NS_MAIN
387 ?
wfMsgHtml( 'blanknamespace' )
388 : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
390 $fields['TitlesNs'.$namespace] = array(
391 'type' => 'multiselect',
392 'options' => array(),
393 'section' => "ns$namespace",
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();
402 if ( $haveInvalidNamespaces ) {
403 wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
404 $this->getContext()->getUser()->cleanupWatchlist();
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' ) );
417 * Build the label for a checkbox, with a link to the title, and various additional bits
419 * @param $title Title
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>';
428 $tools[] = Linker
::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
429 if( $title->exists() ) {
430 $tools[] = Linker
::linkKnown(
432 wfMsgHtml( 'history_short' ),
434 array( 'action' => 'history' )
437 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
438 $tools[] = Linker
::linkKnown(
439 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
440 wfMsgHtml( 'contributions' )
444 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
446 return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
450 * Get a form for editing the watchlist in "raw" mode
454 protected function getRawForm(){
455 $titles = implode( $this->getWatchlist(), "\n" );
458 'type' => 'textarea',
459 'label-message' => 'watchlistedit-raw-titles',
460 'default' => $titles,
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' ) );
473 * Determine whether we are editing the watchlist, and if so, what
474 * kind of editing operation
476 * @param $request WebRequest
480 public static function getMode( $request, $par ) {
481 $mode = strtolower( $request->getVal( 'action', $par ) );
484 case self
::EDIT_CLEAR
:
485 return self
::EDIT_CLEAR
;
489 return self
::EDIT_RAW
;
492 case self
::EDIT_NORMAL
:
493 return self
::EDIT_NORMAL
;
501 * Build a set of links for convenient navigation
502 * between watchlist viewing and editing modes
504 * @param $unused Unused
507 public static function buildTools( $unused ) {
512 'view' => array( 'Watchlist', false ),
513 'edit' => array( 'EditWatchlist', false ),
514 'raw' => array( 'EditWatchlist', 'raw' ),
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}" )
523 return Html
::rawElement( 'span',
524 array( 'class' => 'mw-watchlist-toollinks' ),
525 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
530 class WatchlistEditor
extends SpecialEditWatchlist
{}
533 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
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 ) );