ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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;
// 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();
?>
An exception for terminatinating execution or to throw for unit testing.
The main class for parsing RIS files.
Definition: RISReader.php:60

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
69 }

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

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

◆ getRecords()

LibRIS\RISReader::getRecords ( )

Definition at line 155 of file RISReader.php.

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

References $data.

◆ parseArray()

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.

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

References data.

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

85 {
86 if (!is_file($filename)) {
87 throw new ParseException(sprintf('File %s not found.', htmlentities($filename)));
88 }
89 $flags = FILE_SKIP_EMPTY_LINES | FILE_TEXT;
90 $contents = file($filename, $flags, $context);
91
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:116
$context
Definition: webdav.php:25

References $context, and $filename.

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

108 {
109 $contents = explode (RISReader::RIS_EOL, $string);
110 $this->parseArray($contents);
111 }

◆ printRecords()

LibRIS\RISReader::printRecords ( )

Definition at line 159 of file RISReader.php.

159 {
160 $format = "%s:\n\t%s\n";
161 foreach ($this->data as $record) {
162 foreach ($record as $key => $values) {
163 foreach ($values as $value) {
164 printf($format, RISTags::describeTag($key), $value);
165 }
166 }
167
169 }
170 }
if(! $in) print
static describeTag($tag)
Definition: RISTags.php:15
PHP_EOL
Definition: complexTest.php:7
$key
Definition: croninfo.php:18
$format
Definition: metadata.php:141
$values

References $format, $key, $values, data, PHP_EOL, and print.

Field Documentation

◆ $data

LibRIS\RISReader::$data = NULL
protected

Definition at line 65 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: