-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFSearch.php
171 lines (155 loc) · 6.05 KB
/
FSearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* The FSearch plugin provides MySQL full-text search capabilities.
*
* @package frog
* @subpackage plugin.fsearch
*
* @author Andrew Crookston <[email protected]>
* @version 0.1.0
* @since Frog version 0.9.5
* @license http://www.gnu.org/licenses/gpl.html GPL License
* @copyright Andrew Crookston, 2009
*/
/**
* class FSearch
*
* MySQL full-text search for page-parts and titles.
*
* @author Andrew Crookston <[email protected]>
* @since Frog version 0.9.5
*/
class FSearch extends Record {
const TABLE_NAME = 'page';
/**
* class FSearch
*
* Search and retrieves Page objects.
*
* @author Andrew Crookston <[email protected]>
* @since Frog version 0.9.5
* @return Array of Page objects or empty Array
*/
public static function search($args = null) {
// Collect attributes...
$where = isset($args['where']) ? trim($args['where']) : '';
$search = isset($args['search']) ? trim($args['search']) : '';
$order_by = isset($args['order']) ? trim($args['order']) : '';
$offset = isset($args['offset']) ? (int) $args['offset'] : 0;
$limit = isset($args['limit']) ? (int) $args['limit'] : 0;
// Prepare query parts
$order_by_string = empty($order_by) ? '' : "ORDER BY $order_by";
$limit_string = $limit > 0 ? "LIMIT $offset, $limit" : '';
// Prepare the WHERE string (It's WHERE all the magic happens) ;)
$where_string = empty($where) ? 'WHERE ' : "WHERE $where AND "; // Allow custom queries
$where_string .= "(page.is_fsearchable = 1 AND page_part.name = 'body' AND page.needs_login = 2 " // Security
. ' AND MATCH (page_part.content_fsearchable) AGAINST (\''.(string)$search.'\'))'; // full text
$tablename = self::tableNameFromClassName('FSearch');
$tablename_user = self::tableNameFromClassName('User');
$tablename_part = self::tableNameFromClassName('PagePart');
// Prepare SQL
$sql = "SELECT page.*, creator.name AS created_by_name, updator.name AS updated_by_name FROM $tablename AS page".
" LEFT JOIN $tablename_user AS creator ON page.created_by_id = creator.id".
" LEFT JOIN $tablename_user AS updator ON page.updated_by_id = updator.id".
" LEFT JOIN $tablename_part AS page_part ON page.id = page_part.page_id".
" $where_string $order_by_string $limit_string";
$stmt = self::$__CONN__->prepare($sql);
$stmt->execute();
$objects = array();
while ($obj = $stmt->fetchObject()) {
$parent = self::findParentById($obj->parent_id);
$obj->part = get_parts($obj->id);
$objects[] = new Page($obj, $parent);
}
return $objects;
}
/**
* method findPage
*
* This method a copy of model Page::find
* It's a copy because the model Page is not publicly available in the front-end.
* Some modifications are done to adapt the method to the front-end environment.
*
* @author Philippe Archambault <[email protected]>
* @author Martijn van der Kleijn <[email protected]>
* @since Frog version 0.1
*/
public static function find($args) {
// Collect attributes...
$where = isset($args['where']) ? trim($args['where']) : '';
$order_by = isset($args['order']) ? trim($args['order']) : '';
$offset = isset($args['offset']) ? (int) $args['offset'] : 0;
$limit = isset($args['limit']) ? (int) $args['limit'] : 0;
// Prepare query parts
$where_string = empty($where) ? '' : "WHERE $where";
$order_by_string = empty($order_by) ? '' : "ORDER BY $order_by";
$limit_string = $limit > 0 ? "LIMIT $offset, $limit" : '';
$tablename = self::tableNameFromClassName('FSearch');
$tablename_user = self::tableNameFromClassName('User');
// Prepare SQL
$sql = "SELECT page.*, creator.name AS created_by_name, updator.name AS updated_by_name FROM $tablename AS page".
" LEFT JOIN $tablename_user AS creator ON page.created_by_id = creator.id".
" LEFT JOIN $tablename_user AS updator ON page.updated_by_id = updator.id".
" $where_string $order_by_string $limit_string";
$stmt = self::$__CONN__->prepare($sql);
$stmt->execute();
if ($limit == 1) {
$obj = $stmt->fetchObject();
$parent = self::findParentById($obj->parent_id);
return new Page($obj, $parent);
} else {
$objects = array();
while ($obj = $stmt->fetchObject()) {
$parent = self::findParentById($obj->parent_id);
$objects[] = new Page($obj, $parent);
}
return $objects;
}
}
/**
* method findById
*
* This method a copy of model Page::findById
* It's a copy because the model Page is not publicly available in the front-end.
*
* @author Philippe Archambault <[email protected]>
* @author Martijn van der Kleijn <[email protected]>
* @since Frog version 0.1
*/
public static function findById($id) {
return self::find(array(
'where' => 'page.id='.(int)$id,
'limit' => 1
));
}
/**
* method findParentById
*
* Methods finds the parent.
* Will return false if id == 0 since there is no parent for page id 0
*
* @author Andrew Crookston <[email protected]>
* @since Frog version 0.9.5
*/
public static function findParentById($id) {
if ($id == 0) { return false; }
return self::find(array(
'where' => 'page.id='.(int)$id,
'limit' => 1
));
}
}