prefix . 'aieo_survey_value_stage'; } public static function ensure_table() { if ( null !== self::$table_ok ) { return self::$table_ok; } global $wpdb; $t = self::table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t ) { self::$table_ok = true; return true; } $charset = aieo_get_charset_collate(); $wpdb->query( "CREATE TABLE IF NOT EXISTS {$t} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, surface varchar(64) NOT NULL, field_key varchar(64) NOT NULL, value_hash char(32) NOT NULL, observed_value varchar(255) NOT NULL DEFAULT '', hits int(10) unsigned NOT NULL DEFAULT 0, sample_entry_id bigint(20) unsigned NOT NULL DEFAULT 0, harvested_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY uk_seen (surface, field_key, value_hash), KEY idx_field (surface, field_key, hits) ) {$charset}" ); self::$table_ok = (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) === $t; return self::$table_ok; } public static function replace( $surface, array $seen ) { global $wpdb; $surface = (string) $surface; if ( '' === $surface || ! self::ensure_table() ) { return 0; } $wpdb->delete( self::table(), array( 'surface' => $surface ), array( '%s' ) ); $rows = 0; $tuples = array(); $args = array(); foreach ( $seen as $field => $values ) { foreach ( (array) $values as $value => $meta ) { $value = (string) $value; if ( '' === trim( $value ) ) { continue; } $tuples[] = '(%s, %s, %s, %s, %d, %d)'; $args[] = $surface; $args[] = mb_substr( (string) $field, 0, 64 ); $args[] = md5( AIEO_Survey_Choice_Register::normalise( $value ) ); $args[] = mb_substr( $value, 0, 255 ); $args[] = isset( $meta['hits'] ) ? (int) $meta['hits'] : 1; $args[] = isset( $meta['sample_entry_id'] ) ? (int) $meta['sample_entry_id'] : 0; $rows++; if ( count( $tuples ) >= 500 ) { self::flush( $tuples, $args ); $tuples = array(); $args = array(); } } } if ( $tuples ) { self::flush( $tuples, $args ); } return $rows; } private static function flush( array $tuples, array $args ) { global $wpdb; $sql = 'INSERT INTO ' . self::table() . ' (surface, field_key, value_hash, observed_value, hits, sample_entry_id) VALUES ' . implode( ', ', $tuples ) . ' ON DUPLICATE KEY UPDATE hits = hits + VALUES(hits)'; $wpdb->query( $wpdb->prepare( $sql, $args ) ); } public static function values( $surface, $field_key, $limit = 200 ) { global $wpdb; if ( ! self::ensure_table() ) { return array(); } return (array) $wpdb->get_results( $wpdb->prepare( 'SELECT observed_value, hits, sample_entry_id FROM ' . self::table() . ' WHERE surface = %s AND field_key = %s ORDER BY hits DESC, observed_value ASC LIMIT %d', (string) $surface, (string) $field_key, max( 1, (int) $limit ) ), ARRAY_A ); } public static function unresolved( $surface, $limit = 200 ) { global $wpdb; if ( ! self::ensure_table() || ! AIEO_Survey_Choice_Register::ensure_table() ) { return array(); } $wanted = array(); foreach ( AIEO_Survey_Mapping::resolved( $surface ) as $field => $code ) { $q = AIEO_Survey_Blueprint::question( $code ); if ( ! $q || 'choice' !== $q['answers'] || 'scale' === $q['scored'] ) { continue; } $wanted[ (string) $field ] = true; } if ( ! $wanted ) { return array(); } $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT s.field_key, s.observed_value, s.hits FROM ' . self::table() . ' s LEFT JOIN ' . AIEO_Survey_Choice_Register::table() . ' r ON r.value_hash = s.value_hash WHERE s.surface = %s AND r.id IS NULL ORDER BY s.hits DESC LIMIT %d', (string) $surface, max( 1, (int) $limit ) * 8 ), ARRAY_A ); $out = array(); foreach ( $rows as $r ) { if ( ! isset( $wanted[ (string) $r['field_key'] ] ) ) { continue; } $out[] = $r; if ( count( $out ) >= max( 1, (int) $limit ) ) { break; } } return $out; } public static function clear( $surface ) { global $wpdb; if ( ! self::ensure_table() ) { return; } $wpdb->delete( self::table(), array( 'surface' => (string) $surface ), array( '%s' ) ); } }