author | Dan |
Wed, 21 Nov 2007 15:22:13 -0500 | |
changeset 13 | f6ca7cead82c |
parent 4 | ad3b061a1c76 |
child 15 | ac7d3dedcc44 |
permissions | -rw-r--r-- |
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
1 |
<?php |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
2 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
3 |
/* |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
4 |
* Snapr |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
5 |
* Version 0.1 beta 1 |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
6 |
* Copyright (C) 2007 Dan Fuhry |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
7 |
* |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
10 |
* |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
13 |
*/ |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
14 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
15 |
// |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
16 |
// Search results hook |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
17 |
// |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
18 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
19 |
$plugins->attachHook('search_results', 'gal_searcher($q, $offset);'); |
13 | 20 |
$plugins->attachHook('search_global_inner', 'snapr_search_new_api($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);'); |
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
21 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
22 |
$plugins->attachHook('compile_template', ' |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
23 |
// CSS for gallery browser |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
24 |
$template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />\'); |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
25 |
$template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />\'); |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
26 |
'); |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
27 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
28 |
function gal_searcher($q, $offset) |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
29 |
{ |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
30 |
global $db, $session, $paths, $template, $plugins; // Common objects |
13 | 31 |
if ( defined('SNAPR_SEARCH_USING_NEW_API') ) |
32 |
return false; |
|
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
33 |
|
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
34 |
$fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)'; |
4 | 35 |
$sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery |
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
36 |
WHERE $fulltext_col > 0 |
4 | 37 |
AND ( ( is_folder=1 AND folder_parent IS NULL ) OR is_folder!=1 ) |
38 |
ORDER BY is_folder DESC, score DESC, img_title ASC;"; |
|
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
39 |
if ( !$db->sql_unbuffered_query($sql) ) |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
40 |
{ |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
41 |
echo $db->get_error(); |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
42 |
return false; |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
43 |
} |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
44 |
echo "<h3>Image results</h3>"; |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
45 |
if ( $row = $db->fetchrow() ) |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
46 |
{ |
13 | 47 |
echo '<ul class="snapr-gallery">'; |
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
48 |
$renderer = new SnaprFormatter(); |
4 | 49 |
$fullpage = $paths->fullpage; |
50 |
$paths->fullpage = $paths->nslist['Special'] . 'Gallery'; |
|
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
51 |
do |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
52 |
{ |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
53 |
echo $renderer->render(false, $row, false); |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
54 |
} |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
55 |
while ( $row = $db->fetchrow() ); |
4 | 56 |
$paths->fullpage = $fullpage; |
13 | 57 |
echo '</ul><span class="menuclear"></span>'; |
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
58 |
} |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
59 |
else |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
60 |
{ |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
61 |
echo '<p>No image results.</p>'; |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
62 |
} |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
63 |
} |
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
64 |
|
13 | 65 |
function snapr_search_new_api(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list) |
66 |
{ |
|
67 |
global $db, $session, $paths, $template, $plugins; // Common objects |
|
68 |
||
69 |
if ( !defined('SNAPR_SEARCH_USING_NEW_API') ) |
|
70 |
define('SNAPR_SEARCH_USING_NEW_API', 1); |
|
71 |
||
72 |
// Let's do this all in one query |
|
73 |
$terms = array( |
|
74 |
'any' => array_merge($query['any'], $query_phrase['any']), |
|
75 |
'req' => array_merge($query['req'], $query_phrase['req']), |
|
76 |
'not' => $query['not'] |
|
77 |
); |
|
78 |
$where = array('any' => array(), 'req' => array(), 'not' => array()); |
|
79 |
$where_any =& $where['any']; |
|
80 |
$where_req =& $where['req']; |
|
81 |
$where_not =& $where['not']; |
|
82 |
$title_col = ( $case_sensitive ) ? 'img_title' : 'lcase(img_title)'; |
|
83 |
$desc_col = ( $case_sensitive ) ? 'img_desc' : 'lcase(img_desc)'; |
|
84 |
foreach ( $terms['any'] as $term ) |
|
85 |
{ |
|
86 |
$term = escape_string_like($term); |
|
87 |
if ( !$case_sensitive ) |
|
88 |
$term = strtolower($term); |
|
89 |
$where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; |
|
90 |
} |
|
91 |
foreach ( $terms['req'] as $term ) |
|
92 |
{ |
|
93 |
$term = escape_string_like($term); |
|
94 |
if ( !$case_sensitive ) |
|
95 |
$term = strtolower($term); |
|
96 |
$where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; |
|
97 |
} |
|
98 |
foreach ( $terms['not'] as $term ) |
|
99 |
{ |
|
100 |
$term = escape_string_like($term); |
|
101 |
if ( !$case_sensitive ) |
|
102 |
$term = strtolower($term); |
|
103 |
$where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'"; |
|
104 |
} |
|
105 |
if ( empty($where_any) ) |
|
106 |
unset($where_any, $where['any']); |
|
107 |
if ( empty($where_req) ) |
|
108 |
unset($where_req, $where['req']); |
|
109 |
if ( empty($where_not) ) |
|
110 |
unset($where_not, $where['not']); |
|
111 |
||
112 |
$where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')'; |
|
113 |
||
114 |
if ( isset($where_req) ) |
|
115 |
$where_req = implode(' AND ', $where_req); |
|
116 |
if ( isset($where_not) ) |
|
117 |
$where_not = implode( 'AND ', $where_not); |
|
118 |
||
119 |
$where = implode(' AND ', $where); |
|
120 |
$sql = "SELECT img_id, img_title, img_desc FROM " . table_prefix . "gallery WHERE ( $where ) AND is_folder = 0;"; |
|
121 |
||
122 |
if ( !($q = $db->sql_unbuffered_query($sql)) ) |
|
123 |
{ |
|
124 |
$db->_die('Error is in auto-generated SQL query in the Snapr plugin search module'); |
|
125 |
} |
|
126 |
||
127 |
if ( $row = $db->fetchrow() ) |
|
128 |
{ |
|
129 |
do |
|
130 |
{ |
|
131 |
$idstring = 'ns=Image;pid=' . $row['img_id']; |
|
132 |
foreach ( $word_list as $term ) |
|
133 |
{ |
|
134 |
if ( $case_sensitive ) |
|
135 |
{ |
|
136 |
if ( strstr($row['img_title'], $term) || strstr($row['img_desc'], $term) ) |
|
137 |
{ |
|
138 |
( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; |
|
139 |
} |
|
140 |
} |
|
141 |
else |
|
142 |
{ |
|
143 |
if ( strstr(strtolower($row['img_title']), strtolower($term)) || strstr(strtolower($row['img_desc']), strtolower($term)) ) |
|
144 |
{ |
|
145 |
( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
// Generate text... |
|
150 |
$text = highlight_and_clip_search_result(htmlspecialchars($row['img_desc']), $word_list); |
|
151 |
||
152 |
$preview_and_text = ' |
|
153 |
<table border="0" width="100%" cellspacing="0" cellpadding="0"> |
|
154 |
<tr> |
|
155 |
<td valign="top"> |
|
156 |
' . $text . ' |
|
157 |
</td> |
|
158 |
<td valign="top" style="text-align: right; width: 80px; padding-left: 10px;"> |
|
159 |
<a href="' . makeUrlNS('Image', $row['img_id']) . '"><img alt="[thumbnail]" src="' . makeUrlNS('Special', "GalleryFetcher/thumb/{$row['img_id']}") . '" /></a> |
|
160 |
</td> |
|
161 |
</tr> |
|
162 |
</table> |
|
163 |
'; |
|
164 |
||
165 |
// Inject result |
|
166 |
||
167 |
if ( isset($scores[$idstring]) ) |
|
168 |
{ |
|
169 |
// echo('adding image "' . $row['img_title'] . '" to results<br />'); |
|
170 |
$page_data[$idstring] = array( |
|
171 |
'page_name' => highlight_search_result(htmlspecialchars($row['img_title']), $word_list), |
|
172 |
'page_text' => $preview_and_text, |
|
173 |
'score' => $scores[$idstring], |
|
174 |
'page_note' => '[Gallery image]', |
|
175 |
'page_id' => strval($row['img_id']), |
|
176 |
'namespace' => 'Image', |
|
177 |
'page_length' => strlen($row['img_desc']), |
|
178 |
); |
|
179 |
} |
|
180 |
} |
|
181 |
while ( $row = $db->fetchrow() ); |
|
182 |
||
183 |
} |
|
184 |
} |
|
185 |
||
2
88c954d2846c
Added search functionality (WiP); removed stray .marks file
Dan
parents:
diff
changeset
|
186 |
?> |