File size: 3,845 Bytes
7731906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
namespace App\Models;

class PdfBook {
    private static function assetsDir() {
        return __DIR__ . '/../../public/assets';
    }   

    public static function getBooks() {
        $path = self::assetsDir() . '/books.json';
        if (!file_exists($path)) return [];
        $books = json_decode(file_get_contents($path), true);
        foreach ($books as &$book) {
            $slug = $book['slug'];
            $infoPath = self::assetsDir() . '/' . $slug . '/book.json';
            if (file_exists($infoPath)) {
                $meta = json_decode(file_get_contents($infoPath), true);
                $book['title'] = $meta['title'] ?? $slug;
            } else {
                $book['title'] = !empty($book['file']) ? self::titleFromFile($book['file']) : $slug;
            }
        }
        return $books;
    } 

    private static function getDataDir($slug) {
        $dir = self::assetsDir() . '/' . $slug;
        if (!is_dir($dir)) return null;
        return $dir;
    }

    public static function getInfo($slug) {
        $dir = self::getDataDir($slug);
        if (!$dir) return null;
        $path = $dir . '/book.json';
        if (!file_exists($path)) return null;
        $info = json_decode(file_get_contents($path), true);

        return $info;
    } 
 
    private static function fixLaoText($text) {
        $text = preg_replace('/(?<=[\x{E81}-\x{EAE}]) (?=[\x{EB9}\x{EB8}])/u', '', $text);
        $text = preg_replace('/(?<=[\x{E81}-\x{EAE}]) (?=[\x{EC8}\x{EC9}])/u', "\x{EB9}", $text);
        $text = preg_replace('/\x{EAA} \x{E95}/u', "", $text);
        $text = preg_replace('/\x{E9E} \x{EA1}/u', "\x{E9E}\x{EB9}\x{EA1}", $text);
        $text = str_replace("\x{E84}\x{EB2}\x{E99}\x{EB2}", "\x{E84}\x{EB3}\x{E99}\x{EB3}", $text);
        return $text;
    }

    public static function getAll($slug) {
        $dir = self::getDataDir($slug);
        if (!$dir) return [];
        $path = $dir . '/pages.json';
        if (!file_exists($path)) return [];
        $pages = json_decode(file_get_contents($path), true);
        foreach ($pages as &$page) {
            $page['text'] = self::fixLaoText($page['text']);
        }
        return $pages;
    }

    public static function getPage($slug, $pageNum) {
        $all = self::getAll($slug);
        foreach ($all as $p) {
            if ($p['page'] == $pageNum) return $p;
        }
        return null;
    }

    public static function search($slug, $query) {
        $all = self::getAll($slug);
        if (empty($query)) return [];

        $q = mb_strtolower(trim($query));
        $results = [];

        foreach ($all as $page) {
            $text = mb_strtolower($page['text']);
            $pos = mb_strpos($text, $q);
            if ($pos !== false) {
                $snippet = self::getSnippet($page['text'], $pos, 120);
                $results[] = [
                    'page' => $page['page'],
                    'snippet' => $snippet,
                    'matches' => mb_substr_count($text, $q),
                ];
            }
            if (count($results) >= 100) break;
        }

        return $results;
    }

    private static function getSnippet($text, $pos, $length = 120) {
        $textLen = mb_strlen($text);
        $start = max(0, $pos - intval($length / 2));
        $end = min($textLen, $start + $length);
        if ($start > 0) {
            $snippet = '...' . mb_substr($text, $start, $end - $start);
        } else {
            $snippet = mb_substr($text, $start, $end - $start);
        }
        if ($end < $textLen) $snippet .= '...';
        return $snippet;
    }

    private static function titleFromFile($file) {
        $basename = basename($file);
        $basename = preg_replace('/\.(pdf|docx|doc)$/iu', '', $basename);
        $basename = trim($basename);
        return $basename;
    }
}