/ZendFramework/demos/Zend/Search/Lucene/indexing/CreateIndex.php

https://bitbucket.org/Dal-Papa/is-340-publish-base · PHP · 97 lines · 57 code · 2 blank · 38 comment · 1 complexity · 9e0e4aa2fb78fbf8d896767b73c09e63 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Search_Lucene
  23. */
  24. require_once 'Zend/Search/Lucene.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Search_Lucene
  28. * @subpackage Demos
  29. * @uses Zend_Search_Lucene_Document
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class FileDocument extends Zend_Search_Lucene_Document
  34. {
  35. /**
  36. * Object constructor
  37. *
  38. * @param string $fileName
  39. * @param boolean $storeContent
  40. * @throws Zend_Search_Lucene_Exception
  41. * @return void
  42. */
  43. public function __construct($fileName, $storeContent = false)
  44. {
  45. if (!file_exists($fileName)) {
  46. throw new Zend_Search_Lucene_Exception("File doesn't exists. Filename: '$fileName'");
  47. }
  48. $this->addField(Zend_Search_Lucene_Field::Text('path', $fileName));
  49. $this->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($fileName) ));
  50. $f = fopen($fileName,'rb');
  51. $byteCount = filesize($fileName);
  52. $data = '';
  53. while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
  54. $data .= $nextBlock;
  55. $byteCount -= strlen($nextBlock);
  56. }
  57. fclose($f);
  58. if ($storeContent) {
  59. $this->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO8859-1'));
  60. } else {
  61. $this->addField(Zend_Search_Lucene_Field::UnStored('contents', $data, 'ISO8859-1'));
  62. }
  63. }
  64. }
  65. // Create index
  66. $index = new Zend_Search_Lucene('index', true);
  67. // Uncomment next line if you want to have case sensitive index
  68. // ZSearchAnalyzer::setDefault(new ZSearchTextAnalyzer());
  69. setlocale(LC_CTYPE, 'en_US');
  70. $indexSourceDir = 'IndexSource';
  71. $dir = opendir($indexSourceDir);
  72. while (($file = readdir($dir)) !== false) {
  73. if (is_dir($indexSourceDir . '/' . $file)) {
  74. continue;
  75. }
  76. if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
  77. continue;
  78. }
  79. // Create new Document from a file
  80. $doc = new FileDocument($indexSourceDir . '/' . $file, true);
  81. // Add document to the index
  82. $index->addDocument($doc);
  83. echo $file . "...\n";
  84. flush();
  85. }
  86. closedir($dir);