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