revert r106095, fix apparently not this simple
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
index 1ca21ac..cde2ca3 100644 (file)
@@ -18,6 +18,8 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
        protected $successMessage;
 
+       protected $toc;
+
        public function __construct(){
                parent::__construct( 'EditWatchlist' );
        }
@@ -28,15 +30,13 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @param $mode int
         */
        public function execute( $mode ) {
-               if( wfReadOnly() ) {
-                       throw new ReadOnlyError;
-               }
+               $this->setHeaders();
 
                $out = $this->getOutput();
 
                # Anons don't get a watchlist
                if( $this->getUser()->isAnon() ) {
-                       $out->setPageTitle( wfMsg( 'watchnologin' ) );
+                       $out->setPageTitle( $this->msg( 'watchnologin' ) );
                        $llink = Linker::linkKnown(
                                SpecialPage::getTitleFor( 'Userlogin' ),
                                wfMsgHtml( 'loginreqlink' ),
@@ -47,13 +47,12 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        return;
                }
 
-               $sub  = wfMsgExt(
-                       'watchlistfor2',
-                       array( 'parseinline', 'replaceafter' ),
-                       $this->getUser()->getName(),
-                       SpecialEditWatchlist::buildTools( null )
-               );
-               $out->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
@@ -71,7 +70,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                                // Pass on to the raw editor, from which it's very easy to clear.
 
                        case self::EDIT_RAW:
-                               $out->setPageTitle( wfMsg( 'watchlistedit-raw-title' ) );
+                               $out->setPageTitle( $this->msg( 'watchlistedit-raw-title' ) );
                                $form = $this->getRawForm();
                                if( $form->show() ){
                                        $out->addHTML( $this->successMessage );
@@ -81,11 +80,13 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
                        case self::EDIT_NORMAL:
                        default:
-                               $out->setPageTitle( wfMsg( 'watchlistedit-normal-title' ) );
+                               $out->setPageTitle( $this->msg( 'watchlistedit-normal-title' ) );
                                $form = $this->getNormalForm();
                                if( $form->show() ){
                                        $out->addHTML( $this->successMessage );
                                        $out->returnToMain();
+                               } elseif ( $this->toc !== false ) {
+                                       $out->prependHTML( $this->toc );
                                }
                                break;
                }
@@ -134,26 +135,33 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        }
 
                        if( count( $toWatch ) > 0 ) {
-                               $this->successMessage .= wfMessage(
+                               $this->successMessage .= ' ' . wfMessage(
                                        'watchlistedit-raw-added',
-                                       $this->getLang()->formatNum( count( $toWatch ) )
+                                       $this->getLanguage()->formatNum( count( $toWatch ) )
                                );
                                $this->showTitles( $toWatch, $this->successMessage );
                        }
 
                        if( count( $toUnwatch ) > 0 ) {
-                               $this->successMessage .= wfMessage(
+                               $this->successMessage .= ' ' . wfMessage(
                                        'watchlistedit-raw-removed',
-                                       $this->getLang()->formatNum( count( $toUnwatch ) )
+                                       $this->getLanguage()->formatNum( count( $toUnwatch ) )
                                );
                                $this->showTitles( $toUnwatch, $this->successMessage );
                        }
                } else {
                        $this->clearWatchlist();
-                       $this->getLang()->invalidateCache();
-                       $this->successMessage .= wfMessage(
+                       $this->getUser()->invalidateCache();
+
+                       if( count( $current ) > 0 ){
+                               $this->successMessage = wfMessage( 'watchlistedit-raw-done' )->parse();
+                       } else {
+                               return false;
+                       }
+
+                       $this->successMessage .= ' ' . wfMessage(
                                'watchlistedit-raw-removed',
-                               $this->getLang()->formatNum( count( $current ) )
+                               $this->getLanguage()->formatNum( count( $current ) )
                        );
                        $this->showTitles( $current, $this->successMessage );
                }
@@ -229,8 +237,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
        /**
         * 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.
         *
         * @return array
         */
@@ -239,42 +246,22 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                $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( '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;
        }
 
@@ -373,7 +360,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                if( count( $removed ) > 0 ) {
                        $this->successMessage = wfMessage(
                                'watchlistedit-normal-done',
-                               $this->getLang()->formatNum( count( $removed ) )
+                               $this->getLanguage()->formatNum( count( $removed ) )
                        );
                        $this->showTitles( $removed, $this->successMessage );
                        return true;
@@ -391,27 +378,53 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                global $wgContLang;
 
                $fields = array();
+               $count = 0;
 
+               $haveInvalidNamespaces = false;
                foreach( $this->getWatchlistInfo() as $namespace => $pages ){
-
-                       $namespace == NS_MAIN
-                               ? wfMsgHtml( 'blanknamespace' )
-                               : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
+                       if ( $namespace < 0 ) {
+                               $haveInvalidNamespaces = true;
+                               continue;
+                       }
 
                        $fields['TitlesNs'.$namespace] = array(
-                               'type' => 'multiselect',
+                               'class' => 'EditWatchlistCheckboxSeriesField',
                                'options' => array(),
                                'section' => "ns$namespace",
                        );
 
-                       foreach( $pages as $dbkey => $redirect ){
+                       foreach( array_keys( $pages ) as $dbkey ){
                                $title = Title::makeTitleSafe( $namespace, $dbkey );
-                               $text = $this->buildRemoveLine( $title, $redirect );
+                               $text = $this->buildRemoveLine( $title );
                                $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
+                               $count++;
                        }
                }
+               if ( $haveInvalidNamespaces ) {
+                       wfDebug( "User {$this->getContext()->getUser()->getId()} has invalid watchlist entries, cleaning up...\n" );
+                       $this->getContext()->getUser()->cleanupWatchlist();
+               }
+
+               if ( count( $fields ) > 1 && $count > 30 ) {
+                       $this->toc = Linker::tocIndent();
+                       $tocLength = 0;
+                       foreach( $fields as $key => $data ) {
 
-               $form = new EditWatchlistNormalHTMLForm( $fields );
+                               # strip out the 'ns' prefix from the section name:
+                               $ns = substr( $data['section'], 2 );
+
+                               $nsText = ($ns == NS_MAIN)
+                                       ? wfMsgHtml( 'blanknamespace' )
+                                       : 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, $this->getContext() );
                $form->setTitle( $this->getTitle() );
                $form->setSubmitText( wfMessage( 'watchlistedit-normal-submit' )->text() );
                $form->setWrapperLegend( wfMessage( 'watchlistedit-normal-legend' )->text() );
@@ -424,12 +437,12 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * Build the label for a checkbox, with a link to the title, and various additional bits
         *
         * @param $title Title
-        * @param $redirect bool
         * @return string
         */
-       private function buildRemoveLine( $title, $redirect ) {
+       private function buildRemoveLine( $title ) {
                $link = Linker::link( $title );
-               if( $redirect ) {
+               if( $title->isRedirect() ) {
+                       // Linker already makes class mw-redirect, so this is redundant
                        $link = '<span class="watchlistredir">' . $link . '</span>';
                }
                $tools[] = Linker::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
@@ -448,9 +461,9 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        );
                }
 
-               wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $redirect, $this->getSkin() ) );
+               wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
 
-               return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
+               return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
        }
 
        /**
@@ -459,7 +472,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @return HTMLForm
         */
        protected function getRawForm(){
-               $titles = implode( array_map( 'htmlspecialchars', $this->getWatchlist() ), "\n" );
+               $titles = implode( $this->getWatchlist(), "\n" );
                $fields = array(
                        'Titles' => array(
                                'type' => 'textarea',
@@ -467,7 +480,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                                '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() );
@@ -485,9 +498,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @return int
         */
        public static function getMode( $request, $par ) {
-               $act  = $request->getVal( 'action' );
-               $mode = ( $act == 'view' ) ? $par : $act;
-               $mode = strtolower( $mode );
+               $mode = strtolower( $request->getVal( 'action', $par ) );
                switch( $mode ) {
                        case 'clear':
                        case self::EDIT_CLEAR:
@@ -546,6 +557,27 @@ class EditWatchlistNormalHTMLForm extends HTMLForm {
                $namespace = substr( $namespace, 2 );
                return $namespace == NS_MAIN
                        ? wfMsgHtml( 'blanknamespace' )
-                       : htmlspecialchars( $this->getContext()->getLang()->getFormattedNsText( $namespace ) );
+                       : 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 );
        }
 }