1 <!-- $Id: wiki_msql.php3,v 1.16 2000-09-16 19:20:46 wainstead Exp $ -->
7 MakeDBHash($pagename, $pagehash)
10 RetrievePage($dbi, $pagename, $pagestore)
11 InsertPage($dbi, $pagename, $pagehash)
12 SaveCopyToArchive($dbi, $pagename, $pagehash)
13 IsWikiPage($dbi, $pagename)
14 InitTitleSearch($dbi, $search)
15 TitleSearchNextMatch($dbi, &$pos)
16 InitFullSearch($dbi, $search)
17 FullSearchNextMatch($dbi, &$pos)
18 GetAllWikiPageNames($dbi)
22 // open a database and return the handle
23 // ignores MAX_DBM_ATTEMPTS
25 function OpenDataBase($dbinfo) {
28 if (! ($dbc = msql_connect())) {
29 echo "Cannot establish connection to database, giving up.";
30 echo "Error message: ", msql_error(), "<br>\n";
33 if (!msql_select_db($msql_db, $dbc)) {
34 echo "Cannot open database $msql_db, giving up.";
35 echo "Error message: ", msql_error(), "<br>\n";
40 $dbi['table'] = $dbinfo['table']; // page metadata
41 $dbi['page_table'] = $dbinfo['page_table']; // page content
46 function CloseDataBase($dbi) {
47 // I found msql_pconnect unstable so we go the slow route.
48 return msql_close($dbi['dbc']);
52 // This should receive the full text of the page in one string
53 // It will break the page text into an array of strings
54 // of length MSQL_MAX_LINE_LENGTH which should match the length
55 // of the columns wikipages.LINE, archivepages.LINE in schema.minisql
57 function msqlDecomposeString($string) {
61 // zero, one, infinity
62 // account for the small case
63 if (strlen($string) < MSQL_MAX_LINE_LENGTH) {
64 $ret_arr[$el] = $string;
69 $line = $string2 = "";
71 // split on single spaces
72 $words = preg_split("/ /", $string);
73 $num_words = count($words);
76 $ret_arr[0] = $words[0];
79 // for all words, build up lines < MSQL_MAX_LINE_LENGTH in $ret_arr
80 for ($x = 2; $x < $num_words; $x++) {
81 $length = strlen($line) + strlen($words[$x])
82 + strlen($ret_arr[$el]) + 1;
84 if ($length < MSQL_MAX_LINE_LENGTH) {
85 $line .= " " . $words[$x];
87 // put this line in the return array, reset, continue
88 $ret_arr[$el++] .= $line;
89 $line = " $words[$x]"; // reset
92 $ret_arr[$el] = $line;
97 // Take form data and prepare it for the db
98 function MakeDBHash($pagename, $pagehash)
100 $pagehash["pagename"] = addslashes($pagename);
101 if (!isset($pagehash["flags"]))
102 $pagehash["flags"] = 0;
103 if (!isset($pagehash["content"])) {
104 $pagehash["content"] = array();
106 $pagehash["content"] = implode("\n", $pagehash["content"]);
107 $pagehash["content"] = msqlDecomposeString($pagehash["content"]);
109 $pagehash["author"] = addslashes($pagehash["author"]);
110 $pagehash["refs"] = serialize($pagehash["refs"]);
116 // Take db data and prepare it for display
117 function MakePageHash($dbhash)
119 // unserialize/explode content
120 $dbhash['refs'] = unserialize($dbhash['refs']);
125 // Return hash of page + attributes or default
126 function RetrievePage($dbi, $pagename, $pagestore) {
127 $pagename = addslashes($pagename);
128 $table = $pagestore['table'];
129 $pagetable = $pagestore['page_table'];
131 $query = "select * from $table where pagename='$pagename'";
132 // echo "<p>query: $query<p>";
133 $res = msql_query($query, $dbi['dbc']);
134 if (msql_num_rows($res)) {
135 $dbhash = msql_fetch_array($res);
137 $query = "select lineno,line from $pagetable " .
138 "where pagename='$pagename' " .
141 if ($res = msql_query($query, $dbi['dbc'])) {
142 $dbhash["content"] = array();
143 while ($row = msql_fetch_array($res)) {
144 $msql_content .= $row["line"];
146 $dbhash["content"] = explode("\n", $msql_content);
149 return MakePageHash($dbhash);
155 // Either insert or replace a key/value (a page)
156 function InsertPage($dbi, $pagename, $pagehash) {
158 $pagehash = MakeDBHash($pagename, $pagehash);
159 // $pagehash["content"] is now an array of strings
160 // of MSQL_MAX_LINE_LENGTH
162 // record the time of modification
163 $pagehash["lastmodified"] = time();
165 if (IsWikiPage($dbi, $pagename)) {
167 $PAIRS = "author='$pagehash[author]'," .
168 "created=$pagehash[created]," .
169 "flags=$pagehash[flags]," .
170 "lastmodified=$pagehash[lastmodified]," .
171 "pagename='$pagehash[pagename]'," .
172 "refs='$pagehash[refs]'," .
173 "version=$pagehash[version]";
175 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
179 // build up the column names and values for the query
181 $COLUMNS = "author, created, flags, lastmodified, " .
182 "pagename, refs, version";
184 $VALUES = "'$pagehash[author]', " .
185 "$pagehash[created], $pagehash[flags], " .
186 "$pagehash[lastmodified], '$pagehash[pagename]', " .
187 "'$pagehash[refs]', $pagehash[version]";
190 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
193 // echo "<p>Query: $query<p>\n";
195 // first, insert the metadata
196 $retval = msql_query($query, $dbi['dbc']);
197 if ($retval == false)
198 echo "Insert/update failed: ", msql_error(), "<br>\n";
201 // second, insert the page data
202 // remove old data from page_table
203 $query = "delete from $dbi[page_table] where pagename='$pagename'";
204 // echo "Delete query: $query<br>\n";
205 $retval = msql_query($query, $dbi['dbc']);
206 if ($retval == false)
207 echo "Delete on $dbi[page_table] failed: ", msql_error(), "<br>\n";
209 // insert the new lines
210 reset($pagehash["content"]);
212 for ($x = 0; $x < count($pagehash["content"]); $x++) {
213 $line = addslashes($pagehash["content"][$x]);
214 $query = "INSERT INTO $dbi[page_table] " .
215 "(pagename, lineno, line) " .
216 "VALUES('$pagename', $x, '$line')";
217 // echo "Page line insert query: $query<br>\n";
218 $retval = msql_query($query, $dbi['dbc']);
219 if ($retval == false)
220 echo "Insert into $dbi[page_table] failed: ",
221 msql_error(), "<br>\n";
229 // for archiving pages to a separate table
230 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
231 global $ArchivePageStore;
233 $pagehash = MakeDBHash($pagename, $pagehash);
234 // $pagehash["content"] is now an array of strings
235 // of MSQL_MAX_LINE_LENGTH
237 if (IsInArchive($dbi, $pagename)) {
239 $PAIRS = "author='$pagehash[author]'," .
240 "created=$pagehash[created]," .
241 "flags=$pagehash[flags]," .
242 "lastmodified=$pagehash[lastmodified]," .
243 "pagename='$pagehash[pagename]'," .
244 "refs='$pagehash[refs]'," .
245 "version=$pagehash[version]";
247 $query = "UPDATE $ArchivePageStore[table] SET $PAIRS WHERE pagename='$pagename'";
251 // build up the column names and values for the query
253 $COLUMNS = "author, created, flags, lastmodified, " .
254 "pagename, refs, version";
256 $VALUES = "'$pagehash[author]', " .
257 "$pagehash[created], $pagehash[flags], " .
258 "$pagehash[lastmodified], '$pagehash[pagename]', " .
259 "'$pagehash[refs]', $pagehash[version]";
262 $query = "INSERT INTO archive ($COLUMNS) VALUES($VALUES)";
265 // echo "<p>Query: $query<p>\n";
267 // first, insert the metadata
268 $retval = msql_query($query, $dbi['dbc']);
269 if ($retval == false)
270 echo "Insert/update failed: ", msql_error(), "<br>\n";
273 // second, insert the page data
274 // remove old data from page_table
275 $query = "delete from $ArchivePageStore[page_table] where pagename='$pagename'";
276 // echo "Delete query: $query<br>\n";
277 $retval = msql_query($query, $dbi['dbc']);
278 if ($retval == false)
279 echo "Delete on $ArchivePageStore[page_table] failed: ", msql_error(), "<br>\n";
281 // insert the new lines
282 reset($pagehash["content"]);
284 for ($x = 0; $x < count($pagehash["content"]); $x++) {
285 $line = addslashes($pagehash["content"][$x]);
286 $query = "INSERT INTO $ArchivePageStore[page_table] " .
287 "(pagename, lineno, line) " .
288 "VALUES('$pagename', $x, '$line')";
289 // echo "Page line insert query: $query<br>\n";
290 $retval = msql_query($query, $dbi['dbc']);
291 if ($retval == false)
292 echo "Insert into $ArchivePageStore[page_table] failed: ",
293 msql_error(), "<br>\n";
301 function IsWikiPage($dbi, $pagename) {
302 $pagename = addslashes($pagename);
303 $query = "select pagename from wiki where pagename='$pagename'";
304 // echo "Query: $query<br>\n";
305 if ($res = msql_query($query, $dbi['dbc'])) {
306 return(msql_affected_rows($res));
311 function IsInArchive($dbi, $pagename) {
312 $pagename = addslashes($pagename);
313 $query = "select pagename from archive where pagename='$pagename'";
314 // echo "Query: $query<br>\n";
315 if ($res = msql_query($query, $dbi['dbc'])) {
316 return(msql_affected_rows($res));
322 // setup for title-search
323 function InitTitleSearch($dbi, $search) {
324 $search = addslashes($search);
325 $query = "select pagename from $dbi[table] " .
326 "where pagename clike '%$search%' order by pagename";
327 $res = msql_query($query, $dbi['dbc']);
333 // iterating through database
334 function TitleSearchNextMatch($dbi, $res) {
335 if($o = msql_fetch_object($res)) {
344 // setup for full-text search
345 function InitFullSearch($dbi, $search) {
346 // select unique page names from wikipages, and then
347 // retrieve all pages that come back.
348 $search = addslashes($search);
349 $query = "select distinct pagename from $dbi[page_table] " .
350 "where line clike '%$search%' " .
352 $res = msql_query($query, $dbi['dbc']);
357 // iterating through database
358 function FullSearchNextMatch($dbi, $res) {
359 global $WikiPageStore;
360 if ($row = msql_fetch_row($res)) {
361 return RetrievePage($dbi, $row[0], $WikiPageStore);
367 ////////////////////////
368 // new database features
371 function IncreaseHitCount($dbi, $pagename) {
373 $query = "select hits from hitcount where pagename='$pagename'";
374 $res = msql_query($query, $dbi['dbc']);
375 if (msql_num_rows($res)) {
376 $hits = msql_result($res, 0, 'hits');
378 $query = "update hitcount set hits=$hits where pagename='$pagename'";
379 $res = msql_query($query, $dbi['dbc']);
382 $query = "insert into hitcount (pagename, hits) " .
383 "values ('$pagename', 1)";
384 $res = msql_query($query, $dbi['dbc']);
390 function GetHitCount($dbi, $pagename) {
392 $query = "select hits from hitcount where pagename='$pagename'";
393 $res = msql_query($query, $dbi['dbc']);
394 if (msql_num_rows($res)) {
395 $hits = msql_result($res, 0, 'hits');
405 function InitMostPopular($dbi, $limit) {
407 $query = "select * from hitcount " .
408 "order by hits desc, pagename limit $limit";
410 $res = msql_query($query, $dbi['dbc']);
415 function MostPopularNextMatch($dbi, $res) {
417 if ($hits = msql_fetch_array($res)) {
424 function GetAllWikiPageNames($dbi_) {
425 $res = msql_query("select pagename from wiki", $dbi['dbc']);
426 $rows = msql_num_rows($res);
427 for ($i = 0; $i < $rows; $i++) {
428 $pages[$i] = msql_result($res, $i, 'pagename');