forked from textpattern/textpattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxp_import.php
More file actions
321 lines (275 loc) · 9.56 KB
/
Copy pathtxp_import.php
File metadata and controls
321 lines (275 loc) · 9.56 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/*
* Textpattern Content Management System
* http://textpattern.com
*
* Copyright (C) 2015 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Import panel.
*
* @package Admin\Import
*/
if (!defined('txpinterface')) {
die('txpinterface is undefined.');
}
// TO-DO:
// * Improve performance of file imports
// * Test a php_ini format for blogger exports
// * Provide an Export option
// * Write best help
// Keep error display until we add an error handler for this.
error_reporting(E_ALL);
@ini_set("display_errors", "1");
require_privs('import');
/**
* Configuration variables.
*
* A list of HTTP POST variables.
*
* @global array $vars
*/
$vars = array(
'import_tool',
'import_section',
'import_status',
'import_comments_invite',
'import_blog_id',
'importdb',
'importdblogin',
'importdbpass',
'importdbhost',
'wpdbprefix',
'wpdbcharset',
);
/**
* Importing options.
*
* These are named after the files in ./import directory.
*
* @global array $tools
*/
$tools = array(
'' => '',
'mt' => 'Movable Type (File)',
'mtdb' => 'Movable Type (MySQL DB)',
'blogger' => 'Blogger',
'b2' => 'b2',
'wp' => 'WordPress',
);
if (!$step or !bouncer($step, array(
'switch_tool' => false,
'start_import' => true,
))) {
$step = 'switch_tool';
}
$step();
/**
* Renders a panel for selecting the import tool.
*
* Lets users select the tool and provide required
* configuration options.
*/
function switch_tool()
{
global $vars, $event, $step, $tools;
extract(gpsa($vars));
pagetop(gTxt('txp_import'), '');
echo hed(gTxt('tab_import'), 1, array('class' => 'txp-heading'));
$content = '<section class="txp-edit">';
$content .= hed(gTxt('txp_import'), 2);
// Select tool.
$content .= inputLabel('import_from', tag(type_options($tools), 'select', ' id="import_from" name="import_tool"'), 'select_tool', 'import');
// Some data we collect.
$content .= inputLabel('import_section', import_section_popup(''), 'import_section', 'import_section');
$status_options = array(
STATUS_LIVE => gTxt('live'),
STATUS_DRAFT => gTxt('draft'),
STATUS_HIDDEN => gTxt('hidden'),
STATUS_PENDING => gTxt('pending'),
);
$content .= inputLabel('import_status', tag(type_options($status_options), 'select', ' id="import_status"'), 'import_status', 'import_status');
$content .= inputLabel('import_comment', fInput('text', 'import_comments_invite', gTxt('comments'), '', '', '', INPUT_REGULAR, '', 'import_comment'), 'import_invite', 'import_invite');
// Database imports only.
$databased =
hed(gTxt('database_stuff'), 2).
inputLabel('import_database', fInput('text', 'importdb', '', '', '', '', INPUT_REGULAR, '', 'import_database'), 'import_database', 'import_database').
inputLabel('import_login', fInput('text', 'importdblogin', '', '', '', '', INPUT_REGULAR, '', 'import_login'), 'import_login', 'import_login').
inputLabel('import_password', fInput('text', 'importdbpass', '', '', '', '', INPUT_REGULAR, '', 'import_password'), 'import_password', 'import_password').
inputLabel('import_host', fInput('text', 'importdbhost', '', '', '', '', INPUT_REGULAR, '', 'import_host'), 'import_host', 'import_host');
$content .= tag($databased, 'div', ' id="databased" style="display: none;"');
// Movable Type (MySQL DB) specific.
$mtblogid = inputLabel('import_blogid', fInput('text', 'import_blog_id', '', '', '', '', INPUT_REGULAR, '', 'import_blogid'), 'import_blogid', 'import_blogid');
$content .= tag($mtblogid, 'div', ' id="mtblogid" style="display: none;"');
// WordPress specific.
$wponly = inputLabel('import_wpprefix', fInput('text', 'wpdbprefix', 'wp_', '', '', '', INPUT_REGULAR, '', 'import_wpprefix'), 'import_wpprefix', 'import_wpprefix').
inputLabel('import_wpdbcharset', selectInput('wpdbcharset', array('utf8' => gTxt('utf8'), 'latin1' => gTxt('latin1')), 'utf8', '', '', 'import_wpdbcharset'), 'import_wpdbcharset', 'import_wpdbcharset');
$content .= tag($wponly, 'div', ' id="wponly" style="display: none;"');
$content .= graf(fInput('submit', 'choose', gTxt('continue'), 'publish'));
$content .= sInput('start_import').eInput('import');
$content .= '</section>';
echo '<div id="'.$event.'_container" class="txp-container">'.
form($content, '', '', 'post', '', '', 'import').
'</div>';
}
/**
* Processes the selected import tool action.
*
* Basically does the importing.
*/
function start_import()
{
global $event, $vars;
extract(psa($vars));
$insert_into_section = $import_section;
$insert_with_status = $import_status;
$default_comment_invite = $import_comments_invite;
include_once txpath.'/include/import/import_'.$import_tool.'.php';
$ini_time = ini_get('max_execution_time');
@ini_set('max_execution_time', 300 + intval($ini_time));
switch ($import_tool) {
case 'mtdb':
$out = doImportMTDB(
$importdblogin,
$importdb,
$importdbpass,
$importdbhost,
$import_blog_id,
$insert_into_section,
$insert_with_status,
$default_comment_invite
);
rebuild_tree('root', 1, 'article');
break;
case 'mt':
$file = check_import_file();
if (!empty($file)) {
$out = doImportMT(
$file,
$insert_into_section,
$insert_with_status,
$import_comments_invite
);
// Rebuilding category tree.
rebuild_tree('root', 1, 'article');
} else {
$out = 'Import file not found';
}
break;
case 'b2':
$out = doImportB2(
$importdblogin,
$importdb,
$importdbpass,
$importdbhost,
$insert_into_section,
$insert_with_status,
$default_comment_invite
);
break;
case 'wp':
$out = doImportWP(
$importdblogin,
$importdb,
$importdbpass,
$importdbhost,
$wpdbprefix,
$insert_into_section,
$insert_with_status,
$default_comment_invite,
$wpdbcharset
);
rebuild_tree('root', 1, 'article');
break;
case 'blogger':
$file = check_import_file();
if (!empty($file)) {
$out = doImportBLOGGER(
$file,
$insert_into_section,
$insert_with_status,
$import_comments_invite
);
} else {
$out = gTxt('import_file_not_found');
}
break;
}
$out = tag('max_execution_time = '.ini_get('max_execution_time'), 'p', ' class="highlight"').$out;
pagetop(gTxt('txp_import'));
$content = '<div id="'.$event.'_container" class="txp-container">';
$content .= startTable('', '', 'txp-list');
$content .= tr(tdcs(hed(gTxt('txp_import'), 2), 2));
$content .= tr(td($out));
$content .= endTable();
$content .= '</div>';
echo $content;
$rs = safe_rows_start('parentid, count(*) as thecount', 'txp_discuss', 'visible=1 group by parentid');
if (numRows($rs) > 0) {
while ($a = nextRow($rs)) {
safe_update('textpattern', "comments_count=".$a['thecount'], "ID=".$a['parentid']);
}
}
}
/**
* Checks the existence of a file called 'import.txt' in the 'import' directory.
*
* This function is used when importing from a file.
*
* @return string Path to the file, or an empty string
*/
function check_import_file()
{
// Check file size here too, and explain how to split the file if size is
// too long and time_limit can not be altered.
$import_file = txpath.'/include/import/import.txt';
if (!is_file($import_file)) {
// trigger_error('Import file not found', E_USER_WARNING);
return '';
}
return $import_file;
}
/**
* Quotes a string or an array of values with slashes.
*
* Returns the input value with backslashes added before
* any ' or " characters.
*
* @param array|string $in The input value
* @return mixed
* @access private
* @see addslashes()
*/
function array_slash($in)
{
return is_array($in) ? array_map('addslashes', $in) : addslashes($in);
}
/**
* Renders a <select> input containing all available sections.
*
* @param string $Section The selected option
* @return string HTML
* @access private
*/
function import_section_popup($Section)
{
$rs = safe_column("name", "txp_section", "name!='default'");
if ($rs) {
return selectInput("import_section", $rs, $Section, 1, '', 'import_section');
}
return false;
}