4 * Copyright (C) 2001-2009 $ThePhpWikiProgrammingTeam
5 * Copyright (C) 2010 Alain Peyrat, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Standard Alcatel-Lucent disclaimer for contributing to open source
27 * "The Fusionforge backend ("Contribution") has not been tested and/or
28 * validated for release as or in products, combinations with products or
29 * other commercial use. Any use of the Contribution is entirely made at
30 * the user's own responsibility and the user can not rely on any features,
31 * functionalities or performances Alcatel-Lucent has attributed to the
34 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
35 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
36 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
37 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
38 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
39 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
40 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
42 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
46 require_once 'lib/ErrorManager.php';
47 require_once 'lib/WikiDB/backend/PearDB_pgsql.php';
49 class WikiDB_backend_PearDB_ffpgsql
50 extends WikiDB_backend_PearDB_pgsql
52 function __construct($dbparams)
54 $dbparams['dsn'] = str_replace('ffpgsql:', 'pgsql:', $dbparams['dsn']);
55 parent::__construct($dbparams);
57 $p = strlen(PAGE_PREFIX) + 1;
58 $page_tbl = $this->_table_names['page_tbl'];
59 $this->page_tbl_fields = "$page_tbl.id AS id, substring($page_tbl.pagename from $p) AS pagename, $page_tbl.hits AS hits";
61 pg_set_client_encoding("iso-8859-1");
64 function get_all_pagenames()
67 extract($this->_table_names);
69 $p = strlen($pat) + 1;
70 return $dbh->getCol("SELECT substring(pagename from $p)"
71 . " FROM $nonempty_tbl, $page_tbl"
72 . " WHERE $nonempty_tbl.id=$page_tbl.id"
73 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'");
76 function numPages($filter = false, $exclude = '')
79 extract($this->_table_names);
81 $p = strlen($pat) + 1;
82 return $dbh->getOne("SELECT count(*)"
83 . " FROM $nonempty_tbl, $page_tbl"
84 . " WHERE $nonempty_tbl.id=$page_tbl.id"
85 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'");
88 function get_pagedata($pagename)
90 return parent::get_pagedata(PAGE_PREFIX . $pagename);
93 function update_pagedata($pagename, $newdata)
96 $page_tbl = $this->_table_names['page_tbl'];
98 // Hits is the only thing we can update in a fast manner.
99 if (count($newdata) == 1 && isset($newdata['hits'])) {
100 // Note that this will fail silently if the page does not
101 // have a record in the page table. Since it's just the
102 // hit count, who cares?
103 $pagename = PAGE_PREFIX . $pagename;
104 $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
105 $newdata['hits'], $dbh->escapeSimple($pagename)));
109 $this->lock(array($page_tbl), true);
110 $data = $this->get_pagedata($pagename);
113 $this->_get_pageid($pagename, true); // Creates page record
116 if (isset($data['hits'])) {
117 $hits = (int)$data['hits'];
118 unset($data['hits']);
123 foreach ($newdata as $key => $val) {
126 else if (empty($val))
132 /* Portability issue -- not all DBMS supports huge strings
133 * so we need to 'bind' instead of building a simple SQL statment.
134 * Note that we do not need to escapeSimple when we bind
135 $dbh->query(sprintf("UPDATE $page_tbl"
136 . " SET hits=%d, pagedata='%s'"
137 . " WHERE pagename='%s'",
139 $dbh->escapeSimple($this->_serialize($data)),
140 $dbh->escapeSimple($pagename)));
142 $pagename = PAGE_PREFIX . $pagename;
143 $dbh->query("UPDATE $page_tbl"
144 . " SET hits=?, pagedata=?"
145 . " WHERE pagename=?",
146 array($hits, $this->_serialize($data), $pagename));
147 $this->unlock(array($page_tbl));
150 function get_latest_version($pagename)
152 return parent::get_latest_version(PAGE_PREFIX . $pagename);
155 function get_previous_version($pagename, $version)
157 return parent::get_previous_version(PAGE_PREFIX . $pagename, $version);
160 function get_versiondata($pagename, $version, $want_content = false)
163 extract($this->_table_names);
164 extract($this->_expressions);
166 // assert(is_string($pagename) and $pagename != "");
167 // assert($version > 0);
169 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
170 // FIXME: optimization: sometimes don't get page data?
172 $fields = $this->page_tbl_fields
173 . ",$page_tbl.pagedata as pagedata,"
174 . $this->version_tbl_fields;
176 $fields = $this->page_tbl_fields . ","
177 . "mtime, minor_edit, versiondata,"
178 . "$iscontent AS have_content";
181 $pagename = PAGE_PREFIX . $pagename;
182 $result = $dbh->getRow(sprintf("SELECT $fields"
183 . " FROM $page_tbl, $version_tbl"
184 . " WHERE $page_tbl.id=$version_tbl.id"
185 . " AND pagename='%s'"
187 $dbh->escapeSimple($pagename), $version),
190 return $this->_extract_version_data($result);
193 function get_cached_html($pagename)
195 return parent::get_cached_html(PAGE_PREFIX . $pagename);
198 function set_cached_html($pagename, $data)
200 parent::set_cached_html(PAGE_PREFIX . $pagename, $data);
203 function _get_pageid($pagename, $create_if_missing = false)
208 $cache =& $request->_dbi->_cache->_id_cache;
209 if (isset($cache[$pagename])) {
210 if ($cache[$pagename] or !$create_if_missing) {
211 return $cache[$pagename];
215 // attributes play this game.
216 if ($pagename === '') return 0;
219 $page_tbl = $this->_table_names['page_tbl'];
220 $pagename = PAGE_PREFIX . $pagename;
222 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
223 $dbh->escapeSimple($pagename));
225 if (!$create_if_missing)
226 return $dbh->getOne($query);
228 $id = $dbh->getOne($query);
230 $this->lock(array($page_tbl), true); // write lock
231 $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
233 // requires createSequence and on mysql lock the interim table ->getSequenceName
234 //$id = $dbh->nextId($page_tbl . "_id");
235 $dbh->query(sprintf("INSERT INTO $page_tbl"
236 . " (id,pagename,hits)"
237 . " VALUES (%d,'%s',0)",
238 $id, $dbh->escapeSimple($pagename)));
239 $this->unlock(array($page_tbl));
244 function purge_page($pagename)
247 extract($this->_table_names);
250 if (($id = $this->_get_pageid($pagename, false))) {
251 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
252 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
253 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
254 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$id");
255 $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
257 // We're still in the link table (dangling link) so we can't delete this
259 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
262 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
266 $result = -1; // already purged or not existing
272 function get_links($pagename, $reversed = true, $include_empty = false,
273 $sortby = '', $limit = '', $exclude = '',
274 $want_relations = false)
277 extract($this->_table_names);
280 list($have, $want) = array('linkee', 'linker');
282 list($have, $want) = array('linker', 'linkee');
283 $orderby = $this->sortby($sortby, 'db', array('pagename'));
284 if ($orderby) $orderby = " ORDER BY $want." . $orderby;
285 if ($exclude) // array of pagenames
286 $exclude = " AND $want.pagename NOT IN " . $this->_sql_set($exclude);
291 $p = strlen($pat) + 1;
293 $qpagename = $dbh->escapeSimple($pagename);
294 // MeV+APe 2007-11-14
295 // added "dummyname" so that database accepts "ORDER BY"
296 $sql = "SELECT DISTINCT $want.id AS id, substring($want.pagename from $p) AS pagename, $want.pagename AS dummyname,"
297 . ($want_relations ? " related.pagename as linkrelation" : " $want.hits AS hits")
299 . (!$include_empty ? "$nonempty_tbl, " : '')
300 . " $page_tbl linkee, $page_tbl linker, $link_tbl "
301 . ($want_relations ? " JOIN $page_tbl related ON ($link_tbl.relation=related.id)" : '')
302 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
303 . " AND $have.pagename='$pat$qpagename'"
304 . " AND substring($want.pagename from 0 for $p) = '$pat'"
305 . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
306 //. " GROUP BY $want.id"
310 // extract from,count from limit
311 list($from, $count) = $this->limit($limit);
312 $result = $dbh->limitQuery($sql, $from, $count);
314 $result = $dbh->query($sql);
317 return new WikiDB_backend_PearDB_iter($this, $result);
320 public function get_all_pages($include_empty = false,
321 $sortby = '', $limit = '', $exclude = '')
324 extract($this->_table_names);
327 $p = strlen($pat) + 1;
329 $orderby = $this->sortby($sortby, 'db');
330 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
331 if ($exclude) // array of pagenames
332 $exclude = " AND $page_tbl.pagename NOT IN " . $this->_sql_set($exclude);
336 if (strstr($orderby, 'mtime ')) { // multiple columns possible
337 if ($include_empty) {
339 . $this->page_tbl_fields
340 . " FROM $page_tbl, $recent_tbl, $version_tbl"
341 . " WHERE $page_tbl.id=$recent_tbl.id"
342 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
343 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
348 . $this->page_tbl_fields
349 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
350 . " WHERE $nonempty_tbl.id=$page_tbl.id"
351 . " AND $page_tbl.id=$recent_tbl.id"
352 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
353 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
358 if ($include_empty) {
360 . $this->page_tbl_fields
362 . ($exclude ? " WHERE $exclude" : '')
363 . ($exclude ? " AND " : " WHERE ")
364 . " substring($page_tbl.pagename from 0 for $p) = '$pat'"
368 . $this->page_tbl_fields
369 . " FROM $nonempty_tbl, $page_tbl"
370 . " WHERE $nonempty_tbl.id=$page_tbl.id"
371 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
376 if ($limit && $orderby) {
377 // extract from,count from limit
378 list($from, $count) = $this->limit($limit);
379 $result = $dbh->limitQuery($sql, $from, $count);
380 $options = array('limit_by_db' => 1);
382 $result = $dbh->query($sql);
383 $options = array('limit_by_db' => 0);
385 return new WikiDB_backend_PearDB_iter($this, $result, $options);
388 public function most_popular($limit = 20, $sortby = '-hits')
391 extract($this->_table_names);
393 $p = strlen($pat) + 1;
399 $order = "hits DESC";
400 $where = " AND hits > 0";
403 if ($sortby != '-hits') {
404 if ($order = $this->sortby($sortby, 'db'))
405 $orderby = " ORDER BY " . $order;
407 $orderby = " ORDER BY $order";
409 //$limitclause = $limit ? " LIMIT $limit" : '';
411 . $this->page_tbl_fields
412 . " FROM $nonempty_tbl, $page_tbl"
413 . " WHERE $nonempty_tbl.id=$page_tbl.id"
414 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
418 list($from, $count) = $this->limit($limit);
419 $result = $dbh->limitQuery($sql, $from, $count);
421 $result = $dbh->query($sql);
424 return new WikiDB_backend_PearDB_iter($this, $result);
427 public function most_recent($params)
431 $include_minor_revisions = false;
432 $exclude_major_revisions = false;
433 $include_all_revisions = false;
437 extract($this->_table_names);
441 $pick[] = "mtime >= $since";
443 if ($include_all_revisions) {
444 // Include all revisions of each page.
445 $table = "$page_tbl, $version_tbl";
446 $join_clause = "$page_tbl.id=$version_tbl.id";
448 if ($exclude_major_revisions) {
449 // Include only minor revisions
450 $pick[] = "minor_edit <> 0";
451 } elseif (!$include_minor_revisions) {
452 // Include only major revisions
453 $pick[] = "minor_edit = 0";
456 $table = "$page_tbl, $recent_tbl";
457 $join_clause = "$page_tbl.id=$recent_tbl.id";
458 $table .= ", $version_tbl";
459 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
461 if ($exclude_major_revisions) {
462 // Include only most recent minor revision
463 $pick[] = 'version=latestminor';
464 } elseif (!$include_minor_revisions) {
465 // Include only most recent major revision
466 $pick[] = 'version=latestmajor';
468 // Include only the latest revision (whether major or minor).
469 $pick[] = 'version=latestversion';
477 // $limitclause = $limit ? " LIMIT $limit" : '';
478 $where_clause = $join_clause;
480 $where_clause .= " AND " . join(" AND ", $pick);
483 $p = strlen($pat) + 1;
485 // FIXME: use SQL_BUFFER_RESULT for mysql?
487 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
489 . " WHERE $where_clause"
490 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
491 . " ORDER BY mtime $order";
493 list($from, $count) = $this->limit($limit);
494 $result = $dbh->limitQuery($sql, $from, $count);
496 $result = $dbh->query($sql);
498 return new WikiDB_backend_PearDB_iter($this, $result);
501 function wanted_pages($exclude_from = '', $exclude = '', $sortby = '', $limit = '')
504 extract($this->_table_names);
506 $p = strlen($pat) + 1;
507 if ($orderby = $this->sortby($sortby, 'db', array('pagename', 'wantedfrom')))
508 $orderby = 'ORDER BY ' . $orderby;
510 if ($exclude_from) // array of pagenames
511 $exclude_from = " AND pp.pagename NOT IN " . $this->_sql_set($exclude_from);
512 if ($exclude) // array of pagenames
513 $exclude = " AND p.pagename NOT IN " . $this->_sql_set($exclude);
515 $p = strlen(PAGE_PREFIX) + 1;
516 $sql = "SELECT substring(p.pagename from $p) AS wantedfrom, substring(pp.pagename from $p) AS pagename"
517 . " FROM $page_tbl p, $link_tbl linked"
518 . " LEFT JOIN $page_tbl pp ON linked.linkto = pp.id"
519 . " LEFT JOIN $nonempty_tbl ne ON linked.linkto = ne.id"
520 . " WHERE ne.id IS NULL"
521 . " AND p.id = linked.linkfrom"
522 . " AND substring(p.pagename from 0 for $p) = '$pat'"
523 . " AND substring(pp.pagename from 0 for $p) = '$pat'"
528 // oci8 error: WHERE NULL = NULL appended
529 list($from, $count) = $this->limit($limit);
530 $result = $dbh->limitQuery($sql, $from, $count * 3);
532 $result = $dbh->query($sql);
534 return new WikiDB_backend_PearDB_generic_iter($this, $result);
537 function rename_page($pagename, $to)
540 extract($this->_table_names);
543 if (($id = $this->_get_pageid($pagename, false))) {
544 if ($new = $this->_get_pageid($to, false)) {
546 // This page does not exist (already verified before), but exists in the page table.
547 // So we delete this page.
548 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
549 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
550 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
551 // We have to fix all referring tables to the old id
552 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
553 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
554 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
556 $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
557 $dbh->escapeSimple(PAGE_PREFIX . $to)));
563 function is_wiki_page($pagename)
565 return parent::is_wiki_page(PAGE_PREFIX . $pagename);
568 function increaseHitCount($pagename)
570 parent::increaseHitCount(PAGE_PREFIX . $pagename);
573 function _serialize($data)
575 return WikiDB_backend_PearDB::_serialize($data);
580 * NOTE: Disable vacuum, wikiuser is not the table owner
588 * Text search (title or full text)
590 public function text_search($search, $fulltext = false,
591 $sortby = '', $limit = '', $exclude = '')
594 extract($this->_table_names);
596 $len = strlen($pat) + 1;
597 $orderby = $this->sortby($sortby, 'db');
598 if ($sortby and $orderby) $orderby = ' ORDER BY ' . $orderby;
600 $searchclass = get_class($this) . "_search";
601 // no need to define it everywhere and then fallback. memory!
602 if (!class_exists($searchclass))
603 $searchclass = "WikiDB_backend_PearDB_search";
604 $searchobj = new $searchclass($search, $dbh);
606 $table = "$nonempty_tbl, $page_tbl";
607 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
608 $fields = $this->page_tbl_fields;
611 $table .= ", $recent_tbl";
612 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
614 $table .= ", $version_tbl";
615 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
617 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
618 // TODO: title still ignored, need better rank and subselect
619 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
620 $search_string = $search->makeTsearch2SqlClauseObj($callback);
621 $search_string = str_replace('%', '', $search_string);
622 $search_clause = "substring(plugin_wiki_page.pagename from 0 for $len) = '$pat') AND (";
624 $search_clause .= "idxFTI @@ plainto_tsquery('english', '$search_string')";
626 $orderby = " ORDER BY ts_rank(idxFTI, plainto_tsquery('english', '$search_string')) DESC";
628 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
629 $search_clause = "substring(plugin_wiki_page.pagename from 0 for $len) = '$pat') AND (";
630 $search_clause .= $search->makeSqlClauseObj($callback);
633 $sql = "SELECT $fields FROM $table"
634 . " WHERE $join_clause"
635 . " AND ($search_clause)"
638 list($from, $count) = $this->limit($limit);
639 $result = $dbh->limitQuery($sql, $from, $count);
641 $result = $dbh->query($sql);
644 $iter = new WikiDB_backend_PearDB_iter($this, $result);
645 $iter->stoplisted = @$searchobj->stoplisted;
649 function exists_link($pagename, $link, $reversed = false)
652 extract($this->_table_names);
655 list($have, $want) = array('linkee', 'linker');
657 list($have, $want) = array('linker', 'linkee');
658 $qpagename = $dbh->escapeSimple($pagename);
659 $qlink = $dbh->escapeSimple($link);
660 $row = $dbh->GetRow("SELECT $want.pagename as result"
661 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
662 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
663 . " AND $have.pagename='$qpagename'"
664 . " AND $want.pagename='$qlink'"
666 return $row['result'] ? 1 : 0;
670 class WikiDB_backend_PearDB_ffpgsql_search
671 extends WikiDB_backend_PearDB_pgsql_search
673 function _pagename_match_clause($node)
675 $word = $node->sql();
676 // @alu: use _quote maybe instead of direct pg_escape_string
677 $word = pg_escape_string($word);
678 $len = strlen(PAGE_PREFIX) + 1;
679 if ($node->op == 'REGEX') { // posix regex extensions
680 return ($this->_case_exact
681 ? "substring(pagename from $len) ~* '$word'"
682 : "substring(pagename from $len) ~ '$word'");
684 return ($this->_case_exact
685 ? "substring(pagename from $len) LIKE '$word'"
686 : "substring(pagename from $len) ILIKE '$word'");
691 * use tsearch2. See schemas/psql-tsearch2.sql and /usr/share/postgresql/contrib/tsearch2.sql
692 * TODO: don't parse the words into nodes. rather replace "[ +]" with & and "-" with "!" and " or " with "|"
693 * tsearch2 query language: @@ "word | word", "word & word", ! word
694 * ~* '.*something that does not exist.*'
697 phrase search for "history lesson":
699 SELECT id FROM tab WHERE ts_idx_col @@ to_tsquery('history&lesson')
700 AND text_col ~* '.*history\\s+lesson.*';
702 The full-text index will still be used, and the regex will be used to
703 prune the results afterwards.
705 function _fulltext_match_clause($node)
707 $word = strtolower($node->word);
708 // $word = str_replace(" ", "&", $word); // phrase fix
710 // @alu: use _quote maybe instead of direct pg_escape_string
711 $word = pg_escape_string($word);
721 // c-hanging-comment-ender-p: nil
722 // indent-tabs-mode: nil