Show user names as subpages of special pages in autocomplete search
[lhc/web/wiklou.git] / includes / specials / SpecialUserrights.php
index 1ed392f..cf94e50 100644 (file)
@@ -31,6 +31,10 @@ class UserrightsPage extends SpecialPage {
        # either a GET parameter or a subpage-style parameter, so have a member
        # variable for it.
        protected $mTarget;
+       /*
+        * @var null|User $mFetchedUser The user object of the target username or null.
+        */
+       protected $mFetchedUser = null;
        protected $isself = false;
 
        public function __construct() {
@@ -75,6 +79,8 @@ class UserrightsPage extends SpecialPage {
                // any groups, it's a bit silly to give them the user search prompt.
 
                $user = $this->getUser();
+               $request = $this->getRequest();
+               $out = $this->getOutput();
 
                /*
                 * If the user is blocked and they only have "partial" access
@@ -85,8 +91,6 @@ class UserrightsPage extends SpecialPage {
                        throw new UserBlockedError( $user->getBlock() );
                }
 
-               $request = $this->getRequest();
-
                if ( $par !== null ) {
                        $this->mTarget = $par;
                } else {
@@ -110,13 +114,22 @@ class UserrightsPage extends SpecialPage {
                        $this->isself = true;
                }
 
+               $fetchedStatus = $this->fetchUser( $this->mTarget );
+               if ( $fetchedStatus->isOk() ) {
+                       $this->mFetchedUser = $fetchedStatus->value;
+                       if ( $this->mFetchedUser instanceof User ) {
+                               // Set the 'relevant user' in the skin, so it displays links like Contributions,
+                               // User logs, UserRights, etc.
+                               $this->getSkin()->setRelevantUser( $this->mFetchedUser );
+                       }
+               }
+
                if ( !$this->userCanChangeRights( $user, true ) ) {
                        if ( $this->isself && $request->getCheck( 'success' ) ) {
                                // bug 48609: if the user just removed its own rights, this would
                                // leads it in a "permissions error" page. In that case, show a
                                // message that it can't anymore use this page instead of an error
                                $this->setHeaders();
-                               $out = $this->getOutput();
                                $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", 'userrights-removed-self' );
                                $out->returnToMain();
 
@@ -128,12 +141,19 @@ class UserrightsPage extends SpecialPage {
                        throw new PermissionsError( null, array( array( $msg ) ) );
                }
 
+               // show a successbox, if the user rights was saved successfully
+               if ( $request->getCheck( 'success' ) && $this->mFetchedUser !== null ) {
+                       $out->wrapWikiMsg(
+                               "<div class=\"successbox\">\n$1\n</div>",
+                               array( 'savedrights', $this->mFetchedUser->getName() )
+                       );
+               }
+
                $this->checkReadOnly();
 
                $this->setHeaders();
                $this->outputHeader();
 
-               $out = $this->getOutput();
                $out->addModuleStyles( 'mediawiki.special' );
                $this->addHelpLink( 'Help:Assigning permissions' );
 
@@ -149,14 +169,13 @@ class UserrightsPage extends SpecialPage {
                        $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget )
                ) {
                        // save settings
-                       $status = $this->fetchUser( $this->mTarget );
-                       if ( !$status->isOK() ) {
-                               $this->getOutput()->addWikiText( $status->getWikiText() );
+                       if ( !$fetchedStatus->isOK() ) {
+                               $this->getOutput()->addWikiText( $fetchedStatus->getWikiText() );
 
                                return;
                        }
 
-                       $targetUser = $status->value;
+                       $targetUser = $this->mFetchedUser;
                        if ( $targetUser instanceof User ) { // UserRightsProxy doesn't have this method (bug 61252)
                                $targetUser->clearInstanceCache(); // bug 38989
                        }
@@ -444,8 +463,10 @@ class UserrightsPage extends SpecialPage {
                                30,
                                str_replace( '_', ' ', $this->mTarget ),
                                array(
-                                       'autofocus' => '',
                                        'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
+                               ) + (
+                                       // Set autofocus on blank input and error input
+                                       $this->mFetchedUser === null ? array( 'autofocus' => '' ) : array()
                                )
                        ) . ' ' .
                        Xml::submitButton( $this->msg( 'editusergroup' )->text() ) .
@@ -755,6 +776,24 @@ class UserrightsPage extends SpecialPage {
                LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
        }
 
+       /**
+        * Return an array of subpages beginning with $search that this special page will accept.
+        *
+        * @param string $search Prefix to search for
+        * @param int $limit Maximum number of results to return (usually 10)
+        * @param int $offset Number of results to skip (usually 0)
+        * @return string[] Matching subpages
+        */
+       public function prefixSearchSubpages( $search, $limit, $offset ) {
+               $user = User::newFromName( $search );
+               if ( !$user ) {
+                       // No prefix suggestion for invalid user
+                       return array();
+               }
+               // Autocomplete subpage as user list - public to allow caching
+               return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
+       }
+
        protected function getGroupName() {
                return 'users';
        }