/[drupal]/contributions/modules/dbcron/dbcron.module
ViewVC logotype

Diff of /contributions/modules/dbcron/dbcron.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3.2.1, Thu Apr 13 15:45:05 2006 UTC revision 1.3.2.2, Mon Apr 17 06:38:58 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: dbcron.module,v 1.3 2006/01/01 09:21:05 jaza Exp $  // $Id: dbcron.module,v 1.4 2006/04/13 15:49:31 unconed Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 74  function dbcron_menu($may_cache) { Line 74  function dbcron_menu($may_cache) {
74    
75  /**  /**
76   * Implementation of hook_cron().   * Implementation of hook_cron().
77   *   *
78   * Runs all cron queries that are due to run when this hook is fired. If any   * Runs all cron queries that are due to run when this hook is fired. If any
79   * queries have multiple parts, the multiple parts are broken up and run   * queries have multiple parts, the multiple parts are broken up and run
80   * separately. Each query has its execution speed timed, and this speed (along   * separately. Each query has its execution speed timed, and this speed (along
81   * with the current time) is saved to the database after execution.   * with the current time) is saved to the database after execution.
82   */   */
83  function dbcron_cron() {  function dbcron_cron() {
84    static $reset;    static $reset;
85    
86    $leeway = (int)variable_get('dbcron_interval_leeway', 10);    $leeway = (int)variable_get('dbcron_interval_leeway', 10);
87    $result = db_query('SELECT * FROM {dbcron} WHERE (%d - last_run >= (run_interval - %d)) AND run_interval > 0', time(), $leeway);    $result = db_query('SELECT * FROM {dbcron} WHERE (%d - last_run >= (run_interval - %d)) AND run_interval > 0', time(), $leeway);
88    
89    if (!isset($reset)) {    if (!isset($reset)) {
90      $reset = FALSE;      $reset = FALSE;
91    }    }
92    
93    while ($dq = db_fetch_object($result)) {    while ($dq = db_fetch_object($result)) {
94      $queries = preg_split('/;(\n|\r)/', $dq->body);      $queries = preg_split('/;(\n|\r)/', $dq->body);
95    
96      timer_start('dbcron');      timer_start('dbcron');
97      foreach ($queries as $query) {      foreach ($queries as $query) {
98        $query = preg_replace('/\n|\r/', '', $query);        $query = preg_replace('/\n|\r/', '', $query);
99        if (!empty($query)) {        if (!empty($query)) {
100          db_query($query);          db_query($query);
101    
102          if (!$reset && variable_get('dbcron_search_reset', 0)) {          if (!$reset && variable_get('dbcron_search_reset', 0)) {
103            search_wipe();            search_wipe();
104            $reset = TRUE;            $reset = TRUE;
# Line 107  function dbcron_cron() { Line 107  function dbcron_cron() {
107      }      }
108      $timer = timer_stop('dbcron');      $timer = timer_stop('dbcron');
109      $speed = (int)$timer['time'];      $speed = (int)$timer['time'];
110    
111      db_query('UPDATE {dbcron} SET last_run = %d, exec_speed = %d WHERE dqid = %d', time(), $speed, $dq->dqid);      db_query('UPDATE {dbcron} SET last_run = %d, exec_speed = %d WHERE dqid = %d', time(), $speed, $dq->dqid);
112    }    }
113  }  }
# Line 118  function dbcron_cron() { Line 118  function dbcron_cron() {
118  function dbcron_settings() {  function dbcron_settings() {
119    $form = array();    $form = array();
120    $options = array(0 => t('None'), 5 => format_interval(5), 10 => format_interval(10), 15 => format_interval(15), 20 => format_interval(20), 30 => format_interval(30), 60 => format_interval(60));    $options = array(0 => t('None'), 5 => format_interval(5), 10 => format_interval(10), 15 => format_interval(15), 20 => format_interval(20), 30 => format_interval(30), 60 => format_interval(60));
121    
122    $form['dbcron_interval_leeway'] = array(    $form['dbcron_interval_leeway'] = array(
123      '#type' => 'select',      '#type' => 'select',
124      '#title' => t('Leeway in cron run intervals'),      '#title' => t('Leeway in cron run intervals'),
# Line 126  function dbcron_settings() { Line 126  function dbcron_settings() {
126      '#options' => $options,      '#options' => $options,
127      '#description' => t('The amount of leeway (in seconds) to be given to each cron query when determining (from its running interval) if it\'s time for the query to run.')      '#description' => t('The amount of leeway (in seconds) to be given to each cron query when determining (from its running interval) if it\'s time for the query to run.')
128    );    );
129    
130    $options = array(    $options = array(
131      1 => t('Enabled'),      1 => t('Enabled'),
132      0 => t('Disabled'),      0 => t('Disabled'),
# Line 138  function dbcron_settings() { Line 138  function dbcron_settings() {
138      '#options' => $options,      '#options' => $options,
139      '#description' => t('If enabled, the site\'s search index will be reset every time a cron query is executed. Useful for cron queries that clear the site\'s content.')      '#description' => t('If enabled, the site\'s search index will be reset every time a cron query is executed. Useful for cron queries that clear the site\'s content.')
140    );    );
141    
142    return $form;    return $form;
143  }  }
144    
# Line 231  function dbcron_delete($dqid = 0) { Line 231  function dbcron_delete($dqid = 0) {
231  function dbcron_save_query($dq) {  function dbcron_save_query($dq) {
232    $period = _dbcron_get_run_intervals();    $period = _dbcron_get_run_intervals();
233    $run_interval = $period[$dq->run_interval];    $run_interval = $period[$dq->run_interval];
234    
235    if ($dq->dqid) {    if ($dq->dqid) {
236      db_query("UPDATE {dbcron} SET title = '%s', body = '%s', run_interval = %d, last_run = 0 WHERE dqid = %d", $dq->title, $dq->body, $run_interval, $dq->dqid);      db_query("UPDATE {dbcron} SET title = '%s', body = '%s', run_interval = %d, last_run = 0 WHERE dqid = %d", $dq->title, $dq->body, $run_interval, $dq->dqid);
237    }    }
# Line 247  function dbcron_form($dq = '') { Line 247  function dbcron_form($dq = '') {
247    if (is_array($dq)) {    if (is_array($dq)) {
248      $dq = (object)$dq;      $dq = (object)$dq;
249    }    }
250    
251    $form['title'] = array(    $form['title'] = array(
252      '#type' => 'textfield',      '#type' => 'textfield',
253      '#title' => t('Title'),      '#title' => t('Title'),
# Line 264  function dbcron_form($dq = '') { Line 264  function dbcron_form($dq = '') {
264      '#rows' => 20,      '#rows' => 20,
265      '#required' => TRUE,      '#required' => TRUE,
266    );    );
267    
268    $period = _dbcron_get_run_intervals('form');    $period = _dbcron_get_run_intervals('form');
269    $period_vals = array_flip(_dbcron_get_run_intervals());    $period_vals = array_flip(_dbcron_get_run_intervals());
270    $form['run_interval'] = array(    $form['run_interval'] = array(
# Line 300  function dbcron_form($dq = '') { Line 300  function dbcron_form($dq = '') {
300   */   */
301  function dbcron_form_validate($form_id, $edit) {  function dbcron_form_validate($form_id, $edit) {
302    $dq = _dbcron_edit_into_object($edit);    $dq = _dbcron_edit_into_object($edit);
303    
   node_validate_title($dq);  
   
304    if (db_result(db_query("SELECT COUNT(run_interval) FROM {dbcron} WHERE dqid != %d AND title = '%s'", $dq->dqid, $dq->title))) {    if (db_result(db_query("SELECT COUNT(run_interval) FROM {dbcron} WHERE dqid != %d AND title = '%s'", $dq->dqid, $dq->title))) {
305      form_set_error('title', t('The title %title is already in use.', array('%title' => theme('placeholder', $dq->title))));      form_set_error('title', t('The title %title is already in use.', array('%title' => theme('placeholder', $dq->title))));
306    }    }
# Line 332  function dbcron_load($dqid) { Line 330  function dbcron_load($dqid) {
330   *   *
331   * @param $edit   * @param $edit
332   *   The array of values, as returned by form interfaces.   *   The array of values, as returned by form interfaces.
333   *   *
334   * @return   * @return
335   *   Equivalent values in an object.   *   Equivalent values in an object.
336   */   */
# Line 342  function _dbcron_edit_into_object($edit) Line 340  function _dbcron_edit_into_object($edit)
340    $dq->body = $edit['body'];    $dq->body = $edit['body'];
341    $dq->run_interval = $edit['run_interval'];    $dq->run_interval = $edit['run_interval'];
342    $dq->dqid = $edit['dqid'];    $dq->dqid = $edit['dqid'];
343    
344    return $dq;    return $dq;
345  }  }
346    
# Line 351  function _dbcron_edit_into_object($edit) Line 349  function _dbcron_edit_into_object($edit)
349   */   */
350  function _dbcron_get_run_intervals($type = NULL) {  function _dbcron_get_run_intervals($type = NULL) {
351    $period = array(300, 600, 900, 1800, 2700, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 432000, 604800);    $period = array(300, 600, 900, 1800, 2700, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 432000, 604800);
352    
353    if ($type == 'form') {    if ($type == 'form') {
354      $period = drupal_map_assoc($period, 'format_interval');      $period = drupal_map_assoc($period, 'format_interval');
355      return array_merge(array(0 => t('Never')), $period);      return array_merge(array(0 => t('Never')), $period);

Legend:
Removed from v.1.3.2.1  
changed lines
  Added in v.1.3.2.2

  ViewVC Help
Powered by ViewVC 1.1.2