Source for file backend_xmltcp.php

Documentation is available at backend_xmltcp.php

  1. <?php
  2.  
  3. /**
  4. * @package midcom.services
  5. * @author The Midgard Project, http://www.midgard-project.org
  6. * @version $Id: backend_xmltcp.php,v 1.2 2005/04/16 16:46:39 torben Exp $
  7. * @copyright The Midgard Project, http://www.midgard-project.org
  8. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  9. */
  10.  
  11. require_once 'XMLCommClient.php';
  12.  
  13. /**
  14. * XMLComm implementation using a TCP/IP interface.
  15. *
  16. * ...
  17. *
  18. * @abstract Abstract indexer backend class
  19. * @package midcom.services
  20. * @see midcom_services_indexer
  21. * @see midcom_services_indexer_backend
  22. * @see midcom_services_indexer_XMLComm_RequestWriter
  23. * @see midcom_services_indexer_XMLComm_ResponseParser
  24. *
  25. * @todo Check if there is a better way to handle the exec loop, which looks rather PHP-workaroundy right now.
  26. */
  27.  
  28. class midcom_services_indexer_backend_xmltcp extends midcom_services_indexer_backend
  29. {
  30. /**
  31. * The request to execute.
  32. *
  33. * @access private
  34. * @var midcom_services_indexer_XMLComm_RequestWriter
  35. */
  36. var $_request = null;
  37. /**
  38. * The response received.
  39. *
  40. * @access private
  41. * @var midcom_services_indexer_XMLComm_ResponseParser
  42. */
  43. var $_response = null;
  44. /**
  45. * Constructor is empty at this time.
  46. */
  47. function midcom_services_indexer_backend_xmltcp ()
  48. {
  49. parent::midcom_services_indexer_backend();
  50. // Nothing to do yet.
  51. }
  52. /**
  53. * Adds a document to the index.
  54. *
  55. * Any warning will be treated as error.
  56. *
  57. * Note, that $document may also be an array of documents without further
  58. * changes to this backend.
  59. *
  60. * @param Array $documents A list of midcom_services_indexer_document objects.
  61. * @return bool Indicating success.
  62. */
  63. function index ($documents)
  64. {
  65. $this->_request = new midcom_services_indexer_XMLComm_RequestWriter();
  66. $this->_request->add_index(0, $documents);
  67. $this->_exec();
  68. return (! array_key_exists(0, $this->_response->warnings));
  69. }
  70. /**
  71. * Removes the document with the given resource identifier from the index.
  72. *
  73. * @param string $RI The resource identifier of the document that should be deleted.
  74. * @return bool Indicating success.
  75. */
  76. function delete ($RI)
  77. {
  78. $this->_request = new midcom_services_indexer_XMLComm_RequestWriter();
  79. $this->_request->add_delete(0, $RI);
  80. $this->_exec();
  81. return (! array_key_exists(0, $this->_response->warnings));
  82. }
  83. /**
  84. * Clear the index completly.
  85. *
  86. * This will drop the current index.
  87. *
  88. * @return bool Indicating success.
  89. */
  90. function delete_all()
  91. {
  92. $this->_request = new midcom_services_indexer_XMLComm_RequestWriter();
  93. $this->_request->add_deleteall(0);
  94. $this->_exec();
  95. return (! array_key_exists(0, $this->_response->warnings));
  96. }
  97. /**
  98. * Query the index and, if set, restrict the query by a given filter.
  99. *
  100. * ...
  101. *
  102. * @param string $query The query, which must suite the backends query syntax.
  103. * @param midcom_services_indexer_filter $filter An optional filter used to restrict the query. This may be null indicating no filter.
  104. * @return Array An arary of documents matching the query, or false on a failure.
  105. */
  106. function query ($query, $filter)
  107. {
  108. $this->_request = new midcom_services_indexer_XMLComm_RequestWriter();
  109. $this->_request->add_query(0, $query, $filter);
  110. $this->_exec();
  111. if (array_key_exists(0, $this->_response->warnings))
  112. {
  113. return false;
  114. }
  115. return $this->_response->resultsets[0];
  116. }
  117. /**
  118. * This private helper will create a socket connection to the indexer daemon and
  119. * execute the query.
  120. *
  121. * Note, that both classes call generate_error on critical errors.
  122. */
  123. function _exec ()
  124. {
  125. debug_push_class($this, 'exec');
  126. $errstr = '';
  127. $errcode = 0;
  128. $socket = @fsockopen($GLOBALS['midcom_config']['indexer_xmltcp_host'],
  129. $GLOBALS['midcom_config']['indexer_xmltcp_port'],
  130. $errstr, $errcode);
  131. if (! $socket)
  132. {
  133. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT, "Failed to establish a connection to the indexer: {$errstr} ({$errcode})");
  134. // This will exit
  135. }
  136. $xml = $this->_request->get_xml(true);
  137. debug_print_r('Will send this request:', $xml);
  138. fwrite($socket, $xml);
  139. $response = '';
  140. while (! feof($socket))
  141. {
  142. $response .= fread($socket, 4096);
  143. }
  144. fclose($socket);
  145. debug_print_r('We got this response:', $response);
  146. $this->_response = new midcom_services_indexer_XMLComm_ResponseReader();
  147. $this->_response->parse($response);
  148. foreach ($this->_response->warnings as $id => $warning)
  149. {
  150. debug_add("Failed to execute Request {$id}: {$warning}", MIDCOM_LOG_WARN);
  151. }
  152. debug_pop();
  153. }
  154. }
  155.  
  156. ?>

Documentation generated on Mon, 21 Nov 2005 18:12:26 +0100 by phpDocumentor 1.3.0RC3