1 <!-- $Id: wiki_mysql.php3,v 1.11 2000-07-07 02:44:59 wainstead Exp $ -->
9 RetrievePage($dbi, $pagename)
10 InsertPage($dbi, $pagename, $pagehash)
11 IsWikiPage($dbi, $pagename)
12 InitTitleSearch($dbi, $search)
13 TitleSearchNextMatch($dbi, &$pos)
14 InitFullSearch($dbi, $search)
15 FullSearchNextMatch($dbi, &$pos)
16 IncreaseHitCount($dbi, $pagename)
17 GetHitCount($dbi, $pagename)
18 InitMostPopular($dbi, $limit)
19 MostPopularNextMatch($dbi, $res)
20 GetAllWikiPageNames($dbi)
25 // open a database and return the handle
26 // ignores MAX_DBM_ATTEMPTS
28 function OpenDataBase($dbname) {
29 global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
31 if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
32 echo "Cannot establish connection to database, giving up.";
33 echo "MySQL error: ", mysql_error(), "<br>\n";
36 if (!mysql_select_db($mysql_db, $dbc)) {
37 echo "Cannot open database, giving up.";
38 echo "MySQL error: ", mysql_error(), "<br>\n";
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) {
79 $pagename = addslashes($pagename);
80 if ($res = mysql_query("select * from $dbi[table] 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 $pagehash = MakeDBHash($pagename, $pagehash);
94 $COLUMNS = "author, content, created, flags, " .
95 "lastmodified, pagename, refs, version";
97 $VALUES = "'$pagehash[author]', '$pagehash[content]', " .
98 "$pagehash[created], $pagehash[flags], " .
99 "$pagehash[lastmodified], '$pagehash[pagename]', " .
100 "'$pagehash[refs]', $pagehash[version]";
103 if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
105 echo "error writing page '$pagename' ";
112 function IsWikiPage($dbi, $pagename) {
113 $pagename = addslashes($pagename);
114 if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
115 return(mysql_result($res, 0));
120 function IncreaseHitCount($dbi, $pagename)
122 $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
124 if (!mysql_affected_rows($dbi['dbc'])) {
125 $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
131 function GetHitCount($dbi, $pagename)
133 $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
134 if (mysql_num_rows($res))
135 $hits = mysql_result($res, 0);
143 // setup for title-search
144 function InitTitleSearch($dbi, $search) {
145 $search = addslashes($search);
146 $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
152 // iterating through database
153 function TitleSearchNextMatch($dbi, $res) {
154 if($o = mysql_fetch_object($res)) {
163 // setup for full-text search
164 function InitFullSearch($dbi, $search) {
165 $search = addslashes($search);
166 $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
171 // iterating through database
172 function FullSearchNextMatch($dbi, $res) {
173 if($hash = mysql_fetch_array($res)) {
174 return MakePageHash($hash);
181 function InitMostPopular($dbi, $limit) {
182 $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit");
187 function MostPopularNextMatch($dbi, $res) {
188 if ($hits = mysql_fetch_array($res))
194 function GetAllWikiPageNames($dbi) {
195 $res = mysql_query("select pagename from wiki");
196 $rows = mysql_num_rows($res);
197 for ($i = 0; $i < $rows; $i++) {
198 $pages[$i] = mysql_result($res, $i);