diff --git a/lib/posts.php b/lib/posts.php index cfc60fe..563dc5c 100644 --- a/lib/posts.php +++ b/lib/posts.php @@ -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); diff --git a/lib/uploads.php b/lib/uploads.php index ccb368d..1743d01 100644 --- a/lib/uploads.php +++ b/lib/uploads.php @@ -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 { diff --git a/tools/import_article.php b/tools/import_article.php new file mode 100644 index 0000000..51dfc89 --- /dev/null +++ b/tools/import_article.php @@ -0,0 +1,93 @@ +#!/usr/bin/env php + '', + '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 + ); +} \ No newline at end of file