1 <?php rcs_id('$Id: mysql.php,v 1.5 2000-11-09 02:57:51 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 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;
153 $pagename = addslashes($pagename);
154 $msg = gettext ("Cannot delete '%s' from table '%s'");
156 $msg .= gettext ("MySQL error: %s");
158 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
159 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
161 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
162 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
164 if (!mysql_query("delete from wikilinks where frompage='$pagename'", $dbi['dbc']))
165 ExitWiki(sprintf($msg, $pagename, 'wikilinks', mysql_error()));
167 if (!mysql_query("delete from hitcount where pagename='$pagename'", $dbi['dbc']))
168 ExitWiki(sprintf($msg, $pagename, 'hitcount', mysql_error()));
170 if (!mysql_query("delete from wikiscore where pagename='$pagename'", $dbi['dbc']))
171 ExitWiki(sprintf($msg, $pagename, 'wikiscore', mysql_error()));
175 function IncreaseHitCount($dbi, $pagename)
177 $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
179 if (!mysql_affected_rows($dbi['dbc'])) {
180 $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
186 function GetHitCount($dbi, $pagename)
188 $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
189 if (mysql_num_rows($res))
190 $hits = mysql_result($res, 0);
198 // setup for title-search
199 function InitTitleSearch($dbi, $search) {
200 $search = addslashes($search);
201 $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
207 // iterating through database
208 function TitleSearchNextMatch($dbi, $res) {
209 if($o = mysql_fetch_object($res)) {
218 // setup for full-text search
219 function InitFullSearch($dbi, $search) {
220 $search = addslashes($search);
221 $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
226 // iterating through database
227 function FullSearchNextMatch($dbi, $res) {
228 if($hash = mysql_fetch_array($res)) {
229 return MakePageHash($hash);
236 function InitMostPopular($dbi, $limit) {
237 $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit", $dbi["dbc"]);
242 function MostPopularNextMatch($dbi, $res) {
243 if ($hits = mysql_fetch_array($res))
249 function GetAllWikiPageNames($dbi) {
250 $res = mysql_query("select pagename from wiki", $dbi["dbc"]);
251 $rows = mysql_num_rows($res);
252 for ($i = 0; $i < $rows; $i++) {
253 $pages[$i] = mysql_result($res, $i);
259 ////////////////////////////////////////
260 // functionality for the wikilinks table
262 // takes a page name, returns array of scored incoming and outgoing links
263 function GetWikiPageLinks($dbi, $pagename) {
264 $pagename = addslashes($pagename);
265 $res = mysql_query("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
266 $rows = mysql_num_rows($res);
267 for ($i = 0; $i < $rows; $i++) {
268 $out = mysql_fetch_array($res);
269 $links['out'][] = array($out['topage'], $out['score']);
272 $res = mysql_query("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
273 $rows = mysql_num_rows($res);
274 for ($i = 0; $i < $rows; $i++) {
275 $out = mysql_fetch_array($res);
276 $links['in'][] = array($out['frompage'], $out['score']);
279 $res = mysql_query("select distinct pagename, hits from wikilinks, hitcount where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
280 $rows = mysql_num_rows($res);
281 for ($i = 0; $i < $rows; $i++) {
282 $out = mysql_fetch_array($res);
283 $links['popular'][] = array($out['pagename'], $out['hits']);
290 // takes page name, list of links it contains
291 // the $linklist is an array where the keys are the page names
292 function SetWikiPageLinks($dbi, $pagename, $linklist) {
293 $frompage = addslashes($pagename);
295 // first delete the old list of links
296 mysql_query("delete from wikilinks where frompage='$frompage'",
299 // the page may not have links, return if not
300 if (! count($linklist))
302 // now insert the new list of links
303 while (list($topage, $count) = each($linklist)) {
304 $topage = addslashes($topage);
305 if($topage != $frompage) {
306 mysql_query("insert into wikilinks (frompage, topage) " .
307 "values ('$frompage', '$topage')", $dbi["dbc"]);
312 mysql_query("delete from wikiscore", $dbi["dbc"]);
313 mysql_query("insert into wikiscore select w1.topage, count(*) from wikilinks as w1, wikilinks as w2 where w2.topage=w1.frompage group by w1.topage", $dbi["dbc"]);
316 /* more mysql queries:
319 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;