Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
UserEntitySerializer | |
100.00% |
27 / 27 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @author Andrew Otto <otto@wikimedia.org> |
20 | */ |
21 | namespace MediaWiki\Extension\EventBus\Serializers\MediaWiki; |
22 | |
23 | use MediaWiki\Extension\EventBus\Serializers\EventSerializer; |
24 | use MediaWiki\User\User; |
25 | use MediaWiki\User\UserFactory; |
26 | use MediaWiki\User\UserGroupManager; |
27 | use MediaWiki\User\UserIdentity; |
28 | use Wikimedia\Assert\Assert; |
29 | |
30 | /** |
31 | * Converts a User to an array matching the fragment/mediawiki/state/entity/user schema |
32 | */ |
33 | class UserEntitySerializer { |
34 | /** |
35 | * @var UserGroupManager |
36 | */ |
37 | private UserGroupManager $userGroupManager; |
38 | /** |
39 | * @var UserFactory |
40 | */ |
41 | private UserFactory $userFactory; |
42 | |
43 | /** |
44 | * @param UserFactory $userFactory |
45 | * @param UserGroupManager $userGroupManager |
46 | */ |
47 | public function __construct( |
48 | // NOTE: It would be better not to need a UserFactory |
49 | // and have toArray only take instances of User. |
50 | // But we need to call ::toArray( $userIdentity ) from within RevisionEntitySerializer, |
51 | // which gets the editor of the revision as a UserIdentity, |
52 | // and the correct way to convert a UserIdentity to a User is with a UserFactory. |
53 | // So either we need a UserFactory here, or in RevisionEntitySerializer. It is more useful here. |
54 | UserFactory $userFactory, |
55 | UserGroupManager $userGroupManager |
56 | ) { |
57 | $this->userFactory = $userFactory; |
58 | $this->userGroupManager = $userGroupManager; |
59 | } |
60 | |
61 | /** |
62 | * Given a User or UserIdentity $user, returns an array suitable for |
63 | * use as a mediawiki/state/entity/user JSON object in other MediaWiki |
64 | * state/entity schemas. |
65 | * @param User|UserIdentity $user |
66 | * @return array |
67 | */ |
68 | public function toArray( $user ): array { |
69 | Assert::parameterType( |
70 | [ |
71 | User::class, |
72 | UserIdentity::class, |
73 | ], |
74 | $user, |
75 | '$user' |
76 | ); |
77 | |
78 | // If given a UserIdentity (that is not already a User), convert to a User. |
79 | if ( !( $user instanceof User ) && $user instanceof UserIdentity ) { |
80 | $user = $this->userFactory->newFromUserIdentity( $user ); |
81 | } |
82 | |
83 | $userAttrs = [ |
84 | 'user_text' => $user->getName(), |
85 | 'groups' => $this->userGroupManager->getUserEffectiveGroups( $user ), |
86 | 'is_bot' => $user->isRegistered() && $user->isBot(), |
87 | 'is_system' => $user->isSystemUser(), |
88 | 'is_temp' => $user->isTemp() |
89 | ]; |
90 | |
91 | if ( $user->getId() ) { |
92 | $userAttrs['user_id'] = $user->getId(); |
93 | } |
94 | if ( $user->getRegistration() ) { |
95 | $userAttrs['registration_dt'] = |
96 | EventSerializer::timestampToDt( $user->getRegistration() ); |
97 | } |
98 | if ( $user->isRegistered() ) { |
99 | $userAttrs['edit_count'] = $user->getEditCount(); |
100 | } |
101 | |
102 | return $userAttrs; |
103 | } |
104 | |
105 | } |