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