seeddms-code/utils/indexer.php

132 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2020-02-03 17:28:45 +00:00
if(isset($_SERVER['SEEDDMS_HOME'])) {
ini_set('include_path', $_SERVER['SEEDDMS_HOME'].'/utils'. PATH_SEPARATOR .ini_get('include_path'));
$myincpath = $_SERVER['SEEDDMS_HOME'];
2020-02-03 17:28:45 +00:00
} else {
ini_set('include_path', dirname($argv[0]). PATH_SEPARATOR .ini_get('include_path'));
$myincpath = dirname($argv[0]);
2020-02-03 17:28:45 +00:00
}
function usage() { /* {{{ */
echo "Usage:".PHP_EOL;
echo " seeddms-indexer [-h] [-v] [-c] [--config <file>]".PHP_EOL;
2020-09-04 07:21:40 +00:00
echo PHP_EOL;
echo "Description:".PHP_EOL;
echo " This program recreates the full text index of SeedDMS.".PHP_EOL;
2020-09-04 07:21:40 +00:00
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;
} /* }}} */
2017-12-22 12:05:35 +00:00
$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'])) {
2020-09-04 07:21:40 +00:00
echo $version.PHP_EOL;
exit(0);
}
/* Set alternative config file */
if(isset($options['config'])) {
define('SEEDDMS_CONFIG_FILE', $options['config']);
} elseif(isset($_SERVER['SEEDDMS_CONFIG_FILE'])) {
define('SEEDDMS_CONFIG_FILE', $_SERVER['SEEDDMS_CONFIG_FILE']);
}
/* recreate index */
$recreate = false;
if(isset($options['c'])) {
$recreate = true;
}
include($myincpath."/inc/inc.Settings.php");
include($myincpath."/inc/inc.Init.php");
include($myincpath."/inc/inc.Extension.php");
include($myincpath."/inc/inc.DBInit.php");
function tree($dms, $fulltextservice, $folder, $indent='') { /* {{{ */
global $settings, $themes;
echo $themes->black($indent."D ".$folder->getName()).PHP_EOL;
$subfolders = $folder->getSubFolders();
foreach($subfolders as $subfolder) {
tree($dms, $fulltextservice, $subfolder, $indent.' ');
}
$documents = $folder->getDocuments();
$index = $fulltextservice->Indexer();
$lucenesearch = $fulltextservice->Search();
foreach($documents as $document) {
2020-09-04 07:21:40 +00:00
echo $themes->black($indent." ".$document->getId().":".$document->getName()." ");
if(!($hit = $lucenesearch->getDocument($document->getId()))) {
try {
$idoc = $fulltextservice->IndexedDocument($document, true);
2018-11-13 07:47:46 +00:00
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 = $fulltextservice->IndexedDocument($document, true);
2018-11-13 07:47:46 +00:00
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();
$index = $fulltextservice->Indexer($recreate);
if(!$index) {
2020-09-04 07:21:40 +00:00
echo $themes->error("Could not create index.").PHP_EOL;
exit(1);
}
$folder = $dms->getFolder($settings->_rootFolderID);
tree($dms, $fulltextservice, $folder);
$index->commit();
$index->optimize();