XmlRpcLucene.inc.php 14.4 KB
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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
<?php

/**
 * $Id:$
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008, 2009, 2010 KnowledgeTree Inc.
 *
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 *
 */

require_once('xmlrpc.inc');

class XmlRpcLucene
{
	/**
	 * Reference to the xmlrpc client
	 *
	 * @var xmlrpc_client
	 */
	var $client;

	/**
	 * Identifier for the KT instance
	 *
	 * @var string
	 */
	var $ktid;

	/**
	 * Identifier for the lucene server
	 *
	 * @var string
	 */
	var $authToken;

	/**
	 * The constructoor for the lucene XMLRPC client.
	 *
	 * @param string $url
	 * @param int $port
	 */
	public function __construct($url)
	{
		$this->client=new xmlrpc_client("$url/xmlrpc");
		$this->client->request_charset_encoding = 'UTF-8';
		$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';

		$config = KTConfig::getSingleton();
		$this->authToken = $config->get('indexer/luceneAuthToken','');
		$this->ktid = $config->get('indexer/luceneID','');
	}

	public static function get($url)
	{
	    static $singleton = null;

	    if(is_null($singleton)){
	        $singleton = new XmlRpcLucene($url);
	    }
        return $singleton;
	}

	/**
	 * Set a level for debugging.
	 *
	 * @param int $level
	 */
	function debug($level)
	{
		$this->client->setDebug($level);
	}

	/**
	 * Logs errors to the log file
	 *
	 * @param xmlrpcresult $result
	 * @param string $function
	 */
	function error($result, $function)
	{
		global $default;
		$default->log->error('XMLRPC Lucene - ' . $function . ' - Code: ' . htmlspecialchars($result->faultCode()));
		$default->log->error('XMLRPC Lucene - ' . $function . ' - Reason: ' . htmlspecialchars($result->faultString()));
	}

	/**
	 * Optimise the lucene index.
	 *
	 * @return boolean
	 */
	function optimise()
	{
		$function=new xmlrpcmsg('indexer.optimise',
			array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken)
			));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'optimise');
			return false;
		}
		return php_xmlrpc_decode($result->value()) == 0;
	}

	/**
	 * Add a document to lucene
	 *
	 * @param int $documentid
	 * @param string $contentFile
	 * @param string $discussion
	 * @param string $title
	 * @param string $version
	 * @return boolean
	 */
	function addDocument($documentid, $contentFile, $discussion, $title, $version)
	{
		$function=new xmlrpcmsg('indexer.addDocument',
			array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken),
				php_xmlrpc_encode((int) $documentid),
				php_xmlrpc_encode((string) $contentFile),
				php_xmlrpc_encode((string) $discussion),
				php_xmlrpc_encode((string) $title),
				php_xmlrpc_encode((string) $version)
			));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'addDocument');
			return false;
		}
		return php_xmlrpc_decode($result->value()) == 0;
	}

	/**
	 * Remove the document from the index.
	 *
	 * @param int $documentid
	 * @return boolean
	 */
	function deleteDocument($documentid)
	{
		$function=new xmlrpcmsg('indexer.deleteDocument',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken),
				php_xmlrpc_encode((int) $documentid)));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'deleteDocument');
			return false;
		}
		return php_xmlrpc_decode($result->value()) == 0;
	}

	/**
	 * Does the document exist?
	 *
	 * @param int $documentid
	 * @return boolean
	 */
	function documentExists($documentid)
	{
		$function=new xmlrpcmsg('indexer.documentExists',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken),
				php_xmlrpc_encode((int) $documentid)));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'documentExists');
			return false;
		}
		return php_xmlrpc_decode($result->value());
	}

	/**
	 * Get statistics from the indexer
	 *
	 * @return array
	 */
	function getStatistics()
	{
		$function=new xmlrpcmsg('indexer.getStatistics',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken)));


		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'getStatistics');
			return false;
		}

		$result = php_xmlrpc_decode($result->value());

		//print $result;

		return json_decode($result);
	}

	/**
	 * Run a query on the lucene index
	 *
	 * @param string $query
	 * @return boolean
	 */
	function query($query)
	{
		$function=new xmlrpcmsg('indexer.query',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken),
				php_xmlrpc_encode((string) $query)));

		$result=&$this->client->send($function, 60);
		if($result->faultCode())
		{
			$this->error($result, 'query');
			return false;
		}

		$result = php_xmlrpc_decode($result->value());
		return json_decode($result);
	}

	/**
	 * Updates the discussion text on a given document.
	 *
	 * @param int $docid
	 * @param string $discussion
	 * @return boolean
	 */
	function updateDiscussion($docid, $discussion)
	{
		$function=new xmlrpcmsg('indexer.updateDiscussion',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken),
				php_xmlrpc_encode((int) $docid),
				php_xmlrpc_encode((string) $discussion)));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'updateDiscussion');
			return false;
		}
		return php_xmlrpc_decode($result->value()) == 0;
	}

	/**
	 * Extracts the text from a given document and writes it to the target file
	 *
	 * @param string $sourceFile The full path to the document
	 * @param string $targetFile The full path to the target / output file
	 * @return boolean true on success | false on failure
	 */
	function extractTextContent($sourceFile, $targetFile)
    {
        $function = new xmlrpcmsg('textextraction.getTextFromFile',
            array(
                php_xmlrpc_encode((string) $sourceFile),
                php_xmlrpc_encode((string) $targetFile)
            )
        );

        $result =& $this->client->send($function, 120);

        if($result->faultCode()) {
            $this->error($result, 'extractTextContent');
            return false;
        }
		return php_xmlrpc_decode($result->value()) == 0;
    }

    /**
	 * Extracts the text from a given document stream
	 *
	 * @param string $content The document content
	 * @return string The extracted text on success | false on failure
	 */
    function extractTextContentByStreaming($content)
    {
        $function = new xmlrpcmsg('textextraction.getText',
            array(
                new xmlrpcval($content, 'base64'))
            );
        $result =& $this->client->send($function, 120);

        unset($content);

        if($result->faultCode()) {
            $this->error($result, 'extractTextContent');
            return false;
        }

        $obj = php_xmlrpc_decode($result->value());

        $extractedText = trim($obj['text']);
        return $extractedText;
    }

    /**
     * Writes a given set of custom properties to a document
     *
	 * @param string $sourceFile The full path to the document
	 * @param string $targetFile The full path to the target / output file
     * @param array $properties Associative array of the properties to be added
	 * @return boolean true on success | false on failure
     */
    function writeProperties($sourceFile, $targetFile, $properties)
    {
        $function = new xmlrpcmsg('metadata.writeProperty',
        array(
            php_xmlrpc_encode((string) $sourceFile),
            php_xmlrpc_encode((string) $targetFile),
            php_xmlrpc_encode($properties)
        ));

        $result =& $this->client->send($function);

        if($result->faultCode()) {
            $this->error($result, 'writeProperties');
            return false;
        }

        return php_xmlrpc_decode($result->value()) == 0;
    }

    /**
     * Read the custom document properties
     *
	 * @param string $sourceFile The full path to the document
     * @return array The properties as an associative array | False on failure
     */
    function readProperties($sourceFile, $property)
    {
        $function = new xmlrpcmsg('metadata.readMetadata',
        array(
            php_xmlrpc_encode((string) $sourceFile),
            php_xmlrpc_encode((string) $property)
        ));

        $result =& $this->client->send($function);

        if($result->faultCode()) {
            $this->error($result, 'readProperties');
            return false;
        }

        $obj = php_xmlrpc_decode($result->value());

        if($obj['status'] != '0') {
            return false;
        }

        return $obj['metadata'];
    }

    /**
     * Writes a given set of custom properties to an OOXML document (MS Office 2007)
     *
	 * @param string $sourceFile The full path to the document
	 * @param string $targetFile The full path to the target / output file
	 * @param int $type 1 = docx, 2 = xlsx, 3 = pptx
     * @param array $properties Associative array of the properties to be added
	 * @return boolean true on success | false on failure
     */
    function writeOOXMLProperties($sourceFile, $targetFile, $type, $properties)
    {
        $function = new xmlrpcmsg('metadata.writeOOXMLProperty',
        array(
            php_xmlrpc_encode((string) $sourceFile),
            php_xmlrpc_encode((string) $targetFile),
            php_xmlrpc_encode((int) $type),
            php_xmlrpc_encode($properties)
        ));

        $result =& $this->client->send($function);

        if($result->faultCode()) {
            $this->error($result, 'writeOOXMLProperties');
            return false;
        }

        return php_xmlrpc_decode($result->value()) == 0;
    }

    /**
     * Read the custom document properties of an OOXML document (MS Office 2007)
     *
	 * @param string $sourceFile The full path to the document
	 * @param int $type 1 = docx, 2 = xlsx, 3 = pptx
	 * @param string $property The name of the property to read
     * @return array The properties as an associative array | False on failure
     */
    function readOOXMLProperty($sourceFile, $type, $property)
    {
        $function = new xmlrpcmsg('metadata.readOOXMLProperty',
        array(
            php_xmlrpc_encode((string) $sourceFile),
            php_xmlrpc_encode((int) $type),
            php_xmlrpc_encode((string) $property)
        ));

        $result =& $this->client->send($function);

        if($result->faultCode()) {
            $this->error($result, 'readOOXMLProperty');
            return false;
        }

        $obj = php_xmlrpc_decode($result->value());

        if($obj['status'] != '0') {
            return false;
        }

        return $obj['metadata'];
    }

    /**
     * Converts a document to the format of the given target file based on the extension of both files.
     *
     * @param string $sourceFile The full path of the document to be converted, with extension.
     * @param string $targetFile The full path of the file to save the converted document with the desired extension.
     * @param string $ooHost The host domain or IP address on which OpenOffice is running
     * @param string $ooPort The port on which OpenOffice is listening.
     * @return boolean
     */
    function convertDocument($sourceFile, $targetFile, $ooHost, $ooPort)
    {
        $function = new xmlrpcmsg('openoffice.convertDocument',
            array(
                php_xmlrpc_encode((string) $sourceFile),
                php_xmlrpc_encode((string) $targetFile),
                php_xmlrpc_encode((string) $ooHost),
                php_xmlrpc_encode((int) $ooPort)
            )
        );

        $result=&$this->client->send($function, 120);

        if($result->faultCode()) {
            $this->error($result, 'convertDocument');
            return $result->faultString();
        }
        return php_xmlrpc_decode($result->value()) == 0;
    }

    /**
     * Convert document to given format. Defaults to pdf
     *
     * @deprecated
     * @param $content
     * @param $toExtension
     * @return unknown_type
     */
    function convertDocumentStreamed($content, $toExtension = 'pdf')
    {
        $function = new xmlrpcmsg('openoffice.convertDocument',
            array(
                new xmlrpcval($content, 'base64'),
                php_xmlrpc_encode((string)$toExtension)
            ));

        $result=&$this->client->send($function, 120);

        unset($content);

        if($result->faultCode()) {
            $this->error($result, 'convertDocument');
            return false;
        }

        $obj = php_xmlrpc_decode($result->value());

        if($obj['status'] != '0') {
            return false;
        }

        return $obj['data'];
    }

    function shutdown()
	{
		$function=new xmlrpcmsg('control.shutdown',array(
				php_xmlrpc_encode((string) $this->ktid),
				php_xmlrpc_encode((string) $this->authToken)));

		$result=&$this->client->send($function);
		if($result->faultCode())
		{
			$this->error($result, 'shutdown');
			return false;
		}
		return true;
	}


}

?>