1 <?php rcs_id('$Id: mysql.php,v 1.8 2000-11-24 22:07:34 wainstead Exp $');
7 MakeDBHash($pagename, $pagehash)
9 RetrievePage($dbi, $pagename, $pagestore)
10 InsertPage($dbi, $pagename, $pagehash)
11 SaveCopyToArchive($dbi, $pagename, $pagehash)
12 IsWikiPage($dbi, $pagename)
13 IsInArchive($dbi, $pagename)
14 RemovePage($dbi, $pagename)
15 IncreaseHitCount($dbi, $pagename)
16 GetHitCount($dbi, $pagename)
17 MakeSQLSearchClause($search, $column)
18 InitTitleSearch($dbi, $search)
19 TitleSearchNextMatch($dbi, $res)
20 InitFullSearch($dbi, $search)
21 FullSearchNextMatch($dbi, $res)
22 InitMostPopular($dbi, $limit)
23 MostPopularNextMatch($dbi, $res)
24 GetAllWikiPageNames($dbi)
25 GetWikiPageLinks($dbi, $pagename)
26 SetWikiPageLinks($dbi, $pagename, $linklist)
29 // open a database and return the handle
30 // ignores MAX_DBM_ATTEMPTS
32 function OpenDataBase($dbname) {
33 global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
35 if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
36 $msg = gettext ("Cannot establish connection to database, giving up.");
38 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
41 if (!mysql_select_db($mysql_db, $dbc)) {
42 $msg = sprintf(gettext ("Cannot open database %s, giving up."), $mysql_db);
44 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
48 $dbi['table'] = $dbname;
53 function CloseDataBase($dbi) {
55 // mysql connections are established as persistant
56 // they cannot be closed through mysql_close()
60 function MakeDBHash($pagename, $pagehash)
62 $pagehash["pagename"] = addslashes($pagename);
63 if (!isset($pagehash["flags"]))
64 $pagehash["flags"] = 0;
65 $pagehash["author"] = addslashes($pagehash["author"]);
66 $pagehash["content"] = implode("\n", $pagehash["content"]);
67 $pagehash["content"] = addslashes($pagehash["content"]);
68 $pagehash["refs"] = serialize($pagehash["refs"]);
73 function MakePageHash($dbhash)
75 // unserialize/explode content
76 $dbhash['refs'] = unserialize($dbhash['refs']);
77 $dbhash['content'] = explode("\n", $dbhash['content']);
82 // Return hash of page + attributes or default
83 function RetrievePage($dbi, $pagename, $pagestore) {
84 $pagename = addslashes($pagename);
85 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
86 if ($dbhash = mysql_fetch_array($res)) {
87 return MakePageHash($dbhash);
94 // Either insert or replace a key/value (a page)
95 function InsertPage($dbi, $pagename, $pagehash)
97 global $WikiPageStore; // ugly hack
99 if ($dbi['table'] == $WikiPageStore) { // HACK
100 $linklist = ExtractWikiPageLinks($pagehash['content']);
101 SetWikiPageLinks($dbi, $pagename, $linklist);
104 $pagehash = MakeDBHash($pagename, $pagehash);
106 $COLUMNS = "author, content, created, flags, " .
107 "lastmodified, pagename, refs, version";
109 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
110 "$pagehash[created], $pagehash[flags], " .
111 "$pagehash[lastmodified], '$pagehash[pagename]', " .
112 "'$pagehash[refs]', $pagehash[version]";
114 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
116 $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
118 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
124 // for archiving pages to a seperate dbm
125 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
126 global $ArchivePageStore;
127 $adbi = OpenDataBase($ArchivePageStore);
128 InsertPage($adbi, $pagename, $pagehash);
132 function IsWikiPage($dbi, $pagename) {
133 $pagename = addslashes($pagename);
134 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
135 return(mysql_result($res, 0));
140 function IsInArchive($dbi, $pagename) {
141 global $ArchivePageStore;
143 $pagename = addslashes($pagename);
144 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
145 return(mysql_result($res, 0));
151 function RemovePage($dbi, $pagename) {
152 global $WikiPageStore, $ArchivePageStore;
153 global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
155 $pagename = addslashes($pagename);
156 $msg = gettext ("Cannot delete '%s' from table '%s'");
158 $msg .= gettext ("MySQL error: %s");
160 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
161 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
163 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
164 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
166 if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
167 ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
169 if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
170 ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
172 if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
173 ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
177 function IncreaseHitCount($dbi, $pagename)
179 global $HitCountStore;
181 $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
183 if (!mysql_affected_rows($dbi['dbc'])) {
184 $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
190 function GetHitCount($dbi, $pagename)
192 global $HitCountStore;
194 $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
195 if (mysql_num_rows($res))
196 $hits = mysql_result($res, 0);
203 function MakeSQLSearchClause($search, $column)
205 $search = addslashes(preg_replace("/\s+/", " ", $search));
206 $term = strtok($search, ' ');
209 if ($word[0] == '-') {
210 $word = substr($word, 1);
211 $clause .= "not ($column like '%$word%') ";
213 $clause .= "($column like '%$word%') ";
215 if ($term = strtok(' '))
221 // setup for title-search
222 function InitTitleSearch($dbi, $search) {
223 $clause = MakeSQLSearchClause($search, 'pagename');
224 $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
230 // iterating through database
231 function TitleSearchNextMatch($dbi, $res) {
232 if($o = mysql_fetch_object($res)) {
241 // setup for full-text search
242 function InitFullSearch($dbi, $search) {
243 $clause = MakeSQLSearchClause($search, 'content');
244 $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
249 // iterating through database
250 function FullSearchNextMatch($dbi, $res) {
251 if($hash = mysql_fetch_array($res)) {
252 return MakePageHash($hash);
259 function InitMostPopular($dbi, $limit) {
260 global $HitCountStore;
261 $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
266 function MostPopularNextMatch($dbi, $res) {
267 if ($hits = mysql_fetch_array($res))
273 function GetAllWikiPageNames($dbi) {
274 global $WikiPageStore;
275 $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
276 $rows = mysql_num_rows($res);
277 for ($i = 0; $i < $rows; $i++) {
278 $pages[$i] = mysql_result($res, $i);
284 ////////////////////////////////////////
285 // functionality for the wikilinks table
287 // takes a page name, returns array of scored incoming and outgoing links
288 function GetWikiPageLinks($dbi, $pagename) {
289 global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
291 $pagename = addslashes($pagename);
292 $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
293 $rows = mysql_num_rows($res);
294 for ($i = 0; $i < $rows; $i++) {
295 $out = mysql_fetch_array($res);
296 $links['out'][] = array($out['topage'], $out['score']);
299 $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
300 $rows = mysql_num_rows($res);
301 for ($i = 0; $i < $rows; $i++) {
302 $out = mysql_fetch_array($res);
303 $links['in'][] = array($out['frompage'], $out['score']);
306 $res = mysql_query("select distinct pagename, hits from $WikiLinksStore, $HitCountStore where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
307 $rows = mysql_num_rows($res);
308 for ($i = 0; $i < $rows; $i++) {
309 $out = mysql_fetch_array($res);
310 $links['popular'][] = array($out['pagename'], $out['hits']);
317 // takes page name, list of links it contains
318 // the $linklist is an array where the keys are the page names
319 function SetWikiPageLinks($dbi, $pagename, $linklist) {
320 global $WikiLinksStore, $WikiScoreStore;
322 $frompage = addslashes($pagename);
324 // first delete the old list of links
325 mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
328 // the page may not have links, return if not
329 if (! count($linklist))
331 // now insert the new list of links
332 while (list($topage, $count) = each($linklist)) {
333 $topage = addslashes($topage);
334 if($topage != $frompage) {
335 mysql_query("insert into $WikiLinksStore (frompage, topage) " .
336 "values ('$frompage', '$topage')", $dbi["dbc"]);
341 mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
342 mysql_query("insert into $WikiScoreStore select w1.topage, count(*) from $WikiLinksStore as w1, $WikiLinksStore as w2 where w2.topage=w1.frompage group by w1.topage", $dbi["dbc"]);
345 /* more mysql queries:
348 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;