1 <!-- $Id: wiki_pgsql.php3,v 1.9 2000-06-23 01:40:01 wainstead Exp $ -->
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)
19 // open a database and return a hash
21 function OpenDataBase($table) {
22 global $WikiDataBase, $pg_dbhost, $pg_dbport;
24 $connectstring = "host=$pg_dbhost port=$pg_dbport dbname=$WikiDataBase";
26 if (!($dbc = pg_pconnect($connectstring))) {
27 echo "Cannot establish connection to database, giving up.";
32 $dbi['table'] = $table;
33 // echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
38 function CloseDataBase($dbi) {
39 // NOOP: we use persistent database connections
43 // Return hash of page + attributes or default
44 function RetrievePage($dbi, $pagename) {
45 $pagename = addslashes($pagename);
46 $query = "select * from $dbi[table] where pagename='$pagename'";
48 $res = pg_exec($dbi['dbc'], $query);
50 if (pg_numrows($res)) {
51 if ($array = pg_fetch_array($res, 0)) {
52 while (list($key, $val) = each($array)) {
53 // pg_fetch_array gives us all the values twice,
54 // so we have to manually edit out the indices
55 if (gettype($key) == "integer") {
58 $pagehash[$key] = $val;
61 // unserialize/explode content
62 $pagehash['refs'] = unserialize($pagehash['refs']);
63 $pagehash['content'] = explode("\n", $pagehash['content']);
69 // if we reach this the query failed
74 // Either insert or replace a key/value (a page)
75 function InsertPage($dbi, $pagename, $pagehash) {
76 $pagename = addslashes($pagename);
77 // echo "<p>dbi in InsertPage: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>";
79 // prepare the content for storage
80 if (!isset($pagehash["pagename"]))
81 $pagehash["pagename"] = $pagename;
82 if (!isset($pagehash["flags"]))
83 $pagehash["flags"] = 0;
84 $pagehash["author"] = addslashes($pagehash["author"]);
85 $pagehash["content"] = implode("\n", $pagehash["content"]);
86 $pagehash["content"] = addslashes($pagehash["content"]);
87 $pagehash["pagename"] = addslashes($pagehash["pagename"]);
88 $pagehash["refs"] = serialize($pagehash["refs"]);
90 // temporary hack until the time stuff is brought up to date
91 $pagehash["created"] = time();
92 $pagehash["lastmodified"] = time();
94 if (IsWikiPage($dbi, $pagename)) {
96 $PAIRS = "author='$pagehash[author]'," .
97 "content='$pagehash[content]'," .
98 "created=$pagehash[created]," .
99 "flags=$pagehash[flags]," .
100 "lastmodified=$pagehash[lastmodified]," .
101 "pagename='$pagehash[pagename]'," .
102 "refs='$pagehash[refs]'," .
103 "version=$pagehash[version]";
105 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
109 // build up the column names and values for the query
111 $COLUMNS = "author, content, created, flags, " .
112 "lastmodified, pagename, refs, version";
114 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
115 "$pagehash[created], $pagehash[flags], " .
116 "$pagehash[lastmodified], '$pagehash[pagename]', " .
117 "'$pagehash[refs]', $pagehash[version]";
120 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
123 // echo "<p>Query: $query<p>\n";
124 $retval = pg_exec($dbi['dbc'], $query);
125 if ($retval == false)
126 echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
132 function IsWikiPage($dbi, $pagename) {
133 $pagename = addslashes($pagename);
134 $query = "select count(*) from $dbi[table] where pagename='$pagename'";
135 $res = pg_exec($query);
136 $array = pg_fetch_array($res, 0);
141 // setup for title-search
142 function InitTitleSearch($dbi, $search) {
144 global $search_counter;
147 $search = strtolower($search);
148 $search = addslashes($search);
149 $query = "select pagename from $dbi[table] where lower(pagename) " .
150 "like '%$search%' order by pagename";
151 // echo "search query: $query<br>\n";
152 $res = pg_exec($dbi["dbc"], $query);
158 // iterating through database
159 function TitleSearchNextMatch($dbi, $res) {
160 global $search_counter;
161 if($o = @pg_fetch_object($res, $search_counter)) {
170 // setup for full-text search
171 function InitFullSearch($dbi, $search) {
172 global $search_counter;
174 $search = addslashes($search);
175 $search = addslashes($search);
176 $query = "select pagename,content from $dbi[table] " .
177 "where lower(content) like '%$search%'";
179 $res = pg_exec($dbi["dbc"], $query);
184 // iterating through database
185 function FullSearchNextMatch($dbi, $res) {
186 global $search_counter;
187 if ($hash = @pg_fetch_array($res, $search_counter)) {
189 $page['pagename'] = $hash["pagename"];
190 $page['content'] = explode("\n", $hash["content"]);