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) {
2010Syaroslav@ivinco.com                $db->query("CREATE TABLE {$this->_table} (page varchar(1024), page_crc int(11), hid varchar(1024), title varchar (1024), unique index a on pages(page, page_crc))");
210Syaroslav@ivinco.com            }
220Syaroslav@ivinco.com        }
230Syaroslav@ivinco.com        $this->_db = $db;
240Syaroslav@ivinco.com    }
250Syaroslav@ivinco.com
264Syaroslav@ivinco.com    public function add($page, $title = '', $hid='')
270Syaroslav@ivinco.com    {
28*12Syaroslav@ivinco.com        $result = $this->_db->query("REPLACE into {$this->_table}(page, page_crc, hid, title) values(".$this->_db->quote($page).",
29*12Syaroslav@ivinco.com                                    '".crc32($page.$hid)."',
30*12Syaroslav@ivinco.com                                    ".$this->_db->quote($hid).",
31*12Syaroslav@ivinco.com                                    ".$this->_db->quote($title).")");
32*12Syaroslav@ivinco.com        if (!$result) {
33*12Syaroslav@ivinco.com            //echo "\nPDO::errorInfo():\n";
34*12Syaroslav@ivinco.com            //print_r($this->_db->errorInfo());
35*12Syaroslav@ivinco.com        }
360Syaroslav@ivinco.com    }
370Syaroslav@ivinco.com
380Syaroslav@ivinco.com    public function getAll()
390Syaroslav@ivinco.com    {
400Syaroslav@ivinco.com        $result = $this->_db->query("select * from {$this->_table}");
41*12Syaroslav@ivinco.com        return $result->fetchAll(PDO::FETCH_ASSOC);
420Syaroslav@ivinco.com    }
430Syaroslav@ivinco.com
440Syaroslav@ivinco.com    public function getByCrc($pageCrcList)
450Syaroslav@ivinco.com    {
460Syaroslav@ivinco.com        $sql = sprintf("select * from {$this->_table} where page_crc in (%s)", implode(",", $pageCrcList));
470Syaroslav@ivinco.com        $result = $this->_db->query($sql);
48*12Syaroslav@ivinco.com        $rows = $result->fetchAll(PDO::FETCH_ASSOC);
494Syaroslav@ivinco.com
500Syaroslav@ivinco.com        $pages = array();
510Syaroslav@ivinco.com        foreach($rows as $row){
524Syaroslav@ivinco.com            $pages[$row['page_crc']] = array('page' => $row['page'], 'hid' => $row['hid'], 'title' => $row['title']);
530Syaroslav@ivinco.com        }
540Syaroslav@ivinco.com        return $pages;
550Syaroslav@ivinco.com    }
560Syaroslav@ivinco.com}
57