<?php
+/**
+ * @defgroup Watchlist Users watchlist handling
+ */
/**
* Provides the UI through which users can perform editing
protected $successMessage;
+ protected $toc;
+
+ private $badItems = array();
+
public function __construct(){
parent::__construct( 'EditWatchlist' );
}
* @param $mode int
*/
public function execute( $mode ) {
- global $wgUser, $wgOut, $wgRequest;
- if( wfReadOnly() ) {
- $wgOut->readOnlyPage();
- return;
- }
+ $this->setHeaders();
+
+ $out = $this->getOutput();
# Anons don't get a watchlist
- if( $wgUser->isAnon() ) {
- $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
- $llink = $this->getSkin()->linkKnown(
+ if( $this->getUser()->isAnon() ) {
+ $out->setPageTitle( $this->msg( 'watchnologin' ) );
+ $llink = Linker::linkKnown(
SpecialPage::getTitleFor( 'Userlogin' ),
- wfMsgHtml( 'loginreqlink' ),
+ $this->msg( 'loginreqlink' )->escaped(),
array(),
array( 'returnto' => $this->getTitle()->getPrefixedText() )
);
- $wgOut->addWikiMsgArray( 'watchlistanontext', array( $llink ), array( 'replaceafter' ) );
+ $out->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
return;
}
- $sub = wfMsgExt(
- 'watchlistfor2',
- array( 'parseinline', 'replaceafter' ),
- $wgUser->getName(),
- SpecialEditWatchlist::buildTools( $this->getSkin() )
- );
- $wgOut->setSubtitle( $sub );
+ $this->checkPermissions();
+
+ $this->outputHeader();
+
+ $out->addSubtitle( $this->msg( 'watchlistfor2', $this->getUser()->getName()
+ )->rawParams( SpecialEditWatchlist::buildTools( null ) ) );
# B/C: $mode used to be waaay down the parameter list, and the first parameter
# was $wgUser
$mode = $args[3];
}
}
- $mode = self::getMode( $wgRequest, $mode );
+ $mode = self::getMode( $this->getRequest(), $mode );
switch( $mode ) {
case self::EDIT_CLEAR:
// Pass on to the raw editor, from which it's very easy to clear.
case self::EDIT_RAW:
- $wgOut->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
- $form = $this->getRawForm( $wgUser );
+ $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
+ $form = $this->getRawForm();
if( $form->show() ){
- $wgOut->addHTML( $this->successMessage );
- $wgOut->returnToMain();
+ $out->addHTML( $this->successMessage );
+ $out->returnToMain();
}
break;
case self::EDIT_NORMAL:
default:
- $wgOut->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
- $form = $this->getNormalForm( $wgUser );
+ $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
+ $form = $this->getNormalForm();
if( $form->show() ){
- $wgOut->addHTML( $this->successMessage );
- $wgOut->returnToMain();
+ $out->addHTML( $this->successMessage );
+ $out->returnToMain();
+ } elseif ( $this->toc !== false ) {
+ $out->prependHTML( $this->toc );
}
break;
}
}
public function submitRaw( $data ){
- global $wgUser, $wgLang;
$wanted = $this->extractTitles( $data['Titles'] );
- $current = $this->getWatchlist( $wgUser );
+ $current = $this->getWatchlist();
if( count( $wanted ) > 0 ) {
$toWatch = array_diff( $wanted, $current );
$toUnwatch = array_diff( $current, $wanted );
- $this->watchTitles( $toWatch, $wgUser );
- $this->unwatchTitles( $toUnwatch, $wgUser );
- $wgUser->invalidateCache();
+ $this->watchTitles( $toWatch );
+ $this->unwatchTitles( $toUnwatch );
+ $this->getUser()->invalidateCache();
if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 ){
- $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
+ $this->successMessage = $this->msg( 'watchlistedit-raw-done' )->parse();
} else {
return false;
}
if( count( $toWatch ) > 0 ) {
- $this->successMessage .= wfMessage(
- 'watchlistedit-raw-added',
- $wgLang->formatNum( count( $toWatch ) )
- );
- $this->showTitles( $toWatch, $this->successMessage, $this->getSkin() );
+ $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-added'
+ )->numParams( count( $toWatch ) )->parse();
+ $this->showTitles( $toWatch, $this->successMessage );
}
if( count( $toUnwatch ) > 0 ) {
- $this->successMessage .= wfMessage(
- 'watchlistedit-raw-removed',
- $wgLang->formatNum( count( $toUnwatch ) )
- );
- $this->showTitles( $toUnwatch, $this->successMessage, $this->getSkin() );
+ $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed'
+ )->numParams( count( $toUnwatch ) )->parse();
+ $this->showTitles( $toUnwatch, $this->successMessage );
}
} else {
- $this->clearWatchlist( $wgUser );
- $wgUser->invalidateCache();
- $this->successMessage .= wfMessage(
- 'watchlistedit-raw-removed',
- $wgLang->formatNum( count( $current ) )
- );
- $this->showTitles( $current, $this->successMessage, $this->getSkin() );
+ $this->clearWatchlist();
+ $this->getUser()->invalidateCache();
+
+ if( count( $current ) > 0 ){
+ $this->successMessage = $this->msg( 'watchlistedit-raw-done' )->parse();
+ } else {
+ return false;
+ }
+
+ $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed'
+ )->numParams( count( $current ) )->parse();
+ $this->showTitles( $current, $this->successMessage );
}
return true;
}
*
* @param $titles array of strings, or Title objects
* @param $output String
- * @param $skin Skin
*/
- private function showTitles( $titles, &$output, $skin ) {
- $talk = wfMsgHtml( 'talkpagelinktext' );
+ private function showTitles( $titles, &$output ) {
+ $talk = $this->msg( 'talkpagelinktext' )->escaped();
// Do a batch existence check
$batch = new LinkBatch();
foreach( $titles as $title ) {
}
if( $title instanceof Title ) {
$output .= "<li>"
- . $skin->link( $title )
- . ' (' . $skin->link( $title->getTalkPage(), $talk )
+ . Linker::link( $title )
+ . ' (' . Linker::link( $title->getTalkPage(), $talk )
. ")</li>\n";
}
}
$output .= "</ul>\n";
}
- /**
- * Count the number of titles on a user's watchlist, excluding talk pages
- *
- * @param $user User
- * @return int
- */
- private function countWatchlist( $user ) {
- $dbr = wfGetDB( DB_MASTER );
- $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->getId() ), __METHOD__ );
- $row = $dbr->fetchObject( $res );
- return ceil( $row->count / 2 ); // Paranoia
- }
-
/**
* Prepare a list of titles on a user's watchlist (excluding talk pages)
* and return an array of (prefixed) strings
*
- * @param $user User
* @return array
*/
- private function getWatchlist( $user ) {
+ private function getWatchlist() {
$list = array();
$dbr = wfGetDB( DB_MASTER );
$res = $dbr->select(
'watchlist',
'*',
array(
- 'wl_user' => $user->getId(),
+ 'wl_user' => $this->getUser()->getId(),
),
__METHOD__
);
if( $res->numRows() > 0 ) {
foreach ( $res as $row ) {
$title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
- if( $title instanceof Title && !$title->isTalkPage() )
+ if ( $this->checkTitle( $title, $row->wl_namespace, $row->wl_title )
+ && !$title->isTalkPage()
+ ) {
$list[] = $title->getPrefixedText();
+ }
}
$res->free();
}
+ $this->cleanupWatchlist();
return $list;
}
/**
* Get a list of titles on a user's watchlist, excluding talk pages,
- * and return as a two-dimensional array with namespace, title and
- * redirect status
+ * and return as a two-dimensional array with namespace and title.
*
- * @param $user User
* @return array
*/
- private function getWatchlistInfo( $user ) {
+ private function getWatchlistInfo() {
$titles = array();
$dbr = wfGetDB( DB_MASTER );
$res = $dbr->select(
- array( 'watchlist', 'page' ),
- array(
- 'wl_namespace',
- 'wl_title',
- 'page_id',
- 'page_len',
- 'page_is_redirect',
- 'page_latest'
- ),
- array( 'wl_user' => $user->getId() ),
+ array( 'watchlist' ),
+ array( 'wl_namespace', 'wl_title' ),
+ array( 'wl_user' => $this->getUser()->getId() ),
__METHOD__,
- array( 'ORDER BY' => 'wl_namespace, wl_title' ),
- array( 'page' => array(
- 'LEFT JOIN',
- 'wl_namespace = page_namespace AND wl_title = page_title'
- ) )
+ array( 'ORDER BY' => 'wl_namespace, wl_title' )
);
- if( $res && $dbr->numRows( $res ) > 0 ) {
- $cache = LinkCache::singleton();
- foreach ( $res as $row ) {
- $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
- if( $title instanceof Title ) {
- // Update the link cache while we're at it
- if( $row->page_id ) {
- $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect, $row->page_latest );
- } else {
- $cache->addBadLinkObj( $title );
- }
- // Ignore non-talk
- if( !$title->isTalkPage() ) {
- $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
- }
- }
+ $lb = new LinkBatch();
+ foreach ( $res as $row ) {
+ $lb->add( $row->wl_namespace, $row->wl_title );
+ if ( !MWNamespace::isTalk( $row->wl_namespace ) ) {
+ $titles[$row->wl_namespace][$row->wl_title] = 1;
}
}
+
+ $lb->execute();
return $titles;
}
/**
- * Show a message indicating the number of items on the user's watchlist,
- * and return this count for additional checking
+ * Validates watchlist entry
*
- * @param $output OutputPage
- * @param $user User
- * @return int
+ * @param Title $title
+ * @param int $namespace
+ * @param String $dbKey
+ * @return bool: Whether this item is valid
*/
- private function showItemCount( $output, $user ) {
- if( ( $count = $this->countWatchlist( $user ) ) > 0 ) {
- $output->addHTML( wfMsgExt( 'watchlistedit-numitems', 'parse',
- $GLOBALS['wgLang']->formatNum( $count ) ) );
- } else {
- $output->addHTML( wfMsgExt( 'watchlistedit-noitems', 'parse' ) );
+ private function checkTitle( $title, $namespace, $dbKey ) {
+ if ( $title
+ && ( $title->isExternal()
+ || $title->getNamespace() < 0
+ )
+ ) {
+ $title = false; // unrecoverable
+ }
+ if ( !$title
+ || $title->getNamespace() != $namespace
+ || $title->getDBkey() != $dbKey
+ ) {
+ $this->badItems[] = array( $title, $namespace, $dbKey );
+ }
+ return (bool)$title;
+ }
+
+ /**
+ * Attempts to clean up broken items
+ */
+ private function cleanupWatchlist() {
+ $dbw = wfGetDB( DB_MASTER );
+ foreach ( $this->badItems as $row ) {
+ list( $title, $namespace, $dbKey ) = $row;
+ wfDebug( "User {$this->getUser()} has broken watchlist item ns($namespace):$dbKey, "
+ . ( $title ? 'cleaning up' : 'deleting' ) . ".\n"
+ );
+
+ $dbw->delete( 'watchlist',
+ array(
+ 'wl_user' => $this->getUser()->getId(),
+ 'wl_namespace' => $namespace,
+ 'wl_title' => $dbKey,
+ ),
+ __METHOD__
+ );
+
+ // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
+ if ( $title ) {
+ $this->getUser()->addWatch( $title );
+ }
}
- return $count;
}
/**
* Remove all titles from a user's watchlist
- *
- * @param $user User
*/
- private function clearWatchlist( $user ) {
+ private function clearWatchlist() {
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
'watchlist',
- array( 'wl_user' => $user->getId() ),
+ array( 'wl_user' => $this->getUser()->getId() ),
__METHOD__
);
}
* is preferred, since Titles are very memory-heavy
*
* @param $titles Array of strings, or Title objects
- * @param $user User
*/
- private function watchTitles( $titles, $user ) {
+ private function watchTitles( $titles ) {
$dbw = wfGetDB( DB_MASTER );
$rows = array();
foreach( $titles as $title ) {
}
if( $title instanceof Title ) {
$rows[] = array(
- 'wl_user' => $user->getId(),
+ 'wl_user' => $this->getUser()->getId(),
'wl_namespace' => ( $title->getNamespace() & ~1 ),
'wl_title' => $title->getDBkey(),
'wl_notificationtimestamp' => null,
);
$rows[] = array(
- 'wl_user' => $user->getId(),
+ 'wl_user' => $this->getUser()->getId(),
'wl_namespace' => ( $title->getNamespace() | 1 ),
'wl_title' => $title->getDBkey(),
'wl_notificationtimestamp' => null,
* is preferred, since Titles are very memory-heavy
*
* @param $titles Array of strings, or Title objects
- * @param $user User
*/
- private function unwatchTitles( $titles, $user ) {
+ private function unwatchTitles( $titles ) {
$dbw = wfGetDB( DB_MASTER );
foreach( $titles as $title ) {
if( !$title instanceof Title ) {
$dbw->delete(
'watchlist',
array(
- 'wl_user' => $user->getId(),
+ 'wl_user' => $this->getUser()->getId(),
'wl_namespace' => ( $title->getNamespace() & ~1 ),
'wl_title' => $title->getDBkey(),
),
$dbw->delete(
'watchlist',
array(
- 'wl_user' => $user->getId(),
+ 'wl_user' => $this->getUser()->getId(),
'wl_namespace' => ( $title->getNamespace() | 1 ),
'wl_title' => $title->getDBkey(),
),
__METHOD__
);
- $article = new Article($title);
- wfRunHooks('UnwatchArticleComplete',array(&$user,&$article));
+ $page = WikiPage::factory( $title );
+ wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
}
}
}
public function submitNormal( $data ) {
- global $wgUser;
$removed = array();
foreach( $data as $titles ) {
- $this->unwatchTitles( $titles, $wgUser );
- $removed += $titles;
+ $this->unwatchTitles( $titles );
+ $removed = array_merge( $removed, $titles );
}
if( count( $removed ) > 0 ) {
- global $wgLang;
- $this->successMessage = wfMessage(
- 'watchlistedit-normal-done',
- $wgLang->formatNum( count( $removed ) )
- );
- $this->showTitles( $removed, $this->successMessage, $this->getSkin() );
+ $this->successMessage = $this->msg( 'watchlistedit-normal-done'
+ )->numParams( count( $removed ) )->parse();
+ $this->showTitles( $removed, $this->successMessage );
return true;
} else {
return false;
/**
* Get the standard watchlist editing form
*
- * @param $user User
* @return HTMLForm
*/
- protected function getNormalForm( $user ){
+ protected function getNormalForm(){
global $wgContLang;
- $skin = $user->getSkin();
+
$fields = array();
+ $count = 0;
+
+ foreach( $this->getWatchlistInfo() as $namespace => $pages ){
+ if ( $namespace >= 0 ) {
+ $fields['TitlesNs'.$namespace] = array(
+ 'class' => 'EditWatchlistCheckboxSeriesField',
+ 'options' => array(),
+ 'section' => "ns$namespace",
+ );
+ }
- foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ){
+ foreach( array_keys( $pages ) as $dbkey ){
+ $title = Title::makeTitleSafe( $namespace, $dbkey );
+ if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
+ $text = $this->buildRemoveLine( $title );
+ $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
+ $count++;
+ }
+ }
+ }
+ $this->cleanupWatchlist();
- $namespace == NS_MAIN
- ? wfMsgHtml( 'blanknamespace' )
- : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
+ if ( count( $fields ) > 1 && $count > 30 ) {
+ $this->toc = Linker::tocIndent();
+ $tocLength = 0;
+ foreach( $fields as $key => $data ) {
- $fields['TitlesNs'.$namespace] = array(
- 'type' => 'multiselect',
- 'options' => array(),
- 'section' => "ns$namespace",
- );
+ # strip out the 'ns' prefix from the section name:
+ $ns = substr( $data['section'], 2 );
- foreach( $pages as $dbkey => $redirect ){
- $title = Title::makeTitleSafe( $namespace, $dbkey );
- $text = $this->buildRemoveLine( $title, $redirect, $skin );
- $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
+ $nsText = ($ns == NS_MAIN)
+ ? $this->msg( 'blanknamespace' )->escaped()
+ : htmlspecialchars( $wgContLang->getFormattedNsText( $ns ) );
+ $this->toc .= Linker::tocLine( "editwatchlist-{$data['section']}", $nsText,
+ $this->getLanguage()->formatNum( ++$tocLength ), 1 ) . Linker::tocLineEnd();
}
+ $this->toc = Linker::tocList( $this->toc );
+ } else {
+ $this->toc = false;
}
- $form = new EditWatchlistNormalHTMLForm( $fields );
+ $form = new EditWatchlistNormalHTMLForm( $fields, $this->getContext() );
$form->setTitle( $this->getTitle() );
- $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
- $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
- $form->addHeaderText( wfMessage( 'watchlistedit-normal-explain' )->parse() );
+ $form->setSubmitTextMsg( 'watchlistedit-normal-submit' );
+ # Used message keys: 'accesskey-watchlistedit-normal-submit', 'tooltip-watchlistedit-normal-submit'
+ $form->setSubmitTooltip('watchlistedit-normal-submit');
+ $form->setWrapperLegendMsg( 'watchlistedit-normal-legend' );
+ $form->addHeaderText( $this->msg( 'watchlistedit-normal-explain' )->parse() );
$form->setSubmitCallback( array( $this, 'submitNormal' ) );
return $form;
}
* Build the label for a checkbox, with a link to the title, and various additional bits
*
* @param $title Title
- * @param $redirect bool
- * @param $skin Skin
* @return string
*/
- private function buildRemoveLine( $title, $redirect, $skin ) {
- global $wgLang;
-
- $link = $skin->link( $title );
- if( $redirect ) {
+ private function buildRemoveLine( $title ) {
+ $link = Linker::link( $title );
+ if( $title->isRedirect() ) {
+ // Linker already makes class mw-redirect, so this is redundant
$link = '<span class="watchlistredir">' . $link . '</span>';
}
- $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
+ $tools[] = Linker::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
if( $title->exists() ) {
- $tools[] = $skin->link(
+ $tools[] = Linker::linkKnown(
$title,
- wfMsgHtml( 'history_short' ),
+ $this->msg( 'history_short' )->escaped(),
array(),
- array( 'action' => 'history' ),
- array( 'known', 'noclasses' )
+ array( 'action' => 'history' )
);
}
if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
- $tools[] = $skin->link(
+ $tools[] = Linker::linkKnown(
SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
- wfMsgHtml( 'contributions' ),
- array(),
- array(),
- array( 'known', 'noclasses' )
+ $this->msg( 'contributions' )->escaped()
);
}
- wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $skin ) );
+ wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
- return $link . " (" . $wgLang->pipeList( $tools ) . ")";
+ return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
}
/**
* Get a form for editing the watchlist in "raw" mode
*
- * @param $user User
* @return HTMLForm
*/
- protected function getRawForm( $user ){
- $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist( $user ) ), "\n" );
+ protected function getRawForm(){
+ $titles = implode( $this->getWatchlist(), "\n" );
$fields = array(
'Titles' => array(
'type' => 'textarea',
'default' => $titles,
),
);
- $form = new HTMLForm( $fields );
+ $form = new HTMLForm( $fields, $this->getContext() );
$form->setTitle( $this->getTitle( 'raw' ) );
- $form->setSubmitText( wfMessage( 'watchlistedit-raw-submit' )->text() );
- $form->setWrapperLegend( wfMessage( 'watchlistedit-raw-legend' )->text() );
- $form->addHeaderText( wfMessage( 'watchlistedit-raw-explain' )->parse() );
+ $form->setSubmitTextMsg( 'watchlistedit-raw-submit' );
+ # Used message keys: 'accesskey-watchlistedit-raw-submit', 'tooltip-watchlistedit-raw-submit'
+ $form->setSubmitTooltip('watchlistedit-raw-submit');
+ $form->setWrapperLegendMsg( 'watchlistedit-raw-legend' );
+ $form->addHeaderText( $this->msg( 'watchlistedit-raw-explain' )->parse() );
$form->setSubmitCallback( array( $this, 'submitRaw' ) );
return $form;
}
* Build a set of links for convenient navigation
* between watchlist viewing and editing modes
*
- * @param $skin Skin to use
+ * @param $unused
* @return string
*/
- public static function buildTools( $skin ) {
+ public static function buildTools( $unused ) {
global $wgLang;
$tools = array();
);
foreach( $modes as $mode => $arr ) {
// can use messages 'watchlisttools-view', 'watchlisttools-edit', 'watchlisttools-raw'
- $tools[] = $skin->linkKnown(
+ $tools[] = Linker::linkKnown(
SpecialPage::getTitleFor( $arr[0], $arr[1] ),
wfMsgHtml( "watchlisttools-{$mode}" )
);
*/
class EditWatchlistNormalHTMLForm extends HTMLForm {
public function getLegend( $namespace ){
- global $wgLang;
$namespace = substr( $namespace, 2 );
return $namespace == NS_MAIN
- ? wfMsgHtml( 'blanknamespace' )
- : htmlspecialchars( $wgLang->getFormattedNsText( $namespace ) );
+ ? $this->msg( 'blanknamespace' )->escaped()
+ : htmlspecialchars( $this->getContext()->getLanguage()->getFormattedNsText( $namespace ) );
+ }
+ public function getBody() {
+ return $this->displaySection( $this->mFieldTree, '', 'editwatchlist-' );
+ }
+}
+
+class EditWatchlistCheckboxSeriesField extends HTMLMultiSelectField {
+ /**
+ * HTMLMultiSelectField throws validation errors if we get input data
+ * that doesn't match the data set in the form setup. This causes
+ * problems if something gets removed from the watchlist while the
+ * form is open (bug 32126), but we know that invalid items will
+ * be harmless so we can override it here.
+ *
+ * @param $value String the value the field was submitted with
+ * @param $alldata Array the data collected from the form
+ * @return Mixed Bool true on success, or String error to display.
+ */
+ function validate( $value, $alldata ) {
+ // Need to call into grandparent to be a good citizen. :)
+ return HTMLFormField::validate( $value, $alldata );
}
}