]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PDO_mysql.php
Remove unused variables
[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         $this->_timeout();
91         foreach ($this->_table_names as $table) {
92             $this->query("OPTIMIZE TABLE $table");
93         }
94         return 1;
95     }
96
97     function listOfTables()
98     {
99         $sth = $this->_dbh->prepare("SHOW TABLES");
100         $sth->execute();
101         $tables = array();
102         while ($row = $sth->fetch(PDO_FETCH_NUM)) {
103             $tables[] = $row[0];
104         }
105         return $tables;
106     }
107
108     function listOfFields($database, $table)
109     {
110         $old_db = $this->database();
111         if ($database != $old_db) {
112             try {
113                 $dsn = preg_replace("/dbname=\w+;/", "dbname=" . $database, $this->_dsn);
114                 $dsn = preg_replace("/database=\w+;/", "database=" . $database, $dsn);
115                 $conn = new PDO($dsn,
116                     DBADMIN_USER ? DBADMIN_USER : $this->_parsedDSN['username'],
117                     DBADMIN_PASSWD ? DBADMIN_PASSWD : $this->_parsedDSN['password']);
118             } catch (PDOException $e) {
119                 echo "<br>\nDB Connection failed: " . $e->getMessage();
120                 echo "<br>\nDSN: '", $this->_dsn, "'";
121                 echo "<br>\n_parsedDSN: '", print_r($this->_parsedDSN), "'";
122                 $conn = $this->_dbh;
123             }
124         } else {
125             $conn = $this->_dbh;
126         }
127         $sth = $conn->prepare("SHOW COLUMNS FROM $table");
128         $sth->execute();
129         $field_list = array();
130         while ($row = $sth->fetch(PDO_FETCH_NUM)) {
131             $field_list[] = $row[0];
132         }
133         if ($database != $old_db) {
134             unset($conn);
135         }
136         return $field_list;
137     }
138
139     /*
140      * offset specific syntax within mysql
141      * convert from,count to SQL "LIMIT $offset, $count"
142      */
143     function _limit_sql($limit = false)
144     {
145         if ($limit) {
146             list($offset, $count) = $this->limit($limit);
147             if ($offset)
148                 // pgsql needs "LIMIT $count OFFSET $from"
149                 $limit = " LIMIT $offset, $count";
150             else
151                 $limit = " LIMIT $count";
152         } else
153             $limit = '';
154         return $limit;
155     }
156
157 }
158
159 // Local Variables:
160 // mode: php
161 // tab-width: 8
162 // c-basic-offset: 4
163 // c-hanging-comment-ender-p: nil
164 // indent-tabs-mode: nil
165 // End: