Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateMetadata.php
Go to the documentation of this file.
1<?php
15 private static $cache = [];
17 private static $priorityCache;
18
23 public static function preloadGroups( array $groups, string $caller ) {
24 $missing = array_keys( array_diff_key( array_flip( $groups ), self::$cache ) );
25 if ( !$missing ) {
26 return;
27 }
28
29 $fname = __METHOD__ . " (for $caller)";
30
31 self::$cache += array_fill_keys( $missing, null ); // cache negatives
32
33 $dbr = TranslateUtils::getSafeReadDB();
34 $conds = count( $missing ) <= 500 ? [ 'tmd_group' => array_map( 'strval', $missing ) ] : [];
35 $res = $dbr->select(
36 'translate_metadata',
37 [ 'tmd_group', 'tmd_key', 'tmd_value' ],
38 $conds,
39 $fname
40 );
41 foreach ( $res as $row ) {
42 self::$cache[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
43 }
44 }
45
52 public static function get( $group, $key ) {
53 self::preloadGroups( [ $group ], __METHOD__ );
54
55 return self::$cache[$group][$key] ?? false;
56 }
57
66 public static function getWithDefaultValue(
67 string $group, string $key, string $defaultValue
68 ): string {
69 $value = self::get( $group, $key );
70 return $value === false ? $defaultValue : $value;
71 }
72
80 public static function set( $group, $key, $value ) {
81 $dbw = wfGetDB( DB_PRIMARY );
82 $data = [ 'tmd_group' => $group, 'tmd_key' => $key, 'tmd_value' => $value ];
83 if ( $value === false ) {
84 unset( $data['tmd_value'] );
85 $dbw->delete( 'translate_metadata', $data, __METHOD__ );
86 unset( self::$cache[$group][$key] );
87 } else {
88 $dbw->replace(
89 'translate_metadata',
90 [ [ 'tmd_group', 'tmd_key' ] ],
91 $data,
92 __METHOD__
93 );
94 self::$cache[$group][$key] = $value;
95 }
96
97 self::$priorityCache = null;
98 }
99
106 public static function getSubgroups( string $groupId ): ?array {
107 $groups = self::get( $groupId, 'subgroups' );
108 if ( is_string( $groups ) ) {
109 if ( strpos( $groups, '|' ) !== false ) {
110 $groups = explode( '|', $groups );
111 } else {
112 $groups = array_map( 'trim', explode( ',', $groups ) );
113 }
114
115 foreach ( $groups as $index => $id ) {
116 if ( trim( $id ) === '' ) {
117 unset( $groups[$index] );
118 }
119 }
120 } else {
121 $groups = null;
122 }
123
124 return $groups;
125 }
126
133 public static function setSubgroups( $groupId, $subgroupIds ) {
134 $subgroups = implode( '|', $subgroupIds );
135 self::set( $groupId, 'subgroups', $subgroups );
136 }
137
143 public static function deleteGroup( $groupId ) {
144 $dbw = wfGetDB( DB_PRIMARY );
145 $conds = [ 'tmd_group' => $groupId ];
146 $dbw->delete( 'translate_metadata', $conds, __METHOD__ );
147 self::$cache[$groupId] = null;
148 unset( self::$priorityCache[ $groupId ] );
149 }
150
151 public static function isExcluded( string $groupId, string $code ): bool {
152 if ( self::$priorityCache === null ) {
153 $db = TranslateUtils::getSafeReadDB();
154 $res = $db->select(
155 [
156 'a' => 'translate_metadata',
157 'b' => 'translate_metadata'
158 ],
159 [
160 'group' => 'b.tmd_group',
161 'langs' => 'b.tmd_value',
162 ],
163 [],
164 __METHOD__,
165 [],
166 [
167 'b' => [
168 'INNER JOIN',
169 [
170 'a.tmd_group = b.tmd_group',
171 'a.tmd_key' => 'priorityforce',
172 'a.tmd_value' => 'on',
173 'b.tmd_key' => 'prioritylangs',
174 ]
175 ]
176 ]
177 );
178
179 self::$priorityCache = [];
180 foreach ( $res as $row ) {
181 self::$priorityCache[$row->group] =
182 array_flip( explode( ',', $row->langs ) );
183 }
184 }
185
186 $isDiscouraged = MessageGroups::getPriority( $groupId ) === 'discouraged';
187 $hasLimitedLanguages = isset( self::$priorityCache[$groupId] );
188 $isLanguageIncluded = isset( self::$priorityCache[$groupId][$code] );
189
190 return $isDiscouraged || ( $hasLimitedLanguages && !$isLanguageIncluded );
191 }
192
199 public static function loadBasicMetadataForTranslatablePages( array $groupIds, array $keys ): array {
200 $db = TranslateUtils::getSafeReadDB();
201 $res = $db->select(
202 'translate_metadata',
203 [ 'tmd_group', 'tmd_key', 'tmd_value' ],
204 [
205 'tmd_group' => $groupIds,
206 'tmd_key' => $keys,
207 ],
208 __METHOD__
209 );
210
211 $ret = [];
212 foreach ( $res as $row ) {
213 $ret[$row->tmd_group][$row->tmd_key] = $row->tmd_value;
214 }
215
216 return $ret;
217 }
218
225 public static function moveMetadata(
226 string $oldGroupId,
227 string $newGroupId,
228 array $metadataKeysToMove
229 ): void {
230 self::preloadGroups( [ $oldGroupId, $newGroupId ], __METHOD__ );
231 foreach ( $metadataKeysToMove as $type ) {
232 $value = self::get( $oldGroupId, $type );
233 if ( $value !== false ) {
234 self::set( $oldGroupId, $type, false );
235 self::set( $newGroupId, $type, $value );
236 }
237 }
238 }
239
245 public static function clearMetadata( string $groupId, array $metadataKeys ): void {
246 // remove the entries from metadata table.
247 foreach ( $metadataKeys as $type ) {
248 self::set( $groupId, $type, false );
249 }
250 }
251}
static getPriority( $group)
We want to de-emphasize time sensitive groups like news for 2009.
static getWithDefaultValue(string $group, string $key, string $defaultValue)
Get a metadata value for the given group and key.
static getSubgroups(string $groupId)
Wrapper for getting subgroups.
static moveMetadata(string $oldGroupId, string $newGroupId, array $metadataKeysToMove)
static get( $group, $key)
Get a metadata value for the given group and key.
static deleteGroup( $groupId)
Wrapper for deleting one wiki aggregate group at once.
static preloadGroups(array $groups, string $caller)
static loadBasicMetadataForTranslatablePages(array $groupIds, array $keys)
Do a query optimized for page list in Special:PageTranslation.
static clearMetadata(string $groupId, array $metadataKeys)
static setSubgroups( $groupId, $subgroupIds)
Wrapper for setting subgroups.
Essentially random collection of helper functions, similar to GlobalFunctions.php.