-
-
Save wanze/e8051e0bf754c2f75083 to your computer and use it in GitHub Desktop.
ProcessWire: Migration of files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* !!! TODO's before starting the migration: | |
* | |
* 1) $config->pagefileExtendedPaths = true; in /site/config.php | |
* 2) rename /site/assets/files to "files_old" | |
* 3) Create a new /site/assets/files folder and give www-data write permission | |
* | |
* Execute the migration | |
* | |
* $ sudo -u www-data ./migrate_filesystem.sh test 0 | |
* | |
* arg0: test or live system | |
* arg1: Number of batch; Increment this number for each round | |
* | |
* @author Stefan Wanzenried <stefan.wanzenried@gmail.com> | |
*/ | |
$mode = $argv[1]; | |
$step = $argv[2]; | |
if (!in_array($mode, array('live', 'test'))) { | |
die('Erster parameter muss "live" oder "test" sein'); | |
} | |
if ($mode == 'live') { | |
include('/var/www/prod/index.php'); | |
} else { | |
include('/var/www/test/index.php'); | |
} | |
// Setze admin user wegen den Zugriff-Rechte auf Produkte | |
$users = wire('users'); | |
$superuser = $users->get("admin"); | |
$users->setCurrentUser($superuser); | |
$migration = new MigrateFileSystem($mode, $step); | |
$migration->execute(); | |
class MigrateFileSystem | |
{ | |
const LIMIT = 5000; | |
const FILES_FOLDER_RENAMED = 'files_old'; | |
/** | |
* @var int | |
*/ | |
protected $step = 0; | |
/** | |
* @var string | |
*/ | |
protected $mode = 'dev'; | |
/** | |
* @param string $mode | |
* @param int $step | |
*/ | |
public function __construct($mode, $step) | |
{ | |
$this->step = $step; | |
$this->mode = $mode; | |
} | |
public function execute() | |
{ | |
$start = (int) $this->step * self::LIMIT; | |
$products = wire('pages')->find("template=product,start={$start},limit=" . self::LIMIT); | |
$count = $products->count(); | |
$success = 0; | |
echo "Start migrating files of {$count} products...\n"; | |
foreach ($products as $product) { | |
$old = wire('config')->paths->assets . self::FILES_FOLDER_RENAMED . '/' . self::makeCustomPath($product); | |
if (!is_dir($old)) { | |
$success++; | |
continue; | |
} | |
/** @var PagefilesManager $manager */ | |
$manager = $product->filesManager; | |
$new = $manager->path(); | |
// Kopieren aller alten Daten zum neuen Ort! | |
$result = wireCopy($old, $new, array('recursive' => false)); | |
if ($result) { | |
$success++; | |
} | |
} | |
echo "\n\nMigrated {$success}/{$count} files\n"; | |
$failed = $count - $success; | |
echo "Failures: {$failed}\n"; | |
} | |
static public function makeCustomPath(Page $page) | |
{ | |
// Make subfolders per ID - always two digits (or one per folder) | |
// Example: ID=1780, path = /site/assets/files/17/80/ | |
// ID=19814, path = /site/assets/files/19/81/4/ | |
// ID=205478, path = /site/assets/files/20/54/78/ | |
$tmpPath = ''; | |
$digits = str_split("{$page->id}"); | |
foreach ($digits as $k => $digit) { | |
$tmpPath .= $digit; | |
if (($k + 1) % 2 == 0) $tmpPath .= '/'; | |
} | |
if (substr($tmpPath, -1) != '/') $tmpPath .= '/'; | |
return $tmpPath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment