source: extensions/database_migration/include/tab_export.inc.php @ 6552

Last change on this file since 6552 was 6222, checked in by nikrou, 14 years ago

Database migration plugin : first public version

File size: 2.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | database_migration - a plugin for Piwigo                              |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2010 Nicolas Roudaire             http://www.nikrou.net  |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License version 2 as     |
9// | published by the Free Software Foundation                             |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            |
19// | MA 02110-1301 USA                                                     |
20// +-----------------------------------------------------------------------+
21
22if (empty($_SESSION['dm_export_done']) || !empty($_GET['action'])) {
23  $structure_file = PHPWG_ROOT_PATH .'/install/piwigo_structure-mysql.sql';
24  $content = file_get_contents($structure_file);
25  $pattern = '!CREATE TABLE `([^`]*)`!';
26  preg_match_all($pattern, $content, $matches);
27
28  $destination_dir = $conf['local_data_dir'] . '/plugins/database_migration';
29  mkgetdir($destination_dir);
30  $export = array();
31
32  foreach ($matches[1] as $table) {
33    $filename = sprintf('%s/%s.dump', $destination_dir, $table);
34    $fh = fopen($filename, 'w+');
35    $query = '
36SELECT * FROM '.$table;
37    $result = pwg_query($query);
38    $export[] = array('table' => $table, 'lines_count' => mysql_num_rows($result));
39    while ($row = pwg_db_fetch_assoc($result)) {
40      $insert = '
41INSERT INTO '.$table.' (';
42      $insert_values = ') VALUES (';
43
44      foreach ($row as $key => $value) {
45        $insert .= $key.', ';
46        if (is_null($value)) {
47          $insert_values .= 'null, ';
48        } else {
49          $insert_values .= '\''.$value.'\', ';
50        }
51      }
52      $insert = substr($insert, 0, -2);
53      $insert_values = substr($insert_values, 0, -2);
54      fputs($fh, $insert.$insert_values.');');
55    }
56    fclose($fh);
57  }
58
59  $_SESSION['dm_export_done'] = true;
60  $GLOBALS['template']->assign('EXPORT', $export);
61} else {
62  $GLOBALS['template']->assign('EXPORT_DONE', true);
63  $GLOBALS['template']->assign('U_FORCE_EXPORT', $dm_base_url.'&amp;tab=export&amp;action=force');
64}
65?>
Note: See TracBrowser for help on using the repository browser.