Commit 321de921 authored by generalredneck's avatar generalredneck

Adding Queue logic to replace the array logic for data.

parent 9ac8d938
<?php
/**
* @file
*/
/**
*
*/
class views_natural_sort_handler_sort extends views_handler_sort {
function init(&$view, &$options) {
/**
*
*/
public function init(&$view, &$options) {
parent::init($view, $options);
$this->natural_sort = substr($this->options['order'],0,1) == 'N';
$this->natural_sort = substr($this->options['order'], 0, 1) == 'N';
}
/**
* Provide a list of options for the default sort form.
* Should be overridden by classes that don't override sort_form
* Should be overridden by classes that don't override sort_form.
*/
function sort_options() {
public function sort_options() {
return array(
'ASC' => t('Sort ascending'),
'DESC' => t('Sort descending'),
......@@ -19,7 +30,10 @@ class views_natural_sort_handler_sort extends views_handler_sort {
);
}
function query() {
/**
*
*/
public function query() {
// If this field isn't being used as a Natural Sort Field, move along
// nothing to see here.
if (!$this->natural_sort) {
......@@ -27,7 +41,7 @@ class views_natural_sort_handler_sort extends views_handler_sort {
return;
}
// If someone has submitted the exposed form, lets grab it here
// If someone has submitted the exposed form, lets grab it here.
if ($this->options['exposed'] && $this->view->exposed_data['sort_order']) {
$temporder = $this->view->exposed_data['sort_order'];
}
......@@ -47,7 +61,10 @@ class views_natural_sort_handler_sort extends views_handler_sort {
$this->query->add_orderby($vns_alias, 'content', $order);
}
function natural_sort_join() {
/**
*
*/
public function natural_sort_join() {
$join = new views_join();
$table_data = views_fetch_data($this->table);
$join->definition = array(
......@@ -64,13 +81,17 @@ class views_natural_sort_handler_sort extends views_handler_sort {
'field' => 'field',
'value' => $this->real_field,
),
)
),
);
$join->construct();
$join->adjusted = TRUE;
return $join;
}
function admin_summary() {
/**
*
*/
public function admin_summary() {
if (!empty($this->options['exposed'])) {
return t('Exposed');
}
......@@ -79,17 +100,22 @@ class views_natural_sort_handler_sort extends views_handler_sort {
case 'asc':
default:
return t('asc');
break;
break;
case 'DESC':
case 'desc':
return t('desc');
break;
break;
case 'NASC':
return t('natural asc');
break;
break;
case 'NDESC':
return t('natural asc');
break;
break;
}
}
}
<?php
/**
* @file
*/
/**
*
*/
class views_natural_sort_handler_sort_text_field extends views_natural_sort_handler_sort {
function natural_sort_join() {
/**
*
*/
public function natural_sort_join() {
$join = new views_join();
$other_join = $this->get_join();
$table_data = views_fetch_data($other_join->definition['left_table']);
......@@ -19,8 +30,8 @@ class views_natural_sort_handler_sort_text_field extends views_natural_sort_hand
'left_field' => 'entity_id',
'left_table' => $this->table_alias,
'extra' => $vns_alias . '.delta = ' . $this->table_alias . '.delta AND ' .
$vns_alias . ".entity_type = '" . $table_data['table']['entity type'] . "' AND " .
$vns_alias . ".field = '" . preg_replace('/_value$/', '', $this->field) . "'",
$vns_alias . ".entity_type = '" . $table_data['table']['entity type'] . "' AND " .
$vns_alias . ".field = '" . preg_replace('/_value$/', '', $this->field) . "'",
/*'extra' => array(
"views_natural_sort.delta = $this->table_alias.delta",
array(
......@@ -37,4 +48,5 @@ class views_natural_sort_handler_sort_text_field extends views_natural_sort_hand
$join->adjusted = TRUE;
return $join;
}
}
<?php
/**
* @file
*/
/**
* Implements hook_install().
*/
/**
*
*/
function views_natural_sort_test_install() {
node_types_rebuild();
$types = node_type_get_types();
......
<?php
/**
* @file
*/
/**
* Implement hook_form()
*/
/**
*
*/
function views_natural_sort_test_form($node, $form_state) {
return node_content_form($node, $form_state);
}
......@@ -78,3 +86,35 @@ function views_natural_sort_test_views_default_views() {
$views[$view->name] = $view;
return $views;
}
/**
*
*/
function views_natural_sort_test_views_create_test_content() {
$titles = array(
'1 apple',
'2 apples',
'10 apples',
'-1 apples',
'-10 apples',
'-2 appels',
'-3.550 apples',
'-3.5501 apples',
'3.5501 apples',
'3.550 apples',
'A(Z',
'A[B',
'A\\C',
'A Stripped Zebra',
'Oklahoma',
'The King And I',
);
foreach ($titles as $title) {
$node = new stdClass();
$node->type = 'views_natural_sort_test_content';
$node->title = $title;
node_object_prepare($node);
$node = node_submit($node);
node_save($node);
}
}
<?php
/**
* @file
* Callbacks for managing Views Natural Sort.
......@@ -42,7 +43,7 @@ function views_natural_sort_rebuild_index_form() {
}
/**
* Form callback for the Views Natural Sort settings page
* Form callback for the Views Natural Sort settings page.
*
* Allows the removal of specific words and symbols from all your titles.
*/
......@@ -94,9 +95,9 @@ function views_natural_sort_settings_form() {
* Submit handler that saves custom word handlers and other settings.
*/
function views_natural_sort_settings_form_submit($form, &$form_state) {
$beginning_words = explode(',',$form_state['values']['beginning_words']);
$beginning_words = explode(',', $form_state['values']['beginning_words']);
array_walk($beginning_words, create_function('&$val', '$val = trim($val);'));
$words = explode(',',$form_state['values']['words']);
$words = explode(',', $form_state['values']['words']);
array_walk($words, create_function('&$val', '$val = trim($val);'));
$symbols = trim($form_state['values']['symbols']);
......@@ -115,17 +116,22 @@ function views_natural_sort_rebuild_index_submit() {
views_natural_sort_rebuild_index_batch_set();
}
/**
* Sets up the batch job for reindexing all or specified VNS entry types.
*/
function views_natural_sort_rebuild_index_batch_set(array $entry_types = array()) {
if (empty($entry_types)) {
$entry_types = module_invoke_all('views_natural_sort_get_entry_types');
}
$operations = array();
foreach($entry_types as $entry_type) {
$operations[] = array('views_natural_sort_rebuild_index', array($entry_type));
foreach ($entry_types as $entry_type) {
// Queue up all the data that needs to be rebuilt.
module_invoke_all('views_natural_sort_queue_rebuild_data', $entry_type);
}
// Run the queue.
$batch = array(
'operations' => $operations,
'operations' => array(array('views_natural_sort_rebuild_index', array())),
'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
'finished' => 'views_natural_sort_rebuild_index_finished',
'file' => drupal_get_path('module', 'views_natural_sort') . '/views_natural_sort.admin.inc',
......@@ -137,46 +143,29 @@ function views_natural_sort_rebuild_index_batch_set(array $entry_types = array()
/**
* Batch API callback for rebuild_index.
*/
function views_natural_sort_rebuild_index($entry_type, &$context) {
function views_natural_sort_rebuild_index(&$context) {
$queue = views_natural_sort_get_queue();
// Alias sandbox for easier referencing.
$sandbox = &$context['sandbox'];
// Initialize our context.
if (!isset($context['results']['entries'])) {
$context['results']['entries'] = 0;
}
if (!isset($sandbox['max'])) {
// Hook for modules to implement and return data that views_natural_sort can
// store as an index for that module's entries.
$sandbox['entries'] = module_invoke_all('views_natural_sort_get_rebuild_data', $entry_type);
$sandbox['progress'] = 0;
$sandbox['max'] = count($sandbox['entries']) - 1;
$sandbox['total'] = count($sandbox['entries']);
// Alias results for easier referencing.
$results = &$context['results'];
if (empty($sandbox)) {
$sandbox['current'] = 0;
if ($sandbox['total'] == 0) {
$context['finished'] = 1;
return;
$sandbox['max'] = $queue->numberOfItems();
$sandbox['items_per_batch'] = variable_get('views_natural_sort_rebuild_items_per_batch', '500');
}
for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
$item = $queue->claimItem(10);
if ($item) {
views_natural_sort_store($item->data);
$queue->deleteItem($item);
}
$sandbox['current']++;
}
$results = array_slice($sandbox['entries'], $sandbox['current'], variable_get('views_natural_sort_rebuild_items_per_batch', '500'));
$entity_type = '';
$field = '';
foreach ($results as $row) {
views_natural_sort_store($row);
++$sandbox['progress'];
$sandbox['current'] = $sandbox['progress'];
$entity_type = $row['entity_type'];
$field = $row['field'];
++$context['results']['entries'];
$results['entries'] = $sandbox['current'];
if ($sandbox['current'] != $sandbox['max']) {
$context['finished'] = $sandbox['current'] / $sandbox['max'];
}
$context['message'] = t('Processing %entity_type %field', array('%entity_type' => $entity_type, '%field' => $field));
$context['finished'] = $sandbox['progress'] / $sandbox['total'];
}
/**
......@@ -184,7 +173,7 @@ function views_natural_sort_rebuild_index($entry_type, &$context) {
*/
function views_natural_sort_rebuild_index_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Index update has completed.'));
drupal_set_message(t('Index rebuild has completed.'));
drupal_set_message(t('Indexed %count.', array(
'%count' => format_plural($results['entries'], '1 entry', '@count entries'),
)));
......
<?php
/**
* @file
*
* Hook Definition file for Views Natural Sort
* Hook Definition file for Views Natural Sort.
*/
/**
......@@ -11,7 +11,7 @@
* This information is passed to each module during re-index so that modules can
* determine whether it needs to return items or not.
*
* @return array $entity_types
* @return array
* Array of arrays defining fields and entities to reindex
* array(
* array(
......@@ -25,12 +25,12 @@ function hook_views_natural_sort_get_entry_types() {
array(
'entity_type' => 'user',
'field' => 'book_favorites',
)
),
);
}
/**
* Used for a custom module to define data that needs to be re-indexed when the
* Used for a custom module to queue data that needs to be re-indexed when the
* module is installed or settings are changed.
*
* @param array $entry_type
......@@ -38,20 +38,18 @@ function hook_views_natural_sort_get_entry_types() {
* $entity_type - The type of the entity we are getting
* data that needs to be re-indexed from
* $field - The field that needs to be re-indexed.
*
* @return array $index_entries An array of index entries that need re-indexing.
*/
function hook_views_natural_sort_get_rebuild_data($entry_type){
if($entry_type['entity_type'] != 'user' || $entry_type['field'] != 'book_favorites') {
function hook_views_natural_sort_queue_rebuild_data($entry_type) {
if ($entry_type['entity_type'] != 'user' || $entry_type['field'] != 'book_favorites') {
return array();
}
$result = db_select('user', 'u')
->fields('u', array('uid', 'book_favorites'))
->execute();
$data = array();
foreach ($result as $row ) {
$queue = views_natural_sort_get_queue();
foreach ($result as $row) {
// Grab the data returned and queue it up for transformation.
$data[] = array(
$queue->createItem = array(
'eid' => $row->uid,
'entity_type' => 'user',
'field' => 'book_favorites',
......@@ -59,7 +57,6 @@ function hook_views_natural_sort_get_rebuild_data($entry_type){
'content' => $row->book_favorites,
);
}
return $data;
}
/**
......@@ -76,7 +73,7 @@ function hook_views_natural_sort_get_rebuild_data($entry_type){
* $field - reference to the property or field name
* $delta - the item number in that field or property
* $content - The original string before
* transformations
* transformations.
*/
function hook_views_natural_sort_transformations_alter(&$transformations, $index_entry) {
// This function will receive a single argument that is the string that needs
......@@ -98,10 +95,21 @@ function hook_views_natural_sort_transformations_alter(&$transformations, $index
/**
* This is NOT A HOOK. Example transformation function.
*
* @param string $string The string to be transformed.
* @param string $string
* The string to be transformed.
*
* @return string A transformed string used for sorting "Naturally".
*/
function _my_special_transformation_function($string) {
return str_replace('a', '', $string);
}
/**
* This hook has been deprecated and is no longer called.
*
* @deprecated
*
* @see hook_views_natural_sort_queue_rebuild_data
*/
function hook_views_natural_sort_queue_rebuild_data($entry_type) {
}
<?php
/**
* @file
*/
/**
*
*/
function views_natural_sort_remove_beginning_words($string) {
$beginning_words = variable_get('views_natural_sort_beginning_words_remove', array());
if (empty($beginning_words)) {
......@@ -14,6 +21,9 @@ function views_natural_sort_remove_beginning_words($string) {
);
}
/**
*
*/
function views_natural_sort_remove_words($string) {
$words = variable_get('views_natural_sort_words_remove', array());
if (empty($words)) {
......@@ -28,12 +38,15 @@ function views_natural_sort_remove_words($string) {
),
array(
' ',
''
'',
),
$string
);
}
/**
*
*/
function views_natural_sort_remove_symbols($string) {
$symbols = variable_get('views_natural_sort_symbols_remove', '');
if (strlen($symbols) == 0) {
......@@ -90,9 +103,9 @@ function views_natural_sort_numbers($string) {
*/
function _views_natural_sort_number_transform_match_callback($match) {
// Remove commas and leading zeros from whole number
$whole = (string)(int)str_replace(',', '', (isset($match[4]) && strlen($match[4]) > 0) ? $match[4] : $match[2]);
// Remove traililng 0's from fraction, then add the decimal and one trailing 0
// Remove commas and leading zeros from whole number.
$whole = (string) (int) str_replace(',', '', (isset($match[4]) && strlen($match[4]) > 0) ? $match[4] : $match[2]);
// Remove traililng 0's from fraction, then add the decimal and one trailing 0.
$fraction = trim('.' . $match[3], '0') . '0';
$encode = sprintf('%02u', strlen($whole)) . $whole . $fraction;
if (strlen($match[1])) {
......@@ -103,7 +116,7 @@ function _views_natural_sort_number_transform_match_callback($match) {
$digits = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
$intermediate = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
$rev_digits = array('9', '8', '7', '6', '5', '4', '3', '2', '1', '0');
$encode = $match[1] . str_replace($intermediate, $rev_digits, str_replace($digits, $intermediate, $encode));
$encode = $match[1] . str_replace($intermediate, $rev_digits, str_replace($digits, $intermediate, $encode));
}
return $encode;
}
......@@ -126,37 +139,43 @@ function views_natural_sort_days_of_the_week_sort_days($string) {
$sorted_days = $day_list;
// Go through list and resort it and Translate it.
$start = array_search($first_day, $day_list);
for ($i=0; $i<7; $i++) {
$current_day = ($i+$start)%7;
for ($i = 0; $i < 7; $i++) {
$current_day = ($i + $start) % 7;
$abbreviations = views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day_list[$current_day], $used_language);
$translated_day = t($day_list[$current_day]);
$string = preg_replace(
array(
'/\b' . $translated_day . '\b/i',
'/\b(' . implode(views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day_list[$current_day]), '\.?|') . ')\b/i'
'/\b(' . implode(views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day_list[$current_day]), '\.?|') . ')\b/i',
),
' ' . $i .' ',
' ' . $i . ' ',
$string
);
}
return $string;
}
/**
*
*/
function views_natural_sort_days_of_the_week_get_default_days() {
return array (
return array(
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
"Saturday",
);
}
/**
*
*/
function views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($day, $lang = 'en') {
$default_days = views_natural_sort_days_of_the_week_get_default_days();
$index = array_search($day,$default_days);
$index = array_search($day, $default_days);
$default_abbrev = views_natural_sort_days_of_the_week_get_default_day_abbreviations();
return variable_get(
'views_natural_sort_days_of_the_week_' . $lang . '_' . $day,
......@@ -164,8 +183,11 @@ function views_natural_sort_days_of_the_week_get_acceptable_day_abbreviations($d
);
}
/**
*
*/
function views_natural_sort_days_of_the_week_get_default_day_abbreviations() {
return array (
return array(
array("Sun"),
array("Mon"),
array("Tu", "Tue", "Tues"),
......@@ -175,5 +197,3 @@ function views_natural_sort_days_of_the_week_get_default_day_abbreviations() {
array("Sat"),
);
}
<?php
/**
* @file
*
*/
/**
* Implementation of hook_schema().
*/
......@@ -67,7 +66,7 @@ function views_natural_sort_install() {
t('An'),
t('La'),
t('Le'),
t('Il')
t('Il'),
)
);
variable_set(
......@@ -127,7 +126,7 @@ function views_natural_sort_update_7200() {
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => 'node'
'default' => 'node',
)
);
db_add_field(
......@@ -163,7 +162,7 @@ function views_natural_sort_update_7200() {
* Upgrading 1.x VNS views to 2.x VNS views.
*/
function views_natural_sort_update_7201() {
foreach($views as $view_name => $view) {
foreach ($views as $view_name => $view) {
foreach ($view->display as &$display) {
if (!empty($display->display_options['sorts'])) {
foreach ($display->display_options['sorts'] as &$sort) {
......
<?php
/**
* @file
* Provides a views filter that sorts titles by a more natural manner by
* ignoring articles like "The" and "A."
* ignoring articles like "The" and "A.".
*
* Normal sort:
* A Chorus Line
* All American
* Fiddler on the Roof
* Oklahoma!
* The King And I
* The King And I.
*
* Natural sort:
* All American
......@@ -44,6 +45,10 @@ function views_natural_sort_views_api() {
'api' => 3.0,
);
}
/**
*
*/
function views_natural_sort_module_implements_alter(&$implements, $hook) {
if ($hook == 'views_data_alter') {
// Make views natural sort always last so we get all the up to date info.
......@@ -71,40 +76,36 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
}
/**
* Implements hook_views_natural_sort_get_rebuild_data();
* Implements hook_views_natural_sort_queue_rebuild_data();.
*/
function views_natural_sort_views_natural_sort_get_rebuild_data($entry_type){
function views_natural_sort_views_natural_sort_queue_rebuild_data($entry_type) {
$supported_entity_properties = views_natural_sort_get_views_configurable_properties();
if (empty($supported_entity_properties[$entry_type['entity_type']]) ||
empty($supported_entity_properties[$entry_type['entity_type']][$entry_type['field']])) {
return array();
}
$queue = views_natural_sort_get_queue();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entry_type['entity_type']);
$result = $query->execute();
$result = $query->entityCondition('entity_type', $entry_type['entity_type'])
->execute();
$entity_ids = array();
if (isset($result[$entry_type['entity_type']])) {
$entity_ids = array_keys($result[$entry_type['entity_type']]);
}
$data = array();
foreach ($entity_ids as $entity_id) {
$results = entity_load($entry_type['entity_type'], array($entity_id));
$entity = reset($results);
$data[] = array(
$queue->createItem(array(
'eid' => $entity_id,
'entity_type' => $entry_type['entity_type'],
'field' => $entry_type['field'],
'delta' => 0,
'content' => $entity->$entry_type['field'],
);
));
}
return $data;
}
/**
......@@ -149,7 +150,7 @@ function views_natural_sort_entity_delete($entity, $type) {
}
/**
* Store Multiple views_natural_sort entries
* Store Multiple views_natural_sort entries.
*
* @param array $index_entries
* An array of entries to store in the views_natural_sort table.
......@@ -157,13 +158,13 @@ function views_natural_sort_entity_delete($entity, $type) {
* @see views_natural_sort_store
*/
function views_natural_sort_store_multiple(array $index_entries) {
foreach($index_entries as $entry) {
foreach ($index_entries as $entry) {
views_natural_sort_store($entry);
}
}
/**
* Save an entry to the database that represents a views_natural_sort index
* Save an entry to the database that represents a views_natural_sort index.
*
* @param array $index_entry
* Mirrors the views_natural_sort table
......@@ -174,7 +175,6 @@ function views_natural_sort_store_multiple(array $index_entries) {
* $content - The transformed data that a field will
* be sorted by.
*/
function views_natural_sort_store(array $index_entry) {
// This should take a formatted object and store it into the views_natural_sort table.
$string = views_natural_sort_transform($index_entry);
......@@ -204,7 +204,7 @@ function views_natural_sort_store(array $index_entry) {
}
/**
* Remove a views_natural_sort index entry based on keys
* Remove a views_natural_sort index entry based on keys.
*
* @param array $index_entry
* Mirrors the views_natural_sort table
......@@ -213,7 +213,7 @@ function views_natural_sort_store(array $index_entry) {
* $field - (optional) reference to the property or field name
* $delta - (optional)the item number in that field or property
* If an optional parameter doesn't exist, this is treated as a wild care
* delete
* delete.
*/
function views_natural_sort_remove($index_entry) {
$query = db_delete('views_natural_sort')
......@@ -232,14 +232,14 @@ function views_natural_sort_remove($index_entry) {
* Encodes a string into an ascii-sortable string such as:
* - Leading articles in common languages are ingored: The A An El La Le Il
* - Unimportant punctuation is ignored: # ' " ( )
* - Unimportant words are ignored: and of or
* - Unimportant words are ignored: and of or.
*
* @param array $index_entry
* Mirrors the views_natural_sort table
* $eid - Entity Id of the item referenced
* $entity_type - The Entity Type. Ex. node
* $field - reference to the property or field name
* $delta - the item number in that field or property
* $delta - the item number in that field or property.
*
* @return string
* The transformed string