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