Moved some of the functionality from mediawiki.legacy.prefs into mediawiki.specials...
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
index e67c4fd..5dc17ee 100644 (file)
@@ -1,5 +1,22 @@
 <?php
 /**
+ * Implements Special:Watchlist
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup SpecialPage Watchlist
  */
 function wfSpecialWatchlist( $par ) {
        global $wgUser, $wgOut, $wgLang, $wgRequest;
        global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
-       global $wgEnotifWatchlist;
-       $fname = 'wfSpecialWatchlist';
+       
+       // Add feed links
+       $wlToken = $wgUser->getOption( 'watchlisttoken' );
+       if (!$wlToken) {
+               $wlToken = sha1( mt_rand() . microtime( true ) );
+               $wgUser->setOption( 'watchlisttoken', $wlToken );
+               $wgUser->saveSettings();
+       }
+       
+       global $wgFeedClasses;
+       $apiParams = array( 'action' => 'feedwatchlist', 'allrev' => 'allrev',
+                                               'wlowner' => $wgUser->getName(), 'wltoken' => $wlToken );
+       $feedTemplate = wfScript('api').'?';
+       
+       foreach( $wgFeedClasses as $format => $class ) {
+               $theseParams = $apiParams + array( 'feedformat' => $format );
+               $url = $feedTemplate . wfArrayToCGI( $theseParams );
+               $wgOut->addFeedLink( $format, $url );
+       }
 
        $skin = $wgUser->getSkin();
        $specialTitle = SpecialPage::getTitleFor( 'Watchlist' );
@@ -22,15 +56,19 @@ function wfSpecialWatchlist( $par ) {
        # Anons don't get a watchlist
        if( $wgUser->isAnon() ) {
                $wgOut->setPageTitle( wfMsg( 'watchnologin' ) );
-               $llink = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Userlogin' ), wfMsgHtml( 'loginreqlink' ), 'returnto=' . $specialTitle->getPrefixedUrl() );
-               $wgOut->addHtml( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
+               $llink = $skin->linkKnown(
+                       SpecialPage::getTitleFor( 'Userlogin' ), 
+                       wfMsgHtml( 'loginreqlink' ),
+                       array(),
+                       array( 'returnto' => $specialTitle->getPrefixedText() )
+               );
+               $wgOut->addHTML( wfMsgWikiHtml( 'watchlistanontext', $llink ) );
                return;
        }
 
        $wgOut->setPageTitle( wfMsg( 'watchlist' ) );
 
-       $sub  = wfMsgExt( 'watchlistfor', 'parseinline', $wgUser->getName() );
-       $sub .= '<br />' . WatchlistEditor::buildTools( $wgUser->getSkin() );
+       $sub  = wfMsgExt( 'watchlistfor2', array( 'parseinline', 'replaceafter' ), $wgUser->getName(), WatchlistEditor::buildTools( $wgUser->getSkin() ) );
        $wgOut->setSubtitle( $sub );
 
        if( ( $mode = WatchlistEditor::getMode( $wgRequest, $par ) ) !== false ) {
@@ -40,7 +78,9 @@ function wfSpecialWatchlist( $par ) {
        }
 
        $uid = $wgUser->getId();
-       if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) && $wgRequest->wasPosted() ) {
+       if( ($wgEnotifWatchlist || $wgShowUpdatedMarker) && $wgRequest->getVal( 'reset' ) && 
+               $wgRequest->wasPosted() )
+       {
                $wgUser->clearAllNotifications( $uid );
                $wgOut->redirect( $specialTitle->getFullUrl() );
                return;
@@ -52,6 +92,7 @@ function wfSpecialWatchlist( $par ) {
        /* bool  */ 'hideBots'  => (int)$wgUser->getBoolOption( 'watchlisthidebots' ),
        /* bool  */ 'hideAnons' => (int)$wgUser->getBoolOption( 'watchlisthideanons' ),
        /* bool  */ 'hideLiu'   => (int)$wgUser->getBoolOption( 'watchlisthideliu' ),
+       /* bool  */ 'hidePatrolled' => (int)$wgUser->getBoolOption( 'watchlisthidepatrolled' ),
        /* bool  */ 'hideOwn'   => (int)$wgUser->getBoolOption( 'watchlisthideown' ),
        /* ?     */ 'namespace' => 'all',
        /* ?     */ 'invert'    => false,
@@ -67,6 +108,7 @@ function wfSpecialWatchlist( $par ) {
        $prefs['hideanons'] = $wgUser->getBoolOption( 'watchlisthideanon' );
        $prefs['hideliu']   = $wgUser->getBoolOption( 'watchlisthideliu' );
        $prefs['hideown' ]  = $wgUser->getBoolOption( 'watchlisthideown' );
+       $prefs['hidepatrolled' ] = $wgUser->getBoolOption( 'watchlisthidepatrolled' );
 
        # Get query variables
        $days      = $wgRequest->getVal(  'days'     , $prefs['days'] );
@@ -75,23 +117,24 @@ function wfSpecialWatchlist( $par ) {
        $hideAnons = $wgRequest->getBool( 'hideAnons', $prefs['hideanons'] );
        $hideLiu   = $wgRequest->getBool( 'hideLiu'  , $prefs['hideliu'] );
        $hideOwn   = $wgRequest->getBool( 'hideOwn'  , $prefs['hideown'] );
+       $hidePatrolled   = $wgRequest->getBool( 'hidePatrolled'  , $prefs['hidepatrolled'] );
 
        # Get namespace value, if supplied, and prepare a WHERE fragment
        $nameSpace = $wgRequest->getIntOrNull( 'namespace' );
        $invert = $wgRequest->getIntOrNull( 'invert' );
        if( !is_null( $nameSpace ) ) {
                $nameSpace = intval( $nameSpace );
-               if( $invert && $nameSpace != 'all' )
-                       $nameSpaceClause = " AND rc_namespace != $nameSpace";
+               if( $invert && $nameSpace !== 'all' )
+                       $nameSpaceClause = "rc_namespace != $nameSpace";
                else
-                       $nameSpaceClause = " AND rc_namespace = $nameSpace";
+                       $nameSpaceClause = "rc_namespace = $nameSpace";
        } else {
                $nameSpace = '';
                $nameSpaceClause = '';
        }
 
        $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
-       list( $page, $watchlist, $recentchanges ) = $dbr->tableNamesN( 'page', 'watchlist', 'recentchanges' );
+       $recentchanges = $dbr->tableName( 'recentchanges' );
 
        $watchlistCount = $dbr->selectField( 'watchlist', 'COUNT(*)',
                array( 'wl_user' => $uid ), __METHOD__ );
@@ -121,27 +164,20 @@ function wfSpecialWatchlist( $par ) {
        wfAppendToArrayIfNotDefault( 'hideLiu'  , (int)$hideLiu  , $defaults, $nondefaults );
        wfAppendToArrayIfNotDefault( 'hideOwn'  , (int)$hideOwn  , $defaults, $nondefaults);
        wfAppendToArrayIfNotDefault( 'namespace', $nameSpace     , $defaults, $nondefaults);
+       wfAppendToArrayIfNotDefault( 'hidePatrolled', (int)$hidePatrolled, $defaults, $nondefaults );
 
-       $hookSql = "";
-       if( ! wfRunHooks('BeforeWatchlist', array($nondefaults, $wgUser, &$hookSql)) ) {
-               return;
-       }
-
-       if($nitems == 0) {
+       if( $nitems == 0 ) {
                $wgOut->addWikiMsg( 'nowatchlist' );
                return;
        }
 
-       if ( $days <= 0 ) {
+       # Possible where conditions
+       $conds = array();
+
+       if( $days <= 0 ) {
                $andcutoff = '';
        } else {
-               $andcutoff = "AND rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
-               /*
-               $sql = "SELECT COUNT(*) AS n FROM $page, $revision  WHERE rev_timestamp>'$cutoff' AND page_id=rev_page";
-               $res = $dbr->query( $sql, $fname );
-               $s = $dbr->fetchObject( $res );
-               $npages = $s->n;
-               */
+               $conds[] = "rc_timestamp > '".$dbr->timestamp( time() - intval( $days * 86400 ) )."'";
        }
 
        # If the watchlist is relatively short, it's simplest to zip
@@ -153,133 +189,132 @@ function wfSpecialWatchlist( $par ) {
        # Up estimate of watched items by 15% to compensate for talk pages...
 
        # Toggles
-       $andHideOwn   = $hideOwn   ? "AND (rc_user <> $uid)" : '';
-       $andHideBots  = $hideBots  ? "AND (rc_bot = 0)" : '';
-       $andHideMinor = $hideMinor ? "AND (rc_minor = 0)" : '';
-       $andHideLiu   = $hideLiu   ? "AND (rc_user = 0)" : '';
-       $andHideAnons = $hideAnons ? "AND (rc_user != 0)" : '';
-
-       # Show watchlist header
-       $header = '';
-       if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
-               $header .= wfMsg( 'wlheader-enotif' ) . "\n";
+       if( $hideOwn ) {
+               $conds[] = "rc_user != $uid";
        }
-       if ( $wgShowUpdatedMarker ) {
-               $header .= wfMsg( 'wlheader-showupdated' ) . "\n";
+       if( $hideBots ) {
+               $conds[] = 'rc_bot = 0';
+       }
+       if( $hideMinor ) {
+               $conds[] = 'rc_minor = 0';
+       }
+       if( $hideLiu ) {
+               $conds[] = 'rc_user = 0';
+       }
+       if( $hideAnons ) {
+               $conds[] = 'rc_user != 0';
+       }
+       if ( $wgUser->useRCPatrol() && $hidePatrolled ) {
+               $conds[] = 'rc_patrolled != 1';
+       }
+       if( $nameSpaceClause ) {
+               $conds[] = $nameSpaceClause;
        }
 
-  # Toggle watchlist content (all recent edits or just the latest)
+       # Toggle watchlist content (all recent edits or just the latest)
        if( $wgUser->getOption( 'extendwatchlist' )) {
-               $andLatest='';
-               $limitWatchlist = 'LIMIT ' . intval( $wgUser->getOption( 'wllimit' ) );
+               $limitWatchlist = intval( $wgUser->getOption( 'wllimit' ) );
+               $usePage = false;
        } else {
        # Top log Ids for a page are not stored
-               $andLatest = 'AND (rc_this_oldid=page_latest OR rc_type=' . RC_LOG . ') ';
-               $limitWatchlist = '';
+               $conds[] = 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG;
+               $limitWatchlist = 0;
+               $usePage = true;
        }
 
-       $header .= wfMsgExt( 'watchlist-details', array( 'parsemag' ), $wgLang->formatNum( $nitems ) );
-       $wgOut->addWikiText( $header );
-
        # Show a message about slave lag, if applicable
        if( ( $lag = $dbr->getLag() ) > 0 )
                $wgOut->showLagWarning( $lag );
 
-       if ( $wgShowUpdatedMarker ) {
-               $wgOut->addHTML( '<form action="' .
-                       $specialTitle->escapeLocalUrl() .
-                       '" method="post"><input type="submit" name="dummy" value="' .
-                       htmlspecialchars( wfMsg( 'enotif_reset' ) ) .
-                       '" /><input type="hidden" name="reset" value="all" /></form>' .
-                       "\n\n" );
+       # Create output form
+       $form  = Xml::fieldset( wfMsg( 'watchlist-options' ), false, array( 'id' => 'mw-watchlist-options' ) );
+
+       # Show watchlist header
+       $form .= wfMsgExt( 'watchlist-details', array( 'parseinline' ), $wgLang->formatNum( $nitems ) );
+
+       if( $wgUser->getOption( 'enotifwatchlistpages' ) && $wgEnotifWatchlist) {
+               $form .= wfMsgExt( 'wlheader-enotif', 'parse' ) . "\n";
        }
-       if ( $wgShowUpdatedMarker ) {
-               $wltsfield = ", ${watchlist}.wl_notificationtimestamp ";
-       } else {
-               $wltsfield = '';
+       if( $wgShowUpdatedMarker ) {
+               $form .= Xml::openElement( 'form', array( 'method' => 'post',
+                                       'action' => $specialTitle->getLocalUrl(),
+                                       'id' => 'mw-watchlist-resetbutton' ) ) .
+                               wfMsgExt( 'wlheader-showupdated', array( 'parseinline' ) ) . ' ' .
+                               Xml::submitButton( wfMsg( 'enotif_reset' ), array( 'name' => 'dummy' ) ) .
+                               Xml::hidden( 'reset', 'all' ) .
+                               Xml::closeElement( 'form' );
+       }
+       $form .= '<hr />';
+       
+       $tables = array( 'recentchanges', 'watchlist' );
+       $fields = array( "{$recentchanges}.*" );
+       $join_conds = array(
+               'watchlist' => array('INNER JOIN',"wl_user='{$uid}' AND wl_namespace=rc_namespace AND wl_title=rc_title"),
+       );
+       $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
+       if( $wgShowUpdatedMarker ) {
+               $fields[] = 'wl_notificationtimestamp';
        }
-       $sql = "SELECT ${recentchanges}.* ${wltsfield}
-         FROM $watchlist,$recentchanges
-         LEFT JOIN $page ON rc_cur_id=page_id
-         WHERE wl_user=$uid
-         AND wl_namespace=rc_namespace
-         AND wl_title=rc_title
-         $andcutoff
-         $andLatest
-         $andHideOwn
-         $andHideBots
-         $andHideMinor
-         $andHideLiu
-         $andHideAnons
-         $nameSpaceClause
-         $hookSql
-         ORDER BY rc_timestamp DESC
-         $limitWatchlist";
-
-       $res = $dbr->query( $sql, $fname );
+       if( $limitWatchlist ) {
+               $options['LIMIT'] = $limitWatchlist;
+       }
+
+       $rollbacker = $wgUser->isAllowed('rollback');
+       if ( $usePage || $rollbacker ) {
+               $tables[] = 'page';
+               $join_conds['page'] = array('LEFT JOIN','rc_cur_id=page_id');
+               if ($rollbacker) 
+                       $fields[] = 'page_latest';
+       }
+
+       ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
+       wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
+       
+       $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );
        $numRows = $dbr->numRows( $res );
 
        /* Start bottom header */
-       $wgOut->addHTML( "<hr />\n" );
 
-       if($days >= 1) {
-               $wgOut->addHTML(
-                       wfMsgExt( 'rcnote', 'parseinline',
+       $wlInfo = '';
+       if( $days >= 1 ) {
+               $wlInfo = wfMsgExt( 'rcnote', 'parseinline',
                                $wgLang->formatNum( $numRows ),
                                $wgLang->formatNum( $days ),
                                $wgLang->timeAndDate( wfTimestampNow(), true ),
                                $wgLang->date( wfTimestampNow(), true ),
                                $wgLang->time( wfTimestampNow(), true )
-                       ) . '<br />'
-               );
-       } elseif($days > 0) {
-               $wgOut->addHtml(
-                       wfMsgExt( 'wlnote', 'parseinline',
+                       ) . '<br />';
+       } elseif( $days > 0 ) {
+               $wlInfo = wfMsgExt( 'wlnote', 'parseinline',
                                $wgLang->formatNum( $numRows ),
                                $wgLang->formatNum( round($days*24) )
-                       ) . '<br />'
-               );
+                       ) . '<br />';
        }
 
-       $wgOut->addHTML( "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n" );
+       $cutofflinks = "\n" . wlCutoffLinks( $days, 'Watchlist', $nondefaults ) . "<br />\n";
 
-       # Spit out some control panel links
        $thisTitle = SpecialPage::getTitleFor( 'Watchlist' );
-       $skin = $wgUser->getSkin();
 
-       # Hide/show minor edits
-       $label = $hideMinor ? wfMsgHtml( 'watchlist-show-minor' ) : wfMsgHtml( 'watchlist-hide-minor' );
-       $linkBits = wfArrayToCGI( array( 'hideMinor' => 1 - (int)$hideMinor ), $nondefaults );
-       $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
-
-       # Hide/show bot edits
-       $label = $hideBots ? wfMsgHtml( 'watchlist-show-bots' ) : wfMsgHtml( 'watchlist-hide-bots' );
-       $linkBits = wfArrayToCGI( array( 'hideBots' => 1 - (int)$hideBots ), $nondefaults );
-       $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
-
-       # Hide/show anonymous edits
-       $label = $hideAnons ? wfMsgHtml( 'watchlist-show-anons' ) : wfMsgHtml( 'watchlist-hide-anons' );
-       $linkBits = wfArrayToCGI( array( 'hideAnons' => 1 - (int)$hideAnons ), $nondefaults );
-       $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
-
-       # Hide/show logged in edits
-       $label = $hideLiu ? wfMsgHtml( 'watchlist-show-liu' ) : wfMsgHtml( 'watchlist-hide-liu' );
-       $linkBits = wfArrayToCGI( array( 'hideLiu' => 1 - (int)$hideLiu ), $nondefaults );
-       $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
-
-       # Hide/show own edits
-       $label = $hideOwn ? wfMsgHtml( 'watchlist-show-own' ) : wfMsgHtml( 'watchlist-hide-own' );
-       $linkBits = wfArrayToCGI( array( 'hideOwn' => 1 - (int)$hideOwn ), $nondefaults );
-       $links[] = $skin->makeKnownLinkObj( $thisTitle, $label, $linkBits );
-
-       $wgOut->addHTML( implode( ' | ', $links ) );
-
-       # Form for namespace filtering
-       $form  = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl() ) );
-       $form .= '<p>';
-       $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;';
-       $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&nbsp;';
-       $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&nbsp;';
+       # Spit out some control panel links
+       $links[] = wlShowHideLink( $nondefaults, 'rcshowhideminor', 'hideMinor', $hideMinor );
+       $links[] = wlShowHideLink( $nondefaults, 'rcshowhidebots', 'hideBots', $hideBots );
+       $links[] = wlShowHideLink( $nondefaults, 'rcshowhideanons', 'hideAnons', $hideAnons );
+       $links[] = wlShowHideLink( $nondefaults, 'rcshowhideliu', 'hideLiu', $hideLiu );
+       $links[] = wlShowHideLink( $nondefaults, 'rcshowhidemine', 'hideOwn', $hideOwn );
+
+       if( $wgUser->useRCPatrol() ) {
+               $links[] = wlShowHideLink( $nondefaults, 'rcshowhidepatr', 'hidePatrolled', $hidePatrolled );
+       }
+
+       # Namespace filter and put the whole form together.
+       $form .= $wlInfo;
+       $form .= $cutofflinks;
+       $form .= $wgLang->pipeList( $links );
+       $form .= Xml::openElement( 'form', array( 'method' => 'post', 'action' => $thisTitle->getLocalUrl(), 'id' => 'mw-watchlist-form-namespaceselector' ) );
+       $form .= '<hr /><p>';
+       $form .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;';
+       $form .= Xml::namespaceSelector( $nameSpace, '' ) . '&#160;';
+       $form .= Xml::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $invert ) . '&#160;';
        $form .= Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . '</p>';
        $form .= Xml::hidden( 'days', $days );
        if( $hideMinor )
@@ -293,7 +328,8 @@ function wfSpecialWatchlist( $par ) {
        if( $hideOwn )
                $form .= Xml::hidden( 'hideOwn', 1 );
        $form .= Xml::closeElement( 'form' );
-       $wgOut->addHtml( $form );
+       $form .= Xml::closeElement( 'fieldset' );
+       $wgOut->addHTML( $form );
 
        # If there's nothing to show, stop here
        if( $numRows == 0 ) {
@@ -311,12 +347,15 @@ function wfSpecialWatchlist( $par ) {
                        $linkBatch->add( NS_USER, $userNameUnderscored );
                }
                $linkBatch->add( NS_USER_TALK, $userNameUnderscored );
+
+               $linkBatch->add( $row->rc_namespace, $row->rc_title );
        }
        $linkBatch->execute();
        $dbr->dataSeek( $res, 0 );
 
        $list = ChangesList::newFromUser( $wgUser );
-
+       $list->setWatchlistDivs();
+       
        $s = $list->beginRecentChangesList();
        $counter = 1;
        while ( $obj = $dbr->fetchObject( $res ) ) {
@@ -342,31 +381,60 @@ function wfSpecialWatchlist( $par ) {
                        $rc->numberofWatchingusers = 0;
                }
 
-               $s .= $list->recentChangesLine( $rc, $updated );
+               $s .= $list->recentChangesLine( $rc, $updated, $counter );
        }
        $s .= $list->endRecentChangesList();
 
-       $dbr->freeResult( $res );
        $wgOut->addHTML( $s );
 }
 
+function wlShowHideLink( $options, $message, $name, $value ) {
+       global $wgUser;
+
+       $showLinktext = wfMsgHtml( 'show' );
+       $hideLinktext = wfMsgHtml( 'hide' );
+       $title = SpecialPage::getTitleFor( 'Watchlist' );
+       $skin = $wgUser->getSkin();
+
+       $label = $value ? $showLinktext : $hideLinktext;
+       $options[$name] = 1 - (int) $value;
+
+       return wfMsgHtml( $message, $skin->linkKnown( $title, $label, array(), $options ) );
+}
+
+
 function wlHoursLink( $h, $page, $options = array() ) {
        global $wgUser, $wgLang, $wgContLang;
+
        $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink(
-         $wgContLang->specialPage( $page ),
-         $wgLang->formatNum( $h ),
-         wfArrayToCGI( array('days' => ($h / 24.0)), $options ) );
+       $title = Title::newFromText( $wgContLang->specialPage( $page ) );
+       $options['days'] = ($h / 24.0);
+
+       $s = $sk->linkKnown(
+               $title,
+               $wgLang->formatNum( $h ),
+               array(),
+               $options
+       );
+
        return $s;
 }
 
 function wlDaysLink( $d, $page, $options = array() ) {
        global $wgUser, $wgLang, $wgContLang;
+
        $sk = $wgUser->getSkin();
-       $s = $sk->makeKnownLink(
-         $wgContLang->specialPage( $page ),
-         ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) ),
-         wfArrayToCGI( array('days' => $d), $options ) );
+       $title = Title::newFromText( $wgContLang->specialPage( $page ) );
+       $options['days'] = $d;
+       $message = ($d ? $wgLang->formatNum( $d ) : wfMsgHtml( 'watchlistall2' ) );
+
+       $s = $sk->linkKnown(
+               $title,
+               $message,
+               array(),
+               $options
+       );
+
        return $s;
 }
 
@@ -374,6 +442,8 @@ function wlDaysLink( $d, $page, $options = array() ) {
  * Returns html
  */
 function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
+       global $wgLang;
+
        $hours = array( 1, 2, 6, 12 );
        $days = array( 1, 3, 7 );
        $i = 0;
@@ -386,25 +456,26 @@ function wlCutoffLinks( $days, $page = 'Watchlist', $options = array() ) {
        }
        return wfMsgExt('wlshowlast',
                array('parseinline', 'replaceafter'),
-               implode(' | ', $hours),
-               implode(' | ', $days),
+               $wgLang->pipeList( $hours ),
+               $wgLang->pipeList( $days ),
                wlDaysLink( 0, $page, $options ) );
 }
 
 /**
  * Count the number of items on a user's watchlist
  *
- * @param $talk Include talk pages
- * @return integer
+ * @param $user User object
+ * @param $talk Boolean: include talk pages
+ * @return Integer
  */
 function wlCountItems( &$user, $talk = true ) {
        $dbr = wfGetDB( DB_SLAVE, 'watchlist' );
 
        # Fetch the raw count
-       $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', array( 'wl_user' => $user->mId ), 'wlCountItems' );
+       $res = $dbr->select( 'watchlist', 'COUNT(*) AS count', 
+               array( 'wl_user' => $user->mId ), 'wlCountItems' );
        $row = $dbr->fetchObject( $res );
        $count = $row->count;
-       $dbr->freeResult( $res );
 
        # Halve to remove talk pages if needed
        if( !$talk )