1 <?php rcs_id('$Id: mysql.php,v 1.2 2000-10-19 22:25:45 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 = "Cannot establish connection to database, giving up.";
34 $msg .= "<br>MySQL error: " . mysql_error();
37 if (!mysql_select_db($mysql_db, $dbc)) {
38 $msg = "Cannot open database, giving up.";
39 $msg .= "<br>MySQL error: " . mysql_error();
43 $dbi['table'] = $dbname;
48 function CloseDataBase($dbi) {
50 // mysql connections are established as persistant
51 // they cannot be closed through mysql_close()
55 function MakeDBHash($pagename, $pagehash)
57 $pagehash["pagename"] = addslashes($pagename);
58 if (!isset($pagehash["flags"]))
59 $pagehash["flags"] = 0;
60 $pagehash["author"] = addslashes($pagehash["author"]);
61 $pagehash["content"] = implode("\n", $pagehash["content"]);
62 $pagehash["content"] = addslashes($pagehash["content"]);
63 $pagehash["refs"] = serialize($pagehash["refs"]);
68 function MakePageHash($dbhash)
70 // unserialize/explode content
71 $dbhash['refs'] = unserialize($dbhash['refs']);
72 $dbhash['content'] = explode("\n", $dbhash['content']);
77 // Return hash of page + attributes or default
78 function RetrievePage($dbi, $pagename, $pagestore) {
79 $pagename = addslashes($pagename);
80 if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
81 if ($dbhash = mysql_fetch_array($res)) {
82 return MakePageHash($dbhash);
89 // Either insert or replace a key/value (a page)
90 function InsertPage($dbi, $pagename, $pagehash)
92 global $WikiPageStore; // ugly hack
94 if ($dbi['table'] == $WikiPageStore) { // HACK
95 $linklist = ExtractWikiPageLinks($pagehash['content']);
96 SetWikiPageLinks($dbi, $pagename, $linklist);
99 $pagehash = MakeDBHash($pagename, $pagehash);
101 $COLUMNS = "author, content, created, flags, " .
102 "lastmodified, pagename, refs, version";
104 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
105 "$pagehash[created], $pagehash[flags], " .
106 "$pagehash[lastmodified], '$pagehash[pagename]', " .
107 "'$pagehash[refs]', $pagehash[version]";
109 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
111 $msg = "Error writing page '$pagename' " . mysql_error();
117 // for archiving pages to a seperate dbm
118 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
119 global $ArchivePageStore;
120 $adbi = OpenDataBase($ArchivePageStore);
121 InsertPage($adbi, $pagename, $pagehash);
125 function IsWikiPage($dbi, $pagename) {
126 $pagename = addslashes($pagename);
127 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
128 return(mysql_result($res, 0));
133 function IsInArchive($dbi, $pagename) {
134 global $ArchivePageStore;
136 $pagename = addslashes($pagename);
137 if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
138 return(mysql_result($res, 0));
144 function IncreaseHitCount($dbi, $pagename)
146 $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
148 if (!mysql_affected_rows($dbi['dbc'])) {
149 $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
155 function GetHitCount($dbi, $pagename)
157 $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
158 if (mysql_num_rows($res))
159 $hits = mysql_result($res, 0);
167 // setup for title-search
168 function InitTitleSearch($dbi, $search) {
169 $search = addslashes($search);
170 $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
176 // iterating through database
177 function TitleSearchNextMatch($dbi, $res) {
178 if($o = mysql_fetch_object($res)) {
187 // setup for full-text search
188 function InitFullSearch($dbi, $search) {
189 $search = addslashes($search);
190 $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
195 // iterating through database
196 function FullSearchNextMatch($dbi, $res) {
197 if($hash = mysql_fetch_array($res)) {
198 return MakePageHash($hash);
205 function InitMostPopular($dbi, $limit) {
206 $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit", $dbi["dbc"]);
211 function MostPopularNextMatch($dbi, $res) {
212 if ($hits = mysql_fetch_array($res))
218 function GetAllWikiPageNames($dbi) {
219 $res = mysql_query("select pagename from wiki", $dbi["dbc"]);
220 $rows = mysql_num_rows($res);
221 for ($i = 0; $i < $rows; $i++) {
222 $pages[$i] = mysql_result($res, $i);
228 ////////////////////////////////////////
229 // functionality for the wikilinks table
231 // takes a page name, returns array of scored incoming and outgoing links
232 function GetWikiPageLinks($dbi, $pagename) {
233 $pagename = addslashes($pagename);
234 $res = mysql_query("select topage, score from wikilinks, wikiscore where topage=pagename and frompage='$pagename' order by score desc, topage");
235 $rows = mysql_num_rows($res);
236 for ($i = 0; $i < $rows; $i++) {
237 $out = mysql_fetch_array($res);
238 $links['out'][] = array($out['topage'], $out['score']);
241 $res = mysql_query("select frompage, score from wikilinks, wikiscore where frompage=pagename and topage='$pagename' order by score desc, frompage");
242 $rows = mysql_num_rows($res);
243 for ($i = 0; $i < $rows; $i++) {
244 $out = mysql_fetch_array($res);
245 $links['in'][] = array($out['frompage'], $out['score']);
248 $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");
249 $rows = mysql_num_rows($res);
250 for ($i = 0; $i < $rows; $i++) {
251 $out = mysql_fetch_array($res);
252 $links['popular'][] = array($out['pagename'], $out['hits']);
259 // takes page name, list of links it contains
260 // the $linklist is an array where the keys are the page names
261 function SetWikiPageLinks($dbi, $pagename, $linklist) {
262 $frompage = addslashes($pagename);
264 // first delete the old list of links
265 mysql_query("delete from wikilinks where frompage='$frompage'",
268 // the page may not have links, return if not
269 if (! count($linklist))
271 // now insert the new list of links
272 while (list($topage, $count) = each($linklist)) {
273 $topage = addslashes($topage);
274 if($topage != $frompage) {
275 mysql_query("insert into wikilinks (frompage, topage) " .
276 "values ('$frompage', '$topage')", $dbi["dbc"]);
281 mysql_query("delete from wikiscore", $dbi["dbc"]);
282 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"]);
285 /* more mysql queries:
288 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;