0
1

MODx Evolutionのエレメントをファイル出力する

Last updated at Posted at 2020-02-26

MODx Evolutionは、CMSとしても、ライトなWebアプリケーションフレームワークとしても使い勝手が良いのですが、エレメントの管理に常に悩まされます。エレメント(テンプレート、テンプレート変数、チャンク、スニペット、プラグイン)は管理画面上から手軽に追加・編集可能な利点がある反面、DB上でコードが管理されているのでバージョン管理やコードの比較、バックアップがしづらい等、そのような問題がメンテ時の欠点として出てきます。(手軽さとメンテのしやすさはトレードオフの関係です)
そこで、コードを出力してファイルとして管理できるようにしました。ざっくりと書いた感じなので、調整が必要な場合もあるかも知れませんが、これによってメンテが少しでも楽になれば。。

コード

<?php
$dbname = 'DB名';
$dbhost = 'DBホスト(FQDN or IP)';
$dsn = 'mysql:dbname=' . $dbname . ';host=' . $dbhost;
$user = 'ユーザー名';
$password = 'パスワード';
$dbo = new PDO($dsn, $user, $password);
$dbo->query('set names UTF8');
$base_dir = dirname(__FILE__);
$prefix = 'modx_'; // MODxテーブルプレフィックス
$tables = [
        'site_snippets',
        'site_templates',
        'site_tmplvars',
        'site_modules',
        'site_plugins',
        'site_htmlsnippets',
];
foreach ($tables as $table) {
        $sql = 'select * from ' . $prefix . $table . ';';
        $my_dir = $base_dir . '/' . $table;
        mkdir ($my_dir, 0755);
        foreach ($dbo->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $row) {
                $tmp = $row;
                unset($tmp['id']);
                switch ($table) {
                        case 'site_snippets':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['name']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.php', $row['snippet']);
                                break;
                        case 'site_templates':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['templatename']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.html', $row['content']);
                                break;
                        case 'site_tmplvars':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['name']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.html', $row['elements']);
                                break;
                        case 'site_modules':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['name']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.php', $row['modulecode']);
                                break;
                        case 'site_plugins':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['name']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.php', $row['plugincode']);
                                break;
                        case 'site_htmlsnippets':
                                $filen_base = '/' . str_replace(' ', '_', $tmp['name']);
                                file_put_contents($my_dir . $filen_base . '.json', json_encode($tmp));
                                file_put_contents($my_dir . $filen_base . '.html', $row['snippet']);
                                break;
                }
        }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1