| 1 |
<?php
|
| 2 |
|
| 3 |
/*
|
| 4 |
* Copyright (C) October 2006 Jonathan Hendler
|
| 5 |
*
|
| 6 |
* This program is free software; you can redistribute it and/or
|
| 7 |
* modify it under the terms of the GNU General Public License
|
| 8 |
* as published by the Free Software Foundation; either version 2
|
| 9 |
* of the License, or (at your option) any later version.
|
| 10 |
*
|
| 11 |
* This program is distributed in the hope that it will be useful,
|
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 |
* GNU General Public License for more details.
|
| 15 |
*
|
| 16 |
* You should have received a copy of the GNU General Public License
|
| 17 |
* along with this program; if not, write to the Free Software
|
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
| 19 |
*
|
| 20 |
* The authors can be contacted at:
|
| 21 |
* - jonathan [at] civicactions (dot) com
|
| 22 |
*
|
| 23 |
* @license http://www.affero.org/oagpl.html
|
| 24 |
* @link http://drupal.org/project/semantic_search
|
| 25 |
*/
|
| 26 |
|
| 27 |
/**
|
| 28 |
* This file is the set of functions needed for making javascript and XMLRPC calls
|
| 29 |
* to SEMANTIC_SEARCH
|
| 30 |
* */
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implementation of hook_help().
|
| 34 |
*/
|
| 35 |
function semantic_search_rpc_help($section) {
|
| 36 |
switch ($section) {
|
| 37 |
case 'admin/modules#description' :
|
| 38 |
return ('<strong>SEMANTIC SEARCH:</strong> enables AJAX functionality for SEMANTIC_SEARCH (requires SEMANTIC_SEARCH)');
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_menu
|
| 44 |
*/
|
| 45 |
function semantic_search_rpc_menu($may_cache) {
|
| 46 |
|
| 47 |
$items = array ();
|
| 48 |
|
| 49 |
if ($may_cache) {
|
| 50 |
$items[] = array (
|
| 51 |
'path' => SEMANTIC_SEARCH_JSRPC_PATH_PREFIX,
|
| 52 |
'title' => t('dynamic update of search'
|
| 53 |
), 'access' => user_access('search semantic_search'),
|
| 54 |
'type' => MENU_CALLBACK,
|
| 55 |
'callback' => '_semantic_search_rpc_search');
|
| 56 |
}
|
| 57 |
|
| 58 |
if (!$may_cache){
|
| 59 |
$searches = _semantic_search_get_all_search_form_vars();
|
| 60 |
|
| 61 |
foreach ($searches as $key => $search) {
|
| 62 |
$search_name = $search['name'];
|
| 63 |
$search_path = $search['path'];
|
| 64 |
$search_id = $search['id'];
|
| 65 |
$search_type = $search['id'];
|
| 66 |
|
| 67 |
$items[] = array (
|
| 68 |
'path' => SEMANTIC_SEARCH_JSRPC_ADMIN_PATH_PREFIX.'/'.$search_name,
|
| 69 |
'title' => t($search['title']),
|
| 70 |
'description' => t($search['description']),
|
| 71 |
'callback' => '_semantic_search_rpc_search',
|
| 72 |
'callback arguments' => array (
|
| 73 |
$search_name,
|
| 74 |
),
|
| 75 |
'access' => user_access($search_name . ': access search'),
|
| 76 |
'type' => MENU_CALLBACK);
|
| 77 |
}// end foreach
|
| 78 |
}
|
| 79 |
|
| 80 |
if ($may_cache) {
|
| 81 |
$items[] = array (
|
| 82 |
'path' => SEMANTIC_SEARCH_JSRPC_ADMIN_PATH_PREFIX,
|
| 83 |
'title' => t('builds search - may take a long time!!'),
|
| 84 |
'access' => user_access('search semantic_search'),
|
| 85 |
'type' => MENU_CALLBACK,
|
| 86 |
'callback' => '_semantic_search_sync_cache');
|
| 87 |
}
|
| 88 |
|
| 89 |
return $items;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* this function runs for a long time
|
| 94 |
* */
|
| 95 |
function _semantic_search_sync_cache() {
|
| 96 |
global $_semantic_search_search;
|
| 97 |
if (empty ($_semantic_search_search)) {
|
| 98 |
_SEMANTIC_SEARCH_INIT();
|
| 99 |
}
|
| 100 |
|
| 101 |
if (!user_access("administer semantic_search")) {
|
| 102 |
print message_admin_access();
|
| 103 |
} else {
|
| 104 |
switch($_REQUEST['action']){
|
| 105 |
case 'start':
|
| 106 |
$started = $_semantic_search_search->sonia->io->getCache('SEMANTIC_SEARCH_CACHE_STARTED');
|
| 107 |
if (empty($started)){
|
| 108 |
if (!SONIA_USE_CACHE){
|
| 109 |
print ' FAILED: Please set SONIA_USE_CACHE to "true" in sonia.const.inc.';
|
| 110 |
die();
|
| 111 |
}
|
| 112 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_STARTED','1');// = true;
|
| 113 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_CURRENT','0');
|
| 114 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_COMPLETED','0');
|
| 115 |
//trigger process here
|
| 116 |
|
| 117 |
$_semantic_search_search->sonia->io->startBuildingCache($_semantic_search_search);
|
| 118 |
print t('');
|
| 119 |
}
|
| 120 |
break;
|
| 121 |
case 'stop':
|
| 122 |
$_semantic_search_search->sonia->io->clearCache('SEMANTIC_SEARCH_CACHE_STARTED');
|
| 123 |
print ''; exit();
|
| 124 |
break;
|
| 125 |
case 'status':
|
| 126 |
$completed = $_semantic_search_search->sonia->io->getCache('SEMANTIC_SEARCH_CACHE_COMPLETED');
|
| 127 |
$csize = $_semantic_search_search->sonia->io->getCache('SEMANTIC_SEARCH_CACHE_P_SIZE');
|
| 128 |
$current = $_semantic_search_search->sonia->io->getCache('SEMANTIC_SEARCH_CACHE_CURRENT');
|
| 129 |
//if ($completed < $csize){
|
| 130 |
//if new cache completed add text
|
| 131 |
$started = $_semantic_search_search->sonia->io->getCache('SEMANTIC_SEARCH_CACHE_STARTED');
|
| 132 |
if ( empty($started) ){
|
| 133 |
print '';
|
| 134 |
} else {
|
| 135 |
if ($current != $completed){
|
| 136 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_CURRENT', $completed); //$_COOKIE['SEMANTIC_SEARCH_CACHE_CURRENT'] = $_COOKIE['SEMANTIC_SEARCH_CACHE_COMPLETED'];
|
| 137 |
$size = $_semantic_search_search->sonia->io->getCacheSize();
|
| 138 |
print $size .' done of about '.$csize.' total. ('.$completed.' queries) ';
|
| 139 |
} else {
|
| 140 |
print '.';
|
| 141 |
}
|
| 142 |
|
| 143 |
}
|
| 144 |
|
| 145 |
// } else {
|
| 146 |
// $_semantic_search_search->sonia->io->clearCache('SEMANTIC_SEARCH_CACHE_STARTED');
|
| 147 |
// print ''; exit();
|
| 148 |
// }
|
| 149 |
//
|
| 150 |
|
| 151 |
break;
|
| 152 |
case 'checkClear':
|
| 153 |
$size = $_semantic_search_search->sonia->io->getCacheSize();
|
| 154 |
print "You will remove $size searches from the cache. \nAre you sure you wish to proceed?";
|
| 155 |
exit;
|
| 156 |
break;
|
| 157 |
case 'startClear':
|
| 158 |
$size = $_semantic_search_search->sonia->io->clearCache();
|
| 159 |
print "cache cleared.";
|
| 160 |
exit;
|
| 161 |
break;
|
| 162 |
case 'check':
|
| 163 |
default:
|
| 164 |
$ccount = $_semantic_search_search->sonia->io->getCacheSize();
|
| 165 |
$psize = $_semantic_search_search->sonia->io->getPredictedCacheSize($_semantic_search_search);
|
| 166 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_COMPLETED', $ccount);
|
| 167 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_COUNT', $ccount);
|
| 168 |
$_semantic_search_search->sonia->io->putCache('SEMANTIC_SEARCH_CACHE_P_SIZE', $psize);
|
| 169 |
$_semantic_search_search->sonia->io->clearCache('SEMANTIC_SEARCH_CACHE_STARTED');
|
| 170 |
print "This process will create ".($psize).' entries.'.
|
| 171 |
"\n".'Note: The number of entries in the cache are about '.$ccount.'.' .
|
| 172 |
"\n".' Do you want to proceed?' .
|
| 173 |
"\n".'This may take a long time, but does not need to complete.'
|
| 174 |
.' Also, if a search is already cached, the existing cache will be used.';
|
| 175 |
exit;
|
| 176 |
break;
|
| 177 |
}
|
| 178 |
}
|
| 179 |
exit;
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* returns the search results as a string of html
|
| 184 |
* */
|
| 185 |
function _semantic_search_rpc_search($search_name = null) {
|
| 186 |
if ($search_name !== null){
|
| 187 |
$data = _semantic_search_get_search_form_vars($search_name);
|
| 188 |
$form = _semantic_search_load_search($search_name, null,null, false,true);
|
| 189 |
/**
|
| 190 |
* base URL for semantic_search in drupal.
|
| 191 |
* */
|
| 192 |
define('SEMANTIC_SEARCH_BASE_URL', $data['path']);
|
| 193 |
|
| 194 |
_semantic_search_set_constants();
|
| 195 |
}
|
| 196 |
|
| 197 |
|
| 198 |
global $_semantic_search_search;
|
| 199 |
if (empty ($_semantic_search_search))
|
| 200 |
_SEMANTIC_SEARCH_INIT();
|
| 201 |
$_semantic_search_search_hash = 'rpc' . $_semantic_search_search->search_hash;
|
| 202 |
|
| 203 |
$result = (SONIA_USE_CACHE) ? $_semantic_search_search->sonia->io->getCache($_semantic_search_search_hash) : null;
|
| 204 |
|
| 205 |
if (SONIA_USE_CACHE && $result !== null) {
|
| 206 |
print $result;
|
| 207 |
exit ();
|
| 208 |
} else {
|
| 209 |
$_semantic_search_search_results = _semantic_search_SearchResults($_semantic_search_search, false);
|
| 210 |
$_semantic_search_search_form = theme("semantic_search_search", $_semantic_search_search,$form);
|
| 211 |
$my_query = _semantic_search_SearchNavigation($_semantic_search_search, false, false, '');
|
| 212 |
$pagination = theme("semantic_search_search_results_pagination", $_semantic_search_search->searchPaginaton());
|
| 213 |
$number_of_search_results = isset ($_semantic_search_search->number_of_search_results) ? $_semantic_search_search->number_of_search_results : 0;
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
$result = rawurlencode('[' .
|
| 218 |
_semantic_search_enc_js($_semantic_search_search_results) . ',' . _semantic_search_enc_js($_semantic_search_search_form) . ',' . _semantic_search_enc_js($my_query) . ',' . _semantic_search_enc_js($number_of_search_results) . ',' . _semantic_search_enc_js($pagination) . ']');
|
| 219 |
|
| 220 |
if (SONIA_USE_CACHE)
|
| 221 |
$_semantic_search_search->sonia->io->putCache($_semantic_search_search_hash, $result);
|
| 222 |
|
| 223 |
print $result;
|
| 224 |
exit ();
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* help encode strings for javascript arrays
|
| 230 |
* */
|
| 231 |
function _semantic_search_enc_js($string) {
|
| 232 |
$string = addslashes($string);
|
| 233 |
$string = rawurlencode($string);
|
| 234 |
return '"' . $string . '"';
|
| 235 |
}
|
| 236 |
|
| 237 |
//TODO ?
|
| 238 |
//http://bryght.com/xml-rpc-hacks
|
| 239 |
//drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|