Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / specials / SpecialEditWatchlist.php
index af8e71f..3d43831 100644 (file)
@@ -1,9 +1,36 @@
 <?php
+/**
+ * @defgroup Watchlist Users watchlist handling
+ */
+
+/**
+ * Implements Special:EditWatchlist
+ *
+ * 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
+ * @ingroup Watchlist
+ */
 
 /**
  * Provides the UI through which users can perform editing
  * operations on their watchlist
  *
+ * @ingroup SpecialPage
  * @ingroup Watchlist
  * @author Rob Church <robchur@gmail.com>
  */
@@ -18,6 +45,10 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
        protected $successMessage;
 
+       protected $toc;
+
+       private $badItems = array();
+
        public function __construct(){
                parent::__construct( 'EditWatchlist' );
        }
@@ -28,36 +59,29 @@ 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' ),
+                               $this->msg( 'loginreqlink' )->escaped(),
                                array(),
                                array( 'returnto' => $this->getTitle()->getPrefixedText() )
                        );
-                       $out->addHTML( wfMessage( 'watchlistanontext' )->rawParams( $llink )->parse() );
+                       $out->addHTML( $this->msg( 'watchlistanontext' )->rawParams( $llink )->parse() );
                        return;
                }
 
+               $this->checkPermissions();
+
                $this->outputHeader();
 
-               $sub  = wfMsgExt(
-                       'watchlistfor2',
-                       array( 'parseinline', 'replaceafter' ),
-                       $this->getUser()->getName(),
-                       SpecialEditWatchlist::buildTools( null )
-               );
-               $out->setSubtitle( $sub );
+               $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
@@ -75,7 +99,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 );
@@ -85,11 +109,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;
                }
@@ -132,33 +158,34 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        $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',
-                                       $this->getLang()->formatNum( count( $toWatch ) )
-                               );
+                               $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',
-                                       $this->getLang()->formatNum( count( $toUnwatch ) )
-                               );
+                               $this->successMessage .= ' ' . $this->msg( 'watchlistedit-raw-removed'
+                                       )->numParams( count( $toUnwatch ) )->parse();
                                $this->showTitles( $toUnwatch, $this->successMessage );
                        }
                } else {
                        $this->clearWatchlist();
                        $this->getUser()->invalidateCache();
-                       $this->successMessage .= wfMessage(
-                               'watchlistedit-raw-removed',
-                               $this->getLang()->formatNum( count( $current ) )
-                       );
+
+                       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;
@@ -174,7 +201,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * @param $output String
         */
        private function showTitles( $titles, &$output ) {
-               $talk = wfMsgHtml( 'talkpagelinktext' );
+               $talk = $this->msg( 'talkpagelinktext' )->escaped();
                // Do a batch existence check
                $batch = new LinkBatch();
                foreach( $titles as $title ) {
@@ -214,8 +241,9 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                $dbr = wfGetDB( DB_MASTER );
                $res = $dbr->select(
                        'watchlist',
-                       '*',
                        array(
+                               'wl_namespace', 'wl_title'
+                       ), array(
                                'wl_user' => $this->getUser()->getId(),
                        ),
                        __METHOD__
@@ -223,18 +251,21 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                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.
         *
         * @return array
         */
@@ -247,14 +278,14 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        array( 'wl_namespace',  'wl_title' ),
                        array( 'wl_user' => $this->getUser()->getId() ),
                        __METHOD__,
-                       array( 'ORDER BY' => 'wl_namespace, wl_title' )
+                       array( 'ORDER BY' => array( 'wl_namespace', 'wl_title' ) )
                );
 
                $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] = false;
+                               $titles[$row->wl_namespace][$row->wl_title] = 1;
                        }
                }
 
@@ -262,6 +293,62 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                return $titles;
        }
 
+       /**
+        * Validates watchlist entry
+        *
+        * @param Title $title
+        * @param int $namespace
+        * @param String $dbKey
+        * @return bool: Whether this item is valid
+        */
+       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() {
+               if( !count( $this->badItems ) ) {
+                       return; //nothing to do
+               }
+               $dbw = wfGetDB( DB_MASTER );
+               $user = $this->getUser();
+               foreach ( $this->badItems as $row ) {
+                       list( $title, $namespace, $dbKey ) = $row;
+                       wfDebug( "User {$user->getName()} has broken watchlist item ns($namespace):$dbKey, "
+                               . ( $title ? 'cleaning up' : 'deleting' ) . ".\n"
+                       );
+
+                       $dbw->delete( 'watchlist',
+                               array(
+                                       'wl_user' => $user->getId(),
+                                       'wl_namespace' => $namespace,
+                                       'wl_title' => $dbKey,
+                               ),
+                               __METHOD__
+                       );
+
+                       // Can't just do an UPDATE instead of DELETE/INSERT due to unique index
+                       if ( $title ) {
+                               $user->addWatch( $title );
+                       }
+               }
+       }
+
        /**
         * Remove all titles from a user's watchlist
         */
@@ -340,8 +427,8 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                                        ),
                                        __METHOD__
                                );
-                               $article = new Article( $title, 0 );
-                               wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$article ) );
+                               $page = WikiPage::factory( $title );
+                               wfRunHooks( 'UnwatchArticleComplete', array( $this->getUser(), &$page ) );
                        }
                }
        }
@@ -351,14 +438,12 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
 
                foreach( $data as $titles ) {
                        $this->unwatchTitles( $titles );
-                       $removed += $titles;
+                       $removed = array_merge( $removed, $titles );
                }
 
                if( count( $removed ) > 0 ) {
-                       $this->successMessage = wfMessage(
-                               'watchlistedit-normal-done',
-                               $this->getLang()->formatNum( count( $removed ) )
-                       );
+                       $this->successMessage = $this->msg( 'watchlistedit-normal-done'
+                               )->numParams( count( $removed ) )->parse();
                        $this->showTitles( $removed, $this->successMessage );
                        return true;
                } else {
@@ -375,31 +460,54 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                global $wgContLang;
 
                $fields = array();
+               $count = 0;
 
                foreach( $this->getWatchlistInfo() as $namespace => $pages ){
-
-                       $namespace == NS_MAIN
-                               ? wfMsgHtml( 'blanknamespace' )
-                               : htmlspecialchars( $wgContLang->getFormattedNsText( $namespace ) );
-
-                       $fields['TitlesNs'.$namespace] = array(
-                               'type' => 'multiselect',
-                               'options' => array(),
-                               'section' => "ns$namespace",
-                       );
+                       if ( $namespace >= 0 ) {
+                               $fields['TitlesNs'.$namespace] = array(
+                                       'class' => 'EditWatchlistCheckboxSeriesField',
+                                       'options' => array(),
+                                       'section' => "ns$namespace",
+                               );
+                       }
 
                        foreach( array_keys( $pages ) as $dbkey ){
                                $title = Title::makeTitleSafe( $namespace, $dbkey );
-                               $text = $this->buildRemoveLine( $title );
-                               $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
+                               if ( $this->checkTitle( $title, $namespace, $dbkey ) ) {
+                                       $text = $this->buildRemoveLine( $title );
+                                       $fields['TitlesNs'.$namespace]['options'][$text] = $title->getEscapedText();
+                                       $count++;
+                               }
+                       }
+               }
+               $this->cleanupWatchlist();
+
+               if ( count( $fields ) > 1 && $count > 30 ) {
+                       $this->toc = Linker::tocIndent();
+                       $tocLength = 0;
+                       foreach( $fields as $key => $data ) {
+
+                               # strip out the 'ns' prefix from the section name:
+                               $ns = substr( $data['section'], 2 );
+
+                               $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, $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;
        }
@@ -416,11 +524,11 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                        // Linker already makes class mw-redirect, so this is redundant
                        $link = '<span class="watchlistredir">' . $link . '</span>';
                }
-               $tools[] = Linker::link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
+               $tools[] = Linker::link( $title->getTalkPage(), $this->msg( 'talkpagelinktext' )->escaped() );
                if( $title->exists() ) {
                        $tools[] = Linker::linkKnown(
                                $title,
-                               wfMsgHtml( 'history_short' ),
+                               $this->msg( 'history_short' )->escaped(),
                                array(),
                                array( 'action' => 'history' )
                        );
@@ -428,13 +536,13 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                if( $title->getNamespace() == NS_USER && !$title->isSubpage() ) {
                        $tools[] = Linker::linkKnown(
                                SpecialPage::getTitleFor( 'Contributions', $title->getText() ),
-                               wfMsgHtml( 'contributions' )
+                               $this->msg( 'contributions' )->escaped()
                        );
                }
 
                wfRunHooks( 'WatchlistEditorBuildRemoveLine', array( &$tools, $title, $title->isRedirect(), $this->getSkin() ) );
 
-               return $link . " (" . $this->getLang()->pipeList( $tools ) . ")";
+               return $link . " (" . $this->getLanguage()->pipeList( $tools ) . ")";
        }
 
        /**
@@ -453,9 +561,11 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
                );
                $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;
        }
@@ -492,7 +602,7 @@ class SpecialEditWatchlist extends UnlistedSpecialPage {
         * Build a set of links for convenient navigation
         * between watchlist viewing and editing modes
         *
-        * @param $unused Unused
+        * @param $unused
         * @return string
         */
        public static function buildTools( $unused ) {
@@ -527,7 +637,28 @@ class EditWatchlistNormalHTMLForm extends HTMLForm {
        public function getLegend( $namespace ){
                $namespace = substr( $namespace, 2 );
                return $namespace == NS_MAIN
-                       ? wfMsgHtml( 'blanknamespace' )
-                       : htmlspecialchars( $this->getContext()->getLang()->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 );
        }
 }