12

I've tagged this post as WordPress, but I'm not entirely sure it's WordPress-specific, so I'm posting it on StackOverflow rather than WPSE. The solution doesn't have to be WordPress-specific, simply PHP.

The Scenario
I run a fishkeeping website with a number of tropical fish Species Profiles and Glossary entries.

Our website is oriented around our profiles. They are, as you may term it, the bread and butter of the website.

What I'm hoping to achieve is that, in every species profile which mentions another species or a glossary entry, I can replace those words with a link - such as you'll see here. Ideally, I would also like this to occur in news, articles and blog posts too.

We have nearly 1400 species profiles and 1700 glossary entries. Our species profiles are often lengthy and at last count our species profiles alone numbered more than 1.7 million words of information.

What I'm Currently Attempting
Currently, I have a filter.php with a function that - I believe - does what I need it to do. The code is quite lengthy, and can be found in full here.

In addition, in my WordPress theme's functions.php, I have the following:

# ==============================================================================================
# [Filter]
#
# Every hour, using WP_Cron, `my_updated_posts` is checked. If there are new Post IDs in there,
# it will run a filter on all of the post's content. The filter will search for Glossary terms
# and scientific species names. If found, it will replace those names with links including a 
# pop-up.

    include "filter.php";

# ==============================================================================================
# When saving a post (new or edited), check to make sure it isn't a revision then add its ID
# to `my_updated_posts`.

    add_action( 'save_post', 'my_set_content_filter' );
    function my_set_content_filter( $post_id ) {
        if ( !wp_is_post_revision( $post_id ) ) {

            $post_type = get_post_type( $post_id );

            if ( $post_type == "species" || ( $post_type == "post" && in_category( "articles", $post_id ) ) || ( $post_type == "post" && in_category( "blogs", $post_id ) ) ) {
                //get the previous value
                $ids = get_option( 'my_updated_posts' );

                //add new value if necessary
                if( !in_array( $post_id, $ids ) ) {
                    $ids[] = $post_id;
                    update_option( 'my_updated_posts', $ids );
                }
            }
        }
    }

# ==============================================================================================
# Add the filter to WP_Cron.

    add_action( 'my_filter_posts_content', 'my_filter_content' );
    if( !wp_next_scheduled( 'my_filter_posts_content' ) ) {
        wp_schedule_event( time(), 'hourly', 'my_filter_posts_content' );
    }

# ==============================================================================================
# Run the filter.

    function my_filter_content() {
        //check to see if posts need to be parsed
        if ( !get_option( 'my_updated_posts' ) )
            return false;

        //parse posts
        $ids = get_option( 'my_updated_posts' );

        update_option( 'error_check', $ids );

        foreach( $ids as $v ) {
            if ( get_post_status( $v ) == 'publish' )
                run_filter( $v );

            update_option( 'error_check', "filter has run at least once" );
        }

        //make sure no values have been added while loop was running
        $id_recheck = get_option( 'my_updated_posts' );
        my_close_out_filter( $ids, $id_recheck );

        //once all options, including any added during the running of what could be a long cronjob are done, remove the value and close out
        delete_option( 'my_updated_posts' );
        update_option( 'error_check', 'working m8' );
        return true;
    }

# ==============================================================================================
# A "difference" function to make sure no new posts have been added to `my_updated_posts` whilst
# the potentially time-consuming filter was running.

    function my_close_out_filter( $beginning_array, $end_array ) {
        $diff = array_diff( $beginning_array, $end_array );
        if( !empty ( $diff ) ) {
            foreach( $diff as $v ) {
                run_filter( $v );
            }
        }
        my_close_out_filter( $end_array, get_option( 'my_updated_posts' ) );
    }

The way this works, as (hopefully) described by the code's comments, is that each hour WordPress operates a cron job (which is like a false cron - works upon user hits, but that doesn't really matter as the timing isn't important) which runs the filter found above.

The rationale behind running it on an hourly basis was that if we tried to run it when each post was saved, it would be to the detriment of the author. Once we get guest authors involved, that is obviously not an acceptable way of going about it.

The Problem...
For months now I've been having problems getting this filter running reliably. I don't believe that the problem lies with the filter itself, but with one of the functions that enables the filter - i.e. the cron job, or the function that chooses which posts are filtered, or the function which prepares the wordlists etc. for the filter.

Unfortunately, diagnosing the problem is quite difficult (that I can see), thanks to it running in the background and only on an hourly basis. I've been trying to use WordPress' update_option function (which basically writes a simple database value) to error-check, but I haven't had much luck - and to be honest, I'm quite confused as to where the problem lies.

We ended up putting the website live without this filter working correctly. Sometimes it seems to work, sometimes it doesn't. As a result, we now have quite a few species profiles which aren't correctly filtered.

What I'd Like...
I'm basically seeking advice on the best way to go about running this filter.

Is a Cron Job the answer? I can set up a .php file which runs every day, that wouldn't be a problem. How would it determine which posts need to be filtered? What impact would it have on the server at the time it ran?

Alternatively, is a WordPress admin page the answer? If I knew how to do it, something along the lines of a page - utilising AJAX - which allowed me to select the posts to run the filter on would be perfect. There's a plugin called AJAX Regenerate Thumbnails which works like this, maybe that would be the most effective?

Considerations

  • The size of the database/information being affected/read/written
  • Which posts are filtered
  • The impact the filter has on the server; especially considering I don't seem to be able to increase the WordPress memory limit past 32Mb.
  • Is the actual filter itself efficient, effective and reliable?

This is quite a complex question and I've inevitably (as I was distracted roughly 18 times by colleagues in the process) left out some details. Please feel free to probe me for further information.

Thanks in advance,

5
  • Do you have access to your SQL database from offsite? If you're concerned about a cron running on your server, you could do the initial script run that processed your 1.5 million words from a computer running a PHP CLI script that is connected to your db. Jun 18, 2012 at 11:40
  • Slightly unrelated: While poking around your site I noticed that your url structure is nicely rewritten. However, I can't help but notice that the classification links are all in the form /classification/%s despite being for an order or family. Is this intentional? It seems like /classification/family/%s and /classification/order/%s would be less ambiguous. Jun 18, 2012 at 11:55
  • Thanks for the heads-up there PhpMyCoder, I'll add it to my (ever-growing!) list :) I don't currently allow remote connections to our MySQL database but I could temporarily allow it then run the script from my wamp installation. Will it have much of an impact on the website if the database is being written to constantly for a period of time?
    – turbonerd
    Jun 18, 2012 at 12:17
  • 1
    It might, although it will be less of an effect than if you did all of the processing onsite. The only real hit that your server might take would be the UPDATE queries on the DB. If you're running innodb on your posts table (SHOW TABLE STATUS WHERE Name = 'table' to check), the hit won't be felt at all because it uses row-level locking. However if you're using MyISAM, considering running the CLI script during a low traffic time. Jun 18, 2012 at 12:25
  • I only gave the one answer and your code a cursory glance, so forgive me if this has already been implemented or suggested, but are you using a stoplist of words to not check against the DB like "the", "a", etc.? Jun 18, 2012 at 12:27

1 Answer 1

5

Do it when the profile is created.

Try reversing the whole process. Rather than checking the content for the words, check the words for the content's words.

  1. Break the content post on entry into words (on space)
  2. Eliminate duplicates, ones under the smallest size of a word in the database, ones over the largest size, and ones in a 'common words' list that you keep.
  3. Check against each table, if some of your tables include phrases with spaces, do a %text% search, otherwise do a straight match (much faster) or even build a hash table if it really is that big a problem. (I would do this as a PHP array and cache the result somehow, no sense reinventing the wheel)
  4. Create your links with the now dramatically smaller lists.

You should be able to easily keep this under 1 second even as you move out to even 100,000 words you are checking against. I've done exactly this, without caching the word lists, for a Bayesian Filter before.

With the smaller list, even if it is greedy and gathers words that don't match "clown" will catch "clown loach", the resulting smaller list should be only a few to a few dozen words with links. Which will take no time at all to do a find and replace over a chunk of text.

The above doesn't really address your concern over the older profiles. You don't say exactly how many there are, just that there is a lot of text and that it is on 1400 to 3100 (both items) put together. This older content you could do based on popularity if you have the info. Or on date entered, newest first. Regardless the best way to do this is to write a script that suspends the time limit on PHP and just batch-runs a load/process/save on all the posts. If each one takes about 1 second (probably much less but worst case) you are talking 3100 seconds which is a little less than an hour.

11
  • Great post, +1. I've been attempting to implement your solution this morning but encountered a problem which I suspect might slow things down. Some of the terms that I'll be searching for include P. denisonii and S. daemon - abbreviated species names. As such, explode by " " won't work. Can you recommend an alternative? I can't imagine that searching each field (i.e. distribution, habitat) for all of the search terms will be efficient?
    – turbonerd
    Jun 16, 2012 at 15:28
  • If there isn't an abbreviated form in the database, I don't know that you would be able to detect abbreviated versions. Namely, if in the database you have "Clown Loach" but in the text you have "C. Loach", I don't see any way to deal with that efficiently unless you have a field in the database (for the species) for short name versions. But if you are worried because "C." seems problematic, remember that you are tossing out words under a length threshold, and "%Loach%" in a LIKE search will match "Clown Loach" which could have "C. Loach" in the short field, thus you'll get your match.
    – DampeS8N
    Jun 17, 2012 at 2:44
  • Hmm, wondering then if I'll have to go back to the drawing board. Species of fish all take one of two formats: Satanoperca daemon or S. daemon, which - if I'm interpreting you correctly - is going to be difficult/impossible to run your idea efficiently?
    – turbonerd
    Jun 18, 2012 at 8:31
  • Not true @dunc, all you need to do is include an additional field on your species table with the shorter form within. "daemon", since this is broken on space, will load all the fish with daemon in the name. Then when you do the str_replace() do it on both the long and short names. You want the query to return at least the needed fishes, but not the whole fish table. Breaking on space and doing a %daemon% LIKE full text search will return the needed item.
    – DampeS8N
    Jun 18, 2012 at 11:23
  • Ahh, of course, I think I'm with you now. Shall attempt it - thanks again.
    – turbonerd
    Jun 18, 2012 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.