How Auto-Publish Works in MODX

Did you ever wonder how MODX automatically publishes and unpublishes resources on a particular date? A MODX user asked about this on the Forums and I realized that I had a general idea, but no clue on the details. A little digging in the MODX core code helped me understand how it’s done. In this article, I’ll discuss the process.

MODX logo


The first thing to understand is that when no one is visiting the site, MODX is not on the job. The MODX code only executes when someone visits the site and requests a page. That’s why if you set a publication date in the future, the resource may not actually be published until several days after the specified publication date. In other words, it will be published at the time of the first visit to the site after the publication date.

Short of creating a cron job that runs every few minutes and checks publication dates, there’s really nothing you can do about this. Clients may sometimes complain if they notice the published on date doesn’t match the one they set, but the fact is, there’s really no value in having a resource published when no one is visiting the site.

A Bad Strategy

My first assumption about publication was that MODX checked the pub_date and unpub_date fields of every resource on the site on every page load. This would be horribly inefficient and would slow down page loads considerably, especially since it would be completely unnecessary on most requests. Worse yet, on some sites those fields are never set, so *every* check would be unnecessary.

A Good Strategy

I should have known that the process MODX uses is more efficient. In the MODX core/cache/ directory, there is a folder called auto_publish, and inside it, a file called auto_publish.cache.php. If you look at that file in a text editor, you’ll see that it’s *very* short. On my local machine, it looks like this:

[code language=”php”]
<?php return 0;

It’s returning 0 because there are no resources with their pub_date or unpub_date fields set. If there were, the file’s content would look something like this:

[code language=”php”]
<?php return ‘1393536872’;

The number returned from the file is a Unix timestamp. It represents a number of seconds since Jan 01 1970. In this case, it also represents the time the very next resource due for a change in publication status. On every page request, MODX checks the number in the file against the current time (the code is in the modRequest class). If the number in the file is less than the current time number, something is due to be published or unpublished. If not, there’s nothing to do and MODX goes on with handling the request.

If the number indicates that some resource is due for a change in publication status, MODX leaps into action. It calls cacheManager->refresh(), which in turn calls the autoPublish() method. That method first finds all the resources due to be published or unpublished. It resets their published status, and for resources being published, their publishedon date. It clears the pub_date or unpub_date field, whichever is appropriate. That code looks like this:

[code language=”php”]
$publishingResults[‘published’]= $this->modx->exec("UPDATE {$tblResource} SET published=1,
publishedon=pub_date, pub_date=0 WHERE pub_date IS NOT NULL AND pub_date < {$timeNow} AND pub_date > 0");

$publishingResults[‘unpublished’]= $this->modx->exec("UPDATE $tblResource SET published=0, publishedon=0,
pub_date=0, unpub_date=0 WHERE unpub_date IS NOT NULL AND unpub_date < {$timeNow} AND unpub_date > 0");

Next, the code queries the database asking for the resources with the minimum value in the pub_date and unpub_date, using two queries like this:

[code language=”php”]
$sql= "SELECT MIN(pub_date) FROM {$tblResource} WHERE published = 0 AND pub_date > ?";

$sql = "SELECT MIN(unpub_date) FROM {$tblResource} WHERE published = 1 AND unpub_date > ?"

The lower of the two date numbers goes into the auto_publish.cache.php file as a timestamp. It’s the next time any resource is due to change publication status. This check is done *after* the modifications listed above so it always represents the very next resource due to have a publication status change in the future.

How Does the Number Get Set in the First Place

We’ve just seen how the number in the auto_publish.cache.php file number gets updated when a resource is due for a publication status change, but how does the number initially go from 0 to a real date? As you might guess, it happens when a resource is saved with the pub_date or unpub_date field set. If either one is set, MODX checks its time set against the time in the auto_publish.cache.php file. If it’s lower, or if the time in the file is 0, the resource’s publish or unpublish date (whichever is lower) is converted to a Unix timestamp, and written to the auto_publish.cache.php file.

For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *