xref: /plugin/sphinxsearch-was/PageMapper.php (revision 39:c93d111c37ff)
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    private $_offset = 0;
13    public function  __construct()
14    {
15        global $conf;
16        $this->_dbpath = DOKU_INC . $conf['savedir'] . '/sphinxsearch/'.$this->_database;
17
18        if (false != ($db = new PDO("sqlite:".$this->_dbpath))) {
19            $q = @$db->query("SELECT 1 FROM {$this->_table} limit 1");
20            if ($q === false) {
21                $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))");
22                if (!$result) {
23                    echo "\nPDO::errorInfo():\n";
24                    print_r($db->errorInfo());
25                    exit;
26                }
27
28            }
29        }
30        $this->_db = $db;
31    }
32
33    public function add($page, $title_text, $title, $hid='')
34    {
35        $result = $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title, title_text) values(".$this->_db->quote($page).",
36                                    '".crc32($page.$hid)."',
37                                    ".$this->_db->quote($hid).",
38                                    ".$this->_db->quote($title).",
39                                    ".$this->_db->quote($title_text).")");
40        if (!$result) {
41            //echo "\nPDO::errorInfo():\n";
42            //print_r($this->_db->errorInfo());
43        }
44    }
45
46    public function getAll()
47    {
48        $result = $this->_db->query("select * from {$this->_table}");
49        return $result->fetchAll(PDO::FETCH_ASSOC);
50    }
51
52    public function getByCrc($pageCrcList)
53    {
54        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
55        $result = $this->_db->query($sql);
56        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
57
58        $pages = array();
59        foreach($rows as $row){
60            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title'], 'title_text' => $row['title_text']);
61        }
62        $results = array();
63        foreach($pageCrcList as $crc){
64            $results[$crc] = $pages[$crc];
65        }
66        return $results;
67    }
68}
69