2 rcs_id('$Id: PearDB_mysql.php,v 1.15 2004-12-22 15:47:41 rurban Exp $');
4 require_once('lib/WikiDB/backend/PearDB.php');
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
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))";
30 * Kill timed out processes. ( so far only called on about every 50-th save. )
33 if (empty($this->_dbparams['timeout'])) return;
34 $result = mysql_query("SHOW processlist");
35 while ($row = mysql_fetch_array($result)) {
36 if ($row["db"] == $this->_dbh->dsn['database']
37 and $row["User"] == $this->_dbh->dsn['username']
38 and $row["Time"] > $this->_dbparams['timeout']
39 and $row["Command"] == "Sleep")
41 $process_id = $row["Id"];
42 mysql_query("KILL $process_id");
48 * Create a new revision of a page.
50 function set_versiondata($pagename, $version, $data) {
52 $version_tbl = $this->_table_names['version_tbl'];
54 $minor_edit = (int) !empty($data['is_minor_edit']);
55 unset($data['is_minor_edit']);
57 $mtime = (int)$data['mtime'];
58 unset($data['mtime']);
59 assert(!empty($mtime));
61 @$content = (string) $data['%content'];
62 unset($data['%content']);
63 unset($data['%pagedata']);
66 $id = $this->_get_pageid($pagename, true);
67 // requires PRIMARY KEY (id,version)!
68 // VALUES supported since mysql-3.22.5
69 $dbh->query(sprintf("REPLACE INTO $version_tbl"
70 . " (id,version,mtime,minor_edit,content,versiondata)"
71 . " VALUES(%d,%d,%d,%d,'%s','%s')",
72 $id, $version, $mtime, $minor_edit,
73 $dbh->escapeSimple($content),
74 $dbh->escapeSimple($this->_serialize($data))
76 // real binding (prepare,execute) only since mysqli + PHP5
77 $this->_update_recent_table($id);
78 $this->_update_nonempty_table($id);
82 function _update_recent_table($pageid = false) {
84 extract($this->_table_names);
85 extract($this->_expressions);
87 $pageid = (int)$pageid;
89 // optimized: mysql can do this with one REPLACE INTO.
90 // supported in every (?) mysql version
91 // requires PRIMARY KEY (id)!
92 $dbh->query("REPLACE INTO $recent_tbl"
93 . " (id, latestversion, latestmajor, latestminor)"
94 . " SELECT id, $maxversion, $maxmajor, $maxminor"
95 . " FROM $version_tbl"
96 . ( $pageid ? " WHERE id=$pageid" : "")
100 /* // REPLACE will not delete empy pages, so it was removed --ru
101 function _update_nonempty_table($pageid = false) {
103 extract($this->_table_names);
105 $pageid = (int)$pageid;
107 // Optimized: mysql can do this with one REPLACE INTO.
108 // supported in every (?) mysql version
109 // requires PRIMARY KEY (id)
110 $dbh->query("REPLACE INTO $nonempty_tbl (id)"
111 . " SELECT $recent_tbl.id"
112 . " FROM $recent_tbl, $version_tbl"
113 . " WHERE $recent_tbl.id=$version_tbl.id"
114 . " AND version=latestversion"
116 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
123 function optimize() {
126 foreach ($this->_table_names as $table) {
127 $dbh->query("OPTIMIZE TABLE $table");
135 function _lock_tables($write_lock = true) {
136 $lock_type = $write_lock ? "WRITE" : "READ";
137 foreach ($this->_table_names as $table) {
138 $tables[] = "$table $lock_type";
140 $this->_dbh->query("LOCK TABLES " . join(",", $tables));
146 function _unlock_tables() {
147 $this->_dbh->query("UNLOCK TABLES");
150 function increaseHitCount($pagename) {
152 // Hits is the only thing we can update in a fast manner.
153 // Note that this will fail silently if the page does not
154 // have a record in the page table. Since it's just the
155 // hit count, who cares?
157 $dbh->query(sprintf("UPDATE LOW_PRIORITY %s SET hits=hits+1 WHERE pagename='%s' %s",
158 $this->_table_names['page_tbl'],
159 $dbh->escapeSimple($pagename),
160 ($this->_serverinfo['version'] >= 323.0) ? "LIMIT 1": ""));
166 class WikiDB_backend_PearDB_mysql_search
167 extends WikiDB_backend_PearDB_search
169 function _pagename_match_clause($node) {
170 $word = $node->sql();
171 if ($node->op == 'REGEX') { // posix regex extensions
172 return "pagename REGEXP '$word'";
174 return $this->_case_exact ? "pagename LIKE '$word'"
175 : "LOWER(pagename) LIKE '$word'";
180 // (c-file-style: "gnu")
185 // c-hanging-comment-ender-p: nil
186 // indent-tabs-mode: nil