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