/
wet_sql2php.php
111 lines (89 loc) · 2.5 KB
/
wet_sql2php.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/* $LastChangedRevision: $ */
$plugin['version'] = '0.7';
$plugin['author'] = 'Robert Wetzlmayr';
$plugin['author_uri'] = 'http://wetzlmayr.com/';
$plugin['description'] = 'Export SQL as PHP source code';
$plugin['type'] = 3;
@include_once('zem_tpl.php');
if (0) {
?>
# --- BEGIN PLUGIN HELP ---
h3. Purpose
Exports the current template into PHP code for txpsql.php. Textpattern uses this for generating the default site during setup.
h3. Licence
This plugin is released under the "Gnu General Public Licence":http://www.gnu.org/licenses/gpl.txt.
# --- END PLUGIN HELP ---
<?php
}
# --- BEGIN PLUGIN CODE ---
class wet_sql2php
{
static $template = '/setup/txpsql.php';
static $what = array(
'txp_css' => array('name', 'css'),
'txp_form' => array('name', 'type', 'Form'),
'txp_page' => array('name', 'user_html'),
);
static $where = '$create_sql[]';
/**
* Hook UI, setup privileges
*/
function __construct()
{
if (txpinterface == 'admin') {
add_privs('wet_sql2php', '1');
register_tab('presentation', 'wet_sql2php', gTxt('wet_sql2php'));
register_callback(array(__CLASS__, 'ui'), 'wet_sql2php');
}
}
/**
* User interface
*/
static function ui($event, $step)
{
global $step;
pagetop(gTxt(__CLASS__));
$available_steps = array(
'export'
);
if (!$step or !in_array($step, $available_steps)){
$step = 'export';
}
self::$step();
}
/**
* Weave the current template and show it ready to paste.
*/
static function export()
{
$f = file_get_contents(txpath.self::$template);
foreach (self::$what as $table => $columns) {
$tick = '`';
$cols = (empty($columns) ? '*' : $tick.join('`,`', doSlash($columns)).$tick);
$rs = safe_rows($cols, $table, (empty($columns) ? '1=1' : $columns[0].' not like \'%.min%\'') . (empty($columns) ? '' : ' ORDER BY `'.$columns[0].'`'));
$rows = array();
foreach ($rs as $a) {
// Enforce *nix new-lines
$a = str_replace("\r\n", "\n", $a);
// Literal backslash into corresponding MySQL literal
foreach ($a as &$v) {
$v = addcslashes(addcslashes($v, '\\'), '\\');
}
$a = "'" . join("', '", doSlash($a)) . "'";
$rows[] = self::$where.' = "INSERT INTO `".PFX."'.$table.'`('.$cols.') VALUES('.$a.')";';
}
$f = preg_replace("#(// sql:$table).*(// /sql:$table)#s", '$1'.n. join(n, $rows) .n.'$2', $f);
}
echo text_area('code', 600, '', $f, 'code');
echo script_js(<<<EOS
$('#code').focus(function() {
this.select();
});
EOS
);
}
}
new wet_sql2php;
# --- END PLUGIN CODE ---
?>