View source
<?php
define('FAC_JSON_FILES_DIRECTORY', 'public://fac-json');
function fac_menu() {
$include_path = drupal_get_path('module', 'fac') . '/inc';
$fac_file_path = variable_get('file_public_path', conf_path() . '/files') . '/fac-json/';
$part_count = substr_count($fac_file_path, '/') + 2;
foreach (array_keys(language_list()) as $language) {
$file_path = $fac_file_path . $language . '/%/%';
$items[$file_path] = array(
'title' => 'Generate Fast Autocomplete JSON',
'page callback' => 'fac_generate_json',
'page arguments' => array(
$language,
$part_count,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'fac.json.inc',
'file path' => $include_path,
);
}
$items['admin/config/search/fac'] = array(
'title' => 'Fast Autocomplete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fac_settings_form',
),
'access arguments' => array(
'administer fac settings',
),
'type' => MENU_NORMAL_ITEM,
'description' => 'Configure the Fast Autocomplete module',
'file' => 'fac.admin.inc',
'file path' => drupal_get_path('module', 'fac') . '/inc',
);
$items['admin/config/search/fac/general'] = array(
'title' => 'General settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fac_settings_form',
),
'access arguments' => array(
'administer fac settings',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'fac.admin.inc',
'file path' => drupal_get_path('module', 'fac') . '/inc',
);
$items['admin/config/search/fac/backend'] = array(
'title' => 'Backend settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fac_backend_settings_form',
),
'access arguments' => array(
'administer fac settings',
),
'type' => MENU_LOCAL_TASK,
'file' => 'fac.admin.inc',
'file path' => drupal_get_path('module', 'fac') . '/inc',
'weight' => 5,
);
$items['admin/config/search/fac/delete'] = array(
'title' => 'Delete json files',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fac_delete_form',
),
'access arguments' => array(
'administer fac settings',
),
'type' => MENU_CALLBACK,
'file' => 'fac.admin.inc',
'file path' => drupal_get_path('module', 'fac') . '/inc',
'weight' => 10,
);
return $items;
}
function fac_permission() {
return array(
'administer fac settings' => array(
'title' => t('Administer Fast Autocomplete settings'),
'description' => t('Adminster the Fast Autocomplete module settings'),
),
);
}
function fac_library() {
$fac_path = drupal_get_path('module', 'fac');
$libraries['fastautocomplete'] = array(
'title' => 'Fast Autocomplete jQuery plugin',
'website' => 'http://drupal.org/project/fac',
'version' => 'n/a ',
'js' => array(
$fac_path . '/js/jquery.fastautocomplete.js' => array(),
),
);
return $libraries;
}
function fac_libraries_info() {
$libraries['highlight'] = array(
'name' => 'Highlight jQuery plugin',
'vendor url' => 'http://bartaz.github.io/sandbox.js/jquery.highlight.html',
'download url' => 'http://github.com/bartaz/sandbox.js/raw/master/jquery.highlight.js',
'version' => 'n/a',
'files' => array(
'js' => array(
'jquery.highlight.js',
),
),
);
return $libraries;
}
function fac_page_build(&$page) {
global $language;
$page['content']['#attached']['library'][] = array(
'fac',
'fastautocomplete',
);
$highlight_enabled = FALSE;
if (($library_check = libraries_detect('highlight')) && !empty($library_check['installed'])) {
$highlight_enabled = TRUE;
$page['content']['#attached']['libraries_load'][] = array(
'highlight',
);
}
$empty_result = variable_get('fac_empty_result', '');
drupal_alter('fac_empty_result', $empty_result);
$account = variable_get('fac_anonymous_search', TRUE) ? drupal_anonymous_user() : $GLOBALS['user'];
$fac_settings = array(
'jsonFilesPath' => base_path() . variable_get('file_public_path', conf_path() . '/files') . '/fac-json/' . $language->language . '/' . _fac_get_role_hmac($account) . '/',
'inputSelectors' => variable_get('fac_input_selectors', ''),
'keyMinLength' => variable_get('fac_key_min_length', 1),
'keyMaxLength' => variable_get('fac_key_max_length', 5),
'breakpoint' => variable_get('fac_breakpoint', 0),
'emptyResult' => $empty_result,
'allResultsLink' => variable_get('fac_all_results_link', TRUE),
'allResultsLinkThreshold' => variable_get('fac_all_results_link_threshold', -1),
'highlightEnabled' => $highlight_enabled,
'resultLocation' => variable_get('fac_result_location', FALSE),
);
$page['content']['#attached']['js'][] = array(
'data' => array(
'fac' => $fac_settings,
),
'type' => 'setting',
);
$page['content']['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'fac') . '/js/fac.js',
'type' => 'file',
);
if (variable_get('fac_use_module_css', TRUE)) {
$page['content']['#attached']['css'][] = drupal_get_path('module', 'fac') . '/css/fac.css';
}
}
function fac_preprocess_node(&$variables) {
if ($variables['view_mode'] == variable_get('fac_view_mode', 'fac')) {
$variables['display_submitted'] = FALSE;
}
}
function fac_cron() {
if ($interval = variable_get('fac_change_hmac_key_interval', 60 * 60 * 24 * 7)) {
$key_timestamp = variable_get('fac_hmac_key_timestamp', 0);
if (time() > $key_timestamp + $interval) {
variable_set('fac_hmac_key', drupal_random_key());
variable_set('fac_hmac_key_timestamp', time());
fac_delete_json_files();
}
}
if (variable_get('fac_bulk_generate_json_enabled', FALSE)) {
if (time() > variable_get('fac_bulk_generate_json_next_run', 0)) {
$queue = DrupalQueue::get('fac_bulk_generate_json');
$size = variable_get('fac_bulk_generate_json_size', 2);
$keys = _fac_create_search_array($size);
$languages = language_list('enabled');
$languages = $languages[1];
foreach ($languages as $language) {
foreach ($keys as $key) {
$data = new stdClass();
$data->language = $language->language;
$data->key = $key;
$queue
->createItem($data);
}
}
variable_set('fac_bulk_generate_json_next_run', time() + 24 * 60 * 60);
}
}
if (variable_get('fac_clean_up_files', TRUE)) {
$expire_time = strtotime(variable_get('fac_files_expire_time', '-1 day'));
fac_delete_json_files($expire_time);
}
}
function fac_cron_queue_info() {
$queues['fac_bulk_generate_json'] = array(
'worker callback' => 'fac_bulk_generate_json',
'time' => 60,
);
return $queues;
}
function fac_bulk_generate_json($data) {
fac_generate_json_for_key($data->language, $data->key);
}
function fac_generate_json_for_key($language, $key) {
$json_result = '';
$original_user = $GLOBALS['user'];
if (variable_get('fac_anonymous_search', TRUE)) {
drupal_save_session(FALSE);
$GLOBALS['user'] = drupal_anonymous_user();
}
$original_language_url = $GLOBALS['language_url'];
$language_list = language_list();
$GLOBALS['language_url'] = $language_list[$language];
$backend_service = variable_get('fac_backend_service', '');
if (!empty($backend_service)) {
$service = new $backend_service();
$result = $service
->search($key, $language);
$items = array();
if (!empty($result['items'])) {
foreach ($result['items'] as $item_info) {
$entities = entity_load($item_info['entity_type'], array(
$item_info['etid'],
));
$entity = reset($entities);
$entity_views = entity_view($item_info['entity_type'], array(
$entity,
), variable_get('fac_view_mode', 'fac'), $language);
$entity_view = reset($entity_views);
$items[] = render($entity_view);
}
}
$object = new stdClass();
$object->items = $items;
$json_result = json_encode($object);
$directory = FAC_JSON_FILES_DIRECTORY . '/' . $language . '/' . _fac_get_role_hmac($GLOBALS['user']);
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
$destination = $directory . '/' . $key . '.json';
file_unmanaged_save_data($json_result, $destination, FILE_EXISTS_REPLACE);
}
}
$GLOBALS['language_url'] = $original_language_url;
$GLOBALS['user'] = $original_user;
drupal_save_session(TRUE);
return $json_result;
}
function fac_delete_json_files($expire_time = '') {
if (empty($expire_time)) {
file_unmanaged_delete_recursive(FAC_JSON_FILES_DIRECTORY);
}
else {
$json_files = file_scan_directory(FAC_JSON_FILES_DIRECTORY, '/.*\\.json$/');
foreach ($json_files as $json_file) {
if (filectime($json_file->uri) < $expire_time) {
file_unmanaged_delete($json_file->uri);
}
}
}
}
function fac_entity_info_alter(&$entity_info) {
foreach ($entity_info as &$info) {
$info['view modes']['fac'] = array(
'label' => t('Fast Autocomplete'),
'custom settings' => FALSE,
);
}
}
function fac_get_service_info($id = NULL) {
$services =& drupal_static(__FUNCTION__);
if (!isset($services)) {
$services = array();
foreach (module_implements('fac_service_info') as $module) {
$function = $module . '_fac_service_info';
if (function_exists($function)) {
$new_services = $function();
if (isset($new_services) && is_array($new_services)) {
foreach ($new_services as $service => $info) {
$new_services[$service] += array(
'module' => $module,
);
}
}
$services += $new_services;
}
}
}
if (isset($id)) {
return isset($services[$id]) ? $services[$id] : NULL;
}
return $services;
}
function fac_modules_enabled() {
drupal_static_reset('fac_get_service_info');
}
function fac_modules_disabled() {
drupal_static_reset('fac_get_service_info');
}
function fac_fac_service_info() {
$services['fac_basic_title_search_service'] = array(
'name' => t('Basic title search service'),
'class' => 'Drupal\\fac\\SearchService\\BasicTitleSearchService',
);
if (module_exists('search')) {
$services['fac_core_search_service'] = array(
'name' => t('Core search service'),
'class' => 'Drupal\\fac\\SearchService\\CoreSearchService',
);
}
if (module_exists('search_api')) {
$services['fac_search_api_search_service'] = array(
'name' => t('Search API search service'),
'class' => 'Drupal\\fac\\SearchService\\SearchApiSearchService',
);
}
if (module_exists('apachesolr')) {
$services['fac_apachesolr_search_service'] = array(
'name' => t('Apachesolr search service'),
'class' => 'Drupal\\fac\\SearchService\\ApachesolrSearchService',
);
}
return $services;
}
function fac_stage_file_proxy_excluded_paths_alter(&$excluded_paths) {
$excluded_paths[] = '/fac-json';
}
function _fac_create_search_array($size) {
$search_array = array();
$letters = range('a', 'z');
$numbers = range('0', '9');
$chars = array_merge($letters, $numbers);
$i = 1;
while ($i <= $size) {
$temp_array = _fac_keys_sampling($chars, $i);
$search_array = array_merge($search_array, $temp_array);
$i++;
}
return $search_array;
}
function _fac_keys_sampling(array $chars, $size, $combinations = array()) {
if (empty($combinations)) {
$combinations = $chars;
}
if ($size == 1) {
return $combinations;
}
$new_combinations = array();
foreach ($combinations as $combination) {
foreach ($chars as $char) {
$new_combinations[] = $combination . $char;
}
}
return _fac_keys_sampling($chars, $size - 1, $new_combinations);
}
function _fac_get_role_hmac($account) {
$rids = array_keys($account->roles);
sort($rids);
if ($account->uid == 1) {
$rids[] = -1;
}
return drupal_hmac_base64('fac-' . implode('|', $rids), _fac_get_hmac_key());
}
function _fac_get_hmac_key() {
if (!($key = variable_get('fac_hmac_key', FALSE))) {
$key = drupal_random_key();
variable_set('fac_hmac_key', $key);
variable_set('fac_hmac_key_timestamp', REQUEST_TIME);
}
return $key;
}