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