1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xtools; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\SimpleRequest; |
6
|
|
|
use Symfony\Component\DependencyInjection\Container; |
7
|
|
|
|
8
|
|
|
class UserRepository extends Repository |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** @var int[] */ |
12
|
|
|
protected $userIds; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Convenience method to get a new User object. |
16
|
|
|
* @param string $username |
17
|
|
|
* @return User |
18
|
|
|
*/ |
19
|
|
|
public static function getUser($username, Container $container) |
20
|
|
|
{ |
21
|
|
|
$user = new User($username); |
22
|
|
|
$userRepo = new UserRepository(); |
23
|
|
|
$userRepo->setContainer($container); |
24
|
|
|
$user->setRepository($userRepo); |
25
|
|
|
return $user; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the user's ID. |
30
|
|
|
* @param string $databaseName The database to query. |
31
|
|
|
* @param string $username The username to find. |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function getId($databaseName, $username) |
35
|
|
|
{ |
36
|
|
|
if (isset($this->userIds[$databaseName][$username])) { |
37
|
|
|
return $this->userIds[$databaseName][$username]; |
38
|
|
|
} |
39
|
|
|
$userTable = $this->getTableName($databaseName, 'user'); |
40
|
|
|
$sql = "SELECT user_id FROM $userTable WHERE user_name = :username LIMIT 1"; |
41
|
|
|
$resultQuery = $this->getProjectsConnection()->prepare($sql); |
42
|
|
|
$resultQuery->bindParam("username", $username); |
43
|
|
|
$resultQuery->execute(); |
44
|
|
|
$userId = (int)$resultQuery->fetchColumn(); |
45
|
|
|
$this->userIds[$databaseName][$username] = $userId; |
46
|
|
|
return $userId; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Project $project |
51
|
|
|
* @param string $username |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getGroups(Project $project, $username) |
55
|
|
|
{ |
56
|
|
|
$cacheKey = 'usergroups.'.$project->getDatabaseName().'.'.$username; |
57
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
58
|
|
|
return $this->cache->getItem($cacheKey)->get(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->stopwatch->start($cacheKey, 'XTools'); |
62
|
|
|
$api = $this->getMediawikiApi($project); |
63
|
|
|
$params = [ "list"=>"users", "ususers"=>$username, "usprop"=>"groups" ]; |
64
|
|
|
$query = new SimpleRequest('query', $params); |
65
|
|
|
$result = []; |
66
|
|
|
$res = $api->getRequest($query); |
67
|
|
|
if (isset($res["batchcomplete"]) && isset($res["query"]["users"][0]["groups"])) { |
68
|
|
|
$result = $res["query"]["users"][0]["groups"]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Cache for 10 minutes, and return. |
|
|
|
|
72
|
|
|
$cacheItem = $this->cache->getItem($cacheKey) |
73
|
|
|
->set($result) |
74
|
|
|
->expiresAfter(new \DateInterval('PT10M')); |
75
|
|
|
$this->cache->save($cacheItem); |
76
|
|
|
$this->stopwatch->stop($cacheKey); |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get a user's global group membership (starting at XTools' default project if none is |
83
|
|
|
* provided). This requires the CentralAuth extension to be installed. |
84
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:CentralAuth |
85
|
|
|
* @param string $username The username. |
86
|
|
|
* @param Project $project The project to query. |
87
|
|
|
* @return string[] |
88
|
|
|
*/ |
89
|
|
|
public function getGlobalGroups($username, Project $project = null) |
90
|
|
|
{ |
91
|
|
|
// Get the default project if not provided. |
92
|
|
|
if (!$project instanceof Project) { |
93
|
|
|
$defaultProject = $this->container->getParameter('default_project'); |
94
|
|
|
$project = ProjectRepository::getProject($defaultProject, $this->container); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Create the API query. |
98
|
|
|
$api = $this->getMediawikiApi($project); |
99
|
|
|
$params = [ "meta"=>"globaluserinfo", "guiuser"=>$username, "guiprop"=>"groups" ]; |
100
|
|
|
$query = new SimpleRequest('query', $params); |
101
|
|
|
|
102
|
|
|
// Get the result. |
103
|
|
|
$res = $api->getRequest($query); |
104
|
|
|
$result = []; |
105
|
|
|
if (isset($res["batchcomplete"]) && isset($res["query"]["globaluserinfo"]["groups"])) { |
106
|
|
|
$result = $res["query"]["globaluserinfo"]["groups"]; |
107
|
|
|
} |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.