xref: /plugin/sphinxsearch-was/PageMapper.php (revision 1:256c33d4991a)
1<?php
2/*
3 * To change this template, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7class PageMapper
8{
9    private $_database = 'pagedata';
10    private $_table = 'pages';
11    private $_db = null;
12    public function  __construct()
13    {
14        global $conf;
15        $this->_dbpath = DOKU_INC . $conf['savedir'] . '/sphinxsearch/'.$this->_database;
16
17        if (false != ($db = new PDO("sqlite:".$this->_dbpath))) {
18            $q = @$db->query("SELECT 1 FROM {$this->_table} limit 1");
19            if ($q === false) {
20                $db->query("CREATE TABLE {$this->_table} (page varchar(1024), page_crc int(11))");
21            }
22        }
23        $this->_db = $db;
24    }
25
26    public function add($page)
27    {
28        $this->_db->query("REPLACE into {$this->_table}(page, page_crc) values('{$page}', '".crc32($page)."')");
29    }
30
31    public function getAll()
32    {
33        $result = $this->_db->query("select * from {$this->_table}");
34        return $result->fetchAll($query);
35    }
36
37    public function getByCrc($pageCrcList)
38    {
39        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
40        $result = $this->_db->query($sql);
41        $rows = $result->fetchAll($query);
42        $pages = array();
43        foreach($rows as $row){
44            $pages[$row['page_crc']] = $row['page'];
45        }
46        return $pages;
47    }
48}
49