5 * Copyright (C) 2001-2009 $ThePhpWikiProgrammingTeam
6 * Copyright (C) 2010 Alain Peyrat, Alcatel-Lucent
8 * This file is part of PhpWiki.
10 * PhpWiki is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * PhpWiki is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with PhpWiki; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Standard Alcatel-Lucent disclaimer for contributing to open source
28 * "The Fusionforge backend ("Contribution") has not been tested and/or
29 * validated for release as or in products, combinations with products or
30 * other commercial use. Any use of the Contribution is entirely made at
31 * the user's own responsibility and the user can not rely on any features,
32 * functionalities or performances Alcatel-Lucent has attributed to the
35 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
36 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
37 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
38 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
39 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
40 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
41 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
43 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
48 require_once('lib/ErrorManager.php');
49 require_once('lib/WikiDB/backend/PearDB_pgsql.php');
51 class WikiDB_backend_PearDB_ffpgsql
52 extends WikiDB_backend_PearDB_pgsql
54 function WikiDB_backend_PearDB_ffpgsql($dbparams) {
55 $dbparams['dsn'] = str_replace('ffpgsql:', 'pgsql:', $dbparams['dsn']);
56 parent::WikiDB_backend_PearDB_pgsql($dbparams);
58 $p = strlen(PAGE_PREFIX)+1;
59 $page_tbl = $this->_table_names['page_tbl'];
60 $this->page_tbl_fields = "$page_tbl.id AS id, substring($page_tbl.pagename from $p) AS pagename, $page_tbl.hits AS hits";
62 pg_set_client_encoding("iso-8859-1");
65 function get_all_pagenames() {
67 extract($this->_table_names);
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='') {
78 extract($this->_table_names);
81 return $dbh->getOne("SELECT count(*)"
82 . " FROM $nonempty_tbl, $page_tbl"
83 . " WHERE $nonempty_tbl.id=$page_tbl.id"
84 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'");
87 function get_pagedata($pagename) {
88 return parent::get_pagedata(PAGE_PREFIX.$pagename);
91 function update_pagedata($pagename, $newdata) {
93 $page_tbl = $this->_table_names['page_tbl'];
95 // Hits is the only thing we can update in a fast manner.
96 if (count($newdata) == 1 && isset($newdata['hits'])) {
97 // Note that this will fail silently if the page does not
98 // have a record in the page table. Since it's just the
99 // hit count, who cares?
100 $pagename = PAGE_PREFIX.$pagename;
101 $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
102 $newdata['hits'], $dbh->escapeSimple($pagename)));
106 $this->lock(array($page_tbl), true);
107 $data = $this->get_pagedata($pagename);
110 $this->_get_pageid($pagename, true); // Creates page record
113 @$hits = (int)$data['hits'];
114 unset($data['hits']);
116 foreach ($newdata as $key => $val) {
119 else if (empty($val))
125 /* Portability issue -- not all DBMS supports huge strings
126 * so we need to 'bind' instead of building a simple SQL statment.
127 * Note that we do not need to escapeSimple when we bind
128 $dbh->query(sprintf("UPDATE $page_tbl"
129 . " SET hits=%d, pagedata='%s'"
130 . " WHERE pagename='%s'",
132 $dbh->escapeSimple($this->_serialize($data)),
133 $dbh->escapeSimple($pagename)));
135 $pagename = PAGE_PREFIX.$pagename;
136 $dbh->query("UPDATE $page_tbl"
137 . " SET hits=?, pagedata=?"
138 . " WHERE pagename=?",
139 array($hits, $this->_serialize($data), $pagename));
140 $this->unlock(array($page_tbl));
143 function get_latest_version($pagename) {
144 return parent::get_latest_version(PAGE_PREFIX.$pagename);
147 function get_previous_version($pagename, $version) {
148 return parent::get_previous_version(PAGE_PREFIX.$pagename, $version);
151 function get_versiondata($pagename, $version, $want_content = false) {
153 extract($this->_table_names);
154 extract($this->_expressions);
156 // assert(is_string($pagename) and $pagename != "");
157 // assert($version > 0);
159 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
160 // FIXME: optimization: sometimes don't get page data?
162 $fields = $this->page_tbl_fields
163 . ",$page_tbl.pagedata as pagedata,"
164 . $this->version_tbl_fields;
167 $fields = $this->page_tbl_fields . ","
168 . "mtime, minor_edit, versiondata,"
169 . "$iscontent AS have_content";
172 $pagename = PAGE_PREFIX.$pagename;
173 $result = $dbh->getRow(sprintf("SELECT $fields"
174 . " FROM $page_tbl, $version_tbl"
175 . " WHERE $page_tbl.id=$version_tbl.id"
176 . " AND pagename='%s'"
178 $dbh->escapeSimple($pagename), $version),
181 return $this->_extract_version_data($result);
184 function get_cached_html($pagename) {
185 return parent::get_cached_html(PAGE_PREFIX.$pagename);
188 function set_cached_html($pagename, $data) {
189 return parent::set_cached_html(PAGE_PREFIX.$pagename, $data);
192 function _get_pageid($pagename, $create_if_missing = false) {
196 $cache =& $request->_dbi->_cache->_id_cache;
197 if (isset($cache[$pagename])) {
198 if ($cache[$pagename] or !$create_if_missing) {
199 return $cache[$pagename];
203 // attributes play this game.
204 if ($pagename === '') return 0;
207 $page_tbl = $this->_table_names['page_tbl'];
208 $pagename = PAGE_PREFIX.$pagename;
210 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
211 $dbh->escapeSimple($pagename));
213 if (!$create_if_missing)
214 return $dbh->getOne($query);
216 $id = $dbh->getOne($query);
218 $this->lock(array($page_tbl), true); // write lock
219 $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
221 // requires createSequence and on mysql lock the interim table ->getSequenceName
222 //$id = $dbh->nextId($page_tbl . "_id");
223 $dbh->query(sprintf("INSERT INTO $page_tbl"
224 . " (id,pagename,hits)"
225 . " VALUES (%d,'%s',0)",
226 $id, $dbh->escapeSimple($pagename)));
227 $this->unlock(array($page_tbl));
232 function purge_page($pagename) {
234 extract($this->_table_names);
237 if ( ($id = $this->_get_pageid($pagename, false)) ) {
238 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
239 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
240 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
241 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$id");
242 $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
244 // We're still in the link table (dangling link) so we can't delete this
246 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
250 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
254 $result = -1; // already purged or not existing
260 function get_links($pagename, $reversed=true, $include_empty=false,
261 $sortby='', $limit='', $exclude='',
262 $want_relations = false)
265 extract($this->_table_names);
268 list($have,$want) = array('linkee', 'linker');
270 list($have,$want) = array('linker', 'linkee');
271 $orderby = $this->sortby($sortby, 'db', array('pagename'));
272 if ($orderby) $orderby = " ORDER BY $want." . $orderby;
273 if ($exclude) // array of pagenames
274 $exclude = " AND $want.pagename NOT IN ".$this->_sql_set($exclude);
281 $qpagename = $dbh->escapeSimple($pagename);
282 // MeV+APe 2007-11-14
283 // added "dummyname" so that database accepts "ORDER BY"
284 $sql = "SELECT DISTINCT $want.id AS id, substring($want.pagename from $p) AS pagename, $want.pagename AS dummyname,"
285 . ($want_relations ? " related.pagename as linkrelation" : " $want.hits AS hits")
287 . (!$include_empty ? "$nonempty_tbl, " : '')
288 . " $page_tbl linkee, $page_tbl linker, $link_tbl "
289 . ($want_relations ? " JOIN $page_tbl related ON ($link_tbl.relation=related.id)" : '')
290 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
291 . " AND $have.pagename='$pat$qpagename'"
292 . " AND substring($want.pagename from 0 for $p) = '$pat'"
293 . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
294 //. " GROUP BY $want.id"
298 // extract from,count from limit
299 list($from,$count) = $this->limit($limit);
300 $result = $dbh->limitQuery($sql, $from, $count);
302 $result = $dbh->query($sql);
305 return new WikiDB_backend_PearDB_iter($this, $result);
308 function get_all_pages($include_empty=false, $sortby='', $limit='', $exclude='') {
310 extract($this->_table_names);
315 $orderby = $this->sortby($sortby, 'db');
316 if ($orderby) $orderby = ' ORDER BY ' . $orderby;
317 if ($exclude) // array of pagenames
318 $exclude = " AND $page_tbl.pagename NOT IN ".$this->_sql_set($exclude);
322 if (strstr($orderby, 'mtime ')) { // multiple columns possible
323 if ($include_empty) {
325 . $this->page_tbl_fields
326 . " FROM $page_tbl, $recent_tbl, $version_tbl"
327 . " WHERE $page_tbl.id=$recent_tbl.id"
328 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
329 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
335 . $this->page_tbl_fields
336 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
337 . " WHERE $nonempty_tbl.id=$page_tbl.id"
338 . " AND $page_tbl.id=$recent_tbl.id"
339 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
340 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
345 if ($include_empty) {
347 . $this->page_tbl_fields
349 . ($exclude ? " WHERE $exclude" : '')
350 . ($exclude ? " AND " : " WHERE ")
351 . " substring($page_tbl.pagename from 0 for $p) = '$pat'"
356 . $this->page_tbl_fields
357 . " FROM $nonempty_tbl, $page_tbl"
358 . " WHERE $nonempty_tbl.id=$page_tbl.id"
359 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
364 if ($limit && $orderby) {
365 // extract from,count from limit
366 list($from,$count) = $this->limit($limit);
367 $result = $dbh->limitQuery($sql, $from, $count);
368 $options = array('limit_by_db' => 1);
370 $result = $dbh->query($sql);
371 $options = array('limit_by_db' => 0);
373 return new WikiDB_backend_PearDB_iter($this, $result, $options);
376 function most_popular($limit=20, $sortby='-hits') {
378 extract($this->_table_names);
386 $order = "hits DESC";
387 $where = " AND hits > 0";
390 if ($sortby != '-hits') {
391 if ($order = $this->sortby($sortby, 'db'))
392 $orderby = " ORDER BY " . $order;
394 $orderby = " ORDER BY $order";
396 //$limitclause = $limit ? " LIMIT $limit" : '';
398 . $this->page_tbl_fields
399 . " FROM $nonempty_tbl, $page_tbl"
400 . " WHERE $nonempty_tbl.id=$page_tbl.id"
401 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
405 list($from, $count) = $this->limit($limit);
406 $result = $dbh->limitQuery($sql, $from, $count);
408 $result = $dbh->query($sql);
411 return new WikiDB_backend_PearDB_iter($this, $result);
414 function most_recent($params) {
417 $include_minor_revisions = false;
418 $exclude_major_revisions = false;
419 $include_all_revisions = false;
423 extract($this->_table_names);
427 $pick[] = "mtime >= $since";
430 if ($include_all_revisions) {
431 // Include all revisions of each page.
432 $table = "$page_tbl, $version_tbl";
433 $join_clause = "$page_tbl.id=$version_tbl.id";
435 if ($exclude_major_revisions) {
436 // Include only minor revisions
437 $pick[] = "minor_edit <> 0";
439 elseif (!$include_minor_revisions) {
440 // Include only major revisions
441 $pick[] = "minor_edit = 0";
445 $table = "$page_tbl, $recent_tbl";
446 $join_clause = "$page_tbl.id=$recent_tbl.id";
447 $table .= ", $version_tbl";
448 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
450 if ($exclude_major_revisions) {
451 // Include only most recent minor revision
452 $pick[] = 'version=latestminor';
454 elseif (!$include_minor_revisions) {
455 // Include only most recent major revision
456 $pick[] = 'version=latestmajor';
459 // Include only the latest revision (whether major or minor).
460 $pick[] ='version=latestversion';
468 // $limitclause = $limit ? " LIMIT $limit" : '';
469 $where_clause = $join_clause;
471 $where_clause .= " AND " . join(" AND ", $pick);
476 // FIXME: use SQL_BUFFER_RESULT for mysql?
478 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
480 . " WHERE $where_clause"
481 . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
482 . " ORDER BY mtime $order";
484 list($from, $count) = $this->limit($limit);
485 $result = $dbh->limitQuery($sql, $from, $count);
487 $result = $dbh->query($sql);
489 return new WikiDB_backend_PearDB_iter($this, $result);
492 function wanted_pages($exclude_from='', $exclude='', $sortby='', $limit='') {
494 extract($this->_table_names);
497 if ($orderby = $this->sortby($sortby, 'db', array('pagename','wantedfrom')))
498 $orderby = 'ORDER BY ' . $orderby;
500 if ($exclude_from) // array of pagenames
501 $exclude_from = " AND pp.pagename NOT IN ".$this->_sql_set($exclude_from);
502 if ($exclude) // array of pagenames
503 $exclude = " AND p.pagename NOT IN ".$this->_sql_set($exclude);
505 $p = strlen(PAGE_PREFIX)+1;
506 $sql = "SELECT substring(p.pagename from $p) AS wantedfrom, substring(pp.pagename from $p) AS pagename"
507 . " FROM $page_tbl p, $link_tbl linked"
508 . " LEFT JOIN $page_tbl pp ON linked.linkto = pp.id"
509 . " LEFT JOIN $nonempty_tbl ne ON linked.linkto = ne.id"
510 . " WHERE ne.id IS NULL"
511 . " AND p.id = linked.linkfrom"
512 . " AND substring(p.pagename from 0 for $p) = '$pat'"
513 . " AND substring(pp.pagename from 0 for $p) = '$pat'"
518 // oci8 error: WHERE NULL = NULL appended
519 list($from, $count) = $this->limit($limit);
520 $result = $dbh->limitQuery($sql, $from, $count * 3);
522 $result = $dbh->query($sql);
524 return new WikiDB_backend_PearDB_generic_iter($this, $result);
527 function rename_page ($pagename, $to) {
529 extract($this->_table_names);
532 if (($id = $this->_get_pageid($pagename, false)) ) {
533 if ($new = $this->_get_pageid($to, false)) {
535 // This page does not exist (already verified before), but exists in the page table.
536 // So we delete this page.
537 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
538 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
539 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
540 // We have to fix all referring tables to the old id
541 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
542 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
543 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
545 $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
546 $dbh->escapeSimple(PAGE_PREFIX.$to)));
552 function is_wiki_page($pagename) {
553 return parent::is_wiki_page(PAGE_PREFIX.$pagename);
556 function increaseHitCount($pagename) {
557 return parent::increaseHitCount(PAGE_PREFIX.$pagename);
560 function _serialize($data) {
561 return WikiDB_backend_PearDB::_serialize($data);
566 * NOTE: Disable vacuum, wikiuser is not the table owner
568 function optimize() {
575 function text_search($search, $fulltext=false, $sortby='', $limit='',
579 extract($this->_table_names);
581 $len = strlen($pat)+1;
582 $orderby = $this->sortby($sortby, 'db');
583 if ($sortby and $orderby) $orderby = ' ORDER BY ' . $orderby;
585 $searchclass = get_class($this)."_search";
586 // no need to define it everywhere and then fallback. memory!
587 if (!class_exists($searchclass))
588 $searchclass = "WikiDB_backend_PearDB_search";
589 $searchobj = new $searchclass($search, $dbh);
591 $table = "$nonempty_tbl, $page_tbl";
592 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
593 $fields = $this->page_tbl_fields;
596 $table .= ", $recent_tbl";
597 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
599 $table .= ", $version_tbl";
600 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
602 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
603 // TODO: title still ignored, need better rank and subselect
604 $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
605 $search_string = $search->makeTsearch2SqlClauseObj($callback);
606 $search_string = str_replace(array("%"," "), array("","&"), $search_string);
607 $search_clause = "substring(plugin_wiki_page.pagename from 0 for $len) = '$pat') AND (";
609 $search_clause .= "idxFTI @@ to_tsquery('$search_string')";
611 $orderby = " ORDER BY ts_rank(idxFTI, to_tsquery('$search_string')) DESC";
613 $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
614 $search_clause = "substring(plugin_wiki_page.pagename from 0 for $len) = '$pat') AND (";
615 $search_clause .= $search->makeSqlClauseObj($callback);
618 $sql = "SELECT $fields FROM $table"
619 . " WHERE $join_clause"
620 . " AND ($search_clause)"
623 list($from, $count) = $this->limit($limit);
624 $result = $dbh->limitQuery($sql, $from, $count);
626 $result = $dbh->query($sql);
629 $iter = new WikiDB_backend_PearDB_iter($this, $result);
630 $iter->stoplisted = @$searchobj->stoplisted;
634 function exists_link($pagename, $link, $reversed=false) {
636 extract($this->_table_names);
639 list($have, $want) = array('linkee', 'linker');
641 list($have, $want) = array('linker', 'linkee');
642 $qpagename = $dbh->escapeSimple($pagename);
643 $qlink = $dbh->escapeSimple($link);
644 $row = $dbh->GetRow("SELECT $want.pagename as result"
645 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
646 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
647 . " AND $have.pagename='$qpagename'"
648 . " AND $want.pagename='$qlink'"
650 return $row['result'] ? 1 : 0;
654 class WikiDB_backend_PearDB_ffpgsql_search
655 extends WikiDB_backend_PearDB_pgsql_search
657 function _pagename_match_clause($node) {
658 $word = $node->sql();
659 // @alu: use _quote maybe instead of direct pg_escape_string
660 $word = pg_escape_string($word);
661 $len = strlen(PAGE_PREFIX)+1;
662 if ($node->op == 'REGEX') { // posix regex extensions
663 return ($this->_case_exact
664 ? "substring(pagename from $len) ~* '$word'"
665 : "substring(pagename from $len) ~ '$word'");
667 return ($this->_case_exact
668 ? "substring(pagename from $len) LIKE '$word'"
669 : "substring(pagename from $len) ILIKE '$word'");
674 * use tsearch2. See schemas/psql-tsearch2.sql and /usr/share/postgresql/contrib/tsearch2.sql
675 * TODO: don't parse the words into nodes. rather replace "[ +]" with & and "-" with "!" and " or " with "|"
676 * tsearch2 query language: @@ "word | word", "word & word", ! word
677 * ~* '.*something that does not exist.*'
680 phrase search for "history lesson":
682 SELECT id FROM tab WHERE ts_idx_col @@ to_tsquery('history&lesson')
683 AND text_col ~* '.*history\\s+lesson.*';
685 The full-text index will still be used, and the regex will be used to
686 prune the results afterwards.
688 function _fulltext_match_clause($node) {
689 $word = strtolower($node->word);
690 $word = str_replace(" ", "&", $word); // phrase fix
692 // @alu: use _quote maybe instead of direct pg_escape_string
693 $word = pg_escape_string($word);
703 // c-hanging-comment-ender-p: nil
704 // indent-tabs-mode: nil