Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Search / Lucene / indexing / CreateIndex.php
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-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /**
23 * @see Zend_Search_Lucene
24 */
25 require_once 'Zend/Search/Lucene.php';
26
27 /**
28 * @category Zend
29 * @package Zend_Search_Lucene
30 * @subpackage Demos
31 * @uses Zend_Search_Lucene_Document
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
34 */
35 class FileDocument extends Zend_Search_Lucene_Document
36 {
37 /**
38 * Object constructor
39 *
40 * @param string $fileName
41 * @param boolean $storeContent
42 * @throws Zend_Search_Lucene_Exception
43 * @return void
44 */
45 public function __construct($fileName, $storeContent = false)
46 {
47 if (!file_exists($fileName)) {
48 throw new Zend_Search_Lucene_Exception("File doesn't exists. Filename: '$fileName'");
49 }
50 $this->addField(Zend_Search_Lucene_Field::Text('path', $fileName));
51 $this->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($fileName) ));
52
53 $f = fopen($fileName,'rb');
54 $byteCount = filesize($fileName);
55
56 $data = '';
57 while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
58 $data .= $nextBlock;
59 $byteCount -= strlen($nextBlock);
60 }
61 fclose($f);
62
63 if ($storeContent) {
64 $this->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO8859-1'));
65 } else {
66 $this->addField(Zend_Search_Lucene_Field::UnStored('contents', $data, 'ISO8859-1'));
67 }
68 }
69 }
70
71
72 // Create index
73 $index = new Zend_Search_Lucene('index', true);
74 // Uncomment next line if you want to have case sensitive index
75 // ZSearchAnalyzer::setDefault(new ZSearchTextAnalyzer());
76
77 setlocale(LC_CTYPE, 'en_US');
78
79 $indexSourceDir = 'IndexSource';
80 $dir = opendir($indexSourceDir);
81 while (($file = readdir($dir)) !== false) {
82 if (is_dir($indexSourceDir . '/' . $file)) {
83 continue;
84 }
85 if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
86 continue;
87 }
88
89 // Create new Document from a file
90 $doc = new FileDocument($indexSourceDir . '/' . $file, true);
91 // Add document to the index
92 $index->addDocument($doc);
93
94 echo $file . "...\n";
95 flush();
96 }
97 closedir($dir);