forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
long_text_import.php
419 lines (389 loc) · 14.3 KB
/
long_text_import.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* \file
* \brief Long Text Import
*
* Call: long_text_import.php?...
* op=...
*
* PHP version 8.1
*
* @category User_Interface
* @package Lwt
* @author LWT Project <[email protected]>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/long-text-import.html
* @since 1.5.9
*/
require_once 'inc/session_utility.php';
/**
* Display the check page before a long text import.
*
* @param int $max_input_vars Maximale bytes size for the text.
*
* @return void
*/
function long_text_check($max_input_vars): void
{
$langid = (int) $_REQUEST["LgID"];
$title = (string) $_REQUEST["TxTitle"];
$paragraph_handling = (int) $_REQUEST["paragraph_handling"];
$maxsent = (int) $_REQUEST["maxsent"];
$source_uri = (string) $_REQUEST["TxSourceURI"];
$texttags = null;
if (isset($_REQUEST["TextTags"])) {
$texttags = json_encode($_REQUEST["TextTags"]);
}
// Get $data with \n line endings
if (isset($_FILES["thefile"])
&& $_FILES["thefile"]["tmp_name"] != ""
&& $_FILES["thefile"]["error"] == 0
) {
$data = file_get_contents($_FILES["thefile"]["tmp_name"]);
$data = str_replace("\r\n", "\n", $data);
} else {
$data = prepare_textdata($_REQUEST["Upload"]);
}
$data = replace_supp_unicode_planes_char($data);
$data = trim($data);
// Use ¶ symbol for paragraphs separation
if ($paragraph_handling == 2) {
$data = preg_replace('/\n\s*?\n/u', '¶', $data);
$data = str_replace("\n", ' ', $data);
} else {
$data = str_replace("\n", '¶', $data);
}
$data = preg_replace('/\s{2,}/u', ' ', $data);
$data = str_replace('¶ ', '¶', $data);
// Separate paragraphs by \n finally
$data = str_replace('¶', "\n", $data);
if ($data == "") {
$message = "Error: No text specified!";
echo error_message_with_hide($message, false);
} else {
$sent_array = splitCheckText($data, $langid, -2);
$texts = array();
$text_index = 0;
$texts[$text_index] = array();
$cnt = 0;
$bytes = 0;
foreach ($sent_array as $item) {
$item_len = strlen($item) + 1;
if ($item != '¶') {
$cnt++;
}
if ($cnt <= $maxsent && $bytes+$item_len < 65000) {
$texts[$text_index][] = $item;
$bytes += $item_len;
} else {
$text_index++;
$texts[$text_index] = array($item);
$cnt = 1;
$bytes = $item_len;
}
}
$textcount = count($texts);
$plural = ($textcount==1 ? '' : 's');
$shorter = ($textcount==1 ? ' ' : ' shorter ');
if ($textcount > $max_input_vars-20) {
$message = "Error: Too many texts (" . $textcount . " > " .
($max_input_vars-20) .
"). You must increase 'Maximum Sentences per Text'!";
echo error_message_with_hide($message, false);
} else {
?>
<script type="text/javascript">
//<![CDATA[
$(document).ready(lwtFormCheck.askBeforeExit);
lwtFormCheck.makeDirty();
//]]>
</script>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="LgID" value="<?php echo $langid; ?>" />
<input type="hidden" name="TxTitle" value="<?php echo tohtml($title); ?>" />
<input type="hidden" name="TxSourceURI" value="<?php echo tohtml($source_uri); ?>" />
<input type="hidden" name="TextTags" value="<?php echo tohtml($texttags); ?>" />
<input type="hidden" name="TextCount" value="<?php echo $textcount; ?>" />
<table class="tab1" cellspacing="0" cellpadding="5">
<tr>
<td class="td1" colspan="2">
<?php echo "This long text will be split into " . $textcount . $shorter . "text" . $plural . " - as follows:"; ?>
</td>
</tr>
<tr>
<td class="td1 right" colspan="2">
<input type="button" value="Cancel" onclick="{lwtFormCheck.resetDirty(); location.href='index.php';}" />
<span class="nowrap"></span>
<input type="button" value="Go Back" onclick="{lwtFormCheck.resetDirty(); history.back();}" />
<span class="nowrap"></span>
<input type="submit" name="op" value="Create <?php echo $textcount; ?> text<?php echo $plural; ?>" />
</td>
</tr>
<?php
$textno = -1;
foreach ($texts as $item) {
$textno++;
$textstring = str_replace("¶", "\n", implode(" ", $item));
$bytes = strlen($textstring);
?>
<tr>
<td class="td1 right">
<b>Text <?php echo $textno+1; ?>:</b>
<br /><br /><br />
Length:<br /><?php echo $bytes; ?><br />Bytes
</td>
<td class="td1">
<textarea readonly="readonly"
<?php echo getScriptDirectionTag($langid); ?>
name="text[<?php echo $textno; ?>]" cols="60" rows="10">
<?php echo $textstring; ?>
</textarea>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
}
}
/**
* Save a long text to the database.
*
* @return void
*
* @global string $tppref Database table prefix.
*/
function long_text_save(): void
{
global $tbpref;
$langid = (int) $_REQUEST["LgID"];
$title = $_REQUEST["TxTitle"];
$source_uri = $_REQUEST["TxSourceURI"];
if (isset($_REQUEST["TextTags"])) {
$_REQUEST["TextTags"] = json_decode($_REQUEST["TextTags"], true);
}
$textcount = (int)$_REQUEST["TextCount"];
$texts = $_REQUEST["text"];
if (count($texts) != $textcount ) {
$message = "Error: Number of texts wrong: " . count($texts) . " != " . $textcount;
} else {
$imported = 0;
for ($i = 0; $i < $textcount; $i++) {
$texts[$i] = remove_soft_hyphens($texts[$i]);
$counter = makeCounterWithTotal($textcount, $i+1);
$thistitle = $title . ($counter == '' ? '' : (' (' . $counter . ')'));
$imported += (int) runsql(
'insert into ' . $tbpref . 'texts (
TxLgID, TxTitle, TxText, TxAnnotatedText, TxAudioURI,
TxSourceURI
) values( ' .
$langid . ', ' .
convert_string_to_sqlsyntax($thistitle) . ', ' .
convert_string_to_sqlsyntax($texts[$i]) . ", '',
NULL, " .
convert_string_to_sqlsyntax($source_uri) .
')',
''
);
$id = get_last_key();
saveTextTags($id);
splitCheckText($texts[$i], $langid, $id);
}
$message = $imported . " Text(s) imported!";
}
echo error_message_with_hide($message, false);
?>
<p> <br /><input type="button" value="Show Texts" onclick="location.href='edit_texts.php';" /></p>
<?php
}
/**
* Display the main page for a long tex import.
*
* @param int $max_input_vars Maximal number of bytes for the text.
*
* @global $tbpref
*
* @return void
*/
function long_text_display($max_input_vars)
{
global $tbpref;
$sql = "SELECT LgID, LgGoogleTranslateURI FROM {$tbpref}languages
WHERE LgGoogleTranslateURI<>''";
$res = do_mysqli_query($sql);
$return = array();
while ($lg_record = mysqli_fetch_assoc($res)) {
$url = $lg_record["LgGoogleTranslateURI"];
$return[$lg_record["LgID"]] = langFromDict($url);
}
?>
<script type="text/javascript" charset="utf-8">
/**
* Change the language of inputs for text and title based on selected
* language.
*
* @returns undefined
*/
function change_textboxes_language() {
const lid = document.getElementById("TxLgID").value;
const language_data = <?php echo json_encode($return); ?>;
$('#TxTitle').attr('lang', language_data[lid]);
$('#TxText').attr('lang', language_data[lid]);
}
$(document).ready(lwtFormCheck.askBeforeExit);
$(document).ready(change_textboxes_language);
</script>
<div class="flex-spaced">
<div title="Import of a single text, max. 65,000 bytes long, with optional audio">
<a href="edit_texts.php?new=1">
<img src="icn/plus-button.png">
Short Text Import
</a>
</div>
<div>
<a href="do_feeds.php?page=1&check_autoupdate=1">
<img src="icn/plus-button.png">
Newsfeed Import
</a>
</div>
<div>
<a href="edit_texts.php?query=&page=1">
<img src="icn/drawer--plus.png">
Active Texts
</a>
</div>
<div>
<a href="edit_archivedtexts.php?query=&page=1">
<img src="icn/drawer--minus.png">
Archived Texts
</a>
</div>
</div>
<form enctype="multipart/form-data" class="validate" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table class="tab1" cellspacing="0" cellpadding="5">
<tr>
<td class="td1 right">Language:</td>
<td class="td1">
<select name="LgID" id="TxLgID" class="notempty setfocus" onchange="change_textboxes_language();">
<?php
echo get_languages_selectoptions(getSetting('currentlanguage'), '[Choose...]');
?>
</select>
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">Title:</td>
<td class="td1">
<input type="text" class="notempty checkoutsidebmp respinput"
data_info="Title" name="TxTitle" id="TxTitle" value="" maxlength="200" />
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">
Text:
</td>
<td class="td1">
Either specify a <b>File to upload</b>:<br />
<input name="thefile" type="file" /><br /><br />
<b>Or</b> paste a text from the clipboard
(and do <b>NOT</b> specify file):<br />
<textarea class="checkoutsidebmp respinput" data_info="Upload"
name="Upload" id="TxText" rows="15"></textarea>
<p class="smallgray">
If the text is too long, the import may not be possible. <wbr />
Current upload limits (in bytes):
<br />
<b>post_max_size</b>:
<?php echo ini_get('post_max_size'); ?>
<br />
<b>upload_max_filesize</b>:
<?php echo ini_get('upload_max_filesize'); ?>
<br />
If needed, increase in <wbr />"<?php echo tohtml(php_ini_loaded_file()); ?>" <wbr />
and restart the server.
</p>
</td>
</tr>
<tr>
<td class="td1 right">NEWLINES and paragraphs:</td>
<td class="td1">
<select name="paragraph_handling" class="respinput">
<option value="1" selected="selected">
ONE NEWLINE: Paragraph ends
</option>
<option value="2">
TWO NEWLINEs: Paragraph ends. Single NEWLINE converted to SPACE
</option>
</select>
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
</td>
</tr>
<tr>
<td class="td1 right">Maximum sentences per text:</td>
<td class="td1">
<input type="number" min="0" max="999" class="notempty posintnumber"
data_info="Maximum Sentences per Text" name="maxsent" value="50" maxlength="3" size="3" />
<img src="icn/status-busy.png" title="Field must not be empty" alt="Field must not be empty" />
<br />
<span class="smallgray">
Values higher than 100 may slow down text display.
Very low values (< 5) may result in too many texts.
<br />
The maximum number of new texts must not exceed <?php echo ($max_input_vars-20); ?>.
A single new text will never exceed the length of 65,000 bytes.
</span>
</td>
</tr>
<tr>
<td class="td1 right">Source URI:</td>
<td class="td1">
<input type="url" class="checkurl checkoutsidebmp respinput"
data_info="Source URI" name="TxSourceURI" value="" maxlength="1000" />
</td>
</tr>
<tr>
<td class="td1 right">Tags:</td>
<td class="td1">
<?php echo getTextTags(0); ?>
</td>
</tr>
<tr>
<td class="td1 right" colspan="2">
<input type="button" value="Cancel" onclick="{lwtFormCheck.resetDirty(); location.href='index.php';}" />
<input type="submit" name="op" value="NEXT STEP: Check the Texts" />
</td>
</tr>
</table>
</form>
<?php
}
/**
* Do the main page for the long text import.
*
* @return void
*/
function long_text_do_page(): void
{
pagestart('Long Text Import', true);
$max_input_vars = ini_get('max_input_vars');
if ($max_input_vars === false || $max_input_vars == '') {
$max_input_vars = 1000;
}
if (isset($_REQUEST['op'])) {
if (substr($_REQUEST['op'], 0, 5) == 'NEXT ') {
long_text_check($max_input_vars);
} elseif (substr($_REQUEST['op'], 0, 5) == 'Creat') {
long_text_save();
}
} else {
long_text_display($max_input_vars);
}
pageend();
}
long_text_do_page();
?>