]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB_mysql.php
Use HTML 5 DOCTYPE; force CHARSET=utf-8
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB_mysql.php
1 <?php
2
3 require_once 'lib/WikiDB/backend/PearDB.php';
4
5 // See http://sql-info.de/mysql/gotchas.html for mysql specific quirks.
6
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
11 {
12     /**
13      * Constructor.
14      */
15     function WikiDB_backend_PearDB_mysql($dbparams)
16     {
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                 mysql_query("SET NAMES 'UTF-8'");
33             }
34         }
35     }
36
37     /**
38      * Kill timed out processes. ( so far only called on about every 50-th save. )
39      */
40     function _timeout()
41     {
42         if (empty($this->_dbparams['timeout'])) return;
43         $result = mysql_query("SHOW processlist");
44         while ($row = mysql_fetch_array($result)) {
45             if ($row["db"] == $this->_dbh->dsn['database']
46                 and $row["User"] == $this->_dbh->dsn['username']
47                     and $row["Time"] > $this->_dbparams['timeout']
48                         and $row["Command"] == "Sleep"
49             ) {
50                 $process_id = $row["Id"];
51                 mysql_query("KILL $process_id");
52             }
53         }
54     }
55
56     /**
57      * Create a new revision of a page.
58      */
59     function set_versiondata($pagename, $version, $data)
60     {
61         $dbh = &$this->_dbh;
62         $version_tbl = $this->_table_names['version_tbl'];
63
64         $minor_edit = (int)!empty($data['is_minor_edit']);
65         unset($data['is_minor_edit']);
66
67         $mtime = (int)$data['mtime'];
68         unset($data['mtime']);
69         assert(!empty($mtime));
70
71         @$content = (string)$data['%content'];
72         unset($data['%content']);
73         unset($data['%pagedata']);
74
75         $this->lock();
76         $id = $this->_get_pageid($pagename, true);
77         // requires PRIMARY KEY (id,version)!
78         // VALUES supported since mysql-3.22.5
79         $dbh->query(sprintf("REPLACE INTO $version_tbl"
80                 . " (id,version,mtime,minor_edit,content,versiondata)"
81                 . " VALUES(%d,%d,%d,%d,'%s','%s')",
82             $id, $version, $mtime, $minor_edit,
83             $dbh->escapeSimple($content),
84             $dbh->escapeSimple($this->_serialize($data))
85         ));
86         // real binding (prepare,execute) only since mysqli + PHP5
87         $this->_update_recent_table($id);
88         $this->_update_nonempty_table($id);
89         $this->unlock();
90     }
91
92     function _update_recent_table($pageid = false)
93     {
94         $dbh = &$this->_dbh;
95         extract($this->_table_names);
96         extract($this->_expressions);
97
98         $pageid = (int)$pageid;
99
100         // optimized: mysql can do this with one REPLACE INTO.
101         // supported in every (?) mysql version
102         // requires PRIMARY KEY (id)!
103         $dbh->query("REPLACE INTO $recent_tbl"
104             . " (id, latestversion, latestmajor, latestminor)"
105             . " SELECT id, $maxversion, $maxmajor, $maxminor"
106             . " FROM $version_tbl"
107             . ($pageid ? " WHERE id=$pageid" : "")
108             . " GROUP BY id");
109     }
110
111     /* ISNULL is mysql specific */
112     function wanted_pages($exclude_from = '', $exclude = '', $sortby = '', $limit = '')
113     {
114         $dbh = &$this->_dbh;
115         extract($this->_table_names);
116         if ($orderby = $this->sortby($sortby, 'db', array('pagename', 'wantedfrom')))
117             $orderby = 'ORDER BY ' . $orderby;
118
119         if ($exclude_from) // array of pagenames
120             $exclude_from = " AND pp.pagename NOT IN " . $this->_sql_set($exclude_from);
121         if ($exclude) // array of pagenames
122             $exclude = " AND p.pagename NOT IN " . $this->_sql_set($exclude);
123
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"
130             . $exclude_from
131             . $exclude
132             . $orderby;
133         if ($limit) {
134             list($from, $count) = $this->limit($limit);
135             $result = $dbh->limitQuery($sql, $from, $count * 3);
136         } else {
137             $result = $dbh->query($sql);
138         }
139         return new WikiDB_backend_PearDB_generic_iter($this, $result);
140     }
141
142     /* // REPLACE will not delete empy pages, so it was removed --ru
143     function _update_nonempty_table($pageid = false) {
144         $dbh = &$this->_dbh;
145         extract($this->_table_names);
146
147         $pageid = (int)$pageid;
148
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"
157                     . "  AND content<>''"
158                     . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
159     }
160     */
161
162     /**
163      * Pack tables.
164      */
165     function optimize()
166     {
167         $dbh = &$this->_dbh;
168         $this->_timeout();
169         foreach ($this->_table_names as $table) {
170             $dbh->query("OPTIMIZE TABLE $table");
171         }
172         return 1;
173     }
174
175     /**
176      * Lock tables.
177      */
178     function _lock_tables($write_lock = true)
179     {
180         $lock_type = $write_lock ? "WRITE" : "READ";
181         foreach ($this->_table_names as $table) {
182             $tables[] = "$table $lock_type";
183         }
184         $this->_dbh->query("LOCK TABLES " . join(",", $tables));
185     }
186
187     /**
188      * Release all locks.
189      */
190     function _unlock_tables()
191     {
192         $this->_dbh->query("UNLOCK TABLES");
193     }
194
195     function increaseHitCount($pagename)
196     {
197         $dbh = &$this->_dbh;
198         // Hits is the only thing we can update in a fast manner.
199         // Note that this will fail silently if the page does not
200         // have a record in the page table.  Since it's just the
201         // hit count, who cares?
202         // LIMIT since 3.23
203         $dbh->query(sprintf("UPDATE LOW_PRIORITY %s SET hits=hits+1 WHERE pagename='%s' %s",
204             $this->_table_names['page_tbl'],
205             $dbh->escapeSimple($pagename),
206             ($this->_serverinfo['version'] >= 323.0) ? "LIMIT 1" : ""));
207         return;
208     }
209
210 }
211
212 class WikiDB_backend_PearDB_mysql_search
213     extends WikiDB_backend_PearDB_search
214 {
215     function _pagename_match_clause($node)
216     {
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: