importing article and some fixes

This commit is contained in:
E. S. 2025-02-08 04:39:55 +03:00
parent 7361b3005f
commit f53f467e0b
3 changed files with 110 additions and 3 deletions

View File

@ -305,7 +305,7 @@ class posts {
$db->query("DELETE FROM posts WHERE id=?", $post->id);
$text_ids = [];
$q = $db->query("SELECT id FROM posts_texts WHERE post_id=?");
$q = $db->query("SELECT id FROM posts_texts WHERE post_id=?", $post->id);
while ($row = $db->fetch($q))
$text_ids = $row['id'];
previous_texts::delete(PreviousText::TYPE_POST_TEXT, $text_ids);

View File

@ -19,7 +19,11 @@ class uploads {
return in_array($ext, UPLOADS_ALLOWED_EXTENSIONS);
}
static function add(string $tmp_name, string $name, string $note_en, string $note_ru, string $source_url = ''): ?int {
static function add(string $tmp_name,
string $name,
string $note_en = '',
string $note_ru = '',
string $source_url = ''): ?int {
global $config;
$name = sanitize_filename($name);
@ -61,7 +65,7 @@ class uploads {
chmod($dir, 0775); // g+w
rename($tmp_name, $path);
chmod($path, 0664); // g+w
setperm($path);
return $id;
}
@ -138,6 +142,16 @@ class uploads {
}
}
static function getUploadBySourceUrl(string $source_url): ?Upload {
$db = DB();
$q = $db->query("SELECT * FROM uploads WHERE source_url=? LIMIT 1", $source_url);
if ($db->numRows($q)) {
return new Upload($db->fetch($q));
} else {
return null;
}
}
static function _getNewUploadRandomId(): string {
$db = DB();
do {

93
tools/import_article.php Normal file
View File

@ -0,0 +1,93 @@
#!/usr/bin/env php
<?php
require_once __DIR__.'/../init.php';
$options = getInput();
$content = [];
$langs = ['ru', 'en'];
foreach ($langs as $lang) {
checkFile($options[$lang]);
$content[$lang] = processImages(file_get_contents($options[$lang]));
}
$post = posts::add([
'keywords' => '',
'visible' => false,
'short_name' => $options['short-name'],
'date' => '2025-01-01',
'source_url' => '',
]);
if (!$post)
cli::die("failed to create post");
foreach ($langs as $lang) {
$text = $post->addText(
lang: PostLanguage::from($lang),
title: $options[$lang.'-title'],
md: $content[$lang],
keywords: '',
toc: false);
if (!$text) {
posts::delete($post);
cli::die("failed to create post text");
}
}
echo "done\n";
exit(0);
function getInput() {
global $argv;
$usage = "usage: $argv[0] --ru ./ru.txt --en ./en.txt --ru-title TITLE --en-title TITLE\n";
$option_names = ['ru', 'en', 'ru-title', 'en-title', 'short-name'];
$options = getopt('', array_map(fn($o) => $o.':', $option_names));
foreach ($option_names as $option_name) {
if (!isset($options[$option_name])) {
fwrite(STDERR, "error: missing option '$option_name'\n");
fwrite(STDERR, $usage);
exit(1);
}
}
return $options;
}
function checkFile($file) {
if (!file_exists($file)) {
fwrite(STDERR, "error: file $file does not exist\n");
exit(1);
}
}
function processImages($md) {
return preg_replace_callback(
'/!\[.*?\]\((https?:\/\/[^\s)]+)\)/',
function ($matches) {
$url = $matches[1];
$parsed_url = parse_url($url);
$clean_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];
$upload = uploads::getUploadBySourceUrl($clean_url);
if (!$upload) {
$name = basename($clean_url);
$ext = extension($clean_url);
$tmp = sys_get_temp_dir().'/'.uniqid(rand(), true).'.'.$ext;
if (!copy($clean_url, $tmp)) {
logError('failed to download '.$clean_url.' to '.$tmp);
return $matches[0];
}
$upload_id = uploads::add($tmp, $name, source_url: $clean_url);
$upload = uploads::get($upload_id);
// $tmp file has already been deleted by uploads::add() at this point
} else {
logDebug('found existing upload with source_url='.$clean_url);
}
return $upload->getMarkdown();
},
$md
);
}