1 <?php rcs_id('$Id: mysql.php,v 1.9 2001-01-01 23:34:57 ahollosi 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 if (!isset($pagehash["refs"]))
69 $pagehash["refs"] = array();
70 $pagehash["refs"] = serialize($pagehash["refs"]);
75 function MakePageHash($dbhash)
77 // unserialize/explode content
78 $dbhash['refs'] = unserialize($dbhash['refs']);
79 $dbhash['content'] = explode("\n", $dbhash['content']);
84 // Return hash of page + attributes or default
85 function RetrievePage($dbi, $pagename, $pagestore) {
86 $pagename = addslashes($pagename);
87 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
88 if ($dbhash = mysql_fetch_array($res)) {
89 return MakePageHash($dbhash);
96 // Either insert or replace a key/value (a page)
97 function InsertPage($dbi, $pagename, $pagehash)
99 global $WikiPageStore; // ugly hack
101 if ($dbi['table'] == $WikiPageStore) { // HACK
102 $linklist = ExtractWikiPageLinks($pagehash['content']);
103 SetWikiPageLinks($dbi, $pagename, $linklist);
106 $pagehash = MakeDBHash($pagename, $pagehash);
108 $COLUMNS = "author, content, created, flags, " .
109 "lastmodified, pagename, refs, version";
111 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
112 "$pagehash[created], $pagehash[flags], " .
113 "$pagehash[lastmodified], '$pagehash[pagename]', " .
114 "'$pagehash[refs]', $pagehash[version]";
116 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
118 $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
120 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
126 // for archiving pages to a seperate dbm
127 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
128 global $ArchivePageStore;
129 $adbi = OpenDataBase($ArchivePageStore);
130 InsertPage($adbi, $pagename, $pagehash);
134 function IsWikiPage($dbi, $pagename) {
135 $pagename = addslashes($pagename);
136 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
137 return(mysql_result($res, 0));
142 function IsInArchive($dbi, $pagename) {
143 global $ArchivePageStore;
145 $pagename = addslashes($pagename);
146 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
147 return(mysql_result($res, 0));
153 function RemovePage($dbi, $pagename) {
154 global $WikiPageStore, $ArchivePageStore;
155 global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
157 $pagename = addslashes($pagename);
158 $msg = gettext ("Cannot delete '%s' from table '%s'");
160 $msg .= gettext ("MySQL error: %s");
162 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
163 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
165 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
166 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
168 if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
169 ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
171 if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
172 ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
174 if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
175 ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
179 function IncreaseHitCount($dbi, $pagename)
181 global $HitCountStore;
183 $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
185 if (!mysql_affected_rows($dbi['dbc'])) {
186 $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
192 function GetHitCount($dbi, $pagename)
194 global $HitCountStore;
196 $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
197 if (mysql_num_rows($res))
198 $hits = mysql_result($res, 0);
205 function MakeSQLSearchClause($search, $column)
207 $search = addslashes(preg_replace("/\s+/", " ", $search));
208 $term = strtok($search, ' ');
211 if ($word[0] == '-') {
212 $word = substr($word, 1);
213 $clause .= "not ($column like '%$word%') ";
215 $clause .= "($column like '%$word%') ";
217 if ($term = strtok(' '))
223 // setup for title-search
224 function InitTitleSearch($dbi, $search) {
225 $clause = MakeSQLSearchClause($search, 'pagename');
226 $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
232 // iterating through database
233 function TitleSearchNextMatch($dbi, $res) {
234 if($o = mysql_fetch_object($res)) {
243 // setup for full-text search
244 function InitFullSearch($dbi, $search) {
245 $clause = MakeSQLSearchClause($search, 'content');
246 $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
251 // iterating through database
252 function FullSearchNextMatch($dbi, $res) {
253 if($hash = mysql_fetch_array($res)) {
254 return MakePageHash($hash);
261 function InitMostPopular($dbi, $limit) {
262 global $HitCountStore;
263 $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
268 function MostPopularNextMatch($dbi, $res) {
269 if ($hits = mysql_fetch_array($res))
275 function GetAllWikiPageNames($dbi) {
276 global $WikiPageStore;
277 $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
278 $rows = mysql_num_rows($res);
279 for ($i = 0; $i < $rows; $i++) {
280 $pages[$i] = mysql_result($res, $i);
286 ////////////////////////////////////////
287 // functionality for the wikilinks table
289 // takes a page name, returns array of scored incoming and outgoing links
290 function GetWikiPageLinks($dbi, $pagename) {
291 global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
293 $pagename = addslashes($pagename);
294 $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
295 $rows = mysql_num_rows($res);
296 for ($i = 0; $i < $rows; $i++) {
297 $out = mysql_fetch_array($res);
298 $links['out'][] = array($out['topage'], $out['score']);
301 $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
302 $rows = mysql_num_rows($res);
303 for ($i = 0; $i < $rows; $i++) {
304 $out = mysql_fetch_array($res);
305 $links['in'][] = array($out['frompage'], $out['score']);
308 $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");
309 $rows = mysql_num_rows($res);
310 for ($i = 0; $i < $rows; $i++) {
311 $out = mysql_fetch_array($res);
312 $links['popular'][] = array($out['pagename'], $out['hits']);
319 // takes page name, list of links it contains
320 // the $linklist is an array where the keys are the page names
321 function SetWikiPageLinks($dbi, $pagename, $linklist) {
322 global $WikiLinksStore, $WikiScoreStore;
324 $frompage = addslashes($pagename);
326 // first delete the old list of links
327 mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
330 // the page may not have links, return if not
331 if (! count($linklist))
333 // now insert the new list of links
334 while (list($topage, $count) = each($linklist)) {
335 $topage = addslashes($topage);
336 if($topage != $frompage) {
337 mysql_query("insert into $WikiLinksStore (frompage, topage) " .
338 "values ('$frompage', '$topage')", $dbi["dbc"]);
343 mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
344 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"]);
347 /* more mysql queries:
350 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;