1 <?php rcs_id('$Id: msql.php,v 1.6.2.5 2001-11-16 01:22: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 $query = "INSERT INTO $dbi[page_table] " .
234 "(pagename, lineno, line) " .
235 "VALUES('$pagename', $x, '$line')";
236 // echo "Page line insert query: $query<br>\n";
237 $retval = msql_query($query, $dbi['dbc']);
238 if ($retval == false) {
239 printf(gettext ("Insert into %s failed: %s"), $dbi[page_table],
247 // for archiving pages to a separate table
248 function SaveCopyToArchive($dbi, $pagename, $pagehash) {
249 global $ArchivePageStore;
251 $pagehash = MakeDBHash($pagename, $pagehash);
252 // $pagehash["content"] is now an array of strings
253 // of MSQL_MAX_LINE_LENGTH
255 if (IsInArchive($dbi, $pagename)) {
257 $PAIRS = "author='$pagehash[author]'," .
258 "created=$pagehash[created]," .
259 "flags=$pagehash[flags]," .
260 "lastmodified=$pagehash[lastmodified]," .
261 "pagename='$pagehash[pagename]'," .
262 "refs='$pagehash[refs]'," .
263 "version=$pagehash[version]";
265 $query = "UPDATE $ArchivePageStore[table] SET $PAIRS WHERE pagename='$pagename'";
269 // build up the column names and values for the query
271 $COLUMNS = "author, created, flags, lastmodified, " .
272 "pagename, refs, version";
274 $VALUES = "'$pagehash[author]', " .
275 "$pagehash[created], $pagehash[flags], " .
276 "$pagehash[lastmodified], '$pagehash[pagename]', " .
277 "'$pagehash[refs]', $pagehash[version]";
280 $query = "INSERT INTO archive ($COLUMNS) VALUES($VALUES)";
283 // echo "<p>Query: $query<p>\n";
285 // first, insert the metadata
286 $retval = msql_query($query, $dbi['dbc']);
287 if ($retval == false) {
288 printf(gettext ("Insert/update failed: %s"), msql_error());
292 // second, insert the page data
293 // remove old data from page_table
294 $query = "delete from $ArchivePageStore[page_table] where pagename='$pagename'";
295 // echo "Delete query: $query<br>\n";
296 $retval = msql_query($query, $dbi['dbc']);
297 if ($retval == false) {
298 printf(gettext ("Delete on %s failed: %s"),
299 $ArchivePageStore[page_table], msql_error());
303 // insert the new lines
304 reset($pagehash["content"]);
306 for ($x = 0; $x < count($pagehash["content"]); $x++) {
307 $line = addslashes($pagehash["content"][$x]);
308 $query = "INSERT INTO $ArchivePageStore[page_table] " .
309 "(pagename, lineno, line) " .
310 "VALUES('$pagename', $x, '$line')";
311 // echo "Page line insert query: $query<br>\n";
312 $retval = msql_query($query, $dbi['dbc']);
313 if ($retval == false) {
314 printf(gettext ("Insert into %s failed: %s"),
315 $ArchivePageStore[page_table], msql_error());
324 function IsWikiPage($dbi, $pagename) {
325 $pagename = addslashes($pagename);
326 $query = "select pagename from wiki where pagename='$pagename'";
327 // echo "Query: $query<br>\n";
328 if ($res = msql_query($query, $dbi['dbc'])) {
329 return(msql_affected_rows($res));
334 function IsInArchive($dbi, $pagename) {
335 $pagename = addslashes($pagename);
336 $query = "select pagename from archive where pagename='$pagename'";
337 // echo "Query: $query<br>\n";
338 if ($res = msql_query($query, $dbi['dbc'])) {
339 return(msql_affected_rows($res));
345 // setup for title-search
346 function InitTitleSearch($dbi, $search) {
347 $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
348 $search = addslashes($search);
349 $query = "select pagename from $dbi[table] " .
350 "where pagename clike '%$search%' order by pagename";
351 $res = msql_query($query, $dbi['dbc']);
357 // iterating through database
358 function TitleSearchNextMatch($dbi, $res) {
359 if($o = msql_fetch_object($res)) {
368 // setup for full-text search
369 function InitFullSearch($dbi, $search) {
370 // select unique page names from wikipages, and then
371 // retrieve all pages that come back.
372 $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
373 $search = addslashes($search);
374 $query = "select distinct pagename from $dbi[page_table] " .
375 "where line clike '%$search%' " .
377 $res = msql_query($query, $dbi['dbc']);
382 // iterating through database
383 function FullSearchNextMatch($dbi, $res) {
384 global $WikiPageStore;
385 if ($row = msql_fetch_row($res)) {
386 return RetrievePage($dbi, $row[0], $WikiPageStore);
392 ////////////////////////
393 // new database features
395 // Compute PCRE suitable for searching for links to the given page.
396 function MakeBackLinkSearchRegexp($pagename) {
397 global $WikiNameRegexp;
399 // Note that in (at least some) PHP 3.x's, preg_quote only takes
400 // (at most) one argument. Also it doesn't quote '/'s.
401 // It does quote '='s, so we'll use that for the delimeter.
402 $quoted_pagename = preg_quote($pagename);
403 if (preg_match("/^$WikiNameRegexp\$/", $pagename)) {
404 # FIXME: This may need modification for non-standard (non-english) $WikiNameRegexp.
405 return "=(?<![A-Za-z0-9!])$quoted_pagename(?![A-Za-z0-9])=";
408 // Note from author: Sorry. :-/
410 . '(?<!\[)\[(?!\[)' // Single, isolated '['
411 . '([^]|]*\|)?' // Optional stuff followed by '|'
412 . '\s*' // Optional space
413 . $quoted_pagename // Pagename
414 . '\s*\]=' ); // Optional space, followed by ']'
415 // FIXME: the above regexp is still not quite right.
416 // Consider the text: " [ [ test page ]". This is a link to a page
417 // named '[ test page'. The above regexp will recognize this
418 // as a link either to '[ test page' (good) or to 'test page' (wrong).
422 // setup for back-link search
423 function InitBackLinkSearch($dbi, $pagename) {
424 global $WikiLinksStore;
426 $topage = addslashes($pagename);
429 // FIXME: this is buggy. If a [multiword link] is split accross
430 // multiple lines int the page_table, we wont find it.
431 // (Probably the best fix is to implement the link table, and use it.)
432 $res['regexp'] = MakeBackLinkSearchRegexp($pagename);
433 $query = "SELECT pagename, line FROM $dbi[page_table]"
434 . " WHERE line LIKE '%$topage%'"
435 . " ORDER BY pagename";
437 //echo "<p>$query<p>\n";
438 $res['res'] = msql_query($query, $dbi["dbc"]);
443 // build an array of the results.
444 while ($hash = msql_fetch_array($res[res]) ) {
445 if ($arr[$count -1 ] == $hash[pagename])
447 $arr[$count] = $hash[pagename];
459 // iterating through database
460 function BackLinkSearchNextMatch($dbi, &$res) {
462 if ($res[count] > count($res[arr]))
465 $retval = $res[arr][$res[count]];
472 if ( ($o = msql_fetch_object($res['res'])) == FALSE ) {
473 echo "returning zero<p>\n";
474 echo "it's '$o' <p>\n";
477 if ( $res['lastpage'] == $o->pagename )
479 if ( ! preg_match($res['regexp'], $a->line) )
481 $res['lastpage'] = $o->pagename;
487 function IncreaseHitCount($dbi, $pagename) {
489 $qpagename = addslashes($pagename);
490 $query = "select hits from hitcount where pagename='$qpagename'";
491 $res = msql_query($query, $dbi['dbc']);
492 if (msql_num_rows($res)) {
493 $hits = msql_result($res, 0, 'hits');
495 $query = "update hitcount set hits=$hits where pagename='$qpagename'";
496 $res = msql_query($query, $dbi['dbc']);
499 $query = "insert into hitcount (pagename, hits) " .
500 "values ('$qpagename', 1)";
501 $res = msql_query($query, $dbi['dbc']);
507 function GetHitCount($dbi, $pagename) {
509 $qpagename = addslashes($pagename);
510 $query = "select hits from hitcount where pagename='$qpagename'";
511 $res = msql_query($query, $dbi['dbc']);
512 if (msql_num_rows($res)) {
513 $hits = msql_result($res, 0, 'hits');
523 function InitMostPopular($dbi, $limit) {
525 $query = "select * from hitcount " .
526 "order by hits desc, pagename limit $limit";
528 $res = msql_query($query, $dbi['dbc']);
533 function MostPopularNextMatch($dbi, $res) {
535 if ($hits = msql_fetch_array($res)) {
542 function GetAllWikiPageNames($dbi_) {
543 $res = msql_query("select pagename from wiki", $dbi['dbc']);
544 $rows = msql_num_rows($res);
545 for ($i = 0; $i < $rows; $i++) {
546 $pages[$i] = msql_result($res, $i, 'pagename');
551 ////////////////////////////////////////
552 // functionality for the wikilinks table
554 // takes a page name, returns array of scored incoming and outgoing links
556 /* Not implemented yet. The code below was copied from mysql.php...
558 function GetWikiPageLinks($dbi, $pagename) {
560 $pagename = addslashes($pagename);
561 $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']);
563 $rows = msql_num_rows($res);
564 for ($i = 0; $i < $rows; $i++) {
565 $out = msql_fetch_array($res);
566 $links['out'][] = array($out['topage'], $out['score']);
569 $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']);
570 $rows = msql_num_rows($res);
571 for ($i = 0; $i < $rows; $i++) {
572 $out = msql_fetch_array($res);
573 $links['in'][] = array($out['frompage'], $out['score']);
576 $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']);
577 $rows = msql_num_rows($res);
578 for ($i = 0; $i < $rows; $i++) {
579 $out = msql_fetch_array($res);
580 $links['popular'][] = array($out['pagename'], $out['hits']);
587 // takes page name, list of links it contains
588 // the $linklist is an array where the keys are the page names
589 function SetWikiPageLinks($dbi, $pagename, $linklist) {
590 $frompage = addslashes($pagename);
592 // first delete the old list of links
593 msql_query("delete from wikilinks where frompage='$frompage'",
596 // the page may not have links, return if not
597 if (! count($linklist))
599 // now insert the new list of links
600 while (list($topage, $count) = each($linklist)) {
601 $topage = addslashes($topage);
602 if($topage != $frompage) {
603 msql_query("insert into wikilinks (frompage, topage) " .
604 "values ('$frompage', '$topage')", $dbi["dbc"]);
608 msql_query("delete from wikiscore", $dbi["dbc"]);
609 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"]);