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 $offset = 0;
48 protected $successMessage;
52 private $badItems = array();
54 public function __construct(){
55 parent
::__construct( 'EditWatchlist' );
59 * Main execution point
63 public function execute( $mode ) {
66 $out = $this->getOutput();
68 # Anons don't get a watchlist
69 if( $this->getUser()->isAnon() ) {
70 $out->setPageTitle( $this->msg( 'watchnologin' ) );
71 $llink = Linker
::linkKnown(
72 SpecialPage
::getTitleFor( 'Userlogin' ),
73 $this->msg( 'loginreqlink' )->escaped(),
75 array( 'returnto' => $this->getTitle()->getPrefixedText() )
77 $out->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
81 $this->checkPermissions();
83 $this->outputHeader();
85 $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
86 )->rawParams( SpecialEditWatchlist
::buildTools( null ) ) );
88 # B/C: $mode used to be waaay down the parameter list, and the first parameter
90 if( $mode instanceof User
){
91 $args = func_get_args();
92 if( count( $args >= 4 ) ){
96 $mode = self
::getMode( $this->getRequest(), $mode );
97 list( $this->limit
, $this->offset
) = $this->getRequest()->getLimitOffset( 50, 'wllimit' );
100 case self
::EDIT_CLEAR
:
101 // The "Clear" link scared people too much.
102 // Pass on to the raw editor, from which it's very easy to clear.
105 $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
106 $form = $this->getRawForm();
108 $out->addHTML( $this->successMessage
);
109 $out->returnToMain();
113 case self
::EDIT_NORMAL
:
115 $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
116 $out->addModules( 'mediawiki.special.editWatchlist' );
117 $form = $this->getNormalForm();
119 $out->addHTML( $this->successMessage
);
120 $out->returnToMain();
121 } elseif ( $this->toc
!== false ) {
122 $out->prependHTML( $this->toc
);
129 * Extract a list of titles from a blob of text, returning
130 * (prefixed) strings; unwatchable titles are ignored
132 * @param $list String
135 private function extractTitles( $list ) {
137 $list = explode( "\n", trim( $list ) );
138 if( !is_array( $list ) ) {
141 foreach( $list as $text ) {
142 $text = trim( $text );
143 if( strlen( $text ) > 0 ) {
144 $title = Title
::newFromText( $text );
145 if( $title instanceof Title
&& $title->isWatchable() ) {
146 $titles[] = $title->getPrefixedText();
150 return array_unique( $titles );
153 public function submitRaw( $data ){
154 $wanted = $this->extractTitles( $data['Titles'] );
155 $current = $this->getWatchlist();
157 if( count( $wanted ) > 0 ) {
158 $toWatch = array_diff( $wanted, $current );
159 $toUnwatch = array_diff( $current, $wanted );
160 $this->watchTitles( $toWatch );
161 $this->unwatchTitles( $toUnwatch );
162 $this->getUser()->invalidateCache();
164 if( count( $toWatch ) > 0 ||
count( $toUnwatch ) > 0 ){
165 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
170 if( count( $toWatch ) > 0 ) {
171 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-added'
172 )->numParams( count( $toWatch ) )->parse();
173 $this->showTitles( $toWatch, $this->successMessage
);
176 if( count( $toUnwatch ) > 0 ) {
177 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
178 )->numParams( count( $toUnwatch ) )->parse();
179 $this->showTitles( $toUnwatch, $this->successMessage
);
182 $this->clearWatchlist();
183 $this->getUser()->invalidateCache();
185 if( count( $current ) > 0 ){
186 $this->successMessage
= $this->msg( 'watchlistedit-raw-done' )->parse();
191 $this->successMessage
.= ' ' . $this->msg( 'watchlistedit-raw-removed'
192 )->numParams( count( $current ) )->parse();
193 $this->showTitles( $current, $this->successMessage
);
199 * Print out a list of linked titles
201 * $titles can be an array of strings or Title objects; the former
202 * is preferred, since Titles are very memory-heavy
204 * @param $titles array of strings, or Title objects
205 * @param $output String
207 private function showTitles( $titles, &$output ) {
208 $talk = $this->msg( 'talkpagelinktext' )->escaped();
209 // Do a batch existence check
210 $batch = new LinkBatch();
211 foreach( $titles as $title ) {
212 if( !$title instanceof Title
) {
213 $title = Title
::newFromText( $title );
215 if( $title instanceof Title
) {
216 $batch->addObj( $title );
217 $batch->addObj( $title->getTalkPage() );
221 // Print out the list
223 foreach( $titles as $title ) {
224 if( !$title instanceof Title
) {
225 $title = Title
::newFromText( $title );
227 if( $title instanceof Title
) {
229 . Linker
::link( $title )
230 . ' (' . Linker
::link( $title->getTalkPage(), $talk )
234 $output .= "</ul>\n";
238 * Prepare a list of titles on a user's watchlist (excluding talk pages)
239 * and return an array of (prefixed) strings
243 private function getWatchlist() {
245 $dbr = wfGetDB( DB_MASTER
);
249 'wl_namespace', 'wl_title'
251 'wl_user' => $this->getUser()->getId(),
255 if( $res->numRows() > 0 ) {
256 foreach ( $res as $row ) {
257 $title = Title
::makeTitleSafe( $row->wl_namespace
, $row->wl_title
);
258 if ( $this->checkTitle( $title, $row->wl_namespace
, $row->wl_title
)
259 && !$title->isTalkPage()
261 $list[] = $title->getPrefixedText();
266 $this->cleanupWatchlist();
271 * select from DB watchlist items watched by the current user
272 * @return q query result of watchlist items watched by the current user
274 private function selectWatchListInfo( ) {
276 'ORDER BY' => array( 'wl_namespace', 'wl_title' ),
277 'LIMIT' => intval( $this->limit
),
278 'OFFSET' => intval( $this->offset
)
280 $dbr = wfGetDB( DB_MASTER
);
281 //query only non talk namespaces.
282 $nonTalkNamespaces = MWNamespace
::getContentNamespaces();
284 array( 'watchlist' ),
285 array( 'wl_namespace', 'wl_title' ),
286 array( 'wl_user' => $this->getUser()->getId(), 'wl_namespace' => $nonTalkNamespaces ),
295 * Get a list of titles on a user's watchlist, excluding talk pages,
296 * and return as a two-dimensional array with namespace and title.
298 * @param $watchedItems rows of watched items
301 private function getWatchlistInfo( $watchedItems ) {
303 $lb = new LinkBatch();
304 foreach ( $watchedItems as $row ) {
305 $lb->add( $row->wl_namespace
, $row->wl_title
);
306 $titles[$row->wl_namespace
][$row->wl_title
] = 1;
314 * Validates watchlist entry
316 * @param Title $title
317 * @param int $namespace
318 * @param String $dbKey
319 * @return bool: Whether this item is valid
321 private function checkTitle( $title, $namespace, $dbKey ) {
323 && ( $title->isExternal()
324 ||
$title->getNamespace() < 0
327 $title = false; // unrecoverable
330 ||
$title->getNamespace() != $namespace
331 ||
$title->getDBkey() != $dbKey
333 $this->badItems
[] = array( $title, $namespace, $dbKey );
339 * Attempts to clean up broken items
341 private function cleanupWatchlist() {
342 if( !count( $this->badItems
) ) {
343 return; //nothing to do
345 $dbw = wfGetDB( DB_MASTER
);
346 $user = $this->getUser();
347 foreach ( $this->badItems
as $row ) {
348 list( $title, $namespace, $dbKey ) = $row;
349 wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, "
350 . ( $title ?
'cleaning up' : 'deleting' ) . ".\n"
353 $dbw->delete( 'watchlist',
355 'wl_user' => $user->getId(),
356 'wl_namespace' => $namespace,
357 'wl_title' => $dbKey,
362 // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
364 $user->addWatch( $title );
370 * Remove all titles from a user's watchlist
372 private function clearWatchlist() {
373 $dbw = wfGetDB( DB_MASTER
);
376 array( 'wl_user' => $this->getUser()->getId() ),
382 * Add a list of titles to a user's watchlist
384 * $titles can be an array of strings or Title objects; the former
385 * is preferred, since Titles are very memory-heavy
387 * @param $titles Array of strings, or Title objects
389 private function watchTitles( $titles ) {
390 $dbw = wfGetDB( DB_MASTER
);
392 foreach( $titles as $title ) {
393 if( !$title instanceof Title
) {
394 $title = Title
::newFromText( $title );
396 if( $title instanceof Title
) {
398 'wl_user' => $this->getUser()->getId(),
399 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
400 'wl_title' => $title->getDBkey(),
401 'wl_notificationtimestamp' => null,
404 'wl_user' => $this->getUser()->getId(),
405 'wl_namespace' => ( $title->getNamespace() |
1 ),
406 'wl_title' => $title->getDBkey(),
407 'wl_notificationtimestamp' => null,
411 $dbw->insert( 'watchlist', $rows, __METHOD__
, 'IGNORE' );
415 * Remove a list of titles from a user's watchlist
417 * $titles can be an array of strings or Title objects; the former
418 * is preferred, since Titles are very memory-heavy
420 * @param $titles Array of strings, or Title objects
422 private function unwatchTitles( $titles ) {
423 $dbw = wfGetDB( DB_MASTER
);
424 foreach( $titles as $title ) {
425 if( !$title instanceof Title
) {
426 $title = Title
::newFromText( $title );
428 if( $title instanceof Title
) {
432 'wl_user' => $this->getUser()->getId(),
433 'wl_namespace' => ( $title->getNamespace() & ~
1 ),
434 'wl_title' => $title->getDBkey(),
441 'wl_user' => $this->getUser()->getId(),
442 'wl_namespace' => ( $title->getNamespace() |
1 ),
443 'wl_title' => $title->getDBkey(),
447 $page = WikiPage
::factory( $title );
448 wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
453 public function submitNormal( $data ) {
456 foreach( $data as $titles ) {
457 $this->unwatchTitles( $titles );
458 $removed = array_merge( $removed, $titles );
461 if( count( $removed ) > 0 ) {
462 $this->successMessage
= $this->msg( 'watchlistedit-normal-done'
463 )->numParams( count( $removed ) )->parse();
464 $this->showTitles( $removed, $this->successMessage
);
472 * Get the standard watchlist editing form
476 protected function getNormalForm(){
481 $watchedItems = $this->selectWatchListInfo();
482 $rowNum = $watchedItems->numRows();
483 foreach ( $this->getWatchlistInfo( $watchedItems ) as $namespace => $pages ) {
484 if ( $namespace >= 0 ) {
485 $fields['TitlesNs'.$namespace] = array(
486 'class' => 'EditWatchlistCheckboxSeriesField',
487 'options' => array(),
488 'section' => "ns$namespace",
492 foreach ( array_keys( $pages ) as $dbkey ) {
493 $title = Title
::makeTitleSafe( $namespace, $dbkey );
494 if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
495 $text = $this->buildRemoveLine( $title );
496 $fields['TitlesNs'.$namespace]['options'][$text] = htmlspecialchars( $title->getPrefixedText() );
501 $this->cleanupWatchlist();
503 if ( count( $fields ) > 1 && $count > 30 ) {
504 $this->toc
= Linker
::tocIndent();
506 foreach( $fields as $key => $data ) {
508 # strip out the 'ns' prefix from the section name:
509 $ns = substr( $data['section'], 2 );
511 $nsText = ($ns == NS_MAIN
)
512 ?
$this->msg( 'blanknamespace' )->escaped()
513 : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
514 $this->toc
.= Linker
::tocLine( "editwatchlist-{$data['section']}", $nsText,
515 $this->getLanguage()->formatNum( ++
$tocLength ), 1 ) . Linker
::tocLineEnd();
517 $this->toc
= Linker
::tocList( $this->toc
);
522 $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
523 $form->setTitle( $this->getTitle() );
524 $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
525 $form->setSubmitID( 'watchlistedit-submit' );
526 # Used message keys: 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
527 $form->setSubmitTooltip('watchlistedit-normal-submit');
528 $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
529 $paging = '<p>' . $this->getLanguage()->viewPrevNext( $this->getTitle(), $this->offset
,
530 $this->limit
, array(), ( $rowNum < $this->limit
) ) . '</p>';
531 $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() . $paging );
532 $form->setSubmitCallback( array( $this, 'submitNormal' ) );
537 * Build the label for a checkbox, with a link to the title, and various additional bits
539 * @param $title Title
542 private function buildRemoveLine( $title ) {
543 $link = Linker
::link( $title );
544 if( $title->isRedirect() ) {
545 // Linker already makes class mw-redirect, so this is redundant
546 $link = '<span class="watchlistredir">' . $link . '</span>';
548 $tools[] = Linker
::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
549 if( $title->exists() ) {
550 $tools[] = Linker
::linkKnown(
552 $this->msg( 'history_short' )->escaped(),
554 array( 'action' => 'history' )
557 if( $title->getNamespace() == NS_USER
&& !$title->isSubpage() ) {
558 $tools[] = Linker
::linkKnown(
559 SpecialPage
::getTitleFor( 'Contributions', $title->getText() ),
560 $this->msg( 'contributions' )->escaped()
564 wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
566 return '<span class="watchlist-item">' . $link . '</span>' . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
570 * Get a form for editing the watchlist in "raw" mode
574 protected function getRawForm(){
575 $titles = implode( $this->getWatchlist(), "\n" );
578 'type' => 'textarea',
579 'label-message' => 'watchlistedit-raw-titles',
580 'default' => $titles,
583 $form = new HTMLForm( $fields, $this->getContext() );
584 $form->setTitle( $this->getTitle( 'raw' ) );
585 $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
586 # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
587 $form->setSubmitTooltip('watchlistedit-raw-submit');
588 $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
589 $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
590 $form->setSubmitCallback( array( $this, 'submitRaw' ) );
595 * Determine whether we are editing the watchlist, and if so, what
596 * kind of editing operation
598 * @param $request WebRequest
602 public static function getMode( $request, $par ) {
603 $mode = strtolower( $request->getVal( 'action', $par ) );
606 case self
::EDIT_CLEAR
:
607 return self
::EDIT_CLEAR
;
611 return self
::EDIT_RAW
;
614 case self
::EDIT_NORMAL
:
615 return self
::EDIT_NORMAL
;
623 * Build a set of links for convenient navigation
624 * between watchlist viewing and editing modes
629 public static function buildTools( $unused ) {
634 'view' => array( 'Watchlist', false ),
635 'edit' => array( 'EditWatchlist', false ),
636 'raw' => array( 'EditWatchlist', 'raw' ),
638 foreach( $modes as $mode => $arr ) {
639 // can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
640 $tools[] = Linker
::linkKnown(
641 SpecialPage
::getTitleFor( $arr[0], $arr[1] ),
642 wfMsgHtml( "watchlisttools-{$mode}" )
645 return Html
::rawElement( 'span',
646 array( 'class' => 'mw-watchlist-toollinks' ),
647 wfMsg( 'parentheses', $wgLang->pipeList( $tools ) ) );
652 class WatchlistEditor
extends SpecialEditWatchlist
{}
655 * Extend HTMLForm purely so we can have a more sane way of getting the section headers
657 class EditWatchlistNormalHTMLForm
extends HTMLForm
{
658 public function getLegend( $namespace ){
659 $namespace = substr( $namespace, 2 );
660 return $namespace == NS_MAIN
661 ?
$this->msg( 'blanknamespace' )->escaped()
662 : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
664 public function getBody() {
665 return $this->displaySection( $this->mFieldTree
, '', 'editwatchlist-' );
669 class EditWatchlistCheckboxSeriesField
extends HTMLMultiSelectField
{
671 * HTMLMultiSelectField throws validation errors if we get input data
672 * that doesn't match the data set in the form setup. This causes
673 * problems if something gets removed from the watchlist while the
674 * form is open (bug 32126), but we know that invalid items will
675 * be harmless so we can override it here.
677 * @param $value String the value the field was submitted with
678 * @param $alldata Array the data collected from the form
679 * @return Mixed Bool true on success, or String error to display.
681 function validate( $value, $alldata ) {
682 // Need to call into grandparent to be a good citizen. :)
683 return HTMLFormField
::validate( $value, $alldata );