]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_msql.php3
Minor updates to the text.
[SourceForge/phpwiki.git] / wiki_msql.php3
1 <!-- $Id: wiki_msql.php3,v 1.7 2000-06-28 22:22:05 wainstead Exp $ -->
2 <?
3
4    /*
5       Database functions:
6       MakePageHash($dbhash)
7       MakeDBHash($pagename, $pagehash)
8       OpenDataBase($dbname)
9       CloseDataBase($dbi)
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)
17    */
18
19
20    // open a database and return the handle
21    // ignores MAX_DBM_ATTEMPTS
22
23    function OpenDataBase($dbinfo) {
24       global $msql_db;
25
26       if (! ($dbc = msql_pconnect())) {
27          echo "Cannot establish connection to database, giving up.";
28          echo "Error message: ", msql_error(), "<br>\n";
29          exit();
30       }
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";
34          exit();
35       }
36
37       $dbi['dbc'] = $dbc;
38       $dbi['table'] = $dbinfo['table'];           // page metadata
39       $dbi['page_table'] = $dbinfo['page_table']; // page content
40       return $dbi;
41    }
42
43
44    function CloseDataBase($dbi) {
45       // NOP function
46       // msql connections are established as persistant
47       // they cannot be closed through msql_close()
48    }
49
50
51    function msqlDecomposeString($string) {
52       $ret_arr = array();
53       $el = 0;
54    
55       // zero, one, infinity
56       // account for the small case
57       if (strlen($string) < MSQL_MAX_LINE_LENGTH) { 
58          $ret_arr[$el] = $string;
59          return $ret_arr;
60       }
61    
62       $words = array();
63       $line = $string2 = "";
64    
65       // split on single spaces
66       $words = preg_split("/ /", $string);
67       $num_words = count($words);
68    
69       reset($words);
70       $ret_arr[0] = $words[0];
71       $line = " $words[1]";
72    
73       // for all words, build up lines < MSQL_MAX_LINE_LENGTH in $ret_arr
74       for ($x = 2; $x < $num_words; $x++) {
75          $length = strlen($line) + strlen($words[$x]) 
76                    + strlen($ret_arr[$el]) + 1;
77
78          if ($length < MSQL_MAX_LINE_LENGTH) {
79             $line .= " " .  $words[$x];
80          } else {
81             // put this line in the return array, reset, continue
82             $ret_arr[$el++] .= $line;
83             $line = " $words[$x]"; // reset     
84          }
85       }
86       $ret_arr[$el] = $line;
87       return $ret_arr;
88    }
89
90
91    // Take form data and prepare it for the db
92    function MakeDBHash($pagename, $pagehash)
93    {
94       $pagehash["pagename"] = addslashes($pagename);
95       if (!isset($pagehash["flags"]))
96          $pagehash["flags"] = 0;
97       if (!isset($pagehash["content"]))
98          $pagehash["content"] = array();
99       $pagehash["author"] = addslashes($pagehash["author"]);
100       $pagehash["refs"] = serialize($pagehash["refs"]);
101
102       return $pagehash;
103    }
104
105
106    // Take db data and prepare it for display
107    function MakePageHash($dbhash)
108    {
109       // unserialize/explode content
110       $dbhash['refs'] = unserialize($dbhash['refs']);
111       return $dbhash;
112    }
113
114
115    // Return hash of page + attributes or default
116    function RetrievePage($dbi, $pagename) {
117       $pagename = addslashes($pagename);
118
119       $query = "select * from $dbi[table] where pagename='$pagename'";
120
121       if ($res = msql_query($query, $dbi['dbc'])) {
122          $dbhash = msql_fetch_array($res);
123
124          $query = "select lineno,line from $dbi[page_table] " .
125                   "where pagename='$pagename' " .
126                   "order by lineno";
127
128          if ($res = msql_query($query, $dbi[dbc])) {
129             $dbhash["content"] = array();
130             while ($row = msql_fetch_array($res)) {
131                $dbhash["content"][ $row["lineno"] ] = $row["line"];
132             }
133          }
134
135          return MakePageHash($dbhash);
136       }
137       return -1;
138    }
139
140
141    // Either insert or replace a key/value (a page)
142    function InsertPage($dbi, $pagename, $pagehash)
143    {
144       $pagehash = MakeDBHash($pagename, $pagehash);
145
146       // temporary hack until the time stuff is brought up to date
147       $pagehash["created"] = time();
148       $pagehash["lastmodified"] = time();
149
150       if (IsWikiPage($dbi, $pagename)) {
151
152          $PAIRS = "author='$pagehash[author]'," .
153                   "created=$pagehash[created]," .
154                   "flags=$pagehash[flags]," .
155                   "lastmodified=$pagehash[lastmodified]," .
156                   "pagename='$pagehash[pagename]'," .
157                   "refs='$pagehash[refs]'," .
158                   "version=$pagehash[version]";
159
160          $query  = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
161
162       } else {
163          // do an insert
164          // build up the column names and values for the query
165
166          $COLUMNS = "author, created, flags, lastmodified, " .
167                     "pagename, refs, version";
168
169          $VALUES =  "'$pagehash[author]', " .
170                     "$pagehash[created], $pagehash[flags], " .
171                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
172                     "'$pagehash[refs]', $pagehash[version]";
173
174
175          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
176       }
177
178 //      echo "<p>Query: $query<p>\n";
179
180       // first, insert the metadata
181       $retval = msql_query($query, $dbi['dbc']);
182       if ($retval == false) 
183          echo "Insert/update failed: ", msql_error(), "<br>\n";
184
185
186       // second, insert the page data
187       // remove old data from page_table
188       $query = "delete from $dbi[page_table] where pagename='$pagename'";
189       echo "Delete query: $query<br>\n";
190       $retval = msql_query($query, $dbi['dbc']);
191       if ($retval == false) 
192          echo "Delete on $dbi[page_table] failed: ", msql_error(), "<br>\n";
193
194       // insert the new lines
195       reset($pagehash["content"]);
196
197       $tmparray = array();
198       $y = 0;
199
200       for ($x = 0; $x < count($pagehash["content"]); $x++) {
201
202          // manage line length here, lines should not exceed the
203          // length MSQL_MAX_LINE_LENGTH or something
204
205          if (strlen($pagehash["content"][$x]) > MSQL_MAX_LINE_LENGTH) {
206             $length = strlen($pagehash["content"][$x]);
207             echo "Must break up line ($length): " . $pagehash["content"][$x] ."<br>\n";
208             // can I cheat and use preg_split to break the line up?
209             // match this line with: /(.{1,127})+/
210             // in fact, split it on a zero-width metachar every 127th position
211             // take the returned array and add elements to $tmparray
212          } else {
213             $tmparray[$y] = $pagehash["content"][$x];
214             $y++;
215          }
216       }
217
218       reset($tmparray);
219       for ($x = 0; $x < count($tmparray); $x ++) {
220          $line = addslashes($tmparray[$x]);
221          $query = "INSERT INTO $dbi[page_table] " .
222                   "(pagename, lineno, line) " .
223                   "VALUES('$pagename', $x, '$line')";
224          echo "Page line insert query: $query<br>\n";
225          $retval = msql_query($query, $dbi['dbc']);
226          if ($retval == false) 
227             echo "Insert into $dbi[page_table] failed: ", msql_error(), "<br>\n";;
228          
229       }
230       //echo "<H1>inserted $x lines for $pagename</H1>\n";
231
232    }
233
234
235    function IsWikiPage($dbi, $pagename) {
236       $pagename = addslashes($pagename);
237       $query = "select pagename from $dbi[table] where pagename='$pagename'";
238 //      echo "Query: $query<br>\n";
239       if ($res = msql_query($query, $dbi['dbc'])) {
240          return(msql_affected_rows($res));
241       }
242    }
243
244
245    // setup for title-search
246    function InitTitleSearch($dbi, $search) {
247       $search = addslashes($search);
248       $query = "select pagename from $dbi[table] " .
249                "where pagename clike '%$search%' order by pagename";
250       $res = msql_query($query, $dbi["dbc"]);
251
252       return $res;
253    }
254
255
256    // iterating through database
257    function TitleSearchNextMatch($dbi, $res) {
258       if($o = msql_fetch_object($res)) {
259          return $o->pagename;
260       }
261       else {
262          return 0;
263       }
264    }
265
266
267    // setup for full-text search
268    function InitFullSearch($dbi, $search) {
269       $search = addslashes($search);
270       $query = "select * from $dbi[table] where searchterms clike '%$search%'";
271       $res = msql_query($query, $dbi["dbc"]);
272
273       return $res;
274    }
275
276    // iterating through database
277    function FullSearchNextMatch($dbi, $res) {
278       if($hash = msql_fetch_array($res)) {
279          return MakePageHash($hash);
280       } else {
281          return 0;
282       }
283    }
284
285    ////////////////////////
286    // new database features
287
288
289    function IncreaseHitCount($dbi, $pagename) {
290       return;
291       $query = "update hitcount set hits=hits+1 where pagename='$pagename'";
292       $res = mysql_query($query, $dbi['dbc']);
293
294       if (!mysql_affected_rows($dbi['dbc'])) {
295          $query = "insert into hitcount (pagename, hits) " .
296                   "values ('$pagename', 1)";
297          $res = mysql_query($query, $dbi['dbc']);
298       }
299
300       return $res;
301    }
302
303    function GetHitCount($dbi, $pagename) {
304       return;
305       $query = "select hits from hitcount where pagename='$pagename'";
306       $res = mysql_query($query, $dbi['dbc']);
307       if (mysql_num_rows($res)) {
308          $hits = mysql_result($res, 0);
309       } else {
310          $hits = "0";
311       }
312
313       return $hits;
314    }
315
316
317
318    function InitMostPopular($dbi, $limit) {
319       return;
320       $query = "select * from hitcount " .
321                "order by hits desc, pagename limit $limit";
322
323       $res = mysql_query($query);
324       
325       return $res;
326    }
327
328    function MostPopularNextMatch($dbi, $res) {
329       return;
330       if ($hits = mysql_fetch_array($res)) {
331          return $hits;
332       } else {
333          return 0;
334       }
335    }
336
337
338
339 ?>