updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / pluf-aa-git / backup.patch
blob0030c2a0908758e567cc1a0b364b7972814ef7ac
1 diff --git a/src/Admin/Migrations/Backup.php b/src/Admin/Migrations/Backup.php
2 new file mode 100644
3 index 0000000..8b05b30
4 --- /dev/null
5 +++ b/src/Admin/Migrations/Backup.php
6 @@ -0,0 +1,79 @@
7 +<?php
8 +/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
9 +/*
10 +# ***** BEGIN LICENSE BLOCK *****
11 +# This file is part of Plume Framework, a simple PHP Application Framework.
12 +# Copyright (C) 2001-2009 Loic d'Anterroches and contributors.
14 +# Plume Framework is free software; you can redistribute it and/or modify
15 +# it under the terms of the GNU Lesser General Public License as published by
16 +# the Free Software Foundation; either version 2.1 of the License, or
17 +# (at your option) any later version.
19 +# Plume Framework is distributed in the hope that it will be useful,
20 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
21 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 +# GNU Lesser General Public License for more details.
24 +# You should have received a copy of the GNU Lesser General Public License
25 +# along with this program; if not, write to the Free Software
26 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 +# ***** END LICENSE BLOCK ***** */
30 +/**
31 + * Backup of the Plume Framework.
32 + *
33 + * Backup all the data of the framework models. By default the backup
34 + * name will be the date YYYY-MM-DD.
35 + *
36 + * @param string Path to the folder where to store the backup
37 + * @param string Name of the backup (null)
38 + * @return int The backup was correctly written
39 + */
40 +function Admin_Migrations_Backup_run($folder, $name=null)
42 + $db = Pluf::db();
43 + $models = array(
44 + 'Admin_Log',
45 + );
47 + // Now, for each table, we dump the content in json, this is a
48 + // memory intensive operation
49 + $to_json = array();
50 + foreach ($models as $model) {
51 + $to_json[$model] = Pluf_Test_Fixture::dump($model, false);
52 + }
53 + if (null == $name) {
54 + $name = date('Y-m-d');
55 + }
56 + return file_put_contents(sprintf('%s/%s-Admin.json', $folder, $name),
57 + json_encode($to_json), LOCK_EX);
60 +/**
61 + * Restore Pluf from a backup.
62 + *
63 + * @param string Path to the backup folder
64 + * @param string Backup name
65 + * @return bool Success
66 + */
67 +function Admin_Migrations_Backup_restore($folder, $name)
69 + $models = array(
70 + 'Admin_Log',
71 + );
73 + $db = Pluf::db();
74 + $schema = new Admin_DB_Schema($db);
75 + foreach ($models as $model) {
76 + $schema->model = new $model();
77 + $schema->createTables();
78 + }
80 + $full_data = json_decode(file_get_contents(sprintf('%s/%s-Admin.json', $folder, $name)), true);
81 + foreach ($full_data as $model => $data) {
82 + Pluf_Test_Fixture::load($data, false);
83 + }
84 + return true;