Adding a task to SPIP cron Timed executions from your plugins

The cron in SPIP doesn’t rely on the unix cron table (as it might not be accessible for the user), but on it’s own implementation.
What SPIP does, is insert a small image in each web page (or only the only with the #SPIP_CRON tag), the loading of the image will call a php page running the crons and returning a transparent pixel size image.

This way, the page load is not slowed down as modern browser usually load images in the background. And the cron tasks can be spread amongst multiple visits.

Inserting your own task into the SPIP cron task list is not that difficult, but you will have to create a plugin to do that.
This is not really documented anywhere, not even in French for what I know...

In latest versions of SPIP , plugins can integrate with SPIP throught "pipelines". The system is simple :

  • the plugin says it is interested in a particular pipeline
  • at the execution of the php containing the pipeline, SPIP will call all plugins that declared themselves and expect a particular result from them.

There is a pipeline (1.9.2 has it, the oldest, I am not sure) for inserting a new task into the crons. It’s called taches_generales_cron.

So you will need to create a plugin to register with that pipeline, to create the plugin, take this template

Edit the plugin.xml to specify the data about your plugin and remove all the <pipeline> tags except the one for taches_generales_cron.

You won’t need the <bouton>, <fonctions> and <options> part I think.

The <prefix> tag specifies a unique ID for your plugin that will be the prefix of all your files and functions to avoid clashes. Specify it to something you want.

Now, you will want to copy the template_pipelines_cron.php and keep the function template_taches_generales_cron($flux)
(replace ’template’ by your prefix)

This is the pipeline callback that will insert your new function into the cron, you have to modify the $flux to insert your task by doing something like :

function yourprefix_taches_generales_cron($flux)
  $flux['yourprefix_cron'] = 3600*24;
return $flux;

Flux is an array of handles to cron functions (the one executing your task) pointing to delays in seconds, here we say the cron has to be executed every 24 hours.

Now, for the implementation of the callback handle.
When executing crons, SPIP will look for a file to include with the name yourprefix_cron.php, it should be in SPIP include path, the right way to do that in your plugin is to create a file inc/yourprefix_cron.php under your plugin directory.

After loading that file, SPIP will call the function cron_yourprefix_cron($t). You should implement this in your file and this should execute your task.

I am not sure of what the $t parameter stands for, I thinks it’s the time of the last execution of your task.
Your function should also return a int code, but I am not sure what it’s used for, I found that returning 1 is sufficient if your task is not too long.
For what I understand of the code (ecrire/inc/cron.php), if this is negative, then it means that the task is not finished yet and should be called back for the current time frame (for example, if you have long computations to do, multiple feeds to process, etc.).

You can see some examples in the files :

  • ecrire/inc/syndic.php
  • ecrire/inc/indexation.php
  • ecrire/inc/rubriques.php
  • ... (look for the cron_ functions in ecrire/inc).

, par mortimer