]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_pgsql.php3
InsertPage() and RetrievePage() now work mostly. I noticed during
[SourceForge/phpwiki.git] / wiki_pgsql.php3
1 <!-- $Id: wiki_pgsql.php3,v 1.6 2000-06-20 04:50:57 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 //      echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
34       return $dbi;
35    }
36
37
38    function CloseDataBase($dbi) {
39       // NOOP: we use persistent database connections
40    }
41
42
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'";
47
48       $res = pg_exec($dbi['dbc'], $query);
49
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") {
56                   continue;
57                }
58                $pagehash[$key] = $val;
59             }
60
61             // unserialize/explode content
62             $pagehash['refs'] = unserialize($pagehash['refs']);
63             $pagehash['content'] = explode("\n", $pagehash['content']);
64
65             return $pagehash;
66          }
67       }
68
69       // if we reach this the query failed
70       return -1;
71    }
72
73
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>";
78
79       // prepare the content for storage
80       $pagehash["author"] = addslashes($pagehash["author"]);
81       $pagehash["content"] = implode("\n", $pagehash["content"]);
82       $pagehash["content"] = addslashes($pagehash["content"]);
83       $pagehash["pagename"] = addslashes($pagehash["pagename"]);
84       $pagehash["refs"] = serialize($pagehash["refs"]);
85
86       // temporary hack until the time stuff is brought up to date
87       $pagehash["created"] = time();
88       $pagehash["lastmodified"] = time();
89
90       if (IsWikiPage($dbi, $pagename)) {
91
92          $PAIRS = "author='$pagehash[author]'," .
93                   "content='$pagehash[content]'," .
94                   "created=$pagehash[created]," .
95                   "flags=$pagehash[flags]," .
96                   "lastmodified=$pagehash[lastmodified]," .
97                   "pagename='$pagehash[pagename]'," .
98                   "refs='$pagehash[refs]'," .
99                   "version=$pagehash[version]";
100
101          $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
102
103       } else {
104          // do an insert
105          // build up the column names and values for the query
106
107          $COLUMNS = "author, content, created, flags, " .
108                     "lastmodified, pagename, refs, version";
109
110          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
111                     "$pagehash[created], $pagehash[flags], " .
112                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
113                     "'$pagehash[refs]', $pagehash[version]";
114
115
116          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
117       }
118
119 //      echo "<p>Query: $query<p>\n";
120       $retval = pg_exec($dbi['dbc'], $query);
121       if ($retval == false) 
122          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
123
124    }
125
126
127
128    function IsWikiPage($dbi, $pagename) {
129       $pagename = addslashes($pagename);
130       $query = "select count(*) from $dbi[table] where pagename='$pagename'";
131       $res = pg_exec($query);
132       $array = pg_fetch_array($res, 0);
133       return $array[0];
134    }
135
136
137    // setup for title-search
138    function InitTitleSearch($dbi, $search) {
139       $search = addslashes($search);
140       $res = mysql_query("select page from $dbi[table] where page like '%$search%' order by page", $dbi["dbc"]);
141
142       return $res;
143    }
144
145
146    // iterating through database
147    function TitleSearchNextMatch($dbi, $res) {
148       if($o = mysql_fetch_object($res)) {
149          return $o->page;
150       }
151       else {
152          return 0;
153       }
154    }
155
156
157    // setup for full-text search
158    function InitFullSearch($dbi, $search) {
159       $search = addslashes($search);
160       $res = mysql_query("select page,hash from $dbi[table] where hash like '%$search%'", $dbi["dbc"]);
161
162       return $res;
163    }
164
165    // iterating through database
166    function FullSearchNextMatch($dbi, $res) {
167       if($o = mysql_fetch_object($res)) {
168          $page['name'] = $o->page;
169          $page['hash'] = unserialize($o->hash);
170          return $page;
171       }
172       else {
173          return 0;
174       }
175    }
176
177
178 ?>