1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
<?php
/**
* indexing.lib.php
*
* Author: Steve Bourgeois <owl@bozzit.com>
* Project Founder: Chris Vincent <cvincent@project802.net>
*
* Copyright (c) 1999-2006 The Owl Project Team
* Licensed under the GNU GPL. For full terms see the file COPYING.
*
* $Id: indexing.lib.php,v 1.2 2006/03/20 20:15:54 b0zz Exp $
*/
function DoesFileIDContainKeyword($fileid, $keyword)
{
global $default;
$sql = new Owl_DB;
$sql->query("SELECT * from $default->owl_wordidx where word='$keyword'");
$sql->query("SELECT * from $default->owl_wordidx where word like '%$keyword%'");
if ($sql->num_rows() > 0)
{
$glue = "";
while($sql->next_record())
{
$query .= $glue . " wordid = '" . $sql->f("wordid") . "'";
$glue = " OR ";
}
}
else
{
$query = "wordid = '-1'";
}
$sql->query("SELECT * from $default->owl_searchidx where ($query) and owlfileid = '$fileid'");
return $sql->num_rows();
}
function IndexATextFile($filename, $owlfileid)
{
global $default;
$fileidnum = $owlfileid;
$sql = new Owl_DB;
$sql->query("SELECT * from $default->owl_wordidx"); //Import all words and indexes
$nextwordindex = 0;
$wordindex = array();
while ($sql->next_record()) // this may get ugly, we could have 100K words and indexes, they gotta go into memory.
{
$wordindex[$sql->f("word")] = $sql->f("wordid");
if ($sql->f("wordid") > $nextwordindex)
{
$nextwordindex = $sql->f("wordid"); //get largest word index in table
}
}
$nextwordindex++;
// Note: again, here we've just read in the big wordidx, we should index as many
// files as possible while we have this index in memory, here we
// only index a single filename, but if someone wants to greatly improve performance,
// index an array of filenames here...
if (file_exists($filename))
{
$fp = fopen($filename, "rb");
while (!feof($fp))
{
$line = fgets($fp, 1024);
$line = strtolower($line);
// this line added to deal with WORD Tables
$line = str_replace("|", " ",$line);
// remove long _____________________________ lines
$line = preg_replace('*__*', '', $line);
//$wordtemp = preg_split("/\W/", $line); //split line into words a word is any # of A-Za-z's separated by somethign not a-zA-Z
$wordtemp = preg_split("/\s+/", $line); //split line into words a word is any # of A-Za-z's separated by somethign not a-zA-Z
if (!isset($wordtemp)) continue;
foreach($wordtemp as $wd)
{
$wd = stripslashes(ereg_replace("[$default->list_of_chars_to_remove_from_wordidx]","",str_replace("]", "", str_replace("[", "",$wd))));
if (strlen(trim($wd)) > 0 and strlen(trim($wd)) < 128)
{
$words[$wd]++; //keep a count of how often each word is seen
//print("WORDS: $words[$wd] ---- ");
if ($words[$wd] == 1) // if this is the first time we've seen this word in this document...
{
if ($wordindex[$wd]) // if this word was already in the wordidx table...
{
$sql->query("INSERT INTO $default->owl_searchidx VALUES('$wordindex[$wd]','$fileidnum')"); //add a searchidx table entry for this fileidnum (owlidnum)
}
else // if word not in word index, add to both wordidx and searchidx
{
if (!empty($default->words_to_exclude_from_wordidx))
{
array($WordList);
$WordList = $default->words_to_exclude_from_wordidx;
$checkword = str_replace("+", "\+", $wd);
$checkword = str_replace("'", "\'", $checkword);
$checkword = str_replace("{", "\{", $checkword);
$checkword = str_replace("}", "\}", $checkword);
if (!(preg_grep("/$checkword/", $WordList)))
{
$wordindex[$wd] = $nextwordindex; //first remember this word as being in the wordindex
$sql->query("INSERT into $default->owl_searchidx values('$wordindex[$wd]', '$fileidnum')"); //add pointer to owlidnum for this wordindexnum
$wd = ereg_replace("'", "\\'" , $wd);
$sql->query("SELECT wordid from $default->owl_wordidx where word = '$wd'");
$numrows = $sql->num_rows($sql);
if ( $numrows == 0 )
{
$sql->query("INSERT into $default->owl_wordidx values('$nextwordindex', '$wd')");
$nextwordindex++;
}
}
}
else
{
$wordindex[$wd] = $nextwordindex; //first remember this word as being in the wordindex
$sql->query("INSERT into $default->owl_searchidx values('$wordindex[$wd]', '$fileidnum')"); //add pointer to owlidnum for this wordindexnum
$wd = ereg_replace("'", "\\'" , $wd);
$sql->query("SELECT wordid from $default->owl_wordidx where word = '$wd'");
$numrows = $sql->num_rows($sql);
if ( $numrows == 0 )
{
$sql->query("INSERT into $default->owl_wordidx values('$nextwordindex', '$wd')");
$nextwordindex++;
}
}
}
} //if first instance of this word...
}
} //for each word
} //while!feof
}
else
{
if ($default->debug == true)
{
printError("DEBUG: $owl_lang->err_file_indexing");
}
}
}
function IndexABigString($bigstring, $owlfileid)
{
global $default;
$fileidnum = $owlfileid;
$sql = new Owl_DB;
$sql->query("SELECT * from $default->owl_wordidx"); //Import all words and indexes
$nextwordindex = 0;
$wordindex = array();
while ($sql->next_record()) // this may get ugly, we could have 100K words and indexes, they gotta go into memory.
{
$wordindex[$sql->f("word")] = $sql->f("wordid");
if ($sql->f("wordid") > $nextwordindex)
{
$nextwordindex = $sql->f("wordid"); //get largest word index in table
}
}
$nextwordindex++;
// Note: again, here we've just read in the big wordidx, we should index as many
// files as possible while we have this index in memory, here we
// only index a single filename, but if someone wants to greatly improve performance,
// index an array of filenames here...
$wordtemp = preg_split("/\s+/", strtolower($bigstring)); //split line into words a word is any # of A-Za-z's separated by somethign not a-zA-Z
if (!isset($wordtemp)) return;
foreach($wordtemp as $wd)
{
$wd = ereg_replace("[$default->list_of_chars_to_remove_from_wordidx]","",$wd);
if (strlen(trim($wd)) > 0 and strlen(trim($wd)) < 128)
{
$words[$wd]++; //keep a count of how often each word is seen
//print("WORDS: $words[$wd] ---- ");
if ($words[$wd] == 1) // if this is the first time we've seen this word in this document...
{
if ($wordindex[$wd]) // if this word was already in the wordidx table...
{
$sql->query("INSERT into $default->owl_searchidx values('$wordindex[$wd]','$fileidnum')"); //add a searchidx table entry for this fileidnum (owlidnum)
}
else // if word not in word index, add to both wordidx and searchidx
{
$wordindex[$wd] = $nextwordindex; //first remember this word as being in the wordindex
$sql->query("INSERT into $default->owl_searchidx values('$wordindex[$wd]', '$fileidnum')"); //add pointer to owlidnum for this wordindexnum
$wd = ereg_replace("'", "\\'" , $wd);
$sql->query("SELECT wordid from $default->owl_wordidx where word = '$wd'");
$numrows = $sql->num_rows($sql);
if ( $numrows == 0 )
{
$sql->query("INSERT into $default->owl_wordidx values('$nextwordindex', '$wd')");
$nextwordindex++;
}
}
} //if first instance of this word...
}
} //for each word
}
// When a file gets delete/removed, this should be called to update the indexing
// tables
function fDeleteFileIndexID($fidtoremove)
{
global $default;
$sql = new Owl_DB;
$sql->query("DELETE from $default->owl_searchidx where owlfileid = $fidtoremove");
// Note, I'm leaving the wordidx table alone, it can only grow so large as
// there are only so many words in the language, will make indexing future items a bit faster methinks
}
function fIndexAFile($new_name, $newpath, $id)
{
global $default, $sess, $index_file;
if ($index_file == "1")
{
// IF the file was inserted in the database now INDEX it for SEARCH.
$sSearchExtension = fFindFileExtension($new_name);
if ($sSearchExtension == 'pdf' || $sSearchExtension == 'c' || $sSearchExtension == 'html' || $sSearchExtension == 'htm' || $sSearchExtension == 'php' || $sSearchExtension == 'pl' || $sSearchExtension == 'txt' || $sSearchExtension == 'doc' || $sSearchExtension == 'xls' or $sSearchExtension == 'sxw' or $sSearchExtension == 'rtf' or $sSearchExtension == 'log')
{
if(file_exists($default->pdftotext_path) and $sSearchExtension == 'pdf')
{
$command = $default->pdftotext_path . ' "' . $newpath . '" "' . $default->owl_tmpdir . "/" . $new_name . '.text"';
$last_line = system($command, $retval);
if ($retval > 0)
{
if ($default->debug == true)
{
switch ($retval)
{
case "1":
$sPdfError = "Error opening a PDF file. (Not A PDF File?)";
break;
case "2":
$sPdfError = "Error opening an ouput file. ($default->owl_tmpdir Writeable by the webserver?)";
break;
}
printError('DEBUG: Indexing PDF File \'' . $newpath . '\' Failed:' , $sPdfError. "<br />COMMAND: $command");
}
}
IndexATextFile($default->owl_tmpdir . "/" . $new_name . '.text', $id);
unlink($default->owl_tmpdir . "/" . $new_name . '.text');
}
elseif (file_exists($default->wordtotext_path) and $sSearchExtension == 'doc')
{
//$command = "/bin/sh -c" . ' "' . $default->wordtotext_path . ' ' . $newpath . '"' . ' > "' . $default->owl_tmpdir . "/" . $new_name . '.text"';
$command = $default->wordtotext_path . ' "' . $newpath . '" > "' . $default->owl_tmpdir . "/" . $new_name . '.text"';
//print("C: $command");
//exit;
$last_line = system($command, $retval);
if ($retval > 0)
{
if ($default->debug == true)
{
$sPdfError = "Return: $retval $last_line";
printError('DEBUG: Indexing MS WORD File \'' . $newpath . '\' Failed:' , $sPdfError. "<br />COMMAND: $command");
}
}
IndexATextFile($default->owl_tmpdir . "/" . $new_name . '.text', $id);
unlink($default->owl_tmpdir . "/" . $new_name . '.text');
}
elseif (file_exists($default->rtftotext_path) and $sSearchExtension == 'rtf')
{
$command = "unrtf --text " . ' "' . $newpath . '" > "' . $default->owl_tmpdir . "/" . $new_name . '.text"';
$last_line = system($command, $retval);
if ($retval > 0)
{
if ($default->debug == true)
{
$sPdfError = "Return: $retval $last_line";
printError('DEBUG: Indexing RTFFile \'' . $newpath . '\' Failed:' , $sPdfError . "<br />COMMAND: $command");
}
}
IndexATextFile($default->owl_tmpdir . "/" . $new_name . '.text', $id);
unlink($default->owl_tmpdir . "/" . $new_name . '.text');
}
elseif($sSearchExtension == 'sxw')
{
$tmpDir = $default->owl_tmpdir . "/owltmp.$sess";
if (file_exists($tmpDir))
{
myDelete($tmpDir);
}
mkdir($tmpDir,$default->directory_mask);
$archive = new PclZip($newpath);
$aListOfFiles = $archive->listContent();
while ($aFileDetails = current($aListOfFiles)) {
if($aFileDetails["filename"] == "content.xml")
{
$iContentFileIndex = $aFileDetails["index"];
break;
}
next($aListOfFiles);
}
if ($archive->extractByIndex($iContentFileIndex, $tmpDir) == 0)
{
printError("DEBUG: " .$archive->errorInfo(true), "N: $newpath P: $tmpDir");
}
$text = file_get_contents("$tmpDir/content.xml");
$fp = fopen($tmpDir ."/content.xml.text", "w");
fwrite($fp, strip_tags($text));
fclose($fp);
IndexATextFile($tmpDir ."/content.xml.text", $id);
myDelete($tmpDir);
}
elseif($sSearchExtension == 'xls')
{
$xlwords = '';
require_once('scripts/Excel/reader.php');
$xl = new Spreadsheet_Excel_Reader();
$xl->read($newpath);
for ($k = count($xl->sheets)-1; $k>=0; $k--)
{
for ($i = 1; $i <= $xl->sheets[$k]['numRows']; $i++)
{
for ($j = 1; $j <= $xl->sheets[$k]['numCols']; $j++)
{
$xlwords .= $xl->sheets[$k]['cells'][$i][$j] . ' ';
}
}
}
$xlwords = preg_replace('# +#si',' ',$xlwords);
$xlwords = preg_replace('# $#si','',$xlwords);
IndexABigString($xlwords, $id);
}
else
{
if ($sSearchExtension == 'c' || $sSearchExtension == 'html' || $sSearchExtension == 'htm' || $sSearchExtension == 'php' || $sSearchExtension == 'pl' || $sSearchExtension == 'txt' or $sSearchExtension == 'log')
{
IndexATextFile($newpath, $id);
}
}
}
}
}
?>
|