]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/mysql.php
PhpWiki 1.2.7 backport cvs release-1_2-branch enhancements never released with 1...
[SourceForge/phpwiki.git] / lib / mysql.php
1 <?php rcs_id('$Id: mysql.php,v 1.10.2.7 2005-01-07 14:23:05 rurban 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       $qpagename = addslashes($pagename);
189       $res = mysql_query("update $HitCountStore set hits=hits+1"
190                          . " where pagename='$qpagename'",
191                          $dbi['dbc']);
192
193       if (!mysql_affected_rows($dbi['dbc'])) {
194          $res = mysql_query("insert into $HitCountStore (pagename, hits)"
195                             . " values ('$qpagename', 1)",
196                             $dbi['dbc']);
197       }
198
199       return $res;
200    }
201
202    function GetHitCount($dbi, $pagename)
203    {
204       global $HitCountStore;
205
206       $qpagename = addslashes($pagename);
207       $res = mysql_query("select hits from $HitCountStore"
208                          . " where pagename='$qpagename'",
209                          $dbi['dbc']);
210       if (mysql_num_rows($res))
211          $hits = mysql_result($res, 0);
212       else
213          $hits = "0";
214
215       return $hits;
216    }
217
218    function MakeSQLSearchClause($search, $column)
219    {
220       $search = preg_replace("/\s+/", " ", trim($search));
221       $search = preg_replace('/(?=[%_\\\\])/', "\\", $search);
222       $search = addslashes($search);
223       
224       $term = strtok($search, ' ');
225       $clause = '';
226       while($term) {
227          $word = strtolower("$term");
228          if ($word[0] == '-') {
229             $word = substr($word, 1);
230             $clause .= "not (LCASE($column) like '%$word%') ";
231          } else {
232             $clause .= "(LCASE($column) like '%$word%') ";
233          }
234          if ($term = strtok(' '))
235             $clause .= 'AND ';
236       }
237       
238       return $clause;
239    }
240
241    // setup for title-search
242    function InitTitleSearch($dbi, $search) {
243       $clause = MakeSQLSearchClause($search, 'pagename');
244       $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
245
246       return $res;
247    }
248
249
250    // iterating through database
251    function TitleSearchNextMatch($dbi, $res) {
252       if($o = mysql_fetch_object($res)) {
253          return $o->pagename;
254       }
255       else {
256          return 0;
257       }
258    }
259
260
261    // setup for full-text search
262    function InitFullSearch($dbi, $search) {
263       $clause = MakeSQLSearchClause($search, 'content');
264       $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
265
266       return $res;
267    }
268
269    // iterating through database
270    function FullSearchNextMatch($dbi, $res) {
271       if($hash = mysql_fetch_array($res)) {
272          return MakePageHash($hash);
273       }
274       else {
275          return 0;
276       }
277    }
278
279    // setup for back-link search
280    function InitBackLinkSearch($dbi, $pagename) {
281       global $WikiLinksStore;
282      
283       $topage = addslashes($pagename);
284       $res = mysql_query( "SELECT DISTINCT frompage FROM $WikiLinksStore"
285                           . " WHERE topage='$topage'"
286                           . " ORDER BY frompage",
287                           $dbi["dbc"]);
288       return $res;
289    }
290
291
292    // iterating through database
293    function BackLinkSearchNextMatch($dbi, $res) {
294       if($a = mysql_fetch_row($res)) {
295          return $a[0];
296       }
297       else {
298          return 0;
299       }
300    }
301
302
303    function InitMostPopular($dbi, $limit) {
304       global $HitCountStore;
305       $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
306       
307       return $res;
308    }
309
310    function MostPopularNextMatch($dbi, $res) {
311       if ($hits = mysql_fetch_array($res))
312          return $hits;
313       else
314          return 0;
315    }
316
317    function GetAllWikiPageNames($dbi) {
318       global $WikiPageStore;
319       $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
320       $rows = mysql_num_rows($res);
321       for ($i = 0; $i < $rows; $i++) {
322          $pages[$i] = mysql_result($res, $i);
323       }
324       return $pages;
325    }
326    
327    
328    ////////////////////////////////////////
329    // functionality for the wikilinks table
330
331    // takes a page name, returns array of scored incoming and outgoing links
332    function GetWikiPageLinks($dbi, $pagename) {
333       global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
334
335       $pagename = addslashes($pagename);
336       $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
337       $rows = mysql_num_rows($res);
338       for ($i = 0; $i < $rows; $i++) {
339          $out = mysql_fetch_array($res);
340          $links['out'][] = array($out['topage'], $out['score']);
341       }
342
343       $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
344       $rows = mysql_num_rows($res);
345       for ($i = 0; $i < $rows; $i++) {
346          $out = mysql_fetch_array($res);
347          $links['in'][] = array($out['frompage'], $out['score']);
348       }
349
350       $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");
351       $rows = mysql_num_rows($res);
352       for ($i = 0; $i < $rows; $i++) {
353          $out = mysql_fetch_array($res);
354          $links['popular'][] = array($out['pagename'], $out['hits']);
355       }
356
357       return $links;
358    }
359
360
361    // takes page name, list of links it contains
362    // the $linklist is an array where the keys are the page names
363    function SetWikiPageLinks($dbi, $pagename, $linklist) {
364       global $WikiLinksStore, $WikiScoreStore;
365
366       $frompage = addslashes($pagename);
367
368       // first delete the old list of links
369       mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
370                 $dbi["dbc"]);
371
372       // the page may not have links, return if not
373       if (! count($linklist))
374          return;
375       // now insert the new list of links
376       while (list($topage, $count) = each($linklist)) {
377          $topage = addslashes($topage);
378          if($topage != $frompage) {
379             mysql_query("insert into $WikiLinksStore (frompage, topage) " .
380                      "values ('$frompage', '$topage')", $dbi["dbc"]);
381          }
382       }
383
384       // update pagescore
385       mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
386       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"]);
387    }
388
389 /* more mysql queries:
390
391 orphans:
392 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;
393 */
394 ?>