]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB_ffpgsql.php
Harmonize file footer
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB_ffpgsql.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3
4 /*
5  * Copyright (C) 2001-2009 $ThePhpWikiProgrammingTeam
6  * Copyright (C) 2010 Alain Peyrat, Alcatel-Lucent
7  *
8  * This file is part of PhpWiki.
9  *
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.
14  *
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.
19  *
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
23 */
24
25 /*
26  * Standard Alcatel-Lucent disclaimer for contributing to open source
27  *
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
33  * Contribution.
34  *
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
44  * ALONE BASIS."
45  */
46
47
48 require_once('lib/ErrorManager.php');
49 require_once('lib/WikiDB/backend/PearDB_pgsql.php');
50
51 class WikiDB_backend_PearDB_ffpgsql
52 extends WikiDB_backend_PearDB_pgsql
53 {
54     function WikiDB_backend_PearDB_ffpgsql($dbparams) {
55         $dbparams['dsn'] = str_replace('ffpgsql:', 'pgsql:', $dbparams['dsn']);
56         parent::WikiDB_backend_PearDB_pgsql($dbparams);
57
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";
61
62         pg_set_client_encoding("iso-8859-1");
63     }
64
65     function get_all_pagenames() {
66         $dbh = &$this->_dbh;
67         extract($this->_table_names);
68         $pat = PAGE_PREFIX;
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'");
74     }
75
76     function numPages($filter=false, $exclude='') {
77         $dbh = &$this->_dbh;
78         extract($this->_table_names);
79         $pat = PAGE_PREFIX;
80         $p = strlen($pat)+1;
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'");
85     }
86
87     function get_pagedata($pagename) {
88         return parent::get_pagedata(PAGE_PREFIX.$pagename);
89     }
90
91     function update_pagedata($pagename, $newdata) {
92         $dbh = &$this->_dbh;
93         $page_tbl = $this->_table_names['page_tbl'];
94
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)));
103             return;
104         }
105
106         $this->lock(array($page_tbl), true);
107         $data = $this->get_pagedata($pagename);
108         if (!$data) {
109             $data = array();
110             $this->_get_pageid($pagename, true); // Creates page record
111         }
112       
113         @$hits = (int)$data['hits'];
114         unset($data['hits']);
115
116         foreach ($newdata as $key => $val) {
117             if ($key == 'hits')
118                 $hits = (int)$val;
119             else if (empty($val))
120                 unset($data[$key]);
121             else
122                 $data[$key] = $val;
123         }
124
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'",
131                             $hits,
132                             $dbh->escapeSimple($this->_serialize($data)),
133                             $dbh->escapeSimple($pagename)));
134         */
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));
141     }
142
143     function get_latest_version($pagename) {
144         return parent::get_latest_version(PAGE_PREFIX.$pagename);
145     }
146
147     function get_previous_version($pagename, $version) {
148         return parent::get_previous_version(PAGE_PREFIX.$pagename, $version);
149     }
150
151     function get_versiondata($pagename, $version, $want_content = false) {
152         $dbh = &$this->_dbh;
153         extract($this->_table_names);
154         extract($this->_expressions);
155
156         // assert(is_string($pagename) and $pagename != "");
157         // assert($version > 0);
158       
159         //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
160         // FIXME: optimization: sometimes don't get page data?
161         if ($want_content) {
162             $fields = $this->page_tbl_fields
163                 . ",$page_tbl.pagedata as pagedata,"
164                 . $this->version_tbl_fields;
165         }
166         else {
167             $fields = $this->page_tbl_fields . ","
168                 . "mtime, minor_edit, versiondata,"
169                 . "$iscontent AS have_content";
170         }
171
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'"
177                                        . "  AND version=%d",
178                                        $dbh->escapeSimple($pagename), $version),
179                                DB_FETCHMODE_ASSOC);
180
181         return $this->_extract_version_data($result);
182     }
183
184     function get_cached_html($pagename) {
185         return parent::get_cached_html(PAGE_PREFIX.$pagename);
186     }
187
188     function set_cached_html($pagename, $data) {
189         return parent::set_cached_html(PAGE_PREFIX.$pagename, $data);
190     }
191
192     function _get_pageid($pagename, $create_if_missing = false) {
193       
194         // check id_cache
195         global $request;
196         $cache =& $request->_dbi->_cache->_id_cache;
197         if (isset($cache[$pagename])) {
198             if ($cache[$pagename] or !$create_if_missing) {
199                 return $cache[$pagename];
200             }
201         }
202
203         // attributes play this game.
204         if ($pagename === '') return 0;
205
206         $dbh = &$this->_dbh;
207         $page_tbl = $this->_table_names['page_tbl'];
208         $pagename = PAGE_PREFIX.$pagename;
209       
210         $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
211                          $dbh->escapeSimple($pagename));
212
213         if (!$create_if_missing)
214             return $dbh->getOne($query);
215
216         $id = $dbh->getOne($query);
217         if (empty($id)) {
218             $this->lock(array($page_tbl), true); // write lock
219             $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
220             $id = $max_id + 1;
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));
228         }
229         return $id;
230     }
231
232     function purge_page($pagename) {
233         $dbh = &$this->_dbh;
234         extract($this->_table_names);
235       
236         $this->lock();
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");
243             if ($nlinks) {
244                 // We're still in the link table (dangling link) so we can't delete this
245                 // altogether.
246                 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
247                 $result = 0;
248             }
249             else {
250                 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
251                 $result = 1;
252             }
253         } else {
254             $result = -1; // already purged or not existing
255         }
256         $this->unlock();
257         return $result;
258     }
259
260     function get_links($pagename, $reversed=true, $include_empty=false,
261                        $sortby='', $limit='', $exclude='',
262                        $want_relations = false)
263     {
264         $dbh = &$this->_dbh;
265         extract($this->_table_names);
266
267         if ($reversed)
268             list($have,$want) = array('linkee', 'linker');
269         else
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);
275         else
276             $exclude='';
277
278         $pat = PAGE_PREFIX;
279         $p = strlen($pat)+1;
280
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")
286             . " FROM "
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"
295             . $exclude
296             . $orderby;
297         if ($limit) {
298             // extract from,count from limit
299             list($from,$count) = $this->limit($limit);
300             $result = $dbh->limitQuery($sql, $from, $count);
301         } else {
302             $result = $dbh->query($sql);
303         }
304       
305         return new WikiDB_backend_PearDB_iter($this, $result);
306     }
307
308     function get_all_pages($include_empty=false, $sortby='', $limit='', $exclude='') {
309         $dbh = &$this->_dbh;
310         extract($this->_table_names);
311
312         $pat = PAGE_PREFIX;
313         $p = strlen($pat)+1;
314
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);
319         else
320             $exclude='';
321
322         if (strstr($orderby, 'mtime ')) { // multiple columns possible
323             if ($include_empty) {
324                 $sql = "SELECT "
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'"
330                     . $exclude
331                     . $orderby;
332             }
333             else {
334                 $sql = "SELECT "
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'"
341                     . $exclude
342                     . $orderby;
343             }
344         } else {
345             if ($include_empty) {
346                 $sql = "SELECT "
347                     . $this->page_tbl_fields
348                     . " FROM $page_tbl"
349                     . ($exclude ? " WHERE $exclude" : '')
350                     . ($exclude ? " AND " : " WHERE ")
351                         . " substring($page_tbl.pagename from 0 for $p) = '$pat'"
352                     . $orderby;
353             }
354             else {
355                 $sql = "SELECT "
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'"
360                     . $exclude
361                     . $orderby;
362             }
363         }
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);
369         } else {
370             $result = $dbh->query($sql);
371             $options = array('limit_by_db' => 0);
372         }
373         return new WikiDB_backend_PearDB_iter($this, $result, $options);
374     }
375
376     function most_popular($limit=20, $sortby='-hits') {
377         $dbh = &$this->_dbh;
378         extract($this->_table_names);
379         $pat = PAGE_PREFIX;
380         $p = strlen($pat)+1;
381         if ($limit < 0){
382             $order = "hits ASC";
383             $limit = -$limit;
384             $where = "";
385         } else {
386             $order = "hits DESC";
387             $where = " AND hits > 0";
388         }
389         $orderby = '';
390         if ($sortby != '-hits') {
391             if ($order = $this->sortby($sortby, 'db'))
392                 $orderby = " ORDER BY " . $order;
393         } else {
394             $orderby = " ORDER BY $order";
395         }
396         //$limitclause = $limit ? " LIMIT $limit" : '';
397         $sql = "SELECT "
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'"
402             . $where
403             . $orderby;
404          if ($limit) {
405              list($from, $count) = $this->limit($limit);
406              $result = $dbh->limitQuery($sql, $from, $count);
407          } else {
408              $result = $dbh->query($sql);
409          }
410
411         return new WikiDB_backend_PearDB_iter($this, $result);
412     }
413
414     function most_recent($params) {
415         $limit = 0;
416         $since = 0;
417         $include_minor_revisions = false;
418         $exclude_major_revisions = false;
419         $include_all_revisions = false;
420         extract($params);
421
422         $dbh = &$this->_dbh;
423         extract($this->_table_names);
424
425         $pick = array();
426         if ($since)
427             $pick[] = "mtime >= $since";
428         
429       
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";
434
435             if ($exclude_major_revisions) {
436                 // Include only minor revisions
437                 $pick[] = "minor_edit <> 0";
438             }
439             elseif (!$include_minor_revisions) {
440                 // Include only major revisions
441                 $pick[] = "minor_edit = 0";
442             }
443         }
444         else {
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";
449           
450             if ($exclude_major_revisions) {
451                 // Include only most recent minor revision
452                 $pick[] = 'version=latestminor';
453             }
454             elseif (!$include_minor_revisions) {
455                 // Include only most recent major revision
456                 $pick[] = 'version=latestmajor';
457             }
458             else {
459                 // Include only the latest revision (whether major or minor).
460                 $pick[] ='version=latestversion';
461             }
462         }
463         $order = "DESC";
464         if($limit < 0){
465             $order = "ASC";
466             $limit = -$limit;
467         }
468         // $limitclause = $limit ? " LIMIT $limit" : '';
469         $where_clause = $join_clause;
470         if ($pick)
471             $where_clause .= " AND " . join(" AND ", $pick);
472
473         $pat = PAGE_PREFIX;
474         $p = strlen($pat)+1;
475
476         // FIXME: use SQL_BUFFER_RESULT for mysql?
477         $sql = "SELECT "
478                . $this->page_tbl_fields . ", " . $this->version_tbl_fields
479                . " FROM $table"
480                . " WHERE $where_clause"
481                    . " AND substring($page_tbl.pagename from 0 for $p) = '$pat'"
482                . " ORDER BY mtime $order";
483         if ($limit) {
484              list($from, $count) = $this->limit($limit);
485              $result = $dbh->limitQuery($sql, $from, $count);
486         } else {
487             $result = $dbh->query($sql);
488         }
489         return new WikiDB_backend_PearDB_iter($this, $result);
490     }
491
492     function wanted_pages($exclude_from='', $exclude='', $sortby='', $limit='') {
493         $dbh = &$this->_dbh;
494         extract($this->_table_names);
495         $pat = PAGE_PREFIX;
496         $p = strlen($pat)+1;
497         if ($orderby = $this->sortby($sortby, 'db', array('pagename','wantedfrom')))
498             $orderby = 'ORDER BY ' . $orderby;
499
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);
504
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'"
514             . $exclude_from
515             . $exclude
516             . $orderby;
517         if ($limit) {
518             // oci8 error: WHERE NULL = NULL appended
519             list($from, $count) = $this->limit($limit);
520             $result = $dbh->limitQuery($sql, $from, $count * 3);
521         } else {
522             $result = $dbh->query($sql);
523         }
524         return new WikiDB_backend_PearDB_generic_iter($this, $result);
525     }
526
527     function rename_page ($pagename, $to) {
528         $dbh = &$this->_dbh;
529         extract($this->_table_names);
530       
531         $this->lock();
532         if (($id = $this->_get_pageid($pagename, false)) ) {
533             if ($new = $this->_get_pageid($to, false)) {
534                 // Cludge Alert!
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");
544             }
545             $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
546                                 $dbh->escapeSimple(PAGE_PREFIX.$to)));
547         }
548         $this->unlock();
549         return $id;
550     }
551
552     function is_wiki_page($pagename) {
553         return parent::is_wiki_page(PAGE_PREFIX.$pagename);
554     }
555
556     function increaseHitCount($pagename) {
557         return parent::increaseHitCount(PAGE_PREFIX.$pagename);
558     }
559
560     function _serialize($data) {
561         return WikiDB_backend_PearDB::_serialize($data);
562     }
563
564     /**
565      * Pack tables.
566      * NOTE: Disable vacuum, wikiuser is not the table owner
567      */
568     function optimize() {
569         return 0;
570     }
571
572     /**
573      * Title search.
574      */
575     function text_search($search, $fulltext=false, $sortby='', $limit='',
576                          $exclude='')
577     {
578         $dbh = &$this->_dbh;
579         extract($this->_table_names);
580         $pat = PAGE_PREFIX;
581         $len = strlen($pat)+1;
582         $orderby = $this->sortby($sortby, 'db');
583         if ($sortby and $orderby) $orderby = ' ORDER BY ' . $orderby;
584
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);
590       
591         $table = "$nonempty_tbl, $page_tbl";
592         $join_clause = "$nonempty_tbl.id=$page_tbl.id";
593         $fields = $this->page_tbl_fields;
594
595         if ($fulltext) {
596             $table .= ", $recent_tbl";
597             $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
598
599             $table .= ", $version_tbl";
600             $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
601
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 (";
608
609             $search_clause .= "idxFTI @@ to_tsquery('$search_string')";
610             if (!$orderby)
611                $orderby = " ORDER BY ts_rank(idxFTI, to_tsquery('$search_string')) DESC";
612         } else {
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);
616         }
617       
618         $sql = "SELECT $fields FROM $table"
619             . " WHERE $join_clause"
620             . "  AND ($search_clause)"
621             . $orderby;
622          if ($limit) {
623              list($from, $count) = $this->limit($limit);
624              $result = $dbh->limitQuery($sql, $from, $count);
625          } else {
626              $result = $dbh->query($sql);
627          }
628       
629         $iter = new WikiDB_backend_PearDB_iter($this, $result);
630         $iter->stoplisted = @$searchobj->stoplisted;
631         return $iter;
632     }
633
634      function exists_link($pagename, $link, $reversed=false) {
635          $dbh = &$this->_dbh;
636          extract($this->_table_names);
637
638          if ($reversed)
639              list($have, $want) = array('linkee', 'linker');
640          else
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'"
649                                  . " LIMIT 1");
650          return $row['result'] ? 1 : 0;
651      }
652 };
653
654 class WikiDB_backend_PearDB_ffpgsql_search
655 extends WikiDB_backend_PearDB_pgsql_search
656 {
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'");
666         } else {
667             return ($this->_case_exact
668                     ? "substring(pagename from $len) LIKE '$word'"
669                     : "substring(pagename from $len) ILIKE '$word'");
670         }
671     }
672
673     /**
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.*'
678      */
679     /*
680      phrase search for "history lesson":
681
682      SELECT id FROM tab WHERE ts_idx_col @@ to_tsquery('history&lesson')
683      AND text_col ~* '.*history\\s+lesson.*';
684
685      The full-text index will still be used, and the regex will be used to
686      prune the results afterwards.
687     */
688     function _fulltext_match_clause($node) {
689         $word = strtolower($node->word);
690         $word = str_replace(" ", "&", $word); // phrase fix
691
692         // @alu: use _quote maybe instead of direct pg_escape_string
693         $word = pg_escape_string($word);
694
695         return $word;
696     }
697 }
698
699 // Local Variables:
700 // mode: php
701 // tab-width: 8
702 // c-basic-offset: 4
703 // c-hanging-comment-ender-p: nil
704 // indent-tabs-mode: nil
705 // End: 
706 ?>