Advertisement
Guest User

Menalto2Piwigo - touch dirs based on G2 DB

a guest
Feb 9th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2.  
  3. // This script get a dates of albums from Gallery2 SQL DB
  4. // and touches every dir/album in destination folder.
  5. // Useful within G2-Piwigo process.
  6. // 1. copy directories from G2 data/albums to piwigo/galleries
  7. // 2. run this script with only_list=true, it will show you
  8. // 3. run this script with only_list=false, it will touch dirs
  9. // It will only change a metadata (mtime and ctime) of directories
  10. // and only if they will be founded in G2 table.
  11.  
  12. $root = '/dir/to/galleries';
  13. $only_list = true;
  14.  
  15. mysql_connect("localhost", "g2-username", "g2-pass") or die(mysql_error());
  16. mysql_select_db("database_name") or die(mysql_error());
  17.  
  18. mysql_query("SET NAMES 'utf8'");
  19. mysql_query("SET CHARACTER SET utf8 ");
  20.  
  21. $query = 'SELECT g2_Item.g_id as id,
  22.    g2_Item.g_title as title,
  23.    g2_Item.g_originationTimestamp as timestamp,
  24.    g2_Item.g_originationTimestamp as date,
  25.    g2_FileSystemEntity.g_pathComponent as directory,
  26.    g2_ItemAttributesMap.g_parentSequence as parent
  27.    FROM g2_Item,g2_FileSystemEntity,g2_ItemAttributesMap
  28.    WHERE g2_Item.g_canContainChildren=1 and
  29.    g2_FileSystemEntity.g_id=g2_Item.g_id and
  30.    g2_ItemAttributesMap.g_itemId=g2_Item.g_id
  31.    ORDER BY id';
  32.  
  33. $result = mysql_query($query);
  34.  
  35. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  36. {
  37.     if ($row["directory"] != '') {
  38.       if ($only_list) { echo "$row[id] | $row[title] | " . date('r', $row["date"]) . " | "; }
  39.       $path = '';
  40.       $entity = $row["id"];
  41.       $end = 'false';
  42.       while ($end == 'false') {
  43.         $query2 = "SELECT g2_FileSystemEntity.g_pathComponent as parentDirectory,
  44.          g2_ChildEntity.g_parentId as parentId
  45.          FROM g2_FileSystemEntity,g2_ChildEntity
  46.          WHERE g2_ChildEntity.g_id='$entity' and
  47.          g2_ChildEntity.g_parentId=g2_FileSystemEntity.g_id";
  48.         $result2 = mysql_query($query2);
  49.         $num_rows = mysql_num_rows($result2);
  50.         switch ($num_rows) {
  51.           case 0:
  52.             $end = 'true';
  53.             break;
  54.           case 1:
  55.             $row2 = mysql_fetch_array($result2);
  56.             $entity =  $row2["parentId"];
  57.             $path = $row2["parentDirectory"] . '/' . $path;
  58.             break;
  59.          default:
  60.             echo "error - too much rows ($num_rows) ";
  61.             $end = 'true';
  62.         }
  63.       }
  64.       $fullpath = $path . $row["directory"];
  65.       if ($only_list) {
  66.         echo $fullpath . "\n";
  67.       } else {
  68.         if (is_dir($root . $fullpath))
  69.         {
  70.           echo 'Touching ' . $root . $fullpath . "\n";
  71.           touch($root.$fullpath, $row["date"]);
  72.         } else {
  73.           echo 'Missing directory ' . $root . $fullpath . "\n";
  74.         }
  75.       }
  76.     }
  77. }
  78.  
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement