xref: /plugin/sphinxsearch-was/PageMapper.php (revision 121:7bd6a5adf73f)
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        } else {
34            echo "\nPDO::errorInfo():\n";
35            print_r($db->errorInfo());
36            exit;
37        }
38        $this->_db = $db;
39    }
40
41    public function add($page, $title_text, $title, $hid = '')
42    {
43        $result = $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title, title_text) values(" . $this->_db->quote($page) . ",
44                                    '" . sprintf('%u', crc32($page . $hid)) . "',
45                                    " . $this->_db->quote($hid) . ",
46                                    " . $this->_db->quote($title) . ",
47                                    " . $this->_db->quote($title_text) . ")");
48        if (!$result) {
49            echo "\nPDO::errorInfo():\n";
50            print_r($this->_db->errorInfo());
51        }
52    }
53
54    public function getAll()
55    {
56        $result = $this->_db->query("select * from {$this->_table}");
57        return $result->fetchAll(PDO::FETCH_ASSOC);
58    }
59
60    public function getByCrc($pageCrcList)
61    {
62        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
63        $result = $this->_db->query($sql);
64        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
65
66        $pages = array();
67        foreach ($rows as $row) {
68            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title'], 'title_text' => $row['title_text']);
69        }
70        $results = array();
71        foreach ($pageCrcList as $crc) {
72            $results[$crc] = $pages[$crc];
73        }
74        return $results;
75    }
76}
77