2015-08-10 19:39:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of an indexed document
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS_SQLiteFTS
|
|
|
|
* @license GPL 2
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2010, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @uses SeedDMS_SQLiteFTS_Document
|
|
|
|
*/
|
|
|
|
require_once('Document.php');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for managing an indexed document.
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS_SQLiteFTS
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2011, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
class SeedDMS_SQLiteFTS_IndexedDocument extends SeedDMS_SQLiteFTS_Document {
|
|
|
|
|
2018-04-11 13:31:35 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-30 05:28:19 +00:00
|
|
|
protected $errormsg;
|
|
|
|
|
2018-04-11 13:31:35 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $mimetype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $cmd;
|
|
|
|
|
2015-08-10 19:39:05 +00:00
|
|
|
static function execWithTimeout($cmd, $timeout=2) { /* {{{ */
|
|
|
|
$descriptorspec = array(
|
|
|
|
0 => array("pipe", "r"),
|
|
|
|
1 => array("pipe", "w"),
|
|
|
|
2 => array("pipe", "w")
|
|
|
|
);
|
|
|
|
$pipes = array();
|
2018-01-30 05:28:19 +00:00
|
|
|
|
2017-03-01 15:18:27 +00:00
|
|
|
$timeout += time();
|
2015-08-10 19:39:05 +00:00
|
|
|
$process = proc_open($cmd, $descriptorspec, $pipes);
|
|
|
|
if (!is_resource($process)) {
|
|
|
|
throw new Exception("proc_open failed on: " . $cmd);
|
|
|
|
}
|
2018-01-30 05:28:19 +00:00
|
|
|
stream_set_blocking($pipes[1], 0);
|
|
|
|
stream_set_blocking($pipes[2], 0);
|
2015-08-10 19:39:05 +00:00
|
|
|
|
2018-01-30 05:28:19 +00:00
|
|
|
$output = $error = '';
|
2016-07-04 18:51:40 +00:00
|
|
|
$timeleft = $timeout - time();
|
2018-01-30 05:28:19 +00:00
|
|
|
$read = array($pipes[1], $pipes[2]);
|
2016-07-04 18:51:40 +00:00
|
|
|
$write = NULL;
|
|
|
|
$exeptions = NULL;
|
2015-08-10 19:39:05 +00:00
|
|
|
do {
|
2018-01-30 05:28:19 +00:00
|
|
|
$num_changed_streams = stream_select($read, $write, $exeptions, $timeleft, 200000);
|
|
|
|
|
|
|
|
if ($num_changed_streams === false) {
|
|
|
|
proc_terminate($process);
|
|
|
|
throw new Exception("stream select failed on: " . $cmd);
|
|
|
|
} elseif ($num_changed_streams > 0) {
|
2015-08-10 19:39:05 +00:00
|
|
|
$output .= fread($pipes[1], 8192);
|
2018-01-30 05:28:19 +00:00
|
|
|
$error .= fread($pipes[2], 8192);
|
2017-03-01 15:18:27 +00:00
|
|
|
}
|
2016-07-04 18:51:40 +00:00
|
|
|
$timeleft = $timeout - time();
|
2015-08-10 19:39:05 +00:00
|
|
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
|
|
|
|
|
|
|
if ($timeleft <= 0) {
|
|
|
|
proc_terminate($process);
|
|
|
|
throw new Exception("command timeout on: " . $cmd);
|
|
|
|
} else {
|
2018-01-30 05:28:19 +00:00
|
|
|
return array('stdout'=>$output, 'stderr'=>$error);
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor. Creates our indexable document and adds all
|
|
|
|
* necessary fields to it using the passed in document
|
|
|
|
*/
|
2018-01-30 05:28:19 +00:00
|
|
|
public function __construct($dms, $document, $convcmd=null, $nocontent=false, $timeout=5) { /* {{{ */
|
|
|
|
$this->errormsg = '';
|
2018-04-11 13:31:35 +00:00
|
|
|
$this->cmd = '';
|
|
|
|
$this->mimetype = '';
|
2015-08-10 19:39:05 +00:00
|
|
|
|
|
|
|
$this->addField('title', $document->getName());
|
2020-12-12 15:27:53 +00:00
|
|
|
if($acllist = $document->getReadAccessList(1, 1, 1)) {
|
|
|
|
$allu = [];
|
|
|
|
foreach($acllist['users'] as $u)
|
|
|
|
$allu[] = $u->getLogin();
|
|
|
|
$this->addField('users', implode(' ', $allu));
|
|
|
|
/*
|
|
|
|
$allg = [];
|
|
|
|
foreach($acllist['groups'] as $g)
|
|
|
|
$allg[] = $g->getName();
|
|
|
|
$this->addField('groups', implode(' ', $allg));
|
|
|
|
*/
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
|
|
|
if($attributes = $document->getAttributes()) {
|
|
|
|
foreach($attributes as $attribute) {
|
|
|
|
$attrdef = $attribute->getAttributeDefinition();
|
|
|
|
if($attrdef->getValueSet() != '')
|
|
|
|
$this->addField('attr_'.str_replace(' ', '_', $attrdef->getName()), $attribute->getValue());
|
|
|
|
else
|
|
|
|
$this->addField('attr_'.str_replace(' ', '_', $attrdef->getName()), $attribute->getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$owner = $document->getOwner();
|
|
|
|
$this->addField('owner', $owner->getLogin());
|
2020-12-21 07:14:54 +00:00
|
|
|
$this->addField('path', str_replace(':', 'x', $document->getFolderList()));
|
2015-08-10 19:39:05 +00:00
|
|
|
if($comment = $document->getComment()) {
|
|
|
|
$this->addField('comment', $comment);
|
|
|
|
}
|
2020-12-12 15:27:53 +00:00
|
|
|
|
|
|
|
if($document->isType('document')) {
|
|
|
|
$this->addField('document_id', 'D'.$document->getID());
|
|
|
|
$version = $document->getLatestContent();
|
|
|
|
if($version) {
|
|
|
|
$this->addField('mimetype', $version->getMimeType());
|
|
|
|
$this->addField('origfilename', $version->getOriginalFileName());
|
|
|
|
if(!$nocontent)
|
|
|
|
$this->addField('created', $version->getDate(), 'unindexed');
|
|
|
|
if($attributes = $version->getAttributes()) {
|
|
|
|
foreach($attributes as $attribute) {
|
|
|
|
$attrdef = $attribute->getAttributeDefinition();
|
|
|
|
if($attrdef->getValueSet() != '')
|
|
|
|
$this->addField('attr_'.str_replace(' ', '_', $attrdef->getName()), $attribute->getValue());
|
|
|
|
else
|
|
|
|
$this->addField('attr_'.str_replace(' ', '_', $attrdef->getName()), $attribute->getValue());
|
|
|
|
}
|
2019-12-31 14:42:15 +00:00
|
|
|
}
|
2020-12-12 15:27:53 +00:00
|
|
|
}
|
|
|
|
if($categories = $document->getCategories()) {
|
|
|
|
$names = array();
|
|
|
|
foreach($categories as $cat) {
|
|
|
|
$names[] = $cat->getName();
|
|
|
|
}
|
|
|
|
$this->addField('category', implode(' ', $names));
|
|
|
|
}
|
|
|
|
if($keywords = $document->getKeywords()) {
|
|
|
|
$this->addField('keywords', $keywords);
|
|
|
|
}
|
|
|
|
if($version) {
|
|
|
|
$status = $version->getStatus();
|
|
|
|
$this->addField('status', $status['status']+10);
|
|
|
|
}
|
|
|
|
if($version && !$nocontent) {
|
|
|
|
$path = $dms->contentDir . $version->getPath();
|
|
|
|
if(file_exists($path)) {
|
|
|
|
$content = '';
|
|
|
|
$mimetype = $version->getMimeType();
|
|
|
|
$this->mimetype = $mimetype;
|
|
|
|
$cmd = '';
|
|
|
|
$mimeparts = explode('/', $mimetype, 2);
|
|
|
|
if(isset($convcmd[$mimetype])) {
|
|
|
|
$cmd = sprintf($convcmd[$mimetype], $path);
|
|
|
|
} elseif(isset($convcmd[$mimeparts[0].'/*'])) {
|
|
|
|
$cmd = sprintf($convcmd[$mimetype], $path);
|
|
|
|
} elseif(isset($convcmd['*'])) {
|
|
|
|
$cmd = sprintf($convcmd[$mimetype], $path);
|
|
|
|
}
|
|
|
|
if($cmd) {
|
|
|
|
$this->cmd = $cmd;
|
|
|
|
try {
|
|
|
|
$content = self::execWithTimeout($cmd, $timeout);
|
|
|
|
if($content['stdout']) {
|
|
|
|
$this->addField('content', $content['stdout'], 'unstored');
|
|
|
|
}
|
|
|
|
if($content['stderr']) {
|
|
|
|
$this->errormsg = $content['stderr'];
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2019-12-31 14:42:15 +00:00
|
|
|
}
|
2017-03-01 14:56:12 +00:00
|
|
|
}
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 15:27:53 +00:00
|
|
|
} elseif($document->isType('folder')) {
|
|
|
|
$this->addField('document_id', 'F'.$document->getID());
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
2018-01-30 05:28:19 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
public function getErrorMsg() { /* {{{ */
|
|
|
|
return $this->errormsg;
|
|
|
|
} /* }}} */
|
2018-04-11 13:31:35 +00:00
|
|
|
|
|
|
|
public function getMimeType() { /* {{{ */
|
|
|
|
return $this->mimetype;
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
public function getCmd() { /* {{{ */
|
|
|
|
return $this->cmd;
|
|
|
|
} /* }}} */
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
|
|
|
?>
|