1 <?php rcs_id('$Id: mysql.php,v 1.6 2000-11-13 14:54:08 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 InitTitleSearch($dbi, $search)
18 TitleSearchNextMatch($dbi, $res)
19 InitFullSearch($dbi, $search)
20 FullSearchNextMatch($dbi, $res)
21 InitMostPopular($dbi, $limit)
22 MostPopularNextMatch($dbi, $res)
23 GetAllWikiPageNames($dbi)
24 GetWikiPageLinks($dbi, $pagename)
25 SetWikiPageLinks($dbi, $pagename, $linklist)
28 // open a database and return the handle
29 // ignores MAX_DBM_ATTEMPTS
31 function OpenDataBase($dbname) {
32 global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
34 if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
35 $msg = gettext ("Cannot establish connection to database, giving up.");
37 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
40 if (!mysql_select_db($mysql_db, $dbc)) {
41 $msg = sprintf(gettext ("Cannot open database %s, giving up."), $mysql_db);
43 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
47 $dbi['table'] = $dbname;
52 function CloseDataBase($dbi) {
54 // mysql connections are established as persistant
55 // they cannot be closed through mysql_close()
59 function MakeDBHash($pagename, $pagehash)
61 $pagehash["pagename"] = addslashes($pagename);
62 if (!isset($pagehash["flags"]))
63 $pagehash["flags"] = 0;
64 $pagehash["author"] = addslashes($pagehash["author"]);
65 $pagehash["content"] = implode("\n", $pagehash["content"]);
66 $pagehash["content"] = addslashes($pagehash["content"]);
67 $pagehash["refs"] = serialize($pagehash["refs"]);
72 function MakePageHash($dbhash)
74 // unserialize/explode content
75 $dbhash['refs'] = unserialize($dbhash['refs']);
76 $dbhash['content'] = explode("\n", $dbhash['content']);
81 // Return hash of page + attributes or default
82 function RetrievePage($dbi, $pagename, $pagestore) {
83 $pagename = addslashes($pagename);
84 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
85 if ($dbhash = mysql_fetch_array($res)) {
86 return MakePageHash($dbhash);
93 // Either insert or replace a key/value (a page)
94 function InsertPage($dbi, $pagename, $pagehash)
96 global $WikiPageStore; // ugly hack
98 if ($dbi['table'] == $WikiPageStore) { // HACK
99 $linklist = ExtractWikiPageLinks($pagehash['content']);
100 SetWikiPageLinks($dbi, $pagename, $linklist);
103 $pagehash = MakeDBHash($pagename, $pagehash);
105 $COLUMNS = "author, content, created, flags, " .
106 "lastmodified, pagename, refs, version";
108 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
109 "$pagehash[created], $pagehash[flags], " .
110 "$pagehash[lastmodified], '$pagehash[pagename]', " .
111 "'$pagehash[refs]', $pagehash[version]";
113 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
115 $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
117 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
123 // for archiving pages to a seperate dbm
124 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
125 global $ArchivePageStore;
126 $adbi = OpenDataBase($ArchivePageStore);
127 InsertPage($adbi, $pagename, $pagehash);
131 function IsWikiPage($dbi, $pagename) {
132 $pagename = addslashes($pagename);
133 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
134 return(mysql_result($res, 0));
139 function IsInArchive($dbi, $pagename) {
140 global $ArchivePageStore;
142 $pagename = addslashes($pagename);
143 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
144 return(mysql_result($res, 0));
150 function RemovePage($dbi, $pagename) {
151 global $WikiPageStore, $ArchivePageStore
152 global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
154 $pagename = addslashes($pagename);
155 $msg = gettext ("Cannot delete '%s' from table '%s'");
157 $msg .= gettext ("MySQL error: %s");
159 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
160 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
162 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
163 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
165 if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
166 ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
168 if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
169 ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
171 if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
172 ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
176 function IncreaseHitCount($dbi, $pagename)
178 global $HitCountStore;
180 $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
182 if (!mysql_affected_rows($dbi['dbc'])) {
183 $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
189 function GetHitCount($dbi, $pagename)
191 global $HitCountStore;
193 $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
194 if (mysql_num_rows($res))
195 $hits = mysql_result($res, 0);
203 // setup for title-search
204 function InitTitleSearch($dbi, $search) {
205 $search = addslashes($search);
206 $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
212 // iterating through database
213 function TitleSearchNextMatch($dbi, $res) {
214 if($o = mysql_fetch_object($res)) {
223 // setup for full-text search
224 function InitFullSearch($dbi, $search) {
225 $search = addslashes($search);
226 $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
231 // iterating through database
232 function FullSearchNextMatch($dbi, $res) {
233 if($hash = mysql_fetch_array($res)) {
234 return MakePageHash($hash);
241 function InitMostPopular($dbi, $limit) {
242 global $HitCountStore;
243 $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
248 function MostPopularNextMatch($dbi, $res) {
249 if ($hits = mysql_fetch_array($res))
255 function GetAllWikiPageNames($dbi) {
256 global $WikiPageStore;
257 $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
258 $rows = mysql_num_rows($res);
259 for ($i = 0; $i < $rows; $i++) {
260 $pages[$i] = mysql_result($res, $i);
266 ////////////////////////////////////////
267 // functionality for the wikilinks table
269 // takes a page name, returns array of scored incoming and outgoing links
270 function GetWikiPageLinks($dbi, $pagename) {
271 global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
273 $pagename = addslashes($pagename);
274 $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
275 $rows = mysql_num_rows($res);
276 for ($i = 0; $i < $rows; $i++) {
277 $out = mysql_fetch_array($res);
278 $links['out'][] = array($out['topage'], $out['score']);
281 $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
282 $rows = mysql_num_rows($res);
283 for ($i = 0; $i < $rows; $i++) {
284 $out = mysql_fetch_array($res);
285 $links['in'][] = array($out['frompage'], $out['score']);
288 $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");
289 $rows = mysql_num_rows($res);
290 for ($i = 0; $i < $rows; $i++) {
291 $out = mysql_fetch_array($res);
292 $links['popular'][] = array($out['pagename'], $out['hits']);
299 // takes page name, list of links it contains
300 // the $linklist is an array where the keys are the page names
301 function SetWikiPageLinks($dbi, $pagename, $linklist) {
302 global $WikiLinksStore, $WikiScoreStore;
304 $frompage = addslashes($pagename);
306 // first delete the old list of links
307 mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
310 // the page may not have links, return if not
311 if (! count($linklist))
313 // now insert the new list of links
314 while (list($topage, $count) = each($linklist)) {
315 $topage = addslashes($topage);
316 if($topage != $frompage) {
317 mysql_query("insert into $WikiLinksStore (frompage, topage) " .
318 "values ('$frompage', '$topage')", $dbi["dbc"]);
323 mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
324 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"]);
327 /* more mysql queries:
330 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;