MarkAHershberger has uploaded a new change for review.

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

Change subject: Many improvements
......................................................................

Many improvements

* Add completion for title selection and user selection, etc

Change-Id: I4a79f948aead0cf8fb67dfe73158011b9d17dd84
---
M WhoIsWatching.php
M WhoIsWatching_body.php
M i18n/en.json
3 files changed, 200 insertions(+), 70 deletions(-)


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

diff --git a/WhoIsWatching.php b/WhoIsWatching.php
index 681b8b2..12bcdad 100644
--- a/WhoIsWatching.php
+++ b/WhoIsWatching.php
@@ -37,10 +37,14 @@
 # Set the following to either True or False to optionally display a count of 
zero users watching a particular page
 $whoiswatching_showifzero = true;
 
+$wgGroupPermissions['sysop']['addpagetoanywatchlist']         = true;
+$wgGroupPermissions['sysop']['seepagewatchers']               = true;
+
 function fnShowWatchingCount( &$template, &$tpl ) {
        global $wgLang, $wgPageShowWatchingUsers, $whoiswatching_showifzero, 
$wgOut;
 
-       if ( $wgPageShowWatchingUsers && $whoiswatching_showifzero ) {
+    if ( $wgOut->getTitle()->getNamespace() >= 0 &&
+         $wgPageShowWatchingUsers && $whoiswatching_showifzero ) {
                $dbr = wfGetDB( DB_SLAVE );
                $watchlist = $dbr->tableName( 'watchlist' );
                $t = $template->getTitle();
@@ -51,7 +55,7 @@
                $x = $dbr->fetchObject( $res );
                $numberofwatchingusers = $x->n;
                $msg = wfMsgExt(
-                       'number_of_watching_users_pageview', array( 
'parseinline' ),
+                       'whoiswatching_users_pageview', array( 'parseinline' ),
                        $wgLang->formatNum( $numberofwatchingusers )
                );
                $tpl->set( 'numberofwatchingusers', $msg );
diff --git a/WhoIsWatching_body.php b/WhoIsWatching_body.php
index f5c99de..b36a6a6 100644
--- a/WhoIsWatching_body.php
+++ b/WhoIsWatching_body.php
@@ -2,92 +2,204 @@
 
 class WhoIsWatching extends SpecialPage {
 
+       protected $targetPage = null;
+
        function __construct() {
-               parent::__construct( 'WhoIsWatching' );
+               parent::__construct( 'whoiswatching' );
                return true;
        }
 
        function execute( $par ) {
-               global $wgRequest, $wgOut, $wgUser;
-               global $whoiswatching_nametype, 
$whoiswatching_allowaddingpeople;
+               parent::execute( $par );
 
-               $this->setHeaders();
-               $wgOut->setPagetitle( wfMsg( 'whoiswatching' ) );
-
-               $title = $wgRequest->getVal( 'page' );
-               $ns = $wgRequest->getVal( 'ns', '' );
-               if ( $ns !== '' ) {
-                       $title = $ns.':'.$title;
-               }
-               $pageTitle = Title::newFromText( $title );
-               if ( !$title || !$pageTitle ) {
-                       $wgOut->addWikiMsg( 'specialwhoiswatchingusage' );
-                       return;
-               }
-
-               if ( $whoiswatching_allowaddingpeople &&
-                       $wgRequest->wasPosted() &&
-                       $wgUser->matchEditToken( $wgRequest->getVal( 'token' ) 
) ) {
-                       $idArray = $wgRequest->getArray( 'idArray' );
-                       foreach ( $idArray as $name => $id ) {
-                               #$wgOut->addWikiText("* Adding name $name 
userid $id to watchlist\n");
-                               $u = User::newFromId( $id );
-                               $u->addWatch( $pageTitle );
+               $this->getOutput()->setPagetitle( wfMsg( 'whoiswatching' ) );
+               if ( $this->getTargetPage( $par ) ) {
+                       if ( $this->addWatchersForm() ) {
+                               return true;
                        }
-                       $wgOut->redirect( Title::makeTitle( NS_SPECIAL, 
'WhoIsWatching' )->getLocalUrl(
-                               array( 'page' => $title )
-                       ) );
+                       $this->showWatchingUsers( $this->targetPage );
+               }
+       }
+
+       public function checkPermissions() {
+               if ( $this->getUser()->isAllowed( "seepagewatchers" ) ||
+                        $this->getUser()->isAllowed( "addpagetoanywatchlist" ) 
) {
+                       return true;
+               }
+               throw new ErrorPageError(
+                       "whoiswatching-permission-denied-title", 
"whoiswatching-permission-denied",
+                       [ $this->getLanguage()->commaList( [ "seepagewatchers", 
"addpagetoanywatchlist" ] ) ]
+               );
+       }
+
+       protected function getTargetPage( $par ) {
+               $title = $this->getRequest()->getVal( 'page' );
+               if ( !$title && !$par ) {
+                       return $this->pickPage();
+               }
+
+               if ( $title ) {
+                       $nsRevLookup = array_flip( 
MWNamespace::getCanonicalNamespaces() );
+                       $ns = $this->getRequest()->getVal( 'ns', '' );
+                       if ( !ctype_digit( $ns ) ) {
+                               $ns = isset( $nsRevLookup[ $ns ] ) ? 
$nsRevLookup[ $ns ] : null;
+                       }
+                       $this->targetPage = Title::newFromText( $title, $ns );
+               } else {
+                       $this->targetPage = Title::newFromText( $par );
+               }
+
+               if ( !$this->targetPage ) {
+                       throw new ErrorPageError( "whoiswatching-usage-title", 
"specialwhoiswatchingusage" );
+               }
+               $ns = $this->targetPage->getNamespace();
+               if ( $ns < 0 ) {
+                       throw new ErrorPageError(
+                               "whoiswatching-not-possible-title", 
"whoiswatching-not-possible", [ $this->targetPage ]
+                       );
+               }
+
+               return true;
+       }
+
+       protected function addWatchersForm() {
+               global $whoiswatching_allowaddingpeople;
+
+               if ( $whoiswatching_allowaddingpeople === false &&
+                        !$this->getUser()->isAllowed( "addpagetoanywatchlist" 
) ) {
+                       return false;
+               }
+
+               $this->getOutput()->addModules( 'mediawiki.userSuggest' );
+
+               $this->getOutput()->addHTML(
+                       Html::openElement(
+                               'form',
+                               [ 'method' => 'post',
+                                 'action' => $this->getPageTitle( 
$this->targetPage )->getLocalUrl(),
+                                 'name' => 'uluser',
+                                 'id' => 'mw-whoiswatching-form1' ]
+                       ) .
+                       Html::hidden( 'title', 
$this->getPageTitle()->getPrefixedText() ) .
+                       Html::hidden( 'addToken', 
$this->getUser()->getEditToken( __CLASS__ ) ) .
+                       Xml::fieldset( $this->msg( 'whoiswatching-lookup-user' 
)->text() ) .
+                       Xml::inputLabel(
+                               $this->msg( 'whoiswatching-user-editname' 
)->text(),
+                               'user',
+                               'username',
+                               30,
+                               '',
+                               [ 'autofocus' => true,
+                                 'class' => 'mw-autocomplete-user' ] // used 
by mediawiki.userSuggest
+                       ) . ' ' .
+                       Xml::submitButton( $this->msg( 'whoiswatching-adduser' 
)->text() ) .
+                       Html::closeElement( 'fieldset' ) .
+                       Html::closeElement( 'form' ) . "\n"
+               );
+
+               if ( $this->maybeAddWatcher() ) {
+                       # provide some UI feedback
+               }
+               return false;
+       }
+
+       protected function maybeAddWatcher() {
+               $req = $this->getRequest();
+               $token = $req->getVal( 'addToken' );
+               if ( $req->wasPosted() && $token ) {
+                       if ( $this->getUser()->matchEditToken( $token, 
__CLASS__ ) ) {
+                               $user = User::newFromName( $req->getVal( 'user' 
) );
+                               $title = $this->targetPage;
+                               $user->addWatch( $title );
+                               $this->getOutput()->redirect( 
$this->getPageTitle( $title )->getLocalUrl() );
+                               $this->notifyUser( 'add', $title, $user );
+                               return true;
+                       }
+
+                       $this->getOutput()->showFatalError(
+                               "<span class='error'>" . wfMessage( 
"whoiswatching-token-mismatch" )
+                               . "</span>"
+                       );
+                       return false;
+               }
+               return true;
+       }
+
+       protected function notifyUser( $action, Title $title, User $user ) {
+       }
+
+       protected function pickPage() {
+               $target = $this->getRequest()->getVal( "target" );
+               if ( $target ) {
+                       $this->getOutput()->redirect( $this->getPageTitle( 
$target )->getLocalUrl() );
+                       return false;
+               }
+               $this->getOutput()->addHTML(
+                       Html::openElement(
+                               'form',
+                               [ 'method' => 'get',
+                                 'action' => $this->getPageTitle( $target 
)->getLocalUrl(),
+                                 'name' => 'uluser',
+                                 'id' => 'mw-whoiswatching-form1' ] ) .
+                       Html::hidden( 'title', 
$this->getPageTitle()->getPrefixedText() ) .
+                       Xml::fieldset( $this->msg( 'whoiswatching-lookup-title' 
)->text() ) .
+                       Xml::inputLabel( $this->msg( 'whoiswatching-title' 
)->text(), 'target',
+                                                        
'whoiswatching-target', 40,
+                                                        str_replace( '_', ' ', 
$this->targetPage ),
+                                                        [ 'class' => 
'mw-searchInput' ] ) . ' ' .
+                       Xml::submitButton( $this->msg( 
'whoiswatching-select-title' )->text() ) .
+                       Html::closeElement( 'fieldset' ) .
+                       Html::closeElement( 'form' ) . "\n"
+               );
+               return false;
+       }
+
+       public function maybeRemoveWatcher( array $formData ) {
+               foreach( $formData as $watcherID => $remove ) {
+                       if ( $remove ) {
+                               $watcher = User::newFromId( $watcherID );
+                               $watcher->removeWatch( $this->targetPage );
+                               wfGetDB( DB_MASTER )->commit();
+                               $this->notifyUser( 'remove', $this->targetPage, 
$watcher );
+                       }
+               }
+       }
+
+       protected function showWatchingUsers( ) {
+               global $whoiswatching_nametype;
+
+               if ( !$this->getUser()->isAllowed( "seepagewatchers" ) ) {
                        return;
                }
 
-               $wgOut->addWikiText( "== ".sprintf( wfMsg( 
'specialwhoiswatchingthepage' ), "[[:$pageTitle]] ==" ) );
+               $out = $this->getOutput();
+               $out->addWikiText(
+                       "== ". wfMessage( 'specialwhoiswatchingthepage' 
)->params( $this->targetPage )->plain() . " =="
+               );
 
                $dbr = wfGetDB( DB_SLAVE );
-               $watchingusers = array();
+               $watchingusers = [];
                $res = $dbr->select(
-                       'watchlist', 'wl_user', array(
-                               'wl_namespace' => $pageTitle->getNamespace(),
-                               'wl_title' => $pageTitle->getDBkey(),
-                       ), __METHOD__ );
+                       'watchlist', 'wl_user', [ 'wl_namespace' => 
$this->targetPage->getNamespace(),
+                                                                         
'wl_title' => $this->targetPage->getDBkey() ], __METHOD__ );
                foreach ( $res as $row ) {
                        $u = User::newFromID( $row->wl_user );
+                       $key = $u->mId;
+                       $display = $u->getRealName();
                        if ( ( $whoiswatching_nametype == 'UserName' ) || 
!$u->getRealName() ) {
-                               $watchingusers[$row->wl_user] = ":[[User:" . 
$u->getName() . "]]";
-                       } else {
-                               $watchingusers[$row->wl_user] = ":[[:User:" . 
$u->getName() . "|" . $u->getRealName() . "]]";
+                               $display = $u->getName();
                        }
+                       $watchingusers[$key] = $display;
                }
 
                asort( $watchingusers );
-               $out = '';
-               foreach ( $watchingusers as $id => $link ) {
-                       $out .= "$link\n";
-               }
-               $wgOut->addWikiText( $out );
 
-               if ( $whoiswatching_allowaddingpeople ) {
-                       $wgOut->addWikiText( "== ".wfMsg( 
'specialwhoiswatchingaddusers')." ==" );
-                       $wgOut->addHTML( "<form method=\"post\">" );
-                       $wgOut->addHTML( "<input type=\"hidden\" 
value=\"".$wgUser->getEditToken()."\" name=\"token\" />" );
-                       $wgOut->addHTML( "<div style=\"border: thin solid 
#000000\"><table cellpadding=\"15\" cellspacing=\"0\" border=\"0\">" );
-                       $wgOut->addHTML( "<tr><td>" );
-                       $wgOut->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 ) {
-                               $wgOut->addHTML( "<option 
value=\"".$id."\">".$name."</option>" );
-                       }
-                       $wgOut->addHTML( '</select></td><td>' );
-                       $wgOut->addHTML( "<input type=\"submit\" 
value=\"".wfMsg( 'specialwhoiswatchingaddbtn' )."\" />" );
-                       $wgOut->addHTML( "</td></tr></table></div></form>" );
+               foreach ( $watchingusers as $id => $link ) {
+                       $users[ $id ] = [ 'type' => 'check', 'label' => $link ];
                }
+               $form = new HTMLForm( $users, $this->getContext() );
+               $form->setSubmitText( $this->msg( 'whoiswatching-deluser' 
)->text() );
+               $form->setSubmitCallback( [ $this, 'maybeRemoveWatcher' ] );
+               $form->show();
        }
 }
diff --git a/i18n/en.json b/i18n/en.json
index 00d10ea..871e067 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2,10 +2,24 @@
     "@metadata": {
         "authors": []
     },
+    "whoiswatching_users_pageview": 
"[{{fullurl:Special:WhoIsWatching/{{FULLPAGENAMEE}}}} $1] watching 
{{PLURAL:$1|user|users}}",
     "whoiswatching": "Who is watching a wiki page",
+    "whoiswatching-not-possible-title": "Impossible page to watch",
+    "whoiswatching-not-possible": "It is not possible to watch the [[$1]] 
page.",
     "whoiswatching-desc": "Provides a listing of usernames watching a wiki 
page",
-    "specialwhoiswatchingthepage": "Who is watching %s",
-    "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.",
+    "specialwhoiswatchingthepage": "Watchers on [[$1]]",
+    "whoiswatching-usage-title": "How to use this page",
+    "specialwhoiswatchingusage": "This special page cannot be used on its 
own.",
     "specialwhoiswatchingaddusers": "Add users to watch the page",
-    "specialwhoiswatchingaddbtn": "Add selected users"
+    "specialwhoiswatchingaddbtn": "Add selected users",
+    "whoiswatching-user-editname": "Type a username:",
+    "whoiswatching-adduser": "Add user",
+    "whoiswatching-deluser": "Remove user",
+    "whoiswatching-lookup-user": "User lookup",
+    "whoiswatching-permission-denied-title": "Permission denied",
+    "whoiswatching-permission-denied": "You can't do anything on this 
page.\n\nAt lease one of these permissions are needed: $1",
+    "whoiswatching-token-mismatch": "Token mismatch.",
+    "whoiswatching-lookup-title": "Title lookup",
+    "whoiswatching-title": "Type a title",
+    "whoiswatching-select-title": "See watchlist"
 }

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

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