]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_msql.php3
Added new markup rules for ordered and unordered lists.
[SourceForge/phpwiki.git] / wiki_msql.php3
1 <!-- $Id: wiki_msql.php3,v 1.9 2000-06-30 02:47:07 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    // This should receive the full text of the page in one string
52    // It will break the page text into an array of strings
53    // of length MSQL_MAX_LINE_LENGTH which should match the length
54    // of the columns wikipages.LINE, archivepages.LINE in schema.minisql
55
56    function msqlDecomposeString($string) {
57       $ret_arr = array();
58       $el = 0;
59    
60       // zero, one, infinity
61       // account for the small case
62       if (strlen($string) < MSQL_MAX_LINE_LENGTH) { 
63          $ret_arr[$el] = $string;
64          return $ret_arr;
65       }
66    
67       $words = array();
68       $line = $string2 = "";
69    
70       // split on single spaces
71       $words = preg_split("/ /", $string);
72       $num_words = count($words);
73    
74       reset($words);
75       $ret_arr[0] = $words[0];
76       $line = " $words[1]";
77    
78       // for all words, build up lines < MSQL_MAX_LINE_LENGTH in $ret_arr
79       for ($x = 2; $x < $num_words; $x++) {
80          $length = strlen($line) + strlen($words[$x]) 
81                    + strlen($ret_arr[$el]) + 1;
82
83          if ($length < MSQL_MAX_LINE_LENGTH) {
84             $line .= " " .  $words[$x];
85          } else {
86             // put this line in the return array, reset, continue
87             $ret_arr[$el++] .= $line;
88             $line = " $words[$x]"; // reset     
89          }
90       }
91       $ret_arr[$el] = $line;
92       return $ret_arr;
93    }
94
95
96    // Take form data and prepare it for the db
97    function MakeDBHash($pagename, $pagehash)
98    {
99       $pagehash["pagename"] = addslashes($pagename);
100       if (!isset($pagehash["flags"]))
101          $pagehash["flags"] = 0;
102       if (!isset($pagehash["content"])) {
103          $pagehash["content"] = array();
104       } else {
105          $pagehash["content"] = implode("\n", $pagehash["content"]);
106          $pagehash["content"] = msqlDecomposeString($pagehash["content"]);
107       }
108       $pagehash["author"] = addslashes($pagehash["author"]);
109       $pagehash["refs"] = serialize($pagehash["refs"]);
110
111       return $pagehash;
112    }
113
114
115    // Take db data and prepare it for display
116    function MakePageHash($dbhash)
117    {
118       // unserialize/explode content
119       $dbhash['refs'] = unserialize($dbhash['refs']);
120       return $dbhash;
121    }
122
123
124    // Return hash of page + attributes or default
125    function RetrievePage($dbi, $pagename) {
126       $pagename = addslashes($pagename);
127
128       $query = "select * from $dbi[table] where pagename='$pagename'";
129
130       if ($res = msql_query($query, $dbi['dbc'])) {
131          $dbhash = msql_fetch_array($res);
132
133          $query = "select lineno,line from $dbi[page_table] " .
134                   "where pagename='$pagename' " .
135                   "order by lineno";
136
137          if ($res = msql_query($query, $dbi[dbc])) {
138             $dbhash["content"] = array();
139             while ($row = msql_fetch_array($res)) {
140                 $msql_content .= $row["line"];
141             }
142             $dbhash["content"] = explode("\n", $msql_content);
143          }
144
145          return MakePageHash($dbhash);
146       }
147       return -1;
148    }
149
150
151    // Either insert or replace a key/value (a page)
152    function InsertPage($dbi, $pagename, $pagehash) {
153
154       $pagehash = MakeDBHash($pagename, $pagehash);
155       // $pagehash["content"] is now an array of strings 
156       // of MSQL_MAX_LINE_LENGTH
157
158       // record the time of modification
159       $pagehash["lastmodified"] = time();
160
161       if (IsWikiPage($dbi, $pagename)) {
162
163          $PAIRS = "author='$pagehash[author]'," .
164                   "created=$pagehash[created]," .
165                   "flags=$pagehash[flags]," .
166                   "lastmodified=$pagehash[lastmodified]," .
167                   "pagename='$pagehash[pagename]'," .
168                   "refs='$pagehash[refs]'," .
169                   "version=$pagehash[version]";
170
171          $query  = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
172
173       } else {
174          // do an insert
175          // build up the column names and values for the query
176
177          $COLUMNS = "author, created, flags, lastmodified, " .
178                     "pagename, refs, version";
179
180          $VALUES =  "'$pagehash[author]', " .
181                     "$pagehash[created], $pagehash[flags], " .
182                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
183                     "'$pagehash[refs]', $pagehash[version]";
184
185
186          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
187       }
188
189       // echo "<p>Query: $query<p>\n";
190
191       // first, insert the metadata
192       $retval = msql_query($query, $dbi['dbc']);
193       if ($retval == false) 
194          echo "Insert/update failed: ", msql_error(), "<br>\n";
195
196
197       // second, insert the page data
198       // remove old data from page_table
199       $query = "delete from $dbi[page_table] where pagename='$pagename'";
200       // echo "Delete query: $query<br>\n";
201       $retval = msql_query($query, $dbi['dbc']);
202       if ($retval == false) 
203          echo "Delete on $dbi[page_table] failed: ", msql_error(), "<br>\n";
204
205       // insert the new lines
206       reset($pagehash["content"]);
207
208       for ($x = 0; $x < count($pagehash["content"]); $x++) {
209          $line = addslashes($pagehash["content"][$x]);
210          $query = "INSERT INTO $dbi[page_table] " .
211                   "(pagename, lineno, line) " .
212                   "VALUES('$pagename', $x, '$line')";
213          // echo "Page line insert query: $query<br>\n";
214          $retval = msql_query($query, $dbi['dbc']);
215          if ($retval == false) 
216             echo "Insert into $dbi[page_table] failed: ", 
217                   msql_error(), "<br>\n";
218          
219       }
220
221    }
222
223
224    function IsWikiPage($dbi, $pagename) {
225       $pagename = addslashes($pagename);
226       $query = "select pagename from $dbi[table] where pagename='$pagename'";
227       // echo "Query: $query<br>\n";
228       if ($res = msql_query($query, $dbi['dbc'])) {
229          return(msql_affected_rows($res));
230       }
231    }
232
233
234    // setup for title-search
235    function InitTitleSearch($dbi, $search) {
236       $search = addslashes($search);
237       $query = "select pagename from $dbi[table] " .
238                "where pagename clike '%$search%' order by pagename";
239       $res = msql_query($query, $dbi["dbc"]);
240
241       return $res;
242    }
243
244
245    // iterating through database
246    function TitleSearchNextMatch($dbi, $res) {
247       if($o = msql_fetch_object($res)) {
248          return $o->pagename;
249       }
250       else {
251          return 0;
252       }
253    }
254
255
256    // setup for full-text search
257    function InitFullSearch($dbi, $search) {
258       // select unique page names from wikipages, and then 
259       // retrieve all pages that come back.
260       $search = addslashes($search);
261       $query = "select distinct pagename from $dbi[page_table] " .
262                "where line clike '%$search%' " .
263                "order by pagename";
264       $res = msql_query($query, $dbi["dbc"]);
265
266       return $res;
267    }
268
269    // iterating through database
270    function FullSearchNextMatch($dbi, $res) {
271       if ($row = msql_fetch_row($res)) {
272         return RetrievePage($dbi, $row[0]);
273       } else {
274         return 0;
275       }
276    }
277
278    ////////////////////////
279    // new database features
280
281
282    function IncreaseHitCount($dbi, $pagename) {
283       return;
284       $query = "update hitcount set hits=hits+1 where pagename='$pagename'";
285       $res = mysql_query($query, $dbi['dbc']);
286
287       if (!mysql_affected_rows($dbi['dbc'])) {
288          $query = "insert into hitcount (pagename, hits) " .
289                   "values ('$pagename', 1)";
290          $res = mysql_query($query, $dbi['dbc']);
291       }
292
293       return $res;
294    }
295
296    function GetHitCount($dbi, $pagename) {
297       return;
298       $query = "select hits from hitcount where pagename='$pagename'";
299       $res = mysql_query($query, $dbi['dbc']);
300       if (mysql_num_rows($res)) {
301          $hits = mysql_result($res, 0);
302       } else {
303          $hits = "0";
304       }
305
306       return $hits;
307    }
308
309
310
311    function InitMostPopular($dbi, $limit) {
312       return;
313       $query = "select * from hitcount " .
314                "order by hits desc, pagename limit $limit";
315
316       $res = mysql_query($query);
317       
318       return $res;
319    }
320
321    function MostPopularNextMatch($dbi, $res) {
322       return;
323       if ($hits = mysql_fetch_array($res)) {
324          return $hits;
325       } else {
326          return 0;
327       }
328    }
329
330
331
332 ?>