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