1 <!-- $Id: wiki_pgsql.php3,v 1.19 2000-09-08 02:55:00 wainstead Exp $ -->
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)
26 // open a database and return a hash
28 function OpenDataBase($table) {
29 global $WikiDataBase, $pg_dbhost, $pg_dbport;
31 $connectstring = "host=$pg_dbhost port=$pg_dbport dbname=$WikiDataBase";
33 if (!($dbc = pg_pconnect($connectstring))) {
34 echo "Cannot establish connection to database, giving up.";
39 $dbi['table'] = $table;
40 // echo "<p>dbi after open: '$dbi' '$dbi[table]' '$dbi[dbc]'<p>\n";
45 function CloseDataBase($dbi) {
46 // NOOP: we use persistent database connections
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);
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") {
65 $pagehash[$key] = $val;
68 // unserialize/explode content
69 $pagehash['refs'] = unserialize($pagehash['refs']);
70 $pagehash['content'] = explode("\n", $pagehash['content']);
76 // if we reach this the query failed
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>";
86 // update the wikilinks table
87 // UpdateWikiLinks($dbi, $pagename, implode(" ",$pagehash['content']));
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"]);
100 // record the time of modification
101 $pagehash["lastmodified"] = time();
104 if (IsWikiPage($dbi, $pagename)) {
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]";
115 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
119 // build up the column names and values for the query
121 $COLUMNS = "author, content, created, flags, " .
122 "lastmodified, pagename, refs, version";
124 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
125 "$pagehash[created], $pagehash[flags], " .
126 "$pagehash[lastmodified], '$pagehash[pagename]', " .
127 "'$pagehash[refs]', $pagehash[version]";
130 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
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']);
141 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
142 global $ArchivePageStore;
143 // echo "<p>save copy called<p>";
145 $pagename = addslashes($pagename);
146 // echo "<p>dbi in SaveCopyToArchive: '$dbi' '$ArchivePageStore' '$dbi[dbc]'<p>";
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"]);
159 if (IsInArchive($dbi, $pagename)) {
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]";
170 $query = "UPDATE $ArchivePageStore SET $PAIRS WHERE pagename='$pagename'";
174 // build up the column names and values for the query
176 $COLUMNS = "author, content, created, flags, " .
177 "lastmodified, pagename, refs, version";
179 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
180 "$pagehash[created], $pagehash[flags], " .
181 "$pagehash[lastmodified], '$pagehash[pagename]', " .
182 "'$pagehash[refs]', $pagehash[version]";
185 $query = "INSERT INTO $ArchivePageStore ($COLUMNS) VALUES($VALUES)";
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']);
197 function UpdateWikiLinks($dbi, $pagename, $pagetext) {
199 global $AllowedProtocols;
200 // extract all links from the page, both [] and OldStyle
202 // this is [bracketlinks]
203 $numBracketLinks = preg_match_all("/\[.+?\]/s", $pagetext, $brktlinks);
205 // this is OldSchoolLinking
206 $numWikiLinks = preg_match_all("#!?\b(([A-Z][a-z]+){2,})\b#",
207 $pagetext, $wikilinks);
209 for ($x = 0; $x < $numWikiLinks; $x++) {
210 if (preg_match("/^!/", $wikilinks[0][$x]))
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";
217 for ($x = 0; $x < $numBracketLinks; $x++) {
218 // skip escaped bracket sets [[like this]
219 if (preg_match("/^\[\[/", $brktlinks[0][$x]))
221 // skip anything with an allowed protocol
222 if (preg_match("/$AllowedProtocols/", $brktlinks[0][$x]))
226 $alllinks[$brktlinks[0][$x]]++;
228 //echo "MATCH: ", $brktlinks[0][$x], "<P>\n";
229 //echo "assigned ", $alllinks[$brktlinks[0][$x]], " ", $brktlinks[0][$x], " <br>\n";
232 // call the right function to update the table
233 SetWikiPageLinks($dbi, $pagename, $alllinks);
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);
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);
256 // setup for title-search
257 function InitTitleSearch($dbi, $search) {
259 global $search_counter;
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);
273 // iterating through database
274 function TitleSearchNextMatch($dbi, $res) {
275 global $search_counter;
276 if($o = @pg_fetch_object($res, $search_counter)) {
285 // setup for full-text search
286 function InitFullSearch($dbi, $search) {
287 global $search_counter;
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%'";
295 $res = pg_exec($dbi["dbc"], $query);
300 // iterating through database
301 function FullSearchNextMatch($dbi, $res) {
302 global $search_counter;
303 if ($hash = @pg_fetch_array($res, $search_counter)) {
305 $page['pagename'] = $hash["pagename"];
306 $page['content'] = explode("\n", $hash["content"]);
315 ////////////////////////
316 // new database features
319 function IncreaseHitCount($dbi, $pagename) {
321 $query = "update hitcount set hits=hits+1 where pagename='$pagename'";
322 $res = pg_exec($dbi['dbc'], $query);
324 if (!pg_cmdtuples($res)) {
325 $query = "insert into hitcount (pagename, hits) " .
326 "values ('$pagename', 1)";
327 $res = pg_exec($dbi['dbc'], $query);
333 function GetHitCount($dbi, $pagename) {
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");
348 function InitMostPopular($dbi, $limit) {
350 global $pg_most_pop_ctr;
351 $pg_most_pop_ctr = 0;
353 $query = "select * from hitcount " .
354 "order by hits desc, pagename limit $limit";
355 $res = pg_exec($dbi['dbc'], $query);
359 function MostPopularNextMatch($dbi, $res) {
361 global $pg_most_pop_ctr;
362 if ($hits = @pg_fetch_array($res, $pg_most_pop_ctr)) {
370 function GetAllWikiPageNames($dbi) {
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");
380 ////////////////////////////////////////
381 // functionality for the wikilinks table
383 // takes a page name, returns array of links
385 function GetWikiPageLinks($dbi, $pagename) {
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");
398 // takes page name, list of links it contains
399 // the $linklist is an array where the keys are the page names
401 function SetWikiPageLinks($dbi, $pagename, $linklist) {
403 $frompage = addslashes($pagename);
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);
410 // the page may not have links, return if not
411 if (! count($linklist))
414 // now insert the new list of links
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);