9 protected $config = null;
\r
12 public function __construct()
\r
14 $this->config = Tagebuch_Config::get();
\r
22 * Returns a post object from the given file.
\r
24 * @param string $file File name without path
\r
26 * @return Tagebuch_Post or boolean false if not found
\r
28 public function getPost($file)
\r
30 $stmt = $this->db->prepare('SELECT * FROM posts WHERE file = ?');
\r
31 $stmt->execute(array($file));
\r
32 return $stmt->fetchObject('Tagebuch_Post');
\r
37 public function getPosts($tag = null, $limit = null)
\r
39 if ($tag === null) {
\r
42 $tag = '%,' . $tag . ',%';
\r
44 $stmt = $this->db->prepare(
\r
45 'SELECT * FROM posts'
\r
46 . ' WHERE tags LIKE ?'
\r
47 . ' ORDER BY modified DESC'
\r
48 . ($limit !== null ? ' LIMIT ' . $limit : '')
\r
50 $stmt->execute(array($tag));
\r
51 return $stmt->fetchAll(PDO::FETCH_CLASS, 'Tagebuch_Post');
\r
57 * Fetches the next and previous posts from the database.
\r
59 * @param string $file Filename to fetch
\r
60 * @param string $tag Tag name that determines the file list
\r
61 * - may be null for no tag
\r
63 * @return array Array with prev and next keys, post objects
\r
64 * as value. NULL when empty
\r
66 public function getPrevNext($file, $tag = null)
\r
68 if ($tag === null) {
\r
71 $tag = '%,' . $tag . ',%';
\r
73 $qfile = $this->db->quote($file);
\r
74 $qtag = $this->db->quote($tag);
\r
75 list($date) = $this->db->query(
\r
76 'SELECT modified FROM posts WHERE file LIKE '
\r
80 $qdate = $this->db->quote($date);
\r
83 ->query('SELECT * FROM posts'
\r
84 . ' WHERE modified < ' . $qdate
\r
85 . ' AND tags LIKE ' . $qtag
\r
86 . ' ORDER BY modified DESC LIMIT 1'
\r
87 )->fetchObject('Tagebuch_Post');
\r
89 ->query('SELECT * FROM posts'
\r
90 . ' WHERE modified > ' . $qdate
\r
91 . ' AND tags LIKE ' . $qtag
\r
92 . ' ORDER BY modified ASC LIMIT 1'
\r
93 )->fetchObject('Tagebuch_Post');
\r
104 * Returns an array of all tags
\r
106 public function getTags()
\r
108 $stmt = $this->db->query('SELECT tags FROM posts');
\r
110 while ($row = $stmt->fetch(PDO::FETCH_OBJ, PDO::FETCH_ORI_NEXT)) {
\r
111 $rtags = array_flip(explode(',', substr($row->tags, 1, -1)));
\r
112 $tags = array_merge($tags, $rtags);
\r
116 return array_keys($tags);
\r
123 * Prepares the table structure
\r
127 protected function prepare()
\r
129 $this->db = new PDO('sqlite::memory:');
\r
130 $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
\r
131 $this->db->exec("CREATE TABLE posts(
\r
146 * Loads raw files into database
\r
150 protected function loadFiles()
\r
152 $query = $this->db->prepare(
\r
153 'insert into posts (title,file,size,created,modified,tags,license)'
\r
154 . ' VALUES(:title,:file,:size,:created,:modified,:tags,:license)'
\r
156 $query->bindParam(':title', $title);
\r
157 $query->bindParam(':file', $filename);
\r
158 $query->bindParam(':size', $size);
\r
159 $query->bindParam(':created', $crdate);
\r
160 $query->bindParam(':modified', $mdate);
\r
161 $query->bindParam(':tags', $tags);
\r
162 $query->bindParam(':license', $license);
\r
164 foreach ($this->getRawFiles() as $file) {
\r
165 $hp = new Tagebuch_HtmlPost($file);
\r
166 $crdate = $hp->getCreationDate();
\r
167 $mdate = $hp->getModificationDate();
\r
168 $tags = ',' . implode(
\r
172 $title = $hp->getTitle();
\r
173 $filename = basename($file);
\r
174 $size = filesize($file);
\r
175 $license = $hp->getLicense();
\r
183 * Returns an array full of raw filename paths
\r
185 * @return array Array of raw file names
\r
187 protected function getRawFiles()
\r
189 return glob($this->config->raw . '*.htm');
\r