1 <?php rcs_id('$Id: mysql.php,v 1.10 2001-01-04 18:37:56 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 // prepare $pagehash for storing in mysql
61 function MakeDBHash($pagename, $pagehash)
63 $pagehash["pagename"] = addslashes($pagename);
64 if (!isset($pagehash["flags"]))
65 $pagehash["flags"] = 0;
66 $pagehash["author"] = addslashes($pagehash["author"]);
67 $pagehash["content"] = implode("\n", $pagehash["content"]);
68 $pagehash["content"] = addslashes($pagehash["content"]);
69 if (!isset($pagehash["refs"]))
70 $pagehash["refs"] = array();
71 $pagehash["refs"] = serialize($pagehash["refs"]);
77 // convert mysql result $dbhash to $pagehash
78 function MakePageHash($dbhash)
80 // unserialize/explode content
81 $dbhash['refs'] = unserialize($dbhash['refs']);
82 $dbhash['content'] = explode("\n", $dbhash['content']);
87 // Return hash of page + attributes or default
88 function RetrievePage($dbi, $pagename, $pagestore) {
89 $pagename = addslashes($pagename);
90 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
91 if ($dbhash = mysql_fetch_array($res)) {
92 return MakePageHash($dbhash);
99 // Either insert or replace a key/value (a page)
100 function InsertPage($dbi, $pagename, $pagehash)
102 global $WikiPageStore; // ugly hack
104 if ($dbi['table'] == $WikiPageStore) { // HACK
105 $linklist = ExtractWikiPageLinks($pagehash['content']);
106 SetWikiPageLinks($dbi, $pagename, $linklist);
109 $pagehash = MakeDBHash($pagename, $pagehash);
111 $COLUMNS = "author, content, created, flags, " .
112 "lastmodified, pagename, refs, version";
114 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
115 "$pagehash[created], $pagehash[flags], " .
116 "$pagehash[lastmodified], '$pagehash[pagename]', " .
117 "'$pagehash[refs]', $pagehash[version]";
119 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
121 $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
123 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
129 // for archiving pages to a seperate dbm
130 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
131 global $ArchivePageStore;
132 $adbi = OpenDataBase($ArchivePageStore);
133 InsertPage($adbi, $pagename, $pagehash);
137 function IsWikiPage($dbi, $pagename) {
138 $pagename = addslashes($pagename);
139 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
140 return(mysql_result($res, 0));
145 function IsInArchive($dbi, $pagename) {
146 global $ArchivePageStore;
148 $pagename = addslashes($pagename);
149 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
150 return(mysql_result($res, 0));
156 function RemovePage($dbi, $pagename) {
157 global $WikiPageStore, $ArchivePageStore;
158 global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
160 $pagename = addslashes($pagename);
161 $msg = gettext ("Cannot delete '%s' from table '%s'");
163 $msg .= gettext ("MySQL error: %s");
165 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
166 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
168 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
169 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
171 if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
172 ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
174 if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
175 ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
177 if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
178 ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
182 function IncreaseHitCount($dbi, $pagename)
184 global $HitCountStore;
186 $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
188 if (!mysql_affected_rows($dbi['dbc'])) {
189 $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
195 function GetHitCount($dbi, $pagename)
197 global $HitCountStore;
199 $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
200 if (mysql_num_rows($res))
201 $hits = mysql_result($res, 0);
208 function MakeSQLSearchClause($search, $column)
210 $search = addslashes(preg_replace("/\s+/", " ", $search));
211 $term = strtok($search, ' ');
215 if ($word[0] == '-') {
216 $word = substr($word, 1);
217 $clause .= "not ($column like '%$word%') ";
219 $clause .= "($column like '%$word%') ";
221 if ($term = strtok(' '))
227 // setup for title-search
228 function InitTitleSearch($dbi, $search) {
229 $clause = MakeSQLSearchClause($search, 'pagename');
230 $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
236 // iterating through database
237 function TitleSearchNextMatch($dbi, $res) {
238 if($o = mysql_fetch_object($res)) {
247 // setup for full-text search
248 function InitFullSearch($dbi, $search) {
249 $clause = MakeSQLSearchClause($search, 'content');
250 $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
255 // iterating through database
256 function FullSearchNextMatch($dbi, $res) {
257 if($hash = mysql_fetch_array($res)) {
258 return MakePageHash($hash);
265 function InitMostPopular($dbi, $limit) {
266 global $HitCountStore;
267 $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
272 function MostPopularNextMatch($dbi, $res) {
273 if ($hits = mysql_fetch_array($res))
279 function GetAllWikiPageNames($dbi) {
280 global $WikiPageStore;
281 $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
282 $rows = mysql_num_rows($res);
283 for ($i = 0; $i < $rows; $i++) {
284 $pages[$i] = mysql_result($res, $i);
290 ////////////////////////////////////////
291 // functionality for the wikilinks table
293 // takes a page name, returns array of scored incoming and outgoing links
294 function GetWikiPageLinks($dbi, $pagename) {
295 global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
297 $pagename = addslashes($pagename);
298 $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
299 $rows = mysql_num_rows($res);
300 for ($i = 0; $i < $rows; $i++) {
301 $out = mysql_fetch_array($res);
302 $links['out'][] = array($out['topage'], $out['score']);
305 $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
306 $rows = mysql_num_rows($res);
307 for ($i = 0; $i < $rows; $i++) {
308 $out = mysql_fetch_array($res);
309 $links['in'][] = array($out['frompage'], $out['score']);
312 $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");
313 $rows = mysql_num_rows($res);
314 for ($i = 0; $i < $rows; $i++) {
315 $out = mysql_fetch_array($res);
316 $links['popular'][] = array($out['pagename'], $out['hits']);
323 // takes page name, list of links it contains
324 // the $linklist is an array where the keys are the page names
325 function SetWikiPageLinks($dbi, $pagename, $linklist) {
326 global $WikiLinksStore, $WikiScoreStore;
328 $frompage = addslashes($pagename);
330 // first delete the old list of links
331 mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
334 // the page may not have links, return if not
335 if (! count($linklist))
337 // now insert the new list of links
338 while (list($topage, $count) = each($linklist)) {
339 $topage = addslashes($topage);
340 if($topage != $frompage) {
341 mysql_query("insert into $WikiLinksStore (frompage, topage) " .
342 "values ('$frompage', '$topage')", $dbi["dbc"]);
347 mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
348 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"]);
351 /* more mysql queries:
354 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;