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