]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PDO_mysql.php
Reformat code
[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             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     {
62         return 'mysql';
63     }
64
65     /**
66      * Kill timed out processes. ( so far only called on about every 50-th save. )
67      */
68     function _timeout()
69     {
70         if (empty($this->_dbparams['timeout'])) return;
71         $sth = $this->_dbh->prepare("SHOW processlist");
72         if ($sth->execute())
73             while ($row = $sth->fetch(PDO_FETCH_ASSOC)) {
74                 if ($row["db"] == $this->_dsn['database']
75                     and $row["User"] == $this->_dsn['username']
76                         and $row["Time"] > $this->_dbparams['timeout']
77                             and $row["Command"] == "Sleep"
78                 ) {
79                     $process_id = $row["Id"];
80                     $this->query("KILL $process_id");
81                 }
82             }
83     }
84
85     /**
86      * Pack tables.
87      */
88     function optimize()
89     {
90         $dbh = &$this->_dbh;
91         $this->_timeout();
92         foreach ($this->_table_names as $table) {
93             $this->query("OPTIMIZE TABLE $table");
94         }
95         return 1;
96     }
97
98     function listOfTables()
99     {
100         $sth = $this->_dbh->prepare("SHOW TABLES");
101         $sth->execute();
102         $tables = array();
103         while ($row = $sth->fetch(PDO_FETCH_NUM)) {
104             $tables[] = $row[0];
105         }
106         return $tables;
107     }
108
109     function listOfFields($database, $table)
110     {
111         $old_db = $this->database();
112         if ($database != $old_db) {
113             try {
114                 $dsn = preg_replace("/dbname=\w+;/", "dbname=" . $database, $this->_dsn);
115                 $dsn = preg_replace("/database=\w+;/", "database=" . $database, $dsn);
116                 $conn = new PDO($dsn,
117                     DBADMIN_USER ? DBADMIN_USER : $this->_parsedDSN['username'],
118                     DBADMIN_PASSWD ? DBADMIN_PASSWD : $this->_parsedDSN['password']);
119             } catch (PDOException $e) {
120                 echo "<br>\nDB Connection failed: " . $e->getMessage();
121                 echo "<br>\nDSN: '", $this->_dsn, "'";
122                 echo "<br>\n_parsedDSN: '", print_r($this->_parsedDSN), "'";
123                 $conn = $this->_dbh;
124             }
125         } else {
126             $conn = $this->_dbh;
127         }
128         $sth = $conn->prepare("SHOW COLUMNS FROM $table");
129         $sth->execute();
130         $field_list = array();
131         while ($row = $sth->fetch(PDO_FETCH_NUM)) {
132             $field_list[] = $row[0];
133         }
134         if ($database != $old_db) {
135             unset($conn);
136         }
137         return $field_list;
138     }
139
140     /*
141      * offset specific syntax within mysql
142      * convert from,count to SQL "LIMIT $offset, $count"
143      */
144     function _limit_sql($limit = false)
145     {
146         if ($limit) {
147             list($offset, $count) = $this->limit($limit);
148             if ($offset)
149                 // pgsql needs "LIMIT $count OFFSET $from"
150                 $limit = " LIMIT $offset, $count";
151             else
152                 $limit = " LIMIT $count";
153         } else
154             $limit = '';
155         return $limit;
156     }
157
158 }
159
160 // Local Variables:
161 // mode: php
162 // tab-width: 8
163 // c-basic-offset: 4
164 // c-hanging-comment-ender-p: nil
165 // indent-tabs-mode: nil
166 // End: