]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB_mysql.php
define mysql connection charset
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB_mysql.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PearDB_mysql.php,v 1.17 2005-02-07 15:41:29 rurban Exp $');
3
4 require_once('lib/WikiDB/backend/PearDB.php');
5
6 // The slowest function overall is mysql_connect with [680ms]
7 // 2nd is db_mysql::simpleQuery with [257ms]
8 class WikiDB_backend_PearDB_mysql
9 extends WikiDB_backend_PearDB
10 {
11     /**
12      * Constructor.
13      */
14     function WikiDB_backend_PearDB_mysql($dbparams) {
15         $this->WikiDB_backend_PearDB($dbparams);
16         //$this->_serverinfo = $this->_dbh->ServerInfo();
17         $row = $this->_dbh->GetOne("SELECT version()");
18         if (!DB::isError($row) and !empty($row)) {
19             $arr = explode('.',$row);
20             $this->_serverinfo['version'] = (string)(($arr[0] * 100) + $arr[1]) . "." . (integer)$arr[2];
21             if ($this->_serverinfo['version'] < 323.0) {
22                 // Older MySQL's don't have CASE WHEN ... END
23                 $this->_expressions['maxmajor'] = "MAX(IF(minor_edit=0,version,0))";
24                 $this->_expressions['maxminor'] = "MAX(IF(minor_edit<>0,version,0))";
25             }
26             if ($this->_serverinfo['version'] > 401.0) {
27                 global $charset;
28                 //http://dev.mysql.com/doc/mysql/en/charset-connection.html
29                 //SET NAMES 'latin1'
30                 mysql_query("SET NAMES '$charset'");
31                 //"SET CHARACTER SET latin1" for the default database.
32             }
33         }
34     }
35     
36     /**
37      * Kill timed out processes. ( so far only called on about every 50-th save. )
38      */
39     function _timeout() {
40         if (empty($this->_dbparams['timeout'])) return;
41         $result = mysql_query("SHOW processlist");
42         while ($row = mysql_fetch_array($result)) { 
43             if ($row["db"] == $this->_dbh->dsn['database']
44                 and $row["User"] == $this->_dbh->dsn['username']
45                 and $row["Time"] > $this->_dbparams['timeout']
46                 and $row["Command"] == "Sleep") 
47             {
48                     $process_id = $row["Id"]; 
49                     mysql_query("KILL $process_id");
50             }
51         }
52     }
53
54     /**
55      * Create a new revision of a page.
56      */
57     function set_versiondata($pagename, $version, $data) {
58         $dbh = &$this->_dbh;
59         $version_tbl = $this->_table_names['version_tbl'];
60         
61         $minor_edit = (int) !empty($data['is_minor_edit']);
62         unset($data['is_minor_edit']);
63         
64         $mtime = (int)$data['mtime'];
65         unset($data['mtime']);
66         assert(!empty($mtime));
67
68         @$content = (string) $data['%content'];
69         unset($data['%content']);
70         unset($data['%pagedata']);
71         
72         $this->lock();
73         $id = $this->_get_pageid($pagename, true);
74         // requires PRIMARY KEY (id,version)!
75         // VALUES supported since mysql-3.22.5
76         $dbh->query(sprintf("REPLACE INTO $version_tbl"
77                             . " (id,version,mtime,minor_edit,content,versiondata)"
78                             . " VALUES(%d,%d,%d,%d,'%s','%s')",
79                             $id, $version, $mtime, $minor_edit,
80                             $dbh->escapeSimple($content),
81                             $dbh->escapeSimple($this->_serialize($data))
82                             ));
83         // real binding (prepare,execute) only since mysqli + PHP5
84         $this->_update_recent_table($id);
85         $this->_update_nonempty_table($id);
86         $this->unlock();
87     }
88
89     function _update_recent_table($pageid = false) {
90         $dbh = &$this->_dbh;
91         extract($this->_table_names);
92         extract($this->_expressions);
93
94         $pageid = (int)$pageid;
95
96         // optimized: mysql can do this with one REPLACE INTO.
97         // supported in every (?) mysql version
98         // requires PRIMARY KEY (id)!
99         $dbh->query("REPLACE INTO $recent_tbl"
100                     . " (id, latestversion, latestmajor, latestminor)"
101                     . " SELECT id, $maxversion, $maxmajor, $maxminor"
102                     . " FROM $version_tbl"
103                     . ( $pageid ? " WHERE id=$pageid" : "")
104                     . " GROUP BY id" );
105     }
106
107     /* // REPLACE will not delete empy pages, so it was removed --ru
108     function _update_nonempty_table($pageid = false) {
109         $dbh = &$this->_dbh;
110         extract($this->_table_names);
111
112         $pageid = (int)$pageid;
113
114         // Optimized: mysql can do this with one REPLACE INTO.
115         // supported in every (?) mysql version
116         // requires PRIMARY KEY (id)
117         $dbh->query("REPLACE INTO $nonempty_tbl (id)"
118                     . " SELECT $recent_tbl.id"
119                     . " FROM $recent_tbl, $version_tbl"
120                     . " WHERE $recent_tbl.id=$version_tbl.id"
121                     . "       AND version=latestversion"
122                     . "  AND content<>''"
123                     . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
124     }
125     */
126    
127     /**
128      * Pack tables.
129      */
130     function optimize() {
131         $dbh = &$this->_dbh;
132         $this->_timeout();
133         foreach ($this->_table_names as $table) {
134             $dbh->query("OPTIMIZE TABLE $table");
135         }
136         return 1;
137     }
138
139     /**
140      * Lock tables.
141      */
142     function _lock_tables($write_lock = true) {
143         $lock_type = $write_lock ? "WRITE" : "READ";
144         foreach ($this->_table_names as $table) {
145             $tables[] = "$table $lock_type";
146         }
147         $this->_dbh->query("LOCK TABLES " . join(",", $tables));
148     }
149
150     /**
151      * Release all locks.
152      */
153     function _unlock_tables() {
154         $this->_dbh->query("UNLOCK TABLES");
155     }
156
157     function increaseHitCount($pagename) {
158         $dbh = &$this->_dbh;
159         // Hits is the only thing we can update in a fast manner.
160         // Note that this will fail silently if the page does not
161         // have a record in the page table.  Since it's just the
162         // hit count, who cares?
163         // LIMIT since 3.23
164         $dbh->query(sprintf("UPDATE LOW_PRIORITY %s SET hits=hits+1 WHERE pagename='%s' %s",
165                             $this->_table_names['page_tbl'],
166                             $dbh->escapeSimple($pagename),
167                             ($this->_serverinfo['version'] >= 323.0) ? "LIMIT 1": ""));
168         return;
169     }
170
171 };
172
173 class WikiDB_backend_PearDB_mysql_search
174 extends WikiDB_backend_PearDB_search
175 {
176     function _pagename_match_clause($node) { 
177         $word = $node->sql();
178         if ($node->op == 'REGEX') { // posix regex extensions
179             return "pagename REGEXP '$word'";
180         } else {
181             return ($this->_case_exact 
182                     ? "pagename LIKE '$word'" 
183                     : "LOWER(pagename) LIKE '$word'");
184         }
185     }
186 }
187
188 // (c-file-style: "gnu")
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End:   
196 ?>