? SolrPhpClient ? drush-61.patch Index: apachesolr.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.admin.inc,v retrieving revision 1.1.2.33 diff -u -p -r1.1.2.33 apachesolr.admin.inc --- apachesolr.admin.inc 22 Oct 2009 13:32:44 -0000 1.1.2.33 +++ apachesolr.admin.inc 26 Oct 2009 19:12:24 -0000 @@ -406,17 +406,25 @@ function apachesolr_delete_index_confirm catch (Exception $e) { watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR); } -} +} /** * Utility function to delete the index and reset all index counters. * + * @param $type + * a single content type to be deleted from the index. + * * @throws Exception */ -function apachesolr_delete_index() { +function apachesolr_delete_index($type = NULL) { // Instantiate a new Solr object. $solr = apachesolr_get_solr(); - $query = '*:*'; + if ($type) { + $query = 'type:' . $type; + } + else { + $query = '*:*'; + } // Allow other modules to modify the delete query. // For example, use the site hash so that you only delete this site's // content: $query = 'hash:' . apachesolr_site_hash() @@ -428,6 +436,7 @@ function apachesolr_delete_index() { apachesolr_index_updated(time()); } + /** * Try to map a schema field name to a human-readable description. */ Index: apachesolr.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v retrieving revision 1.1.2.12.2.170 diff -u -p -r1.1.2.12.2.170 apachesolr.module --- apachesolr.module 26 Oct 2009 02:04:12 -0000 1.1.2.12.2.170 +++ apachesolr.module 26 Oct 2009 19:12:24 -0000 @@ -265,26 +265,39 @@ function apachesolr_clear_last_index($na else { variable_del('apachesolr_index_last'); } -} +} /** * Truncate and rebuild the apachesolr_search_node table, reset the apachesolr_index_last variable. * This is the most complete way to force reindexing, or to build the indexing table for the * first time. + * + * @param $type + * A single content type to be reindexed, leaving the others unaltered. */ -function apachesolr_rebuild_index_table() { - db_query("DELETE FROM {apachesolr_search_node}"); - // Populate table - db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed) - SELECT n.nid, n.status, GREATEST(n.created, n.changed, c.last_comment_timestamp) AS changed - FROM {node} n - LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid"); - // Make sure no nodes end up with a timestamp that's in the future. - $time = time(); - db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE changed > %d", $time, $time); - apachesolr_clear_last_index(); +function apachesolr_rebuild_index_table($type = NULL) { + if (isset($type)) { + db_query("DELETE FROM {apachesolr_search_node} WHERE nid IN (SELECT nid FROM {node} WHERE type = '%s')", $type); + // Populate table + db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed) + SELECT n.nid, n.status, %d AS changed + FROM {node} n WHERE n.type = '%s'", time(), $type); + } + else { + db_query("DELETE FROM {apachesolr_search_node}"); + // Populate table + db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed) + SELECT n.nid, n.status, GREATEST(n.created, n.changed, c.last_comment_timestamp) AS changed + FROM {node} n + LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid"); + // Make sure no nodes end up with a timestamp that's in the future. + $time = time(); + db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE changed > %d", $time, $time); + apachesolr_clear_last_index(); + } } + function _apachesolr_exclude_types($namespace) { extract(apachesolr_get_last_index($namespace)); $excluded_types = module_invoke_all('apachesolr_types_exclude', $namespace); Index: drush/apachesolr.drush.inc =================================================================== RCS file: drush/apachesolr.drush.inc diff -N drush/apachesolr.drush.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ drush/apachesolr.drush.inc 26 Oct 2009 19:12:24 -0000 @@ -0,0 +1,151 @@ + 'apachesolr_drush_solr_delete_index', + // a short description of your command + 'description' => dt('Deletes the content from the index. Can take content types as parameters.'), + 'arguments' => array( + 'types' => dt('Optional. A space delimited list of content types to be deleted from the index.'), + ), + ); + $items['solr reindex'] = array( + // the name of the function implementing your command. + 'callback' => 'apachesolr_drush_solr_reindex', + // a short description of your command + 'description' => dt('Marks content for reindexing. Can take content types as parameters.'), + 'arguments' => array( + 'types' => dt('Optional. A space delimited list of content types to be marked for reindexing.'), + ), + ); + $items['solr phpclient'] = array( + 'callback' => 'apachesolr_drush_solr_phpclient', + 'description' => dt('Downloads the required SolrPhpClient from googlecode.com.'), + 'arguments' => array( + 'path' => dt('Optional. A path to the apachesolr module. If omitted Drush will use the default location.'), + ), + ); + $items['solr search'] = array( + 'callback' => 'apachesolr_drush_solr_search', + 'description' => dt('Search the site for keywords using Apache Solr'), + 'arguments' => array( + 'keywords' => dt('One or more keywords, separated by spaces.'), + ), + ); + return $items; +} + +/** + * Implementation of hook_drush_help(). + * + * This function is called whenever a drush user calls + * 'drush help ' + * + * @param + * A string with the help section (prepend with 'drush:') + * + * @return + * A string with the help text for your command. + */ +function apachesolr_drush_help($section) { + switch ($section) { + case 'drush:solr delete index': + return dt("Used without parameters, this command deletes the entire Solr index. Used with parameters for content type, it deletes just the content types that are specified. After the index has been deleted, all content will be indexed again on future cron runs."); + case 'drush:solr reindex': + return dt("Used without parameters, this command marks all of the content in the Solr index for reindexing. Used with paramters for content type, it marks just the content types that are specified. Reindexing is different than deleting as the content is still searchable while it is in queue to be reindexed. Reindexing is done on future cron runs."); + case 'drush:solr phpclient': + return dt("Downloads the SolrPhpClient libraray from googlecode.com. Include the optional path to an apachesolr module installation if you have more than one, or if the module is not yet enabled."); + case 'drush:solr search': + return dt('Executes a search against the site\'s Apache Solr search index and returns the restults.'); + } +} + +/** + * Example drush command callback. + * + * This is where the action takes place. + * + * In this function, all of Drupals API is (usually) available, including + * any functions you have added in your own modules/themes. + * + * To print something to the terminal window, use drush_print(). + * + */ +function apachesolr_drush_solr_delete_index() { + $args = func_get_args(); + $path = drupal_get_path('module', 'apachesolr'); + include_once($path . "/apachesolr.admin.inc"); + if (count($args) > 0) { + foreach ($args as $type) { + apachesolr_delete_index($type); + } + } + else { + apachesolr_delete_index(); + } +} + +function apachesolr_drush_solr_reindex() { + $args = func_get_args(); + if (count($args) > 0) { + foreach ($args as $type) { + apachesolr_rebuild_index_table($type); + } + } + else { + apachesolr_rebuild_index_table(); + } +} + +function apachesolr_drush_solr_phpclient() { + $args = func_get_args(); + if ($args[0]) { + $path = $args[0]; + } + else { + $path = drupal_get_path('module', 'apachesolr'); + } + drush_op('chdir', $path); + if (drush_shell_exec('svn export -r6 http://solr-php-client.googlecode.com/svn/trunk/ SolrPhpClient')) { + drush_log(dt('SolrPhpClient has been downloaded to @path', array('@path' => $path)), 'success'); + } + else { + drush_log(dt('Drush was unable to download the SolrPhpClient to @path', array('@path' => $path)), 'error'); + } +} + +function apachesolr_drush_solr_search() { + $args = func_get_args(); + $keys = implode(' ', $args); + foreach(apachesolr_search_execute($keys) as $result) { + $output = 'node/' . $result['node']->nid . ' ' . dt('by @name (user/@uid)', array('@name' => strip_tags($result['user']), '@uid' => $result['node']->uid)) . "\n"; + $output .= dt('title: ') . $result['title'] . "\n"; + $output .= preg_replace('/[\s]+/', ' ', strip_tags($result['snippet'])) . "\n\n"; + drush_print($output); + } +}