ILIAS  release_4-4 Revision
Structures_BibTex Class Reference
+ Collaboration diagram for Structures_BibTex:

Public Member Functions

 Structures_BibTex ($options=array())
 Constructor. More...
 
 setOption ($option, $value)
 Sets run-time configuration options. More...
 
 loadFile ($filename)
 Reads a give BibTex File. More...
 
 parse ()
 Parses what is stored in content and clears the content if the parsing is successfull. More...
 
 _parseEntry ($entry)
 Extracting the data of one content. More...
 
 _checkEqualSign ($entry, $position)
 Checking whether the position of the '=' is correct. More...
 
 _checkAllowedEntryType ($entry)
 Checking if the entry type is allowed. More...
 
 _checkAt ($entry)
 Checking whether an at is outside an entry. More...
 
 _stripDelimiter ($entry)
 Stripping Delimiter. More...
 
 _unwrap ($entry)
 Unwrapping entry. More...
 
 _wordwrap ($entry)
 Wordwrap an entry. More...
 
 _extractAuthors ($entry)
 Extracting the authors. More...
 
 _determineCase ($word)
 Case Determination according to the needs of BibTex. More...
 
 _validateValue ($entry, $wholeentry)
 Validation of a value. More...
 
 _removeCurlyBraces ($value)
 Remove curly braces from entry. More...
 
 _generateWarning ($type, $entry, $wholeentry='')
 Generates a warning. More...
 
 clearWarnings ()
 Cleares all warnings. More...
 
 hasWarning ()
 Is there a warning? More...
 
 amount ()
 Returns the amount of available BibTex entries. More...
 
 _formatAuthor ($array)
 Returns the author formatted. More...
 
 bibTex ()
 Converts the stored BibTex entries to a BibTex String. More...
 
 addEntry ($newentry)
 Adds a new BibTex entry to the data. More...
 
 getStatistic ()
 Returns statistic. More...
 
 rtf ()
 Returns the stored data in RTF format. More...
 
 html ()
 Returns the stored data in HTML format. More...
 

Data Fields

 $data
 
 $content
 
 $_delimiters
 
 $warnings
 
 $_options
 
 $rtfstring
 
 $htmlstring
 
 $allowedEntryTypes
 
 $authorstring
 

Detailed Description

Definition at line 77 of file BibTex.php.

Member Function Documentation

◆ _checkAllowedEntryType()

Structures_BibTex::_checkAllowedEntryType (   $entry)

Checking if the entry type is allowed.

private

Parameters
string$entryThe entry to check
Returns
bool true if allowed, false otherwise

Definition at line 508 of file BibTex.php.

Referenced by _parseEntry().

509  {
510  return in_array($entry, $this->allowedEntryTypes);
511  }
+ Here is the caller graph for this function:

◆ _checkAt()

Structures_BibTex::_checkAt (   $entry)

Checking whether an at is outside an entry.

Sometimes an entry misses an entry brace. Then the at of the next entry seems to be inside an entry. This is checked here. When it is most likely that the at is an opening at of the next entry this method returns true.

private

Parameters
string$entryThe text of the entry until the at
Returns
bool true if the at is correct, false if the at is likely to begin the next entry.

Definition at line 524 of file BibTex.php.

References $ret.

Referenced by parse().

525  {
526  $ret = false;
527  $opening = array_keys($this->_delimiters);
528  $closing = array_values($this->_delimiters);
529  //Getting the value (at is only allowd in values)
530  if (strrpos($entry,'=') !== false) {
531  $position = strrpos($entry, '=');
532  $proceed = true;
533  if (substr($entry, $position-1, 1) == '\\') {
534  $proceed = false;
535  }
536  while (!$proceed) {
537  $substring = substr($entry, 0, $position);
538  $position = strrpos($substring,'=');
539  $proceed = true;
540  if (substr($entry, $position-1, 1) == '\\') {
541  $proceed = false;
542  }
543  }
544  $value = trim(substr($entry, $position+1));
545  $open = 0;
546  $char = '';
547  $lastchar = '';
548  for ($i = 0; $i < strlen($value); $i++) {
549  $char = substr($this->content, $i, 1);
550  if (in_array($char, $opening) && ('\\' != $lastchar)) {
551  $open++;
552  } elseif (in_array($char, $closing) && ('\\' != $lastchar)) {
553  $open--;
554  }
555  $lastchar = $char;
556  }
557  //if open is grater zero were are inside an entry
558  if ($open>0) {
559  $ret = true;
560  }
561  }
562  return $ret;
563  }
+ Here is the caller graph for this function:

◆ _checkEqualSign()

Structures_BibTex::_checkEqualSign (   $entry,
  $position 
)

Checking whether the position of the '=' is correct.

Sometimes there is a problem if a '=' is used inside an entry (for example abstract). This method checks if the '=' is outside braces then the '=' is correct and true is returned. If the '=' is inside braces it contains to a equation and therefore false is returned.

private

Parameters
string$entryThe text of the whole remaining entry
intthe current used place of the '='
Returns
bool true if the '=' is correct, false if it contains to an equation

Definition at line 450 of file BibTex.php.

References $ret.

Referenced by _parseEntry().

451  {
452  $ret = true;
453  //This is getting tricky
454  //We check the string backwards until the position and count the closing an opening braces
455  //If we reach the position the amount of opening and closing braces should be equal
456  $length = strlen($entry);
457  $open = 0;
458  for ($i = $length-1; $i >= $position; $i--) {
459  $precedingchar = substr($entry, $i-1, 1);
460  $char = substr($entry, $i, 1);
461  if (('{' == $char) && ('\\' != $precedingchar)) {
462  $open++;
463  }
464  if (('}' == $char) && ('\\' != $precedingchar)) {
465  $open--;
466  }
467  }
468  if (0 != $open) {
469  $ret = false;
470  }
471  //There is still the posibility that the entry is delimited by double quotes.
472  //Then it is possible that the braces are equal even if the '=' is in an equation.
473  if ($ret) {
474  $entrycopy = trim($entry);
475  $lastchar = $entrycopy{strlen($entrycopy)-1};
476  if (',' == $lastchar) {
477  $lastchar = $entrycopy{strlen($entrycopy)-2};
478  }
479  if ('"' == $lastchar) {
480  //The return value is set to false
481  //If we find the closing " before the '=' it is set to true again.
482  //Remember we begin to search the entry backwards so the " has to show up twice - ending and beginning delimiter
483  $ret = false;
484  $found = 0;
485  for ($i = $length; $i >= $position; $i--) {
486  $precedingchar = substr($entry, $i-1, 1);
487  $char = substr($entry, $i, 1);
488  if (('"' == $char) && ('\\' != $precedingchar)) {
489  $found++;
490  }
491  if (2 == $found) {
492  $ret = true;
493  break;
494  }
495  }
496  }
497  }
498  return $ret;
499  }
+ Here is the caller graph for this function:

◆ _determineCase()

Structures_BibTex::_determineCase (   $word)

Case Determination according to the needs of BibTex.

To parse the Author(s) correctly a determination is needed to get the Case of a word. There are three possible values:

  • Upper Case (return value 1)
  • Lower Case (return value 0)
  • Caseless (return value -1)

private

Parameters
string$word
Returns
int The Case or PEAR_Error if there was a problem

Definition at line 760 of file BibTex.php.

References $ret, and PEAR\raiseError().

Referenced by _extractAuthors().

760  {
761  $ret = -1;
762  $trimmedword = trim ($word);
763  /*We need this variable. Without the next of would not work
764  (trim changes the variable automatically to a string!)*/
765  if (is_string($word) && (strlen($trimmedword) > 0)) {
766  $i = 0;
767  $found = false;
768  $openbrace = 0;
769  while (!$found && ($i <= strlen($word))) {
770  $letter = substr($trimmedword, $i, 1);
771  $ord = ord($letter);
772  if ($ord == 123) { //Open brace
773  $openbrace++;
774  }
775  if ($ord == 125) { //Closing brace
776  $openbrace--;
777  }
778  if (($ord>=65) && ($ord<=90) && (0==$openbrace)) { //The first character is uppercase
779  $ret = 1;
780  $found = true;
781  } elseif ( ($ord>=97) && ($ord<=122) && (0==$openbrace) ) { //The first character is lowercase
782  $ret = 0;
783  $found = true;
784  } else { //Not yet found
785  $i++;
786  }
787  }
788  } else {
789  $ret = PEAR::raiseError('Could not determine case on word: '.(string)$word);
790  }
791  return $ret;
792  }
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _extractAuthors()

Structures_BibTex::_extractAuthors (   $entry)

Extracting the authors.

private

Parameters
string$entryThe entry with the authors
Returns
array the extracted authors

Definition at line 625 of file BibTex.php.

References $size, _determineCase(), _unwrap(), and PEAR\isError().

Referenced by _parseEntry().

625  {
626  $entry = $this->_unwrap($entry);
627  $authorarray = array();
628  $authorarray = explode(' and ', $entry);
629  for ($i = 0; $i < sizeof($authorarray); $i++) {
630  $author = trim($authorarray[$i]);
631  /*The first version of how an author could be written (First von Last)
632  has no commas in it*/
633  $first = '';
634  $von = '';
635  $last = '';
636  $jr = '';
637  if (strpos($author, ',') === false) {
638  $tmparray = array();
639  //$tmparray = explode(' ', $author);
640  $tmparray = explode(' |~', $author);
641  $size = sizeof($tmparray);
642  if (1 == $size) { //There is only a last
643  $last = $tmparray[0];
644  } elseif (2 == $size) { //There is a first and a last
645  $first = $tmparray[0];
646  $last = $tmparray[1];
647  } else {
648  $invon = false;
649  $inlast = false;
650  for ($j=0; $j<($size-1); $j++) {
651  if ($inlast) {
652  $last .= ' '.$tmparray[$j];
653  } elseif ($invon) {
654  $case = $this->_determineCase($tmparray[$j]);
655  if (PEAR::isError($case)) {
656  // IGNORE?
657  } elseif ((0 == $case) || (-1 == $case)) { //Change from von to last
658  //You only change when there is no more lower case there
659  $islast = true;
660  for ($k=($j+1); $k<($size-1); $k++) {
661  $futurecase = $this->_determineCase($tmparray[$k]);
662  if (PEAR::isError($case)) {
663  // IGNORE?
664  } elseif (0 == $futurecase) {
665  $islast = false;
666  }
667  }
668  if ($islast) {
669  $inlast = true;
670  if (-1 == $case) { //Caseless belongs to the last
671  $last .= ' '.$tmparray[$j];
672  } else {
673  $von .= ' '.$tmparray[$j];
674  }
675  } else {
676  $von .= ' '.$tmparray[$j];
677  }
678  } else {
679  $von .= ' '.$tmparray[$j];
680  }
681  } else {
682  $case = $this->_determineCase($tmparray[$j]);
683  if (PEAR::isError($case)) {
684  // IGNORE?
685  } elseif (0 == $case) { //Change from first to von
686  $invon = true;
687  $von .= ' '.$tmparray[$j];
688  } else {
689  $first .= ' '.$tmparray[$j];
690  }
691  }
692  }
693  //The last entry is always the last!
694  $last .= ' '.$tmparray[$size-1];
695  }
696  } else { //Version 2 and 3
697  $tmparray = array();
698  $tmparray = explode(',', $author);
699  //The first entry must contain von and last
700  $vonlastarray = array();
701  $vonlastarray = explode(' ', $tmparray[0]);
702  $size = sizeof($vonlastarray);
703  if (1==$size) { //Only one entry->got to be the last
704  $last = $vonlastarray[0];
705  } else {
706  $inlast = false;
707  for ($j=0; $j<($size-1); $j++) {
708  if ($inlast) {
709  $last .= ' '.$vonlastarray[$j];
710  } else {
711  if (0 != ($this->_determineCase($vonlastarray[$j]))) { //Change from von to last
712  $islast = true;
713  for ($k=($j+1); $k<($size-1); $k++) {
714  $this->_determineCase($vonlastarray[$k]);
715  $case = $this->_determineCase($vonlastarray[$k]);
716  if (PEAR::isError($case)) {
717  // IGNORE?
718  } elseif (0 == $case) {
719  $islast = false;
720  }
721  }
722  if ($islast) {
723  $inlast = true;
724  $last .= ' '.$vonlastarray[$j];
725  } else {
726  $von .= ' '.$vonlastarray[$j];
727  }
728  } else {
729  $von .= ' '.$vonlastarray[$j];
730  }
731  }
732  }
733  $last .= ' '.$vonlastarray[$size-1];
734  }
735  //Now we check if it is version three (three entries in the array (two commas)
736  if (3==sizeof($tmparray)) {
737  $jr = $tmparray[1];
738  }
739  //Everything in the last entry is first
740  $first = $tmparray[sizeof($tmparray)-1];
741  }
742  $authorarray[$i] = array('first'=>trim($first), 'von'=>trim($von), 'last'=>trim($last), 'jr'=>trim($jr));
743  }
744  return $authorarray;
745  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:597
_determineCase($word)
Case Determination according to the needs of BibTex.
Definition: BibTex.php:760
$size
Definition: RandomTest.php:79
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _formatAuthor()

Structures_BibTex::_formatAuthor (   $array)

Returns the author formatted.

The Author is formatted as setted in the authorstring

private

Parameters
array$arrayAuthor array
Returns
string the formatted author string

Definition at line 928 of file BibTex.php.

References $authorstring, and $ret.

Referenced by bibTex(), html(), and rtf().

929  {
930  if (!array_key_exists('von', $array)) {
931  $array['von'] = '';
932  } else {
933  $array['von'] = trim($array['von']);
934  }
935  if (!array_key_exists('last', $array)) {
936  $array['last'] = '';
937  } else {
938  $array['last'] = trim($array['last']);
939  }
940  if (!array_key_exists('jr', $array)) {
941  $array['jr'] = '';
942  } else {
943  $array['jr'] = trim($array['jr']);
944  }
945  if (!array_key_exists('first', $array)) {
946  $array['first'] = '';
947  } else {
948  $array['first'] = trim($array['first']);
949  }
951  $ret = str_replace("VON", $array['von'], $ret);
952  $ret = str_replace("LAST", $array['last'], $ret);
953  $ret = str_replace("JR", $array['jr'], $ret);
954  $ret = str_replace("FIRST", $array['first'], $ret);
955  return trim($ret);
956  }
+ Here is the caller graph for this function:

◆ _generateWarning()

Structures_BibTex::_generateWarning (   $type,
  $entry,
  $wholeentry = '' 
)

Generates a warning.

private

Parameters
string$typeThe type of the warning
string$entryThe line of the entry where the warning occurred
string$wholeentryOPTIONAL The whole entry where the warning occurred

Definition at line 878 of file BibTex.php.

Referenced by _parseEntry(), _validateValue(), html(), parse(), and rtf().

879  {
880  $warning['warning'] = $type;
881  $warning['entry'] = $entry;
882  $warning['wholeentry'] = $wholeentry;
883  $this->warnings[] = $warning;
884  }
+ Here is the caller graph for this function:

◆ _parseEntry()

Structures_BibTex::_parseEntry (   $entry)

Extracting the data of one content.

The parse function splits the content into its entries. Then every entry is parsed by this function. It parses the entry backwards. First the last '=' is searched and the value extracted from that. A copy is made of the entry if warnings should be generated. This takes quite some memory but it is needed to get good warnings. If nor warnings are generated then you don have to worry about memory. Then the last ',' is searched and the field extracted from that. Again the entry is shortened. Finally after all field=>value pairs the cite and type is extraced and the authors are splitted. If there is a problem false is returned.

private

Parameters
string$entryThe entry
Returns
array The representation of the entry or false if there is a problem

Definition at line 354 of file BibTex.php.

References $ret, _checkAllowedEntryType(), _checkEqualSign(), _extractAuthors(), _generateWarning(), _removeCurlyBraces(), _stripDelimiter(), _unwrap(), and _validateValue().

Referenced by parse().

355  {
356  $entrycopy = '';
357  if ($this->_options['validate']) {
358  $entrycopy = $entry; //We need a copy for printing the warnings
359  }
360  $ret = array();
361  if ('@string' == strtolower(substr($entry, 0, 7))) {
362  //String are not yet supported!
363  if ($this->_options['validate']) {
364  $this->_generateWarning('STRING_ENTRY_NOT_YET_SUPPORTED', '', $entry.'}');
365  }
366  } elseif ('@preamble' == strtolower(substr($entry, 0, 9))) {
367  //Preamble not yet supported!
368  if ($this->_options['validate']) {
369  $this->_generateWarning('PREAMBLE_ENTRY_NOT_YET_SUPPORTED', '', $entry.'}');
370  }
371  } else {
372  //Parsing all fields
373  while (strrpos($entry,'=') !== false) {
374  $position = strrpos($entry, '=');
375  //Checking that the equal sign is not quoted or is not inside a equation (For example in an abstract)
376  $proceed = true;
377  if (substr($entry, $position-1, 1) == '\\') {
378  $proceed = false;
379  }
380  if ($proceed) {
381  $proceed = $this->_checkEqualSign($entry, $position);
382  }
383  while (!$proceed) {
384  $substring = substr($entry, 0, $position);
385  $position = strrpos($substring,'=');
386  $proceed = true;
387  if (substr($entry, $position-1, 1) == '\\') {
388  $proceed = false;
389  }
390  if ($proceed) {
391  $proceed = $this->_checkEqualSign($entry, $position);
392  }
393  }
394 
395  $value = trim(substr($entry, $position+1));
396  $entry = substr($entry, 0, $position);
397 
398  if (',' == substr($value, strlen($value)-1, 1)) {
399  $value = substr($value, 0, -1);
400  }
401  if ($this->_options['validate']) {
402  $this->_validateValue($value, $entrycopy);
403  }
404  if ($this->_options['stripDelimiter']) {
405  $value = $this->_stripDelimiter($value);
406  }
407  if ($this->_options['unwrap']) {
408  $value = $this->_unwrap($value);
409  }
410  if ($this->_options['removeCurlyBraces']) {
411  $value = $this->_removeCurlyBraces($value);
412  }
413  $position = strrpos($entry, ',');
414  $field = strtolower(trim(substr($entry, $position+1)));
415  $ret[$field] = $value;
416  $entry = substr($entry, 0, $position);
417  }
418  //Parsing cite and entry type
419  $arr = explode('{', $entry);
420  $ret['cite'] = trim($arr[1]);
421  $ret['entryType'] = strtolower(trim($arr[0]));
422  if ('@' == $ret['entryType']{0}) {
423  $ret['entryType'] = substr($ret['entryType'], 1);
424  }
425  if ($this->_options['validate']) {
426  if (!$this->_checkAllowedEntryType($ret['entryType'])) {
427  $this->_generateWarning('WARNING_NOT_ALLOWED_ENTRY_TYPE', $ret['entryType'], $entry.'}');
428  }
429  }
430  //Handling the authors
431  if (in_array('author', array_keys($ret)) && $this->_options['extractAuthors']) {
432  $ret['author'] = $this->_extractAuthors($ret['author']);
433  }
434  }
435  return $ret;
436  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:597
_stripDelimiter($entry)
Stripping Delimiter.
Definition: BibTex.php:572
_validateValue($entry, $wholeentry)
Validation of a value.
Definition: BibTex.php:806
_checkEqualSign($entry, $position)
Checking whether the position of the &#39;=&#39; is correct.
Definition: BibTex.php:450
_extractAuthors($entry)
Extracting the authors.
Definition: BibTex.php:625
_removeCurlyBraces($value)
Remove curly braces from entry.
Definition: BibTex.php:842
_checkAllowedEntryType($entry)
Checking if the entry type is allowed.
Definition: BibTex.php:508
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:878
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _removeCurlyBraces()

Structures_BibTex::_removeCurlyBraces (   $value)

Remove curly braces from entry.

private

Parameters
string$valueThe value in which curly braces to be removed
stringValue with removed curly braces

Definition at line 842 of file BibTex.php.

Referenced by _parseEntry().

843  {
844  //First we save the delimiters
845  $beginningdels = array_keys($this->_delimiters);
846  $firstchar = substr($entry, 0, 1);
847  $lastchar = substr($entry, -1, 1);
848  $begin = '';
849  $end = '';
850  while (in_array($firstchar, $beginningdels)) { //The first character is an opening delimiter
851  if ($lastchar == $this->_delimiters[$firstchar]) { //Matches to closing Delimiter
852  $begin .= $firstchar;
853  $end .= $lastchar;
854  $value = substr($value, 1, -1);
855  } else {
856  break;
857  }
858  $firstchar = substr($value, 0, 1);
859  $lastchar = substr($value, -1, 1);
860  }
861  //Now we get rid of the curly braces
862  $pattern = '/([^\\\\])\{(.*?[^\\\\])\}/';
863  $replacement = '$1$2';
864  $value = preg_replace($pattern, $replacement, $value);
865  //Reattach delimiters
866  $value = $begin.$value.$end;
867  return $value;
868  }
+ Here is the caller graph for this function:

◆ _stripDelimiter()

Structures_BibTex::_stripDelimiter (   $entry)

Stripping Delimiter.

private

Parameters
string$entryThe entry where the Delimiter should be stripped from
Returns
string Stripped entry

Definition at line 572 of file BibTex.php.

Referenced by _parseEntry().

573  {
574  $beginningdels = array_keys($this->_delimiters);
575  $length = strlen($entry);
576  $firstchar = substr($entry, 0, 1);
577  $lastchar = substr($entry, -1, 1);
578  while (in_array($firstchar, $beginningdels)) { //The first character is an opening delimiter
579  if ($lastchar == $this->_delimiters[$firstchar]) { //Matches to closing Delimiter
580  $entry = substr($entry, 1, -1);
581  } else {
582  break;
583  }
584  $firstchar = substr($entry, 0, 1);
585  $lastchar = substr($entry, -1, 1);
586  }
587  return $entry;
588  }
+ Here is the caller graph for this function:

◆ _unwrap()

Structures_BibTex::_unwrap (   $entry)

Unwrapping entry.

private

Parameters
string$entryThe entry to unwrap
Returns
string unwrapped entry

Definition at line 597 of file BibTex.php.

Referenced by _extractAuthors(), _parseEntry(), html(), and rtf().

598  {
599  $entry = preg_replace('/\s+/', ' ', $entry);
600  return trim($entry);
601  }
+ Here is the caller graph for this function:

◆ _validateValue()

Structures_BibTex::_validateValue (   $entry,
  $wholeentry 
)

Validation of a value.

There may be several problems with the value of a field. These problems exist but do not break the parsing. If a problem is detected a warning is appended to the array warnings.

private

Parameters
string$entryThe entry aka one line which which should be validated
string$wholeentryThe whole BibTex Entry which the one line is part of
Returns
void

Definition at line 806 of file BibTex.php.

References _generateWarning().

Referenced by _parseEntry().

807  {
808  //There is no @ allowed if the entry is enclosed by braces
809  if (preg_match('/^{.*@.*}$/', $entry)) {
810  $this->_generateWarning('WARNING_AT_IN_BRACES', $entry, $wholeentry);
811  }
812  //No escaped " allowed if the entry is enclosed by double quotes
813  if (preg_match('/^\".*\\".*\"$/', $entry)) {
814  $this->_generateWarning('WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES', $entry, $wholeentry);
815  }
816  //Amount of Braces is not correct
817  $open = 0;
818  $lastchar = '';
819  $char = '';
820  for ($i = 0; $i < strlen($entry); $i++) {
821  $char = substr($entry, $i, 1);
822  if (('{' == $char) && ('\\' != $lastchar)) {
823  $open++;
824  }
825  if (('}' == $char) && ('\\' != $lastchar)) {
826  $open--;
827  }
828  $lastchar = $char;
829  }
830  if (0 != $open) {
831  $this->_generateWarning('WARNING_UNBALANCED_AMOUNT_OF_BRACES', $entry, $wholeentry);
832  }
833  }
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:878
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _wordwrap()

Structures_BibTex::_wordwrap (   $entry)

Wordwrap an entry.

private

Parameters
string$entryThe entry to wrap
Returns
string wrapped entry

Definition at line 610 of file BibTex.php.

611  {
612  if ( (''!=$entry) && (is_string($entry)) ) {
613  $entry = wordwrap($entry, $this->_options['wordWrapWidth'], $this->_options['wordWrapBreak'], $this->_options['wordWrapCut']);
614  }
615  return $entry;
616  }

◆ addEntry()

Structures_BibTex::addEntry (   $newentry)

Adds a new BibTex entry to the data.

public

Parameters
array$newentryThe new data to add
Returns
void

Definition at line 1008 of file BibTex.php.

1009  {
1010  $this->data[] = $newentry;
1011  }

◆ amount()

Structures_BibTex::amount ( )

Returns the amount of available BibTex entries.

public

Returns
int The amount of available BibTex entries

Definition at line 914 of file BibTex.php.

References $data.

915  {
916  return sizeof($this->data);
917  }

◆ bibTex()

Structures_BibTex::bibTex ( )

Converts the stored BibTex entries to a BibTex String.

In the field list, the author is the last field.

public

Returns
string The BibTex string

Definition at line 966 of file BibTex.php.

References $bibtex, and _formatAuthor().

967  {
968  $bibtex = '';
969  foreach ($this->data as $entry) {
970  //Intro
971  $bibtex .= '@'.strtolower($entry['entryType']).' { '.$entry['cite'].",\n";
972  //Other fields except author
973  foreach ($entry as $key=>$val) {
974  if ($this->_options['wordWrapWidth']>0) {
975  $val = $this->_wordWrap($val);
976  }
977  if (!in_array($key, array('cite','entryType','author'))) {
978  $bibtex .= "\t".$key.' = {'.$val."},\n";
979  }
980  }
981  //Author
982  if (array_key_exists('author', $entry)) {
983  if ($this->_options['extractAuthors']) {
984  $tmparray = array(); //In this array the authors are saved and the joind with an and
985  foreach ($entry['author'] as $authorentry) {
986  $tmparray[] = $this->_formatAuthor($authorentry);
987  }
988  $author = join(' and ', $tmparray);
989  } else {
990  $author = $entry['author'];
991  }
992  } else {
993  $author = '';
994  }
995  $bibtex .= "\tauthor = {".$author."}\n";
996  $bibtex.="}\n\n";
997  }
998  return $bibtex;
999  }
_formatAuthor($array)
Returns the author formatted.
Definition: BibTex.php:928
+ Here is the call graph for this function:

◆ clearWarnings()

Structures_BibTex::clearWarnings ( )

Cleares all warnings.

public

Definition at line 891 of file BibTex.php.

892  {
893  $this->warnings = array();
894  }

◆ getStatistic()

Structures_BibTex::getStatistic ( )

Returns statistic.

This functions returns a hash table. The keys are the different entry types and the values are the amount of these entries.

public

Returns
array Hash Table with the data

Definition at line 1022 of file BibTex.php.

References $ret.

1023  {
1024  $ret = array();
1025  foreach ($this->data as $entry) {
1026  if (array_key_exists($entry['entryType'], $ret)) {
1027  $ret[$entry['entryType']]++;
1028  } else {
1029  $ret[$entry['entryType']] = 1;
1030  }
1031  }
1032  return $ret;
1033  }

◆ hasWarning()

Structures_BibTex::hasWarning ( )

Is there a warning?

public

Returns
true if there is, false otherwise

Definition at line 902 of file BibTex.php.

903  {
904  if (sizeof($this->warnings)>0) return true;
905  else return false;
906  }

◆ html()

Structures_BibTex::html ( )

Returns the stored data in HTML format.

This method simply returns a HTML formatted string. This is done very simple and is not intended for heavy using and fine formatting. This should be done by BibTex! It is intended to give some kind of quick preview. If you want to change the default format you have to override the class variable "htmlstring". This variable is used and the placeholders simply replaced. Lines with no data cause an warning!

Returns
string the HTML Strings

Definition at line 1106 of file BibTex.php.

References $htmlstring, $ret, _formatAuthor(), _generateWarning(), and _unwrap().

1107  {
1108  $ret = "<p>\n";
1109  foreach ($this->data as $entry) {
1110  $line = $this->htmlstring;
1111  $title = '';
1112  $journal = '';
1113  $year = '';
1114  $authors = '';
1115  if (array_key_exists('title', $entry)) {
1116  $title = $this->_unwrap($entry['title']);
1117  }
1118  if (array_key_exists('journal', $entry)) {
1119  $journal = $this->_unwrap($entry['journal']);
1120  }
1121  if (array_key_exists('year', $entry)) {
1122  $year = $this->_unwrap($entry['year']);
1123  }
1124  if (array_key_exists('author', $entry)) {
1125  if ($this->_options['extractAuthors']) {
1126  $tmparray = array(); //In this array the authors are saved and the joind with an and
1127  foreach ($entry['author'] as $authorentry) {
1128  $tmparray[] = $this->_formatAuthor($authorentry);
1129  }
1130  $authors = join(', ', $tmparray);
1131  } else {
1132  $authors = $entry['author'];
1133  }
1134  }
1135  if ((''!=$title) || (''!=$journal) || (''!=$year) || (''!=$authors)) {
1136  $line = str_replace("TITLE", $title, $line);
1137  $line = str_replace("JOURNAL", $journal, $line);
1138  $line = str_replace("YEAR", $year, $line);
1139  $line = str_replace("AUTHORS", $authors, $line);
1140  $line .= "\n";
1141  $ret .= $line;
1142  } else {
1143  $this->_generateWarning('WARNING_LINE_WAS_NOT_CONVERTED', '', print_r($entry,1));
1144  }
1145  }
1146  $ret .= "</p>\n";
1147  return $ret;
1148  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:597
_formatAuthor($array)
Returns the author formatted.
Definition: BibTex.php:928
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:878
+ Here is the call graph for this function:

◆ loadFile()

Structures_BibTex::loadFile (   $filename)

Reads a give BibTex File.

public

Parameters
string$filenameName of the file
Returns
mixed true on success PEAR_Error on failure

Definition at line 221 of file BibTex.php.

References $filename, and PEAR\raiseError().

222  {
223  if (file_exists($filename)) {
224  if (($this->content = @file_get_contents($filename)) === false) {
225  return PEAR::raiseError('Could not open file '.$filename);
226  } else {
227  $this->_pos = 0;
228  $this->_oldpos = 0;
229  return true;
230  }
231  } else {
232  return PEAR::raiseError('Could not find file '.$filename);
233  }
234  }
$filename
Definition: buildRTE.php:89
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:

◆ parse()

Structures_BibTex::parse ( )

Parses what is stored in content and clears the content if the parsing is successfull.

public

Returns
boolean true on success and PEAR_Error if there was a problem

This is not yet used. We are here if the Entry is either not correct or not supported. But this should already generate a warning. Therefore it should not be necessary to do anything here

Definition at line 242 of file BibTex.php.

References $valid, _checkAt(), _generateWarning(), _parseEntry(), and PEAR\raiseError().

243  {
244  //The amount of opening braces is compared to the amount of closing braces
245  //Braces inside comments are ignored
246  $this->warnings = array();
247  $this->data = array();
248  $valid = true;
249  $open = 0;
250  $entry = false;
251  $char = '';
252  $lastchar = '';
253  $buffer = '';
254  for ($i = 0; $i < strlen($this->content); $i++) {
255  $char = substr($this->content, $i, 1);
256  if ((0 != $open) && ('@' == $char)) {
257  if (!$this->_checkAt($buffer)) {
258  $this->_generateWarning('WARNING_MISSING_END_BRACE', '', $buffer);
259  //To correct the data we need to insert a closing brace
260  $char = '}';
261  $i--;
262  }
263  }
264  if ((0 == $open) && ('@' == $char)) { //The beginning of an entry
265  $entry = true;
266  } elseif ($entry && ('{' == $char) && ('\\' != $lastchar)) { //Inside an entry and non quoted brace is opening
267  $open++;
268  } elseif ($entry && ('}' == $char) && ('\\' != $lastchar)) { //Inside an entry and non quoted brace is closing
269  $open--;
270  if ($open < 0) { //More are closed than opened
271  $valid = false;
272  }
273  if (0 == $open) { //End of entry
274  $entry = false;
275  $entrydata = $this->_parseEntry($buffer);
276  if (!$entrydata) {
283  } else {
284  $this->data[] = $entrydata;
285  }
286  $buffer = '';
287  }
288  }
289  if ($entry) { //Inside entry
290  $buffer .= $char;
291  }
292  $lastchar = $char;
293  }
294  //If open is one it may be possible that the last ending brace is missing
295  if (1 == $open) {
296  $entrydata = $this->_parseEntry($buffer);
297  if (!$entrydata) {
298  $valid = false;
299  } else {
300  $this->data[] = $entrydata;
301  $buffer = '';
302  $open = 0;
303  }
304  }
305  //At this point the open should be zero
306  if (0 != $open) {
307  $valid = false;
308  }
309  //Are there Multiple entries with the same cite?
310  if ($this->_options['validate']) {
311  $cites = array();
312  foreach ($this->data as $entry) {
313  $cites[] = $entry['cite'];
314  }
315  $unique = array_unique($cites);
316  if (sizeof($cites) != sizeof($unique)) { //Some values have not been unique!
317  $notuniques = array();
318  for ($i = 0; $i < sizeof($cites); $i++) {
319  if ('' == $unique[$i]) {
320  $notuniques[] = $cites[$i];
321  }
322  }
323  $this->_generateWarning('WARNING_MULTIPLE_ENTRIES', implode(',',$notuniques));
324  }
325  }
326  if ($valid) {
327  $this->content = '';
328  return true;
329  } else {
330  return PEAR::raiseError('Unbalanced parenthesis');
331  }
332  }
_parseEntry($entry)
Extracting the data of one content.
Definition: BibTex.php:354
$valid
_checkAt($entry)
Checking whether an at is outside an entry.
Definition: BibTex.php:524
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:878
+ Here is the call graph for this function:

◆ rtf()

Structures_BibTex::rtf ( )

Returns the stored data in RTF format.

This method simply returns a RTF formatted string. This is done very simple and is not intended for heavy using and fine formatting. This should be done by BibTex! It is intended to give some kind of quick preview or to send someone a reference list as word/rtf format (even some people in the scientific field still use word). If you want to change the default format you have to override the class variable "rtfstring". This variable is used and the placeholders simply replaced. Lines with no data cause an warning!

Returns
string the RTF Strings

Definition at line 1049 of file BibTex.php.

References $ret, $rtfstring, _formatAuthor(), _generateWarning(), and _unwrap().

1050  {
1051  $ret = "{\\rtf\n";
1052  foreach ($this->data as $entry) {
1053  $line = $this->rtfstring;
1054  $title = '';
1055  $journal = '';
1056  $year = '';
1057  $authors = '';
1058  if (array_key_exists('title', $entry)) {
1059  $title = $this->_unwrap($entry['title']);
1060  }
1061  if (array_key_exists('journal', $entry)) {
1062  $journal = $this->_unwrap($entry['journal']);
1063  }
1064  if (array_key_exists('year', $entry)) {
1065  $year = $this->_unwrap($entry['year']);
1066  }
1067  if (array_key_exists('author', $entry)) {
1068  if ($this->_options['extractAuthors']) {
1069  $tmparray = array(); //In this array the authors are saved and the joind with an and
1070  foreach ($entry['author'] as $authorentry) {
1071  $tmparray[] = $this->_formatAuthor($authorentry);
1072  }
1073  $authors = join(', ', $tmparray);
1074  } else {
1075  $authors = $entry['author'];
1076  }
1077  }
1078  if ((''!=$title) || (''!=$journal) || (''!=$year) || (''!=$authors)) {
1079  $line = str_replace("TITLE", $title, $line);
1080  $line = str_replace("JOURNAL", $journal, $line);
1081  $line = str_replace("YEAR", $year, $line);
1082  $line = str_replace("AUTHORS", $authors, $line);
1083  $line .= "\n\\par\n";
1084  $ret .= $line;
1085  } else {
1086  $this->_generateWarning('WARNING_LINE_WAS_NOT_CONVERTED', '', print_r($entry,1));
1087  }
1088  }
1089  $ret .= '}';
1090  return $ret;
1091  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:597
_formatAuthor($array)
Returns the author formatted.
Definition: BibTex.php:928
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:878
+ Here is the call graph for this function:

◆ setOption()

Structures_BibTex::setOption (   $option,
  $value 
)

Sets run-time configuration options.

public

Parameters
string$optionoption name
mixed$valuevalue for the option
Returns
mixed true on success PEAR_Error on failure

Definition at line 203 of file BibTex.php.

References $ret, and PEAR\raiseError().

Referenced by Structures_BibTex().

204  {
205  $ret = true;
206  if (array_key_exists($option, $this->_options)) {
207  $this->_options[$option] = $value;
208  } else {
209  $ret = PEAR::raiseError('Unknown option '.$option);
210  }
211  return $ret;
212  }
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object&#39;s de...
Definition: PEAR.php:524
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Structures_BibTex()

Structures_BibTex::Structures_BibTex (   $options = array())

Constructor.

public

Returns
void

Definition at line 149 of file BibTex.php.

References $options, $test, PEAR\isError(), and setOption().

150  {
151  $this->_delimiters = array('"'=>'"',
152  '{'=>'}');
153  $this->data = array();
154  $this->content = '';
155  //$this->_stripDelimiter = $stripDel;
156  //$this->_validate = $val;
157  $this->warnings = array();
158  $this->_options = array(
159  'stripDelimiter' => true,
160  'validate' => true,
161  'unwrap' => false,
162  'wordWrapWidth' => false,
163  'wordWrapBreak' => "\n",
164  'wordWrapCut' => 0,
165  'removeCurlyBraces' => false,
166  'extractAuthors' => true,
167  );
168  foreach ($options as $option => $value) {
169  $test = $this->setOption($option, $value);
170  if (PEAR::isError($test)) {
171  //Currently nothing is done here, but it could for example raise an warning
172  }
173  }
174  $this->rtfstring = 'AUTHORS, "{\b TITLE}", {\i JOURNAL}, YEAR';
175  $this->htmlstring = 'AUTHORS, "<strong>TITLE</strong>", <em>JOURNAL</em>, YEAR<br />';
176  $this->allowedEntryTypes = array(
177  'article',
178  'book',
179  'booklet',
180  'confernce',
181  'inbook',
182  'incollection',
183  'inproceedings',
184  'manual',
185  'mastersthesis',
186  'misc',
187  'phdthesis',
188  'proceedings',
189  'techreport',
190  'unpublished'
191  );
192  $this->authorstring = 'VON LAST, JR, FIRST';
193  }
setOption($option, $value)
Sets run-time configuration options.
Definition: BibTex.php:203
if(!is_array($argv)) $options
$test
Definition: Utf8Test.php:85
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:279
+ Here is the call graph for this function:

Field Documentation

◆ $_delimiters

Structures_BibTex::$_delimiters

Definition at line 99 of file BibTex.php.

◆ $_options

Structures_BibTex::$_options

Definition at line 113 of file BibTex.php.

◆ $allowedEntryTypes

Structures_BibTex::$allowedEntryTypes

Definition at line 134 of file BibTex.php.

◆ $authorstring

Structures_BibTex::$authorstring

Definition at line 141 of file BibTex.php.

Referenced by _formatAuthor().

◆ $content

Structures_BibTex::$content

Definition at line 92 of file BibTex.php.

◆ $data

Structures_BibTex::$data

Definition at line 85 of file BibTex.php.

Referenced by amount().

◆ $htmlstring

Structures_BibTex::$htmlstring

Definition at line 127 of file BibTex.php.

Referenced by html().

◆ $rtfstring

Structures_BibTex::$rtfstring

Definition at line 120 of file BibTex.php.

Referenced by rtf().

◆ $warnings

Structures_BibTex::$warnings

Definition at line 106 of file BibTex.php.


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