1 <!-- $Id: wiki_msql.php3,v 1.8 2000-06-29 04:29:06 wainstead Exp $ -->
7 MakeDBHash($pagename, $pagehash)
10 RetrievePage($dbi, $pagename)
11 InsertPage($dbi, $pagename, $pagehash)
12 IsWikiPage($dbi, $pagename)
13 InitTitleSearch($dbi, $search)
14 TitleSearchNextMatch($dbi, &$pos)
15 InitFullSearch($dbi, $search)
16 FullSearchNextMatch($dbi, &$pos)
20 // open a database and return the handle
21 // ignores MAX_DBM_ATTEMPTS
23 function OpenDataBase($dbinfo) {
26 if (! ($dbc = msql_pconnect())) {
27 echo "Cannot establish connection to database, giving up.";
28 echo "Error message: ", msql_error(), "<br>\n";
31 if (!msql_select_db($msql_db, $dbc)) {
32 echo "Cannot open database $msql_db, giving up.";
33 echo "Error message: ", msql_error(), "<br>\n";
38 $dbi['table'] = $dbinfo['table']; // page metadata
39 $dbi['page_table'] = $dbinfo['page_table']; // page content
44 function CloseDataBase($dbi) {
46 // msql connections are established as persistant
47 // they cannot be closed through msql_close()
51 // This should receive the full text of the page in one string
52 // It will break the page text into an array of strings
53 // of length MSQL_MAX_LINE_LENGTH which should match the length
54 // of the columns wikipages.LINE, archivepages.LINE in schema.minisql
56 function msqlDecomposeString($string) {
60 // zero, one, infinity
61 // account for the small case
62 if (strlen($string) < MSQL_MAX_LINE_LENGTH) {
63 $ret_arr[$el] = $string;
68 $line = $string2 = "";
70 // split on single spaces
71 $words = preg_split("/ /", $string);
72 $num_words = count($words);
75 $ret_arr[0] = $words[0];
78 // for all words, build up lines < MSQL_MAX_LINE_LENGTH in $ret_arr
79 for ($x = 2; $x < $num_words; $x++) {
80 $length = strlen($line) + strlen($words[$x])
81 + strlen($ret_arr[$el]) + 1;
83 if ($length < MSQL_MAX_LINE_LENGTH) {
84 $line .= " " . $words[$x];
86 // put this line in the return array, reset, continue
87 $ret_arr[$el++] .= $line;
88 $line = " $words[$x]"; // reset
91 $ret_arr[$el] = $line;
96 // Take form data and prepare it for the db
97 function MakeDBHash($pagename, $pagehash)
99 $pagehash["pagename"] = addslashes($pagename);
100 if (!isset($pagehash["flags"]))
101 $pagehash["flags"] = 0;
102 if (!isset($pagehash["content"])) {
103 $pagehash["content"] = array();
105 $pagehash["content"] = implode("\n", $pagehash["content"]);
106 $pagehash["content"] = msqlDecomposeString($pagehash["content"]);
108 $pagehash["author"] = addslashes($pagehash["author"]);
109 $pagehash["refs"] = serialize($pagehash["refs"]);
115 // Take db data and prepare it for display
116 function MakePageHash($dbhash)
118 // unserialize/explode content
119 $dbhash['refs'] = unserialize($dbhash['refs']);
124 // Return hash of page + attributes or default
125 function RetrievePage($dbi, $pagename) {
126 $pagename = addslashes($pagename);
128 $query = "select * from $dbi[table] where pagename='$pagename'";
130 if ($res = msql_query($query, $dbi['dbc'])) {
131 $dbhash = msql_fetch_array($res);
133 $query = "select lineno,line from $dbi[page_table] " .
134 "where pagename='$pagename' " .
137 if ($res = msql_query($query, $dbi[dbc])) {
138 $dbhash["content"] = array();
139 while ($row = msql_fetch_array($res)) {
140 $msql_content .= $row["line"];
142 $dbhash["content"] = explode("\n", $msql_content);
145 return MakePageHash($dbhash);
151 // Either insert or replace a key/value (a page)
152 function InsertPage($dbi, $pagename, $pagehash) {
154 $pagehash = MakeDBHash($pagename, $pagehash);
155 // $pagehash["content"] is now an array of strings
156 // of MSQL_MAX_LINE_LENGTH
158 // record the time of modification
159 $pagehash["lastmodified"] = time();
161 if (IsWikiPage($dbi, $pagename)) {
163 $PAIRS = "author='$pagehash[author]'," .
164 "created=$pagehash[created]," .
165 "flags=$pagehash[flags]," .
166 "lastmodified=$pagehash[lastmodified]," .
167 "pagename='$pagehash[pagename]'," .
168 "refs='$pagehash[refs]'," .
169 "version=$pagehash[version]";
171 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
175 // build up the column names and values for the query
177 $COLUMNS = "author, created, flags, lastmodified, " .
178 "pagename, refs, version";
180 $VALUES = "'$pagehash[author]', " .
181 "$pagehash[created], $pagehash[flags], " .
182 "$pagehash[lastmodified], '$pagehash[pagename]', " .
183 "'$pagehash[refs]', $pagehash[version]";
186 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
189 // echo "<p>Query: $query<p>\n";
191 // first, insert the metadata
192 $retval = msql_query($query, $dbi['dbc']);
193 if ($retval == false)
194 echo "Insert/update failed: ", msql_error(), "<br>\n";
197 // second, insert the page data
198 // remove old data from page_table
199 $query = "delete from $dbi[page_table] where pagename='$pagename'";
200 // echo "Delete query: $query<br>\n";
201 $retval = msql_query($query, $dbi['dbc']);
202 if ($retval == false)
203 echo "Delete on $dbi[page_table] failed: ", msql_error(), "<br>\n";
205 // insert the new lines
206 reset($pagehash["content"]);
208 for ($x = 0; $x < count($pagehash["content"]); $x++) {
209 $line = addslashes($pagehash["content"][$x]);
210 $query = "INSERT INTO $dbi[page_table] " .
211 "(pagename, lineno, line) " .
212 "VALUES('$pagename', $x, '$line')";
213 // echo "Page line insert query: $query<br>\n";
214 $retval = msql_query($query, $dbi['dbc']);
215 if ($retval == false)
216 echo "Insert into $dbi[page_table] failed: ",
217 msql_error(), "<br>\n";
224 function IsWikiPage($dbi, $pagename) {
225 $pagename = addslashes($pagename);
226 $query = "select pagename from $dbi[table] where pagename='$pagename'";
227 // echo "Query: $query<br>\n";
228 if ($res = msql_query($query, $dbi['dbc'])) {
229 return(msql_affected_rows($res));
234 // setup for title-search
235 function InitTitleSearch($dbi, $search) {
236 $search = addslashes($search);
237 $query = "select pagename from $dbi[table] " .
238 "where pagename clike '%$search%' order by pagename";
239 $res = msql_query($query, $dbi["dbc"]);
245 // iterating through database
246 function TitleSearchNextMatch($dbi, $res) {
247 if($o = msql_fetch_object($res)) {
256 // setup for full-text search
257 function InitFullSearch($dbi, $search) {
258 $search = addslashes($search);
259 $query = "select * from $dbi[table] where searchterms clike '%$search%'";
260 $res = msql_query($query, $dbi["dbc"]);
265 // iterating through database
266 function FullSearchNextMatch($dbi, $res) {
267 if($hash = msql_fetch_array($res)) {
268 return MakePageHash($hash);
274 ////////////////////////
275 // new database features
278 function IncreaseHitCount($dbi, $pagename) {
280 $query = "update hitcount set hits=hits+1 where pagename='$pagename'";
281 $res = mysql_query($query, $dbi['dbc']);
283 if (!mysql_affected_rows($dbi['dbc'])) {
284 $query = "insert into hitcount (pagename, hits) " .
285 "values ('$pagename', 1)";
286 $res = mysql_query($query, $dbi['dbc']);
292 function GetHitCount($dbi, $pagename) {
294 $query = "select hits from hitcount where pagename='$pagename'";
295 $res = mysql_query($query, $dbi['dbc']);
296 if (mysql_num_rows($res)) {
297 $hits = mysql_result($res, 0);
307 function InitMostPopular($dbi, $limit) {
309 $query = "select * from hitcount " .
310 "order by hits desc, pagename limit $limit";
312 $res = mysql_query($query);
317 function MostPopularNextMatch($dbi, $res) {
319 if ($hits = mysql_fetch_array($res)) {