MarkAHershberger has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/279741

Change subject: Fix form submission
......................................................................

Fix form submission

Prior to this the form was not immediately affecting the list of
watching users in the display.

To fix this, calls are made to the HTMLForm::prepareForm() and
HTMLFormr::tryAuthorizedSubmit() to validate the ensure that the form is
processed before the form is displayed.

De-coupling the form processing and form display like this allows us to
make change our display based on what is submitted without relying on
where the form is placed on the page.

Also:

* Make configuration parameters instance values.
* Remove use of static methods by using the right callback type.

Change-Id: Ied368112cbc45c41ee4ed1ebd030f7539b7c6eac
---
M WhoIsWatching_body.php
M i18n/en.json
2 files changed, 93 insertions(+), 49 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WhoIsWatching 
refs/changes/41/279741/1

diff --git a/WhoIsWatching_body.php b/WhoIsWatching_body.php
index 750efd6..205d7fb 100644
--- a/WhoIsWatching_body.php
+++ b/WhoIsWatching_body.php
@@ -3,6 +3,10 @@
 class WhoIsWatching extends SpecialPage {
 
        protected $watchingUsers = [];
+       protected $availableWatchers = false;
+       protected $form;
+       protected $nameType;
+       protected $allowAddingUsers;
 
        public function __construct() {
                parent::__construct( 'WhoIsWatching' );
@@ -40,10 +44,9 @@
 
        public function execute( $par ) {
                $out = $this->getOutput();
-               $title = Title::newFromText( $par );
                $conf = new GlobalVarConfig( "whoiswatching_" );
-               $nameType = $conf->get( "nametype" );
-               $allowAddingPeople = $conf->get( "allowaddingpeople" );
+               $this->nameType = $conf->get( "nametype" );
+               $this->allowAddingUsers = $conf->get( "allowaddingpeople" );
 
                $this->setHeaders();
                if ( $par ) {
@@ -57,11 +60,16 @@
                        return;
                }
 
-               $out->addWikiMsg( 'specialwhoiswatchingthepage', $title );
-               $this->showWatchers( $pageTitle, $nameType );
-               if ( $allowAddingPeople ) {
-                       $this->showForm( $pageTitle );
+               // First load the users so that if they're added to the list
+               // of watchers they won't show up.
+               $this->loadForm( $pageTitle );
+               $this->showWatchers( $pageTitle );
+               $users = $this->getAvailableWatchers();
+               if ( count( $users ) === 0 ) {
+                       $out->addWikiMsg( 'whoiswatchingnousersavailable', 
$title );
+                       return false;
                }
+               $this->showForm( $pageTitle );
        }
 
        protected function backCompatRedir() {
@@ -74,8 +82,8 @@
                if ( $namespace ) {
                        $title = $namespace.':'.$title;
                }
-               $this->getOutput->redirect( $this->getTitleFor( 
$this->getName() )
-                                               ->getSubpage( $title 
)->getFullURL() );
+               $this->getOutput()->redirect( $this->getTitleFor( 
$this->getName() )->
+                                                                         
getSubpage( $title )->getFullURL() );
                return true;
        }
 
@@ -83,52 +91,59 @@
                return wfGetDB( $dbType, $group );
        }
 
-       protected function showWatchers( $pageTitle, $nameType ) {
+       protected function getWatchingUsers( Title $pageTitle ) {
+               if ( $this->watchingUsers ) {
+                       return $this->watchingUsers;
+               }
                $res = $this->getDB()->select(
-                       'watchlist', 'wl_user', [
-                               'wl_namespace' => $pageTitle->getNamespace(),
-                               'wl_title' => $pageTitle->getDBkey(),
-                       ], __METHOD__ );
+                       'watchlist', 'wl_user', [ 'wl_namespace' => 
$pageTitle->getNamespace(),
+                                                                         
'wl_title' => $pageTitle->getDBkey() ],
+                       __METHOD__ );
                foreach ( $res as $row ) {
                        $uid = $row->wl_user;
-                       $thisUser = User::newFromID( $uid );
-                       if ( ( $nameType == 'UserName' ) || 
!$thisUser->getRealName() ) {
-                               $this->watchingUsers[$uid] =
-                                       ':[[User:' . $thisUser->getName() . 
']]';
+                       $this->watchingUsers[$uid] = User::newFromID( $uid );
+                       $this->makeWatcherUnavailable( $uid );
+               }
+               return $this->watchingUsers;
+       }
+
+       protected function makeWatcherUnavailable( $uid ) {
+               unset( $this->availableWatchers[$uid] );
+       }
+
+       protected function showWatchers( Title $pageTitle ) {
+               $this->getOutput()->addWikiText(
+                       implode( "\n", $this->mapWatchingNames( $pageTitle ) )
+               );
+       }
+
+       protected function mapWatchingNames( $pageTitle ) {
+               $names = [];
+               foreach ( $this->getWatchingUsers( $pageTitle ) as $thisUser ) {
+                       if ( ( $this->nameType == 'UserName' ) || 
!$thisUser->getRealName() ) {
+                               $names[ $thisUser->getId() ] = ':[[User:' . 
$thisUser->getName() . ']]';
                        } else {
-                               $this->watchingUsers[$uid] =
+                               $names[ $thisUser->getId() ] =
                                        ':[[User:' . $thisUser->getName() . '|' 
.
                                        $thisUser->getRealName() . ']]';
                        }
                }
-
-               asort( $this->watchingUsers );
-               $this->getOutput()->addWikiText( implode( "\n",
-                                                                               
                  $this->watchingUsers ) );
+               return $names;
        }
 
        protected function showForm( Title $title ) {
-               $users = [];
-               $res = $this->getDB()->select( 'user', 'user_name', '', 
__METHOD__ );
-               foreach ( $res as $row ) {
-                       $u = User::newFromName( $row->user_name );
-                       $uid = $u->getID();
-                       if ( !isset( $this->watchingUsers[$uid] ) &&
-                                $u->isAllowed( 'read' ) &&
-                                $u->getEmail()
-                       ) {
-                               $users[$uid] = $u->getRealName()
-                                                               ? 
$u->getRealName()
-                                                               : $u->getName();
+               if ( $this->allowAddingUsers ) {
+                       $this->getOutput()->addWikiMsg( 
'specialwhoiswatchingaddusers', $title );
+                       $htmlForm = $this->loadForm( $title );
+                       if ( $htmlForm ) {
+                               $htmlForm->show();
                        }
                }
-               asort( $users );
-               $users = array_flip( $users );
-               if ( count( $users ) === 0 ) {
-                       $this->getOutput()->addWikiMsg( 
'whoiswatchingnousersavailable' );
-                       return true;
-               }
-               $this->getOutput()->addWikiMsg( 'specialwhoiswatchingaddusers' 
);
+       }
+
+       protected function loadForm( Title $title ) {
+
+               $users = $this->getAvailableWatchers();
                $formDesc = [ 'users' => [ 'type' => 'multiselect',
                                                                   "options" => 
$users ],
                                          'titleID' => [ 'type' => 'hidden',
@@ -136,15 +151,42 @@
                                          'userID' => [ 'type' => 'hidden',
                                                                        
'default' => $this->getUser()->getID() ],
                ];
-
                $htmlForm = HTMLForm::factory( 'ooui', $formDesc,
                                                                           
$this->getContext(), 'testform' );
                $htmlForm->setSubmitTextMsg( "specialwhoiswatchingaddbtn" );
-               $htmlForm->setSubmitCallback( __CLASS__ . '::handlePost' );
-               $htmlForm->show();
+               $htmlForm->setSubmitCallback( [ $this, 'handlePost' ] );
+
+               // These two steps are neccessary to preload the form values 
from the request
+               $htmlForm->prepareForm();
+               $htmlForm->tryAuthorizedSubmit();
+
+               return $htmlForm;
        }
 
-       public static function handlePost( $formData ) {
+       protected function getAvailableWatchers() {
+               if ( $this->availableWatchers !== false ) {
+                       $ret = array_flip( $this->availableWatchers );
+                       return $ret;
+               }
+               $this->availableWatchers = [];
+               $res = wfGetDB( DB_SLAVE )->select( 'user', 'user_name', '', 
__METHOD__ );
+               foreach ( $res as $row ) {
+                       $u = User::newFromName( $row->user_name );
+                       $uid = $u->getID();
+                       if ( !isset( $this->watchingUsers[$uid] ) &&
+                                                $u->isAllowed( 'read' ) &&
+                                                $u->getEmail()
+                       ) {
+                               $this->availableWatchers[$uid] = 
$u->getRealName()
+                                       ? $u->getRealName()
+                                       : $u->getName();
+                       }
+               }
+               asort( $this->availableWatchers );
+               return array_flip( $this->availableWatchers );
+       }
+
+       public function handlePost( $formData ) {
                $idArray = $formData['users'];
                $pageTitle = Title::newFromID( $formData['titleID'] );
                if ( ! $pageTitle ) {
@@ -157,6 +199,7 @@
                                return "Invalid user ID passed ($id)\n";
                        }
                        $uName->addWatch( $pageTitle );
+                       $this->makeWatcherUnavailable( $id );
                }
                return false;
        }
diff --git a/i18n/en.json b/i18n/en.json
index 189bb1c..da14bdf 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2,11 +2,12 @@
        "@metadata": {
                "authors": []
        },
-       "whoiswatching": "Who is watching a wiki page",
+       "whoiswatching": "Who is watching",
        "whoiswatching-desc": "Provides a listing of usernames watching a wiki 
page",
        "specialwhoiswatchingthepage": "Who is watching: [[$1]]",
        "specialwhoiswatchingusage": "This special page cannot be used on its 
own.\nPlease use the page [[MediaWiki:Number_of_watching_users_pageview]] to 
define an entry point to this special page.",
-       "specialwhoiswatchingaddusers": "Add users to watch the page",
+       "specialwhoiswatchingaddusers": "Add users to watch: [[$1]]",
        "specialwhoiswatchingaddbtn": "Add selected users",
-       "whoiswatchingpageview": 
"[{{fullurl:Special:WhoIsWatching|ns={{NAMESPACEE}}&page={{PAGENAMEE}}}} $1] 
watching {{PLURAL:$1|user|users}}"
+       "whoiswatchingnousersavailable": "All available users are already 
wataching [[$1]]",
+       "whoiswatchingpageview": 
"[{{fullurl:Special:WhoIsWatching/{{FULLPAGENAMEE}}}} $1 watching 
{{PLURAL:$1|user|users}}]"
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/279741
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ied368112cbc45c41ee4ed1ebd030f7539b7c6eac
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WhoIsWatching
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to