PluginProbe ʕ •ᴥ•ʔ
Aruba HiSpeed Cache / 3.0.15
Aruba HiSpeed Cache v3.0.15
3.0.15 3.0.14 3.0.13 1.2.4 1.2.5 1.2.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.18 2.0.19 2.0.20 2.0.21 2.0.22 2.0.23 2.0.24 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3
aruba-hispeed-cache / src / AHSC_Dboptimization.php
aruba-hispeed-cache / src Last commit date
APC 3 days ago Events 3 days ago Purger 3 days ago assets 3 days ago AHSC_Apc.php 3 days ago AHSC_Check.php 3 days ago AHSC_Config.php 3 days ago AHSC_Dboptimization.php 3 days ago AHSC_Functions.php 3 days ago AHSC_HtmlOptimizer.php 5 months ago AHSC_Lazyload.php 3 days ago AHSC_Preconnect.php 5 months ago AHSC_Static.php 4 months ago AHSC_Version.php 5 months ago AHSC_Warmer.php 3 days ago AHSC_XmlRPC.php 5 months ago index.php 5 months ago
AHSC_Dboptimization.php
353 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 $ahsc_tables=array();
6 $ahsc_tables['wp_postmeta']=array(
7 'meta_id_optimized'=>array(
8 'type'=>'UNIQUE KEY',
9 'param'=>array('meta_id')
10 ),
11 'post_id_optimized'=>array(
12 'type'=>'KEY',
13 'param'=>array('post_id', 'meta_key', 'meta_id')
14 ),
15 'meta_key_optimized'=>array(
16 'type'=>'KEY',
17 'param'=>array('post_id', 'meta_key')
18 )
19 );
20
21 $ahsc_tables['wp_usermeta']=array(
22 'umeta_id_optimized'=>array(
23 'type'=>'UNIQUE KEY',
24 'param'=>array('umeta_id')
25 ),
26 'user_id_optimized'=>array(
27 'type'=>'KEY',
28 'param'=>array('user_id', 'meta_key','umeta_id')
29 ),
30 'meta_key_optimized'=>array(
31 'type'=>'KEY',
32 'param'=>array( 'meta_key','user_id')
33 )
34 );
35
36 $ahsc_tables['wp_termmeta']=array(
37 'meta_id_optimized'=>array(
38 'type'=>'UNIQUE KEY',
39 'param'=>array('meta_id')
40 ),
41 'term_id_optimized'=>array(
42 'type'=>'KEY',
43 'param'=>array('term_id', 'meta_key', 'meta_id')
44 ),
45 'meta_key_optimized'=>array(
46 'type'=>'KEY',
47 'param'=>array('meta_key','term_id')
48 ),
49
50 );
51
52 $ahsc_tables['wp_options']=array(
53 'option_id_optimized'=>array(
54 'type'=>'UNIQUE KEY',
55 'param'=>array('option_id')
56 ),
57 'autolod_optimized'=>array(
58 'type'=>'KEY',
59 'param'=>array('autoload','option_id')
60 )
61 );
62
63 $ahsc_tables['wp_posts']=array(
64 'type_status_date_optimized'=>array(
65 'type'=>'KEY',
66 'param'=>array('post_type','post_status','post_date','post_author','ID')
67 ),
68 'post_author_optimized'=>array(
69 'type'=>'KEY',
70 'param'=>array('post_author','post_type','post_status','post_date','ID')
71 )
72 );
73 $ahsc_tables['wp_comments']=array(
74 'comment_post_parent_approved_optimized'=>array(
75 'type'=>'KEY',
76 'param'=>array('comment_post_ID','comment_parent','comment_approved','comment_ID')
77 )
78 );
79
80 /*CONTROLLO PER ESISTENZA UNIQUE KEY
81
82 SELECT EXISTS (SELECT constraint_name
83 FROM INFORMATION_SCHEMA.table_constraints
84 WHERE table_name = 'my_table' AND constraint_type='UNIQUE');
85 */
86 /*CONTROLLO PER ESISTENZA KEU
87 SELECT DISTINCT
88 INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS
89 WHERE INDEX_NAME = 'KEY_NAME'
90 and TABLE_NAME='TABLE_NAME'
91 */
92
93 function AHSC_DBOPT_Check(){
94 global $ahsc_tables,$wpdb;
95 $query_result=array();
96 $check=true;
97 foreach($ahsc_tables as $table_name=>$index_settings){
98 foreach($index_settings as $index_name=>$index_param){
99 $pfx=$wpdb->prefix.substr($table_name,'3',strlen($table_name));
100 $query_result[$pfx][$index_name]=AHSC_check_key_exists($index_name,$table_name);
101 }
102 }
103
104 foreach($query_result as $table=>$index){
105 foreach($index as $index_name=>$index_exist){
106 if($index_exist===0){
107 $check=false;
108 break;
109 }else{
110 continue;
111 }
112 }
113 }
114
115 /*echo "<pre> <p>===================================SQLCONTROLLO=====================================================</p>".
116 "<p>". var_export($query_result,true)."</p>".
117 "<p>CHECK RESULT: ".var_export($check,true)."</p>".
118 "<p>================================================================================================</p></pre>";*/
119 return $check;
120 }
121
122 //AHSC_DBOPT_Check();
123 /*
124 * The three rules switched off across this file cannot be satisfied by an index management
125 * feature: WordPress exposes no API for DDL or for reading index metadata, altering the
126 * schema is the whole point, and a DDL statement has nothing to cache. They are disabled by
127 * name, with the reason recorded, rather than through a blanket suppression — every other
128 * rule still applies to this file.
129 */
130 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- index management on core tables: no WordPress API exists and nothing here is cacheable.
131
132 function AHSC_check_key_exists($index_name,$table_name){
133 global $wpdb;
134 $pfx=$wpdb->prefix.substr($table_name,'3',strlen($table_name));
135
136 /*
137 * Three things were wrong here.
138 *
139 * 1. The index and table names were interpolated straight into the query text. They
140 * come from the $ahsc_tables constant map rather than from user input, so it was
141 * not exploitable, but there is no reason not to prepare it: INDEX_NAME and
142 * TABLE_NAME are compared as values, so %s is the correct placeholder.
143 *
144 * 2. The result came from $wpdb->query(), which returns false when the query fails,
145 * and the old "return ( $result !== 0 ) ? 1 : 0" read that false as 1, i.e. "the
146 * index is already there". A failing lookup therefore reported the table as
147 * optimized: AHSC_DBOPT_Optimize() skipped creating the index and
148 * AHSC_DBOPT_Check() declared everything fine. get_var() returns null on failure,
149 * which is distinguishable from a legitimate count of zero.
150 *
151 * 3. INFORMATION_SCHEMA spans every schema on the server, and the query had no
152 * TABLE_SCHEMA filter. On shared MySQL with several installations using the same
153 * table prefix, an index belonging to another database counted as ours.
154 * DATABASE() pins the lookup to the current connection.
155 */
156 $found = $wpdb->get_var(
157 $wpdb->prepare(
158 'SELECT COUNT(DISTINCT INDEX_NAME) FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND INDEX_NAME = %s',
159 array( $pfx, $index_name )
160 )
161 );
162
163 if ( null === $found ) {
164 AHSC_log(
165 sprintf( 'Index lookup failed for %1$s.%2$s: %3$s', $pfx, $index_name, $wpdb->last_error ),
166 'db-optimization',
167 'warning'
168 );
169
170 /*
171 * Report the index as missing rather than present. The optimization is then
172 * attempted and any problem surfaces — at worst MySQL answers "Duplicate key
173 * name", which is harmless and gets logged — whereas claiming it exists hides
174 * the failure behind a green status.
175 */
176 return 0;
177 }
178
179 $result = (int) $found;
180 /*echo "<pre> <p>===================================SQLCONTROLLOESISTENZA=====================================================</p>".
181 "<p>index : $index_name table: $pfx </p>".
182 "<p>sql : $sql</p>".
183 "<p> SQL RESULT :".var_export($result,true)."<p>".
184 "<p> CHECK SINGLE RESULT :".var_export(($result!==0)?1:0,true)."</p>".
185 "<p>================================================================================================</p></pre>";*/
186 return ($result!==0)?1:0;
187 }
188 function AHSC_DBOPT_manage($status){
189 $result=array("status"=>$status);
190 if($status!=="false"){
191 $result['action']="ottimizza";
192 $result+=AHSC_DBOPT_Optimize();
193 }else{
194 $result['action']="elimina";
195 $result+=AHSC_DBOPT_Drop_chenges();
196 }
197 return $result;
198 }
199
200 /* AGGIUNTA KEY
201
202 ALTER TABLE `ps_cart_rule` ADD KEY `id_customer` (`id_customer`,`active`,`date_to`);
203
204 */
205
206 /*AGGIUNTA UNQIUE
207
208 ALTER TABLE table_name ADD CONSTRAINT unique_name UNIQUE (field1, field2, ...);
209
210 */
211
212 function AHSC_DBOPT_Optimize(){
213 global $ahsc_tables,$wpdb;
214 $query_result=array();
215 foreach($ahsc_tables as $table_name=>$index_settings){
216 $pfx=$wpdb->prefix.substr($table_name,'3',strlen($table_name));
217 //$sql="ALTER TABLE {$pfx} ROW_FORMAT=DYNAMIC;";
218 $wpdb->query( $wpdb->prepare( "ALTER TABLE %i ROW_FORMAT=DYNAMIC;", array( $pfx ) ) );
219 foreach($index_settings as $index_name=>$index_param){
220
221 //$str_param=implode(",",$index_param['param']);
222
223 //$query_result[$pfx][$index_name]=array();
224 /*
225 * The placeholder list used to be assembled at runtime ("%i,%i,%i") and
226 * interpolated into the query text. That works — $wpdb->prepare() accepts the
227 * arguments as a single array — but it makes the statement impossible to
228 * verify statically, because neither a reader nor PHPCS can match the
229 * placeholders against the arguments. Index definitions carry 1 to 5 columns
230 * (wp_posts uses 5), so one literal query per arity keeps every placeholder
231 * checkable, with a logged fallback if a wider index is ever added.
232 */
233 $columns = array_values( $index_param["param"] );
234 $k_exs=AHSC_check_key_exists($index_name,$table_name);
235
236 /*switch ($index_param['type']) {
237 case "UNIQUE KEY":
238 $query_result[$pfx][$index_name]['sql']=$wpdb->prepare("ALTER TABLE %i ADD CONSTRAINT %i UNIQUE ($param_prepare_str)",$prepare_arr);
239 case "KEY":
240 $query_result[$pfx][$index_name]['sql']=$wpdb->prepare("ALTER TABLE %i ADD KEY %i ($param_prepare_str)",$prepare_arr);
241
242 }*/
243
244 if(!$k_exs){
245
246 $is_unique = ( 'UNIQUE KEY' === $index_param['type'] );
247
248 // Cleared so the statements below can be checked as a group afterwards.
249 $wpdb->last_error = '';
250
251 switch ( count( $columns ) ) {
252 case 1:
253 if ( $is_unique ) {
254 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD CONSTRAINT %i UNIQUE (%i)', $pfx, $index_name, $columns[0] ) );
255 } else {
256 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD KEY %i (%i)', $pfx, $index_name, $columns[0] ) );
257 }
258 break;
259 case 2:
260 if ( $is_unique ) {
261 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD CONSTRAINT %i UNIQUE (%i,%i)', $pfx, $index_name, $columns[0], $columns[1] ) );
262 } else {
263 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD KEY %i (%i,%i)', $pfx, $index_name, $columns[0], $columns[1] ) );
264 }
265 break;
266 case 3:
267 if ( $is_unique ) {
268 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD CONSTRAINT %i UNIQUE (%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2] ) );
269 } else {
270 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD KEY %i (%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2] ) );
271 }
272 break;
273 case 4:
274 if ( $is_unique ) {
275 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD CONSTRAINT %i UNIQUE (%i,%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2], $columns[3] ) );
276 } else {
277 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD KEY %i (%i,%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2], $columns[3] ) );
278 }
279 break;
280 case 5:
281 if ( $is_unique ) {
282 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD CONSTRAINT %i UNIQUE (%i,%i,%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2], $columns[3], $columns[4] ) );
283 } else {
284 $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD KEY %i (%i,%i,%i,%i,%i)', $pfx, $index_name, $columns[0], $columns[1], $columns[2], $columns[3], $columns[4] ) );
285 }
286 break;
287 default:
288 // Never silently skip an index: adding a wider one to $ahsc_tables must be noticed.
289 AHSC_log(
290 sprintf( 'Unsupported index width (%1$d columns) for %2$s.%3$s: index not created.', count( $columns ), $pfx, $index_name ),
291 'db-optimization',
292 'warning'
293 );
294 break;
295 }
296
297 // A failed ALTER used to pass unnoticed: the return value was discarded.
298 if ( '' !== $wpdb->last_error ) {
299 AHSC_log(
300 sprintf( 'Could not create index %1$s on %2$s: %3$s', $index_name, $pfx, $wpdb->last_error ),
301 'db-optimization',
302 'warning'
303 );
304 }
305
306 }
307 }
308 }
309 /*echo "<pre><p>===================================AGGIUNTA=====================================================</p>".
310 var_export($query_result,true).
311 "<p>================================================================================================</p></pre>";*/
312 return $query_result;
313 }
314 //AHSC_DBOPT_Optimize();
315
316 /*CANCELLAZIONE
317 *
318 * ALTER TABLE `my_table` DROP KEY `name_of_my_key`
319 * ALTER TABLE table_name DROP INDEX unique_name,
320 **/
321 function AHSC_DBOPT_Drop_chenges(){
322 global $ahsc_tables,$wpdb;
323 $query_result=array();
324 foreach($ahsc_tables as $table_name=>$index_settings){
325 foreach($index_settings as $index_name=>$index_param){
326
327 //$str_param=implode(",",$index_param['param']);
328 $pfx=$wpdb->prefix.substr($table_name,'3',strlen($table_name));
329 switch ($index_param['type']){
330 case "UNIQUE KEY":
331 //$sql="ALTER TABLE {$pfx} DROP INDEX {$index_name}";
332
333 $wpdb->query( $wpdb->prepare( "DROP INDEX %i ON %i;", array( $index_name, $pfx ) ) );
334
335 break;
336 case "KEY":
337 //$sql="ALTER TABLE {$pfx} DROP KEY {$index_name}";
338 $wpdb->query( $wpdb->prepare( "ALTER TABLE %i DROP KEY %i;", array( $pfx, $index_name ) ) );
339 break;
340 }
341 //$query_result[$pfx][$index_name]=array();
342 //$query_result[$pfx][$index_name]['sql'] = $sql; //$wpdb->query( $sql );
343 //$query_result[$pfx][$index_name]['result'] = $wpdb->query( $sql );
344
345 }
346 }
347 //var_dump($query_result);
348 return $query_result;
349 }
350
351 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
352
353 //AHSC_DBOPT_Drop_chenges();