mirror of
https://git.code.sf.net/p/seeddms/code
synced 2024-11-26 23:42:11 +00:00
168 lines
5.2 KiB
PHP
168 lines
5.2 KiB
PHP
<?php
|
||
if(isset($_SERVER['SEEDDMS_HOME'])) {
|
||
require_once($_SERVER['SEEDDMS_HOME']."/inc/inc.ClassSettings.php");
|
||
} else {
|
||
require_once("../inc/inc.ClassSettings.php");
|
||
}
|
||
|
||
function usage() { /* {{{ */
|
||
echo "Usage:".PHP_EOL;
|
||
echo " seeddms-indexer [-h] [-v] [--config <file>]".PHP_EOL;
|
||
echo "".PHP_EOL;
|
||
echo "Description:".PHP_EOL;
|
||
echo " This program recreates the full text index of SeedDMS.".PHP_EOL;
|
||
echo "".PHP_EOL;
|
||
echo "Options:".PHP_EOL;
|
||
echo " -h, --help: print usage information and exit.".PHP_EOL;
|
||
echo " -v, --version: print version and exit.".PHP_EOL;
|
||
echo " -c: recreate index.".PHP_EOL;
|
||
echo " --config: set alternative config file.".PHP_EOL;
|
||
} /* }}} */
|
||
|
||
$version = "0.0.2";
|
||
$shortoptions = "hvc";
|
||
$longoptions = array('help', 'version', 'config:');
|
||
if(false === ($options = getopt($shortoptions, $longoptions))) {
|
||
usage();
|
||
exit(0);
|
||
}
|
||
|
||
/* Print help and exit */
|
||
if(isset($options['h']) || isset($options['help'])) {
|
||
usage();
|
||
exit(0);
|
||
}
|
||
|
||
/* Print version and exit */
|
||
if(isset($options['v']) || isset($options['verѕion'])) {
|
||
echo $version."".PHP_EOL;
|
||
exit(0);
|
||
}
|
||
|
||
/* Set alternative config file */
|
||
if(isset($options['config'])) {
|
||
$settings = new Settings($options['config']);
|
||
} else {
|
||
$settings = new Settings();
|
||
}
|
||
|
||
/* recreate index */
|
||
$recreate = false;
|
||
if(isset($options['c'])) {
|
||
$recreate = true;
|
||
}
|
||
|
||
if(isset($settings->_extraPath))
|
||
ini_set('include_path', $settings->_extraPath. PATH_SEPARATOR .ini_get('include_path'));
|
||
//ini_set('include_path', $settings->_rootDir. PATH_SEPARATOR .ini_get('include_path'));
|
||
|
||
require_once("inc/inc.Init.php");
|
||
require_once("inc/inc.Extension.php");
|
||
require_once("inc/inc.DBInit.php");
|
||
require "vendor/autoload.php";
|
||
|
||
if($settings->_fullSearchEngine == 'sqlitefts') {
|
||
$indexconf = array(
|
||
'Indexer' => 'SeedDMS_SQLiteFTS_Indexer',
|
||
'Search' => 'SeedDMS_SQLiteFTS_Search',
|
||
'IndexedDocument' => 'SeedDMS_SQLiteFTS_IndexedDocument'
|
||
);
|
||
|
||
require_once('SeedDMS/SQLiteFTS.php');
|
||
} else {
|
||
$indexconf = array(
|
||
'Indexer' => 'SeedDMS_Lucene_Indexer',
|
||
'Search' => 'SeedDMS_Lucene_Search',
|
||
'IndexedDocument' => 'SeedDMS_Lucene_IndexedDocument'
|
||
);
|
||
|
||
require_once('SeedDMS/Lucene.php');
|
||
}
|
||
|
||
function tree($dms, $index, $indexconf, $folder, $indent='') { /* {{{ */
|
||
global $settings, $themes;
|
||
echo $themes->black($indent."D ".$folder->getName()).PHP_EOL;
|
||
$subfolders = $folder->getSubFolders();
|
||
foreach($subfolders as $subfolder) {
|
||
tree($dms, $index, $indexconf, $subfolder, $indent.' ');
|
||
}
|
||
$documents = $folder->getDocuments();
|
||
foreach($documents as $document) {
|
||
echo $indent." ".$document->getId().":".$document->getName()." ";
|
||
$lucenesearch = new $indexconf['Search']($index);
|
||
if(!($hit = $lucenesearch->getDocument($document->getId()))) {
|
||
try {
|
||
$idoc = new $indexconf['IndexedDocument']($dms, $document, isset($settings->_converters['fulltext']) ? $settings->_converters['fulltext'] : null, false, $settings->_cmdTimeout);
|
||
if(isset($GLOBALS['SEEDDMS_HOOKS']['indexDocument'])) {
|
||
foreach($GLOBALS['SEEDDMS_HOOKS']['indexDocument'] as $hookObj) {
|
||
if (method_exists($hookObj, 'preIndexDocument')) {
|
||
$hookObj->preIndexDocument(null, $document, $idoc);
|
||
}
|
||
}
|
||
}
|
||
$index->addDocument($idoc);
|
||
echo $themes->green(" (Document added)").PHP_EOL;
|
||
} catch(Exception $e) {
|
||
echo $themes->error(" (Timeout)").PHP_EOL;
|
||
}
|
||
} else {
|
||
try {
|
||
$created = (int) $hit->getDocument()->getFieldValue('created');
|
||
} catch (Exception $e) {
|
||
$created = 0;
|
||
}
|
||
$content = $document->getLatestContent();
|
||
if($created >= $content->getDate()) {
|
||
echo $themes->italic(" (Document unchanged)").PHP_EOL;
|
||
} else {
|
||
$index->delete($hit->id);
|
||
try {
|
||
$idoc = new $indexconf['IndexedDocument']($dms, $document, isset($settings->_converters['fulltext']) ? $settings->_converters['fulltext'] : null, false, $settings->_cmdTimeout);
|
||
if(isset($GLOBALS['SEEDDMS_HOOKS']['indexDocument'])) {
|
||
foreach($GLOBALS['SEEDDMS_HOOKS']['indexDocument'] as $hookObj) {
|
||
if (method_exists($hookObj, 'preIndexDocument')) {
|
||
$hookObj->preIndexDocument(null, $document, $idoc);
|
||
}
|
||
}
|
||
}
|
||
$index->addDocument($idoc);
|
||
echo $themes->green(" (Document updated)").PHP_EOL;
|
||
} catch(Exception $e) {
|
||
echo $themes->error(" (Timeout)").PHP_EOL;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} /* }}} */
|
||
|
||
$themes = new \AlecRabbit\ConsoleColour\Themes();
|
||
|
||
$db = new SeedDMS_Core_DatabaseAccess($settings->_dbDriver, $settings->_dbHostname, $settings->_dbUser, $settings->_dbPass, $settings->_dbDatabase);
|
||
$db->connect() or die ("Could not connect to db-server \"" . $settings->_dbHostname . "\"");
|
||
|
||
$dms = new SeedDMS_Core_DMS($db, $settings->_contentDir.$settings->_contentOffsetDir);
|
||
if(!$settings->_doNotCheckDBVersion && !$dms->checkVersion()) {
|
||
echo "Database update needed.".PHP_EOL;
|
||
exit(1);
|
||
}
|
||
|
||
$dms->setRootFolderID($settings->_rootFolderID);
|
||
|
||
if($recreate)
|
||
$index = $indexconf['Indexer']::create($settings->_luceneDir);
|
||
else
|
||
$index = $indexconf['Indexer']::open($settings->_luceneDir);
|
||
if(!$index) {
|
||
echo "Could not create index.".PHP_EOL;
|
||
exit(1);
|
||
}
|
||
|
||
$indexconf['Indexer']::init($settings->_stopWordsFile);
|
||
|
||
$folder = $dms->getFolder($settings->_rootFolderID);
|
||
tree($dms, $index, $indexconf, $folder);
|
||
|
||
$index->commit();
|
||
$index->optimize();
|
||
?>
|