ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
LibRIS\RISReader Class Reference

The main class for parsing RIS files. More...

+ Collaboration diagram for LibRIS\RISReader:

Public Member Functions

 __construct ($options=array())
 parseFile ($filename, $context=NULL)
 Parse an RIS file.
 parseString ($string)
 Parse a string of RIS data.
 getRecords ()
 printRecords ()

Data Fields

const RIS_EOL = "\r\n"
const LINE_REGEX = '/^(([A-Z1-9]{2})\s+-(.*))|(.*)$/'

Protected Member Functions

 parseArray ($lines)
 Take an array of lines and parse them into an RIS record.
 cleanData (&$lines)
 Clean up the data before processing.

Protected Attributes

 $data = NULL

Detailed Description

The main class for parsing RIS files.

Usage:

<?php
use \LibRIS\RISReader;
$reader = new RISReader();
// Parse a file of RIS data.
$reader->parseFile('path/to/file.ris');
// Parse a string containing RIS data.
$reader->parseString($someRisString);
// Parse an array of lines.
$reader->parseArray($arrayOfRISDirectives);
// Get an associative array of records.
$array = $reader->getRecords();
// Dump the records to STDOUT
$reader->printRecords();
?>

The data structure generated by this class is of the form

<?php
array(
[0] => array(
'T1' => array('title one', 'title 2'),
'TY' => array('JOUR'),
// Other tags and their values.
),
[1] => array(
'T1' => array('another entry'),
'TY' => array('JOUR'),
),
);
?>

Definition at line 60 of file RISReader.php.

Constructor & Destructor Documentation

LibRIS\RISReader::__construct (   $options = array())

Definition at line 67 of file RISReader.php.

{
}

Member Function Documentation

LibRIS\RISReader::cleanData ( $lines)
protected

Clean up the data before processing.

Parameters
array$linesIndexed array of lines of data.

Definition at line 178 of file RISReader.php.

{
if (empty($lines)) return;
// Currently, we only need to strip a BOM if it exists.
// Thanks to Derik Badman (http://madinkbeard.com/) for finding the
// bug and suggesting this fix:
// http://blog.philipp-michels.de/?p=32
$first = $lines[0];
if (substr($first, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
$lines[0] = substr($first, 3);
}
}
LibRIS\RISReader::getRecords ( )

Definition at line 155 of file RISReader.php.

{
return $this->data;
}
LibRIS\RISReader::parseArray (   $lines)
protected

Take an array of lines and parse them into an RIS record.

Definition at line 116 of file RISReader.php.

{
$recordset = array();
// Do any cleaning and normalizing.
$this->cleanData($lines);
$record = array();
$lastTag = NULL;
foreach ($lines as $line) {
$line = trim($line);
$matches = array();
preg_match(self::LINE_REGEX, $line, $matches);
if (!empty($matches[3])) {
$lastTag = $matches[2];
$record[$matches[2]][] = trim($matches[3]);
}
// End record and prep a new one.
elseif (!empty($matches[2]) && $matches[2] == 'ER') {
$lastTag = NULL;
$recordset[] = $record;
$record = array();
}
elseif (!empty($matches[4])) {
// Append to the last one.
// We skip leading info (like BOMs).
if (!empty($lastTag)) {
$lastEntry = count($record[$lastTag]) - 1;
// We trim because some encoders add tabs or multiple spaces.
// Standard is silent on how this should be handled.
$record[$lastTag][$lastEntry] .= ' ' . trim($matches[4]);
}
}
}
if (!empty($record)) $recordset[] = $record;
$this->data = $recordset;
}
LibRIS\RISReader::parseFile (   $filename,
  $context = NULL 
)

Parse an RIS file.

This will parse the file and return a data structure representing the record.

Parameters
string$filenameThe full path to the file to parse.
StreamContext$contextThe stream context (in desired) for handling the file.
Return values
arrayAn indexed array of individual sources, each of which is an associative array of entry details. (See LibRIS)

Definition at line 85 of file RISReader.php.

References $filename.

{
if (!is_file($filename)) {
throw new ParseException(sprintf('File %s not found.', htmlentities($filename)));
}
$flags = FILE_SKIP_EMPTY_LINES | FILE_TEXT;
$contents = file($filename, $flags, $context);
$this->parseArray($contents);
}
LibRIS\RISReader::parseString (   $string)

Parse a string of RIS data.

This will parse an RIS record into a representative data structure.

Parameters
string$stringRIS-formatted data in a string.
StreamContext$contextThe stream context (in desired) for handling the file.
Return values
arrayAn indexed array of individual sources, each of which is an associative array of entry details. (See LibRIS)

Definition at line 108 of file RISReader.php.

{
$contents = explode (RISReader::RIS_EOL, $string);
$this->parseArray($contents);
}
LibRIS\RISReader::printRecords ( )

Definition at line 159 of file RISReader.php.

{
$format = "%s:\n\t%s\n";
foreach ($this->data as $record) {
foreach ($record as $key => $values) {
foreach ($values as $value) {
printf($format, RISTags::describeTag($key), $value);
}
}
print PHP_EOL;
}
}

Field Documentation

LibRIS\RISReader::$data = NULL
protected

Definition at line 65 of file RISReader.php.

const LibRIS\RISReader::LINE_REGEX = '/^(([A-Z1-9]{2})\s+-(.*))|(.*)$/'

Definition at line 63 of file RISReader.php.

const LibRIS\RISReader::RIS_EOL = "\r\n"

Definition at line 62 of file RISReader.php.


The documentation for this class was generated from the following file: