9cd885a89760b0ca0011a778e4b8aa63b9f43783
[infodrom/musiikki-web.git] / class / cache.class.php
1 <?php
2
3 class Cache {
4     public function __construct()
5     {
6         $cache = Config::main()->get('cache');
7
8         if (is_null($cache))
9             throw new Exception('Cache file not found');
10
11         $this->db = new PDO('sqlite:/'.$cache);
12     }
13
14     public function findTitles($keyword)
15     {
16         $media_dir = Config::main()->get('media_dir_A');
17         $results = array('count' => 0, 'list' => []);
18         $sql = sprintf("SELECT path, title, timestamp, size FROM details WHERE path LIKE %s AND mime IS NOT NULL",
19                        $this->db->quote('%'.$keyword.'%'));
20
21         $sth = $this->db->query($sql);
22         if ($sth === false) return $results;
23
24         $seen = [];
25         while ($row = $sth->fetchObject()) {
26             $id = "{$row->TIMESTAMP}-{$row->SIZE}";
27             if (!array_key_exists($id, $seen)) {
28                 $results['count']++;
29                 $seen[$id] = true;
30             }
31             $section = '[empty]';
32             $path = substr($row->PATH, strlen($media_dir)+1);
33
34             if (($pos = strrpos($path, '.')) > strlen($path)-10)
35                 $path = substr($path, 0, $pos);
36
37             if (($pos = strpos($path, '/')) < 20) {
38                 $section = substr($path, 0, $pos);
39                 $path = substr($path, $pos+1);
40             }
41
42             if (!array_key_exists($section, $results['list']))
43                 $results['list'][$section] = [];
44             $results['list'][$section][] = ['path' => dirname($path), 'name' => basename($path), 'title' => $row->TITLE];
45         }
46
47         return $results;
48     }
49 }