ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules 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. More...
 
 parseString ($string)
 Parse a string of RIS data. More...
 
 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. More...
 
 cleanData (&$lines)
 Clean up the data before processing. More...
 

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

◆ __construct()

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

Definition at line 67 of file RISReader.php.

67  {
68  }

Member Function Documentation

◆ cleanData()

LibRIS\RISReader::cleanData ( $lines)
protected

Clean up the data before processing.

Parameters
array$linesIndexed array of lines of data.

Definition at line 179 of file RISReader.php.

179  {
180  if (empty($lines)) {
181  return;
182  }
183  // Currently, we only need to strip a BOM if it exists.
184  // Thanks to Derik Badman (http://madinkbeard.com/) for finding the
185  // bug and suggesting this fix:
186  // http://blog.philipp-michels.de/?p=32
187  $first = $lines[0];
188  if (substr($first, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
189  $lines[0] = substr($first, 3);
190  }
191  }

◆ getRecords()

LibRIS\RISReader::getRecords ( )

Definition at line 155 of file RISReader.php.

References $data.

155  {
156  return $this->data;
157  }

◆ parseArray()

LibRIS\RISReader::parseArray (   $lines)
protected

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

Definition at line 119 of file RISReader.php.

119  {
120  $recordset = array();
121  // Do any cleaning and normalizing.
122  $this->cleanData($lines);
123  $record = array();
124  $lastTag = NULL;
125  foreach ($lines as $line) {
126  $line = trim($line);
127  $matches = array();
128  preg_match(self::LINE_REGEX, $line, $matches);
129  if (! empty($matches[3])) {
130  $lastTag = $matches[2];
131  $record[$matches[2]][] = trim($matches[3]);
132  } // End record and prep a new one.
133  elseif (! empty($matches[2]) && $matches[2] == 'ER') {
134  $lastTag = NULL;
135  $recordset[] = $record;
136  $record = array();
137  } elseif (! empty($matches[4])) {
138  // Append to the last one.
139  // We skip leading info (like BOMs).
140  if (! empty($lastTag)) {
141  $lastEntry = count($record[$lastTag]) - 1;
142  // We trim because some encoders add tabs or multiple spaces.
143  // Standard is silent on how this should be handled.
144  $record[$lastTag][$lastEntry] .= ' ' . trim($matches[4]);
145  }
146  }
147  }
148  if (! empty($record)) {
149  $recordset[] = $record;
150  }
151  $this->data = $recordset;
152  }
cleanData(&$lines)
Clean up the data before processing.
Definition: RISReader.php:179

◆ parseFile()

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 86 of file RISReader.php.

References $filename.

86  {
87  if (! is_file($filename)) {
88  throw new ParseException(sprintf('File %s not found.', htmlentities($filename)));
89  }
90  $flags = FILE_SKIP_EMPTY_LINES | FILE_TEXT;
91  $contents = file($filename, $flags, $context);
92  $this->parseArray($contents);
93  }
$filename
Definition: buildRTE.php:89
parseArray($lines)
Take an array of lines and parse them into an RIS record.
Definition: RISReader.php:119

◆ parseString()

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 110 of file RISReader.php.

110  {
111  $contents = explode(RISReader::RIS_EOL, $string);
112  $this->parseArray($contents);
113  }
parseArray($lines)
Take an array of lines and parse them into an RIS record.
Definition: RISReader.php:119

◆ printRecords()

LibRIS\RISReader::printRecords ( )

Definition at line 160 of file RISReader.php.

160  {
161  $format = "%s:\n\t%s\n";
162  foreach ($this->data as $record) {
163  foreach ($record as $key => $values) {
164  foreach ($values as $value) {
165  printf($format, RISTags::describeTag($key), $value);
166  }
167  }
168  print PHP_EOL;
169  }
170  }
static describeTag($tag)
Definition: RISTags.php:17

Field Documentation

◆ $data

LibRIS\RISReader::$data = NULL
protected

Definition at line 64 of file RISReader.php.

◆ LINE_REGEX

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

Definition at line 63 of file RISReader.php.

◆ RIS_EOL

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: