Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
49.12% covered (danger)
49.12%
28 / 57
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
NameTableStoreFactory
49.12% covered (danger)
49.12%
28 / 57
66.67% covered (warning)
66.67%
4 / 6
26.94
0.00% covered (danger)
0.00%
0 / 1
 getTableInfo
6.67% covered (danger)
6.67%
2 / 30
0.00% covered (danger)
0.00%
0 / 1
5.25
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
5
 getChangeTagDef
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContentModels
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSlotRoles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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 */
20
21namespace MediaWiki\Storage;
22
23use Psr\Log\LoggerInterface;
24use WANObjectCache;
25use Wikimedia\Rdbms\ILBFactory;
26
27class NameTableStoreFactory {
28    private static $info;
29    private $stores = [];
30
31    /** @var ILBFactory */
32    private $lbFactory;
33
34    /** @var WANObjectCache */
35    private $cache;
36
37    /** @var LoggerInterface */
38    private $logger;
39
40    private static function getTableInfo() {
41        if ( self::$info ) {
42            return self::$info;
43        }
44        self::$info = [
45            'change_tag_def' => [
46                'idField' => 'ctd_id',
47                'nameField' => 'ctd_name',
48                'normalizationCallback' => null,
49                'insertCallback' => static function ( $insertFields ) {
50                    $insertFields['ctd_user_defined'] = 0;
51                    $insertFields['ctd_count'] = 0;
52                    return $insertFields;
53                }
54            ],
55
56            'content_models' => [
57                'idField' => 'model_id',
58                'nameField' => 'model_name',
59                /**
60                 * No strtolower normalization is added to the service as there are examples of
61                 * extensions that do not stick to this assumption.
62                 * - extensions/examples/DataPages define( 'CONTENT_MODEL_XML_DATA','XML_DATA' );
63                 * - extensions/Scribunto define( 'CONTENT_MODEL_SCRIBUNTO', 'Scribunto' );
64                 */
65            ],
66
67            'slot_roles' => [
68                'idField' => 'role_id',
69                'nameField' => 'role_name',
70                'normalizationCallback' => 'strtolower',
71            ],
72        ];
73        return self::$info;
74    }
75
76    public function __construct(
77        ILBFactory $lbFactory,
78        WANObjectCache $cache,
79        LoggerInterface $logger
80    ) {
81        $this->lbFactory = $lbFactory;
82        $this->cache = $cache;
83        $this->logger = $logger;
84    }
85
86    /**
87     * Get a NameTableStore for a specific table
88     *
89     * @param string $tableName
90     * @param string|false $wiki The target wiki ID, or false for the current wiki
91     * @return NameTableStore
92     */
93    public function get( $tableName, $wiki = false ): NameTableStore {
94        $infos = self::getTableInfo();
95        if ( !isset( $infos[$tableName] ) ) {
96            throw new \InvalidArgumentException( "Invalid table name \$tableName" );
97        }
98        if ( $wiki !== false && $wiki === $this->lbFactory->getLocalDomainID() ) {
99            $wiki = false;
100        }
101
102        if ( isset( $this->stores[$tableName][$wiki] ) ) {
103            return $this->stores[$tableName][$wiki];
104        }
105
106        $info = $infos[$tableName];
107        $store = new NameTableStore(
108            $this->lbFactory->getMainLB( $wiki ),
109            $this->cache,
110            $this->logger,
111            $tableName,
112            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
113            $info['idField'],
114            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
115            $info['nameField'],
116            $info['normalizationCallback'] ?? null,
117            $wiki,
118            $info['insertCallback'] ?? null
119        );
120        $this->stores[$tableName][$wiki] = $store;
121        return $store;
122    }
123
124    /**
125     * Get a NameTableStore for the change_tag_def table
126     *
127     * @param string|bool $wiki
128     * @return NameTableStore
129     */
130    public function getChangeTagDef( $wiki = false ): NameTableStore {
131        return $this->get( 'change_tag_def', $wiki );
132    }
133
134    /**
135     * Get a NameTableStore for the content_models table
136     *
137     * @param string|bool $wiki
138     * @return NameTableStore
139     */
140    public function getContentModels( $wiki = false ): NameTableStore {
141        return $this->get( 'content_models', $wiki );
142    }
143
144    /**
145     * Get a NameTableStore for the slot_roles table
146     *
147     * @param string|bool $wiki
148     * @return NameTableStore
149     */
150    public function getSlotRoles( $wiki = false ): NameTableStore {
151        return $this->get( 'slot_roles', $wiki );
152    }
153}