3 require_once 'lib/WikiDB/backend/PearDB.php';
5 // See http://sql-info.de/mysql/gotchas.html for mysql specific quirks.
7 // The slowest function overall is mysql_connect with [680ms]
8 // 2nd is db_mysql::simpleQuery with [257ms]
9 class WikiDB_backend_PearDB_mysql
10 extends WikiDB_backend_PearDB
12 function __construct($dbparams)
14 parent::__construct($dbparams);
15 if (DB::isError($this->_dbh)) return;
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]) .
21 "." . (integer)$arr[2];
22 if ($this->_serverinfo['version'] < 323.0) {
23 // Older MySQL's don't have CASE WHEN ... END
24 $this->_expressions['maxmajor'] = "MAX(IF(minor_edit=0,version,0))";
25 $this->_expressions['maxminor'] = "MAX(IF(minor_edit<>0,version,0))";
27 // esp. needed for utf databases
28 if ($this->_serverinfo['version'] > 401.0) {
29 mysql_query("SET NAMES 'UTF-8'");
35 * Kill timed out processes. ( so far only called on about every 50-th save. )
39 if (empty($this->_dbparams['timeout'])) return;
40 $result = mysql_query("SHOW processlist");
41 while ($row = mysql_fetch_array($result)) {
42 if ($row["db"] == $this->_dbh->dsn['database']
43 and $row["User"] == $this->_dbh->dsn['username']
44 and $row["Time"] > $this->_dbparams['timeout']
45 and $row["Command"] == "Sleep"
47 $process_id = $row["Id"];
48 mysql_query("KILL $process_id");
54 * Create a new revision of a page.
56 function set_versiondata($pagename, $version, $data)
59 $version_tbl = $this->_table_names['version_tbl'];
61 $minor_edit = (int)!empty($data['is_minor_edit']);
62 unset($data['is_minor_edit']);
64 $mtime = (int)$data['mtime'];
65 unset($data['mtime']);
66 assert(!empty($mtime));
68 @$content = (string)$data['%content'];
69 unset($data['%content']);
70 unset($data['%pagedata']);
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))
83 // real binding (prepare,execute) only since mysqli + PHP5
84 $this->_update_recent_table($id);
85 $this->_update_nonempty_table($id);
89 function _update_recent_table($pageid = false)
92 extract($this->_table_names);
93 extract($this->_expressions);
95 $pageid = (int)$pageid;
97 // optimized: mysql can do this with one REPLACE INTO.
98 // supported in every (?) mysql version
99 // requires PRIMARY KEY (id)!
100 $dbh->query("REPLACE INTO $recent_tbl"
101 . " (id, latestversion, latestmajor, latestminor)"
102 . " SELECT id, $maxversion, $maxmajor, $maxminor"
103 . " FROM $version_tbl"
104 . ($pageid ? " WHERE id=$pageid" : "")
109 * Find referenced empty pages.
111 function wanted_pages($exclude_from = '', $exclude = '', $sortby = '', $limit = '')
114 extract($this->_table_names);
115 if ($orderby = $this->sortby($sortby, 'db', array('pagename', 'wantedfrom')))
116 $orderby = 'ORDER BY ' . $orderby;
118 if ($exclude_from) // array of pagenames
119 $exclude_from = " AND pp.pagename NOT IN " . $this->_sql_set($exclude_from);
120 if ($exclude) // array of pagenames
121 $exclude = " AND p.pagename NOT IN " . $this->_sql_set($exclude);
123 /* ISNULL is mysql specific */
124 $sql = "SELECT p.pagename, pp.pagename AS wantedfrom"
125 . " FROM $page_tbl p, $link_tbl linked"
126 . " LEFT JOIN $page_tbl pp ON (linked.linkto = pp.id)"
127 . " LEFT JOIN $nonempty_tbl ne ON (linked.linkto = ne.id)"
128 . " WHERE ISNULL(ne.id)"
129 . " AND p.id = linked.linkfrom"
134 list($from, $count) = $this->limit($limit);
135 $result = $dbh->limitQuery($sql, $from, $count * 3);
137 $result = $dbh->query($sql);
139 return new WikiDB_backend_PearDB_generic_iter($this, $result);
142 /* // REPLACE will not delete empy pages, so it was removed --ru
143 function _update_nonempty_table($pageid = false) {
145 extract($this->_table_names);
147 $pageid = (int)$pageid;
149 // Optimized: mysql can do this with one REPLACE INTO.
150 // supported in every (?) mysql version
151 // requires PRIMARY KEY (id)
152 $dbh->query("REPLACE INTO $nonempty_tbl (id)"
153 . " SELECT $recent_tbl.id"
154 . " FROM $recent_tbl, $version_tbl"
155 . " WHERE $recent_tbl.id=$version_tbl.id"
156 . " AND version=latestversion"
158 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
169 foreach ($this->_table_names as $table) {
170 $dbh->query("OPTIMIZE TABLE $table");
178 protected function _lock_tables($write_lock = true)
180 $lock_type = $write_lock ? "WRITE" : "READ";
182 foreach ($this->_table_names as $table) {
183 $tables[] = "$table $lock_type";
185 $this->_dbh->query("LOCK TABLES " . join(",", $tables));
191 protected function _unlock_tables()
193 $this->_dbh->query("UNLOCK TABLES");
196 function increaseHitCount($pagename)
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?
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" : ""));
212 class WikiDB_backend_PearDB_mysql_search
213 extends WikiDB_backend_PearDB_search
215 function _pagename_match_clause($node)
217 $word = $node->sql();
219 $word = $dbh->escapeSimple($word);
220 if ($node->op == 'REGEX') { // posix regex extensions
221 return "pagename REGEXP '$word'";
223 return ($this->_case_exact
224 ? "pagename LIKE '$word'"
225 : "LOWER(pagename) LIKE '$word'");
234 // c-hanging-comment-ender-p: nil
235 // indent-tabs-mode: nil