From c0dc531ebefd8912819f3b6c8bda1fed3c7e750c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 Jan 2024 06:11:00 +0300 Subject: make it simple, but not simpler --- model/Page.php | 43 ------------- model/Post.php | 193 ------------------------------------------------------- model/Tag.php | 24 ------- model/Upload.php | 163 ---------------------------------------------- 4 files changed, 423 deletions(-) delete mode 100644 model/Page.php delete mode 100644 model/Post.php delete mode 100644 model/Tag.php delete mode 100644 model/Upload.php (limited to 'model') diff --git a/model/Page.php b/model/Page.php deleted file mode 100644 index f516836..0000000 --- a/model/Page.php +++ /dev/null @@ -1,43 +0,0 @@ -md) - $data['html'] = markup::markdownToHtml($data['md']); - parent::edit($data); - } - - public function isUpdated(): bool { - return $this->updateTs && $this->updateTs != $this->ts; - } - - public function getHtml(bool $is_retina, string $user_theme): string { - $html = $this->html; - $html = markup::htmlImagesFix($html, $is_retina, $user_theme); - return $html; - } - - public function getUrl(): string { - return "/{$this->shortName}/"; - } - - public function updateHtml() { - $html = markup::markdownToHtml($this->md); - $this->html = $html; - getDb()->query("UPDATE pages SET html=? WHERE short_name=?", $html, $this->shortName); - } - -} diff --git a/model/Post.php b/model/Post.php deleted file mode 100644 index 6f3f1ab..0000000 --- a/model/Post.php +++ /dev/null @@ -1,193 +0,0 @@ -visible && $data['visible']) - $data['ts'] = $cur_ts; - - $data['update_ts'] = $cur_ts; - - if ($data['md'] != $this->md) { - $data['html'] = markup::markdownToHtml($data['md']); - $data['text'] = markup::htmlToText($data['html']); - } - - if ((isset($data['toc']) && $data['toc']) || $this->toc) { - $data['toc_html'] = markup::toc($data['md']); - } - - parent::edit($data); - $this->updateImagePreviews(); - } - - public function updateHtml() { - $html = markup::markdownToHtml($this->md); - $this->html = $html; - - getDb()->query("UPDATE posts SET html=? WHERE id=?", $html, $this->id); - } - - public function updateText() { - $html = markup::markdownToHtml($this->md); - $text = markup::htmlToText($html); - $this->text = $text; - - getDb()->query("UPDATE posts SET text=? WHERE id=?", $text, $this->id); - } - - public function getDescriptionPreview(int $len): string { - if (mb_strlen($this->text) >= $len) - return mb_substr($this->text, 0, $len-3).'...'; - return $this->text; - } - - public function getFirstImage(): ?Upload { - if (!preg_match('/\{image:([\w]{8})/', $this->md, $match)) - return null; - return uploads::getByRandomId($match[1]); - } - - public function getUrl(): string { - return $this->shortName != '' ? "/{$this->shortName}/" : "/{$this->id}/"; - } - - public function getDate(): string { - return date('j M', $this->ts); - } - - public function getYear(): int { - return (int)date('Y', $this->ts); - } - - public function getFullDate(): string { - return date('j F Y', $this->ts); - } - - public function getUpdateDate(): string { - return date('j M', $this->updateTs); - } - - public function getFullUpdateDate(): string { - return date('j F Y', $this->updateTs); - } - - public function getHtml(bool $is_retina, string $theme): string { - $html = $this->html; - $html = markup::htmlImagesFix($html, $is_retina, $theme); - return $html; - } - - public function getToc(): ?string { - return $this->toc ? $this->tocHtml : null; - } - - public function isUpdated(): bool { - return $this->updateTs && $this->updateTs != $this->ts; - } - - /** - * @return Tag[] - */ - public function getTags(): array { - $db = getDb(); - $q = $db->query("SELECT tags.* FROM posts_tags - LEFT JOIN tags ON tags.id=posts_tags.tag_id - WHERE posts_tags.post_id=? - ORDER BY posts_tags.tag_id", $this->id); - return array_map('Tag::create_instance', $db->fetchAll($q)); - } - - /** - * @return int[] - */ - public function getTagIds(): array { - $ids = []; - $db = getDb(); - $q = $db->query("SELECT tag_id FROM posts_tags WHERE post_id=? ORDER BY tag_id", $this->id); - while ($row = $db->fetch($q)) { - $ids[] = (int)$row['tag_id']; - } - return $ids; - } - - public function setTagIds(array $new_tag_ids) { - $cur_tag_ids = $this->getTagIds(); - $add_tag_ids = array_diff($new_tag_ids, $cur_tag_ids); - $rm_tag_ids = array_diff($cur_tag_ids, $new_tag_ids); - - $db = getDb(); - if (!empty($add_tag_ids)) { - $rows = []; - foreach ($add_tag_ids as $id) - $rows[] = ['post_id' => $this->id, 'tag_id' => $id]; - $db->multipleInsert('posts_tags', $rows); - } - - if (!empty($rm_tag_ids)) - $db->query("DELETE FROM posts_tags WHERE post_id=? AND tag_id IN(".implode(',', $rm_tag_ids).")", $this->id); - - $upd_tag_ids = array_merge($new_tag_ids, $rm_tag_ids); - $upd_tag_ids = array_unique($upd_tag_ids); - foreach ($upd_tag_ids as $id) - posts::recountPostsWithTag($id); - } - - /** - * @param bool $update Whether to overwrite preview if already exists - * @return int - */ - public function updateImagePreviews(bool $update = false): int { - $images = []; - if (!preg_match_all('/\{image:([\w]{8}),(.*?)}/', $this->md, $matches)) - return 0; - - for ($i = 0; $i < count($matches[0]); $i++) { - $id = $matches[1][$i]; - $w = $h = null; - $opts = explode(',', $matches[2][$i]); - foreach ($opts as $opt) { - if (strpos($opt, '=') !== false) { - list($k, $v) = explode('=', $opt); - if ($k == 'w') - $w = (int)$v; - else if ($k == 'h') - $h = (int)$v; - } - } - $images[$id][] = [$w, $h]; - } - - if (empty($images)) - return 0; - - $images_affected = 0; - $uploads = uploads::getUploadsByRandomId(array_keys($images), true); - foreach ($uploads as $u) { - foreach ($images[$u->randomId] as $s) { - list($w, $h) = $s; - list($w, $h) = $u->getImagePreviewSize($w, $h); - if ($u->createImagePreview($w, $h, $update, $u->imageMayHaveAlphaChannel())) - $images_affected++; - } - } - - return $images_affected; - } - -} diff --git a/model/Tag.php b/model/Tag.php deleted file mode 100644 index a8324f7..0000000 --- a/model/Tag.php +++ /dev/null @@ -1,24 +0,0 @@ -tag.'/'; - } - - public function getPostsCount(bool $is_admin): int { - return $is_admin ? $this->postsCount : $this->visiblePostsCount; - } - - public function __toString(): string { - return $this->tag; - } - -} diff --git a/model/Upload.php b/model/Upload.php deleted file mode 100644 index 06b348b..0000000 --- a/model/Upload.php +++ /dev/null @@ -1,163 +0,0 @@ -randomId; - } - - public function getDirectUrl(): string { - global $config; - return 'https://'.$config['uploads_host'].'/'.$this->randomId.'/'.$this->name; - } - - public function getDirectPreviewUrl(int $w, int $h, bool $retina = false): string { - global $config; - if ($w == $this->imageW && $this->imageH == $h) - return $this->getDirectUrl(); - - if ($retina) { - $w *= 2; - $h *= 2; - } - - $prefix = $this->imageMayHaveAlphaChannel() ? 'a' : 'p'; - return 'https://'.$config['uploads_host'].'/'.$this->randomId.'/'.$prefix.$w.'x'.$h.'.jpg'; - } - - // TODO remove? - public function incrementDownloads() { - $db = getDb(); - $db->query("UPDATE uploads SET downloads=downloads+1 WHERE id=?", $this->id); - $this->downloads++; - } - - public function getSize(): string { - return sizeString($this->size); - } - - public function getMarkdown(): string { - if ($this->isImage()) { - $md = '{image:'.$this->randomId.',w='.$this->imageW.',h='.$this->imageH.'}{/image}'; - } else if ($this->isVideo()) { - $md = '{video:'.$this->randomId.'}{/video}'; - } else { - $md = '{fileAttach:'.$this->randomId.'}{/fileAttach}'; - } - $md .= ' '; - return $md; - } - - public function setNote(string $note) { - $db = getDb(); - $db->query("UPDATE uploads SET note=? WHERE id=?", $note, $this->id); - } - - public function isImage(): bool { - return in_array(extension($this->name), self::$ImageExtensions); - } - - // assume all png images have alpha channel - // i know this is wrong, but anyway - public function imageMayHaveAlphaChannel(): bool { - return strtolower(extension($this->name)) == 'png'; - } - - public function isVideo(): bool { - return in_array(extension($this->name), self::$VideoExtensions); - } - - public function getImageRatio(): float { - return $this->imageW / $this->imageH; - } - - public function getImagePreviewSize(?int $w = null, ?int $h = null): array { - if (is_null($w) && is_null($h)) - throw new Exception(__METHOD__.': both width and height can\'t be null'); - - if (is_null($h)) - $h = round($w / $this->getImageRatio()); - - if (is_null($w)) - $w = round($h * $this->getImageRatio()); - - return [$w, $h]; - } - - public function createImagePreview(?int $w = null, - ?int $h = null, - bool $force_update = false, - bool $may_have_alpha = false): bool { - global $config; - - $orig = $config['uploads_dir'].'/'.$this->randomId.'/'.$this->name; - $updated = false; - - foreach (themes::getThemes() as $theme) { - if (!$may_have_alpha && $theme == 'dark') - continue; - - for ($mult = 1; $mult <= 2; $mult++) { - $dw = $w * $mult; - $dh = $h * $mult; - - $prefix = $may_have_alpha ? 'a' : 'p'; - $dst = $config['uploads_dir'].'/'.$this->randomId.'/'.$prefix.$dw.'x'.$dh.($theme == 'dark' ? '_dark' : '').'.jpg'; - - if (file_exists($dst)) { - if (!$force_update) - continue; - unlink($dst); - } - - $img = imageopen($orig); - imageresize($img, $dw, $dh, themes::getThemeAlphaColorAsRGB($theme)); - imagejpeg($img, $dst, $mult == 1 ? 93 : 67); - imagedestroy($img); - - setperm($dst); - $updated = true; - } - } - - return $updated; - } - - /** - * @return int Number of deleted files - */ - public function deleteAllImagePreviews(): int { - global $config; - $dir = $config['uploads_dir'].'/'.$this->randomId; - $files = scandir($dir); - $deleted = 0; - foreach ($files as $f) { - if (preg_match('/^[ap](\d+)x(\d+)(?:_dark)?\.jpg$/', $f)) { - if (is_file($dir.'/'.$f)) - unlink($dir.'/'.$f); - else - logError(__METHOD__.': '.$dir.'/'.$f.' is not a file!'); - $deleted++; - } - } - return $deleted; - } - -} -- cgit v1.2.3