PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/acquia/acquia_search/Acquia_Search_Service.php

https://github.com/rtanglao/acquia-drupal-6
PHP | 130 lines | 87 code | 16 blank | 27 comment | 14 complexity | 2398574cbe40471ceda4550c81c49b05 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. // $Id$
  3. include_once './' . drupal_get_path('module', 'apachesolr') . '/Drupal_Apache_Solr_Service.php';
  4. include_once './' . drupal_get_path('module', 'acquia_agent') . '/acquia_agent_streams.inc';
  5. /**
  6. * Starting point for the Solr API. Represents a Solr server resource and has
  7. * methods for pinging, adding, deleting, committing, optimizing and searching.
  8. *
  9. */
  10. class Acquia_Search_Service extends Drupal_Apache_Solr_Service {
  11. protected function add_request_id(&$url) {
  12. $id = uniqid();
  13. if (!stristr($url,'?')) {
  14. $url .= "?";
  15. }
  16. $url .= '&request_id=' . $id;
  17. }
  18. /**
  19. * Central method for making a get operation against this Solr Server
  20. *
  21. * @see Drupal_Apache_Solr_Service::_sendRawGet()
  22. */
  23. protected function _sendRawGet($url, $timeout = FALSE) {
  24. $this->add_request_id($url);
  25. list($cookie, $nonce) = acquia_search_auth_cookie($url);
  26. $request_headers = array(
  27. 'Cookie' => $cookie,
  28. );
  29. list ($data, $headers) = $this->_makeHttpRequest($url, 'GET', $request_headers, '', $timeout);
  30. $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays);
  31. $hmac = acquia_search_extract_hmac($headers);
  32. if ($response->getHttpStatus() != 200) {
  33. throw new Exception('"' . $response->getHttpStatus() . '" Status: ' . $response->getHttpStatusMessage() . "\n<br />request ID: $id <br />" . $url, $response->getHttpStatus());
  34. }
  35. elseif (!acquia_search_valid_response($hmac, $nonce, $data)) {
  36. throw new Exception('Authentication of search content failed url: '. $url);
  37. }
  38. return $response;
  39. }
  40. /**
  41. * Central method for making a post operation against this Solr Server
  42. *
  43. * @see Drupal_Apache_Solr_Service::_sendRawGet()
  44. */
  45. protected function _sendRawPost($url, $rawPost, $timeout = FALSE, $contentType = 'text/xml; charset=UTF-8') {
  46. $this->add_request_id($url);
  47. list($cookie, $nonce) = acquia_search_auth_cookie($url, $rawPost);
  48. $request_headers = array(
  49. 'Content-Type' => $contentType,
  50. 'Cookie' => $cookie
  51. );
  52. list ($data, $headers) = $this->_makeHttpRequest($url, 'POST', $request_headers, $rawPost, $timeout);
  53. $response = new Apache_Solr_Response($data, $headers, $this->_createDocuments, $this->_collapseSingleValueArrays);
  54. if ($response->getHttpStatus() != 200) {
  55. throw new Exception('"' . $response->getHttpStatus() . '" Status: ' . $response->getHttpStatusMessage() . "\n<br />request ID: $id <br />" . $url, $response->getHttpStatus());
  56. }
  57. return $response;
  58. }
  59. /**
  60. * Send an optimize command.
  61. *
  62. * We want to control the schedule of optimize commands ourselves,
  63. * so do a method override to make ->optimize() a no-op.
  64. *
  65. * @see Drupal_Apache_Solr_Service::optimize()
  66. */
  67. public function optimize($waitFlush = true, $waitSearcher = true, $timeout = 3600) {
  68. return TRUE;
  69. }
  70. protected function _makeHttpRequest($url, $method = 'GET', $headers = array(), $content = '', $timeout = FALSE) {
  71. // Set a response timeout
  72. if ($timeout) {
  73. $default_socket_timeout = ini_set('default_socket_timeout', $timeout);
  74. }
  75. $ctx = acquia_agent_stream_context_create($url, 'acquia_search');
  76. if (!$ctx) {
  77. throw new Exception(t("Could not create stream context"));
  78. }
  79. $result = acquia_agent_http_request($ctx, $url, $headers, $method, $content);
  80. // Restore the response timeout
  81. if ($timeout) {
  82. ini_set('default_socket_timeout', $default_socket_timeout);
  83. }
  84. // This will no longer be needed after http://drupal.org/node/345591 is committed
  85. $responses = array(
  86. 0 => 'Request failed',
  87. 100 => 'Continue', 101 => 'Switching Protocols',
  88. 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
  89. 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
  90. 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',
  91. 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'
  92. );
  93. if (!isset($result->code) || $result->code < 0) {
  94. $result->code = 0;
  95. }
  96. if (!isset($result->data)) {
  97. $result->data = '';
  98. }
  99. if (!isset($result->error)) {
  100. $result->error = '';
  101. }
  102. if (!isset($responses[$result->code])) {
  103. $result->code = floor($result->code / 100) * 100;
  104. }
  105. $protocol = "HTTP/1.1";
  106. $headers[] = "{$protocol} {$result->code} {$responses[$result->code]}. {$result->error}";
  107. if (isset($result->headers)) {
  108. foreach ($result->headers as $name => $value) {
  109. $headers[] = "$name: $value";
  110. }
  111. }
  112. return array($result->data, $headers);
  113. }
  114. }