OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / lib / patTemplate / patTemplate / Reader / DB.php
1 <?PHP
2 /**
3  * patTemplate Reader that reads from a database using PEAR::DB
4  *
5  * $Id: DB.php 440 2008-03-30 09:00:16Z fishbone $
6  *
7  * @package             patTemplate
8  * @subpackage  Readers
9  * @author              Stephan Schmidt <schst@php.net>
10  */
11
12 /**
13  * PEAR::DB is not installed
14  */
15 define('PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND', 'patTemplate::Reader::DB::001');
16
17 /**
18  * Connection could not be established
19  */
20 define('PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION', 'patTemplate::Reader::DB::002');
21
22 /**
23  * Could not find input
24  */
25 define('PATTEMPLATE_READER_DB_ERROR_NO_INPUT', 'patTemplate::Reader::DB::003');
26
27 /**
28  * Unknown input syntax
29  */
30 define('PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT', 'patTemplate::Reader::DB::004');
31
32 /**
33  * patTemplate Reader that reads from a database using PEAR::DB
34  *
35  * $Id: DB.php 440 2008-03-30 09:00:16Z fishbone $
36  *
37  * @package             patTemplate
38  * @subpackage  Readers
39  * @author              Stephan Schmidt <schst@php.net>
40  */
41 class patTemplate_Reader_DB extends patTemplate_Reader
42 {
43    /**
44     * reader name
45         * @access       private
46         * @var          string
47         */
48         var     $_name = 'DB';
49
50    /**
51     * read templates from the database
52     *
53     * Input may either be an SQL query or a string defining the location
54     * of the template using the format:
55     * <code>
56     * table[@key=value]/@templateField
57     * </code>
58         *
59         * @final
60         * @access       public
61         * @param        string  file to parse
62         * @return       array   templates
63         */
64         function readTemplates($input)
65         {
66             $content = $this->getDataFromDb($input);
67             if (patErrorManager::isError($content)) {
68                 return $content;
69             }
70                 $templates = $this->parseString($content);
71                 return $templates;
72         }
73
74    /**
75     * fetch the template data from the database
76     *
77     * @access   protected
78     * @param    string      input to read from
79     */
80         function getDataFromDb($input)
81         {
82             // check for PEAR DB
83             if (!class_exists('DB')) {
84             @include_once 'DB.php';
85             if (!class_exists('DB')) {
86                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_CLASS_NOT_FOUND, 'This reader requires PEAR::DB which could not be found on your system.');
87             }
88             }
89
90             // establish connection
91             $db = &DB::connect($this->getTemplateRoot());
92             if (PEAR::isError($db)) {
93                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_CONNECTION, 'Could not establish database connection: ' . $db->getMessage());
94             }
95
96             $input = $this->parseInputStringToQuery($input, $db);    
97             if (patErrorManager::isError($input)) {
98                 return $input;
99             }
100
101             $content = $db->getOne($input);
102             if (PEAR::isError($content)) {
103                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_NO_INPUT, 'Could not fetch template: ' . $content->getMessage());
104             }
105             return $content;
106         }
107         
108    /**
109         * Parse the template location syntax to a query
110         *
111         * @access  private
112         * @param   string
113         * @param   DB_common
114         */
115         function parseInputStringToQuery($input, $db)
116         {
117             // Input is no query
118             if (strstr($input, 'SELECT') !== false) {
119                 return $input;
120             }
121
122             $matches = array();
123         if (!preg_match('/^([a-z]+)\[([^]]+)\]\/@([a-z]+)$/i', $input, $matches)) {
124                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT, 'Could not parse input string.');
125         }
126         
127         $table         = $matches[1];
128         $templateField = $matches[3];
129         $where         = array();
130         $tmp = explode(',', $matches[2]);
131         foreach ($tmp as $clause) {
132                 list($field, $value) = explode('=', trim($clause));
133                 if ($field{0} !== '@') {
134                 return patErrorManager::raiseError(PATTEMPLATE_READER_DB_ERROR_UNKNOWN_INPUT, 'Could not parse input string.');
135                 }
136                 $field = substr($field, 1);
137                 array_push($where, $field . '=' . $db->quoteSmart($value));
138         }
139         
140         $query = sprintf('SELECT %s FROM %s WHERE %s', $templateField, $table, implode(' AND ', $where));
141         return $query;
142         }
143         
144    /**
145     * load template from any input 
146     *
147     * If the a template is loaded, the content will not get
148     * analyzed but the whole content is returned as a string.
149         *
150         * @abstract     must be implemented in the template readers
151         * @param        mixed   input to load from.
152         *                                       This can be a string, a filename, a resource or whatever the derived class needs to read from
153         * @return       string  template content
154         */
155         function loadTemplate($input)
156         {
157             $content = $this->getDataFromDb($input);
158             return $content;
159         }
160 }
161 ?>