/var/www/html_de/wp-content/plugins/query-monitor/output/html/db_queries.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php declare(strict_types 1);
/**
 * Database query output for HTML pages.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

class 
QM_Output_Html_DB_Queries extends QM_Output_Html {

    
/**
     * Collector instance.
     *
     * @var QM_Collector_DB_Queries Collector.
     */
    
protected $collector;

    
/**
     * @var int
     */
    
public $query_row 0;

    public function 
__constructQM_Collector $collector ) {
        
parent::__construct$collector );
        
add_filter'qm/output/menus', array( $this'admin_menu' ), 20 );
        
add_filter'qm/output/panel_menus', array( $this'panel_menu' ), 20 );
        
add_filter'qm/output/title', array( $this'admin_title' ), 20 );
        
add_filter'qm/output/menu_class', array( $this'admin_class' ) );
    }

    
/**
     * @return string
     */
    
public function name() {
        return 
__'Database Queries''query-monitor' );
    }

    
/**
     * @return void
     */
    
public function output() {
        
/** @var QM_Data_DB_Queries $data */
        
$data $this->collector->get_data();

        if ( empty( 
$data->rows ) ) {
            
$this->output_empty_queries();
            return;
        }

        if ( ! empty( 
$data->errors ) ) {
            
$this->output_error_queries$data->errors );
        }

        if ( ! empty( 
$data->expensive ) ) {
            
$this->output_expensive_queries$data->expensive );
        }

        
$this->output_queries$data );
    }

    
/**
     * @return void
     */
    
protected function output_empty_queries() {
        
$this->before_non_tabular_output();

        if ( ! 
SAVEQUERIES ) {
            
$notice sprintf(
                
/* translators: 1: Name of PHP constant, 2: Value of PHP constant */
                
esc_html__'No database queries were logged because the %1$s constant is set to %2$s.''query-monitor' ),
                
'<code>SAVEQUERIES</code>',
                
'<code>false</code>'
            
);
        } else {
            
$notice __'No database queries were logged.''query-monitor' );
        }

        echo 
$this->build_notice$notice ); // WPCS: XSS ok.
        
$this->after_non_tabular_output();
    }

    
/**
     * @param array<int, mixed> $errors
     * @return void
     */
    
protected function output_error_queries( array $errors ) {
        
$this->before_tabular_output'qm-query-errors'__'Database Errors''query-monitor' ) );

        echo 
'<thead>';
        echo 
'<tr>';
        echo 
'<th scope="col">' esc_html__'Query''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Caller''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Component''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Error Message''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Error Code''query-monitor' ) . '</th>';
        echo 
'</tr>';
        echo 
'</thead>';
        echo 
'<tbody>';

        foreach ( 
$errors as $row ) {
            
$this->output_query_row$row, array( 'sql''caller''component''errno''result' ) );
        }

        echo 
'</tbody>';

        
$this->after_tabular_output();
    }

    
/**
     * @param array<int, mixed> $expensive
     * @return void
     */
    
protected function output_expensive_queries( array $expensive ) {
        
$dp strlensubstrstrrchr( (string) QM_DB_EXPENSIVE'.' ) ?: '.0') );

        
$panel_name sprintf(
            
/* translators: %s: Database query time in seconds */
            
esc_html__'Slow Database Queries (above %ss)''query-monitor' ),
            
'<span class="qm-warn">' esc_htmlnumber_format_i18nQM_DB_EXPENSIVE$dp ) ) . '</span>'
        
);
        
$this->before_tabular_output'qm-query-expensive'$panel_name );

        echo 
'<thead>';
        echo 
'<tr>';
        echo 
'<th scope="col">' esc_html__'Query''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Caller''query-monitor' ) . '</th>';

        if ( isset( 
$expensive[0]['component'] ) ) {
            echo 
'<th scope="col">' esc_html__'Component''query-monitor' ) . '</th>';
        }

        if ( isset( 
$expensive[0]['result'] ) ) {
            echo 
'<th scope="col" class="qm-num">' esc_html__'Rows''query-monitor' ) . '</th>';
        }

        echo 
'<th scope="col" class="qm-num">' esc_html__'Time''query-monitor' ) . '</th>';
        echo 
'</tr>';
        echo 
'</thead>';
        echo 
'<tbody>';

        foreach ( 
$expensive as $row ) {
            
$this->output_query_row$row, array( 'sql''caller''component''result''time' ) );
        }

        echo 
'</tbody>';

        
$this->after_tabular_output();
    }

    
/**
     * @param QM_Data_DB_Queries $data
     * @return void
     */
    
protected function output_queriesQM_Data_DB_Queries $data ) {
        
$this->query_row 0;
        
$span 4;

        if ( 
$data->has_result ) {
            
$span++;
        }
        if ( 
$data->has_trace ) {
            
$span++;
        }

        
$this->before_tabular_output();

        echo 
'<thead>';

        
/**
         * Filter whether to show the QM extended query information prompt.
         *
         * By default QM shows a prompt to install the QM db.php drop-in,
         * this filter allows a dev to choose not to show the prompt.
         *
         * @since 2.9.0
         *
         * @param bool $show_prompt Whether to show the prompt.
         */
        
if ( apply_filters'qm/show_extended_query_prompt'true ) && ! $data->has_trace ) {
            echo 
'<tr>';
            
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            
echo '<th colspan="' intval$span ) . '" class="qm-warn">' QueryMonitor::icon'warning' );
            if ( 
file_existsWP_CONTENT_DIR '/db.php' ) ) {
                
/* translators: %s: File name */
                
$message __'Extended query information such as the component and affected rows is not available. A conflicting %s file is present.''query-monitor' );
            } elseif ( 
defined'QM_DB_SYMLINK' ) && ! QM_DB_SYMLINK ) {
                
/* translators: 1: File name, 2: Configuration constant name */
                
$message __'Extended query information such as the component and affected rows is not available. Query Monitor was prevented from symlinking its %1$s file into place by the %2$s constant.''query-monitor' );
            } else {
                
/* translators: %s: File name */
                
$message __'Extended query information such as the component and affected rows is not available. Query Monitor was unable to symlink its %s file into place.''query-monitor' );
            }
            
printf(
                
esc_html$message ),
                
'<code>db.php</code>',
                
'<code>QM_DB_SYMLINK</code>'
            
);

            
printf(
                
' <a href="%s" target="_blank" class="qm-external-link">See this help page for more information.</a>',
                
'https://querymonitor.com/help/db-php-symlink/'
            
);
            echo 
'</th>';
            echo 
'</tr>';
        }

        
$types array_keys$data->types );
        
$prepend = array();
        
$callers array_column$data->times'caller' );

        
sort$types );
        
usort$callers'strcasecmp' );

        if ( 
count$types ) > ) {
            
$prepend['non-select'] = __'Non-SELECT''query-monitor' );
        }

        
$args = array(
            
'prepend' => $prepend,
        );

        echo 
'<tr>';
        echo 
'<th scope="col" class="qm-sorted-asc qm-sortable-column" role="columnheader" aria-sort="ascending">';
        echo 
$this->build_sorter'#' ); // WPCS: XSS ok;
        
echo '</th>';
        echo 
'<th scope="col" class="qm-filterable-column">';
        echo 
$this->build_filter'type'$types__'Query''query-monitor' ), $args ); // WPCS: XSS ok;
        
echo '</th>';
        echo 
'<th scope="col" class="qm-filterable-column">';

        
$prepend = array();

        if ( 
$data->has_main_query ) {
            
$prepend['qm-main-query'] = __'Main Query''query-monitor' );
        }

        
$args = array(
            
'prepend' => $prepend,
        );
        echo 
$this->build_filter'caller'$callers__'Caller''query-monitor' ), $args ); // WPCS: XSS ok.
        
echo '</th>';

        if ( 
$data->has_trace ) {
            
$components array_column$data->component_times'component' );

            
usort$components'strcasecmp' );

            echo 
'<th scope="col" class="qm-filterable-column">';
            echo 
$this->build_filter'component'$components__'Component''query-monitor' ) ); // WPCS: XSS ok.
            
echo '</th>';
        }

        if ( 
$data->has_result ) {
            if ( empty( 
$data->errors ) ) {
                
$class 'qm-num';
            } else {
                
$class '';
            }
            echo 
'<th scope="col" class="' esc_attr$class ) . ' qm-sortable-column" role="columnheader">';
            echo 
$this->build_sorter__'Rows''query-monitor' ) ); // WPCS: XSS ok.
            
echo '</th>';
        }

        echo 
'<th scope="col" class="qm-num qm-sortable-column" role="columnheader">';
        echo 
$this->build_sorter__'Time''query-monitor' ) ); // WPCS: XSS ok.
        
echo '</th>';
        echo 
'</tr>';
        echo 
'</thead>';

        echo 
'<tbody>';

        foreach ( 
$data->rows as $row ) {
            
$this->output_query_row$row, array( 'row''sql''caller''component''result''time' ) );
        }

        echo 
'</tbody>';
        echo 
'<tfoot>';

        
$total_stime number_format_i18n$data->total_time);

        echo 
'<tr>';
        echo 
'<td colspan="' intval$span ) . '">';
        
printf(
            
/* translators: %s: Number of database queries */
            
esc_html_nx'Total: %s''Total: %s'$data->total_qs'Query count''query-monitor' ) ),
            
'<span class="qm-items-number">' esc_htmlnumber_format_i18n$data->total_qs ) ) . '</span>'
        
);
        echo 
'</td>';
        echo 
'<td class="qm-num qm-items-time">' esc_html$total_stime ) . '</td>';
        echo 
'</tr>';
        echo 
'</tfoot>';

        
$this->after_tabular_output();
    }

    
/**
     * @param array<string, mixed> $row
     * @param array<int, string> $cols
     * @return void
     */
    
protected function output_query_row( array $row, array $cols ) {

        
$cols array_flip$cols );

        if ( ! isset( 
$row['component'] ) ) {
            unset( 
$cols['component'] );
        }
        if ( ! isset( 
$row['result'] ) ) {
            unset( 
$cols['result'], $cols['errno'] );
        }

        
$stime number_format_i18n$row['ltime'], );
        
$sql $row['sql'];

        if ( 
'Unknown' === $row['type'] ) {
            
$sql "<code>{$sql}</code>";
        } else {
            
$sql self::format_sql$row['sql'] );
        }

        if ( 
'SELECT' !== $row['type'] ) {
            
$sql "<span class='qm-nonselectsql'>{$sql}</span>";
        }

        if ( isset( 
$row['trace'] ) ) {

            
$caller $row['trace']->get_caller();
            
$caller_name self::output_filename$row['caller'], $caller['calling_file'], $caller['calling_line'] );
            
$stack = array();
            
$filtered_trace $row['trace']->get_filtered_trace();
            
array_shift$filtered_trace );

            foreach ( 
$filtered_trace as $frame ) {
                
$stack[] = self::output_filename$frame['display'], $frame['calling_file'], $frame['calling_line'] );
            }
        } else {

            if ( ! empty( 
$row['caller'] ) ) {
                
$caller_name '<code>' esc_html$row['caller'] ) . '</code>';
            } else {
                
$caller_name '<code>' esc_html__'Unknown''query-monitor' ) . '</code>';
            }

            
$stack $row['stack'];
            
array_shift$stack );
            
$stack array_map( function( $frame ) {
                return 
'<code>' esc_html$frame ) . '</code>';
            }, 
$stack );

        }

        
$row_attr = array();

        if ( 
$row['result'] instanceof WP_Error ) {
            
$row_attr['class'] = 'qm-warn';
        }
        if ( isset( 
$cols['sql'] ) ) {
            
$row_attr['data-qm-type'] = $row['type'];
            if ( 
'SELECT' !== $row['type'] ) {
                
$row_attr['data-qm-type'] .= ' non-select';
            }
        }
        if ( isset( 
$cols['component'] ) && $row['component'] ) {
            
$row_attr['data-qm-component'] = $row['component']->name;

            if ( 
'core' !== $row['component']->context ) {
                
$row_attr['data-qm-component'] .= ' non-core';
            }
        }
        if ( isset( 
$cols['caller'] ) && ! empty( $row['caller_name'] ) ) {
            
$row_attr['data-qm-caller'] = $row['caller_name'];

            if ( 
$row['is_main_query'] ) {
                
$row_attr['data-qm-caller'] .= ' qm-main-query';
            }
        }
        if ( isset( 
$cols['time'] ) ) {
            
$row_attr['data-qm-time'] = $row['ltime'];
        }

        
$attr '';

        foreach ( 
$row_attr as $a => $v ) {
            
$attr .= ' ' $a '="' esc_attr$v ) . '"';
        }

        echo 
"<tr{$attr}>"// WPCS: XSS ok.

        
if ( isset( $cols['row'] ) ) {
            echo 
'<th scope="row" class="qm-row-num qm-num">' intval( ++$this->query_row ) . '</th>';
        }

        if ( isset( 
$cols['sql'] ) ) {
            
printf// WPCS: XSS ok.
                
'<td class="qm-row-sql qm-ltr qm-wrap">%s</td>',
                
$sql
            
);
        }

        if ( isset( 
$cols['caller'] ) ) {
            echo 
'<td class="qm-row-caller qm-ltr qm-has-toggle qm-nowrap">';

            if ( ! empty( 
$stack ) ) {
                echo 
self::build_toggler(); // WPCS: XSS ok;
            
}

            echo 
'<ol>';
            echo 
"<li>{$caller_name}</li>"// WPCS: XSS ok.

            
if ( ! empty( $stack ) ) {
                echo 
'<div class="qm-toggled"><li>' implode'</li><li>'$stack ) . '</li></div>'// WPCS: XSS ok.
            
}

            echo 
'</ol>';
            if ( 
$row['is_main_query'] ) {
                
printf(
                    
'<p>%s</p>',
                    
esc_html__'Main Query''query-monitor' )
                );
            }
            echo 
'</td>';
        }

        if ( isset( 
$cols['stack'] ) ) {
            echo 
'<td class="qm-row-caller qm-row-stack qm-nowrap qm-ltr"><ol>';
            if ( ! empty( 
$stack ) ) {
                echo 
'<li>' implode'</li><li>'$stack ) . '</li>'// WPCS: XSS ok.
            
}
            echo 
"<li>{$caller_name}</li>"// WPCS: XSS ok.
            
echo '</ol></td>';
        }

        if ( isset( 
$cols['component'] ) ) {
            if ( 
$row['component'] ) {
            echo 
"<td class='qm-row-component qm-nowrap'>" esc_html$row['component']->name ) . "</td>\n";
            } else {
                echo 
"<td class='qm-row-component qm-nowrap'>" esc_html__'Unknown''query-monitor' ) . "</td>\n";
            }
        }

        if ( isset( 
$cols['result'] ) ) {
            if ( 
$row['result'] instanceof WP_Error ) {
                
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                
echo "<td class='qm-row-result qm-row-error'>" QueryMonitor::icon'warning' ) . esc_html$row['result']->get_error_message() ) . "</td>\n";
            } else {
                echo 
"<td class='qm-row-result qm-num'>" esc_html$row['result'] ) . "</td>\n";
            }
        }

        if ( isset( 
$cols['errno'] ) && ( $row['result'] instanceof WP_Error ) ) {
            echo 
"<td class='qm-row-result qm-row-error'>" esc_html$row['result']->get_error_code() ) . "</td>\n";
        }

        if ( isset( 
$cols['time'] ) ) {
            
$expensive $this->collector->is_expensive$row );
            
$td_class = ( $expensive ) ? ' qm-warn' '';

            echo 
'<td class="qm-num qm-row-time' esc_attr$td_class ) . '" data-qm-sort-weight="' esc_attr$row['ltime'] ) . '">';

            if ( 
$expensive ) {
                
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                
echo QueryMonitor::icon'warning' );
            }

            echo 
esc_html$stime );
            echo 
"</td>\n";
        }

        echo 
'</tr>';

    }

    
/**
     * @param array<int, string> $title
     * @return array<int, string>
     */
    
public function admin_title( array $title ) {
        
/** @var QM_Data_DB_Queries $data */
        
$data $this->collector->get_data();

        if ( isset( 
$data->rows ) ) {
            
$title[] = sprintf(
                
/* translators: %s: A time in seconds with a decimal fraction. No space between value and unit symbol. */
                
esc_html_x'%ss''Time in seconds''query-monitor' ),
                
number_format_i18n$data->total_time)
            );

            
/* translators: %s: Number of database queries. Note the space between value and unit symbol. */
            
$text _n'%s Q''%s Q'$data->total_qs'query-monitor' );

            
// Avoid a potentially blank translation for the plural form.
            // @see https://meta.trac.wordpress.org/ticket/5377
            
if ( '' === $text ) {
                
$text '%s Q';
            }

            
$title[] = preg_replace'#\s?([^0-9,\.]+)#''<small>$1</small>'sprintf(
                
esc_html$text ),
                
number_format_i18n$data->total_qs )
            ) );
        } elseif ( isset( 
$data->total_qs ) ) {
            
/* translators: %s: Number of database queries. Note the space between value and unit symbol. */
            
$text _n'%s Q''%s Q'$data->total_qs'query-monitor' );

            
// Avoid a potentially blank translation for the plural form.
            // @see https://meta.trac.wordpress.org/ticket/5377
            
if ( '' === $text ) {
                
$text '%s Q';
            }

            
$title[] = preg_replace'#\s?([^0-9,\.]+)#''<small>$1</small>'sprintf(
                
esc_html$text ),
                
number_format_i18n$data->total_qs )
            ) );
        }

        return 
$title;
    }

    
/**
     * @param array<int, string> $class
     * @return array<int, string>
     */
    
public function admin_class( array $class ) {

        if ( 
$this->collector->get_errors() ) {
            
$class[] = 'qm-error';
        }
        if ( 
$this->collector->get_expensive() ) {
            
$class[] = 'qm-expensive';
        }
        return 
$class;

    }

    
/**
     * @param array<string, mixed[]> $menu
     * @return array<string, mixed[]>
     */
    
public function admin_menu( array $menu ) {
        
/** @var QM_Data_DB_Queries $data */
        
$data $this->collector->get_data();
        
$errors $this->collector->get_errors();
        
$expensive $this->collector->get_expensive();

        
$id $this->collector->id();
        
$menu$id ] = $this->menu( array(
            
'title' => esc_html__'Database Queries''query-monitor' ),
            
// 'href' => esc_attr( sprintf( '#%s', $this->collector->id() ) ),
        
) );

        if ( 
$errors ) {
            
$id $this->collector->id() . '-errors';
            
$count count$errors );
            
$menu$id ] = $this->menu( array(
                
'id' => 'query-monitor-errors',
                
'href' => '#qm-query-errors',
                
'title' => esc_htmlsprintf(
                    
/* translators: %s: Number of database errors */
                    
__'Database Errors (%s)''query-monitor' ),
                    
number_format_i18n$count )
                ) ),
            ) );
        }

        if ( 
$expensive ) {
            
$id $this->collector->id() . '-expensive';
            
$count count$expensive );
            
$menu$id ] = $this->menu( array(
                
'id' => 'query-monitor-expensive',
                
'href' => '#qm-query-expensive',
                
'title' => esc_htmlsprintf(
                    
/* translators: %s: Number of slow database queries */
                    
__'Slow Queries (%s)''query-monitor' ),
                    
number_format_i18n$count )
                ) ),
            ) );
        }

        return 
$menu;

    }

    
/**
     * @param array<string, mixed[]> $menu
     * @return array<string, mixed[]>
     */
    
public function panel_menu( array $menu ) {
        foreach ( array( 
'errors''expensive' ) as $sub ) {
            
$id $this->collector->id() . '-' $sub;
            if ( isset( 
$menu$id ] ) ) {
                
$menu['qm-db_queries']['children'][] = $menu$id ];
                unset( 
$menu$id ] );
            }
        }

        return 
$menu;
    }

}

/**
 * @param array<string, QM_Output> $output
 * @param QM_Collectors $collectors
 * @return array<string, QM_Output>
 */
function register_qm_output_html_db_queries( array $outputQM_Collectors $collectors ) {
    
$collector QM_Collectors::get'db_queries' );
    if ( 
$collector ) {
        
$output['db_queries'] = new QM_Output_Html_DB_Queries$collector );
    }
    return 
$output;
}

add_filter'qm/outputter/html''register_qm_output_html_db_queries'20);