Go to file
Ingo Schommer fb86f8a626 Fixed tests for 3.2
Partial merge from master on 2977f85cb5
2016-03-04 15:26:29 +13:00
_config NEW SolrCellTextExtractor 2013-02-01 15:35:16 +01:00
code 3.1 compat 2013-05-07 21:54:51 +02:00
tests Fixed tests for 3.2 2016-03-04 15:26:29 +13:00
_config.php Initial commit 2012-08-22 17:52:08 +02:00
.travis.yml Don't test against master, not supported 2016-03-04 13:58:28 +13:00
composer.json Fix description in composer.json 2013-03-13 23:59:40 +01:00
LICENSE Added License 2012-08-22 23:23:34 +02:00
README.md Made readme example less specific 2014-02-18 10:28:02 +13:00

Text Extraction Module

Build Status

Overview

Provides an extraction API for file content, which can hook into different extractor engines based on availability and the parsed file format. The output is always a string: the file content.

Via the FileTextExtractable extension, this logic can be used to cache the extracted content on a DataObject subclass (usually File).

Note: Previously part of the sphinx module.

Requirements

Supported Formats

  • HTML (built-in)
  • PDF (with XPDF or Solr)
  • Microsoft Word, Excel, Powerpoint (Solr)
  • OpenOffice (Solr)
  • CSV (Solr)
  • RTF (Solr)
  • EPub (Solr)

Installation

The recommended installation is through composer. Add the following to your composer.json:

```js
{
	"require": {
		"silverstripe/textextraction": "*"
	}
}
```

The module depends on the Guzzle HTTP Library, which is automatically checked out by composer. Alternatively, install Guzzle through PEAR and ensure its in your include_path.

Configuration

Basic

By default, only extraction from HTML documents is supported. No configuration is required for that, unless you want to make the content available through your DataObject subclass. In this case, add the following to mysite/_config.php:

DataObject::add_extension('File', 'FileTextExtractable');

XPDF

PDFs require special handling, for example through the XPDF commandline utility. Follow their installation instructions, its presence will be automatically detected. You can optionally set the binary path in mysite/_config/config.yml:

```yml
PDFTextExtractor:
	binary_location: /my/path/pdftotext
```

Apache Solr

Apache Solr is a fulltext search engine, an aspect which is often used alongside this module. But more importantly for us, it has bindings to Apache Tika through the ExtractingRequestHandler interface. This allows Solr to inspect the contents of various file formats, such as Office documents and PDF files. The textextraction module retrieves the output of this service, rather than altering the index. With the raw text output, you can decide to store it in a database column for fulltext search in your database driver, or even pass it back to Solr as part of a full index update.

In order to use Solr, you need to configure a URL for it (in mysite/_config/config.yml):

```yml
SolrCellTextExtractor:
	base_url: 'http://localhost:8983/solr/update/extract'
```

Note that in case you're using multiple cores, you'll need to add the core name to the URL (e.g. 'http://localhost:8983/solr/PageSolrIndex/update/extract'). The "fulltext" module uses multiple cores by default, and comes prepackaged with a Solr server. Its a stripped-down version of Solr, follow the module README on how to add Apache Tika text extraction capabilities.

You need to ensure that some indexable property on your object returns the contents, either by directly accessing FileTextExtractable->extractFileAsText(), or by writing your own method around FileTextExtractor->getContent() (see "Usage" below). The property should be listed in your SolrIndex subclass, e.g. as follows:

```php
class MyDocument extends DataObject {
	static $db = array('Path' => 'Text');
	function getContent() {
		$extractor = FileTextExtractor::for_file($this->Path);
		return $extractor ? $extractor->getContent($this->Path) : null;		
	}
}
class MySolrIndex extends SolrIndex {
	function init() {
		$this->addClass('MyDocument');
		$this->addFulltextField('Content', 'HTMLText');
	}
}
```

Note: This isn't a terribly efficient way to process large amounts of files, since each HTTP request is run synchronously.

Usage

Manual extraction:

$myFile = '/my/path/myfile.pdf';
$extractor = FileTextExtractor::for_file($myFile);
$content = $extractor->getContent($myFile);

Extraction with FileTextExtractable extension applied:

$myFileObj = File::get()->First();
$content = $myFileObj->extractFileAsText();