Skip to content

Commit

Permalink
Improve user store
Browse files Browse the repository at this point in the history
  • Loading branch information
it-spiderman committed Dec 3, 2024
1 parent 5ef437c commit fffb298
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
return;
}

define( 'MWSTAKE_MEDIAWIKI_COMPONENT_COMMONWEBAPIS_VERSION', '2.0.29' );
define( 'MWSTAKE_MEDIAWIKI_COMPONENT_COMMONWEBAPIS_VERSION', '2.0.30' );

MWStake\MediaWiki\ComponentLoader\Bootstrapper::getInstance()
->register( 'commonwebapis', static function () {
Expand Down
48 changes: 40 additions & 8 deletions src/Data/UserQueryStore/PrimaryDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MWStake\MediaWiki\Component\CommonWebAPIs\Data\UserQueryStore;

use GlobalVarConfig;
use MediaWiki\Message\Message;
use MWStake\MediaWiki\Component\DataStore\Filter;
use MWStake\MediaWiki\Component\DataStore\PrimaryDatabaseDataProvider;
use MWStake\MediaWiki\Component\DataStore\ReaderParams;
Expand Down Expand Up @@ -45,6 +46,29 @@ private function getSupportingData() {
$this->blocks = $this->getBlocks();
}

/**
* @return array
*/
public function getGroupBuckets(): array {
$groupBlacklist = $this->mwsgConfig->get( 'CommonWebAPIsComponentUserStoreExcludeGroups' );
$res = $this->db->select(
'user_groups',
[ 'DISTINCT ug_group' ],
[
'ug_group NOT IN (' . $this->db->makeList( $groupBlacklist ) . ')',
],
__METHOD__,
[ 'GROUP BY' => 'ug_group' ]
);
$groups = [];
foreach ( $res as $row ) {
$msg = Message::newFromKey( 'group-' . $row->ug_group );
$groups[$row->ug_group] = $msg->exists() ? $msg->text() : $row->ug_group;
}

return $groups;
}

/**
* @return array
*/
Expand All @@ -69,10 +93,17 @@ private function getGroups() {
* @return array
*/
private function getBlocks() {
$res = $this->db->select(
[ 'b' => 'block', 'bt' => 'block_target' ],
[ 'bt_user' ],
[],
__METHOD__,
[],
[ 'b' => [ 'INNER JOIN', 'bt.bt_id = b.bl_target' ] ]
);
$blocks = [];
$blocksRes = $this->db->select( 'ipblocks', '*', '', __METHOD__ );
foreach ( $blocksRes as $row ) {
$blocks[$row->ipb_user] = $row->ipb_address;
foreach ( $res as $row ) {
$blocks[] = (int)$row->bt_user;
}

return $blocks;
Expand Down Expand Up @@ -136,16 +167,16 @@ protected function makePreFilterConds( ReaderParams $params ) {
* @inheritDoc
*/
protected function skipPreFilter( Filter $filter ) {
return $filter->getField() === 'user_name';
return $filter->getField() === 'user_name' || $filter->getField() === 'enabled';
}

/**
* @param string $userId
* @param int $userId
*
* @return bool
*/
protected function isUserBlocked( string $userId ) {
return isset( $this->blocks[$userId] );
protected function isUserBlocked( int $userId ) {
return in_array( (int)$userId, $this->blocks );
}

/**
Expand All @@ -160,8 +191,9 @@ protected function appendRowToData( \stdClass $row ) {
'user_real_name' => $row->user_real_name,
'user_registration' => $row->user_registration,
'user_editcount' => (int)$row->user_editcount,
'user_email' => $row->user_email,
'groups' => isset( $this->groups[$row->user_id] ) ? $this->groups[$row->user_id] : [],
'enabled' => !$this->isUserBlocked( $row->user_id ),
'enabled' => !$this->isUserBlocked( (int)$row->user_id ),
// legacy fields
'display_name' => $row->user_real_name == null ? $row->user_name : $row->user_real_name,
];
Expand Down
7 changes: 7 additions & 0 deletions src/Data/UserQueryStore/SecondaryDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MWStake\MediaWiki\Component\CommonWebAPIs\Data\UserQueryStore;

use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Message\Message;
use MediaWiki\User\UserFactory;
use MWStake\MediaWiki\Component\DataStore\ISecondaryDataProvider;

Expand Down Expand Up @@ -42,6 +43,12 @@ public function extend( $dataSets ) {
$dataSet->set( UserRecord::PAGE_LINK, $userPageLink );
$dataSet->set( UserRecord::PAGE_URL, $userPage->getLocalURL() );
$dataSet->set( UserRecord::PAGE_PREFIXED_TEXT, $userPage->getPrefixedText() );
$groups = $dataSet->get( UserRecord::GROUPS );
$groups = array_map( function ( $group ) {
$msg = Message::newFromKey( 'group-' . $group );
return $msg->exists() ? $msg->text() : $group;
}, $groups );
$dataSet->set( UserRecord::GROUPS, $groups );
}

return $dataSets;
Expand Down
1 change: 1 addition & 0 deletions src/Data/UserQueryStore/UserRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class UserRecord extends Record {
public const USER_REAL_NAME = 'user_real_name';
public const USER_REGISTRATION = 'user_registration';
public const USER_EDITCOUNT = 'user_editcount';
public const USER_EMAIL = 'user_email';
public const GROUPS = 'groups';
public const ENABLED = 'enabled';
public const DISPLAY_NAME = 'display_name';
Expand Down
5 changes: 5 additions & 0 deletions src/Data/UserQueryStore/UserSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct() {
self::SORTABLE => true,
self::TYPE => FieldType::STRING
],
UserRecord::USER_EMAIL => [
self::FILTERABLE => true,
self::SORTABLE => true,
self::TYPE => FieldType::STRING
],
UserRecord::USER_EDITCOUNT => [
self::FILTERABLE => false,
self::SORTABLE => true,
Expand Down
4 changes: 2 additions & 2 deletions src/Rest/QueryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

abstract class QueryStore extends Handler {
/** @var HookContainer */
private $hookContainer;
protected $hookContainer;

/**
* @param HookContainer $hookContainer
Expand Down Expand Up @@ -100,7 +100,7 @@ protected function returnResult( ResultSet $result ): Response {
*
* @return false|string
*/
private function encodeJson( $data ) {
protected function encodeJson( $data ) {
return json_encode( $data, $this->getFormat() === 'jsonfm' ? JSON_PRETTY_PRINT : 0 );
}

Expand Down
33 changes: 33 additions & 0 deletions src/Rest/UserQueryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use GlobalVarConfig;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Rest\Response;
use MediaWiki\User\UserFactory;
use MWStake\MediaWiki\Component\CommonWebAPIs\Data\UserQueryStore\Store;
use MWStake\MediaWiki\Component\DataStore\IStore;
use MWStake\MediaWiki\Component\DataStore\ResultSet;
use Wikimedia\Rdbms\ILoadBalancer;

class UserQueryStore extends QueryStore {
Expand All @@ -29,11 +31,42 @@ public function __construct(
parent::__construct( $hookContainer );
$this->store = new Store( $lb, $userFactory, $linkRenderer, $titleFactory, $mwsgConfig );
}

/**
* @param ResultSet $result
*
* @return Response
*/
protected function returnResult( ResultSet $result ): Response {
$this->hookContainer->run( 'MWStakeCommonWebAPIsQueryStoreResult', [ $this, &$result ] );
$contentType = $contentType ?? 'application/json';
$response = new Response( $this->encodeJson( [
'buckets' => $this->getBuckets(),
'results' => $result->getRecords(),
'total' => $result->getTotal(),
] ) );
$response->setHeader( 'Content-Type', $contentType );
return $response;
}

/**
* @return IStore
*/
protected function getStore(): IStore {
return $this->store;
}

/**
* @return array
*/
private function getBuckets(): array {
$groups = $this->getStore()
->getReader()
->makePrimaryDataProvider( $this->getReaderParams() )
->getGroupBuckets();

return [
'groups' => $groups
];
}
}

0 comments on commit fffb298

Please sign in to comment.