]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_pgsql.php3
New function rcs_id(): we don't want any garbage emitted when generating zip files.
[SourceForge/phpwiki.git] / wiki_pgsql.php3
1 <!-- $Id: wiki_pgsql.php3,v 1.12 2000-06-30 00:56:27 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       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"]);
89
90       // record the time of modification
91       $pagehash["lastmodified"] = time();
92
93       if (IsWikiPage($dbi, $pagename)) {
94
95          $PAIRS = "author='$pagehash[author]'," .
96                   "content='$pagehash[content]'," .
97                   "created=$pagehash[created]," .
98                   "flags=$pagehash[flags]," .
99                   "lastmodified=$pagehash[lastmodified]," .
100                   "pagename='$pagehash[pagename]'," .
101                   "refs='$pagehash[refs]'," .
102                   "version=$pagehash[version]";
103
104          $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
105
106       } else {
107          // do an insert
108          // build up the column names and values for the query
109
110          $COLUMNS = "author, content, created, flags, " .
111                     "lastmodified, pagename, refs, version";
112
113          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
114                     "$pagehash[created], $pagehash[flags], " .
115                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
116                     "'$pagehash[refs]', $pagehash[version]";
117
118
119          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
120       }
121
122 //      echo "<p>Query: $query<p>\n";
123       $retval = pg_exec($dbi['dbc'], $query);
124       if ($retval == false) 
125          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
126
127    }
128
129
130
131    function IsWikiPage($dbi, $pagename) {
132       $pagename = addslashes($pagename);
133       $query = "select count(*) from $dbi[table] where pagename='$pagename'";
134       $res = pg_exec($query);
135       $array = pg_fetch_array($res, 0);
136       return $array[0];
137    }
138
139
140    // setup for title-search
141    function InitTitleSearch($dbi, $search) {
142
143       global $search_counter;
144       $search_counter = 0;
145
146       $search = strtolower($search);
147       $search = addslashes($search);
148       $query = "select pagename from $dbi[table] where lower(pagename) " .
149                "like '%$search%' order by pagename";
150 //      echo "search query: $query<br>\n";
151       $res = pg_exec($dbi["dbc"], $query);
152
153       return $res;
154    }
155
156
157    // iterating through database
158    function TitleSearchNextMatch($dbi, $res) {
159       global $search_counter;
160       if($o = @pg_fetch_object($res, $search_counter)) {
161          $search_counter++;
162          return $o->pagename;
163       } else {
164          return 0;
165       }
166    }
167
168
169    // setup for full-text search
170    function InitFullSearch($dbi, $search) {
171       global $search_counter;
172       $search_counter = 0;
173       $search = addslashes($search);
174       $search = addslashes($search);
175       $query = "select pagename,content from $dbi[table] " .
176                "where lower(content) like '%$search%'";
177
178       $res = pg_exec($dbi["dbc"], $query);
179
180       return $res;
181    }
182
183    // iterating through database
184    function FullSearchNextMatch($dbi, $res) {
185       global $search_counter;
186       if ($hash = @pg_fetch_array($res, $search_counter)) {
187          $search_counter++;
188          $page['pagename'] = $hash["pagename"];
189          $page['content'] = explode("\n", $hash["content"]);
190          return $page;
191       }
192       else {
193          return 0;
194       }
195    }
196
197
198    ////////////////////////
199    // new database features
200
201
202    function IncreaseHitCount($dbi, $pagename) {
203       return;
204       $query = "update hitcount set hits=hits+1 where pagename='$pagename'";
205       $res = mysql_query($query, $dbi['dbc']);
206
207       if (!mysql_affected_rows($dbi['dbc'])) {
208          $query = "insert into hitcount (pagename, hits) " .
209                   "values ('$pagename', 1)";
210          $res = mysql_query($query, $dbi['dbc']);
211       }
212
213       return $res;
214    }
215
216    function GetHitCount($dbi, $pagename) {
217       return;
218       $query = "select hits from hitcount where pagename='$pagename'";
219       $res = mysql_query($query, $dbi['dbc']);
220       if (mysql_num_rows($res)) {
221          $hits = mysql_result($res, 0);
222       } else {
223          $hits = "0";
224       }
225
226       return $hits;
227    }
228
229
230
231    function InitMostPopular($dbi, $limit) {
232       return;
233       $query = "select * from hitcount " .
234                "order by hits desc, pagename limit $limit";
235
236       $res = mysql_query($query);
237       
238       return $res;
239    }
240
241    function MostPopularNextMatch($dbi, $res) {
242       return;
243       if ($hits = mysql_fetch_array($res)) {
244          return $hits;
245       } else {
246          return 0;
247       }
248    }
249
250
251 ?>