Ignore:
Timestamp:
Feb 11, 2014, 10:47:44 PM (10 years ago)
Author:
rvelices
Message:

arrayfromquery optimizations: move double if from inside loop to outside + use directly mysqli calls to avoid function call overhead for every row retrieved from db

File:
1 edited

Legend:

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

    r27158 r27336  
    1 <?php 
     1<?php
    22// +-----------------------------------------------------------------------+
    33// | Piwigo - a PHP based photo gallery                                    |
     
    482482    return $date1->diff($date2);
    483483  }
    484  
     484
    485485  $diff = new stdClass();
    486  
     486
    487487  //Make sure $date1 is ealier
    488488  $diff->invert = $date2 < $date1;
     
    491491    list($date1, $date2) = array($date2, $date1);
    492492  }
    493  
     493
    494494  //Calculate R values
    495495  $R = ($date1 <= $date2 ? '+' : '-');
     
    525525    $date1->modify('-1 '.$period);
    526526  }
    527  
     527
    528528  list($diff->y, $diff->m, $diff->d, $diff->h) = array_values($periods);
    529529
     
    532532  $diff->i = floor($diff->s/60);
    533533  $diff->s = $diff->s - $diff->i*60;
    534  
     534
    535535  return $diff;
    536536}
     
    570570        $tok = strtok('- :/');
    571571      }
    572      
     572
    573573      if (count($ymdhms)<3) return false;
    574574      if (!isset($ymdhms[3])) $ymdhms[3] = 0;
    575575      if (!isset($ymdhms[4])) $ymdhms[4] = 0;
    576576      if (!isset($ymdhms[5])) $ymdhms[5] = 0;
    577      
     577
    578578      $date = new DateTime();
    579579      $date->setDate($ymdhms[0], $ymdhms[1], $ymdhms[2]);
     
    596596{
    597597  global $lang;
    598  
     598
    599599  $date = str2DateTime($original, $format);
    600600
     
    609609    $print.= $lang['day'][ $date->format('w') ].' ';
    610610  }
    611  
     611
    612612  $print.= $date->format('j');
    613613  $print.= ' '.$lang['month'][ $date->format('n') ];
    614614  $print.= ' '.$date->format('Y');
    615  
     615
    616616  if ($show_time)
    617617  {
     
    644644    return l10n('N/A');
    645645  }
    646  
     646
    647647  $now = new DateTime();
    648648  $diff = dateDiff($now, $date);
    649  
     649
    650650  $chunks = array(
    651651    'year' => $diff->y,
     
    657657    'second' => $diff->s,
    658658  );
    659  
     659
    660660  // DateInterval does not contain the number of weeks
    661661  if ($with_week)
     
    664664    $chunks['day'] = $chunks['day'] - $chunks['week']*7;
    665665  }
    666  
     666
    667667  $j = array_search($stop, array_keys($chunks));
    668  
     668
    669669  $print = ''; $i=0;
    670670  foreach ($chunks as $name => $value)
     
    680680    $i++;
    681681  }
    682  
     682
    683683  $print = trim($print);
    684  
     684
    685685  if ($with_text)
    686686  {
     
    934934  WHERE user_id = '.$user['id'].'
    935935;';
    936   $in_caddie = array_from_query($query, 'element_id');
     936  $in_caddie = query2array($query, null, 'element_id');
    937937
    938938  $caddiables = array_diff($elements_id, $in_caddie);
     
    10431043 * returns a string formated with l10n elements.
    10441044 * it is usefull to "prepare" a text and translate it later
    1045  * @see get_l10n_args() 
     1045 * @see get_l10n_args()
    10461046 *
    10471047 * @param array $key_args one l10n_args element or array of l10n_args elements
     
    11531153    $conf[ $row['param'] ] = $val;
    11541154  }
    1155  
     1155
    11561156  trigger_action('load_conf', $condition);
    11571157}
     
    11661166{
    11671167  $query = '
    1168 SELECT
    1169     param,
    1170     value
     1168SELECT param
    11711169  FROM '.CONFIG_TABLE.'
    11721170  WHERE param = \''.$param.'\'
    11731171;';
    1174   $params = array_from_query($query, 'param');
     1172  $params = query2array($query, null, 'param');
    11751173
    11761174  if (count($params) == 0)
     
    12041202{
    12051203  global $conf;
    1206  
     1204
    12071205  if (!is_array($params))
    12081206  {
     
    12131211    return;
    12141212  }
    1215  
     1213
    12161214  $query = '
    12171215DELETE FROM '.CONFIG_TABLE.'
     
    12191217;';
    12201218  pwg_query($query);
    1221  
     1219
    12221220  foreach ($params as $param)
    12231221  {
     
    12451243
    12461244/**
    1247  * Builds an data array from a SQL query.
    1248  * Depending on $key_name and $value_name it can return :
    1249  *
    1250  *    - an array of arrays of all fields (key=null, value=null)
    1251  *        array(
    1252  *          array('id'=>1, 'name'=>'DSC8956', ...),
    1253  *          array('id'=>2, 'name'=>'DSC8957', ...),
    1254  *          ...
    1255  *          )
    1256  *
    1257  *    - an array of a single field (key=null, value='...')
    1258  *        array('DSC8956', 'DSC8957', ...)
    1259  *
    1260  *    - an associative array of array of all fields (key='...', value=null)
    1261  *        array(
    1262  *          'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
    1263  *          'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
    1264  *          ...
    1265  *          )
    1266  *
    1267  *    - an associative array of a single field (key='...', value='...')
    1268  *        array(
    1269  *          'DSC8956' => 1,
    1270  *          'DSC8957' => 2,
    1271  *          ...
    1272  *          )
    1273  *
    1274  * @since 2.6
    1275  *
    1276  * @param string $query
    1277  * @param string $key_name
    1278  * @param string $value_name
    1279  * @return array
    1280  */
    1281 function query2array($query, $key_name=null, $value_name=null)
    1282 {
    1283   $result = pwg_query($query);
    1284   $data = array();
    1285 
    1286   while ($row = pwg_db_fetch_assoc($result))
    1287   {
    1288     if (isset($value_name))
    1289     {
    1290       $value = $row[ $value_name ];
    1291     }
    1292     else
    1293     {
    1294       $value = $row;
    1295     }
    1296     if (isset($key_name))
    1297     {
    1298       $data[ $row[$key_name] ] = $value;
    1299     }
    1300     else
    1301     {
    1302       $data[] = $value;
    1303     }
    1304   }
    1305 
    1306   return $data;
    1307 }
    1308 
    1309 /**
    13101245 * creates an simple hashmap based on a SQL query.
    13111246 * choose one to be the key, another one to be the value.
     
    13691304    while ($row = pwg_db_fetch_assoc($result))
    13701305    {
    1371       $array[] = $row;     
     1306      $array[] = $row;
    13721307    }
    13731308  }
     
    14941429{
    14951430  global $user, $language_files;
    1496  
     1431
    14971432  if ( !empty($dirname) and !empty($filename) )
    14981433  {
     
    15741509      if ( !isset($lang) ) $lang=array();
    15751510      if ( !isset($lang_info) ) $lang_info=array();
    1576      
     1511
    15771512      $parent_language = !empty($load_lang_info['parent']) ? $load_lang_info['parent'] : (
    15781513                            !empty($lang_info['parent']) ? $lang_info['parent'] : null );
     
    20662001    list($user['nb_available_comments']) = pwg_db_fetch_row(pwg_query($query));
    20672002
    2068     single_update(USER_CACHE_TABLE, 
     2003    single_update(USER_CACHE_TABLE,
    20692004      array('nb_available_comments'=>$user['nb_available_comments']),
    20702005      array('user_id'=>$user['id'])
     
    20872022{
    20882023  $replace_chars = create_function('$m', 'return ord(strtolower($m[1]));');
    2089  
     2024
    20902025  // add dot before groups of letters (version_compare does the same thing)
    20912026  $a = preg_replace('#([0-9]+)([a-z]+)#i', '$1.$2', $a);
    20922027  $b = preg_replace('#([0-9]+)([a-z]+)#i', '$1.$2', $b);
    2093  
     2028
    20942029  // apply ord() to any single letter
    20952030  $a = preg_replace_callback('#\b([a-z]{1})\b#i', $replace_chars, $a);
    20962031  $b = preg_replace_callback('#\b([a-z]{1})\b#i', $replace_chars, $b);
    2097  
     2032
    20982033  if (empty($op))
    20992034  {
Note: See TracChangeset for help on using the changeset viewer.