b2evolution PHP Cross Reference Blogging Systems

Source: /inc/sessions/model/_hitlist.class.php - 189 lines - 6531 bytes - Summary - Text - Print

Description: This file implements the Hitlist class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the Hitlist class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * Daniel HAHLER grants Francois PLANQUE the right to license
  22   * Daniel HAHLER's contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   * @package evocore
  27   *
  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29   * @author blueyed: Daniel HAHLER.
  30   * @author fplanque: Francois PLANQUE.
  31   *
  32   * @version $Id: _hitlist.class.php 6136 2014-03-08 07:59:48Z manuel $
  33   *
  34   */
  35  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  36  
  37  
  38  /**
  39   * A list of hits. Provides functions for maintaining and extraction of Hits.
  40   *
  41   * @package evocore
  42   */
  43  class Hitlist
  44  {
  45  
  46  
  47      /**
  48       * Delete a hit.
  49       *
  50       * @static
  51       * @param int ID to delete
  52       * @return mixed Return value of {@link DB::query()}
  53       */
  54  	function delete( $hit_ID )
  55      {
  56          global $DB;
  57  
  58          return $DB->query( "DELETE FROM T_hitlog WHERE hit_ID = $hit_ID", 'Delete a hit' );
  59      }
  60  
  61  
  62      /**
  63       * Delete all hits for a specific date
  64       *
  65       * @static
  66       * @param int unix timestamp to delete hits for
  67       * @return mixed Return value of {@link DB::query()}
  68       */
  69  	function prune( $date )
  70      {
  71          global $DB;
  72  
  73          $iso_date = date ('Y-m-d', $date);
  74          $sql = "
  75              DELETE FROM T_hitlog
  76               WHERE DATE_FORMAT(hit_datetime,'%Y-%m-%d') = '$iso_date'";
  77  
  78          return $DB->query( $sql, 'Prune hits for a specific date' );
  79      }
  80  
  81  
  82      /**
  83       * Change type for a hit
  84       *
  85       * @static
  86       * @param int ID to change
  87       * @param string new type, must be valid ENUM for hit_referer_type field
  88       * @return mixed Return value of {@link DB::query()}
  89       */
  90  	function change_type( $hit_ID, $type )
  91      {
  92          global $DB;
  93  
  94          $sql = '
  95                  UPDATE T_hitlog
  96                     SET hit_referer_type = '.$DB->quote($type).",
  97                         hit_datetime = hit_datetime " /* prevent mySQL from updating timestamp */ ."
  98                   WHERE hit_ID = $hit_ID";
  99          return $DB->query( $sql, 'Change type for a specific hit' );
 100      }
 101  
 102  
 103      /**
 104       * Auto pruning of old stats.
 105       *
 106       * It uses a general setting to store the day of the last prune, avoiding multiple prunes per day.
 107       * fplanque>> Check: How much faster is this than DELETING right away with an INDEX on the date field?
 108       *
 109       * Note: we're using {@link $localtimenow} to log hits, so use this for pruning, too.
 110       *
 111       * NOTE: do not call this directly, but only in conjuction with auto_prune_stats_mode.
 112       *
 113       * @static
 114       * @return string Empty, if ok.
 115       */
 116  	function dbprune()
 117      {
 118          /**
 119           * @var DB
 120           */
 121          global $DB;
 122          global $Debuglog, $Settings, $localtimenow;
 123          global $Plugins, $Messages;
 124  
 125          // Prune when $localtime is a NEW day (which will be the 1st request after midnight):
 126          $last_prune = $Settings->get( 'auto_prune_stats_done' );
 127          if( $last_prune >= date('Y-m-d', $localtimenow) && $last_prune <= date('Y-m-d', $localtimenow+86400) )
 128          { // Already pruned today (and not more than one day in the future -- which typically never happens)
 129              $message = T_('Pruning has already been done today');
 130              $Messages->add( $message, 'error' );
 131              return $message;
 132          }
 133  
 134          $time_prune_before = ($localtimenow - ($Settings->get('auto_prune_stats') * 86400)); // 1 day = 86400 seconds
 135  
 136          $rows_affected = $DB->query( "
 137              DELETE FROM T_hitlog
 138              WHERE hit_datetime < '".date('Y-m-d', $time_prune_before)."'", 'Autopruning hit log' );
 139          $Debuglog->add( 'Hitlist::dbprune(): autopruned '.$rows_affected.' rows from T_hitlog.', 'request' );
 140  
 141  /*        // Prune for internal searches
 142          $rows_affected = $DB->query( "
 143              DELETE FROM T_logs__internal_searches 
 144              WHERE isrch_hit_ID not in 
 145                  (
 146                      SELECT hit_ID from T_hitlog
 147                  )" );
 148          $Debuglog->add( 'Hitlist::dbprune(): autopruned '.$rows_affected.' rows from T_logs__internal_searches.', 'request' );
 149  
 150          // Optimizing tables
 151          // fp> TODO see if we want to do this here
 152          $DB->query('OPTIMIZE TABLE T_logs__internal_searches');
 153  */
 154          
 155          // Prune sessions that have timed out and are older than auto_prune_stats
 156          $sess_prune_before = ($localtimenow - $Settings->get( 'timeout_sessions' ));
 157          $smaller_time = min( $sess_prune_before, $time_prune_before );
 158          // allow plugins to prune session based data
 159          $Plugins->trigger_event( 'BeforeSessionsDelete', $temp_array = array( 'cutoff_timestamp' => $smaller_time ) );
 160  
 161          $rows_affected = $DB->query( 'DELETE FROM T_sessions WHERE sess_lastseen_ts < '.$DB->quote(date('Y-m-d H:i:s', $smaller_time)), 'Autoprune sessions' );
 162          $Debuglog->add( 'Hitlist::dbprune(): autopruned '.$rows_affected.' rows from T_sessions.', 'request' );
 163  
 164          // Prune non-referrered basedomains (where the according hits got deleted)
 165          // BUT only those with unknown dom_type/dom_status, because otherwise this
 166          //     info is useful when we get hit again.
 167          // Note: MySQL server version >= 4 is required for multi-table deletes, but v 4.1 is now a requirement for b2evolution:
 168          $rows_affected = $DB->query( "
 169              DELETE T_basedomains
 170                FROM T_basedomains LEFT JOIN T_hitlog ON hit_referer_dom_ID = dom_ID
 171               WHERE hit_referer_dom_ID IS NULL
 172               AND dom_type = 'unknown'
 173               AND dom_status = 'unknown'" );
 174          $Debuglog->add( 'Hitlist::dbprune(): autopruned '.$rows_affected.' rows from T_basedomains.', 'request' );
 175  
 176          // Optimizing tables
 177          $DB->query('OPTIMIZE TABLE T_hitlog');
 178          $DB->query('OPTIMIZE TABLE T_sessions');
 179          $DB->query('OPTIMIZE TABLE T_basedomains');
 180  
 181          $Settings->set( 'auto_prune_stats_done', date('Y-m-d H:i:s', $localtimenow) ); // save exact datetime
 182          $Settings->dbupdate();
 183  
 184          $Messages->add( T_('The old hits & sessions have been pruned.'), 'success' );
 185          return ''; /* ok */
 186      }
 187  }
 188  
 189  ?>

title

Description

title

Description

title

Description

title

title

Body