]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_msql.php3
This is still in transition. Do not use in a production setting.
[SourceForge/phpwiki.git] / wiki_msql.php3
1 <!-- $Id: wiki_msql.php3,v 1.4 2000-06-26 03:55:27 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    // Take form data and prepare it for the db
52    function MakeDBHash($pagename, $pagehash)
53    {
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"]);
61
62       return $pagehash;
63    }
64
65
66    // Take db data and prepare it for display
67    function MakePageHash($dbhash)
68    {
69       // unserialize/explode content
70       $dbhash['refs'] = unserialize($dbhash['refs']);
71       return $dbhash;
72    }
73
74
75    // Return hash of page + attributes or default
76    function RetrievePage($dbi, $pagename) {
77       $pagename = addslashes($pagename);
78
79       $query = "select * from $dbi[table] where pagename='$pagename'";
80
81       if ($res = msql_query($query, $dbi['dbc'])) {
82          $dbhash = msql_fetch_array($res);
83
84          $query = "select lineno,line from $dbi[page_table] " .
85                   "where pagename='$pagename' " .
86                   "order by lineno";
87
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"];
92             }
93          }
94
95          return MakePageHash($dbhash);
96       }
97       return -1;
98    }
99
100
101    // Either insert or replace a key/value (a page)
102    function InsertPage($dbi, $pagename, $pagehash)
103    {
104       $pagehash = MakeDBHash($pagename, $pagehash);
105
106       // temporary hack until the time stuff is brought up to date
107       $pagehash["created"] = time();
108       $pagehash["lastmodified"] = time();
109
110       if (IsWikiPage($dbi, $pagename)) {
111
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]";
119
120          $query  = "UPDATE $dbi[table] SET $PAIRS WHERE pagename='$pagename'";
121
122       } else {
123          // do an insert
124          // build up the column names and values for the query
125
126          $COLUMNS = "author, created, flags, lastmodified, " .
127                     "pagename, refs, version";
128
129          $VALUES =  "'$pagehash[author]', " .
130                     "$pagehash[created], $pagehash[flags], " .
131                     "$pagehash[lastmodified], '$pagehash[pagename]', " .
132                     "'$pagehash[refs]', $pagehash[version]";
133
134
135          $query = "INSERT INTO $dbi[table] ($COLUMNS) VALUES($VALUES)";
136       }
137
138 //      echo "<p>Query: $query<p>\n";
139
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";
144
145
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";
153
154       // insert the new lines
155       reset($pagehash["content"]);
156
157       $tmparray = array();
158       $y = 0;
159
160       for ($x = 0; $x < count($pagehash["content"]); $x++) {
161
162          // manage line length here, lines should not exceed the
163          // length MSQL_MAX_LINE_LENGTH or something
164
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
172          } else {
173             $tmparray[$y] = $pagehash["content"][$x];
174             $y++;
175          }
176       }
177
178       reset($tmparray);
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";;
188          
189       }
190       //echo "<H1>inserted $x lines for $pagename</H1>\n";
191
192    }
193
194
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));
201       }
202    }
203
204
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"]);
211
212       return $res;
213    }
214
215
216    // iterating through database
217    function TitleSearchNextMatch($dbi, $res) {
218       if($o = msql_fetch_object($res)) {
219          return $o->pagename;
220       }
221       else {
222          return 0;
223       }
224    }
225
226
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"]);
232
233       return $res;
234    }
235
236    // iterating through database
237    function FullSearchNextMatch($dbi, $res) {
238       if($hash = msql_fetch_array($res)) {
239          return MakePageHash($hash);
240       } else {
241          return 0;
242       }
243    }
244
245
246 ?>