Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / includes / specials / pagers / UsersPager.php
index adc1dd8..45d9a7f 100644 (file)
@@ -33,7 +33,7 @@
 class UsersPager extends AlphabeticPager {
 
        /**
-        * @var array A array with user ids as key and a array of groups as value
+        * @var array[] A array with user ids as key and a array of groups as value
         */
        protected $userGroupCache;
 
@@ -100,7 +100,7 @@ class UsersPager extends AlphabeticPager {
         * @return array
         */
        function getQueryInfo() {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
                $conds = [];
 
                // Don't show hidden names
@@ -112,6 +112,7 @@ class UsersPager extends AlphabeticPager {
 
                if ( $this->requestedGroup != '' ) {
                        $conds['ug_group'] = $this->requestedGroup;
+                       $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
                }
 
                if ( $this->requestedUser != '' ) {
@@ -161,7 +162,7 @@ class UsersPager extends AlphabeticPager {
         * @return string
         */
        function formatRow( $row ) {
-               if ( $row->user_id == 0 ) { # Bug 16487
+               if ( $row->user_id == 0 ) { # T18487
                        return '';
                }
 
@@ -177,12 +178,12 @@ class UsersPager extends AlphabeticPager {
                $lang = $this->getLanguage();
 
                $groups = '';
-               $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
+               $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
 
-               if ( !$this->including && count( $groups_list ) > 0 ) {
+               if ( !$this->including && count( $ugms ) > 0 ) {
                        $list = [];
-                       foreach ( $groups_list as $group ) {
-                               $list[] = self::buildGroupLink( $group, $userName );
+                       foreach ( $ugms as $ugm ) {
+                               $list[] = $this->buildGroupLink( $ugm, $userName );
                        }
                        $groups = $lang->commaList( $list );
                }
@@ -228,18 +229,21 @@ class UsersPager extends AlphabeticPager {
                }
 
                // Lookup groups for all the users
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
                $groupRes = $dbr->select(
                        'user_groups',
-                       [ 'ug_user', 'ug_group' ],
+                       UserGroupMembership::selectFields(),
                        [ 'ug_user' => $userIds ],
                        __METHOD__
                );
                $cache = [];
                $groups = [];
                foreach ( $groupRes as $row ) {
-                       $cache[intval( $row->ug_user )][] = $row->ug_group;
-                       $groups[$row->ug_group] = true;
+                       $ugm = UserGroupMembership::newFromRow( $row );
+                       if ( !$ugm->isExpired() ) {
+                               $cache[$row->ug_user][$row->ug_group] = $ugm;
+                               $groups[$row->ug_group] = true;
+                       }
                }
 
                // Give extensions a chance to add things like global user group data
@@ -250,7 +254,7 @@ class UsersPager extends AlphabeticPager {
 
                // Add page of groups to link batch
                foreach ( $groups as $group => $unused ) {
-                       $groupPage = User::getGroupPage( $group );
+                       $groupPage = UserGroupMembership::getGroupPage( $group );
                        if ( $groupPage ) {
                                $batch->addObj( $groupPage );
                        }
@@ -266,71 +270,89 @@ class UsersPager extends AlphabeticPager {
        function getPageHeader() {
                list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
 
-               $this->getOutput()->addModules( 'mediawiki.userSuggest' );
-
-               # Form tag
-               $out = Xml::openElement(
-                               'form',
-                               [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
-                       ) .
-                       Xml::fieldset( $this->msg( 'listusers' )->text() ) .
-                       Html::hidden( 'title', $self );
-
-               # Username field (with autocompletion support)
-               $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
-                       Html::input(
-                               'username',
-                               $this->requestedUser,
-                               'text',
-                               [
-                                       'class' => 'mw-autocomplete-user',
-                                       'id' => 'offset',
-                                       'size' => 20,
-                                       'autofocus' => $this->requestedUser === ''
-                               ]
-                       ) . ' ';
-
-               # Group drop-down list
-               $sel = new XmlSelect( 'group', 'group', $this->requestedGroup );
-               $sel->addOption( $this->msg( 'group-all' )->text(), '' );
+               $groupOptions = [ $this->msg( 'group-all' )->text() => '' ];
                foreach ( $this->getAllGroups() as $group => $groupText ) {
-                       $sel->addOption( $groupText, $group );
+                       $groupOptions[ $groupText ] = $group;
                }
 
-               $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
-               $out .= $sel->getHTML() . '<br />';
-               $out .= Xml::checkLabel(
-                       $this->msg( 'listusers-editsonly' )->text(),
-                       'editsOnly',
-                       'editsOnly',
-                       $this->editsOnly
-               );
-               $out .= '&#160;';
-               $out .= Xml::checkLabel(
-                       $this->msg( 'listusers-creationsort' )->text(),
-                       'creationSort',
-                       'creationSort',
-                       $this->creationSort
-               );
-               $out .= '&#160;';
-               $out .= Xml::checkLabel(
-                       $this->msg( 'listusers-desc' )->text(),
-                       'desc',
-                       'desc',
-                       $this->mDefaultDirection
-               );
-               $out .= '<br />';
+               $formDescriptor = [
+                       'user' => [
+                               'class' => 'HTMLUserTextField',
+                               'label' => $this->msg( 'listusersfrom' )->text(),
+                               'name' => 'username',
+                               'default' => $this->requestedUser,
+                       ],
+                       'dropdown' => [
+                               'label' => $this->msg( 'group' )->text(),
+                               'name' => 'group',
+                               'default' => $this->requestedGroup,
+                               'class' => 'HTMLSelectField',
+                               'options' => $groupOptions,
+                       ],
+                       'editsOnly' => [
+                               'type' => 'check',
+                               'label' => $this->msg( 'listusers-editsonly' )->text(),
+                               'name' => 'editsOnly',
+                               'id' => 'editsOnly',
+                               'default' => $this->editsOnly
+                       ],
+                       'creationSort' => [
+                               'type' => 'check',
+                               'label' => $this->msg( 'listusers-creationsort' )->text(),
+                               'name' => 'creationSort',
+                               'id' => 'creationSort',
+                               'default' => $this->creationSort
+                       ],
+                       'desc' => [
+                               'type' => 'check',
+                               'label' => $this->msg( 'listusers-desc' )->text(),
+                               'name' => 'desc',
+                               'id' => 'desc',
+                               'default' => $this->mDefaultDirection
+                       ],
+                       'limithiddenfield' => [
+                               'class' => 'HTMLHiddenField',
+                               'name' => 'limit',
+                               'default' => $this->mLimit
+                       ]
+               ];
 
-               Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
+               $beforeSubmitButtonHookOut = '';
+               Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$beforeSubmitButtonHookOut ] );
 
-               # Submit button and form bottom
-               $out .= Html::hidden( 'limit', $this->mLimit );
-               $out .= Xml::submitButton( $this->msg( 'listusers-submit' )->text() );
-               Hooks::run( 'SpecialListusersHeader', [ $this, &$out ] );
-               $out .= Xml::closeElement( 'fieldset' ) .
-                       Xml::closeElement( 'form' );
+               if ( $beforeSubmitButtonHookOut !== '' ) {
+                       $formDescriptior[ 'beforeSubmitButtonHookOut' ] = [
+                               'class' => 'HTMLInfoField',
+                               'raw' => true,
+                               'default' => $beforeSubmitButtonHookOut
+                       ];
+               }
+
+               $formDescriptor[ 'submit' ] = [
+                       'class' => 'HTMLSubmitField',
+                       'buttonlabel-message' => 'listusers-submit',
+               ];
+
+               $beforeClosingFieldsetHookOut = '';
+               Hooks::run( 'SpecialListusersHeader', [ $this, &$beforeClosingFieldsetHookOut ] );
+
+               if ( $beforeClosingFieldsetHookOut !== '' ) {
+                       $formDescriptior[ 'beforeClosingFieldsetHookOut' ] = [
+                               'class' => 'HTMLInfoField',
+                               'raw' => true,
+                               'default' => $beforeClosingFieldsetHookOut
+                       ];
+               }
 
-               return $out;
+               $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
+               $htmlForm
+                       ->setMethod( 'get' )
+                       ->setAction( Title::newFromText( $self )->getLocalURL() )
+                       ->setId( 'mw-listusers-form' )
+                       ->setFormIdentifier( 'mw-listusers-form' )
+                       ->suppressDefaultSubmit()
+                       ->setWrapperLegendMsg( 'listusers' );
+               return $htmlForm->prepareForm()->getHTML( true );
        }
 
        /**
@@ -340,7 +362,7 @@ class UsersPager extends AlphabeticPager {
        function getAllGroups() {
                $result = [];
                foreach ( User::getAllGroups() as $group ) {
-                       $result[$group] = User::getGroupName( $group );
+                       $result[$group] = UserGroupMembership::getGroupName( $group );
                }
                asort( $result );
 
@@ -365,36 +387,30 @@ class UsersPager extends AlphabeticPager {
        }
 
        /**
-        * Get a list of groups the specified user belongs to
+        * Get an associative array containing groups the specified user belongs to,
+        * and the relevant UserGroupMembership objects
         *
         * @param int $uid User id
-        * @param array|null $cache
-        * @return array
+        * @param array[]|null $cache
+        * @return UserGroupMembership[] (group name => UserGroupMembership object)
         */
-       protected static function getGroups( $uid, $cache = null ) {
+       protected static function getGroupMemberships( $uid, $cache = null ) {
                if ( $cache === null ) {
                        $user = User::newFromId( $uid );
-                       $effectiveGroups = $user->getEffectiveGroups();
+                       return $user->getGroupMemberships();
                } else {
-                       $effectiveGroups = isset( $cache[$uid] ) ? $cache[$uid] : [];
+                       return isset( $cache[$uid] ) ? $cache[$uid] : [];
                }
-               $groups = array_diff( $effectiveGroups, User::getImplicitGroups() );
-
-               return $groups;
        }
 
        /**
         * Format a link to a group description page
         *
-        * @param string $group Group name
+        * @param string|UserGroupMembership $group Group name or UserGroupMembership object
         * @param string $username Username
         * @return string
         */
-       protected static function buildGroupLink( $group, $username ) {
-               return User::makeGroupLinkHTML(
-                       $group,
-                       User::getGroupMember( $group, $username )
-               );
+       protected function buildGroupLink( $group, $username ) {
+               return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
        }
-
 }