xref: /plugin/sphinxsearch-was/PageMapper.php (revision 27:fbc14c95b41d)
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, $rpp)
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        $counter = 1;
59        foreach($rows as $row){
60            if(auth_quickaclcheck($row['page']) >= AUTH_READ){
61                $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title'], 'title_text' => $row['title_text']);
62                if ($counter++ == $rpp){
63                    break;
64                }
65            }
66        }
67        return $pages;
68    }
69}
70