Support » Plugin: TablePress » Feature request for auto update: Don't update if table is unchanged

  • Resolved spellingerrer

    (@spellingerrer)



    Hi Tobias, I have a feature request for the auto update plugin: At the moment, the table will be listed as “last modified” whenever the auto update runs, even if the data in the table remains unchanged. Would it be possible to add a feature to test whether the imported file matches the previously imported version? Perhaps by saving a hash of the imported file in the database and then comparing whenever an import is performed?

    Thanks again for a great product!

    https://wordpress.org/plugins/tablepress/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your suggestion!

    I see what you mean, and this shouldn’t be too difficult to add, actually. One could maybe do this in the _import_tablepress_table() method, where the existing table data would have to be loaded, in addition to the imported data. Then, one could json_encode() both to strings and simply compare those, and then exit early if they are equal.

    I’m a little concerned about the extra performance/memory cost that this could have for large tables, so I’m not sure if this is something that I’d want to add for all users without a lot of testing. If you decide to try to implement this, I’d be happy to hear about your findings!

    Regards,
    Tobias

    For manual imports I think it should forcibly update the table even if it’s unchanged, so I think it really only needs to be done for the auto updates.

    One could just add a column to the table database entry called “autohash” or something that’s blank by default, whenever an auto update is performed that value is filled with a hash of the imported text. If the auto update ever matches the previous hash, it exits early. This would add a tiny computational cost, one could use a very cheap hash for this since collisions should be rare.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    hashing shouldn’t be necessary, comparing the json_encoded() table data array (a string) would be sufficient. It’s that JSON encoding that worries me a bit, but then again, TablePress does that for the data anyways, later on when saving…

    I’ll add this to my list of ideas/suggestions, but unfortunately, I can’t yet estimate on when this might be available.

    Regards,
    Tobias

    Hi, OK I’ve added this, here’s a patched version that adds a hash to not update if the table is unchanged:

    https://www.dropbox.com/s/jt7bsd13ig2ukp3/hashed-auto-update.tar.gz?dl=1

    Made a minor update to it to not show “failed” when the hashes match.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    very nice! Thanks for sharing this! Just had a quick look, and it looks very solid!

    One suggestion: I’d replace

    $new_hash = md5( serialize( $imported_table['data'] ) );

    with

    $new_hash = md5( wp_json_encode( $imported_table['data'] ) );

    (wp_json_encode() is a slightly extended version of json_encode()). That’s more robust than serialize() when it comes to UTF-8 data in tables.

    A maybe shorter version, without calculating/storing a hash (which would break if a table was edited manually, between imports), could be this (untested!):
    In the (original) file, change lines 180-185 of the _import_tablepress_table() method from

    // Load existing table from DB.
    $table = self::$model_table->load( $replace_id, false, true ); // Don't load data, but options and visibility.
    if ( is_wp_error( $table ) ) {
    	return false;
    }

    to

    // Load existing table from DB.
    $table = self::$model_table->load( $replace_id, true, true ); // Load data, options, and visibility.
    if ( is_wp_error( $table ) ) {
    	return false;
    }
    
    // Only import if the new data is different to the existing data.
    if ( wp_json_encode( $imported_table['data'] ) === wp_json_encode( $table['data'] ) ) {
    	return $replace_id; // Return the table ID to not trigger a "failed" notice.
    }

    But your version will work totally fine as well.

    Best wishes,
    Tobias

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Feature request for auto update: Don't update if table is unchanged’ is closed to new replies.