]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pgsql.php
PhpWiki 1.2.7 backport cvs release-1_2-branch enhancements never released with 1...
[SourceForge/phpwiki.git] / lib / pgsql.php
1 <?php rcs_id('$Id: pgsql.php,v 1.4.2.9 2005-01-07 14:23:05 rurban Exp $');
2
3    /*
4       Database functions:
5
6       OpenDataBase($table)
7       CloseDataBase($dbi)
8       RetrievePage($dbi, $pagename, $pagestore)
9       InsertPage($dbi, $pagename, $pagehash)
10       SaveCopyToArchive($dbi, $pagename, $pagehash) 
11       IsWikiPage($dbi, $pagename)
12       IsInArchive($dbi, $pagename)
13       InitTitleSearch($dbi, $search)
14       TitleSearchNextMatch($dbi, $res)
15       InitFullSearch($dbi, $search)
16       FullSearchNextMatch($dbi, $res)
17       InitBackLinkSearch($dbi, $pagename) 
18       BackLinkSearchNextMatch($dbi, &$pos) 
19       IncreaseHitCount($dbi, $pagename)
20       GetHitCount($dbi, $pagename)
21       InitMostPopular($dbi, $limit)
22       MostPopularNextMatch($dbi, $res)
23       GetAllWikiPageNames($dbi)
24       GetWikiPageLinks($dbi, $pagename)
25       SetWikiPageLinks($dbi, $pagename, $linklist)
26    */
27
28
29    // open a database and return a hash
30
31    function OpenDataBase($table) {
32        global $WikiDataBase, $pg_dbhost, $pg_dbport, $pg_dbuser, $pg_dbpass;
33
34       $connectstring = $pg_dbhost?"host=$pg_dbhost ":"";
35       $connectstring .= $pg_dbport?"port=$pg_dbport ":"";
36       $connectstring .= $pg_dbuser?"user=$pg_dbuser ":"";
37       $connectstring .= $pg_dbpass?"password=$pg_dbpass ":"";
38       $connectstring .= $WikiDataBase?"dbname=$WikiDataBase":"";
39
40       if (!($dbc = pg_pconnect($connectstring))) {
41          echo "Cannot establish connection to database, giving up.";
42          exit();
43       }
44
45       $dbi['dbc'] = $dbc;
46       $dbi['table'] = $table;
47       // echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
48       return $dbi;
49    }
50
51
52    function CloseDataBase($dbi) {
53       // NOOP: we use persistent database connections
54    }
55
56
57    // Return hash of page + attributes or default
58    function RetrievePage($dbi, $pagename, $pagestore) {
59       $pagename = addslashes($pagename);
60       $query = "select * from $pagestore where pagename='$pagename'";
61       // echo "<p>$query<p>";
62       $res = pg_exec($dbi['dbc'], $query);
63
64       if (pg_numrows($res)) {
65          if ($array = pg_fetch_array($res, 0)) {
66             while (list($key, $val) = each($array)) {
67                // pg_fetch_array gives us all the values twice,
68                // so we have to manually edit out the indices
69                if (gettype($key) == "integer") {
70                   continue;
71                }
72                $pagehash[$key] = $val;
73             }
74
75             // unserialize/explode content
76             $pagehash['refs'] = unserialize($pagehash['refs']);
77             $pagehash['content'] = explode("\n", $pagehash['content']);
78
79             return $pagehash;
80          }
81       }
82
83       // if we reach this the query failed
84       return -1;
85    }
86
87
88    // Either insert or replace a key/value (a page)
89    function InsertPage($dbi, $pagename, $pagehash) {
90       // update the wikilinks table
91       $linklist = ExtractWikiPageLinks($pagehash['content']);
92       SetWikiPageLinks($dbi, $pagename, $linklist);
93
94
95       // prepare the content for storage
96       if (!isset($pagehash["pagename"]))
97          $pagehash["pagename"] = $pagename;
98       if (!isset($pagehash["flags"]))
99          $pagehash["flags"] = 0;
100       $pagehash["author"] = addslashes($pagehash["author"]);
101       $pagehash["content"] = implode("\n", $pagehash["content"]);
102       $pagehash["content"] = addslashes($pagehash["content"]);
103       $pagehash["pagename"] = addslashes($pagehash["pagename"]);
104       $pagehash["refs"] = serialize($pagehash["refs"]);
105
106          // Check for empty variables which can cause a sql error
107          if(empty($pagehash["created"]))
108                 $pagehash["created"] = time();
109          if(empty($pagehash["version"]))
110                 $pagehash["version"] = 1;
111
112       // record the time of modification
113       $pagehash["lastmodified"] = time();
114
115
116       if (IsWikiPage($dbi, $pagename)) {
117
118          $PAIRS = "author='$pagehash[author]'," .
119                   "content='$pagehash[content]'," .
120                   "created=$pagehash[created]," .
121                   "flags=$pagehash[flags]," .
122                   "lastmodified=$pagehash[lastmodified]," .
123                   "pagename='$pagehash[pagename]'," .
124                   "refs='$pagehash[refs]'," .
125                   "version=$pagehash[version]";
126
127          $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
128
129       } else {
130          // do an insert
131          // build up the column names and values for the query
132
133          $COLUMNS = "author, content, created, flags, " .
134                     "lastmodified, pagename, refs, version";
135
136          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
137                     "$pagehash[created], $pagehash[flags], " .
138                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
139                     "'$pagehash[refs]', $pagehash[version]";
140
141
142          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
143       }
144
145       // echo "<p>Query: $query<p>\n";
146       $retval = pg_exec($dbi['dbc'], $query);
147       if ($retval == false) 
148          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
149
150    }
151
152
153    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
154       global $ArchivePageStore;
155       // echo "<p>save copy called<p>";
156
157       // echo "<p>dbi in SaveCopyToArchive: '$dbi' '$ArchivePageStore' '$dbi[dbc]'<p>";
158
159       // prepare the content for storage
160       if (!isset($pagehash["pagename"]))
161          $pagehash["pagename"] = $pagename;
162       if (!isset($pagehash["flags"]))
163          $pagehash["flags"] = 0;
164       $pagehash["author"] = addslashes($pagehash["author"]);
165       $pagehash["content"] = implode("\n", $pagehash["content"]);
166       $pagehash["content"] = addslashes($pagehash["content"]);
167       $pagehash["pagename"] = addslashes($pagehash["pagename"]);
168       $pagehash["refs"] = serialize($pagehash["refs"]);
169
170       if (IsInArchive($dbi, $pagename)) {
171
172          $PAIRS = "author='$pagehash[author]'," .
173                   "content='$pagehash[content]'," .
174                   "created=$pagehash[created]," .
175                   "flags=$pagehash[flags]," .
176                   "lastmodified=$pagehash[lastmodified]," .
177                   "pagename='$pagehash[pagename]'," .
178                   "refs='$pagehash[refs]'," .
179                   "version=$pagehash[version]";
180
181          $query = "UPDATE $ArchivePageStore SET $PAIRS WHERE pagename='$pagehash[pagename]'";
182
183       } else {
184          // do an insert
185          // build up the column names and values for the query
186
187          $COLUMNS = "author, content, created, flags, " .
188                     "lastmodified, pagename, refs, version";
189
190          $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
191                     "$pagehash[created], $pagehash[flags], " .
192                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
193                     "'$pagehash[refs]', $pagehash[version]";
194
195
196          $query = "INSERT INTO $ArchivePageStore ($COLUMNS) VALUES($VALUES)";
197       }
198
199       // echo "<p>Query: $query<p>\n";
200       $retval = pg_exec($dbi['dbc'], $query);
201       if ($retval == false) 
202          echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
203
204
205    }
206
207
208    function IsWikiPage($dbi, $pagename) {
209       global $WikiPageStore;    
210       $pagename = addslashes($pagename);
211       $query = "select count(*) from $WikiPageStore " .
212                "where pagename='$pagename'";
213       $res = pg_exec($query);
214       $array = pg_fetch_array($res, 0);
215       return $array[0];
216    }
217
218
219    function IsInArchive($dbi, $pagename) {
220          global $ArchivePageStore;
221       $pagename = addslashes($pagename);
222       $query = "select count(*) from $ArchivePageStore " .
223                "where pagename='$pagename'";
224       $res = pg_exec($query);
225       $array = pg_fetch_array($res, 0);
226       return $array[0];
227    }
228
229
230    // setup for title-search
231    function InitTitleSearch($dbi, $search) {
232
233       global $search_counter;
234       $search_counter = 0;
235
236       $search = strtolower($search);
237       $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
238       $search = addslashes($search);
239       $query = "select pagename from $dbi[table] where lower(pagename) " .
240                "like '%$search%' order by pagename";
241       //echo "search query: $query<br>\n";
242       $res = pg_exec($dbi["dbc"], $query);
243
244       return $res;
245    }
246
247
248    // iterating through database
249    function TitleSearchNextMatch($dbi, $res) {
250       global $search_counter;
251       if($o = @pg_fetch_object($res, $search_counter)) {
252          $search_counter++;
253          return $o->pagename;
254       } else {
255          return 0;
256       }
257    }
258
259
260    // setup for full-text search
261    function InitFullSearch($dbi, $search) {
262       global $search_counter;
263       $search_counter = 0;
264       $search = strtolower($search);
265       $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
266       $search = addslashes($search);
267       $query = "select pagename,content from $dbi[table] " .
268                "where lower(content) like '%$search%'";
269
270       $res = pg_exec($dbi["dbc"], $query);
271
272       return $res;
273    }
274
275    // iterating through database
276    function FullSearchNextMatch($dbi, $res) {
277       global $search_counter;
278       if ($hash = @pg_fetch_array($res, $search_counter)) {
279          $search_counter++;
280          $page['pagename'] = $hash["pagename"];
281          $page['content'] = explode("\n", $hash["content"]);
282          return $page;
283       }
284       else {
285          return 0;
286       }
287    }
288
289
290    ////////////////////////
291    // new database features
292
293    // setup for back-link search
294    function InitBackLinkSearch($dbi, $pagename) {
295       global $WikiLinksPageStore;
296      
297       $topage = addslashes($pagename);
298       $query = "SELECT DISTINCT frompage FROM $WikiLinksPageStore"
299            . " WHERE topage='$topage'"
300            . " ORDER BY frompage";
301       $res['res'] = pg_exec( $dbi["dbc"], $query);
302       $res['row'] = 0;
303       return $res;
304    }
305
306
307 // iterating through database
308 function BackLinkSearchNextMatch($dbi, &$res) {
309     if($a = @pg_fetch_row($res['res'], $res['row'])) {
310         $res['row']++;
311         return $a[0];
312     }
313     else {
314         return 0;
315     }
316 }
317
318
319    function IncreaseHitCount($dbi, $pagename) {
320       global $HitCountPageStore;
321
322       $qpagename = addslashes($pagename);
323       $query = "update $HitCountPageStore set hits=hits+1 where pagename='$qpagename'";
324       $res = pg_exec($dbi['dbc'], $query);
325
326       if (!pg_cmdtuples($res)) {
327          $query = "insert into $HitCountPageStore (pagename, hits) " .
328                   "values ('$qpagename', 1)";
329          $res = pg_exec($dbi['dbc'], $query);
330       }
331
332       return $res;
333    }
334
335    function GetHitCount($dbi, $pagename) {
336       global $HitCountPageStore;
337       $qpagename = addslashes($pagename);
338       $query = "select hits from $HitCountPageStore where pagename='$qpagename'";
339       $res = pg_exec($dbi['dbc'], $query);
340       if (pg_cmdtuples($res)) {
341          $hits = pg_result($res, 0, "hits");
342       } else {
343          $hits = "0";
344       }
345
346       return $hits;
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 ?>