xref: /plugin/sphinxsearch-was/PageMapper.php (revision 12:0f4db8442a6a)
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), hid varchar(1024), title varchar (1024), unique index a on pages(page, page_crc))");
21            }
22        }
23        $this->_db = $db;
24    }
25
26    public function add($page, $title = '', $hid='')
27    {
28        $result = $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title) values(".$this->_db->quote($page).",
29                                    '".crc32($page.$hid)."',
30                                    ".$this->_db->quote($hid).",
31                                    ".$this->_db->quote($title).")");
32        if (!$result) {
33            //echo "\nPDO::errorInfo():\n";
34            //print_r($this->_db->errorInfo());
35        }
36    }
37
38    public function getAll()
39    {
40        $result = $this->_db->query("select * from {$this->_table}");
41        return $result->fetchAll(PDO::FETCH_ASSOC);
42    }
43
44    public function getByCrc($pageCrcList)
45    {
46        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
47        $result = $this->_db->query($sql);
48        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
49
50        $pages = array();
51        foreach($rows as $row){
52            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title']);
53        }
54        return $pages;
55    }
56}
57