]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PDO_mysql.php
Use __construct
[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 __construct($dbparams)
32     {
33         parent::__construct($dbparams);
34
35         if (!empty($this->_serverinfo['version'])) {
36             $arr = explode('.', $this->_serverinfo['version']);
37             $this->_serverinfo['version'] = (string)(($arr[0] * 100) + $arr[1]) . "." . (integer)$arr[2];
38         }
39         if ($this->_serverinfo['version'] < 323.0) {
40             // Older MySQL's don't have CASE WHEN ... END
41             $this->_expressions['maxmajor'] = "MAX(IF(minor_edit=0,version,0))";
42             $this->_expressions['maxminor'] = "MAX(IF(minor_edit<>0,version,0))";
43         }
44
45         if ($this->_serverinfo['version'] > 401.0) {
46             mysql_query("SET NAMES 'UTF-8'");
47         }
48     }
49
50     function backendType()
51     {
52         return 'mysql';
53     }
54
55     /**
56      * Kill timed out processes. ( so far only called on about every 50-th save. )
57      */
58     function _timeout()
59     {
60         if (empty($this->_dbparams['timeout'])) return;
61         $sth = $this->_dbh->prepare("SHOW processlist");
62         if ($sth->execute())
63             while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
64                 if ($row["db"] == $this->_dsn['database']
65                     and $row["User"] == $this->_dsn['username']
66                         and $row["Time"] > $this->_dbparams['timeout']
67                             and $row["Command"] == "Sleep"
68                 ) {
69                     $process_id = $row["Id"];
70                     $this->query("KILL $process_id");
71                 }
72             }
73     }
74
75     /**
76      * Pack tables.
77      */
78     function optimize()
79     {
80         $this->_timeout();
81         foreach ($this->_table_names as $table) {
82             $this->query("OPTIMIZE TABLE $table");
83         }
84         return 1;
85     }
86
87     function listOfTables()
88     {
89         $sth = $this->_dbh->prepare("SHOW TABLES");
90         $sth->execute();
91         $tables = array();
92         while ($row = $sth->fetch(PDO::FETCH_NUM)) {
93             $tables[] = $row[0];
94         }
95         return $tables;
96     }
97
98     function listOfFields($database, $table)
99     {
100         $old_db = $this->database();
101         if ($database != $old_db) {
102             try {
103                 $dsn = preg_replace("/dbname=\w+;/", "dbname=" . $database, $this->_dsn);
104                 $dsn = preg_replace("/database=\w+;/", "database=" . $database, $dsn);
105                 $conn = new PDO($dsn,
106                     DBADMIN_USER ? DBADMIN_USER : $this->_parsedDSN['username'],
107                     DBADMIN_PASSWD ? DBADMIN_PASSWD : $this->_parsedDSN['password']);
108             } catch (PDOException $e) {
109                 echo "<br>\nDB Connection failed: " . $e->getMessage();
110                 echo "<br>\nDSN: '", $this->_dsn, "'";
111                 echo "<br>\n_parsedDSN: '", print_r($this->_parsedDSN), "'";
112                 $conn = $this->_dbh;
113             }
114         } else {
115             $conn = $this->_dbh;
116         }
117         $sth = $conn->prepare("SHOW COLUMNS FROM $table");
118         $sth->execute();
119         $field_list = array();
120         while ($row = $sth->fetch(PDO::FETCH_NUM)) {
121             $field_list[] = $row[0];
122         }
123         if ($database != $old_db) {
124             unset($conn);
125         }
126         return $field_list;
127     }
128
129     /*
130      * offset specific syntax within mysql
131      * convert from,count to SQL "LIMIT $offset, $count"
132      */
133     function _limit_sql($limit = false)
134     {
135         if ($limit) {
136             list($offset, $count) = $this->limit($limit);
137             if ($offset)
138                 // pgsql needs "LIMIT $count OFFSET $from"
139                 $limit = " LIMIT $offset, $count";
140             else
141                 $limit = " LIMIT $count";
142         } else
143             $limit = '';
144         return $limit;
145     }
146
147 }
148
149 // Local Variables:
150 // mode: php
151 // tab-width: 8
152 // c-basic-offset: 4
153 // c-hanging-comment-ender-p: nil
154 // indent-tabs-mode: nil
155 // End: