1 <?php rcs_id('$Id: mysql.php,v 1.4 2000-11-08 15:40:56 ahollosi Exp $');
8 RetrievePage($dbi, $pagename, $pagestore)
9 InsertPage($dbi, $pagename, $pagehash)
10 SaveCopyToArchive($dbi, $pagename, $pagehash)
11 IsWikiPage($dbi, $pagename)
12 IsInArchive($dbi, $pagename)
13 InitTitleSearch($dbi, $search)
14 TitleSearchNextMatch($dbi, &$pos)
15 InitFullSearch($dbi, $search)
16 FullSearchNextMatch($dbi, &$pos)
17 IncreaseHitCount($dbi, $pagename)
18 GetHitCount($dbi, $pagename)
19 InitMostPopular($dbi, $limit)
20 MostPopularNextMatch($dbi, $res)
21 GetAllWikiPageNames($dbi)
22 GetWikiPageLinks($dbi, $pagename)
23 SetWikiPageLinks($dbi, $pagename, $linklist)
26 // open a database and return the handle
27 // ignores MAX_DBM_ATTEMPTS
29 function OpenDataBase($dbname) {
30 global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
32 if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
33 $msg = gettext ("Cannot establish connection to database, giving up.");
35 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
38 if (!mysql_select_db($mysql_db, $dbc)) {
39 $msg = sprintf(gettext ("Cannot open database %s, giving up."), $mysql_db);
41 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
45 $dbi['table'] = $dbname;
50 function CloseDataBase($dbi) {
52 // mysql connections are established as persistant
53 // they cannot be closed through mysql_close()
57 function MakeDBHash($pagename, $pagehash)
59 $pagehash["pagename"] = addslashes($pagename);
60 if (!isset($pagehash["flags"]))
61 $pagehash["flags"] = 0;
62 $pagehash["author"] = addslashes($pagehash["author"]);
63 $pagehash["content"] = implode("\n", $pagehash["content"]);
64 $pagehash["content"] = addslashes($pagehash["content"]);
65 $pagehash["refs"] = serialize($pagehash["refs"]);
70 function MakePageHash($dbhash)
72 // unserialize/explode content
73 $dbhash['refs'] = unserialize($dbhash['refs']);
74 $dbhash['content'] = explode("\n", $dbhash['content']);
79 // Return hash of page + attributes or default
80 function RetrievePage($dbi, $pagename, $pagestore) {
81 $pagename = addslashes($pagename);
82 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
83 if ($dbhash = mysql_fetch_array($res)) {
84 return MakePageHash($dbhash);
91 // Either insert or replace a key/value (a page)
92 function InsertPage($dbi, $pagename, $pagehash)
94 global $WikiPageStore; // ugly hack
96 if ($dbi['table'] == $WikiPageStore) { // HACK
97 $linklist = ExtractWikiPageLinks($pagehash['content']);
98 SetWikiPageLinks($dbi, $pagename, $linklist);
101 $pagehash = MakeDBHash($pagename, $pagehash);
103 $COLUMNS = "author, content, created, flags, " .
104 "lastmodified, pagename, refs, version";
106 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
107 "$pagehash[created], $pagehash[flags], " .
108 "$pagehash[lastmodified], '$pagehash[pagename]', " .
109 "'$pagehash[refs]', $pagehash[version]";
111 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
113 $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
115 $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
121 // for archiving pages to a seperate dbm
122 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
123 global $ArchivePageStore;
124 $adbi = OpenDataBase($ArchivePageStore);
125 InsertPage($adbi, $pagename, $pagehash);
129 function IsWikiPage($dbi, $pagename) {
130 $pagename = addslashes($pagename);
131 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
132 return(mysql_result($res, 0));
137 function IsInArchive($dbi, $pagename) {
138 global $ArchivePageStore;
140 $pagename = addslashes($pagename);
141 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
142 return(mysql_result($res, 0));
148 function RemovePage($dbi, $pagename) {
149 global $WikiPageStore, $ArchivePageStore;
151 $pagename = addslashes($pagename);
152 $msg = gettext ("Cannot delete '%s' from table '%s'");
154 $msg .= gettext ("MySQL error: %s");
156 if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
157 ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
159 if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
160 ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
162 if (!mysql_query("delete from wikilinks where frompage='$pagename'", $dbi['dbc']))
163 ExitWiki(sprintf($msg, $pagename, 'wikilinks', mysql_error()));
165 if (!mysql_query("delete from hitcount where pagename='$pagename'", $dbi['dbc']))
166 ExitWiki(sprintf($msg, $pagename, 'hitcount', mysql_error()));
168 if (!mysql_query("delete from wikiscore where pagename='$pagename'", $dbi['dbc']))
169 ExitWiki(sprintf($msg, $pagename, 'wikiscore', mysql_error()));
173 function IncreaseHitCount($dbi, $pagename)
175 $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
177 if (!mysql_affected_rows($dbi['dbc'])) {
178 $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
184 function GetHitCount($dbi, $pagename)
186 $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
187 if (mysql_num_rows($res))
188 $hits = mysql_result($res, 0);
196 // setup for title-search
197 function InitTitleSearch($dbi, $search) {
198 $search = addslashes($search);
199 $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
205 // iterating through database
206 function TitleSearchNextMatch($dbi, $res) {
207 if($o = mysql_fetch_object($res)) {
216 // setup for full-text search
217 function InitFullSearch($dbi, $search) {
218 $search = addslashes($search);
219 $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
224 // iterating through database
225 function FullSearchNextMatch($dbi, $res) {
226 if($hash = mysql_fetch_array($res)) {
227 return MakePageHash($hash);
234 function InitMostPopular($dbi, $limit) {
235 $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit", $dbi["dbc"]);
240 function MostPopularNextMatch($dbi, $res) {
241 if ($hits = mysql_fetch_array($res))
247 function GetAllWikiPageNames($dbi) {
248 $res = mysql_query("select pagename from wiki", $dbi["dbc"]);
249 $rows = mysql_num_rows($res);
250 for ($i = 0; $i < $rows; $i++) {
251 $pages[$i] = mysql_result($res, $i);
257 ////////////////////////////////////////
258 // functionality for the wikilinks table
260 // takes a page name, returns array of scored incoming and outgoing links
261 function GetWikiPageLinks($dbi, $pagename) {
262 $pagename = addslashes($pagename);
263 $res = mysql_query("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
264 $rows = mysql_num_rows($res);
265 for ($i = 0; $i < $rows; $i++) {
266 $out = mysql_fetch_array($res);
267 $links['out'][] = array($out['topage'], $out['score']);
270 $res = mysql_query("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
271 $rows = mysql_num_rows($res);
272 for ($i = 0; $i < $rows; $i++) {
273 $out = mysql_fetch_array($res);
274 $links['in'][] = array($out['frompage'], $out['score']);
277 $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");
278 $rows = mysql_num_rows($res);
279 for ($i = 0; $i < $rows; $i++) {
280 $out = mysql_fetch_array($res);
281 $links['popular'][] = array($out['pagename'], $out['hits']);
288 // takes page name, list of links it contains
289 // the $linklist is an array where the keys are the page names
290 function SetWikiPageLinks($dbi, $pagename, $linklist) {
291 $frompage = addslashes($pagename);
293 // first delete the old list of links
294 mysql_query("delete from wikilinks where frompage='$frompage'",
297 // the page may not have links, return if not
298 if (! count($linklist))
300 // now insert the new list of links
301 while (list($topage, $count) = each($linklist)) {
302 $topage = addslashes($topage);
303 if($topage != $frompage) {
304 mysql_query("insert into wikilinks (frompage, topage) " .
305 "values ('$frompage', '$topage')", $dbi["dbc"]);
310 mysql_query("delete from wikiscore", $dbi["dbc"]);
311 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"]);
314 /* more mysql queries:
317 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;