]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/ADODB_mssqlnative.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / backend / ADODB_mssqlnative.php
1 <?php
2
3 /**
4  * MS SQL extensions for the ADODB DB backend.
5  */
6 require_once 'lib/WikiDB/backend/ADODB.php';
7
8 class WikiDB_backend_ADODB_mssqlnative
9     extends WikiDB_backend_ADODB
10 {
11     /**
12      * Constructor.
13      */
14     function WikiDB_backend_ADODB_mssqlnative($dbparams)
15     {
16         // Lowercase Assoc arrays
17         define('ADODB_ASSOC_CASE', 0);
18
19         // Backend constructor
20         $this->WikiDB_backend_ADODB($dbparams);
21
22         // Empty strings in MSSQL?  NULLS?
23         $this->_expressions['notempty'] = "NOT LIKE ''";
24         //doesn't work if content is of the "text" type http://msdn2.microsoft.com/en-us/library/ms188074.aspx
25         $this->_expressions['iscontent'] = "dbo.hasContent({$this->_table_names['version_tbl']}.content)";
26
27         $this->_prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
28
29     }
30
31     /**
32      * Pack tables.
33      */
34     function optimize()
35     {
36         // Do nothing here -- Leave that for the DB
37         // Cost Based Optimizer tuning vary from version to version
38         return 1;
39     }
40
41     // Search callabcks
42     // Page name
43     function _sql_match_clause($word)
44     {
45         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
46         $word = $this->_dbh->qstr("%$word%");
47         return "LOWER(pagename) LIKE $word";
48     }
49
50     // Fulltext -- case sensitive :-\
51     function _fullsearch_sql_match_clause($word)
52     {
53         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
54         $wordq = $this->_dbh->qstr("%$word%");
55         return "LOWER(pagename) LIKE $wordq "
56             . "OR CHARINDEX(content, '$word') > 0";
57     }
58
59     /**
60      * Serialize data
61      */
62     function _serialize($data)
63     {
64         if (empty($data))
65             return '';
66         assert(is_array($data));
67         return addslashes(serialize($data));
68     }
69
70     /**
71      * Unserialize data
72      */
73     function _unserialize($data)
74     {
75         return empty($data) ? array() : unserialize(stripslashes($data));
76     }
77
78     /*
79      * Update link table.
80      * on DEBUG: delete old, deleted links from page
81      */
82     function set_links($pagename, $links)
83     {
84         // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
85
86         $dbh = &$this->_dbh;
87         extract($this->_table_names);
88
89         $this->lock(array('link'));
90         $pageid = $this->_get_pageid($pagename, true);
91
92         $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as id, page.pagename FROM $link_tbl"
93             . " JOIN page ON ($link_tbl.linkto = page.id)"
94             . " WHERE linkfrom=$pageid");
95         // Delete current links,
96         $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
97         // and insert new links. Faster than checking for all single links
98         if ($links) {
99             foreach ($links as $link) {
100                 $linkto = $link['linkto'];
101                 if (isset($link['relation']))
102                     $relation = $this->_get_pageid($link['relation'], true);
103                 else
104                     $relation = 0;
105                 if ($linkto === "") { // ignore attributes
106                     continue;
107                 }
108                 // avoid duplicates
109                 if (isset($linkseen[$linkto]) and !$relation) {
110                     continue;
111                 }
112                 if (!$relation) {
113                     $linkseen[$linkto] = true;
114                 }
115                 $linkid = $this->_get_pageid($linkto, true);
116                 assert($linkid);
117                 if ($relation) {
118                     $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
119                         . " VALUES ($pageid, $linkid, $relation)");
120                 } else {
121                     $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
122                         . " VALUES ($pageid, $linkid)");
123                 }
124                 if ($oldlinks and array_key_exists($linkid, $oldlinks)) {
125                     // This was also in the previous page
126                     unset($oldlinks[$linkid]);
127                 }
128             }
129         }
130         // purge page table: delete all non-referenced pages
131         // for all previously linked pages, which have no other linkto links
132         if (DEBUG and $oldlinks) {
133             // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
134             foreach ($oldlinks as $id => $name) {
135                 // ...check if the page is empty and has no version
136                 if ($id != '') {
137                     $result = $dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
138                         . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)" //'"id" is not a recognized table hints option'
139                         . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)" //'"id" is not a recognized table hints option'
140                         . " WHERE $nonempty_tbl.id is NULL"
141                         . " AND $version_tbl.id is NULL"
142                         . " AND $page_tbl.id=$id");
143                     $linkto = $dbh->getRow("SELECT linkfrom FROM $link_tbl WHERE linkto=$id");
144                     if ($result and empty($linkto)) {
145                         trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
146                         $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
147                         $dbh->Execute("DELETE FROM $link_tbl WHERE linkto=$id");
148                         $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
149                     }
150                 }
151             }
152         }
153         $this->unlock(array('link'));
154         return true;
155     }
156
157     /* get all oldlinks in hash => id, relation
158        check for all new links
159      */
160     function set_links1($pagename, $links)
161     {
162
163         $dbh = &$this->_dbh;
164         extract($this->_table_names);
165
166         $this->lock(array('link'));
167         $pageid = $this->_get_pageid($pagename, true);
168
169         $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as linkto, $link_tbl.relation, page.pagename"
170             . " FROM $link_tbl"
171             . " JOIN page ON ($link_tbl.linkto = page.id)"
172             . " WHERE linkfrom=$pageid");
173         /*      old                  new
174          *      X => [1,0 2,0 1,1]   X => [1,1 3,0]
175          * => delete 1,0 2,0 + insert 3,0
176          */
177         if ($links) {
178             foreach ($links as $link) {
179                 $linkto = $link['linkto'];
180                 if ($link['relation'])
181                     $relation = $this->_get_pageid($link['relation'], true);
182                 else
183                     $relation = 0;
184                 // avoid duplicates
185                 if (isset($linkseen[$linkto]) and !$relation) {
186                     continue;
187                 }
188                 if (!$relation) {
189                     $linkseen[$linkto] = true;
190                 }
191                 $linkid = $this->_get_pageid($linkto, true);
192                 assert($linkid);
193                 $skip = 0;
194                 // find linkfrom,linkto,relation triple in oldlinks
195                 foreach ($oldlinks as $l) {
196                     if ($relation) { // relation NOT NULL
197                         if ($l['linkto'] == $linkid and $l['relation'] == $relation) {
198                             // found and skip
199                             $skip = 1;
200                         }
201                     }
202                 }
203                 if (!$skip) {
204                     if ($update) {
205                     }
206                     if ($relation) {
207                         $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
208                             . " VALUES ($pageid, $linkid, $relation)");
209                     } else {
210                         $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
211                             . " VALUES ($pageid, $linkid)");
212                     }
213                 }
214
215                 if (array_key_exists($linkid, $oldlinks)) {
216                     // This was also in the previous page
217                     unset($oldlinks[$linkid]);
218                 }
219             }
220         }
221         // purge page table: delete all non-referenced pages
222         // for all previously linked pages...
223         if (DEBUG and $oldlinks) {
224             // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
225             foreach ($oldlinks as $id => $name) {
226                 // ...check if the page is empty and has no version
227                 if ($id != '') {
228                     if ($dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
229                         . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)" //'"id" is not a recognized table hints option'
230                         . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)" //'"id" is not a recognized table hints option'
231                         . " WHERE $nonempty_tbl.id is NULL"
232                         . " AND $version_tbl.id is NULL"
233                         . " AND $page_tbl.id=$id")
234                     ) {
235                         trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
236                         $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
237                         $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
238                     }
239                 }
240             }
241         }
242         $this->unlock(array('link'));
243         return true;
244     }
245
246 }
247
248 ;
249
250 // Local Variables:
251 // mode: php
252 // tab-width: 8
253 // c-basic-offset: 4
254 // c-hanging-comment-ender-p: nil
255 // indent-tabs-mode: nil
256 // End: