10Syaroslav@ivinco.com<?php
20Syaroslav@ivinco.com/*
30Syaroslav@ivinco.com * To change this template, choose Tools | Templates
40Syaroslav@ivinco.com * and open the template in the editor.
50Syaroslav@ivinco.com */
60Syaroslav@ivinco.com
70Syaroslav@ivinco.comclass PageMapper
80Syaroslav@ivinco.com{
91Syaroslav@ivinco.com    private $_database = 'pagedata';
100Syaroslav@ivinco.com    private $_table = 'pages';
110Syaroslav@ivinco.com    private $_db = null;
120Syaroslav@ivinco.com    public function  __construct()
130Syaroslav@ivinco.com    {
140Syaroslav@ivinco.com        global $conf;
151Syaroslav@ivinco.com        $this->_dbpath = DOKU_INC . $conf['savedir'] . '/sphinxsearch/'.$this->_database;
160Syaroslav@ivinco.com
171Syaroslav@ivinco.com        if (false != ($db = new PDO("sqlite:".$this->_dbpath))) {
180Syaroslav@ivinco.com            $q = @$db->query("SELECT 1 FROM {$this->_table} limit 1");
190Syaroslav@ivinco.com            if ($q === false) {
2019Syaroslav@ivinco.com                $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))");
2119Syaroslav@ivinco.com                if (!$result) {
2219Syaroslav@ivinco.com                    echo "\nPDO::errorInfo():\n";
2319Syaroslav@ivinco.com                    print_r($db->errorInfo());
2419Syaroslav@ivinco.com                    exit;
2519Syaroslav@ivinco.com                }
2619Syaroslav@ivinco.com
270Syaroslav@ivinco.com            }
280Syaroslav@ivinco.com        }
290Syaroslav@ivinco.com        $this->_db = $db;
300Syaroslav@ivinco.com    }
310Syaroslav@ivinco.com
3219Syaroslav@ivinco.com    public function add($page, $title_text, $title, $hid='')
330Syaroslav@ivinco.com    {
3419Syaroslav@ivinco.com        $result = $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title, title_text) values(".$this->_db->quote($page).",
3512Syaroslav@ivinco.com                                    '".crc32($page.$hid)."',
3612Syaroslav@ivinco.com                                    ".$this->_db->quote($hid).",
3719Syaroslav@ivinco.com                                    ".$this->_db->quote($title).",
3819Syaroslav@ivinco.com                                    ".$this->_db->quote($title_text).")");
3912Syaroslav@ivinco.com        if (!$result) {
40*56Syaroslav@ivinco.com            echo "\nPDO::errorInfo():\n";
41*56Syaroslav@ivinco.com            print_r($this->_db->errorInfo());
4212Syaroslav@ivinco.com        }
430Syaroslav@ivinco.com    }
440Syaroslav@ivinco.com
450Syaroslav@ivinco.com    public function getAll()
460Syaroslav@ivinco.com    {
470Syaroslav@ivinco.com        $result = $this->_db->query("select * from {$this->_table}");
4812Syaroslav@ivinco.com        return $result->fetchAll(PDO::FETCH_ASSOC);
490Syaroslav@ivinco.com    }
500Syaroslav@ivinco.com
5139Syaroslav@ivinco.com    public function getByCrc($pageCrcList)
520Syaroslav@ivinco.com    {
530Syaroslav@ivinco.com        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
540Syaroslav@ivinco.com        $result = $this->_db->query($sql);
5512Syaroslav@ivinco.com        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
564Syaroslav@ivinco.com
570Syaroslav@ivinco.com        $pages = array();
580Syaroslav@ivinco.com        foreach($rows as $row){
5939Syaroslav@ivinco.com            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title'], 'title_text' => $row['title_text']);
600Syaroslav@ivinco.com        }
6139Syaroslav@ivinco.com        $results = array();
6239Syaroslav@ivinco.com        foreach($pageCrcList as $crc){
6339Syaroslav@ivinco.com            $results[$crc] = $pages[$crc];
6439Syaroslav@ivinco.com        }
6539Syaroslav@ivinco.com        return $results;
6639Syaroslav@ivinco.com    }
670Syaroslav@ivinco.com}
68