1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
9
|
|
|
|
10
|
|
|
class LabsHelper |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $dbName; |
14
|
|
|
|
15
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** @var ContainerInterface */ |
19
|
|
|
protected $container; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $url; |
23
|
|
|
|
24
|
|
|
public function __construct(ContainerInterface $container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function checkEnabled($tool) |
30
|
|
|
{ |
31
|
|
|
if (!$this->container->getParameter("enable.$tool")) { |
32
|
|
|
throw new NotFoundHttpException('This tool is disabled'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Is xTools connecting to MMF Labs? |
38
|
|
|
* @return boolean |
39
|
|
|
*/ |
40
|
|
|
public function isLabs() |
41
|
|
|
{ |
42
|
|
|
return (bool)$this->container->getParameter('app.is_labs'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set up LabsHelper::$client and return the database name, wiki name, and URL of a given |
47
|
|
|
* project. |
48
|
|
|
* @todo: Handle failure better |
49
|
|
|
* @return string[] With keys 'dbName', 'wikiName', and 'url'. |
50
|
|
|
*/ |
51
|
|
|
public function databasePrepare($project = 'wiki') |
52
|
|
|
{ |
53
|
|
|
if ($this->container->getParameter('app.single_wiki')) { |
54
|
|
|
$dbName = $this->container->getParameter('database_replica_name'); |
55
|
|
|
$wikiName = 'wiki'; |
56
|
|
|
$url = $this->container->getParameter('wiki_url'); |
57
|
|
|
} else { |
58
|
|
|
// Grab the connection to the meta database |
59
|
|
|
$this->client = $this->container->get('doctrine')->getManager('meta')->getConnection(); |
60
|
|
|
|
61
|
|
|
// Create the query we're going to run against the meta database |
62
|
|
|
$wikiQuery = $this->client->createQueryBuilder(); |
63
|
|
|
$wikiQuery |
64
|
|
|
->select([ 'dbName', 'name', 'url', 'lang' ]) |
65
|
|
|
->from('wiki') |
66
|
|
|
->where($wikiQuery->expr()->eq('dbname', ':project')) |
67
|
|
|
// The meta database will have the project's URL stored as https://en.wikipedia.org |
68
|
|
|
// so we need to query for it accordingly, trying different variations the user |
69
|
|
|
// might have inputted. |
70
|
|
|
->orwhere($wikiQuery->expr()->like('url', ':projectUrl')) |
71
|
|
|
->orwhere($wikiQuery->expr()->like('url', ':projectUrl2')) |
72
|
|
|
->setParameter('project', $project) |
73
|
|
|
->setParameter('projectUrl', "https://$project") |
74
|
|
|
->setParameter('projectUrl2', "https://$project.org"); |
75
|
|
|
$wikiStatement = $wikiQuery->execute(); |
76
|
|
|
|
77
|
|
|
// Fetch the wiki data |
78
|
|
|
$wikis = $wikiStatement->fetchAll(); |
79
|
|
|
|
80
|
|
|
// Throw an exception if we can't find the wiki |
81
|
|
|
if (count($wikis) < 1) { |
82
|
|
|
// TODO: Fix so that we're rendering a flash rather than dying... |
83
|
|
|
throw new Exception("Unable to find project '$project'"); |
84
|
|
|
// $this->container->get('controller')->addFlash('notice', ["nowiki", $project]); |
|
|
|
|
85
|
|
|
// return $this->container->redirectToRoute($route); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Grab the data we need out of it, using the first result |
89
|
|
|
// (in the rare event there are more than one). |
90
|
|
|
$dbName = $wikis[0]['dbName']; |
91
|
|
|
$wikiName = $wikis[0]['name']; |
92
|
|
|
$url = $wikis[0]['url']; |
93
|
|
|
$lang = $wikis[0]['lang']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->container->getParameter('app.is_labs') && substr($dbName, -2) != '_p') { |
97
|
|
|
$dbName .= '_p'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->dbName = $dbName; |
101
|
|
|
$this->url = $url; |
102
|
|
|
|
103
|
|
|
return [ 'dbName' => $dbName, 'wikiName' => $wikiName, 'url' => $url, 'lang' => $lang ]; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get a list of all projects. |
108
|
|
|
*/ |
109
|
|
|
public function allProjects() |
110
|
|
|
{ |
111
|
|
|
$wikiQuery = $this->client->createQueryBuilder(); |
112
|
|
|
$wikiQuery->select([ 'dbName', 'name', 'url' ])->from('wiki'); |
113
|
|
|
$stmt = $wikiQuery->execute(); |
114
|
|
|
$out = $stmt->fetchAll(); |
115
|
|
|
return $out; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* All mapping tables to environment-specific names, as specified in config/table_map.yml |
120
|
|
|
* Used for example to convert revision -> revision_replica |
121
|
|
|
* https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
122
|
|
|
* |
123
|
|
|
* @param string $table Table name |
124
|
|
|
* @param string $dbName Database name |
125
|
|
|
* |
126
|
|
|
* @return string Converted table name |
127
|
|
|
*/ |
128
|
|
|
public function getTable($table, $dbName = null) |
129
|
|
|
{ |
130
|
|
|
// Use the table specified in the table mapping configuration, if present. |
131
|
|
|
$mapped = false; |
132
|
|
|
if ($this->container->hasParameter("app.table.$table")) { |
133
|
|
|
$mapped = true; |
134
|
|
|
$table = $this->container->getParameter("app.table.$table"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// For 'revision' and 'logging' tables (actually views) on Labs, use the indexed versions |
138
|
|
|
// (that have some rows hidden, e.g. for revdeleted users). |
139
|
|
|
$isLoggingOrRevision = in_array($table, ['revision', 'logging', 'archive']); |
140
|
|
|
if (!$mapped && $isLoggingOrRevision && $this->isLabs()) { |
141
|
|
|
$table = $table."_userindex"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Figure out database name. |
145
|
|
|
// Use class variable for the database name if not set via function parameter. |
146
|
|
|
$dbNameActual = $dbName ? $dbName : $this->dbName; |
147
|
|
|
if ($this->isLabs() && substr($dbNameActual, -2) != '_p') { |
148
|
|
|
// Append '_p' if this is labs. |
149
|
|
|
$dbNameActual .= '_p'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $dbNameActual ? "$dbNameActual.$table" : $table; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// TODO: figure out how to use Doctrine to query host 'tools-db' |
156
|
|
|
} |
157
|
|
|
|
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.