Advertisement
Guest User

[PHP] Piwigo V2P with duplicate check

a guest
Dec 25th, 2016
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. #!/usr/bin/env  php
  2. <?php
  3. // twitter.com/jimaek
  4. // www.jsdelivr.com
  5. // Converts all virtual albums to physical and moves the associated images to galleries/
  6. // Put in Piwigo root and run from console
  7. // BACKUP EVERYTHING BEFORE RUNNING!!!!!!!
  8. // I am not responsible for data loss or any other problems
  9. // amount of photos per iteration
  10. $limit = 20;
  11. $site_id=1; // id directory with images [http://piwigo/admin.php?page=site_manager]
  12.  
  13. error_reporting(E_ALL);
  14. header("Content-type: text/plain");
  15. include('local/config/database.inc.php');
  16.  
  17. $dbc = new mysqli($conf['db_host'],$conf['db_user'],$conf['db_password'],$conf['db_base']);
  18.  
  19. $offset = 0;
  20. do {
  21.     $continue = true;
  22.     $results = array();
  23.     $sql = "SELECT
  24.         i.*, c.`name`, c.`dir`,
  25.         c.`uppercats`,
  26.         (SELECT GROUP_CONCAT(pc.id,':::',IFNULL(pc.dir,''),':::',IFNULL(pc.name,'') SEPARATOR '///')
  27.         FROM `{$prefixeTable}categories` pc
  28.         WHERE FIND_IN_SET(pc.id, c.`uppercats`)) AS full_path
  29.         FROM `{$prefixeTable}images` i
  30.         INNER JOIN `{$prefixeTable}image_category` ic ON (i.id = ic.`image_id`)
  31.         INNER JOIN `{$prefixeTable}categories` c ON (ic.`category_id` = c.`id`)
  32.     WHERE path LIKE './upload/%'
  33.     GROUP BY i.id
  34.     LIMIT $offset, $limit";
  35.     //echo $sql;
  36.     if ($result = $dbc->query($sql)) {
  37.         if ($result->num_rows > 0) {
  38.             $continue = true;
  39.         }
  40.         else {
  41.             $continue = false;
  42.         }
  43.                 //$results = $result->fetch_all(MYSQLI_ASSOC);
  44.                 //for PHP with mysql driver (not mysqlnd) - without fetch_all function
  45.                 for ($results = array(); $tmp = $result->fetch_array(MYSQLI_ASSOC);) $results[] = $tmp;
  46.         $result->close();
  47.         //
  48.         foreach ($results as $image) {
  49.             // echo "\n".print_r($image, true);
  50.             // get category path
  51.             $filename = $image['file'];
  52.             $full_q_path = explode('///', $image['full_path']);
  53.             $real_path = '';
  54.             $real_path_a = array();
  55.             $sorted_q_path = array();
  56.             $storage_category_id = 0;
  57.             foreach ($full_q_path as $el) {
  58.                 $sorted_q_path[] = explode(':::', $el);
  59.             };
  60.             asort($sorted_q_path, SORT_ASC);
  61.             foreach ($sorted_q_path as $el) {
  62.                 list($id, $fs_dir, $virt_dir) = $el;
  63.                 if (empty($fs_dir)) {
  64.                     $fs_dir = preg_replace("/[^\\w-_\ ]+/",'',$virt_dir);
  65.                     $fs_dir = trim($fs_dir);
  66.                     $dbc->query("UPDATE `{$prefixeTable}categories` pc SET pc.dir='".$dbc->real_escape_string($fs_dir)."', site_id = {$site_id} WHERE pc.id = {$id} ");
  67.                 }
  68.                 $real_path_a[] = $fs_dir;
  69.                 $storage_category_id = $id;
  70.             }
  71.             $real_path = './galleries/'. implode('/',$real_path_a);
  72.             echo "\n".$real_path.'/'. $image['file'];
  73.             if(is_dir($real_path)) {
  74.                 echo ' ';
  75.             }
  76.             else {
  77.                 mkdir($real_path, 0775, true);
  78.                 echo '+';
  79.             }
  80.             // check for duplicates
  81.             $new_file_path = $real_path.'/'.$image['file'];
  82.             $dupe_file_path = $new_file_path;
  83.             $dupes = 1;
  84.             while ($dupes > 0) {
  85.                 $sql="SELECT path FROM images WHERE path='".$dbc->real_escape_string($dupe_file_path)."'";
  86.                 if ($result = $dbc->query($sql)) {
  87.                     if ($result->num_rows > 0) {
  88.                         $dupe_file_path = pathinfo($new_file_path,PATHINFO_DIRNAME) . "/" . pathinfo($new_file_path,PATHINFO_FILENAME) . $dupes++ . "." . pathinfo($new_file_path,PATHINFO_EXTENSION);
  89.                     }
  90.                     else {
  91.                         $dupes = 0;
  92.                         $new_file_path = $dupe_file_path;
  93.                         break;
  94.                     }
  95.                 }
  96.             }
  97.             // copy
  98.             copy($image['path'], $new_file_path);
  99.             echo ' [v]';
  100.             //update path, storage category
  101.             $dbc->query("UPDATE `{$prefixeTable}images` SET `path`='".$dbc->real_escape_string($new_file_path)."' WHERE id = {$image['id']}");
  102.             if ($storage_category_id > 0) {
  103.                 $dbc->query("UPDATE `{$prefixeTable}images` SET storage_category_id = $storage_category_id WHERE id = {$image['id']}");
  104.             }
  105.             // remove old thumbnails
  106.             $image_dir = pathinfo($image['path'], PATHINFO_DIRNAME);
  107.             $thumb_dir = "./_data/i" . ltrim($image_dir,".");
  108.             foreach (glob($thumb_dir . "/*") as $filename) {
  109.                 unlink($filename);
  110.             }
  111.             foreach (glob($thumb_dir . "/pwg_representative/*") as $filename) {
  112.                 unlink($filename);
  113.             }
  114.             // try deleting thumbnail directory too
  115.             @rmdir($thumb_dir);
  116.             @rmdir($image_dir . "/pwg_representative");
  117.             // try deleting two parent directories (month, year)
  118.             @rmdir(dirname($thumb_dir));
  119.             @rmdir(dirname($thumb_dir,1));
  120.         }
  121.     }
  122.     else {
  123.         $continue = false;
  124.     }
  125. }
  126. while ($continue);
  127.  
  128. $dbc->close();
  129.  
  130. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement