]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/mysql.php
squashed some E_NOTICE messages about unset variables
[SourceForge/phpwiki.git] / lib / mysql.php
1 <?php rcs_id('$Id: mysql.php,v 1.9 2001-01-01 23:34:57 ahollosi 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       if (!isset($pagehash["refs"]))
69          $pagehash["refs"] = array();
70       $pagehash["refs"] = serialize($pagehash["refs"]);
71  
72       return $pagehash;
73    }
74
75    function MakePageHash($dbhash)
76    {
77       // unserialize/explode content
78       $dbhash['refs'] = unserialize($dbhash['refs']);
79       $dbhash['content'] = explode("\n", $dbhash['content']);
80       return $dbhash;
81    }
82
83
84    // Return hash of page + attributes or default
85    function RetrievePage($dbi, $pagename, $pagestore) {
86       $pagename = addslashes($pagename);
87       if ($res = mysql_query("select * from $pagestore where pagename='$pagename'", $dbi['dbc'])) {
88          if ($dbhash = mysql_fetch_array($res)) {
89             return MakePageHash($dbhash);
90          }
91       }
92       return -1;
93    }
94
95
96    // Either insert or replace a key/value (a page)
97    function InsertPage($dbi, $pagename, $pagehash)
98    {
99       global $WikiPageStore; // ugly hack
100
101       if ($dbi['table'] == $WikiPageStore) { // HACK
102          $linklist = ExtractWikiPageLinks($pagehash['content']);
103          SetWikiPageLinks($dbi, $pagename, $linklist);
104       }
105
106       $pagehash = MakeDBHash($pagename, $pagehash);
107
108       $COLUMNS = "author, content, created, flags, " .
109                  "lastmodified, pagename, refs, version";
110
111       $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
112                  "$pagehash[created], $pagehash[flags], " .
113                  "$pagehash[lastmodified], '$pagehash[pagename]', " .
114                  "'$pagehash[refs]', $pagehash[version]";
115
116       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
117                         $dbi['dbc'])) {
118             $msg = sprintf(gettext ("Error writing page '%s'"), $pagename);
119             $msg .= "<BR>";
120             $msg .= sprintf(gettext ("MySQL error: %s"), mysql_error());
121             ExitWiki($msg);
122       }
123    }
124
125
126    // for archiving pages to a seperate dbm
127    function SaveCopyToArchive($dbi, $pagename, $pagehash) {
128       global $ArchivePageStore;
129       $adbi = OpenDataBase($ArchivePageStore);
130       InsertPage($adbi, $pagename, $pagehash);
131    }
132
133
134    function IsWikiPage($dbi, $pagename) {
135       $pagename = addslashes($pagename);
136       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
137          return(mysql_result($res, 0));
138       }
139       return 0;
140    }
141
142    function IsInArchive($dbi, $pagename) {
143       global $ArchivePageStore;
144
145       $pagename = addslashes($pagename);
146       if ($res = mysql_query("select count(*) from $ArchivePageStore where pagename='$pagename'", $dbi['dbc'])) {
147          return(mysql_result($res, 0));
148       }
149       return 0;
150    }
151
152
153    function RemovePage($dbi, $pagename) {
154       global $WikiPageStore, $ArchivePageStore;
155       global $WikiLinksStore, $HitCountStore, $WikiScoreStore;
156
157       $pagename = addslashes($pagename);
158       $msg = gettext ("Cannot delete '%s' from table '%s'");
159       $msg .= "<br>\n";
160       $msg .= gettext ("MySQL error: %s");
161
162       if (!mysql_query("delete from $WikiPageStore where pagename='$pagename'", $dbi['dbc']))
163          ExitWiki(sprintf($msg, $pagename, $WikiPageStore, mysql_error()));
164
165       if (!mysql_query("delete from $ArchivePageStore where pagename='$pagename'", $dbi['dbc']))
166          ExitWiki(sprintf($msg, $pagename, $ArchivePageStore, mysql_error()));
167
168       if (!mysql_query("delete from $WikiLinksStore where frompage='$pagename'", $dbi['dbc']))
169          ExitWiki(sprintf($msg, $pagename, $WikiLinksStore, mysql_error()));
170
171       if (!mysql_query("delete from $HitCountStore where pagename='$pagename'", $dbi['dbc']))
172          ExitWiki(sprintf($msg, $pagename, $HitCountStore, mysql_error()));
173
174       if (!mysql_query("delete from $WikiScoreStore where pagename='$pagename'", $dbi['dbc']))
175          ExitWiki(sprintf($msg, $pagename, $WikiScoreStore, mysql_error()));
176    }
177
178
179    function IncreaseHitCount($dbi, $pagename)
180    {
181       global $HitCountStore;
182
183       $res = mysql_query("update $HitCountStore set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
184
185       if (!mysql_affected_rows($dbi['dbc'])) {
186          $res = mysql_query("insert into $HitCountStore (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
187       }
188
189       return $res;
190    }
191
192    function GetHitCount($dbi, $pagename)
193    {
194       global $HitCountStore;
195
196       $res = mysql_query("select hits from $HitCountStore where pagename='$pagename'", $dbi['dbc']);
197       if (mysql_num_rows($res))
198          $hits = mysql_result($res, 0);
199       else
200          $hits = "0";
201
202       return $hits;
203    }
204
205    function MakeSQLSearchClause($search, $column)
206    {
207       $search = addslashes(preg_replace("/\s+/", " ", $search));
208       $term = strtok($search, ' ');
209       while($term) {
210          $word = "$term";
211          if ($word[0] == '-') {
212             $word = substr($word, 1);
213             $clause .= "not ($column like '%$word%') ";
214          } else {
215             $clause .= "($column like '%$word%') ";
216          }
217          if ($term = strtok(' '))
218             $clause .= 'and ';
219       }
220       return $clause;
221    }
222
223    // setup for title-search
224    function InitTitleSearch($dbi, $search) {
225       $clause = MakeSQLSearchClause($search, 'pagename');
226       $res = mysql_query("select pagename from $dbi[table] where $clause order by pagename", $dbi["dbc"]);
227
228       return $res;
229    }
230
231
232    // iterating through database
233    function TitleSearchNextMatch($dbi, $res) {
234       if($o = mysql_fetch_object($res)) {
235          return $o->pagename;
236       }
237       else {
238          return 0;
239       }
240    }
241
242
243    // setup for full-text search
244    function InitFullSearch($dbi, $search) {
245       $clause = MakeSQLSearchClause($search, 'content');
246       $res = mysql_query("select * from $dbi[table] where $clause", $dbi["dbc"]);
247
248       return $res;
249    }
250
251    // iterating through database
252    function FullSearchNextMatch($dbi, $res) {
253       if($hash = mysql_fetch_array($res)) {
254          return MakePageHash($hash);
255       }
256       else {
257          return 0;
258       }
259    }
260
261    function InitMostPopular($dbi, $limit) {
262       global $HitCountStore;
263       $res = mysql_query("select * from $HitCountStore order by hits desc, pagename limit $limit", $dbi["dbc"]);
264       
265       return $res;
266    }
267
268    function MostPopularNextMatch($dbi, $res) {
269       if ($hits = mysql_fetch_array($res))
270          return $hits;
271       else
272          return 0;
273    }
274
275    function GetAllWikiPageNames($dbi) {
276       global $WikiPageStore;
277       $res = mysql_query("select pagename from $WikiPageStore", $dbi["dbc"]);
278       $rows = mysql_num_rows($res);
279       for ($i = 0; $i < $rows; $i++) {
280          $pages[$i] = mysql_result($res, $i);
281       }
282       return $pages;
283    }
284    
285    
286    ////////////////////////////////////////
287    // functionality for the wikilinks table
288
289    // takes a page name, returns array of scored incoming and outgoing links
290    function GetWikiPageLinks($dbi, $pagename) {
291       global $WikiLinksStore, $WikiScoreStore, $HitCountStore;
292
293       $pagename = addslashes($pagename);
294       $res = mysql_query("select topage, score from $WikiLinksStore, $WikiScoreStore where topage=pagename and frompage='$pagename' order by score desc, topage");
295       $rows = mysql_num_rows($res);
296       for ($i = 0; $i < $rows; $i++) {
297          $out = mysql_fetch_array($res);
298          $links['out'][] = array($out['topage'], $out['score']);
299       }
300
301       $res = mysql_query("select frompage, score from $WikiLinksStore, $WikiScoreStore where frompage=pagename and topage='$pagename' order by score desc, frompage");
302       $rows = mysql_num_rows($res);
303       for ($i = 0; $i < $rows; $i++) {
304          $out = mysql_fetch_array($res);
305          $links['in'][] = array($out['frompage'], $out['score']);
306       }
307
308       $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");
309       $rows = mysql_num_rows($res);
310       for ($i = 0; $i < $rows; $i++) {
311          $out = mysql_fetch_array($res);
312          $links['popular'][] = array($out['pagename'], $out['hits']);
313       }
314
315       return $links;
316    }
317
318
319    // takes page name, list of links it contains
320    // the $linklist is an array where the keys are the page names
321    function SetWikiPageLinks($dbi, $pagename, $linklist) {
322       global $WikiLinksStore, $WikiScoreStore;
323
324       $frompage = addslashes($pagename);
325
326       // first delete the old list of links
327       mysql_query("delete from $WikiLinksStore where frompage='$frompage'",
328                 $dbi["dbc"]);
329
330       // the page may not have links, return if not
331       if (! count($linklist))
332          return;
333       // now insert the new list of links
334       while (list($topage, $count) = each($linklist)) {
335          $topage = addslashes($topage);
336          if($topage != $frompage) {
337             mysql_query("insert into $WikiLinksStore (frompage, topage) " .
338                      "values ('$frompage', '$topage')", $dbi["dbc"]);
339          }
340       }
341
342       // update pagescore
343       mysql_query("delete from $WikiScoreStore", $dbi["dbc"]);
344       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"]);
345    }
346
347 /* more mysql queries:
348
349 orphans:
350 select pagename from wiki left join wikilinks on pagename=topage where topage is NULL;
351 */
352 ?>