3 require_once 'lib/ErrorManager.php';
4 require_once 'lib/WikiDB/backend/PearDB.php';
6 if (!defined("USE_BYTEA")) // see schemas/psql-initialize.sql
7 define("USE_BYTEA", true);
8 //define("USE_BYTEA", false);
11 Since 1.3.12 changed to use:
17 class WikiDB_backend_PearDB_pgsql
18 extends WikiDB_backend_PearDB
20 function WikiDB_backend_PearDB_pgsql($dbparams)
22 // The pgsql handler of (at least my version of) the PEAR::DB
23 // library generates three warnings when a database is opened:
25 // Undefined index: options
26 // Undefined index: tty
27 // Undefined index: port
29 // This stuff is all just to catch and ignore these warnings,
30 // so that they don't get reported to the user. (They are
31 // not consequential.)
34 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pgsql_open_error'));
35 $this->WikiDB_backend_PearDB($dbparams);
36 $ErrorManager->popErrorHandler();
39 function _pgsql_open_error($error)
41 if (preg_match('/^Undefined\s+index:\s+(options|tty|port)/',
44 return true; // Ignore error
50 * NOTE: Only the table owner can do this. Either fix the schema or setup autovacuum.
54 return 0; // if the wikiuser is not the table owner
56 foreach ($this->_table_names as $table) {
57 $this->_dbh->query("VACUUM ANALYZE $table");
65 return pg_escape_bytea($s);
66 if (function_exists('pg_escape_string'))
67 return pg_escape_string($s);
69 return base64_encode($s);
75 return pg_unescape_bytea($s);
76 if (function_exists('pg_escape_string'))
79 return base64_decode($s);
82 // Until the binary escape problems on pear pgsql are solved */
83 function get_cached_html($pagename)
86 $page_tbl = $this->_table_names['page_tbl'];
87 $data = $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename='%s'",
88 $dbh->escapeSimple($pagename)));
89 if ($data) return $this->_unquote($data);
93 function set_cached_html($pagename, $data)
96 $page_tbl = $this->_table_names['page_tbl'];
98 $sth = $dbh->query(sprintf("UPDATE $page_tbl"
99 . " SET cached_html='%s'"
100 . " WHERE pagename='%s'",
101 $this->_quote($data),
102 $dbh->escapeSimple($pagename)));
104 $sth = $dbh->query("UPDATE $page_tbl"
105 . " SET cached_html=?"
106 . " WHERE pagename=?",
107 // PearDB does NOT use pg_escape_string()! Oh dear.
108 array($this->_quote($data), $pagename));
112 * Create a new revision of a page.
114 function _todo_set_versiondata($pagename, $version, $data)
117 $version_tbl = $this->_table_names['version_tbl'];
119 $minor_edit = (int)!empty($data['is_minor_edit']);
120 unset($data['is_minor_edit']);
122 $mtime = (int)$data['mtime'];
123 unset($data['mtime']);
124 assert(!empty($mtime));
126 @$content = (string)$data['%content'];
127 unset($data['%content']);
128 unset($data['%pagedata']);
131 $id = $this->_get_pageid($pagename, true);
132 $dbh->query(sprintf("DELETE FROM version WHERE id=%d AND version=%d", $id, $version));
133 $dbh->query(sprintf("INSERT INTO version (id,version,mtime,minor_edit,content,versiondata)" .
134 " VALUES (%d, %d, %d, %d, '%s', '%s')",
135 $id, $version, $mtime, $minor_edit,
136 $this->_quote($content),
137 $this->_serialize($data)));
138 // TODO: This function does not work yet
139 $dbh->query(sprintf("SELECT update_recent (%d, %d)", $id, $version));
144 * Delete an old revision of a page.
146 function _todo_delete_versiondata($pagename, $version)
149 // TODO: This function was removed
150 $dbh->query(sprintf("SELECT delete_versiondata (%d, %d)", $id, $version));
154 * Rename page in the database.
156 function _todo_rename_page($pagename, $to)
159 extract($this->_table_names);
162 if (($id = $this->_get_pageid($pagename, false))) {
163 if ($new = $this->_get_pageid($to, false)) {
165 // This page does not exist (already verified before), but exists in the page table.
166 // So we delete this page in one step.
167 $dbh->query("SELECT prepare_rename_page($id, $new)");
169 $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
170 $dbh->escapeSimple($to)));
177 * Lock all tables we might use.
179 function _lock_tables($write_lock = true)
181 $this->_dbh->query("BEGIN");
187 function _unlock_tables()
189 $this->_dbh->query("COMMIT");
195 function _serialize($data)
199 assert(is_array($data));
200 return $this->_quote(serialize($data));
206 function _unserialize($data)
210 // Base64 encoded data does not contain colons.
211 // (only alphanumerics and '+' and '/'.)
212 if (substr($data, 0, 2) == 'a:')
213 return unserialize($data);
214 return unserialize($this->_unquote($data));
220 function text_search($search, $fulltext = false, $sortby = '', $limit = '',
224 extract($this->_table_names);
225 $orderby = $this->sortby($sortby, 'db');
226 if ($sortby and $orderby) $orderby = ' ORDER BY ' . $orderby;
228 $searchclass = get_class($this) . "_search";
229 // no need to define it everywhere and then fallback. memory!
230 if (!class_exists($searchclass))
231 $searchclass = "WikiDB_backend_PearDB_search";
232 $searchobj = new $searchclass($search, $dbh);
234 $table = "$nonempty_tbl, $page_tbl";
235 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
236 $fields = $this->page_tbl_fields;
239 $table .= ", $recent_tbl";
240 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
242 $table .= ", $version_tbl";
243 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
245 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
246 // TODO: title still ignored, need better rank and subselect
247 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
248 $search_string = $search->makeTsearch2SqlClauseObj($callback);
249 $search_string = str_replace(array("%", " "), array("", "&"), $search_string);
250 $search_clause = "idxFTI @@ to_tsquery('$search_string')";
252 $orderby = " ORDER BY rank(idxFTI, to_tsquery('$search_string')) DESC";
254 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
255 $search_clause = $search->makeSqlClauseObj($callback);
258 $sql = "SELECT $fields FROM $table"
259 . " WHERE $join_clause"
260 . " AND ($search_clause)"
263 list($from, $count) = $this->limit($limit);
264 $result = $dbh->limitQuery($sql, $from, $count);
266 $result = $dbh->query($sql);
269 $iter = new WikiDB_backend_PearDB_iter($this, $result);
270 $iter->stoplisted = @$searchobj->stoplisted;
276 class WikiDB_backend_PearDB_pgsql_search
277 extends WikiDB_backend_PearDB_search
279 function _pagename_match_clause($node)
281 $word = $node->sql();
282 if ($node->op == 'REGEX') { // posix regex extensions
283 return ($this->_case_exact
284 ? "pagename ~* '$word'"
285 : "pagename ~ '$word'");
287 return ($this->_case_exact
288 ? "pagename LIKE '$word'"
289 : "pagename ILIKE '$word'");
295 select * from stat('select idxfti from version') order by ndoc desc, nentry desc, word limit 10;
297 -----------------+------+--------
300 phpwikidocument | 62 | 62
311 * use tsearch2. See schemas/psql-tsearch2.sql and /usr/share/postgresql/contrib/tsearch2.sql
312 * TODO: don't parse the words into nodes. rather replace "[ +]" with & and "-" with "!" and " or " with "|"
313 * tsearch2 query language: @@ "word | word", "word & word", ! word
314 * ~* '.*something that does not exist.*'
317 phrase search for "history lesson":
319 SELECT id FROM tab WHERE ts_idx_col @@ to_tsquery('history&lesson')
320 AND text_col ~* '.*history\\s+lesson.*';
322 The full-text index will still be used, and the regex will be used to
323 prune the results afterwards.
325 function _fulltext_match_clause($node)
327 $word = strtolower($node->word);
328 $word = str_replace(" ", "&", $word); // phrase fix
331 // clause specified above.
332 return $this->_pagename_match_clause($node) . " OR idxFTI @@ to_tsquery('$word')";
340 // c-hanging-comment-ender-p: nil
341 // indent-tabs-mode: nil