aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/extractor/sqlite3_extractor.php
blob: ce8da6a65246ab04e2d7db663a4b1fa43e9138c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
index 1cf139c..d885d50 100644
--- a/
MANIFEST
+++ b/MANIFEST
@@ -18,6 +18,8 @@ extras/desktop/manauserlog.desktop
extras/desktop/manawall.desktop
extras/desktop/mpan.desktop
extras/desktop/rpmdragora.desktop
+extras/module_templete/mana_mt
+extras/module_templete/ManaTools/Module/ModuleTemplate.pm
extras/polkit/org.mageia.dragoraupdate.policy
extras/polkit/org.mageia.manaclock.policy
extras/polkit/org.mageia.manadm.policy
='#n131'>131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\db\extractor;

use phpbb\db\extractor\exception\extractor_not_initialized_exception;

class sqlite3_extractor extends base_extractor
{
	/**
	* {@inheritdoc}
	*/
	public function write_start($table_prefix)
	{
		if (!$this->is_initialized)
		{
			throw new extractor_not_initialized_exception();
		}

		$sql_data = "--\n";
		$sql_data .= "-- phpBB Backup Script\n";
		$sql_data .= "-- Dump of tables for $table_prefix\n";
		$sql_data .= "-- DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
		$sql_data .= "--\n";
		$sql_data .= "BEGIN TRANSACTION;\n";
		$this->flush($sql_data);
	}

	/**
	* {@inheritdoc}
	*/
	public function write_table($table_name)
	{
		if (!$this->is_initialized)
		{
			throw new extractor_not_initialized_exception();
		}

		$sql_data = '-- Table: ' . $table_name . "\n";
		$sql_data .= "DROP TABLE $table_name;\n";

		$sql = "SELECT sql
			FROM sqlite_master
			WHERE type = 'table'
				AND name = '" . $this->db->sql_escape($table_name) . "'
			ORDER BY name ASC;";
		$result = $this->db->sql_query($sql);
		$row = $this->db->sql_fetchrow($result);
		$this->db->sql_freeresult($result);

		// Create Table
		$sql_data .= $row['sql'] . ";\n";

		$result = $this->db->sql_query("PRAGMA index_list('" . $this->db->sql_escape($table_name) . "');");

		while ($row = $this->db->sql_fetchrow($result))
		{
			if (strpos($row['name'], 'autoindex') !== false)
			{
				continue;
			}

			$result2 = $this->db->sql_query("PRAGMA index_info('" . $this->db->sql_escape($row['name']) . "');");

			$fields = array();
			while ($row2 = $this->db->sql_fetchrow($result2))
			{
				$fields[] = $row2['name'];
			}
			$this->db->sql_freeresult($result2);

			$sql_data .= 'CREATE ' . ($row['unique'] ? 'UNIQUE ' : '') . 'INDEX ' . $row['name'] . ' ON ' . $table_name . ' (' . implode(', ', $fields) . ");\n";
		}
		$this->db->sql_freeresult($result);

		$this->flush($sql_data . "\n");
	}

	/**
	* {@inheritdoc}
	*/
	public function write_data($table_name)
	{
		if (!$this->is_initialized)
		{
			throw new extractor_not_initialized_exception();
		}

		$result = $this->db->sql_query("PRAGMA table_info('" . $this->db->sql_escape($table_name) . "');");

		$col_types = array();
		while ($row = $this->db->sql_fetchrow($result))
		{
			$col_types[$row['name']] = $row['type'];
		}
		$this->db->sql_freeresult($result);

		$sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES (';

		$sql = "SELECT *
			FROM $table_name";
		$result = $this->db->sql_query($sql);

		while ($row = $this->db->sql_fetchrow($result))
		{
			foreach ($row as $column_name => $column_data)
			{
				if (is_null($column_data))
				{
					$row[$column_name] = 'NULL';
				}
				else if ($column_data === '')
				{
					$row[$column_name] = "''";
				}
				else if (stripos($col_types[$column_name], 'text') !== false || stripos($col_types[$column_name], 'char') !== false || stripos($col_types[$column_name], 'blob') !== false)
				{
					$row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data));
				}
			}
			$this->flush($sql_insert . implode(', ', $row) . ");\n");
		}
	}

	/**
	* Writes closing line(s) to database backup
	*
	* @return null
	* @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
	*/
	public function write_end()
	{
		if (!$this->is_initialized)
		{
			throw new extractor_not_initialized_exception();
		}

		$this->flush("COMMIT;\n");
		parent::write_end();
	}
}