Completed
Push — master ( b47a89...8fd593 )
by Sam
02:40
created

UserRepository::getXtoolsUserInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains only the UserRepository class.
4
 */
5
6
namespace Xtools;
7
8
use DateInterval;
9
use Mediawiki\Api\SimpleRequest;
10
use Symfony\Component\DependencyInjection\Container;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
13
/**
14
 * This class provides data for the User class.
15
 */
16
class UserRepository extends Repository
17
{
18
19
    /**
20
     * Convenience method to get a new User object.
21
     * @param string $username The username.
22
     * @param Container $container The DI container.
23
     * @return User
24
     */
25
    public static function getUser($username, Container $container)
26
    {
27
        $user = new User($username);
28
        $userRepo = new UserRepository();
29
        $userRepo->setContainer($container);
30
        $user->setRepository($userRepo);
31
        return $user;
32
    }
33
34
    /**
35
     * Get the user's ID.
36
     * @param string $databaseName The database to query.
37
     * @param string $username The username to find.
38
     * @return int
39
     */
40
    public function getId($databaseName, $username)
41
    {
42
        $cacheKey = 'user_id.'.$databaseName.'.'.$username;
43
        if ($this->cache->hasItem($cacheKey)) {
44
            return $this->cache->getItem($cacheKey)->get();
45
        }
46
47
        $userTable = $this->getTableName($databaseName, 'user');
48
        $sql = "SELECT user_id FROM $userTable WHERE user_name = :username LIMIT 1";
49
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
50
        $resultQuery->bindParam("username", $username);
51
        $resultQuery->execute();
52
        $userId = (int)$resultQuery->fetchColumn();
53
54
        // Cache for 10 minutes.
55
        $cacheItem = $this->cache
56
            ->getItem($cacheKey)
57
            ->set($userId)
58
            ->expiresAfter(new DateInterval('PT10M'));
59
        $this->cache->save($cacheItem);
60
        return $userId;
61
    }
62
63
    /**
64
     * Get group names of the given user.
65
     * @param Project $project The project.
66
     * @param string $username The username.
67
     * @return string[]
68
     */
69
    public function getGroups(Project $project, $username)
70
    {
71
        $cacheKey = 'usergroups.'.$project->getDatabaseName().'.'.$username;
72
        if ($this->cache->hasItem($cacheKey)) {
73
            return $this->cache->getItem($cacheKey)->get();
74
        }
75
76
        $this->stopwatch->start($cacheKey, 'XTools');
77
        $api = $this->getMediawikiApi($project);
78
        $params = [ "list"=>"users", "ususers"=>$username, "usprop"=>"groups" ];
79
        $query = new SimpleRequest('query', $params);
80
        $result = [];
81
        $res = $api->getRequest($query);
82
        if (isset($res["batchcomplete"]) && isset($res["query"]["users"][0]["groups"])) {
83
            $result = $res["query"]["users"][0]["groups"];
84
        }
85
86
        // Cache for 10 minutes, and return.
1 ignored issue
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87
        $cacheItem = $this->cache->getItem($cacheKey)
88
            ->set($result)
89
            ->expiresAfter(new DateInterval('PT10M'));
90
        $this->cache->save($cacheItem);
91
        $this->stopwatch->stop($cacheKey);
92
93
        return $result;
94
    }
95
96
    /**
97
     * Get a user's global group membership (starting at XTools' default project if none is
98
     * provided). This requires the CentralAuth extension to be installed.
99
     * @link https://www.mediawiki.org/wiki/Extension:CentralAuth
100
     * @param string $username The username.
101
     * @param Project $project The project to query.
102
     * @return string[]
103
     */
104
    public function getGlobalGroups($username, Project $project = null)
105
    {
106
        // Get the default project if not provided.
107
        if (!$project instanceof Project) {
108
            $project = ProjectRepository::getDefaultProject($this->container);
109
        }
110
111
        // Create the API query.
112
        $api = $this->getMediawikiApi($project);
113
        $params = [ "meta"=>"globaluserinfo", "guiuser"=>$username, "guiprop"=>"groups" ];
114
        $query = new SimpleRequest('query', $params);
115
116
        // Get the result.
117
        $res = $api->getRequest($query);
118
        $result = [];
119
        if (isset($res["batchcomplete"]) && isset($res["query"]["globaluserinfo"]["groups"])) {
120
            $result = $res["query"]["globaluserinfo"]["groups"];
121
        }
122
        return $result;
123
    }
124
125
    /**
126
     * Get information about the currently-logged in user.
127
     * @return array
128
     */
129
    public function getXtoolsUserInfo()
130
    {
131
        /** @var Session $session */
132
        $session = $this->container->get('session');
133
        return $session->get('logged_in_user');
134
    }
135
}
136