Improvements to the watchlist editor per Brion; diff the current watchlist against...
authorRob Church <robchurch@users.mediawiki.org>
Thu, 5 Jul 2007 18:20:46 +0000 (18:20 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Thu, 5 Jul 2007 18:20:46 +0000 (18:20 +0000)
includes/Namespace.php
includes/Title.php
includes/WatchlistEditor.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index e50f3f8..ebb2bce 100644 (file)
@@ -134,7 +134,17 @@ class Namespace {
        public static function isContent( $index ) {
                global $wgContentNamespaces;
                return $index == NS_MAIN || in_array( $index, $wgContentNamespaces );
-       }        
+       }
+       
+       /**
+        * Can pages in a namespace be watched?
+        *
+        * @param int $index
+        * @return bool
+        */
+       public static function isWatchable( $index ) {
+               return $index >= NS_MAIN;
+       }
         
 }
 
index 34b9412..d089d8a 100644 (file)
@@ -2305,6 +2305,16 @@ class Title {
                # Return true if there was no history
                return $row === false;
        }
+       
+       /**
+        * Can this title be added to a user's watchlist?
+        *
+        * @return bool
+        */
+       public function isWatchable() {
+               return !$this->isExternal()
+                       && Namespace::isWatchable( $this->getNamespace() );
+       }
 
        /**
         * Get categories to which this Title belongs and return an array of
@@ -2429,6 +2439,15 @@ class Title {
                        && $this->getNamespace() == $title->getNamespace()
                        && $this->getDbkey() === $title->getDbkey();
        }
+       
+       /**
+        * Return a string representation of this title
+        *
+        * @return string
+        */
+       public function __toString() {
+               return $this->getPrefixedText();
+       }
 
        /**
         * Check if page exists
index 938fbd5..729ac29 100644 (file)
@@ -43,11 +43,23 @@ class WatchlistEditor {
                        case self::EDIT_RAW:
                                $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
                                if( $request->wasPosted() && $this->checkToken( $request, $user ) ) {
-                                       $titles = $this->extractTitles( $request->getText( 'titles' ) );
-                                       $this->clearWatchlist( $user );
-                                       $this->watchTitles( $titles, $user );
+                                       $current = $this->getWatchlist( $user );
+                                       $wanted = $this->extractTitles( $request->getText( 'titles' ) );
+                                       $toWatch = array_diff( $wanted, $current );
+                                       $toUnwatch = array_diff( $current, $wanted );
+                                       $this->watchTitles( $toWatch, $user );
+                                       $this->unwatchTitles( $toUnwatch, $user );
                                        $user->invalidateCache();
-                                       $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
+                                       if( count( $toWatch ) > 0 || count( $toUnwatch ) > 0 )
+                                               $output->addHtml( wfMsgExt( 'watchlistedit-raw-done', 'parse' ) );
+                                       if( ( $count = count( $toWatch ) ) > 0 ) {
+                                               $output->addHtml( wfMsgExt( 'watchlistedit-raw-added', 'parse', $count ) );
+                                               $this->showTitles( $toWatch, $output, $user->getSkin() );
+                                       }
+                                       if( ( $count = count( $toUnwatch ) ) > 0 ) {
+                                               $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
+                                               $this->showTitles( $toUnwatch, $output, $user->getSkin() );
+                                       }
                                }
                                $this->showRawForm( $output, $user );
                                break;
@@ -78,7 +90,7 @@ class WatchlistEditor {
        
        /**
         * Extract a list of titles from a text list; if we're given
-        * an array, convert each item into a Title
+        * an array, convert each item into a Title (ignores unwatchable titles)
         *
         * @param mixed $list
         * @return array
@@ -94,7 +106,7 @@ class WatchlistEditor {
                        $text = trim( $text );
                        if( strlen( $text ) > 0 ) {
                                $title = Title::newFromText( $text );
-                               if( $title instanceof Title )
+                               if( $title instanceof Title && $title->isWatchable() )
                                        $titles[] = $title;
                        }
                }
@@ -138,6 +150,27 @@ class WatchlistEditor {
                return ceil( $row->count / 2 ); // Paranoia
        }
        
+       /**
+        * Get a list of titles on a user's watchlist, excluding talk pages
+        *
+        * @param User $user
+        * @return array
+        */
+       private function getWatchlist( $user ) {
+               $titles = array();
+               $dbr = wfGetDB( DB_SLAVE );
+               $res = $dbr->select( 'watchlist', '*', array( 'wl_user' => $user->getId() ), __METHOD__ );
+               if( $res && $dbr->numRows( $res ) > 0 ) {
+                       while( $row = $dbr->fetchObject( $res ) ) {
+                               $title = Title::makeTitleSafe( $row->wl_namespace, $row->wl_title );
+                               if( $title instanceof Title && !$title->isTalkPage() )
+                                       $titles[] = $title;
+                       }
+                       $dbr->freeResult( $res );
+               }
+               return $titles;
+       }
+       
        /**
         * Get a list of titles on a user's watchlist, excluding talk pages,
         * and return as a two-dimensional array with namespace, title and
@@ -146,7 +179,7 @@ class WatchlistEditor {
         * @param User $user
         * @return array
         */
-       private function getWatchlist( $user ) {
+       private function getWatchlistInfo( $user ) {
                $titles = array();
                $dbr = wfGetDB( DB_SLAVE );
                $uid = intval( $user->getId() );
@@ -293,7 +326,7 @@ class WatchlistEditor {
                        $form .= Xml::hidden( 'token', $user->editToken( 'watchlistedit' ) );
                        $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-normal-legend' ) . '</legend>';
                        $form .= wfMsgExt( 'watchlistedit-normal-explain', 'parse' );
-                       foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
+                       foreach( $this->getWatchlistInfo( $user ) as $namespace => $pages ) {
                                $form .= '<h2>' . $this->getNamespaceHeading( $namespace ) . '</h2>';
                                $form .= '<ul>';
                                foreach( $pages as $dbkey => $redirect ) {
@@ -359,13 +392,8 @@ class WatchlistEditor {
                $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
                $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
                        'rows' => 6, 'cols' => 80 ) );
-               foreach( $this->getWatchlist( $user ) as $namespace => $pages ) {
-                       foreach( $pages as $dbkey => $redirect ) {
-                               $title = Title::makeTitleSafe( $namespace, $dbkey );
-                               if( $title instanceof Title )
-                                       $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
-                       }
-               }
+               foreach( $this->getWatchlist( $user ) as $title )
+                       $form .= htmlspecialchars( $title->getPrefixedText() ) . "\n";
                $form .= '</textarea>';
                $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
                $form .= '</fieldset></form>';
index 21fb6c2..5044568 100644 (file)
@@ -2909,5 +2909,7 @@ $1',
 'watchlistedit-raw-titles'     => 'Titles:',
 'watchlistedit-raw-submit'     => 'Update Watchlist',
 'watchlistedit-raw-done'       => 'Your watchlist has been updated.',
+'watchlistedit-raw-added' => '{{PLURAL:$1|1 title was|$1 titles were}} added:',
+'watchlistedit-raw-removed' => '{{PLURAL:$1|1 title was|$1 titles were}} removed:',
 
 );
\ No newline at end of file
index fa0ede3..9b87411 100644 (file)
@@ -2141,6 +2141,8 @@ $wgMessageStructure = array(
                'watchlistedit-raw-titles',
                'watchlistedit-raw-submit',
                'watchlistedit-raw-done',
+               'watchlistedit-raw-added',
+               'watchlistedit-raw-removed',
        ),      
 );
 /** Comments for each block */