Ignore:
Timestamp:
Aug 29, 2008, 2:35:16 PM (16 years ago)
Author:
rvelices
Message:

synchro improvements:

  • able to sync metadata at the same time as the files/dirs
  • by default empty metadata does not overwrite database infos (checkbox can switch to previous behaviour) (bug 132)
  • the form is shown again even after a successfull non simulated run
File:
1 edited

Legend:

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

    r2488 r2491  
    378378    $first = true;
    379379
    380     $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\';';
     380    $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
    381381    list(, $packet_size) = mysql_fetch_row(pwg_query($query));
    382382    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
     
    387387      if (strlen($query) >= $packet_size)
    388388      {
    389         $query .= '
    390 ;';
    391389        pwg_query($query);
    392390        $first = true;
     
    426424      $query .= ')';
    427425    }
    428 
    429     $query .= '
    430 ;';
    431426    pwg_query($query);
    432427  }
    433428}
    434429
     430define('MASS_UPDATES_SKIP_EMPTY', 1);
    435431/**
    436432 * updates multiple lines in a table
     
    439435 * @param array dbfields
    440436 * @param array datas
     437 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
    441438 * @return void
    442439 */
    443 function mass_updates($tablename, $dbfields, $datas)
    444 {
    445   if (count($datas) != 0)
    446   {
    447     // depending on the MySQL version, we use the multi table update or N
    448     // update queries
    449     if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0)
    450     {
    451       // MySQL is prior to version 4.0.4, multi table update feature is not
    452       // available
    453       foreach ($datas as $data)
    454       {
    455         $query = '
     440function mass_updates($tablename, $dbfields, $datas, $flags=0)
     441{
     442  if (count($datas) == 0)
     443    return;
     444  // depending on the MySQL version, we use the multi table update or N update queries
     445  if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0)
     446  { // MySQL is prior to version 4.0.4, multi table update feature is not available
     447    foreach ($datas as $data)
     448    {
     449      $query = '
    456450UPDATE '.$tablename.'
    457451  SET ';
    458         $is_first = true;
    459         foreach ($dbfields['update'] as $key)
     452      $is_first = true;
     453      foreach ($dbfields['update'] as $key)
     454      {
     455        $separator = $is_first ? '' : ",\n    ";
     456
     457        if (isset($data[$key]) and $data[$key] != '')
    460458        {
    461           if (!$is_first)
    462           {
    463             $query.= ",\n      ";
    464           }
    465           $query.= $key.' = ';
    466           if (isset($data[$key]) and $data[$key] != '')
    467           {
    468             $query.= '\''.$data[$key].'\'';
    469           }
    470           else
    471           {
    472             $query.= 'NULL';
    473           }
    474           $is_first = false;
     459          $query.= $separator.$key.' = \''.$data[$key].'\'';
    475460        }
     461        else
     462        {
     463          if ($flags & MASS_UPDATES_SKIP_EMPTY )
     464            continue; // next field
     465          $query.= "$separator$key = NULL";
     466        }
     467        $is_first = false;
     468      }
     469      if (!$is_first)
     470      {// only if one field at least updated
    476471        $query.= '
    477472  WHERE ';
    478 
    479473        $is_first = true;
    480474        foreach ($dbfields['primary'] as $key)
     
    494488          $is_first = false;
    495489        }
    496         $query.= '
    497 ;';
    498490        pwg_query($query);
    499491      }
    500     }
     492    } // foreach update
     493  } // if mysql_ver or count<X
     494  else
     495  {
     496    // creation of the temporary table
     497    $query = '
     498SHOW FULL COLUMNS FROM '.$tablename;
     499    $result = pwg_query($query);
     500    $columns = array();
     501    $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
     502    while ($row = mysql_fetch_array($result))
     503    {
     504      if (in_array($row['Field'], $all_fields))
     505      {
     506        $column = $row['Field'];
     507        $column.= ' '.$row['Type'];
     508
     509        $nullable = true;
     510        if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO')
     511        {
     512          $column.= ' NOT NULL';
     513          $nullable = false;
     514        }
     515        if (isset($row['Default']))
     516        {
     517          $column.= " default '".$row['Default']."'";
     518        }
     519        elseif ($nullable)
     520        {
     521          $column.= " default NULL";
     522        }
     523        if (isset($row['Collation']) and $row['Collation'] != 'NULL')
     524        {
     525          $column.= " collate '".$row['Collation']."'";
     526        }
     527        array_push($columns, $column);
     528      }
     529    }
     530
     531    $temporary_tablename = $tablename.'_'.micro_seconds();
     532
     533    $query = '
     534CREATE TABLE '.$temporary_tablename.'
     535(
     536  '.implode(",\n  ", $columns).',
     537  UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).')
     538)';
     539
     540    pwg_query($query);
     541    mass_inserts($temporary_tablename, $all_fields, $datas);
     542    if ( $flags & MASS_UPDATES_SKIP_EMPTY )
     543      $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";');
    501544    else
    502     {
    503       // creation of the temporary table
    504       $query = '
    505 SHOW FULL COLUMNS FROM '.$tablename.'
    506 ;';
    507       $result = pwg_query($query);
    508       $columns = array();
    509       $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
    510       while ($row = mysql_fetch_array($result))
    511       {
    512         if (in_array($row['Field'], $all_fields))
    513         {
    514           $column = $row['Field'];
    515           $column.= ' '.$row['Type'];
    516 
    517           $nullable = true;
    518           if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO')
    519           {
    520             $column.= ' NOT NULL';
    521             $nullable = false;
    522           }
    523           if (isset($row['Default']))
    524           {
    525             $column.= " default '".$row['Default']."'";
    526           }
    527           elseif ($nullable)
    528           {
    529             $column.= " default NULL";
    530           }
    531           if (isset($row['Collation']) and $row['Collation'] != 'NULL')
    532           {
    533             $column.= " collate '".$row['Collation']."'";
    534           }
    535           array_push($columns, $column);
    536         }
    537       }
    538 
    539       $temporary_tablename = $tablename.'_'.micro_seconds();
    540 
    541       $query = '
    542   CREATE TABLE '.$temporary_tablename.'
    543   (
    544   '.implode(",\n", $columns).',
    545   UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).')
    546   )
    547 ;';
    548 
    549       pwg_query($query);
    550       mass_inserts($temporary_tablename, $all_fields, $datas);
    551       // update of images table by joining with temporary table
    552       $query = '
     545      $func_set = create_function('$s', 'return "t1.$s = t2.$s";');
     546
     547    // update of images table by joining with temporary table
     548    $query = '
    553549UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2
    554550  SET '.
    555         implode(
    556           "\n    , ",
    557           array_map(
    558             create_function('$s', 'return "t1.$s = t2.$s";'),
    559             $dbfields['update']
    560             )
    561           ).'
     551      implode(
     552        "\n    , ",
     553        array_map($func_set,$dbfields['update'])
     554        ).'
    562555  WHERE '.
    563         implode(
    564           "\n    AND ",
    565           array_map(
    566             create_function('$s', 'return "t1.$s = t2.$s";'),
    567             $dbfields['primary']
    568             )
    569           ).'
    570   ;';
    571       pwg_query($query);
    572       $query = '
    573 DROP TABLE '.$temporary_tablename.'
    574 ;';
    575       pwg_query($query);
    576     }
     556      implode(
     557        "\n    AND ",
     558        array_map(
     559          create_function('$s', 'return "t1.$s = t2.$s";'),
     560          $dbfields['primary']
     561          )
     562        );
     563    pwg_query($query);
     564    $query = '
     565DROP TABLE '.$temporary_tablename;
     566    pwg_query($query);
    577567  }
    578568}
     
    588578SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat, uppercats, rank, global_rank
    589579  FROM '.CATEGORIES_TABLE.'
    590   ORDER BY id_uppercat,rank,name
    591 ;';
     580  ORDER BY id_uppercat,rank,name';
    592581
    593582  $cat_map = array();
     
    658647  if (!in_array($value, array('true', 'false')))
    659648  {
     649    trigger_error("set_cat_visible invalid param $value", E_USER_WARNING);
    660650    return false;
    661651  }
     
    668658UPDATE '.CATEGORIES_TABLE.'
    669659  SET visible = \'true\'
    670   WHERE id IN ('.implode(',', $uppercats).')
    671 ;';
     660  WHERE id IN ('.implode(',', $uppercats).')';
    672661    pwg_query($query);
    673662  }
     
    679668UPDATE '.CATEGORIES_TABLE.'
    680669  SET visible = \'false\'
    681   WHERE id IN ('.implode(',', $subcats).')
    682 ;';
     670  WHERE id IN ('.implode(',', $subcats).')';
    683671    pwg_query($query);
    684672  }
     
    696684  if (!in_array($value, array('public', 'private')))
    697685  {
     686    trigger_error("set_cat_status invalid param $value", E_USER_WARNING);
    698687    return false;
    699688  }
     
    717706UPDATE '.CATEGORIES_TABLE.'
    718707  SET status = \'private\'
    719   WHERE id IN ('.implode(',', $subcats).')
    720 ;';
     708  WHERE id IN ('.implode(',', $subcats).')';
    721709    pwg_query($query);
    722710  }
     
    15831571
    15841572  // List all tables
    1585   $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\';';
     1573  $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\'';
    15861574  $result = pwg_query($query);
    15871575  while ($row = mysql_fetch_array($result))
     
    15911579
    15921580  // Repair all tables
    1593   $query = 'REPAIR TABLE '.implode(', ', $all_tables).';';
     1581  $query = 'REPAIR TABLE '.implode(', ', $all_tables);
    15941582  $mysql_rc = pwg_query($query);
    15951583
     
    16171605
    16181606  // Optimize all tables
    1619   $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables).';';
     1607  $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables);
    16201608  $mysql_rc = $mysql_rc && pwg_query($query);
    16211609  if ($mysql_rc)
     
    18331821  $query = '
    18341822UPDATE '.USER_CACHE_TABLE.'
    1835   SET need_update = \'true\'
    1836 ;';
     1823  SET need_update = \'true\'';
    18371824  pwg_query($query);
    18381825  trigger_action('invalidate_user_cache');
Note: See TracChangeset for help on using the changeset viewer.