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