]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_pgsql.php3
The to-do list.
[SourceForge/phpwiki.git] / wiki_pgsql.php3
1 <!-- $Id: wiki_pgsql.php3,v 1.4 2000-06-18 03:59:20 wainstead Exp $ -->
2 <?
3
4    /*
5       Database functions:
6
7       OpenDataBase($dbname)
8       CloseDataBase($dbi)
9       RetrievePage($dbi, $pagename)
10       InsertPage($dbi, $pagename, $pagehash)
11       IsWikiPage($dbi, $pagename)
12       InitTitleSearch($dbi, $search)
13       TitleSearchNextMatch($dbi, &$pos)
14       InitFullSearch($dbi, $search)
15       FullSearchNextMatch($dbi, &$pos)
16    */
17
18
19    // open a database and return a hash
20
21    function OpenDataBase($table) {
22       global $WikiDataBase, $pg_dbhost, $pg_dbport;
23
24       $connectstring = "host=$pg_dbhost port=$pg_dbport dbname=$WikiDataBase";
25
26       if (!($dbc = pg_pconnect($connectstring))) {
27          echo "Cannot establish connection to database, giving up.";
28          exit();
29       }
30
31       $dbi['dbc'] = $dbc;
32       $dbi['table'] = $table;
33       return $dbi;
34    }
35
36
37    function CloseDataBase($dbi) {
38       // NOOP: we use persistent database connections
39    }
40
41
42    // Return hash of page + attributes or default
43    function RetrievePage($dbi, $pagename) {
44       $pagename = addslashes($pagename);
45       $query = "select * from $dbi[table] where pagename='$pagename'";
46
47       $res = pg_exec($dbi['dbc'], $query);
48
49       if (pg_numrows($res)) {
50          if ($pagehash = pg_fetch_array($res, 0)) {
51             // don't forget to unserialize the references
52             if ($pagehash['refs']) {
53                $pagehash['refs'] = unserialize($pagehash['refs']);
54             }
55             return $pagehash;
56          }
57       }
58
59       // if we reach this the query failed
60       return -1;
61    }
62
63
64    // Either insert or replace a key/value (a page)
65    function InsertPage($dbi, $pagename, $pagehash) {
66       $pagename = addslashes($pagename);
67       $pagedata = addslashes(serialize($pagehash));
68
69       if (!mysql_query("replace into $dbi[table] (page, hash) values ('$pagename', '$pagedata')", $dbi['dbc'])) {
70             echo "error writing value";
71             exit();
72       }
73    }
74
75
76
77    function IsWikiPage($dbi, $pagename) {
78       $pagename = addslashes($pagename);
79       $query = "select count(*) from $dbi[table] where pagename='$pagename'";
80       $res = pg_exec($query);
81       //return(pg_numrows($res));
82       $array = pg_fetch_array($res, 0);
83       return $array[0];
84    }
85
86
87    // setup for title-search
88    function InitTitleSearch($dbi, $search) {
89       $search = addslashes($search);
90       $res = mysql_query("select page from $dbi[table] where page like '%$search%' order by page", $dbi["dbc"]);
91
92       return $res;
93    }
94
95
96    // iterating through database
97    function TitleSearchNextMatch($dbi, $res) {
98       if($o = mysql_fetch_object($res)) {
99          return $o->page;
100       }
101       else {
102          return 0;
103       }
104    }
105
106
107    // setup for full-text search
108    function InitFullSearch($dbi, $search) {
109       $search = addslashes($search);
110       $res = mysql_query("select page,hash from $dbi[table] where hash like '%$search%'", $dbi["dbc"]);
111
112       return $res;
113    }
114
115    // iterating through database
116    function FullSearchNextMatch($dbi, $res) {
117       if($o = mysql_fetch_object($res)) {
118          $page['name'] = $o->page;
119          $page['hash'] = unserialize($o->hash);
120          return $page;
121       }
122       else {
123          return 0;
124       }
125    }
126
127
128 ?>