3 * @defgroup Watchlist Users watchlist handling
7 * Implements Special:EditWatchlist
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup SpecialPage
30 * Provides the UI through which users can perform editing
31 * operations on their watchlist
33 * @ingroup SpecialPage
35 * @author Rob Church <robchur@gmail.com>
37 class SpecialEditWatchlist
extends UnlistedSpecialPage
{
44 const EDIT_NORMAL
= 3;
46 protected $successMessage;
50 private $badItems = array();
52 public function __construct(){
53 parent
::__construct( 'EditWatchlist' );
57 * Main execution point
61 public function execute( $mode ) {
64 $out = $this->getOutput();
66 # Anons don't get a watchlist
67 if( $this->getUser()->isAnon() ) {
68 $out->setPageTitle( $this->msg( 'watchnologin' ) );
69 $llink = Linker
::linkKnown(
70 SpecialPage
::getTitleFor( 'Userlogin' ),
71 $this->msg( 'loginreqlink' )->escaped(),
73 array( 'returnto' => $this->getTitle()->getPrefixedText() )
75 $out->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
79 $this->checkPermissions();
81 $this->outputHeader();
83 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
84 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
86 # B/C: $mode used to be waaay down the parameter list, and the first parameter
88 if( $mode instanceof User
){
89 $args = func_get_args();
90 if( count( $args >= 4 ) ){
94 $mode = self
::getMode( $this->getRequest(), $mode );
97 case self
::EDIT_CLEAR
:
98 // The "Clear" link scared people too much.
99 // Pass on to the raw editor, from which it's very easy to clear.
102 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
103 $form = $this->getRawForm();
105 $out->addHTML( $this->successMessage
);
106 $out->returnToMain();
110 case self
::EDIT_NORMAL
:
112 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
113 $form = $this->getNormalForm();
115 $out->addHTML( $this->successMessage
);
116 $out->returnToMain();
117 } elseif ( $this->toc
!== false ) {
118 $out->prependHTML( $this->toc
);
125 * Extract a list of titles from a blob of text, returning
126 * (prefixed) strings; unwatchable titles are ignored
128 * @param $list String
131 private function extractTitles( $list ) {
132 $list = explode( "\n", trim( $list ) );
133 if( !is_array( $list ) ) {
137 foreach( $list as $text ) {
138 $text = trim( $text );
139 if( strlen( $text ) > 0 ) {
140 $title = Title
::newFromText( $text );
141 if( $title instanceof Title
&& $title->isWatchable() ) {
147 GenderCache
::singleton()->doTitlesArray( $titles );
150 foreach( $titles as $title ) {
151 $list[] = $title->getPrefixedText();
153 return array_unique( $list );
156 public function submitRaw( $data ){
157 $wanted = $this->extractTitles( $data['Titles'] );
158 $current = $this->getWatchlist();
160 if( count( $wanted ) > 0 ) {
161 $toWatch = array_diff( $wanted, $current );
162 $toUnwatch = array_diff( $current, $wanted );
163 $this->watchTitles( $toWatch );
164 $this->unwatchTitles( $toUnwatch );
165 $this->getUser()->invalidateCache();
167 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
168 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
173 if( count( $toWatch ) > 0 ) {
174 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added'
175 )->numParams( count( $toWatch ) )->parse();
176 $this->showTitles( $toWatch, $this->successMessage
);
179 if( count( $toUnwatch ) > 0 ) {
180 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
181 )->numParams( count( $toUnwatch ) )->parse();
182 $this->showTitles( $toUnwatch, $this->successMessage
);
185 $this->clearWatchlist();
186 $this->getUser()->invalidateCache();
188 if( count( $current ) > 0 ){
189 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
194 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
195 )->numParams( count( $current ) )->parse();
196 $this->showTitles( $current, $this->successMessage
);
202 * Print out a list of linked titles
204 * $titles can be an array of strings or Title objects; the former
205 * is preferred, since Titles are very memory-heavy
207 * @param $titles array of strings, or Title objects
208 * @param $output String
210 private function showTitles( $titles, &$output ) {
211 $talk = $this->msg( 'talkpagelinktext' )->escaped();
212 // Do a batch existence check
213 $batch = new LinkBatch();
214 foreach( $titles as $title ) {
215 if( !$title instanceof Title
) {
216 $title = Title
::newFromText( $title );
218 if( $title instanceof Title
) {
219 $batch->addObj( $title );
220 $batch->addObj( $title->getTalkPage() );
224 // Print out the list
226 foreach( $titles as $title ) {
227 if( !$title instanceof Title
) {
228 $title = Title
::newFromText( $title );
230 if( $title instanceof Title
) {
232 . Linker
::link( $title )
233 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
237 $output .= "</ul>\n";
241 * Prepare a list of titles on a user's watchlist (excluding talk pages)
242 * and return an array of (prefixed) strings
246 private function getWatchlist() {
248 $dbr = wfGetDB( DB_MASTER
);
252 'wl_namespace', 'wl_title'
254 'wl_user' => $this->getUser()->getId(),
258 if( $res->numRows() > 0 ) {
260 foreach ( $res as $row ) {
261 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
262 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
263 && !$title->isTalkPage()
270 GenderCache
::singleton()->doTitlesArray( $titles );
272 foreach( $titles as $title ) {
273 $list[] = $title->getPrefixedText();
276 $this->cleanupWatchlist();
281 * Get a list of titles on a user's watchlist, excluding talk pages,
282 * and return as a two-dimensional array with namespace and title.
286 private function getWatchlistInfo() {
288 $dbr = wfGetDB( DB_MASTER
);
291 array( 'watchlist' ),
292 array( 'wl_namespace', 'wl_title' ),
293 array( 'wl_user' => $this->getUser()->getId() ),
295 array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
298 $lb = new LinkBatch();
299 foreach ( $res as $row ) {
300 $lb->add( $row->wl_namespace
, $row->wl_title
);
301 if ( !MWNamespace
::isTalk( $row->wl_namespace
) ) {
302 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
311 * Validates watchlist entry
313 * @param Title $title
314 * @param int $namespace
315 * @param String $dbKey
316 * @return bool: Whether this item is valid
318 private function checkTitle( $title, $namespace, $dbKey ) {
320 && ( $title->isExternal()
321 ||
$title->getNamespace() < 0
324 $title = false; // unrecoverable
327 ||
$title->getNamespace() != $namespace
328 ||
$title->getDBkey() != $dbKey
330 $this->badItems
[] = array( $title, $namespace, $dbKey );
336 * Attempts to clean up broken items
338 private function cleanupWatchlist() {
339 if( !count( $this->badItems
) ) {
340 return; //nothing to do
342 $dbw = wfGetDB( DB_MASTER
);
343 $user = $this->getUser();
344 foreach ( $this->badItems
as $row ) {
345 list( $title, $namespace, $dbKey ) = $row;
346 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, "
347 . ( $title ?
'cleaning up' : 'deleting' ) . ".\n"
350 $dbw->delete( 'watchlist',
352 'wl_user' => $user->getId(),
353 'wl_namespace' => $namespace,
354 'wl_title' => $dbKey,
359 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
361 $user->addWatch( $title );
367 * Remove all titles from a user's watchlist
369 private function clearWatchlist() {
370 $dbw = wfGetDB( DB_MASTER
);
373 array( 'wl_user' => $this->getUser()->getId() ),
379 * Add a list of titles to a user's watchlist
381 * $titles can be an array of strings or Title objects; the former
382 * is preferred, since Titles are very memory-heavy
384 * @param $titles Array of strings, or Title objects
386 private function watchTitles( $titles ) {
387 $dbw = wfGetDB( DB_MASTER
);
389 foreach( $titles as $title ) {
390 if( !$title instanceof Title
) {
391 $title = Title
::newFromText( $title );
393 if( $title instanceof Title
) {
395 'wl_user' => $this->getUser()->getId(),
396 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
397 'wl_title' => $title->getDBkey(),
398 'wl_notificationtimestamp' => null,
401 'wl_user' => $this->getUser()->getId(),
402 'wl_namespace' => ( $title->getNamespace() |
1 ),
403 'wl_title' => $title->getDBkey(),
404 'wl_notificationtimestamp' => null,
408 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
412 * Remove a list of titles from a user's watchlist
414 * $titles can be an array of strings or Title objects; the former
415 * is preferred, since Titles are very memory-heavy
417 * @param $titles Array of strings, or Title objects
419 private function unwatchTitles( $titles ) {
420 $dbw = wfGetDB( DB_MASTER
);
421 foreach( $titles as $title ) {
422 if( !$title instanceof Title
) {
423 $title = Title
::newFromText( $title );
425 if( $title instanceof Title
) {
429 'wl_user' => $this->getUser()->getId(),
430 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
431 'wl_title' => $title->getDBkey(),
438 'wl_user' => $this->getUser()->getId(),
439 'wl_namespace' => ( $title->getNamespace() |
1 ),
440 'wl_title' => $title->getDBkey(),
444 $page = WikiPage
::factory( $title );
445 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
450 public function submitNormal( $data ) {
453 foreach( $data as $titles ) {
454 $this->unwatchTitles( $titles );
455 $removed = array_merge( $removed, $titles );
458 if( count( $removed ) > 0 ) {
459 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
460 )->numParams( count( $removed ) )->parse();
461 $this->showTitles( $removed, $this->successMessage
);
469 * Get the standard watchlist editing form
473 protected function getNormalForm(){
479 foreach( $this->getWatchlistInfo() as $namespace => $pages ){
480 if ( $namespace >= 0 ) {
481 $fields['TitlesNs'.$namespace] = array(
482 'class' => 'EditWatchlistCheckboxSeriesField',
483 'options' => array(),
484 'section' => "ns$namespace",
488 foreach( array_keys( $pages ) as $dbkey ){
489 $title = Title
::makeTitleSafe( $namespace, $dbkey );
490 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
491 $text = $this->buildRemoveLine( $title );
492 $fields['TitlesNs'.$namespace]['options'][$text] = htmlspecialchars( $title->getPrefixedText() );
497 $this->cleanupWatchlist();
499 if ( count( $fields ) > 1 && $count > 30 ) {
500 $this->toc
= Linker
::tocIndent();
502 foreach( $fields as $key => $data ) {
504 # strip out the 'ns' prefix from the section name:
505 $ns = substr( $data['section'], 2 );
507 $nsText = ($ns == NS_MAIN
)
508 ?
$this->msg( 'blanknamespace' )->escaped()
509 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
510 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
511 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
513 $this->toc
= Linker
::tocList( $this->toc
);
518 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
519 $form->setTitle( $this->getTitle() );
520 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
521 # Used message keys: 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
522 $form->setSubmitTooltip('watchlistedit-normal-submit');
523 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
524 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
525 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
530 * Build the label for a checkbox, with a link to the title, and various additional bits
532 * @param $title Title
535 private function buildRemoveLine( $title ) {
536 $link = Linker
::link( $title );
537 if( $title->isRedirect() ) {
538 // Linker already makes class mw-redirect, so this is redundant
539 $link = '<span class="watchlistredir">' . $link . '</span>';
541 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
542 if( $title->exists() ) {
543 $tools[] = Linker
::linkKnown(
545 $this->msg( 'history_short' )->escaped(),
547 array( 'action' => 'history' )
550 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
551 $tools[] = Linker
::linkKnown(
552 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
553 $this->msg( 'contributions' )->escaped()
557 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
559 return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
563 * Get a form for editing the watchlist in "raw" mode
567 protected function getRawForm(){
568 $titles = implode( $this->getWatchlist(), "\n" );
571 'type' => 'textarea',
572 'label-message' => 'watchlistedit-raw-titles',
573 'default' => $titles,
576 $form = new HTMLForm( $fields, $this->getContext() );
577 $form->setTitle( $this->getTitle( 'raw' ) );
578 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
579 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
580 $form->setSubmitTooltip('watchlistedit-raw-submit');
581 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
582 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
583 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
588 * Determine whether we are editing the watchlist, and if so, what
589 * kind of editing operation
591 * @param $request WebRequest
595 public static function getMode( $request, $par ) {
596 $mode = strtolower( $request->getVal( 'action', $par ) );
599 case self
::EDIT_CLEAR
:
600 return self
::EDIT_CLEAR
;
604 return self
::EDIT_RAW
;
607 case self
::EDIT_NORMAL
:
608 return self
::EDIT_NORMAL
;
616 * Build a set of links for convenient navigation
617 * between watchlist viewing and editing modes
622 public static function buildTools( $unused ) {
627 'view' => array( 'Watchlist', false ),
628 'edit' => array( 'EditWatchlist', false ),
629 'raw' => array( 'EditWatchlist', 'raw' ),
631 foreach( $modes as $mode => $arr ) {
632 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
633 $tools[] = Linker
::linkKnown(
634 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
635 wfMsgHtml( "watchlisttools-{$mode}" )
638 return Html
::rawElement( 'span',
639 array( 'class' => 'mw-watchlist-toollinks' ),
640 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
645 class WatchlistEditor
extends SpecialEditWatchlist
{}
648 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
650 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
651 public function getLegend( $namespace ){
652 $namespace = substr( $namespace, 2 );
653 return $namespace == NS_MAIN
654 ?
$this->msg( 'blanknamespace' )->escaped()
655 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
657 public function getBody() {
658 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
662 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
664 * HTMLMultiSelectField throws validation errors if we get input data
665 * that doesn't match the data set in the form setup. This causes
666 * problems if something gets removed from the watchlist while the
667 * form is open (bug 32126), but we know that invalid items will
668 * be harmless so we can override it here.
670 * @param $value String the value the field was submitted with
671 * @param $alldata Array the data collected from the form
672 * @return Mixed Bool true on success, or String error to display.
674 function validate( $value, $alldata ) {
675 // Need to call into grandparent to be a good citizen. :)
676 return HTMLFormField
::validate( $value, $alldata );