]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pgsql.php
Fixed bug: BackLinkSearchNextMatch needed the address of $res. It was
[SourceForge/phpwiki.git] / lib / pgsql.php
1 <!-- $Id: pgsql.php,v 1.4.2.2 2001-09-21 21:57:07 wainstead Exp $ -->
2 <?php
3
4    /*
5       Database functions:
6
7       OpenDataBase($table)
8       CloseDataBase($dbi)
9       RetrievePage($dbi, $pagename, $pagestore)
10       InsertPage($dbi, $pagename, $pagehash)
11       SaveCopyToArchive($dbi, $pagename, $pagehash) 
12       IsWikiPage($dbi, $pagename)
13       IsInArchive($dbi, $pagename)
14       InitTitleSearch($dbi, $search)
15       TitleSearchNextMatch($dbi, $res)
16       InitFullSearch($dbi, $search)
17       FullSearchNextMatch($dbi, $res)
18       InitBackLinkSearch($dbi, $pagename) 
19       BackLinkSearchNextMatch($dbi, &$pos) 
20       IncreaseHitCount($dbi, $pagename)
21       GetHitCount($dbi, $pagename)
22       InitMostPopular($dbi, $limit)
23       MostPopularNextMatch($dbi, $res)
24       GetAllWikiPageNames($dbi)
25       GetWikiPageLinks($dbi, $pagename)
26       SetWikiPageLinks($dbi, $pagename, $linklist)
27    */
28
29
30    // open a database and return a hash
31
32    function OpenDataBase($table) {
33       global $WikiDataBase, $pg_dbhost, $pg_dbport;
34
35       $connectstring = $pg_dbhost?"host=$pg_dbhost ":"";
36          $connectstring .= $pg_dbport?"port=$pg_dbport ":"";
37          $connectstring .= $WikiDataBase?"dbname=$WikiDataBase":"";
38
39       if (!($dbc = pg_pconnect($connectstring))) {
40          echo "Cannot establish connection to database, giving up.";
41          exit();
42       }
43
44       $dbi['dbc'] = $dbc;
45       $dbi['table'] = $table;
46       // echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
47       return $dbi;
48    }
49
50
51    function CloseDataBase($dbi) {
52       // NOOP: we use persistent database connections
53    }
54
55
56    // Return hash of page + attributes or default
57    function RetrievePage($dbi, $pagename, $pagestore) {
58       $pagename = addslashes($pagename);
59       $query = "select * from $pagestore where pagename='$pagename'";
60       // echo "<p>$query<p>";
61       $res = pg_exec($dbi['dbc'], $query);
62
63       if (pg_numrows($res)) {
64          if ($array = pg_fetch_array($res, 0)) {
65             while (list($key, $val) = each($array)) {
66                // pg_fetch_array gives us all the values twice,
67                // so we have to manually edit out the indices
68                if (gettype($key) == "integer") {
69                   continue;
70                }
71                $pagehash[$key] = $val;
72             }
73
74             // unserialize/explode content
75             $pagehash['refs'] = unserialize($pagehash['refs']);
76             $pagehash['content'] = explode("\n", $pagehash['content']);
77
78             return $pagehash;
79          }
80       }
81
82       // if we reach this the query failed
83       return -1;
84    }
85
86
87    // Either insert or replace a key/value (a page)
88    function InsertPage($dbi, $pagename, $pagehash) {
89       $pagename = addslashes($pagename);
90
91       // update the wikilinks table
92       $linklist = ExtractWikiPageLinks($pagehash['content']);
93       SetWikiPageLinks($dbi, $pagename, $linklist);
94
95
96       // prepare the content for storage
97       if (!isset($pagehash["pagename"]))
98          $pagehash["pagename"] = $pagename;
99       if (!isset($pagehash["flags"]))
100          $pagehash["flags"] = 0;
101       $pagehash["author"] = addslashes($pagehash["author"]);
102       $pagehash["content"] = implode("\n", $pagehash["content"]);
103       $pagehash["content"] = addslashes($pagehash["content"]);
104       $pagehash["pagename"] = addslashes($pagehash["pagename"]);
105       $pagehash["refs"] = serialize($pagehash["refs"]);
106
107          // Check for empty variables which can cause a sql error
108          if(empty($pagehash["created"]))
109                 $pagehash["created"] = time();
110          if(empty($pagehash["version"]))
111                 $pagehash["version"] = 1;
112
113       // record the time of modification
114       $pagehash["lastmodified"] = time();
115
116
117       if (IsWikiPage($dbi, $pagename)) {
118
119          $PAIRS = "author='$pagehash[author]'," .
120                   "content='$pagehash[content]'," .
121                   "created=$pagehash[created]," .
122                   "flags=$pagehash[flags]," .
123                   "lastmodified=$pagehash[lastmodified]," .
124                   "pagename='$pagehash[pagename]'," .
125                   "refs='$pagehash[refs]'," .
126                   "version=$pagehash[version]";
127
128          $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
129
130       } else {
131          // do an insert
132          // build up the column names and values for the query
133
134          $COLUMNS = "author, content, created, flags, " .
135                     "lastmodified, pagename, refs, version";
136
137          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
138                     "$pagehash[created], $pagehash[flags], " .
139                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
140                     "'$pagehash[refs]', $pagehash[version]";
141
142
143          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
144       }
145
146       // echo "<p>Query: $query<p>\n";
147       $retval = pg_exec($dbi['dbc'], $query);
148       if ($retval == false) 
149          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
150
151    }
152
153
154    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
155       global $ArchivePageStore;
156       // echo "<p>save copy called<p>";
157
158       $pagename = addslashes($pagename);
159       // echo "<p>dbi in SaveCopyToArchive: '$dbi' '$ArchivePageStore' '$dbi[dbc]'<p>";
160
161       // prepare the content for storage
162       if (!isset($pagehash["pagename"]))
163          $pagehash["pagename"] = $pagename;
164       if (!isset($pagehash["flags"]))
165          $pagehash["flags"] = 0;
166       $pagehash["author"] = addslashes($pagehash["author"]);
167       $pagehash["content"] = implode("\n", $pagehash["content"]);
168       $pagehash["content"] = addslashes($pagehash["content"]);
169       $pagehash["pagename"] = addslashes($pagehash["pagename"]);
170       $pagehash["refs"] = serialize($pagehash["refs"]);
171
172       if (IsInArchive($dbi, $pagename)) {
173
174          $PAIRS = "author='$pagehash[author]'," .
175                   "content='$pagehash[content]'," .
176                   "created=$pagehash[created]," .
177                   "flags=$pagehash[flags]," .
178                   "lastmodified=$pagehash[lastmodified]," .
179                   "pagename='$pagehash[pagename]'," .
180                   "refs='$pagehash[refs]'," .
181                   "version=$pagehash[version]";
182
183          $query = "UPDATE $ArchivePageStore SET $PAIRS WHERE pagename='$pagename'";
184
185       } else {
186          // do an insert
187          // build up the column names and values for the query
188
189          $COLUMNS = "author, content, created, flags, " .
190                     "lastmodified, pagename, refs, version";
191
192          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
193                     "$pagehash[created], $pagehash[flags], " .
194                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
195                     "'$pagehash[refs]', $pagehash[version]";
196
197
198          $query = "INSERT INTO $ArchivePageStore ($COLUMNS) VALUES($VALUES)";
199       }
200
201       // echo "<p>Query: $query<p>\n";
202       $retval = pg_exec($dbi['dbc'], $query);
203       if ($retval == false) 
204          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
205
206
207    }
208
209
210    function IsWikiPage($dbi, $pagename) {
211       global $WikiPageStore;    
212       $pagename = addslashes($pagename);
213       $query = "select count(*) from $WikiPageStore " .
214                "where pagename='$pagename'";
215       $res = pg_exec($query);
216       $array = pg_fetch_array($res, 0);
217       return $array[0];
218    }
219
220
221    function IsInArchive($dbi, $pagename) {
222          global $ArchivePageStore;
223       $pagename = addslashes($pagename);
224       $query = "select count(*) from $ArchivePageStore " .
225                "where pagename='$pagename'";
226       $res = pg_exec($query);
227       $array = pg_fetch_array($res, 0);
228       return $array[0];
229    }
230
231
232    // setup for title-search
233    function InitTitleSearch($dbi, $search) {
234
235       global $search_counter;
236       $search_counter = 0;
237
238       $search = strtolower($search);
239       $search = addslashes($search);
240       $query = "select pagename from $dbi[table] where lower(pagename) " .
241                "like '%$search%' order by pagename";
242       //echo "search query: $query<br>\n";
243       $res = pg_exec($dbi["dbc"], $query);
244
245       return $res;
246    }
247
248
249    // iterating through database
250    function TitleSearchNextMatch($dbi, $res) {
251       global $search_counter;
252       if($o = @pg_fetch_object($res, $search_counter)) {
253          $search_counter++;
254          return $o->pagename;
255       } else {
256          return 0;
257       }
258    }
259
260
261    // setup for full-text search
262    function InitFullSearch($dbi, $search) {
263       global $search_counter;
264       $search_counter = 0;
265       $search = strtolower($search);
266       $search = addslashes($search);
267       $search = addslashes($search);
268       $query = "select pagename,content from $dbi[table] " .
269                "where lower(content) like '%$search%'";
270
271       $res = pg_exec($dbi["dbc"], $query);
272
273       return $res;
274    }
275
276    // iterating through database
277    function FullSearchNextMatch($dbi, $res) {
278       global $search_counter;
279       if ($hash = @pg_fetch_array($res, $search_counter)) {
280          $search_counter++;
281          $page['pagename'] = $hash["pagename"];
282          $page['content'] = explode("\n", $hash["content"]);
283          return $page;
284       }
285       else {
286          return 0;
287       }
288    }
289
290
291    ////////////////////////
292    // new database features
293
294    // setup for back-link search
295    function InitBackLinkSearch($dbi, $pagename) {
296       global $WikiLinksPageStore;
297      
298       $topage = addslashes($pagename);
299       $query = "SELECT DISTINCT frompage FROM $WikiLinksPageStore"
300            . " WHERE topage='$topage'"
301            . " ORDER BY frompage";
302       $res['res'] = pg_exec( $dbi["dbc"], $query);
303       $res['row'] = 0;
304       return $res;
305    }
306
307
308 // iterating through database
309 function BackLinkSearchNextMatch($dbi, &$res) {
310     if($a = @pg_fetch_row($res['res'], $res['row'])) {
311         $res['row']++;
312         return $a[0];
313     }
314     else {
315         return 0;
316     }
317 }
318
319
320    function IncreaseHitCount($dbi, $pagename) {
321       global $HitCountPageStore;
322       $query = "update $HitCountPageStore set hits=hits+1 where pagename='$pagename'";
323       $res = pg_exec($dbi['dbc'], $query);
324
325       if (!pg_cmdtuples($res)) {
326          $query = "insert into $HitCountPageStore (pagename, hits) " .
327                   "values ('$pagename', 1)";
328          $res = pg_exec($dbi['dbc'], $query);
329       }
330
331       return $res;
332    }
333
334    function GetHitCount($dbi, $pagename) {
335       global $HitCountPageStore;
336       $query = "select hits from $HitCountPageStore where pagename='$pagename'";
337       $res = pg_exec($dbi['dbc'], $query);
338       if (pg_cmdtuples($res)) {
339          $hits = pg_result($res, 0, "hits");
340       } else {
341          $hits = "0";
342       }
343
344       return $hits;
345    }
346
347
348
349    function InitMostPopular($dbi, $limit) {
350
351       global $pg_most_pop_ctr, $HitCountPageStore;
352       $pg_most_pop_ctr = 0;
353
354       $query = "select * from $HitCountPageStore " .
355                "order by hits desc, pagename limit $limit";
356       $res = pg_exec($dbi['dbc'], $query);
357       return $res;
358    }
359
360    function MostPopularNextMatch($dbi, $res) {
361
362       global $pg_most_pop_ctr;
363       if ($hits = @pg_fetch_array($res, $pg_most_pop_ctr)) {
364          $pg_most_pop_ctr++;
365          return $hits;
366       } else {
367          return 0;
368       }
369    }
370
371    function GetAllWikiPageNames($dbi) {
372       global $WikiPageStore;
373       $res = pg_exec($dbi['dbc'], "select pagename from $WikiPageStore");
374       $rows = pg_numrows($res);
375       for ($i = 0; $i < $rows; $i++) {
376          $pages[$i] = pg_result($res, $i, "pagename");
377       }
378       return $pages;
379    }
380
381    ////////////////////////////////////////
382    // functionality for the wikilinks table
383
384    // takes a page name, returns array of links
385    function GetWikiPageLinks($dbi, $pagename) {
386       global $WikiLinksPageStore;
387       $pagename = addslashes($pagename);
388
389       $res = pg_exec("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
390       $rows = pg_numrows($res);
391       for ($i = 0; $i < $rows; $i++) {
392          $out = pg_fetch_array($res, $i);
393          $links['out'][] = array($out['topage'], $out['score']);
394       }
395
396       $res = pg_exec("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
397       $rows = pg_numrows($res);
398       for ($i = 0; $i < $rows; $i++) {
399          $out = pg_fetch_array($res, $i);
400          $links['in'][] = array($out['frompage'], $out['score']);
401       }
402
403       $res = pg_exec("select distinct pagename, hits from wikilinks, hitcount where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
404       $rows = pg_numrows($res);
405       for ($i = 0; $i < $rows; $i++) {
406          $out = pg_fetch_array($res, $i);
407          $links['popular'][] = array($out['pagename'], $out['hits']);
408       }
409
410       return $links;
411
412    }
413
414
415    // takes page name, list of links it contains
416    // the $linklist is an array where the keys are the page names
417
418    function SetWikiPageLinks($dbi, $pagename, $linklist) {
419       global $WikiLinksPageStore;
420       $frompage = addslashes($pagename);
421
422       // first delete the old list of links
423       $query = "delete from $WikiLinksPageStore where frompage='$frompage'";
424       //echo "$query<br>\n";
425       $res = pg_exec($dbi['dbc'], $query);
426
427       // the page may not have links, return if not
428       if (! count($linklist))
429          return;
430
431       // now insert the new list of links
432       reset($linklist);
433       while (list($topage, $count) = each($linklist)) {
434          $topage = addslashes($topage);
435          if ($topage != $frompage) {
436             $query = "insert into $WikiLinksPageStore (frompage, topage) " .
437                      "values ('$frompage', '$topage')";
438             //echo "$query<br>\n";
439             $res = pg_exec($dbi['dbc'], $query);
440          }
441       }
442       // update pagescore
443       pg_exec("delete from wikiscore");
444       pg_exec("insert into wikiscore select w1.topage, count(*) from wikilinks as w1, wikilinks as w2 where w2.topage=w1.frompage group by w1.topage");
445
446    }
447
448
449 ?>