1 <?php rcs_id('$Id: pgsql.php,v 1.4.2.4 2001-11-07 18:58:14 dairiki Exp $');
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)
29 // open a database and return a hash
31 function OpenDataBase($table) {
32 global $WikiDataBase, $pg_dbhost, $pg_dbport;
34 $connectstring = $pg_dbhost?"host=$pg_dbhost ":"";
35 $connectstring .= $pg_dbport?"port=$pg_dbport ":"";
36 $connectstring .= $WikiDataBase?"dbname=$WikiDataBase":"";
38 if (!($dbc = pg_pconnect($connectstring))) {
39 echo "Cannot establish connection to database, giving up.";
44 $dbi['table'] = $table;
45 // echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
50 function CloseDataBase($dbi) {
51 // NOOP: we use persistent database connections
55 // Return hash of page + attributes or default
56 function RetrievePage($dbi, $pagename, $pagestore) {
57 $pagename = addslashes($pagename);
58 $query = "select * from $pagestore where pagename='$pagename'";
59 // echo "<p>$query<p>";
60 $res = pg_exec($dbi['dbc'], $query);
62 if (pg_numrows($res)) {
63 if ($array = pg_fetch_array($res, 0)) {
64 while (list($key, $val) = each($array)) {
65 // pg_fetch_array gives us all the values twice,
66 // so we have to manually edit out the indices
67 if (gettype($key) == "integer") {
70 $pagehash[$key] = $val;
73 // unserialize/explode content
74 $pagehash['refs'] = unserialize($pagehash['refs']);
75 $pagehash['content'] = explode("\n", $pagehash['content']);
81 // if we reach this the query failed
86 // Either insert or replace a key/value (a page)
87 function InsertPage($dbi, $pagename, $pagehash) {
88 $pagename = addslashes($pagename);
90 // update the wikilinks table
91 $linklist = ExtractWikiPageLinks($pagehash['content']);
92 SetWikiPageLinks($dbi, $pagename, $linklist);
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"]);
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;
112 // record the time of modification
113 $pagehash["lastmodified"] = time();
116 if (IsWikiPage($dbi, $pagename)) {
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]";
127 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
131 // build up the column names and values for the query
133 $COLUMNS = "author, content, created, flags, " .
134 "lastmodified, pagename, refs, version";
136 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
137 "$pagehash[created], $pagehash[flags], " .
138 "$pagehash[lastmodified], '$pagehash[pagename]', " .
139 "'$pagehash[refs]', $pagehash[version]";
142 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
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']);
153 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
154 global $ArchivePageStore;
155 // echo "<p>save copy called<p>";
157 $pagename = addslashes($pagename);
158 // echo "<p>dbi in SaveCopyToArchive: '$dbi' '$ArchivePageStore' '$dbi[dbc]'<p>";
160 // prepare the content for storage
161 if (!isset($pagehash["pagename"]))
162 $pagehash["pagename"] = $pagename;
163 if (!isset($pagehash["flags"]))
164 $pagehash["flags"] = 0;
165 $pagehash["author"] = addslashes($pagehash["author"]);
166 $pagehash["content"] = implode("\n", $pagehash["content"]);
167 $pagehash["content"] = addslashes($pagehash["content"]);
168 $pagehash["pagename"] = addslashes($pagehash["pagename"]);
169 $pagehash["refs"] = serialize($pagehash["refs"]);
171 if (IsInArchive($dbi, $pagename)) {
173 $PAIRS = "author='$pagehash[author]'," .
174 "content='$pagehash[content]'," .
175 "created=$pagehash[created]," .
176 "flags=$pagehash[flags]," .
177 "lastmodified=$pagehash[lastmodified]," .
178 "pagename='$pagehash[pagename]'," .
179 "refs='$pagehash[refs]'," .
180 "version=$pagehash[version]";
182 $query = "UPDATE $ArchivePageStore SET $PAIRS WHERE pagename='$pagename'";
186 // build up the column names and values for the query
188 $COLUMNS = "author, content, created, flags, " .
189 "lastmodified, pagename, refs, version";
191 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
192 "$pagehash[created], $pagehash[flags], " .
193 "$pagehash[lastmodified], '$pagehash[pagename]', " .
194 "'$pagehash[refs]', $pagehash[version]";
197 $query = "INSERT INTO $ArchivePageStore ($COLUMNS) VALUES($VALUES)";
200 // echo "<p>Query: $query<p>\n";
201 $retval = pg_exec($dbi['dbc'], $query);
202 if ($retval == false)
203 echo "Insert/update failed: " . pg_errormessage($dbi['dbc']);
209 function IsWikiPage($dbi, $pagename) {
210 global $WikiPageStore;
211 $pagename = addslashes($pagename);
212 $query = "select count(*) from $WikiPageStore " .
213 "where pagename='$pagename'";
214 $res = pg_exec($query);
215 $array = pg_fetch_array($res, 0);
220 function IsInArchive($dbi, $pagename) {
221 global $ArchivePageStore;
222 $pagename = addslashes($pagename);
223 $query = "select count(*) from $ArchivePageStore " .
224 "where pagename='$pagename'";
225 $res = pg_exec($query);
226 $array = pg_fetch_array($res, 0);
231 // setup for title-search
232 function InitTitleSearch($dbi, $search) {
234 global $search_counter;
237 $search = strtolower($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);
248 // iterating through database
249 function TitleSearchNextMatch($dbi, $res) {
250 global $search_counter;
251 if($o = @pg_fetch_object($res, $search_counter)) {
260 // setup for full-text search
261 function InitFullSearch($dbi, $search) {
262 global $search_counter;
264 $search = strtolower($search);
265 $search = addslashes($search);
266 $search = addslashes($search);
267 $query = "select pagename,content from $dbi[table] " .
268 "where lower(content) like '%$search%'";
270 $res = pg_exec($dbi["dbc"], $query);
275 // iterating through database
276 function FullSearchNextMatch($dbi, $res) {
277 global $search_counter;
278 if ($hash = @pg_fetch_array($res, $search_counter)) {
280 $page['pagename'] = $hash["pagename"];
281 $page['content'] = explode("\n", $hash["content"]);
290 ////////////////////////
291 // new database features
293 // setup for back-link search
294 function InitBackLinkSearch($dbi, $pagename) {
295 global $WikiLinksPageStore;
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);
307 // iterating through database
308 function BackLinkSearchNextMatch($dbi, &$res) {
309 if($a = @pg_fetch_row($res['res'], $res['row'])) {
319 function IncreaseHitCount($dbi, $pagename) {
320 global $HitCountPageStore;
322 $qpagename = addslashes($pagename);
323 $query = "update $HitCountPageStore set hits=hits+1 where pagename='$qpagename'";
324 $res = pg_exec($dbi['dbc'], $query);
326 if (!pg_cmdtuples($res)) {
327 $query = "insert into $HitCountPageStore (pagename, hits) " .
328 "values ('$qpagename', 1)";
329 $res = pg_exec($dbi['dbc'], $query);
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");
351 function InitMostPopular($dbi, $limit) {
353 global $pg_most_pop_ctr, $HitCountPageStore;
354 $pg_most_pop_ctr = 0;
356 $query = "select * from $HitCountPageStore " .
357 "order by hits desc, pagename limit $limit";
358 $res = pg_exec($dbi['dbc'], $query);
362 function MostPopularNextMatch($dbi, $res) {
364 global $pg_most_pop_ctr;
365 if ($hits = @pg_fetch_array($res, $pg_most_pop_ctr)) {
373 function GetAllWikiPageNames($dbi) {
374 global $WikiPageStore;
375 $res = pg_exec($dbi['dbc'], "select pagename from $WikiPageStore");
376 $rows = pg_numrows($res);
377 for ($i = 0; $i < $rows; $i++) {
378 $pages[$i] = pg_result($res, $i, "pagename");
383 ////////////////////////////////////////
384 // functionality for the wikilinks table
386 // takes a page name, returns array of links
387 function GetWikiPageLinks($dbi, $pagename) {
388 global $WikiLinksPageStore;
389 $pagename = addslashes($pagename);
391 $res = pg_exec("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
392 $rows = pg_numrows($res);
393 for ($i = 0; $i < $rows; $i++) {
394 $out = pg_fetch_array($res, $i);
395 $links['out'][] = array($out['topage'], $out['score']);
398 $res = pg_exec("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
399 $rows = pg_numrows($res);
400 for ($i = 0; $i < $rows; $i++) {
401 $out = pg_fetch_array($res, $i);
402 $links['in'][] = array($out['frompage'], $out['score']);
405 $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");
406 $rows = pg_numrows($res);
407 for ($i = 0; $i < $rows; $i++) {
408 $out = pg_fetch_array($res, $i);
409 $links['popular'][] = array($out['pagename'], $out['hits']);
417 // takes page name, list of links it contains
418 // the $linklist is an array where the keys are the page names
420 function SetWikiPageLinks($dbi, $pagename, $linklist) {
421 global $WikiLinksPageStore;
422 $frompage = addslashes($pagename);
424 // first delete the old list of links
425 $query = "delete from $WikiLinksPageStore where frompage='$frompage'";
426 //echo "$query<br>\n";
427 $res = pg_exec($dbi['dbc'], $query);
429 // the page may not have links, return if not
430 if (! count($linklist))
433 // now insert the new list of links
435 while (list($topage, $count) = each($linklist)) {
436 $topage = addslashes($topage);
437 if ($topage != $frompage) {
438 $query = "insert into $WikiLinksPageStore (frompage, topage) " .
439 "values ('$frompage', '$topage')";
440 //echo "$query<br>\n";
441 $res = pg_exec($dbi['dbc'], $query);
445 pg_exec("delete from wikiscore");
446 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");