From b8e656c3dbd22e61f42275e1a1e465777e4271b2 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 10 Oct 2023 10:06:57 +0200 Subject: [PATCH 1/6] Added better conditions on the slow queries to process faster --- inc/classes/class-imagify-db.php | 27 +++++++++++++++++++-------- inc/functions/attachments.php | 13 +++++++++---- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index 6a12cd066..d021dcca4 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -159,18 +159,20 @@ public static function get_post_statuses() { * It returns an empty string if the database has no attachments without the required metadada. * It also triggers Imagify_DB::unlimit_joins(). * - * @since 1.7 - * @access public - * @author Grégory Viguier - * - * @param string $id_field An ID field to match the metadata ID against in the JOIN clause. + * @param string $id_field An ID field to match the metadata ID against in the JOIN clause. * Default is the posts table `ID` field, using the `p` alias: `p.ID`. * In case of "false" value or PEBKAC, fallback to the same field without alias. - * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. - * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. + * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. + * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. + * @param string $special_join_conditions Special conditions to apply on the join. + * * @return string + * @author Grégory Viguier + * + * @since 1.7 + * @access public */ - public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true ) { + public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $matching = true, $test = true, $special_join_conditions = '' ) { global $wpdb; if ( $test && ! imagify_has_attachments_without_required_metadata() ) { @@ -186,7 +188,16 @@ public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $join = $matching ? 'INNER' : 'LEFT'; + $first = true; + foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) { + if($first) { + $first = false; + $clause .= " + $join JOIN $wpdb->postmeta AS $alias + ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' $special_join_conditions )"; + continue; + } $clause .= " $join JOIN $wpdb->postmeta AS $alias ON ( $id_field = $alias.post_id AND $alias.meta_key = '$meta_name' )"; diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index f24999cc4..5691f3097 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -115,7 +115,14 @@ function imagify_has_attachments_without_required_metadata() { $mime_types = Imagify_DB::get_mime_types(); $statuses = Imagify_DB::get_post_statuses(); - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( 'p.ID', false, false ); + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause( + 'p.ID', + false, + false, + "AND p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses )" + ); $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause( array( 'matching' => false, 'test' => false, @@ -125,9 +132,7 @@ function imagify_has_attachments_without_required_metadata() { SELECT p.ID FROM $wpdb->posts AS p $nodata_join - WHERE p.post_mime_type IN ( $mime_types ) - AND p.post_type = 'attachment' - AND p.post_status IN ( $statuses ) + WHERE $nodata_where LIMIT 1" ); From 4cab9a42c18ff3a79320c1b43ce740b807e6b3a0 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 10 Oct 2023 11:35:25 +0200 Subject: [PATCH 2/6] Fixed query syntax --- inc/classes/class-imagify-db.php | 10 ++++++++-- inc/functions/admin-stats.php | 9 +++++---- inc/functions/attachments.php | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index d021dcca4..ab3532ad7 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -323,10 +323,16 @@ public static function get_extensions_where_clause( $args = false ) { return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; } + $extensions = array_map(function ($ex) { + return strrev($ex); + }, $extensions); + + $regex = '^' . implode('\..*|^', $extensions) . '\..*'; + if ( $matching ) { - $query[ $key ] = "AND ( LOWER( $alias.meta_value ) LIKE '%." . implode( "' OR LOWER( $alias.meta_value ) LIKE '%.", $extensions ) . "' )"; + $query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'"; } else { - $query[ $key ] = "OR ( LOWER( $alias.meta_value ) NOT LIKE '%." . implode( "' AND LOWER( $alias.meta_value ) NOT LIKE '%.", $extensions ) . "' )"; + $query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) NOT REGEXP '$regex'"; } return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; diff --git a/inc/functions/admin-stats.php b/inc/functions/admin-stats.php index 95f1bb69d..987796356 100755 --- a/inc/functions/admin-stats.php +++ b/inc/functions/admin-stats.php @@ -33,16 +33,17 @@ function imagify_count_attachments() { $mime_types = Imagify_DB::get_mime_types(); $statuses = Imagify_DB::get_post_statuses(); - $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause(); + $nodata_join = Imagify_DB::get_required_wp_metadata_join_clause('p.ID', true, true, + "AND p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses )"); $nodata_where = Imagify_DB::get_required_wp_metadata_where_clause(); $count = (int) $wpdb->get_var( // WPCS: unprepared SQL ok. " SELECT COUNT( p.ID ) FROM $wpdb->posts AS p $nodata_join - WHERE p.post_mime_type IN ( $mime_types ) - AND p.post_type = 'attachment' - AND p.post_status IN ( $statuses ) + WHERE 1 = 1 $nodata_where" ); diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index 5691f3097..94802e4c6 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -133,6 +133,7 @@ function imagify_has_attachments_without_required_metadata() { FROM $wpdb->posts AS p $nodata_join WHERE + 1 = 1 $nodata_where LIMIT 1" ); From 8517876890ef44b03ecbf408da1b9eafdeae2bbe Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 10 Oct 2023 11:55:50 +0200 Subject: [PATCH 3/6] Fixed PHPCS --- inc/classes/class-imagify-db.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index d021dcca4..cb2a5e671 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -162,8 +162,8 @@ public static function get_post_statuses() { * @param string $id_field An ID field to match the metadata ID against in the JOIN clause. * Default is the posts table `ID` field, using the `p` alias: `p.ID`. * In case of "false" value or PEBKAC, fallback to the same field without alias. - * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. - * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. + * @param bool $matching Set to false to get a query to fetch metas NOT matching the file extensions. + * @param bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway. * @param string $special_join_conditions Special conditions to apply on the join. * * @return string @@ -191,7 +191,7 @@ public static function get_required_wp_metadata_join_clause( $id_field = 'p.ID', $first = true; foreach ( self::get_required_wp_metadata_aliases() as $meta_name => $alias ) { - if($first) { + if ( $first ) { $first = false; $clause .= " $join JOIN $wpdb->postmeta AS $alias From 717e66048a5c0819c9c915952a85080dd153ed4b Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 10 Oct 2023 15:33:10 +0200 Subject: [PATCH 4/6] Fixed PHPCS --- inc/classes/class-imagify-db.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index 48ea45402..62aad17a7 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -323,11 +323,11 @@ public static function get_extensions_where_clause( $args = false ) { return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; } - $extensions = array_map(function ($ex) { - return strrev($ex); + $extensions = array_map(function ( $ex ) { + return strrev( $ex ); }, $extensions); - $regex = '^' . implode('\..*|^', $extensions) . '\..*'; + $regex = '^' . implode( '\..*|^', $extensions ) . '\..*'; if ( $matching ) { $query[ $key ] = "AND REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'"; From 64ed64577242930525d98b84c74a70d8f1ec9503 Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Wed, 25 Oct 2023 14:46:20 +0200 Subject: [PATCH 5/6] Fixed problem with the output --- inc/functions/admin-stats.php | 4 +++- inc/functions/attachments.php | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/inc/functions/admin-stats.php b/inc/functions/admin-stats.php index 987796356..84d534de9 100755 --- a/inc/functions/admin-stats.php +++ b/inc/functions/admin-stats.php @@ -43,7 +43,9 @@ function imagify_count_attachments() { SELECT COUNT( p.ID ) FROM $wpdb->posts AS p $nodata_join - WHERE 1 = 1 + WHERE p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses ) $nodata_where" ); diff --git a/inc/functions/attachments.php b/inc/functions/attachments.php index 94802e4c6..af361d74a 100755 --- a/inc/functions/attachments.php +++ b/inc/functions/attachments.php @@ -132,8 +132,9 @@ function imagify_has_attachments_without_required_metadata() { SELECT p.ID FROM $wpdb->posts AS p $nodata_join - WHERE - 1 = 1 + WHERE p.post_mime_type IN ( $mime_types ) + AND p.post_type = 'attachment' + AND p.post_status IN ( $statuses ) $nodata_where LIMIT 1" ); From fe533a1f740baa32208db080e75cb9236d3a01cc Mon Sep 17 00:00:00 2001 From: COQUARD Cyrille Date: Tue, 31 Oct 2023 10:59:29 +0100 Subject: [PATCH 6/6] Fixed the static problem --- inc/classes/class-imagify-db.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/inc/classes/class-imagify-db.php b/inc/classes/class-imagify-db.php index 62aad17a7..b457c2145 100644 --- a/inc/classes/class-imagify-db.php +++ b/inc/classes/class-imagify-db.php @@ -310,6 +310,9 @@ public static function get_extensions_where_clause( $args = false ) { $extensions = array_keys( imagify_get_mime_types() ); $extensions = implode( '|', $extensions ); $extensions = explode( '|', $extensions ); + $extensions = array_map(function ( $ex ) { + return strrev( $ex ); + }, $extensions); } if ( ! $alias ) { @@ -323,10 +326,6 @@ public static function get_extensions_where_clause( $args = false ) { return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ]; } - $extensions = array_map(function ( $ex ) { - return strrev( $ex ); - }, $extensions); - $regex = '^' . implode( '\..*|^', $extensions ) . '\..*'; if ( $matching ) {