MarkAHershberger has uploaded a new change for review.

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

Change subject: Switch to HTMLForm
......................................................................

Switch to HTMLForm

Add instance variable to pass information between methods.

Change-Id: I2d5576f11444658bc0af565e0481f82954f16933
---
M WhoIsWatching_body.php
1 file changed, 89 insertions(+), 59 deletions(-)


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

diff --git a/WhoIsWatching_body.php b/WhoIsWatching_body.php
index db900a2..fab77ca 100644
--- a/WhoIsWatching_body.php
+++ b/WhoIsWatching_body.php
@@ -2,6 +2,8 @@
 
 class WhoIsWatching extends SpecialPage {
 
+       protected $watchingUsers = [];
+
        public function __construct() {
                parent::__construct( 'WhoIsWatching' );
                return true;
@@ -38,7 +40,6 @@
 
        public function execute( $par ) {
                $out = $this->getOutput();
-               $user = $this->getUser();
                $req = $this->getRequest();
                $title = Title::newFromText( $par );
                $conf = new GlobalVarConfig( "whoiswatching_" );
@@ -57,69 +58,98 @@
                        }
                }
                $pageTitle = Title::newFromText( $title );
-               if ( !$title || !$pageTitle ) {
+               if ( !$pageTitle ) {
                        $out->addWikiMsg( 'specialwhoiswatchingusage' );
                        return;
                }
 
-               if ( $allowAddingPeople &&
-                       $req->wasPosted() &&
-                       $user->matchEditToken( $req->getVal( 'token' ) ) ) {
-                       $idArray = $req->getArray( 'idArray' );
-                       foreach ( $idArray as $name => $id ) {
-                               #$out->addWikiText("* Adding name $name userid 
$id to watchlist\n");
-                               $u = User::newFromId( $id );
-                               $u->addWatch( $pageTitle );
-                       }
-                       $out->redirect( Title::makeTitle( NS_SPECIAL, 
'WhoIsWatching' )->getLocalUrl(
-                               array( 'page' => $title )
-                       ) );
-                       return;
-               }
-               $this->getOutput()->addWikiMsg( 'specialwhoiswatchingthepage', 
$title );
-
-               $dbr = wfGetDB( DB_SLAVE );
-               $watchingusers = array();
-               $res = $dbr->select(
-                       'watchlist', 'wl_user', array(
-                               'wl_namespace' => $pageTitle->getNamespace(),
-                               'wl_title' => $pageTitle->getDBkey(),
-                       ), __METHOD__ );
-               foreach ( $res as $row ) {
-                       $u = User::newFromID( $row->wl_user );
-                       if ( ( $nameType == 'UserName' ) || !$u->getRealName() 
) {
-                               $watchingusers[$row->wl_user] = ":[[User:" . 
$u->getName() . "]]";
-                       } else {
-                               $watchingusers[$row->wl_user] = ":[[:User:" . 
$u->getName() . "|" . $u->getRealName() . "]]";
-                       }
-               }
-
-               asort( $watchingusers );
-               $out->addWikiText( implode( "\n", $watchingusers ) );
-
+               $out->addWikiMsg( 'specialwhoiswatchingthepage', $title );
+               $this->showWatchers( $pageTitle, $nameType );
                if ( $allowAddingPeople ) {
-                       $out->addWikiMsg( 'specialwhoiswatchingaddusers' );
-                       $out->addHTML( "<form method=\"post\">" );
-                       $out->addHTML( "<input type=\"hidden\" 
value=\"".$user->getEditToken()."\" name=\"token\" />" );
-                       $out->addHTML( "<div style=\"border: thin solid 
#000000\"><table cellpadding=\"15\" cellspacing=\"0\" border=\"0\">" );
-                       $out->addHTML( "<tr><td>" );
-                       $out->addHTML( '<select name="idArray[]" size="12" 
multiple="multiple">' );
-                       $users = array();
-                       $res = $dbr->select( 'user', 'user_name', '', 
__METHOD__);
-                       foreach ( $res as $row ) {
-                               $u = User::newFromName( $row->user_name );
-                               if ( !array_key_exists( $u->getID(), 
$watchingusers ) &&
-                                       $u->isAllowed( 'read' ) && 
$u->getEmail() ) {
-                                       $users[ $u->getID() ] = 
$u->getRealName() ? $u->getRealName() : $u->getName();
-                               }
-                       }
-                       asort( $users );
-                       foreach ( $users as $id => $name ) {
-                               $out->addHTML( "<option 
value=\"".$id."\">".$name."</option>" );
-                       }
-                       $out->addHTML( '</select></td><td>' );
-                       $out->addHTML( "<input type=\"submit\" 
value=\"".$this->msg( 'specialwhoiswatchingaddbtn' )->escaped()."\" />" );
-                       $out->addHTML( "</td></tr></table></div></form>" );
+                       $this->showForm( $pageTitle );
                }
        }
+
+       protected function getDB( $dbType = DB_SLAVE, $group = "whoWatches" ) {
+               return wfGetDB( $dbType, $group );
+       }
+
+       protected function showWatchers( $pageTitle, $nameType ) {
+               $res = $this->getDB()->select(
+                       '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() . 
']]';
+                       } else {
+                               $this->watchingUsers[$uid] =
+                                       ':[[User:' . $thisUser->getName() . '|' 
.
+                                       $thisUser->getRealName() . ']]';
+                       }
+               }
+
+               asort( $this->watchingUsers );
+               $this->getOutput()->addWikiText( implode( "\n",
+                                                                               
                  $this->watchingUsers ) );
+       }
+
+       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();
+                       }
+               }
+               asort( $users );
+               $users = array_flip( $users );
+               if ( count( $users ) === 0 ) {
+                       $this->getOutput()->addWikiMsg( 
'whoiswatchingnousersavailable' );
+                       return true;
+               }
+               $this->getOutput()->addWikiMsg( 'specialwhoiswatchingaddusers' 
);
+               $formDesc = [ 'users' => [ 'type' => 'multiselect',
+                                                                  "options" => 
$users ],
+                                         'titleID' => [ 'type' => 'hidden',
+                                                                        
'default' => $title->getArticleID() ],
+                                         'userID' => [ 'type' => 'hidden',
+                                                                       
'default' => $this->getUser()->getID() ],
+               ];
+
+               $htmlForm = HTMLForm::factory( 'ooui', $formDesc,
+                                                                          
$this->getContext(), 'testform' );
+               $htmlForm->setSubmitTextMsg( "specialwhoiswatchingaddbtn" );
+               $htmlForm->setSubmitCallback( __CLASS__ . '::handlePost' );
+               $htmlForm->show();
+       }
+
+       public static function handlePost( $formData ) {
+               $idArray = $formData['users'];
+               $pageTitle = Title::newFromID( $formData['titleID'] );
+               if ( ! $pageTitle ) {
+                       return "Invalid title id passed: 
{$formData['titleID']}\n";
+               }
+
+               foreach ( $idArray as $id ) {
+                       $uName = User::newFromId( $id );
+                       if ( ! $uName ) {
+                               return "Invalid user ID passed ($id)\n";
+                       }
+                       $uName->addWatch( $pageTitle );
+               }
+               return false;
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2d5576f11444658bc0af565e0481f82954f16933
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