Changeset 2333 for trunk


Ignore:
Timestamp:
May 6, 2008, 3:04:58 AM (17 years ago)
Author:
rvelices
Message:
  • remove some unnecessary db columns (#history_summary.id, #history.year, month, day and hour)
Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r2325 r2333  
    278278    $where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')';
    279279  }
    280  
     280
    281281  // find all categories where the setted representative is not possible :
    282282  // the picture does not exist
     
    395395      {
    396396        $query = '
    397   INSERT INTO '.$table_name.'
    398     ('.implode(',', $dbfields).')
    399      VALUES';
     397INSERT INTO '.$table_name.'
     398  ('.implode(',', $dbfields).')
     399  VALUES';
    400400        $first = false;
    401401      }
     
    403403      {
    404404        $query .= '
    405     , ';
     405  , ';
    406406      }
    407407
     
    453453      {
    454454        $query = '
    455   UPDATE '.$tablename.'
    456     SET ';
     455UPDATE '.$tablename.'
     456  SET ';
    457457        $is_first = true;
    458458        foreach ($dbfields['update'] as $key)
     
    474474        }
    475475        $query.= '
    476     WHERE ';
     476  WHERE ';
    477477
    478478        $is_first = true;
     
    483483            $query.= ' AND ';
    484484          }
    485           $query.= $key.' = \''.$data[$key].'\'';
     485          if ( isset($data[$key]) )
     486          {
     487            $query.= $key.' = \''.$data[$key].'\'';
     488          }
     489          else
     490          {
     491            $query.= $key.' IS NULL';
     492          }
    486493          $is_first = false;
    487494        }
    488495        $query.= '
    489   ;';
     496;';
    490497        pwg_query($query);
    491498      }
     
    495502      // creation of the temporary table
    496503      $query = '
    497   SHOW FULL COLUMNS FROM '.$tablename.'
     504SHOW FULL COLUMNS FROM '.$tablename.'
    498505;';
    499506      $result = pwg_query($query);
     
    506513          $column = $row['Field'];
    507514          $column.= ' '.$row['Type'];
    508           if (!isset($row['Null']) or $row['Null'] == '')
     515
     516          $nullable = true;
     517          if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO')
    509518          {
    510519            $column.= ' NOT NULL';
     520            $nullable = false;
    511521          }
    512522          if (isset($row['Default']))
    513523          {
    514524            $column.= " default '".$row['Default']."'";
     525          }
     526          elseif ($nullable)
     527          {
     528            $column.= " default NULL";
    515529          }
    516530          if (isset($row['Collation']) and $row['Collation'] != 'NULL')
     
    528542  (
    529543  '.implode(",\n", $columns).',
    530   PRIMARY KEY ('.implode(',', $dbfields['primary']).')
     544  UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).')
    531545  )
    532546;';
     547
    533548      pwg_query($query);
    534549      mass_inserts($temporary_tablename, $all_fields, $datas);
    535550      // update of images table by joining with temporary table
    536551      $query = '
    537   UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2
    538     SET '.
     552UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2
     553  SET '.
    539554        implode(
    540555          "\n    , ",
     
    544559            )
    545560          ).'
    546     WHERE '.
     561  WHERE '.
    547562        implode(
    548563          "\n    AND ",
     
    555570      pwg_query($query);
    556571      $query = '
    557   DROP TABLE '.$temporary_tablename.'
     572DROP TABLE '.$temporary_tablename.'
    558573;';
    559574      pwg_query($query);
  • trunk/admin/stats.php

    r2299 r2333  
    116116$query = '
    117117SELECT
    118     year,
    119     month,
    120     day,
    121     hour,
    122     max(id) AS max_id,
     118    date,
     119    HOUR(time) AS hour,
     120    MAX(id) AS max_id,
    123121    COUNT(*) AS nb_pages
    124122  FROM '.HISTORY_TABLE.'
    125123  WHERE summarized = \'false\'
    126124  GROUP BY
    127     year ASC,
    128     month ASC,
    129     day ASC,
    130     hour ASC
     125    date ASC,
     126    HOUR(time) ASC
    131127;';
    132128$result = pwg_query($query);
     
    141137{
    142138  $time_keys = array(
     139    substr($row['date'], 0, 4), //yyyy
     140    substr($row['date'], 0, 7), //yyyy-mm
     141    substr($row['date'], 0, 10),//yyyy-mm-dd
    143142    sprintf(
    144       '%4u',
    145       $row['year']
    146       ),
    147     sprintf(
    148       '%4u.%02u',
    149       $row['year'], $row['month']
    150       ),
    151     sprintf(
    152       '%4u.%02u.%02u',
    153       $row['year'], $row['month'], $row['day']
    154       ),
    155     sprintf(
    156       '%4u.%02u.%02u.%02u',
    157       $row['year'], $row['month'], $row['day'], $row['hour']
     143      '%s-%02u',
     144      $row['date'], $row['hour']
    158145      ),
    159146    );
     
    190177// +---------------+----------+
    191178// | 2005          |   241109 |
    192 // | 2005.08       |    20133 |
    193 // | 2005.08.25    |      620 |
    194 // | 2005.08.25.21 |      151 |
     179// | 2005-08       |    20133 |
     180// | 2005-08-25    |      620 |
     181// | 2005-08-25-21 |      151 |
    195182// +---------------+----------+
    196183
    197 $existing_time_keys = array();
    198 
    199 if (isset($first_time_key))
    200 {
    201   list($year, $month, $day, $hour) = explode('.', $first_time_key);
    202 
    203   $time_keys = array(
    204     sprintf('%4u',                $year),
    205     sprintf('%4u.%02u',           $year, $month),
    206     sprintf('%4u.%02u.%02u',      $year, $month, $day),
    207     sprintf('%4u.%02u.%02u.%02u', $year, $month, $day, $hour),
    208     );
    209 
    210   $query = '
    211 SELECT
    212     id,
    213     nb_pages
    214   FROM '.HISTORY_SUMMARY_TABLE.'
    215   WHERE id IN (\''.implode("', '", $time_keys).'\')
    216 ;';
    217   $result = pwg_query($query);
    218   while ($row = mysql_fetch_array($result))
    219   {
    220     $existing_time_keys[ $row['id'] ] = $row['nb_pages'];
    221   }
    222 }
    223184
    224185$updates = array();
    225186$inserts = array();
    226187
    227 foreach (array_keys($need_update) as $time_key)
    228 {
    229   $time_tokens = explode('.', $time_key);
    230 
    231   if (isset($existing_time_keys[$time_key]))
    232   {
    233     array_push(
    234       $updates,
    235       array(
    236         'id'       => $time_key,
    237         'nb_pages' => $existing_time_keys[$time_key] + $need_update[$time_key],
     188if (isset($first_time_key))
     189{
     190  list($year, $month, $day, $hour) = explode('-', $first_time_key);
     191
     192  $query = '
     193SELECT *
     194  FROM '.HISTORY_SUMMARY_TABLE.'
     195  WHERE year='.$year.'
     196    AND ( month IS NULL
     197      OR ( month='.$month.'
     198        AND ( day is NULL
     199          OR (day='.$day.'
     200            AND (hour IS NULL OR hour='.$hour.')
     201          )
    238202        )
    239       );
    240   }
    241   else
    242   {
    243     array_push(
     203      )
     204    )
     205;';
     206  $result = pwg_query($query);
     207  while ($row = mysql_fetch_assoc($result))
     208  {
     209    $key = sprintf('%4u', $row['year']);
     210    if ( isset($row['month']) )
     211    {
     212      $key .= sprintf('-%02u', $row['month']);
     213      if ( isset($row['day']) )
     214      {
     215        $key .= sprintf('-%02u', $row['day']);
     216        if ( isset($row['hour']) )
     217        {
     218          $key .= sprintf('-%02u', $row['hour']);
     219        }
     220      }
     221    }
     222
     223    if (isset($need_update[$key]))
     224    {
     225      $row['nb_pages'] += $need_update[$key];
     226      array_push($updates, $row);
     227      unset($need_update[$key]);
     228    }
     229  }
     230}
     231
     232foreach ($need_update as $time_key => $nb_pages)
     233{
     234  $time_tokens = explode('-', $time_key);
     235
     236  array_push(
    244237      $inserts,
    245238      array(
    246         'id'       => $time_key,
    247239        'year'     => $time_tokens[0],
    248240        'month'    => @$time_tokens[1],
    249241        'day'      => @$time_tokens[2],
    250242        'hour'     => @$time_tokens[3],
    251         'nb_pages' => $need_update[$time_key],
     243        'nb_pages' => $nb_pages,
    252244        )
    253245      );
    254   }
    255246}
    256247
     
    260251    HISTORY_SUMMARY_TABLE,
    261252    array(
    262       'primary' => array('id'),
     253      'primary' => array('year','month','day','hour'),
    263254      'update'  => array('nb_pages'),
    264255      ),
  • trunk/include/functions.inc.php

    r2313 r2333  
    570570    date,
    571571    time,
    572     year,
    573     month,
    574     day,
    575     hour,
    576572    user_id,
    577573    IP,
     
    586582    CURDATE(),
    587583    CURTIME(),
    588     YEAR( CURDATE() ),
    589     MONTH( CURDATE() ),
    590     DAYOFMONTH( CURDATE() ),
    591     HOUR( CURTIME() ),
    592584    '.$user['id'].',
    593585    \''.$_SERVER['REMOTE_ADDR'].'\',
  • trunk/install/phpwebgallery_structure.sql

    r2324 r2333  
    117117  `date` date NOT NULL default '0000-00-00',
    118118  `time` time NOT NULL default '00:00:00',
    119   `year` smallint(4) NOT NULL default '0',
    120   `month` tinyint(2) NOT NULL default '0',
    121   `day` tinyint(2) NOT NULL default '0',
    122   `hour` tinyint(2) NOT NULL default '0',
    123119  `user_id` smallint(5) NOT NULL default '0',
    124120  `IP` varchar(15) NOT NULL default '',
     
    139135DROP TABLE IF EXISTS `phpwebgallery_history_summary`;
    140136CREATE TABLE `phpwebgallery_history_summary` (
    141   `id` varchar(13) NOT NULL default '',
    142137  `year` smallint(4) NOT NULL default '0',
    143138  `month` tinyint(2) default NULL,
     
    145140  `hour` tinyint(2) default NULL,
    146141  `nb_pages` int(11) default NULL,
    147   PRIMARY KEY  (`id`)
     142  UNIQUE KEY history_summary_ymdh (`year`,`month`,`day`,`hour`)
    148143) TYPE=MyISAM;
    149144
Note: See TracChangeset for help on using the changeset viewer.