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