1 <?php rcs_id('$Id: msql.php,v 1.6.2.6 2001-11-16 02:42:27 wainstead Exp $');
6 MakeDBHash($pagename, $pagehash)
9 RetrievePage($dbi, $pagename, $pagestore)
10 InsertPage($dbi, $pagename, $pagehash)
11 SaveCopyToArchive($dbi, $pagename, $pagehash)
12 IsWikiPage($dbi, $pagename)
13 InitTitleSearch($dbi, $search)
14 TitleSearchNextMatch($dbi, &$pos)
15 InitFullSearch($dbi, $search)
16 FullSearchNextMatch($dbi, &$pos)
17 MakeBackLinkSearchRegexp($pagename)
18 InitBackLinkSearch($dbi, $pagename)
19 BackLinkSearchNextMatch($dbi, &$pos)
20 GetAllWikiPageNames($dbi)
24 // open a database and return the handle
25 // ignores MAX_DBM_ATTEMPTS
27 function OpenDataBase($dbinfo) {
30 if (! ($dbc = msql_connect())) {
31 $msg = gettext ("Cannot establish connection to database, giving up.");
33 $msg .= sprintf(gettext ("Error message: %s"), msql_error());
36 if (!msql_select_db($msql_db, $dbc)) {
37 $msg = gettext ("Cannot open database %s, giving up.");
39 $msg .= sprintf(gettext ("Error message: %s"), msql_error());
44 $dbi['table'] = $dbinfo['table']; // page metadata
45 $dbi['page_table'] = $dbinfo['page_table']; // page content
50 function CloseDataBase($dbi) {
51 // I found msql_pconnect unstable so we go the slow route.
52 return msql_close($dbi['dbc']);
56 // This should receive the full text of the page in one string
57 // It will break the page text into an array of strings
58 // of length MSQL_MAX_LINE_LENGTH which should match the length
59 // of the columns wikipages.LINE, archivepages.LINE in schema.minisql
61 function msqlDecomposeString($string) {
64 // initialize the array to satisfy E_NOTICE
65 for ($i = 0; $i < MSQL_MAX_LINE_LENGTH; $i++) {
70 // zero, one, infinity
71 // account for the small case
72 if (strlen($string) < MSQL_MAX_LINE_LENGTH) {
73 $ret_arr[$el] = $string;
78 $line = $string2 = "";
80 // split on single spaces
81 $words = preg_split("/ /", $string);
82 $num_words = count($words);
85 $ret_arr[0] = $words[0];
88 // for all words, build up lines < MSQL_MAX_LINE_LENGTH in $ret_arr
89 for ($x = 2; $x < $num_words; $x++) {
90 $length = strlen($line) + strlen($words[$x])
91 + strlen($ret_arr[$el]) + 1;
93 if ($length < MSQL_MAX_LINE_LENGTH) {
94 $line .= " " . $words[$x];
96 // put this line in the return array, reset, continue
97 $ret_arr[$el++] .= $line;
98 $line = " $words[$x]"; // reset
101 $ret_arr[$el] = $line;
106 // Take form data and prepare it for the db
107 function MakeDBHash($pagename, $pagehash)
109 $pagehash["pagename"] = addslashes($pagename);
110 if (!isset($pagehash["flags"]))
111 $pagehash["flags"] = 0;
112 if (!isset($pagehash["content"])) {
113 $pagehash["content"] = array();
115 $pagehash["content"] = implode("\n", $pagehash["content"]);
116 $pagehash["content"] = msqlDecomposeString($pagehash["content"]);
118 $pagehash["author"] = addslashes($pagehash["author"]);
119 if (empty($pagehash["refs"])) {
120 $pagehash["refs"] = "";
122 $pagehash["refs"] = serialize($pagehash["refs"]);
129 // Take db data and prepare it for display
130 function MakePageHash($dbhash)
132 // unserialize/explode content
133 $dbhash['refs'] = unserialize($dbhash['refs']);
138 // Return hash of page + attributes or default
139 function RetrievePage($dbi, $pagename, $pagestore) {
140 $pagename = addslashes($pagename);
141 $table = $pagestore['table'];
142 $pagetable = $pagestore['page_table'];
144 $query = "select * from $table where pagename='$pagename'";
145 // echo "<p>query: $query<p>";
146 $res = msql_query($query, $dbi['dbc']);
147 if (msql_num_rows($res)) {
148 $dbhash = msql_fetch_array($res);
150 $query = "select lineno,line from $pagetable " .
151 "where pagename='$pagename' " .
155 if ($res = msql_query($query, $dbi['dbc'])) {
156 $dbhash["content"] = array();
157 while ($row = msql_fetch_array($res)) {
158 $msql_content .= $row["line"];
160 $dbhash["content"] = explode("\n", $msql_content);
163 return MakePageHash($dbhash);
169 // Either insert or replace a key/value (a page)
170 function InsertPage($dbi, $pagename, $pagehash) {
172 $pagehash = MakeDBHash($pagename, $pagehash);
173 // $pagehash["content"] is now an array of strings
174 // of MSQL_MAX_LINE_LENGTH
176 // record the time of modification
177 $pagehash["lastmodified"] = time();
179 if (IsWikiPage($dbi, $pagename)) {
181 $PAIRS = "author='$pagehash[author]'," .
182 "created=$pagehash[created]," .
183 "flags=$pagehash[flags]," .
184 "lastmodified=$pagehash[lastmodified]," .
185 "pagename='$pagehash[pagename]'," .
186 "refs='$pagehash[refs]'," .
187 "version=$pagehash[version]";
189 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
193 // build up the column names and values for the query
195 $COLUMNS = "author, created, flags, lastmodified, " .
196 "pagename, refs, version";
198 $VALUES = "'$pagehash[author]', " .
199 "$pagehash[created], $pagehash[flags], " .
200 "$pagehash[lastmodified], '$pagehash[pagename]', " .
201 "'$pagehash[refs]', $pagehash[version]";
204 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
207 // echo "<p>Query: $query<p>\n";
209 // first, insert the metadata
210 $retval = msql_query($query, $dbi['dbc']);
211 if ($retval == false) {
212 printf(gettext ("Insert/update failed: %s"), msql_error());
217 // second, insert the page data
218 // remove old data from page_table
219 $query = "delete from $dbi[page_table] where pagename='$pagename'";
220 // echo "Delete query: $query<br>\n";
221 $retval = msql_query($query, $dbi['dbc']);
222 if ($retval == false) {
223 printf(gettext ("Delete on %s failed: %s"), $dbi[page_table],
228 // insert the new lines
229 reset($pagehash["content"]);
231 for ($x = 0; $x < count($pagehash["content"]); $x++) {
232 $line = addslashes($pagehash["content"][$x]);
233 $esc_pagename = addslashes($pagename);
234 $query = "INSERT INTO $dbi[page_table] " .
235 "(pagename, lineno, line) " .
236 "VALUES('$esc_pagename', $x, '$line')";
237 echo "Page line insert query: $query<br>\n";
238 $retval = msql_query($query, $dbi['dbc']);
239 if ($retval == false) {
240 printf(gettext ("Insert into %s failed: %s"), $dbi[page_table],
248 // for archiving pages to a separate table
249 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
250 global $ArchivePageStore;
252 $pagehash = MakeDBHash($pagename, $pagehash);
253 // $pagehash["content"] is now an array of strings
254 // of MSQL_MAX_LINE_LENGTH
256 if (IsInArchive($dbi, $pagename)) {
258 $PAIRS = "author='$pagehash[author]'," .
259 "created=$pagehash[created]," .
260 "flags=$pagehash[flags]," .
261 "lastmodified=$pagehash[lastmodified]," .
262 "pagename='$pagehash[pagename]'," .
263 "refs='$pagehash[refs]'," .
264 "version=$pagehash[version]";
266 $query = "UPDATE $ArchivePageStore[table] SET $PAIRS WHERE pagename='$pagename'";
270 // build up the column names and values for the query
272 $COLUMNS = "author, created, flags, lastmodified, " .
273 "pagename, refs, version";
275 $VALUES = "'$pagehash[author]', " .
276 "$pagehash[created], $pagehash[flags], " .
277 "$pagehash[lastmodified], '$pagehash[pagename]', " .
278 "'$pagehash[refs]', $pagehash[version]";
281 $query = "INSERT INTO archive ($COLUMNS) VALUES($VALUES)";
284 // echo "<p>Query: $query<p>\n";
286 // first, insert the metadata
287 $retval = msql_query($query, $dbi['dbc']);
288 if ($retval == false) {
289 printf(gettext ("Insert/update failed: %s"), msql_error());
293 // second, insert the page data
294 // remove old data from page_table
295 $query = "delete from $ArchivePageStore[page_table] where pagename='$pagename'";
296 // echo "Delete query: $query<br>\n";
297 $retval = msql_query($query, $dbi['dbc']);
298 if ($retval == false) {
299 printf(gettext ("Delete on %s failed: %s"),
300 $ArchivePageStore[page_table], msql_error());
304 // insert the new lines
305 reset($pagehash["content"]);
307 for ($x = 0; $x < count($pagehash["content"]); $x++) {
308 $line = addslashes($pagehash["content"][$x]);
309 $query = "INSERT INTO $ArchivePageStore[page_table] " .
310 "(pagename, lineno, line) " .
311 "VALUES('$pagename', $x, '$line')";
312 // echo "Page line insert query: $query<br>\n";
313 $retval = msql_query($query, $dbi['dbc']);
314 if ($retval == false) {
315 printf(gettext ("Insert into %s failed: %s"),
316 $ArchivePageStore[page_table], msql_error());
325 function IsWikiPage($dbi, $pagename) {
326 $pagename = addslashes($pagename);
327 $query = "select pagename from wiki where pagename='$pagename'";
328 // echo "Query: $query<br>\n";
329 if ($res = msql_query($query, $dbi['dbc'])) {
330 return(msql_affected_rows($res));
335 function IsInArchive($dbi, $pagename) {
336 $pagename = addslashes($pagename);
337 $query = "select pagename from archive where pagename='$pagename'";
338 // echo "Query: $query<br>\n";
339 if ($res = msql_query($query, $dbi['dbc'])) {
340 return(msql_affected_rows($res));
346 // setup for title-search
347 function InitTitleSearch($dbi, $search) {
348 $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
349 $search = addslashes($search);
350 $query = "select pagename from $dbi[table] " .
351 "where pagename clike '%$search%' order by pagename";
352 $res = msql_query($query, $dbi['dbc']);
358 // iterating through database
359 function TitleSearchNextMatch($dbi, $res) {
360 if($o = msql_fetch_object($res)) {
369 // setup for full-text search
370 function InitFullSearch($dbi, $search) {
371 // select unique page names from wikipages, and then
372 // retrieve all pages that come back.
373 $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
374 $search = addslashes($search);
375 $query = "select distinct pagename from $dbi[page_table] " .
376 "where line clike '%$search%' " .
378 $res = msql_query($query, $dbi['dbc']);
383 // iterating through database
384 function FullSearchNextMatch($dbi, $res) {
385 global $WikiPageStore;
386 if ($row = msql_fetch_row($res)) {
387 return RetrievePage($dbi, $row[0], $WikiPageStore);
393 ////////////////////////
394 // new database features
396 // Compute PCRE suitable for searching for links to the given page.
397 function MakeBackLinkSearchRegexp($pagename) {
398 global $WikiNameRegexp;
400 // Note that in (at least some) PHP 3.x's, preg_quote only takes
401 // (at most) one argument. Also it doesn't quote '/'s.
402 // It does quote '='s, so we'll use that for the delimeter.
403 $quoted_pagename = preg_quote($pagename);
404 if (preg_match("/^$WikiNameRegexp\$/", $pagename)) {
405 # FIXME: This may need modification for non-standard (non-english) $WikiNameRegexp.
406 return "=(?<![A-Za-z0-9!])$quoted_pagename(?![A-Za-z0-9])=";
409 // Note from author: Sorry. :-/
411 . '(?<!\[)\[(?!\[)' // Single, isolated '['
412 . '([^]|]*\|)?' // Optional stuff followed by '|'
413 . '\s*' // Optional space
414 . $quoted_pagename // Pagename
415 . '\s*\]=' ); // Optional space, followed by ']'
416 // FIXME: the above regexp is still not quite right.
417 // Consider the text: " [ [ test page ]". This is a link to a page
418 // named '[ test page'. The above regexp will recognize this
419 // as a link either to '[ test page' (good) or to 'test page' (wrong).
423 // setup for back-link search
424 function InitBackLinkSearch($dbi, $pagename) {
425 global $WikiLinksStore;
427 $topage = addslashes($pagename);
430 // FIXME: this is buggy. If a [multiword link] is split accross
431 // multiple lines int the page_table, we wont find it.
432 // (Probably the best fix is to implement the link table, and use it.)
433 $res['regexp'] = MakeBackLinkSearchRegexp($pagename);
434 $query = "SELECT pagename, line FROM $dbi[page_table]"
435 . " WHERE line LIKE '%$topage%'"
436 . " ORDER BY pagename";
438 //echo "<p>$query<p>\n";
439 $res['res'] = msql_query($query, $dbi["dbc"]);
444 // build an array of the results.
445 while ($hash = msql_fetch_array($res[res]) ) {
446 if ($arr[$count -1 ] == $hash[pagename])
448 $arr[$count] = $hash[pagename];
460 // iterating through database
461 function BackLinkSearchNextMatch($dbi, &$res) {
463 if ($res[count] > count($res[arr]))
466 $retval = $res[arr][$res[count]];
473 if ( ($o = msql_fetch_object($res['res'])) == FALSE ) {
474 echo "returning zero<p>\n";
475 echo "it's '$o' <p>\n";
478 if ( $res['lastpage'] == $o->pagename )
480 if ( ! preg_match($res['regexp'], $a->line) )
482 $res['lastpage'] = $o->pagename;
488 function IncreaseHitCount($dbi, $pagename) {
490 $qpagename = addslashes($pagename);
491 $query = "select hits from hitcount where pagename='$qpagename'";
492 $res = msql_query($query, $dbi['dbc']);
493 if (msql_num_rows($res)) {
494 $hits = msql_result($res, 0, 'hits');
496 $query = "update hitcount set hits=$hits where pagename='$qpagename'";
497 $res = msql_query($query, $dbi['dbc']);
500 $query = "insert into hitcount (pagename, hits) " .
501 "values ('$qpagename', 1)";
502 $res = msql_query($query, $dbi['dbc']);
508 function GetHitCount($dbi, $pagename) {
510 $qpagename = addslashes($pagename);
511 $query = "select hits from hitcount where pagename='$qpagename'";
512 $res = msql_query($query, $dbi['dbc']);
513 if (msql_num_rows($res)) {
514 $hits = msql_result($res, 0, 'hits');
524 function InitMostPopular($dbi, $limit) {
526 $query = "select * from hitcount " .
527 "order by hits desc, pagename limit $limit";
529 $res = msql_query($query, $dbi['dbc']);
534 function MostPopularNextMatch($dbi, $res) {
536 if ($hits = msql_fetch_array($res)) {
543 function GetAllWikiPageNames($dbi_) {
544 $res = msql_query("select pagename from wiki", $dbi['dbc']);
545 $rows = msql_num_rows($res);
546 for ($i = 0; $i < $rows; $i++) {
547 $pages[$i] = msql_result($res, $i, 'pagename');
552 ////////////////////////////////////////
553 // functionality for the wikilinks table
555 // takes a page name, returns array of scored incoming and outgoing links
557 /* Not implemented yet. The code below was copied from mysql.php...
559 function GetWikiPageLinks($dbi, $pagename) {
561 $pagename = addslashes($pagename);
562 $res = msql_query("select wikilinks.topage, wikiscore.score from wikilinks, wikiscore where wikilinks.topage=wikiscore.pagename and wikilinks.frompage='$pagename' order by score desc, topage", $dbi['dbc']);
564 $rows = msql_num_rows($res);
565 for ($i = 0; $i < $rows; $i++) {
566 $out = msql_fetch_array($res);
567 $links['out'][] = array($out['topage'], $out['score']);
570 $res = msql_query("select wikilinks.frompage, wikiscore.score from wikilinks, wikiscore where wikilinks.frompage=wikiscore.pagename and wikilinks.topage='$pagename' order by score desc, frompage", $dbi['dbc']);
571 $rows = msql_num_rows($res);
572 for ($i = 0; $i < $rows; $i++) {
573 $out = msql_fetch_array($res);
574 $links['in'][] = array($out['frompage'], $out['score']);
577 $res = msql_query("select distinct hitcount.pagename, hitcount.hits from wikilinks, hitcount where (wikilinks.frompage=hitcounts.pagename and wikilinks.topage='$pagename') or (wikilinks.topage=pagename and wikilinks.frompage='$pagename') order by hitcount.hits desc, wikilinks.pagename", $dbi['dbc']);
578 $rows = msql_num_rows($res);
579 for ($i = 0; $i < $rows; $i++) {
580 $out = msql_fetch_array($res);
581 $links['popular'][] = array($out['pagename'], $out['hits']);
588 // takes page name, list of links it contains
589 // the $linklist is an array where the keys are the page names
590 function SetWikiPageLinks($dbi, $pagename, $linklist) {
591 $frompage = addslashes($pagename);
593 // first delete the old list of links
594 msql_query("delete from wikilinks where frompage='$frompage'",
597 // the page may not have links, return if not
598 if (! count($linklist))
600 // now insert the new list of links
601 while (list($topage, $count) = each($linklist)) {
602 $topage = addslashes($topage);
603 if($topage != $frompage) {
604 msql_query("insert into wikilinks (frompage, topage) " .
605 "values ('$frompage', '$topage')", $dbi["dbc"]);
609 msql_query("delete from wikiscore", $dbi["dbc"]);
610 msql_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"]);