]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PDO_mysql.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PDO_mysql.php
1 <?php // -*-php-*-
2
3
4 /*
5  * Copyright 2005 $ThePhpWikiProgrammingTeam
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * @author: Reini Urban
26  */
27 require_once 'lib/WikiDB/backend/PDO.php';
28
29 class WikiDB_backend_PDO_mysql
30 extends WikiDB_backend_PDO
31 {
32     function WikiDB_backend_PDO_mysql($dbparams) {
33
34         $this->WikiDB_backend_PDO($dbparams);
35
36         if (!empty($this->_serverinfo['version'])) {
37             $arr = explode('.', $this->_serverinfo['version']);
38             $this->_serverinfo['version'] = (string)(($arr[0] * 100) + $arr[1]) . "." . (integer)$arr[2];
39         }
40         if ($this->_serverinfo['version'] < 323.0) {
41             // Older MySQL's don't have CASE WHEN ... END
42             $this->_expressions['maxmajor'] = "MAX(IF(minor_edit=0,version,0))";
43             $this->_expressions['maxminor'] = "MAX(IF(minor_edit<>0,version,0))";
44         }
45
46         if ($this->_serverinfo['version'] > 401.0) {
47             global $charset;
48             $aliases = array('iso-8859-1' => 'latin1',
49                              'utf-8'      => 'utf8');
50             //http://dev.mysql.com/doc/mysql/en/charset-connection.html
51             if (isset($aliases[strtolower($charset)])) {
52                 // mysql needs special unusual names and doesn't resolve aliases
53                 mysql_query("SET NAMES '". $aliases[$charset] . "'");
54             } else {
55                 mysql_query("SET NAMES '$charset'");
56             }
57         }
58     }
59
60     function backendType() {
61         return 'mysql';
62     }
63
64     /**
65      * Kill timed out processes. ( so far only called on about every 50-th save. )
66      */
67     function _timeout() {
68         if (empty($this->_dbparams['timeout'])) return;
69     $sth = $this->_dbh->prepare("SHOW processlist");
70         if ($sth->execute())
71       while ($row = $sth->fetch(PDO_FETCH_ASSOC)) {
72         if ($row["db"] == $this->_dsn['database']
73             and $row["User"] == $this->_dsn['username']
74             and $row["Time"] > $this->_dbparams['timeout']
75             and $row["Command"] == "Sleep")
76             {
77                 $process_id = $row["Id"];
78                 $this->query("KILL $process_id");
79         }
80     }
81     }
82
83     /**
84      * Pack tables.
85      */
86     function optimize() {
87         $dbh = &$this->_dbh;
88         $this->_timeout();
89         foreach ($this->_table_names as $table) {
90             $this->query("OPTIMIZE TABLE $table");
91         }
92         return 1;
93     }
94
95     function listOfTables() {
96         $sth = $this->_dbh->prepare("SHOW TABLES");
97         $sth->execute();
98         $tables = array();
99         while ($row = $sth->fetch(PDO_FETCH_NUM)) { $tables[] = $row[0]; }
100         return $tables;
101     }
102
103     function listOfFields($database, $table) {
104         $old_db = $this->database();
105         if ($database != $old_db) {
106             try {
107                 $dsn = preg_replace("/dbname=\w+;/", "dbname=".$database, $this->_dsn);
108                 $dsn = preg_replace("/database=\w+;/", "database=".$database, $dsn);
109                 $conn = new PDO($dsn,
110                                 DBADMIN_USER ? DBADMIN_USER : $this->_parsedDSN['username'],
111                                 DBADMIN_PASSWD ? DBADMIN_PASSWD : $this->_parsedDSN['password']);
112             }
113             catch (PDOException $e) {
114                 echo "<br>\nDB Connection failed: " . $e->getMessage();
115                 echo "<br>\nDSN: '", $this->_dsn, "'";
116                 echo "<br>\n_parsedDSN: '", print_r($this->_parsedDSN), "'";
117                 $conn = $this->_dbh;
118             }
119         } else {
120             $conn = $this->_dbh;
121         }
122         $sth = $conn->prepare("SHOW COLUMNS FROM $table");
123         $sth->execute();
124         $field_list = array();
125         while ($row = $sth->fetch(PDO_FETCH_NUM)) {
126             $field_list[] = $row[0];
127         }
128         if ($database != $old_db) {
129             unset($conn);
130         }
131         return $field_list;
132     }
133
134     /*
135      * offset specific syntax within mysql
136      * convert from,count to SQL "LIMIT $offset, $count"
137      */
138     function _limit_sql($limit = false) {
139         if ($limit) {
140             list($offset, $count) = $this->limit($limit);
141             if ($offset)
142                 // pgsql needs "LIMIT $count OFFSET $from"
143                 $limit = " LIMIT $offset, $count";
144             else
145                 $limit = " LIMIT $count";
146         } else
147             $limit = '';
148         return $limit;
149     }
150
151 }
152
153 // Local Variables:
154 // mode: php
155 // tab-width: 8
156 // c-basic-offset: 4
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil
159 // End: