1 <!-- $Id: wiki_msql.php3,v 1.4 2000-06-26 03:55:27 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 // Take form data and prepare it for the db
52 function MakeDBHash($pagename, $pagehash)
54 $pagehash["pagename"] = addslashes($pagename);
55 if (!isset($pagehash["flags"]))
56 $pagehash["flags"] = 0;
57 if (!isset($pagehash["content"]))
58 $pagehash["content"] = array();
59 $pagehash["author"] = addslashes($pagehash["author"]);
60 $pagehash["refs"] = serialize($pagehash["refs"]);
66 // Take db data and prepare it for display
67 function MakePageHash($dbhash)
69 // unserialize/explode content
70 $dbhash['refs'] = unserialize($dbhash['refs']);
75 // Return hash of page + attributes or default
76 function RetrievePage($dbi, $pagename) {
77 $pagename = addslashes($pagename);
79 $query = "select * from $dbi[table] where pagename='$pagename'";
81 if ($res = msql_query($query, $dbi['dbc'])) {
82 $dbhash = msql_fetch_array($res);
84 $query = "select lineno,line from $dbi[page_table] " .
85 "where pagename='$pagename' " .
88 if ($res = msql_query($query, $dbi[dbc])) {
89 $dbhash["content"] = array();
90 while ($row = msql_fetch_array($res)) {
91 $dbhash["content"][ $row["lineno"] ] = $row["line"];
95 return MakePageHash($dbhash);
101 // Either insert or replace a key/value (a page)
102 function InsertPage($dbi, $pagename, $pagehash)
104 $pagehash = MakeDBHash($pagename, $pagehash);
106 // temporary hack until the time stuff is brought up to date
107 $pagehash["created"] = time();
108 $pagehash["lastmodified"] = time();
110 if (IsWikiPage($dbi, $pagename)) {
112 $PAIRS = "author='$pagehash[author]'," .
113 "created=$pagehash[created]," .
114 "flags=$pagehash[flags]," .
115 "lastmodified=$pagehash[lastmodified]," .
116 "pagename='$pagehash[pagename]'," .
117 "refs='$pagehash[refs]'," .
118 "version=$pagehash[version]";
120 $query = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
124 // build up the column names and values for the query
126 $COLUMNS = "author, created, flags, lastmodified, " .
127 "pagename, refs, version";
129 $VALUES = "'$pagehash[author]', " .
130 "$pagehash[created], $pagehash[flags], " .
131 "$pagehash[lastmodified], '$pagehash[pagename]', " .
132 "'$pagehash[refs]', $pagehash[version]";
135 $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
138 // echo "<p>Query: $query<p>\n";
140 // first, insert the metadata
141 $retval = msql_query($query, $dbi['dbc']);
142 if ($retval == false)
143 echo "Insert/update failed: ", msql_error(), "<br>\n";
146 // second, insert the page data
147 // remove old data from page_table
148 $query = "delete from $dbi[page_table] where pagename='$pagename'";
149 echo "Delete query: $query<br>\n";
150 $retval = msql_query($query, $dbi['dbc']);
151 if ($retval == false)
152 echo "Delete on $dbi[page_table] failed: ", msql_error(), "<br>\n";
154 // insert the new lines
155 reset($pagehash["content"]);
160 for ($x = 0; $x < count($pagehash["content"]); $x++) {
162 // manage line length here, lines should not exceed the
163 // length MSQL_MAX_LINE_LENGTH or something
165 if (strlen($pagehash["content"][$x]) > MSQL_MAX_LINE_LENGTH) {
166 $length = strlen($pagehash["content"][$x]);
167 echo "Must break up line ($length): " . $pagehash["content"][$x] ."<br>\n";
168 // can I cheat and use preg_split to break the line up?
169 // match this line with: /(.{1,127})+/
170 // in fact, split it on a zero-width metachar every 127th position
171 // take the returned array and add elements to $tmparray
173 $tmparray[$y] = $pagehash["content"][$x];
179 for ($x = 0; $x < count($tmparray); $x ++) {
180 $line = addslashes($tmparray[$x]);
181 $query = "INSERT INTO $dbi[page_table] " .
182 "(pagename, lineno, line) " .
183 "VALUES('$pagename', $x, '$line')";
184 echo "Page line insert query: $query<br>\n";
185 $retval = msql_query($query, $dbi['dbc']);
186 if ($retval == false)
187 echo "Insert into $dbi[page_table] failed: ", msql_error(), "<br>\n";;
190 //echo "<H1>inserted $x lines for $pagename</H1>\n";
195 function IsWikiPage($dbi, $pagename) {
196 $pagename = addslashes($pagename);
197 $query = "select pagename from $dbi[table] where pagename='$pagename'";
198 // echo "Query: $query<br>\n";
199 if ($res = msql_query($query, $dbi['dbc'])) {
200 return(msql_affected_rows($res));
205 // setup for title-search
206 function InitTitleSearch($dbi, $search) {
207 $search = addslashes($search);
208 $query = "select pagename from $dbi[table] " .
209 "where pagename clike '%$search%' order by pagename";
210 $res = msql_query($query, $dbi["dbc"]);
216 // iterating through database
217 function TitleSearchNextMatch($dbi, $res) {
218 if($o = msql_fetch_object($res)) {
227 // setup for full-text search
228 function InitFullSearch($dbi, $search) {
229 $search = addslashes($search);
230 $query = "select * from $dbi[table] where searchterms clike '%$search%'";
231 $res = msql_query($query, $dbi["dbc"]);
236 // iterating through database
237 function FullSearchNextMatch($dbi, $res) {
238 if($hash = msql_fetch_array($res)) {
239 return MakePageHash($hash);