]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - wiki_mysql.php3
This will test all the markup rules eventually. Right now I'm using it
[SourceForge/phpwiki.git] / wiki_mysql.php3
1 <? rcs_id('$Id: wiki_mysql.php3,v 1.12 2000-07-07 19:53:50 dairiki Exp $');
2
3    /*
4       Database functions:
5
6       OpenDataBase($dbname)
7       CloseDataBase($dbi)
8       RetrievePage($dbi, $pagename)
9       InsertPage($dbi, $pagename, $pagehash)
10       IsWikiPage($dbi, $pagename)
11       InitTitleSearch($dbi, $search)
12       TitleSearchNextMatch($dbi, &$pos)
13       InitFullSearch($dbi, $search)
14       FullSearchNextMatch($dbi, &$pos)
15       IncreaseHitCount($dbi, $pagename)  
16       GetHitCount($dbi, $pagename)   
17       InitMostPopular($dbi, $limit)   
18       MostPopularNextMatch($dbi, $res)
19       GetAllWikiPageNames($dbi)
20
21    */
22
23
24    // open a database and return the handle
25    // ignores MAX_DBM_ATTEMPTS
26
27    function OpenDataBase($dbname) {
28       global $mysql_server, $mysql_user, $mysql_pwd, $mysql_db;
29
30       if (!($dbc = mysql_pconnect($mysql_server, $mysql_user, $mysql_pwd))) {
31          echo "Cannot establish connection to database, giving up.";
32          echo "MySQL error: ", mysql_error(), "<br>\n";
33          exit();
34       }
35       if (!mysql_select_db($mysql_db, $dbc)) {
36          echo "Cannot open database, giving up.";
37          echo "MySQL error: ", mysql_error(), "<br>\n";
38          exit();
39       }
40
41       $dbi['dbc'] = $dbc;
42       $dbi['table'] = $dbname;
43       return $dbi;
44    }
45
46
47    function CloseDataBase($dbi) {
48       // NOP function
49       // mysql connections are established as persistant
50       // they cannot be closed through mysql_close()
51    }
52
53
54    function MakeDBHash($pagename, $pagehash)
55    {
56       $pagehash["pagename"] = addslashes($pagename);
57       if (!isset($pagehash["flags"]))
58          $pagehash["flags"] = 0;
59       $pagehash["author"] = addslashes($pagehash["author"]);
60       $pagehash["content"] = implode("\n", $pagehash["content"]);
61       $pagehash["content"] = addslashes($pagehash["content"]);
62       $pagehash["refs"] = serialize($pagehash["refs"]);
63  
64       return $pagehash;
65    }
66
67    function MakePageHash($dbhash)
68    {
69       // unserialize/explode content
70       $dbhash['refs'] = unserialize($dbhash['refs']);
71       $dbhash['content'] = explode("\n", $dbhash['content']);
72       return $dbhash;
73    }
74
75
76    // Return hash of page + attributes or default
77    function RetrievePage($dbi, $pagename) {
78       $pagename = addslashes($pagename);
79       if ($res = mysql_query("select * from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
80          if ($dbhash = mysql_fetch_array($res)) {
81             return MakePageHash($dbhash);
82          }
83       }
84       return -1;
85    }
86
87
88    // Either insert or replace a key/value (a page)
89    function InsertPage($dbi, $pagename, $pagehash)
90    {
91       $pagehash = MakeDBHash($pagename, $pagehash);
92
93       $COLUMNS = "author, content, created, flags, " .
94                  "lastmodified, pagename, refs, version";
95
96       $VALUES =  "'$pagehash[author]', '$pagehash[content]', " .
97                  "$pagehash[created], $pagehash[flags], " .
98                  "$pagehash[lastmodified], '$pagehash[pagename]', " .
99                  "'$pagehash[refs]', $pagehash[version]";
100
101
102       if (!mysql_query("replace into $dbi[table] ($COLUMNS) values ($VALUES)",
103                         $dbi['dbc'])) {
104             echo "error writing page '$pagename' ";
105             echo mysql_error();
106             exit();
107       }
108    }
109
110
111    function IsWikiPage($dbi, $pagename) {
112       $pagename = addslashes($pagename);
113       if ($res = mysql_query("select count(*) from $dbi[table] where pagename='$pagename'", $dbi['dbc'])) {
114          return(mysql_result($res, 0));
115       }
116    }
117
118
119    function IncreaseHitCount($dbi, $pagename)
120    {
121       $res = mysql_query("update hitcount set hits=hits+1 where pagename='$pagename'", $dbi['dbc']);
122
123       if (!mysql_affected_rows($dbi['dbc'])) {
124          $res = mysql_query("insert into hitcount (pagename, hits) values ('$pagename', 1)", $dbi['dbc']);
125       }
126
127       return $res;
128    }
129
130    function GetHitCount($dbi, $pagename)
131    {
132       $res = mysql_query("select hits from hitcount where pagename='$pagename'", $dbi['dbc']);
133       if (mysql_num_rows($res))
134          $hits = mysql_result($res, 0);
135       else
136          $hits = "0";
137
138       return $hits;
139    }
140
141
142    // setup for title-search
143    function InitTitleSearch($dbi, $search) {
144       $search = addslashes($search);
145       $res = mysql_query("select pagename from $dbi[table] where pagename like '%$search%' order by pagename", $dbi["dbc"]);
146
147       return $res;
148    }
149
150
151    // iterating through database
152    function TitleSearchNextMatch($dbi, $res) {
153       if($o = mysql_fetch_object($res)) {
154          return $o->pagename;
155       }
156       else {
157          return 0;
158       }
159    }
160
161
162    // setup for full-text search
163    function InitFullSearch($dbi, $search) {
164       $search = addslashes($search);
165       $res = mysql_query("select * from $dbi[table] where content like '%$search%'", $dbi["dbc"]);
166
167       return $res;
168    }
169
170    // iterating through database
171    function FullSearchNextMatch($dbi, $res) {
172       if($hash = mysql_fetch_array($res)) {
173          return MakePageHash($hash);
174       }
175       else {
176          return 0;
177       }
178    }
179
180    function InitMostPopular($dbi, $limit) {
181       $res = mysql_query("select * from hitcount order by hits desc, pagename limit $limit");
182       
183       return $res;
184    }
185
186    function MostPopularNextMatch($dbi, $res) {
187       if ($hits = mysql_fetch_array($res))
188          return $hits;
189       else
190          return 0;
191    }
192
193    function GetAllWikiPageNames($dbi) {
194       $res = mysql_query("select pagename from wiki");
195       $rows = mysql_num_rows($res);
196       for ($i = 0; $i < $rows; $i++) {
197          $pages[$i] = mysql_result($res, $i);
198       }
199       return $pages;
200    }
201 ?>