]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/mysql.php
Added semicolon on line 153, which caused the file to not compile correctly.
[SourceForge/phpwiki.git] / lib / mysql.php
1 <?php rcs_id('$Id: mysql.php,v 1.8 2000-11-24 22:07:34 wainstead Exp $');
2
3    /*
4       Database functions:
5       OpenDataBase($dbname)
6       CloseDataBase($dbi)
7       MakeDBHash($pagename, $pagehash)
8       MakePageHash($dbhash)
9       RetrievePage($dbi, $pagename, $pagestore)
10       InsertPage($dbi, $pagename, $pagehash)
11       SaveCopyToArchive($dbi, $pagename, $pagehash)
12       IsWikiPage($dbi, $pagename)
13       IsInArchive($dbi, $pagename)
14       RemovePage($dbi, $pagename)
15       IncreaseHitCount($dbi, $pagename)
16       GetHitCount($dbi, $pagename)
17       MakeSQLSearchClause($search, $column)
18       InitTitleSearch($dbi, $search)
19       TitleSearchNextMatch($dbi, $res)
20       InitFullSearch($dbi, $search)
21       FullSearchNextMatch($dbi, $res)
22       InitMostPopular($dbi, $limit)
23       MostPopularNextMatch($dbi, $res)
24       GetAllWikiPageNames($dbi)
25       GetWikiPageLinks($dbi, $pagename)
26       SetWikiPageLinks($dbi, $pagename, $linklist)
27    */
28
29    // open a database and return the handle
30    // ignores MAX_DBM_ATTEMPTS
31
32    function OpenDataBase($dbname) {
33       global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
34
35       if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
36          $msg = gettext ("Cannot establish connection to database, giving up.");
37          $msg .= "<BR>";
38          $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
39          ExitWiki($msg);
40       }
41       if (!mysql_select_db($mysql_db, $dbc)) {
42          $msg =  sprintf(gettext ("Cannot open database %s, giving up."), $mysql_db);
43          $msg .= "<BR>";
44          $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
45          ExitWiki($msg);
46       }
47       $dbi['dbc'] = $dbc;
48       $dbi['table'] = $dbname;
49       return $dbi;
50    }
51
52
53    function CloseDataBase($dbi) {
54       // NOP function
55       // mysql connections are established as persistant
56       // they cannot be closed through mysql_close()
57    }
58
59
60    function MakeDBHash($pagename, $pagehash)
61    {
62       $pagehash["pagename"] = addslashes($pagename);
63       if (!isset($pagehash["flags"]))
64          $pagehash["flags"] = 0;
65       $pagehash["author"] = addslashes($pagehash["author"]);
66       $pagehash["content"] = implode("\n", $pagehash["content"]);
67       $pagehash["content"] = addslashes($pagehash["content"]);
68       $pagehash["refs"] = serialize($pagehash["refs"]);
69  
70       return $pagehash;
71    }
72
73    function MakePageHash($dbhash)
74    {
75       // unserialize/explode content
76       $dbhash['refs'] = unserialize($dbhash['refs']);
77       $dbhash['content'] = explode("\n", $dbhash['content']);
78       return $dbhash;
79    }
80
81
82    // Return hash of page + attributes or default
83    function RetrievePage($dbi, $pagename, $pagestore) {
84       $pagename = addslashes($pagename);
85       if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
86          if ($dbhash = mysql_fetch_array($res)) {
87             return MakePageHash($dbhash);
88          }
89       }
90       return -1;
91    }
92
93
94    // Either insert or replace a key/value (a page)
95    function InsertPage($dbi, $pagename, $pagehash)
96    {
97       global $WikiPageStore; // ugly hack
98
99       if ($dbi['table'] == $WikiPageStore) { // HACK
100          $linklist = ExtractWikiPageLinks($pagehash['content']);
101          SetWikiPageLinks($dbi, $pagename, $linklist);
102       }
103
104       $pagehash = MakeDBHash($pagename, $pagehash);
105
106       $COLUMNS = "author, content, created, flags, " .
107                  "lastmodified, pagename, refs, version";
108
109       $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
110                  "$pagehash[created], $pagehash[flags], " .
111                  "$pagehash[lastmodified], '$pagehash[pagename]', " .
112                  "'$pagehash[refs]', $pagehash[version]";
113
114       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
115                         $dbi['dbc'])) {
116             $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
117             $msg .= "<BR>";
118             $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
119             ExitWiki($msg);
120       }
121    }
122
123
124    // for archiving pages to a seperate dbm
125    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
126       global $ArchivePageStore;
127       $adbi = OpenDataBase($ArchivePageStore);
128       InsertPage($adbi, $pagename, $pagehash);
129    }
130
131
132    function IsWikiPage($dbi, $pagename) {
133       $pagename = addslashes($pagename);
134       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
135          return(mysql_result($res, 0));
136       }
137       return 0;
138    }
139
140    function IsInArchive($dbi, $pagename) {
141       global $ArchivePageStore;
142
143       $pagename = addslashes($pagename);
144       if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
145          return(mysql_result($res, 0));
146       }
147       return 0;
148    }
149
150
151    function RemovePage($dbi, $pagename) {
152       global $WikiPageStore, $ArchivePageStore;
153       global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
154
155       $pagename = addslashes($pagename);
156       $msg = gettext ("Cannot delete '%s' from table '%s'");
157       $msg .= "<br>\n";
158       $msg .= gettext ("MySQL error: %s");
159
160       if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
161          ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
162
163       if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
164          ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
165
166       if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
167          ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
168
169       if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
170          ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
171
172       if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
173          ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
174    }
175
176
177    function IncreaseHitCount($dbi, $pagename)
178    {
179       global $HitCountStore;
180
181       $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
182
183       if (!mysql_affected_rows($dbi['dbc'])) {
184          $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
185       }
186
187       return $res;
188    }
189
190    function GetHitCount($dbi, $pagename)
191    {
192       global $HitCountStore;
193
194       $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
195       if (mysql_num_rows($res))
196          $hits = mysql_result($res, 0);
197       else
198          $hits = "0";
199
200       return $hits;
201    }
202
203    function MakeSQLSearchClause($search, $column)
204    {
205       $search = addslashes(preg_replace("/\s+/", " ", $search));
206       $term = strtok($search, ' ');
207       while($term) {
208          $word = "$term";
209          if ($word[0] == '-') {
210             $word = substr($word, 1);
211             $clause .= "not ($column like '%$word%') ";
212          } else {
213             $clause .= "($column like '%$word%') ";
214          }
215          if ($term = strtok(' '))
216             $clause .= 'and ';
217       }
218       return $clause;
219    }
220
221    // setup for title-search
222    function InitTitleSearch($dbi, $search) {
223       $clause = MakeSQLSearchClause($search, 'pagename');
224       $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
225
226       return $res;
227    }
228
229
230    // iterating through database
231    function TitleSearchNextMatch($dbi, $res) {
232       if($o = mysql_fetch_object($res)) {
233          return $o->pagename;
234       }
235       else {
236          return 0;
237       }
238    }
239
240
241    // setup for full-text search
242    function InitFullSearch($dbi, $search) {
243       $clause = MakeSQLSearchClause($search, 'content');
244       $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
245
246       return $res;
247    }
248
249    // iterating through database
250    function FullSearchNextMatch($dbi, $res) {
251       if($hash = mysql_fetch_array($res)) {
252          return MakePageHash($hash);
253       }
254       else {
255          return 0;
256       }
257    }
258
259    function InitMostPopular($dbi, $limit) {
260       global $HitCountStore;
261       $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
262       
263       return $res;
264    }
265
266    function MostPopularNextMatch($dbi, $res) {
267       if ($hits = mysql_fetch_array($res))
268          return $hits;
269       else
270          return 0;
271    }
272
273    function GetAllWikiPageNames($dbi) {
274       global $WikiPageStore;
275       $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
276       $rows = mysql_num_rows($res);
277       for ($i = 0; $i < $rows; $i++) {
278          $pages[$i] = mysql_result($res, $i);
279       }
280       return $pages;
281    }
282    
283    
284    ////////////////////////////////////////
285    // functionality for the wikilinks table
286
287    // takes a page name, returns array of scored incoming and outgoing links
288    function GetWikiPageLinks($dbi, $pagename) {
289       global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
290
291       $pagename = addslashes($pagename);
292       $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
293       $rows = mysql_num_rows($res);
294       for ($i = 0; $i < $rows; $i++) {
295          $out = mysql_fetch_array($res);
296          $links['out'][] = array($out['topage'], $out['score']);
297       }
298
299       $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
300       $rows = mysql_num_rows($res);
301       for ($i = 0; $i < $rows; $i++) {
302          $out = mysql_fetch_array($res);
303          $links['in'][] = array($out['frompage'], $out['score']);
304       }
305
306       $res = mysql_query("select distinct pagename, hits from $WikiLinksStore, $HitCountStore where (frompage=pagename and topage='$pagename') or (topage=pagename and frompage='$pagename') order by hits desc, pagename");
307       $rows = mysql_num_rows($res);
308       for ($i = 0; $i < $rows; $i++) {
309          $out = mysql_fetch_array($res);
310          $links['popular'][] = array($out['pagename'], $out['hits']);
311       }
312
313       return $links;
314    }
315
316
317    // takes page name, list of links it contains
318    // the $linklist is an array where the keys are the page names
319    function SetWikiPageLinks($dbi, $pagename, $linklist) {
320       global $WikiLinksStore, $WikiScoreStore;
321
322       $frompage = addslashes($pagename);
323
324       // first delete the old list of links
325       mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
326                 $dbi["dbc"]);
327
328       // the page may not have links, return if not
329       if (! count($linklist))
330          return;
331       // now insert the new list of links
332       while (list($topage, $count) = each($linklist)) {
333          $topage = addslashes($topage);
334          if($topage != $frompage) {
335             mysql_query("insert into $WikiLinksStore (frompage, topage) " .
336                      "values ('$frompage', '$topage')", $dbi["dbc"]);
337          }
338       }
339
340       // update pagescore
341       mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
342       mysql_query("insert into $WikiScoreStore select w1.topage, count(*) from $WikiLinksStore as w1, $WikiLinksStore as w2 where w2.topage=w1.frompage group by w1.topage", $dbi["dbc"]);
343    }
344
345 /* more mysql queries:
346
347 orphans:
348 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;
349 */
350 ?>