1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains only the LabsHelper class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace AppBundle\Helper; |
7
|
|
|
|
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The Labs helper provides information relating to the WMF Labs installation of XTools. |
13
|
|
|
*/ |
14
|
|
|
class LabsHelper |
15
|
|
|
{ |
16
|
|
|
/** @var string The current database name. */ |
17
|
|
|
protected $dbName; |
18
|
|
|
|
19
|
|
|
/** @var Connection The database connection. */ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** @var ContainerInterface The DI container. */ |
23
|
|
|
protected $container; |
24
|
|
|
|
25
|
|
|
/** @var string The project URL. */ |
26
|
|
|
protected $url; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* LabsHelper constructor. |
30
|
|
|
* @param ContainerInterface $container |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ContainerInterface $container) |
33
|
|
|
{ |
34
|
|
|
$this->container = $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Is XTools connecting to WMF Labs? |
39
|
|
|
* |
40
|
|
|
* @return boolean |
41
|
|
|
*/ |
42
|
|
|
public function isLabs() |
43
|
|
|
{ |
44
|
|
|
return (bool)$this->container->getParameter('app.is_labs'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* All mapping tables to environment-specific names, as specified in config/table_map.yml |
49
|
|
|
* Used for example to convert revision -> revision_replica |
50
|
|
|
* https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs |
51
|
|
|
* |
52
|
|
|
* @param string $table Table name |
53
|
|
|
* @param string $dbName Database name |
54
|
|
|
* @param string|null $table_extension Optional table extension, which will only get used if we're on labs. |
55
|
|
|
* |
56
|
|
|
* @return string Converted table name |
57
|
|
|
*/ |
58
|
|
|
public function getTable($table, $dbName = null, $table_extension = null) |
59
|
|
|
{ |
60
|
|
|
// This is a workaround for a one-to-many mapping |
61
|
|
|
// as required by Labs. We combine $table with |
62
|
|
|
// $table_extension in order to generate the new table name |
63
|
|
|
if ($this->isLabs() && $table_extension !== null) { |
64
|
|
|
$table = $table . "_" . $table_extension; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Use the table specified in the table mapping configuration, if present. |
68
|
|
|
$mapped = false; |
|
|
|
|
69
|
|
View Code Duplication |
if ($this->container->hasParameter("app.table.$table")) { |
|
|
|
|
70
|
|
|
$mapped = true; |
|
|
|
|
71
|
|
|
$table = $this->container->getParameter("app.table.$table"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Figure out database name. |
75
|
|
|
// Use class variable for the database name if not set via function parameter. |
76
|
|
|
$dbNameActual = $dbName ? $dbName : $this->dbName; |
77
|
|
|
if ($this->isLabs() && substr($dbNameActual, -2) != '_p') { |
78
|
|
|
// Append '_p' if this is labs. |
79
|
|
|
$dbNameActual .= '_p'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $dbNameActual ? "$dbNameActual.$table" : $table; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// TODO: figure out how to use Doctrine to query host 'tools-db' |
86
|
|
|
} |
87
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.