]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_msql.php3
bringing documentation up to date
[SourceForge/phpwiki.git] / wiki_msql.php3
1 <!-- $Id: wiki_msql.php3,v 1.16 2000-09-16 19:20:46 wainstead Exp $ -->
2 <?php
3
4    /*
5       Database functions:
6       MakePageHash($dbhash)
7       MakeDBHash($pagename, $pagehash)
8       OpenDataBase($dbname)
9       CloseDataBase($dbi)
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)
19    */
20
21
22    // open a database and return the handle
23    // ignores MAX_DBM_ATTEMPTS
24
25    function OpenDataBase($dbinfo) {
26       global $msql_db;
27
28       if (! ($dbc = msql_connect())) {
29          echo "Cannot establish connection to database, giving up.";
30          echo "Error message: ", msql_error(), "<br>\n";
31          exit();
32       }
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";
36          exit();
37       }
38
39       $dbi['dbc'] = $dbc;
40       $dbi['table'] = $dbinfo['table'];           // page metadata
41       $dbi['page_table'] = $dbinfo['page_table']; // page content
42       return $dbi;
43    }
44
45
46    function CloseDataBase($dbi) {
47       // I found msql_pconnect unstable so we go the slow route.
48       return msql_close($dbi['dbc']);
49    }
50
51
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
56
57    function msqlDecomposeString($string) {
58       $ret_arr = array();
59       $el = 0;
60    
61       // zero, one, infinity
62       // account for the small case
63       if (strlen($string) < MSQL_MAX_LINE_LENGTH) { 
64          $ret_arr[$el] = $string;
65          return $ret_arr;
66       }
67    
68       $words = array();
69       $line = $string2 = "";
70    
71       // split on single spaces
72       $words = preg_split("/ /", $string);
73       $num_words = count($words);
74    
75       reset($words);
76       $ret_arr[0] = $words[0];
77       $line = " $words[1]";
78    
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;
83
84          if ($length < MSQL_MAX_LINE_LENGTH) {
85             $line .= " " .  $words[$x];
86          } else {
87             // put this line in the return array, reset, continue
88             $ret_arr[$el++] .= $line;
89             $line = " $words[$x]"; // reset     
90          }
91       }
92       $ret_arr[$el] = $line;
93       return $ret_arr;
94    }
95
96
97    // Take form data and prepare it for the db
98    function MakeDBHash($pagename, $pagehash)
99    {
100       $pagehash["pagename"] = addslashes($pagename);
101       if (!isset($pagehash["flags"]))
102          $pagehash["flags"] = 0;
103       if (!isset($pagehash["content"])) {
104          $pagehash["content"] = array();
105       } else {
106          $pagehash["content"] = implode("\n", $pagehash["content"]);
107          $pagehash["content"] = msqlDecomposeString($pagehash["content"]);
108       }
109       $pagehash["author"] = addslashes($pagehash["author"]);
110       $pagehash["refs"] = serialize($pagehash["refs"]);
111
112       return $pagehash;
113    }
114
115
116    // Take db data and prepare it for display
117    function MakePageHash($dbhash)
118    {
119       // unserialize/explode content
120       $dbhash['refs'] = unserialize($dbhash['refs']);
121       return $dbhash;
122    }
123
124
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'];
130
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);
136
137          $query = "select lineno,line from $pagetable " .
138                   "where pagename='$pagename' " .
139                   "order by lineno";
140
141          if ($res = msql_query($query, $dbi['dbc'])) {
142             $dbhash["content"] = array();
143             while ($row = msql_fetch_array($res)) {
144                 $msql_content .= $row["line"];
145             }
146             $dbhash["content"] = explode("\n", $msql_content);
147          }
148
149          return MakePageHash($dbhash);
150       }
151       return -1;
152    }
153
154
155    // Either insert or replace a key/value (a page)
156    function InsertPage($dbi, $pagename, $pagehash) {
157
158       $pagehash = MakeDBHash($pagename, $pagehash);
159       // $pagehash["content"] is now an array of strings 
160       // of MSQL_MAX_LINE_LENGTH
161
162       // record the time of modification
163       $pagehash["lastmodified"] = time();
164
165       if (IsWikiPage($dbi, $pagename)) {
166
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]";
174
175          $query  = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
176
177       } else {
178          // do an insert
179          // build up the column names and values for the query
180
181          $COLUMNS = "author, created, flags, lastmodified, " .
182                     "pagename, refs, version";
183
184          $VALUES =  "'$pagehash[author]', " .
185                     "$pagehash[created], $pagehash[flags], " .
186                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
187                     "'$pagehash[refs]', $pagehash[version]";
188
189
190          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
191       }
192
193       // echo "<p>Query: $query<p>\n";
194
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";
199
200
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";
208
209       // insert the new lines
210       reset($pagehash["content"]);
211
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";
222          
223       }
224
225    }
226
227
228
229    // for archiving pages to a separate table
230    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
231       global $ArchivePageStore;
232
233       $pagehash = MakeDBHash($pagename, $pagehash);
234       // $pagehash["content"] is now an array of strings 
235       // of MSQL_MAX_LINE_LENGTH
236
237       if (IsInArchive($dbi, $pagename)) {
238
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]";
246
247          $query  = "UPDATE $ArchivePageStore[table] SET $PAIRS WHERE pagename='$pagename'";
248
249       } else {
250          // do an insert
251          // build up the column names and values for the query
252
253          $COLUMNS = "author, created, flags, lastmodified, " .
254                     "pagename, refs, version";
255
256          $VALUES =  "'$pagehash[author]', " .
257                     "$pagehash[created], $pagehash[flags], " .
258                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
259                     "'$pagehash[refs]', $pagehash[version]";
260
261
262          $query = "INSERT INTO archive ($COLUMNS) VALUES($VALUES)";
263       }
264
265       // echo "<p>Query: $query<p>\n";
266
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";
271
272
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";
280
281       // insert the new lines
282       reset($pagehash["content"]);
283
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";
294          
295       }
296
297
298    }
299
300
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));
307       }
308    }
309
310
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));
317       }
318    }
319
320
321
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']);
328
329       return $res;
330    }
331
332
333    // iterating through database
334    function TitleSearchNextMatch($dbi, $res) {
335       if($o = msql_fetch_object($res)) {
336          return $o->pagename;
337       }
338       else {
339          return 0;
340       }
341    }
342
343
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%' " .
351                "order by pagename";
352       $res = msql_query($query, $dbi['dbc']);
353
354       return $res;
355    }
356
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);
362       } else {
363         return 0;
364       }
365    }
366
367    ////////////////////////
368    // new database features
369
370
371    function IncreaseHitCount($dbi, $pagename) {
372
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');
377          $hits++;
378          $query = "update hitcount set hits=$hits where pagename='$pagename'";
379          $res = msql_query($query, $dbi['dbc']);
380
381       } else {
382          $query = "insert into hitcount (pagename, hits) " .
383                   "values ('$pagename', 1)";
384          $res = msql_query($query, $dbi['dbc']);
385       }
386
387       return $res;
388    }
389
390    function GetHitCount($dbi, $pagename) {
391
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');
396       } else {
397          $hits = "0";
398       }
399
400       return $hits;
401    }
402
403
404
405    function InitMostPopular($dbi, $limit) {
406
407       $query = "select * from hitcount " .
408                "order by hits desc, pagename limit $limit";
409
410       $res = msql_query($query, $dbi['dbc']);
411       
412       return $res;
413    }
414
415    function MostPopularNextMatch($dbi, $res) {
416
417       if ($hits = msql_fetch_array($res)) {
418          return $hits;
419       } else {
420          return 0;
421       }
422    }
423
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');
429       }
430       return $pages;
431    }
432
433
434 ?>