(bug 13454) Article::updateCategoryCounts can attempt to execute empty inserts.
[lhc/web/wiklou.git] / includes / WatchlistEditor.php
index caad4c4..7e37dca 100644 (file)
@@ -32,34 +32,34 @@ class WatchlistEditor {
                }
                switch( $mode ) {
                        case self::EDIT_CLEAR:
-                               $output->setPageTitle( wfMsg( 'watchlistedit-clear-title' ) );
-                               if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
-                                       $this->clearWatchlist( $user );
-                                       $user->invalidateCache();
-                                       $output->addHtml( wfMsgExt( 'watchlistedit-clear-done', 'parse' ) );
-                               } else {
-                                       $this->showClearForm( $output, $user );
-                               }
-                               break;
+                               // The "Clear" link scared people too much.
+                               // Pass on to the raw editor, from which it's very easy to clear.
                        case self::EDIT_RAW:
                                $output->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
                                if( $request->wasPosted() && $this->checkToken( $request, $wgUser ) ) {
-                                       $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();
-                                       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, $wgUser->getSkin() );
-                                       }
-                                       if( ( $count = count( $toUnwatch ) ) > 0 ) {
-                                               $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
-                                               $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
+                                       $current = $this->getWatchlist( $user );
+                                       if( count( $wanted ) > 0 ) {
+                                               $toWatch = array_diff( $wanted, $current );
+                                               $toUnwatch = array_diff( $current, $wanted );
+                                               $this->watchTitles( $toWatch, $user );
+                                               $this->unwatchTitles( $toUnwatch, $user );
+                                               $user->invalidateCache();
+                                               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, $wgUser->getSkin() );
+                                               }
+                                               if( ( $count = count( $toUnwatch ) ) > 0 ) {
+                                                       $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', $count ) );
+                                                       $this->showTitles( $toUnwatch, $output, $wgUser->getSkin() );
+                                               }
+                                       } else {
+                                               $this->clearWatchlist( $user );
+                                               $user->invalidateCache();
+                                               $output->addHtml( wfMsgExt( 'watchlistedit-raw-removed', 'parse', count( $current ) ) );
+                                               $this->showTitles( $current, $output, $wgUser->getSkin() );
                                        }
                                }
                                $this->showRawForm( $output, $user );
@@ -111,7 +111,7 @@ class WatchlistEditor {
                                        $titles[] = $title->getPrefixedText();
                        }
                }
-               return $titles;
+               return array_unique( $titles );
        }
        
        /**
@@ -125,7 +125,7 @@ class WatchlistEditor {
         * @param Skin $skin
         */
        private function showTitles( $titles, $output, $skin ) {
-               $talk = htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) );
+               $talk = wfMsgHtml( 'talkpagelinktext' );
                // Do a batch existence check           
                $batch = new LinkBatch();
                foreach( $titles as $title ) {
@@ -157,7 +157,7 @@ class WatchlistEditor {
         * @return int
         */
        private function countWatchlist( $user ) {
-               $dbr = wfGetDB( DB_SLAVE );
+               $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
@@ -172,7 +172,7 @@ class WatchlistEditor {
         */
        private function getWatchlist( $user ) {
                $list = array();        
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_MASTER );
                $res = $dbr->select(
                        'watchlist',
                        '*',
@@ -202,7 +202,7 @@ class WatchlistEditor {
         */
        private function getWatchlistInfo( $user ) {
                $titles = array();
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_MASTER );
                $uid = intval( $user->getId() );
                list( $watchlist, $page ) = $dbr->tableNamesN( 'watchlist', 'page' );
                $sql = "SELECT wl_namespace, wl_title, page_id, page_is_redirect
@@ -326,27 +326,6 @@ class WatchlistEditor {
                        }
                }
        }
-
-       /**
-        * Show a confirmation form for users wishing to clear their watchlist
-        *
-        * @param OutputPage $output
-        * @param User $user
-        */
-       private function showClearForm( $output, $user ) {
-               global $wgUser;
-               if( ( $count = $this->showItemCount( $output, $user ) ) > 0 ) {
-                       $self = SpecialPage::getTitleFor( 'Watchlist' );
-                       $form  = Xml::openElement( 'form', array( 'method' => 'post',
-                               'action' => $self->getLocalUrl( 'action=clear' ) ) );
-                       $form .= Xml::hidden( 'token', $wgUser->editToken( 'watchlistedit' ) );
-                       $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-clear-legend' ) . '</legend>';
-                       $form .= wfMsgExt( 'watchlistedit-clear-confirm', 'parse' );
-                       $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-clear-submit' ) ) . '</p>';
-                       $form .= '</fieldset></form>';
-                       $output->addHtml( $form );
-               }
-       }
        
        /**
         * Show the standard watchlist editing form
@@ -403,10 +382,13 @@ class WatchlistEditor {
                $link = $skin->makeLinkObj( $title );
                if( $redirect )
                        $link = '<span class="watchlistredir">' . $link . '</span>';
-               $tools[] = $skin->makeLinkObj( $title->getTalkPage(),
-                       htmlspecialchars( $GLOBALS['wgContLang']->getFormattedNsText( NS_TALK ) ) );
-               if( $title->exists() )
+               $tools[] = $skin->makeLinkObj( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
+               if( $title->exists() ) {
                        $tools[] = $skin->makeKnownLinkObj( $title, wfMsgHtml( 'history_short' ), 'action=history' );
+               }
+               if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
+                       $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Contributions', $title->getText() ), wfMsgHtml( 'contributions' ) );
+               }
                return '<li>'
                        . Xml::check( 'titles[]', false, array( 'value' => $title->getPrefixedText() ) )
                        . $link . ' (' . implode( ' | ', $tools ) . ')' . '</li>';
@@ -428,6 +410,7 @@ class WatchlistEditor {
                $form .= '<fieldset><legend>' . wfMsgHtml( 'watchlistedit-raw-legend' ) . '</legend>';
                $form .= wfMsgExt( 'watchlistedit-raw-explain', 'parse' );
                $form .= Xml::label( wfMsg( 'watchlistedit-raw-titles' ), 'titles' );
+               $form .= "<br />\n";
                $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
                        'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
                $titles = $this->getWatchlist( $user );
@@ -460,5 +443,21 @@ class WatchlistEditor {
                                return false;
                }
        }
+       
+       /**
+        * Build a set of links for convenient navigation
+        * between watchlist viewing and editing modes
+        *
+        * @param Skin $skin Skin to use
+        * @return string
+        */
+       public static function buildTools( $skin ) {
+               $tools = array();
+               $modes = array( 'view' => false, 'edit' => 'edit', 'raw' => 'raw' );
+               foreach( $modes as $mode => $subpage ) {
+                       $tools[] = $skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Watchlist', $subpage ), wfMsgHtml( "watchlisttools-{$mode}" ) );
+               }
+               return implode( ' | ', $tools );
+       }
 
 }