Changeset 27336


Ignore:
Timestamp:
Feb 11, 2014, 10:47:44 PM (6 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

Location:
trunk/include
Files:
6 edited

Legend:

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

    r26461 r27336  
    134134if ($conf['display_fromto'])
    135135{
    136   $dates_of_category = array();
    137136  if (count($category_ids) > 0)
    138137  {
     
    140139SELECT
    141140    category_id,
    142     MIN(date_creation) AS date_creation_min,
    143     MAX(date_creation) AS date_creation_max
     141    MIN(date_creation) AS `from`,
     142    MAX(date_creation) AS `to`
    144143  FROM '.IMAGE_CATEGORY_TABLE.'
    145144    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
     
    156155  GROUP BY category_id
    157156;';
    158     $result = pwg_query($query);
    159     while ($row = pwg_db_fetch_assoc($result))
    160     {
    161       $dates_of_category[ $row['category_id'] ] = array(
    162         'from' => $row['date_creation_min'],
    163         'to'   => $row['date_creation_max'],
    164         );
    165     }
     157    $dates_of_category = query2array($query, 'category_id');
    166158  }
    167159}
  • trunk/include/dblayer/functions_mysql.inc.php

    r26461 r27336  
    761761}
    762762
     763/**
     764 * Builds an data array from a SQL query.
     765 * Depending on $key_name and $value_name it can return :
     766 *
     767 *    - an array of arrays of all fields (key=null, value=null)
     768 *        array(
     769 *          array('id'=>1, 'name'=>'DSC8956', ...),
     770 *          array('id'=>2, 'name'=>'DSC8957', ...),
     771 *          ...
     772 *          )
     773 *
     774 *    - an array of a single field (key=null, value='...')
     775 *        array('DSC8956', 'DSC8957', ...)
     776 *
     777 *    - an associative array of array of all fields (key='...', value=null)
     778 *        array(
     779 *          'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
     780 *          'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
     781 *          ...
     782 *          )
     783 *
     784 *    - an associative array of a single field (key='...', value='...')
     785 *        array(
     786 *          'DSC8956' => 1,
     787 *          'DSC8957' => 2,
     788 *          ...
     789 *          )
     790 *
     791 * @since 2.6
     792 *
     793 * @param string $query
     794 * @param string $key_name
     795 * @param string $value_name
     796 * @return array
     797 */
     798function query2array($query, $key_name=null, $value_name=null)
     799{
     800  $result = pwg_query($query);
     801  $data = array();
     802
     803  if (isset($key_name))
     804  {
     805    if (isset($value_name))
     806    {
     807      while ($row = pwg_db_fetch_assoc($result))
     808        $data[ $row[$key_name] ] = $row[$value_name];
     809    }
     810    else
     811    {
     812      while ($row = pwg_db_fetch_assoc($result))
     813        $data[ $row[$key_name] ] = $row;
     814    }
     815  }
     816  else
     817  {
     818    if (isset($value_name))
     819    {
     820      while ($row = pwg_db_fetch_assoc($result))
     821        $data[] = $row[$value_name];
     822    }
     823    else
     824    {
     825      while ($row = pwg_db_fetch_assoc($result))
     826        $data[] = $row;
     827    }
     828  }
     829
     830  return $data;
     831}
     832
    763833?>
  • trunk/include/dblayer/functions_mysqli.inc.php

    r26461 r27336  
    800800}
    801801
     802/**
     803 * Builds an data array from a SQL query.
     804 * Depending on $key_name and $value_name it can return :
     805 *
     806 *    - an array of arrays of all fields (key=null, value=null)
     807 *        array(
     808 *          array('id'=>1, 'name'=>'DSC8956', ...),
     809 *          array('id'=>2, 'name'=>'DSC8957', ...),
     810 *          ...
     811 *          )
     812 *
     813 *    - an array of a single field (key=null, value='...')
     814 *        array('DSC8956', 'DSC8957', ...)
     815 *
     816 *    - an associative array of array of all fields (key='...', value=null)
     817 *        array(
     818 *          'DSC8956' => array('id'=>1, 'name'=>'DSC8956', ...),
     819 *          'DSC8957' => array('id'=>2, 'name'=>'DSC8957', ...),
     820 *          ...
     821 *          )
     822 *
     823 *    - an associative array of a single field (key='...', value='...')
     824 *        array(
     825 *          'DSC8956' => 1,
     826 *          'DSC8957' => 2,
     827 *          ...
     828 *          )
     829 *
     830 * @since 2.6
     831 *
     832 * @param string $query
     833 * @param string $key_name
     834 * @param string $value_name
     835 * @return array
     836 */
     837function query2array($query, $key_name=null, $value_name=null)
     838{
     839  $result = pwg_query($query);
     840  $data = array();
     841
     842  if (isset($key_name))
     843  {
     844    if (isset($value_name))
     845    {
     846      while ($row = $result->fetch_assoc())
     847        $data[ $row[$key_name] ] = $row[$value_name];
     848    }
     849    else
     850    {
     851      while ($row = $result->fetch_assoc())
     852        $data[ $row[$key_name] ] = $row;
     853    }
     854  }
     855  else
     856  {
     857    if (isset($value_name))
     858    {
     859      while ($row = $result->fetch_assoc())
     860        $data[] = $row[$value_name];
     861    }
     862    else
     863    {
     864      while ($row = $result->fetch_assoc())
     865        $data[] = $row;
     866    }
     867  }
     868
     869  return $data;
     870}
     871
    802872?>
  • 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  {
  • trunk/include/functions_tag.inc.php

    r26649 r27336  
    7171  GROUP BY tag_id
    7272;';
    73   $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
     73  $tag_counters = query2array($query, 'tag_id', 'counter');
    7474
    7575  if ( empty($tag_counters) )
     
    234234  $query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by);
    235235
    236   return array_from_query($query, 'id');
     236  return query2array($query, null, 'id');
    237237}
    238238
     
    321321    OR ', $where_clauses);
    322322
    323   $result = pwg_query($query);
    324   $tags = array();
    325   while ($row = pwg_db_fetch_assoc($result))
    326   {
    327     $tags[] = $row;
    328   }
    329   return $tags;
     323  return query2array($query);
    330324}
    331325
  • trunk/include/section_init.inc.php

    r26461 r27336  
    9393  // url compatibility with versions below 1.6
    9494  if ( isset($_GET['image_id'])
    95        and isset($_GET['cat']) 
     95       and isset($_GET['cat'])
    9696       and is_numeric($_GET['cat']) )
    9797  {
     
    103103    redirect($url);
    104104  }
    105  
     105
    106106  $token = $tokens[$next_token];
    107107  $next_token++;
     
    243243  {
    244244    $page = array_merge(
    245       $page, 
     245      $page,
    246246      array(
    247247        'comment' => trigger_event(
     
    293293        "\n  AND"
    294294        );
    295        
    296         $subcat_ids = array_from_query($query, 'id');
     295
     296        $subcat_ids = query2array($query,null, 'id');
    297297        $subcat_ids[] = $page['category']['id'];
    298298        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
     
    326326;';
    327327
    328     $page['items'] = array_from_query($query, 'image_id');
     328    $page['items'] = query2array($query,null, 'image_id');
    329329  }
    330330}
     
    417417        $page,
    418418        array(
    419           'items' => array_from_query($query, 'image_id'),
     419          'items' => query2array($query,null, 'image_id'),
    420420          )
    421421        );
     
    464464        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
    465465                    .l10n('Recent photos').'</a>',
    466         'items' => array_from_query($query, 'id'),
     466        'items' => query2array($query,null, 'id'),
    467467        )
    468468      );
     
    487487    $page['super_order_by'] = true;
    488488    $conf['order_by'] = ' ORDER BY hit DESC, id DESC';
    489    
     489
    490490    $query = '
    491491SELECT DISTINCT(id)
     
    503503        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
    504504                    .$conf['top_number'].' '.l10n('Most visited').'</a>',
    505         'items' => array_from_query($query, 'id'),
     505        'items' => query2array($query,null, 'id'),
    506506        )
    507507      );
     
    529529        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
    530530                    .$conf['top_number'].' '.l10n('Best rated').'</a>',
    531         'items' => array_from_query($query, 'id'),
     531        'items' => query2array($query,null, 'id'),
    532532        )
    533533      );
     
    552552        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
    553553                    .l10n('Random photos').'</a>',
    554         'items' => array_from_query($query, 'id'),
     554        'items' => query2array($query,null, 'id'),
    555555        )
    556556      );
     
    633633  {
    634634    $redirect_url = script_basename()=='picture' ? duplicate_picture_url() : duplicate_index_url();
    635    
     635
    636636    if (!headers_sent())
    637637    { // this is a permanent redirection
Note: See TracChangeset for help on using the changeset viewer.