View source
<?php
include_once 'scald_index.features.inc';
function scald_index_help($path, $arg) {
$result = '';
switch ($path) {
case 'admin/help#scald_index':
$result = '<p>' . t("This module maintains an index of atoms referenced in nodes for both atoms referenced through an atom reference field and through SAS tokens within a node's body field markup. Navigate to Structure > Scald > Atoms index to get an overview of all atoms with their node references.") . '</p>';
break;
}
return $result;
}
function scald_index_menu() {
$items = array();
$items['admin/structure/scald/atoms-index'] = array(
'title' => 'Atoms index',
'description' => 'View the index of atoms.',
'access arguments' => array(
'administer scald index',
),
'page callback' => 'views_embed_view',
'page arguments' => array(
'scald_atoms_index',
'page_1',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/structure/scald/atoms-index/view'] = array(
'title' => 'View atoms index',
'description' => 'View the index of atoms.',
'access arguments' => array(
'administer scald index',
),
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/scald/atoms-index/rebuild'] = array(
'title' => 'Rebuild index',
'access arguments' => array(
'administer scald index',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'scald_index_rebuild_confirm_form',
),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function scald_index_permission() {
return array(
'administer scald index' => array(
'title' => t('Administer Scald Index'),
'description' => t('Perform administration tasks for Scald Index.'),
),
);
}
function scald_index_node_insert($node) {
scald_index_build_node_index($node);
}
function scald_index_node_update($node) {
scald_index_delete_node_index($node);
scald_index_build_node_index($node);
}
function scald_index_node_delete($node) {
scald_index_delete_node_index($node);
}
function scald_index_scald_atom_delete($atom) {
db_delete('scald_index')
->condition('sid', $atom->sid)
->execute();
}
function scald_index_field_delete_instance($instance) {
db_delete('scald_index')
->condition('field_name', $instance['field_name'])
->condition('type', $instance['bundle'])
->execute();
}
function scald_index_build_node_index($node) {
$status = NULL;
$type = NULL;
if (!empty($node->original)) {
$status = (int) (!empty($node->status) || !isset($node->status) && !empty($node->original->status));
$type = $node->original->type;
}
else {
$status = (int) (!empty($node->status));
$type = $node->type;
}
$sid_all = array();
$instances = field_info_instances('node', $node->type);
foreach ($instances as $field_name => $instance) {
$field = field_info_field($field_name);
$analyse_field = FALSE;
if ($field['module'] == 'atom_reference' && $field['storage']['type'] == 'field_sql_storage') {
$analyse_field = TRUE;
}
if (isset($instance['settings']['mee_enabled']) && $instance['settings']['mee_enabled'] or isset($instance['settings']['dnd_enabled']) && $instance['settings']['dnd_enabled']) {
$analyse_field = TRUE;
}
if ($analyse_field) {
foreach (field_available_languages('node', $field) as $langcode) {
$values = field_get_items('node', $node, $field_name, $langcode);
if (!empty($values)) {
foreach ($values as $value) {
if (isset($value['sid'])) {
$sid_all[] = array(
'sid' => $value['sid'],
'field_name' => $field_name,
);
}
if (isset($value['value'])) {
$extracted_sids = scald_index_included($value['value']);
foreach ($extracted_sids as $sid) {
$sid_all[] = array(
'sid' => $sid,
'field_name' => $field_name,
);
}
}
}
}
}
}
}
drupal_alter('scald_index_node_atoms', $node, $sid_all);
if (!empty($sid_all)) {
$query = db_insert('scald_index')
->fields(array(
'nid',
'sid',
'status',
'type',
'field_name',
));
foreach ($sid_all as $sid_infos) {
$query
->values(array(
'nid' => $node->nid,
'sid' => $sid_infos['sid'],
'status' => $status,
'type' => $type,
'field_name' => $sid_infos['field_name'],
));
}
$query
->execute();
module_invoke_all('scald_index_after_build_node_index', $node, $sid_all);
}
}
function scald_index_delete_node_index($node) {
db_delete('scald_index')
->condition('nid', $node->nid)
->execute();
}
function scald_index_rebuild() {
db_truncate('scald_index')
->execute();
$nids = db_query('SELECT nid FROM {node}')
->fetchCol();
$step = 5;
$current = 0;
$i = 0;
$batch_operations = array();
foreach ($nids as $nid) {
if (!isset($batch_operations[$i])) {
$batch_operations[$i] = array(
'scald_index_rebuild_run',
array(
array(
$nid,
),
),
);
}
else {
$batch_operations[$i][1][0][] = $nid;
}
$current++;
if ($current == $step) {
$current = 0;
$i++;
}
}
batch_set(array(
'title' => t('Adding existing atom/node references'),
'operations' => $batch_operations,
'init_message' => t('The atom/node association is getting added.'),
'progress_message' => t('Processed @current groups of :step nodes out of @total.', array(
':step' => $step,
)),
'finished' => 'scald_index_rebuild_finished',
));
batch_process('admin/structure/scald/atoms-index');
}
function scald_index_rebuild_run($nids) {
$nodes = node_load_multiple($nids, array());
foreach ($nodes as $current_node) {
scald_index_build_node_index($current_node);
}
}
function scald_index_rebuild_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Rebuilding index complete.'));
}
else {
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
));
drupal_set_message($message, 'error');
}
}
function scald_index_get_nodes($atom, $lang = NULL) {
$nids = array();
$query = db_select('scald_index', 'si');
$query
->join('node', 'n', 'n.nid = si.nid');
$query
->fields('n', array(
'nid',
));
$query
->condition('si.sid', $atom);
$query
->condition('n.status', 1);
if (!is_null($lang)) {
$query
->condition('n.language', $lang);
}
$result = $query
->execute();
foreach ($result as $r) {
$nids[] = $r->nid;
}
return $nids;
}
function scald_index_get_atoms($node) {
$sids = array();
$query = db_select('scald_index', 'si');
$query
->join('node', 'n', 'n.nid = si.nid');
$query
->fields('si', array(
'sid',
));
$query
->condition('n.nid', $node);
$result = $query
->execute();
foreach ($result as $r) {
$sids[] = $r->sid;
}
return $sids;
}
function scald_index_rebuild_confirm_form($form, &$form_state) {
$text = array(
t('Delete and rebuild scald index.'),
t('Do you really want to rebuild this index?'),
t('This will delete the complete scald index and rebuilt it. This action cannot be undone.'),
t('The index has been successfully rebuilt.'),
);
$form = array(
'message' => array(
'#type' => 'value',
'#value' => $text[3],
),
);
$desc = "<h3>{$text[1]}</h3><p>{$text[2]}</p>";
return confirm_form($form, $text[0], "admin/structure/scald/atoms-index", $desc);
}
function scald_index_rebuild_confirm_form_submit(array $form, array &$form_state) {
scald_index_rebuild();
}
function scald_index_included($string) {
$atoms = array();
if (preg_match_all(SCALD_SAS_MATCH_PATTERN, $string, $sas_atoms)) {
$atoms = array_merge($atoms, $sas_atoms[1]);
}
if (preg_match_all('/<div.*data-scald-sid=\\"(\\d+)\\".*>/', $string, $div_atoms)) {
$atoms = array_merge($atoms, $div_atoms[1]);
}
return $atoms;
}