Changeset 2306


Ignore:
Timestamp:
Apr 21, 2008, 12:20:20 AM (16 years ago)
Author:
rvelices
Message:
  • merged function ordering() with update_global_rank() and also optimized the queries
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/cat_list.php

    r2299 r2306  
    5858  mass_updates(CATEGORIES_TABLE, $fields, $datas);
    5959
    60   update_global_rank(@$_GET['parent_id']);
     60  update_global_rank();
    6161}
    6262
     
    6767$categories = array();
    6868
    69 $base_url = PHPWG_ROOT_PATH.'admin.php?page=cat_list';
    70 $navigation = '<a class="" href="'.$base_url.'">';
     69$base_url = get_root_url().'admin.php?page=cat_list';
     70$navigation = '<a href="'.$base_url.'">';
    7171$navigation.= l10n('home');
    7272$navigation.= '</a>';
     
    8080  delete_categories(array($_GET['delete']));
    8181  array_push($page['infos'], l10n('cat_virtual_deleted'));
    82   ordering();
    8382  update_global_rank();
    8483}
     
    237236      'U_CHILDREN' => $cat_list_url.'&amp;parent_id='.$category['id'],
    238237      'U_EDIT'     => $base_url.'cat_modify&amp;cat_id='.$category['id'],
    239      
     238
    240239      'IS_VIRTUAL' => empty($category['dir'])
    241240    );
  • trunk/admin/include/functions.php

    r2304 r2306  
    666666
    667667/**
    668  * updates the global_rank of categories under the given id_uppercat
    669  *
    670  * @param int id_uppercat
     668 * order categories (update categories.rank and global_rank database fields)
     669 * so that rank field are consecutive integers starting at 1 for each child
    671670 * @return void
    672671 */
    673 function update_global_rank($id_uppercat = 'all')
    674 {
    675   $query = '
    676 SELECT id,rank
     672function update_global_rank()
     673{
     674  $query = '
     675SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat, uppercats, rank, global_rank
    677676  FROM '.CATEGORIES_TABLE.'
    678 ;';
    679   $result = pwg_query($query);
    680   $ranks_array = array();
    681   while ($row = mysql_fetch_array($result))
    682   {
    683     $ranks_array[$row['id']] = $row['rank'];
    684   }
    685 
    686   // which categories to update ?
    687   $uppercats_array = array();
    688 
    689   $query = '
    690 SELECT id,uppercats
    691   FROM '.CATEGORIES_TABLE;
    692   if (is_numeric($id_uppercat))
    693   {
    694     $query.= '
    695   WHERE uppercats REGEXP \'(^|,)'.$id_uppercat.'(,|$)\'
    696     AND id != '.$id_uppercat.'
    697 ';
    698   }
    699   $query.= '
    700 ;';
     677  ORDER BY id_uppercat,rank,name
     678;';
     679
     680  $cat_map = array();
     681
     682  $current_rank = 0;
     683  $current_uppercat = '';
     684
    701685  $result = pwg_query($query);
    702686  while ($row = mysql_fetch_array($result))
    703687  {
    704     $uppercats_array[$row['id']] =  $row['uppercats'];
     688    if ($row['id_uppercat'] != $current_uppercat)
     689    {
     690      $current_rank = 0;
     691      $current_uppercat = $row['id_uppercat'];
     692    }
     693    ++$current_rank;
     694    $cat =
     695      array(
     696        'rank' =>        $current_rank,
     697        'rank_changed' =>$current_rank!=$row['rank'],
     698        'global_rank' => $row['global_rank'],
     699        'uppercats' =>   $row['uppercats'],
     700        );
     701    $cat_map[ $row['id'] ] = $cat;
    705702  }
    706703
    707704  $datas = array();
    708   foreach ($uppercats_array as $id => $uppercats)
    709   {
    710     array_push(
    711       $datas,
    712       array(
    713         'id'          => $id,
    714         'global_rank' => preg_replace(
     705
     706  foreach( $cat_map as $id=>$cat )
     707  {
     708    $new_global_rank = preg_replace(
    715709          '/(\d+)/e',
    716           "\$ranks_array['$1']",
    717           str_replace(',', '.', $uppercats)
    718           ),
    719         )
    720       );
     710          "\$cat_map['$1']['rank']",
     711          str_replace(',', '.', $cat['uppercats'] )
     712          );
     713    if ( $cat['rank_changed']
     714      or $new_global_rank!=$cat['global_rank']
     715      )
     716    {
     717      $datas[] = array(
     718          'id' => $id,
     719          'rank' => $cat['rank'],
     720          'global_rank' => $new_global_rank,
     721        );
     722    }
    721723  }
    722724
     
    725727    array(
    726728      'primary' => array('id'),
    727       'update'  => array('global_rank')
     729      'update'  => array('rank', 'global_rank')
    728730      ),
    729731    $datas
    730732    );
     733  return count($datas);
    731734}
    732735
     
    874877    $datas
    875878    );
    876 }
    877 
    878 /**
    879  * order categories (update categories.rank and global_rank database fields)
    880  *
    881  * the purpose of this function is to give a rank for all categories
    882  * (insides its sub-category), even the newer that have none at te
    883  * beginning. For this, ordering function selects all categories ordered by
    884  * rank ASC then name ASC for each uppercat.
    885  *
    886  * @returns void
    887  */
    888 function ordering()
    889 {
    890   $current_rank = 0;
    891   $current_uppercat = '';
    892 
    893   $query = '
    894 SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat
    895   FROM '.CATEGORIES_TABLE.'
    896   ORDER BY id_uppercat,rank,name
    897 ;';
    898   $result = pwg_query($query);
    899   $datas = array();
    900   while ($row = mysql_fetch_array($result))
    901   {
    902     if ($row['id_uppercat'] != $current_uppercat)
    903     {
    904       $current_rank = 0;
    905       $current_uppercat = $row['id_uppercat'];
    906     }
    907     $data = array('id' => $row['id'], 'rank' => ++$current_rank);
    908     array_push($datas, $data);
    909   }
    910 
    911   $fields = array('primary' => array('id'), 'update' => array('rank'));
    912   mass_updates(CATEGORIES_TABLE, $fields, $datas);
    913879}
    914880
     
    13531319
    13541320  update_uppercats();
    1355   ordering();
    13561321  update_global_rank();
    13571322
  • trunk/admin/maintenance.php

    r2299 r2306  
    4646    update_uppercats();
    4747    update_category('all');
    48     ordering();
    4948    update_global_rank();
    5049    invalidate_user_cache();
  • trunk/admin/site_update.php

    r2299 r2306  
    447447    }
    448448
    449     if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) 
     449    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
    450450          and !isset($fs[$path]['tn_ext']) )
    451     { // For a picture thumbnail is mandatory and for non picture element, 
     451    { // For a picture thumbnail is mandatory and for non picture element,
    452452      // thumbnail and representative are optionnal
    453453      array_push(
     
    471471        'storage_category_id' => $db_fulldirs[$dirname],
    472472        );
    473        
     473
    474474      if ( $_POST['privacy_level']!=0 )
    475475      {
     
    622622      . ' -->' );
    623623    $start = get_moment();
    624     ordering();
    625624    update_global_rank();
    626625    $template->append('footer_elements', '<!-- ordering categories : '
  • trunk/install/upgrade_1.3.1.php

    r2299 r2306  
    6868  ADD COLUMN commentable enum('true','false') NOT NULL default 'true'
    6969;",
    70  
     70
    7171  "
    7272ALTER TABLE phpwebgallery_categories
     
    8888  SET date_temp = date
    8989;",
    90  
     90
    9191  "
    9292ALTER TABLE phpwebgallery_comments
     
    123123  SET date_temp = date
    124124;",
    125  
     125
    126126  "
    127127ALTER TABLE phpwebgallery_history
     
    204204  ADD INDEX images_i5 (date_creation)
    205205;",
    206  
     206
    207207  "
    208208ALTER TABLE phpwebgallery_sessions
     
    219219  SET expiration_temp = expiration
    220220;",
    221  
     221
    222222  "
    223223ALTER TABLE phpwebgallery_sessions
     
    234234  DROP COLUMN expiration_temp
    235235;",
    236  
     236
    237237  "
    238238ALTER TABLE phpwebgallery_sites
     
    248248DROP TABLE phpwebgallery_user_category
    249249;",
    250  
     250
    251251  "
    252252ALTER TABLE phpwebgallery_users
     
    273273  ADD UNIQUE users_ui1 (username)
    274274;",
    275  
     275
    276276  "
    277277CREATE TABLE phpwebgallery_rate (
     
    339339{
    340340  $existing_indexes = array();
    341  
     341
    342342  $query = '
    343343SHOW INDEX
     
    526526
    527527// refresh calculated datas
    528 ordering();
    529528update_global_rank();
    530529update_category();
     
    584583  array(
    585584    'all sub-categories of private categories become private',
    586    
     585
    587586    'user permissions and group permissions have been erased',
    588587
Note: See TracChangeset for help on using the changeset viewer.