xref: /plugin/sphinxsearch-was/PageMapper.php (revision 4:c8a70a2936eb)
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                $db->query("CREATE TABLE {$this->_table} (page varchar(1024), page_crc int(11), hid varchar(1024), title varchar (1024))");
21            }
22        }
23        $this->_db = $db;
24    }
25
26    public function add($page, $title = '', $hid='')
27    {
28        $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title) values('{$page}', '".crc32($page.$hid)."', '{$hid}', '{$title}')");
29    }
30
31    public function getAll()
32    {
33        $result = $this->_db->query("select * from {$this->_table}");
34        return $result->fetchAll($query);
35    }
36
37    public function getByCrc($pageCrcList)
38    {
39        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
40        $result = $this->_db->query($sql);
41        $rows = $result->fetchAll($query);
42
43        $pages = array();
44        foreach($rows as $row){
45            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title']);
46        }
47        return $pages;
48    }
49}
50