ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f87
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 528 of file BibTex.php.

Referenced by _parseEntry().

528  {
529  return in_array($entry, $this->allowedEntryTypes);
530  }
+ 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 546 of file BibTex.php.

References $ret.

Referenced by parse().

546  {
547  $ret = false;
548  $opening = array_keys($this->_delimiters);
549  $closing = array_values($this->_delimiters);
550  //Getting the value (at is only allowd in values)
551  if (strrpos($entry, '=') !== false) {
552  $position = strrpos($entry, '=');
553  $proceed = true;
554  if (substr($entry, $position - 1, 1) == '\\') {
555  $proceed = false;
556  }
557  while (! $proceed) {
558  $substring = substr($entry, 0, $position);
559  $position = strrpos($substring, '=');
560  $proceed = true;
561  if (substr($entry, $position - 1, 1) == '\\') {
562  $proceed = false;
563  }
564  }
565  $value = trim(substr($entry, $position + 1));
566  $open = 0;
567  $char = '';
568  $lastchar = '';
569  for ($i = 0; $i < strlen($value); $i ++) {
570  $char = substr($this->content, $i, 1);
571  if (in_array($char, $opening) && ('\\' != $lastchar)) {
572  $open ++;
573  } elseif (in_array($char, $closing) && ('\\' != $lastchar)) {
574  $open --;
575  }
576  $lastchar = $char;
577  }
578  //if open is grater zero were are inside an entry
579  if ($open > 0) {
580  $ret = true;
581  }
582  }
583 
584  return $ret;
585  }
+ 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 467 of file BibTex.php.

References $ret.

Referenced by _parseEntry().

467  {
468  $ret = true;
469  //This is getting tricky
470  //We check the string backwards until the position and count the closing an opening braces
471  //If we reach the position the amount of opening and closing braces should be equal
472  $length = strlen($entry);
473  $open = 0;
474  for ($i = $length - 1; $i >= $position; $i --) {
475  $precedingchar = substr($entry, $i - 1, 1);
476  $char = substr($entry, $i, 1);
477  if (('{' == $char) && ('\\' != $precedingchar)) {
478  $open ++;
479  }
480  if (('}' == $char) && ('\\' != $precedingchar)) {
481  $open --;
482  }
483  }
484  if (0 != $open) {
485  $ret = false;
486  }
487  //There is still the posibility that the entry is delimited by double quotes.
488  //Then it is possible that the braces are equal even if the '=' is in an equation.
489  if ($ret) {
490  $entrycopy = trim($entry);
491  $lastchar = $entrycopy{strlen($entrycopy) - 1};
492  if (',' == $lastchar) {
493  $lastchar = $entrycopy{strlen($entrycopy) - 2};
494  }
495  if ('"' == $lastchar) {
496  //The return value is set to false
497  //If we find the closing " before the '=' it is set to true again.
498  //Remember we begin to search the entry backwards so the " has to show up twice - ending and beginning delimiter
499  $ret = false;
500  $found = 0;
501  for ($i = $length; $i >= $position; $i --) {
502  $precedingchar = substr($entry, $i - 1, 1);
503  $char = substr($entry, $i, 1);
504  if (('"' == $char) && ('\\' != $precedingchar)) {
505  $found ++;
506  }
507  if (2 == $found) {
508  $ret = true;
509  break;
510  }
511  }
512  }
513  }
514 
515  return $ret;
516  }
+ 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 803 of file BibTex.php.

References $ret, and PEAR\raiseError().

Referenced by _extractAuthors().

803  {
804  $ret = - 1;
805  $trimmedword = trim($word);
806  /*We need this variable. Without the next of would not work
807  (trim changes the variable automatically to a string!)*/
808  if (is_string($word) && (strlen($trimmedword) > 0)) {
809  $i = 0;
810  $found = false;
811  $openbrace = 0;
812  while (! $found && ($i <= strlen($word))) {
813  $letter = substr($trimmedword, $i, 1);
814  $ord = ord($letter);
815  if ($ord == 123) { //Open brace
816  $openbrace ++;
817  }
818  if ($ord == 125) { //Closing brace
819  $openbrace --;
820  }
821  if (($ord >= 65) && ($ord <= 90) && (0 == $openbrace)) { //The first character is uppercase
822  $ret = 1;
823  $found = true;
824  } elseif (($ord >= 97) && ($ord <= 122) && (0 == $openbrace)) { //The first character is lowercase
825  $ret = 0;
826  $found = true;
827  } else { //Not yet found
828  $i ++;
829  }
830  }
831  } else {
832  $ret = PEAR::raiseError('Could not determine case on word: ' . (string)$word);
833  }
834 
835  return $ret;
836  }
& 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 659 of file BibTex.php.

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

Referenced by _parseEntry().

659  {
660  $entry = $this->_unwrap($entry);
661  $authorarray = array();
662  $authorarray = explode(' and ', $entry);
663  for ($i = 0; $i < sizeof($authorarray); $i ++) {
664  $author = trim($authorarray[$i]);
665  /*The first version of how an author could be written (First von Last)
666  has no commas in it*/
667  $first = '';
668  $von = '';
669  $last = '';
670  $jr = '';
671  if (strpos($author, ',') === false) {
672  $tmparray = array();
673  //$tmparray = explode(' ', $author);
674  $tmparray = explode(' |~', $author);
675  $size = sizeof($tmparray);
676  if (1 == $size) { //There is only a last
677  $last = $tmparray[0];
678  } elseif (2 == $size) { //There is a first and a last
679  $first = $tmparray[0];
680  $last = $tmparray[1];
681  } else {
682  $invon = false;
683  $inlast = false;
684  for ($j = 0; $j < ($size - 1); $j ++) {
685  if ($inlast) {
686  $last .= ' ' . $tmparray[$j];
687  } elseif ($invon) {
688  $case = $this->_determineCase($tmparray[$j]);
689  if (PEAR::isError($case)) {
690  // IGNORE?
691  } elseif ((0 == $case) || (- 1 == $case)) { //Change from von to last
692  //You only change when there is no more lower case there
693  $islast = true;
694  for ($k = ($j + 1); $k < ($size - 1); $k ++) {
695  $futurecase = $this->_determineCase($tmparray[$k]);
696  if (PEAR::isError($case)) {
697  // IGNORE?
698  } elseif (0 == $futurecase) {
699  $islast = false;
700  }
701  }
702  if ($islast) {
703  $inlast = true;
704  if (- 1 == $case) { //Caseless belongs to the last
705  $last .= ' ' . $tmparray[$j];
706  } else {
707  $von .= ' ' . $tmparray[$j];
708  }
709  } else {
710  $von .= ' ' . $tmparray[$j];
711  }
712  } else {
713  $von .= ' ' . $tmparray[$j];
714  }
715  } else {
716  $case = $this->_determineCase($tmparray[$j]);
717  if (PEAR::isError($case)) {
718  // IGNORE?
719  } elseif (0 == $case) { //Change from first to von
720  $invon = true;
721  $von .= ' ' . $tmparray[$j];
722  } else {
723  $first .= ' ' . $tmparray[$j];
724  }
725  }
726  }
727  //The last entry is always the last!
728  $last .= ' ' . $tmparray[$size - 1];
729  }
730  } else { //Version 2 and 3
731  $tmparray = array();
732  $tmparray = explode(',', $author);
733  //The first entry must contain von and last
734  $vonlastarray = array();
735  $vonlastarray = explode(' ', $tmparray[0]);
736  $size = sizeof($vonlastarray);
737  if (1 == $size) { //Only one entry->got to be the last
738  $last = $vonlastarray[0];
739  } else {
740  $inlast = false;
741  for ($j = 0; $j < ($size - 1); $j ++) {
742  if ($inlast) {
743  $last .= ' ' . $vonlastarray[$j];
744  } else {
745  if (0 != ($this->_determineCase($vonlastarray[$j]))) { //Change from von to last
746  $islast = true;
747  for ($k = ($j + 1); $k < ($size - 1); $k ++) {
748  $this->_determineCase($vonlastarray[$k]);
749  $case = $this->_determineCase($vonlastarray[$k]);
750  if (PEAR::isError($case)) {
751  // IGNORE?
752  } elseif (0 == $case) {
753  $islast = false;
754  }
755  }
756  if ($islast) {
757  $inlast = true;
758  $last .= ' ' . $vonlastarray[$j];
759  } else {
760  $von .= ' ' . $vonlastarray[$j];
761  }
762  } else {
763  $von .= ' ' . $vonlastarray[$j];
764  }
765  }
766  }
767  $last .= ' ' . $vonlastarray[$size - 1];
768  }
769  //Now we check if it is version three (three entries in the array (two commas)
770  if (3 == sizeof($tmparray)) {
771  $jr = $tmparray[1];
772  }
773  //Everything in the last entry is first
774  $first = $tmparray[sizeof($tmparray) - 1];
775  }
776  $authorarray[$i] = array(
777  'first' => trim($first),
778  'von' => trim($von),
779  'last' => trim($last),
780  'jr' => trim($jr)
781  );
782  }
783 
784  return $authorarray;
785  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:625
_determineCase($word)
Case Determination according to the needs of BibTex.
Definition: BibTex.php:803
$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 983 of file BibTex.php.

References $authorstring, and $ret.

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

983  {
984  if (! array_key_exists('von', $array)) {
985  $array['von'] = '';
986  } else {
987  $array['von'] = trim($array['von']);
988  }
989  if (! array_key_exists('last', $array)) {
990  $array['last'] = '';
991  } else {
992  $array['last'] = trim($array['last']);
993  }
994  if (! array_key_exists('jr', $array)) {
995  $array['jr'] = '';
996  } else {
997  $array['jr'] = trim($array['jr']);
998  }
999  if (! array_key_exists('first', $array)) {
1000  $array['first'] = '';
1001  } else {
1002  $array['first'] = trim($array['first']);
1003  }
1005  $ret = str_replace("VON", $array['von'], $ret);
1006  $ret = str_replace("LAST", $array['last'], $ret);
1007  $ret = str_replace("JR", $array['jr'], $ret);
1008  $ret = str_replace("FIRST", $array['first'], $ret);
1009 
1010  return trim($ret);
1011  }
+ 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 928 of file BibTex.php.

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

928  {
929  $warning['warning'] = $type;
930  $warning['entry'] = $entry;
931  $warning['wholeentry'] = $wholeentry;
932  $this->warnings[] = $warning;
933  }
+ 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 370 of file BibTex.php.

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

Referenced by parse().

370  {
371  $entrycopy = '';
372  if ($this->_options['validate']) {
373  $entrycopy = $entry; //We need a copy for printing the warnings
374  }
375  $ret = array();
376  if ('@string' == strtolower(substr($entry, 0, 7))) {
377  //String are not yet supported!
378  if ($this->_options['validate']) {
379  $this->_generateWarning('STRING_ENTRY_NOT_YET_SUPPORTED', '', $entry . '}');
380  }
381  } elseif ('@preamble' == strtolower(substr($entry, 0, 9))) {
382  //Preamble not yet supported!
383  if ($this->_options['validate']) {
384  $this->_generateWarning('PREAMBLE_ENTRY_NOT_YET_SUPPORTED', '', $entry . '}');
385  }
386  } else {
387  //Parsing all fields
388  while (strrpos($entry, '=') !== false) {
389  $position = strrpos($entry, '=');
390  //Checking that the equal sign is not quoted or is not inside a equation (For example in an abstract)
391  $proceed = true;
392  if (substr($entry, $position - 1, 1) == '\\') {
393  $proceed = false;
394  }
395  if ($proceed) {
396  $proceed = $this->_checkEqualSign($entry, $position);
397  }
398  while (! $proceed) {
399  $substring = substr($entry, 0, $position);
400  $position = strrpos($substring, '=');
401  $proceed = true;
402  if (substr($entry, $position - 1, 1) == '\\') {
403  $proceed = false;
404  }
405  if ($proceed) {
406  $proceed = $this->_checkEqualSign($entry, $position);
407  }
408  }
409  $value = trim(substr($entry, $position + 1));
410  $entry = substr($entry, 0, $position);
411  if (',' == substr($value, strlen($value) - 1, 1)) {
412  $value = substr($value, 0, - 1);
413  }
414  if ($this->_options['validate']) {
415  $this->_validateValue($value, $entrycopy);
416  }
417  if ($this->_options['stripDelimiter']) {
418  $value = $this->_stripDelimiter($value);
419  }
420  if ($this->_options['unwrap']) {
421  $value = $this->_unwrap($value);
422  }
423  if ($this->_options['removeCurlyBraces']) {
424  $value = $this->_removeCurlyBraces($value);
425  }
426  $position = strrpos($entry, ',');
427  $field = strtolower(trim(substr($entry, $position + 1)));
428  $ret[$field] = $value;
429  $entry = substr($entry, 0, $position);
430  }
431  //Parsing cite and entry type
432  $arr = explode('{', $entry);
433  $ret['cite'] = trim($arr[1]);
434  $ret['entryType'] = strtolower(trim($arr[0]));
435  if ('@' == $ret['entryType']{0}) {
436  $ret['entryType'] = substr($ret['entryType'], 1);
437  }
438  if ($this->_options['validate']) {
439  if (! $this->_checkAllowedEntryType($ret['entryType'])) {
440  $this->_generateWarning('WARNING_NOT_ALLOWED_ENTRY_TYPE', $ret['entryType'], $entry . '}');
441  }
442  }
443  //Handling the authors
444  if (in_array('author', array_keys($ret)) && $this->_options['extractAuthors']) {
445  $ret['author'] = $this->_extractAuthors($ret['author']);
446  }
447  }
448 
449  return $ret;
450  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:625
_stripDelimiter($entry)
Stripping Delimiter.
Definition: BibTex.php:597
_validateValue($entry, $wholeentry)
Validation of a value.
Definition: BibTex.php:853
_checkEqualSign($entry, $position)
Checking whether the position of the &#39;=&#39; is correct.
Definition: BibTex.php:467
_extractAuthors($entry)
Extracting the authors.
Definition: BibTex.php:659
_removeCurlyBraces($value)
Remove curly braces from entry.
Definition: BibTex.php:890
_checkAllowedEntryType($entry)
Checking if the entry type is allowed.
Definition: BibTex.php:528
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:928
+ 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 890 of file BibTex.php.

Referenced by _parseEntry().

890  {
891  //First we save the delimiters
892  $beginningdels = array_keys($this->_delimiters);
893  $firstchar = substr($entry, 0, 1);
894  $lastchar = substr($entry, - 1, 1);
895  $begin = '';
896  $end = '';
897  while (in_array($firstchar, $beginningdels)) { //The first character is an opening delimiter
898  if ($lastchar == $this->_delimiters[$firstchar]) { //Matches to closing Delimiter
899  $begin .= $firstchar;
900  $end .= $lastchar;
901  $value = substr($value, 1, - 1);
902  } else {
903  break;
904  }
905  $firstchar = substr($value, 0, 1);
906  $lastchar = substr($value, - 1, 1);
907  }
908  //Now we get rid of the curly braces
909  $pattern = '/([^\\\\])\{(.*?[^\\\\])\}/';
910  $replacement = '$1$2';
911  $value = preg_replace($pattern, $replacement, $value);
912  //Reattach delimiters
913  $value = $begin . $value . $end;
914 
915  return $value;
916  }
+ 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 597 of file BibTex.php.

Referenced by _parseEntry().

597  {
598  $beginningdels = array_keys($this->_delimiters);
599  $length = strlen($entry);
600  $firstchar = substr($entry, 0, 1);
601  $lastchar = substr($entry, - 1, 1);
602  while (in_array($firstchar, $beginningdels)) { //The first character is an opening delimiter
603  if ($lastchar == $this->_delimiters[$firstchar]) { //Matches to closing Delimiter
604  $entry = substr($entry, 1, - 1);
605  } else {
606  break;
607  }
608  $firstchar = substr($entry, 0, 1);
609  $lastchar = substr($entry, - 1, 1);
610  }
611 
612  return $entry;
613  }
+ 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 625 of file BibTex.php.

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

625  {
626  $entry = preg_replace('/\s+/', ' ', $entry);
627 
628  return trim($entry);
629  }
+ 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 853 of file BibTex.php.

References _generateWarning().

Referenced by _parseEntry().

853  {
854  //There is no @ allowed if the entry is enclosed by braces
855  if (preg_match('/^{.*@.*}$/', $entry)) {
856  $this->_generateWarning('WARNING_AT_IN_BRACES', $entry, $wholeentry);
857  }
858  //No escaped " allowed if the entry is enclosed by double quotes
859  if (preg_match('/^\".*\\".*\"$/', $entry)) {
860  $this->_generateWarning('WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES', $entry, $wholeentry);
861  }
862  //Amount of Braces is not correct
863  $open = 0;
864  $lastchar = '';
865  $char = '';
866  for ($i = 0; $i < strlen($entry); $i ++) {
867  $char = substr($entry, $i, 1);
868  if (('{' == $char) && ('\\' != $lastchar)) {
869  $open ++;
870  }
871  if (('}' == $char) && ('\\' != $lastchar)) {
872  $open --;
873  }
874  $lastchar = $char;
875  }
876  if (0 != $open) {
877  $this->_generateWarning('WARNING_UNBALANCED_AMOUNT_OF_BRACES', $entry, $wholeentry);
878  }
879  }
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:928
+ 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 641 of file BibTex.php.

641  {
642  if (('' != $entry) && (is_string($entry))) {
643  $entry = wordwrap($entry, $this->_options['wordWrapWidth'], $this->_options['wordWrapBreak'], $this->_options['wordWrapCut']);
644  }
645 
646  return $entry;
647  }

◆ 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 1067 of file BibTex.php.

1067  {
1068  $this->data[] = $newentry;
1069  }

◆ amount()

Structures_BibTex::amount ( )

Returns the amount of available BibTex entries.

public

Returns
int The amount of available BibTex entries

Definition at line 967 of file BibTex.php.

References $data.

967  {
968  return sizeof($this->data);
969  }

◆ 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 1022 of file BibTex.php.

References $bibtex, and _formatAuthor().

1022  {
1023  $bibtex = '';
1024  foreach ($this->data as $entry) {
1025  //Intro
1026  $bibtex .= '@' . strtolower($entry['entryType']) . ' { ' . $entry['cite'] . ",\n";
1027  //Other fields except author
1028  foreach ($entry as $key => $val) {
1029  if ($this->_options['wordWrapWidth'] > 0) {
1030  $val = $this->_wordWrap($val);
1031  }
1032  if (! in_array($key, array( 'cite', 'entryType', 'author' ))) {
1033  $bibtex .= "\t" . $key . ' = {' . $val . "},\n";
1034  }
1035  }
1036  //Author
1037  if (array_key_exists('author', $entry)) {
1038  if ($this->_options['extractAuthors']) {
1039  $tmparray = array(); //In this array the authors are saved and the joind with an and
1040  foreach ($entry['author'] as $authorentry) {
1041  $tmparray[] = $this->_formatAuthor($authorentry);
1042  }
1043  $author = join(' and ', $tmparray);
1044  } else {
1045  $author = $entry['author'];
1046  }
1047  } else {
1048  $author = '';
1049  }
1050  $bibtex .= "\tauthor = {" . $author . "}\n";
1051  $bibtex .= "}\n\n";
1052  }
1053 
1054  return $bibtex;
1055  }
_formatAuthor($array)
Returns the author formatted.
Definition: BibTex.php:983
+ Here is the call graph for this function:

◆ clearWarnings()

Structures_BibTex::clearWarnings ( )

Cleares all warnings.

public

Definition at line 941 of file BibTex.php.

941  {
942  $this->warnings = array();
943  }

◆ 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 1081 of file BibTex.php.

References $ret.

1081  {
1082  $ret = array();
1083  foreach ($this->data as $entry) {
1084  if (array_key_exists($entry['entryType'], $ret)) {
1085  $ret[$entry['entryType']] ++;
1086  } else {
1087  $ret[$entry['entryType']] = 1;
1088  }
1089  }
1090 
1091  return $ret;
1092  }

◆ hasWarning()

Structures_BibTex::hasWarning ( )

Is there a warning?

public

Returns
true if there is, false otherwise

Definition at line 952 of file BibTex.php.

952  {
953  if (sizeof($this->warnings) > 0) {
954  return true;
955  } else {
956  return false;
957  }
958  }

◆ 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 1167 of file BibTex.php.

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

1167  {
1168  $ret = "<p>\n";
1169  foreach ($this->data as $entry) {
1170  $line = $this->htmlstring;
1171  $title = '';
1172  $journal = '';
1173  $year = '';
1174  $authors = '';
1175  if (array_key_exists('title', $entry)) {
1176  $title = $this->_unwrap($entry['title']);
1177  }
1178  if (array_key_exists('journal', $entry)) {
1179  $journal = $this->_unwrap($entry['journal']);
1180  }
1181  if (array_key_exists('year', $entry)) {
1182  $year = $this->_unwrap($entry['year']);
1183  }
1184  if (array_key_exists('author', $entry)) {
1185  if ($this->_options['extractAuthors']) {
1186  $tmparray = array(); //In this array the authors are saved and the joind with an and
1187  foreach ($entry['author'] as $authorentry) {
1188  $tmparray[] = $this->_formatAuthor($authorentry);
1189  }
1190  $authors = join(', ', $tmparray);
1191  } else {
1192  $authors = $entry['author'];
1193  }
1194  }
1195  if (('' != $title) || ('' != $journal) || ('' != $year) || ('' != $authors)) {
1196  $line = str_replace("TITLE", $title, $line);
1197  $line = str_replace("JOURNAL", $journal, $line);
1198  $line = str_replace("YEAR", $year, $line);
1199  $line = str_replace("AUTHORS", $authors, $line);
1200  $line .= "\n";
1201  $ret .= $line;
1202  } else {
1203  $this->_generateWarning('WARNING_LINE_WAS_NOT_CONVERTED', '', print_r($entry, 1));
1204  }
1205  }
1206  $ret .= "</p>\n";
1207 
1208  return $ret;
1209  }
_unwrap($entry)
Unwrapping entry.
Definition: BibTex.php:625
_formatAuthor($array)
Returns the author formatted.
Definition: BibTex.php:983
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
Definition: BibTex.php:928
+ 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 229 of file BibTex.php.

References $filename, and PEAR\raiseError().

229  {
230  if (file_exists($filename)) {
231  if (($this->content = @file_get_contents($filename)) === false) {
232  return PEAR::raiseError('Could not open file ' . $filename);
233  } else {
234  $this->_pos = 0;
235  $this->_oldpos = 0;
236 
237  return true;
238  }
239  } else {
240  return PEAR::raiseError('Could not find file ' . $filename);
241  }
242  }
$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 251 of file BibTex.php.

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

251  {
252  //The amount of opening braces is compared to the amount of closing braces
253  //Braces inside comments are ignored
254  $this->warnings = array();
255  $this->data = array();
256  $valid = true;
257  $open = 0;
258  $entry = false;
259  $char = '';
260  $lastchar = '';
261  $buffer = '';
262  for ($i = 0; $i < strlen($this->content); $i ++) {
263  $char = substr($this->content, $i, 1);
264  if ((0 != $open) && ('@' == $char)) {
265  if (! $this->_checkAt($buffer)) {
266  $this->_generateWarning('WARNING_MISSING_END_BRACE', '', $buffer);
267  //To correct the data we need to insert a closing brace
268  $char = '}';
269  $i --;
270  }
271  }
272  if ((0 == $open) && ('@' == $char)) { //The beginning of an entry
273  $entry = true;
274  } elseif ($entry && ('{' == $char)
275  && ('\\' != $lastchar)
276  ) { //Inside an entry and non quoted brace is opening
277  $open ++;
278  } elseif ($entry && ('}' == $char)
279  && ('\\' != $lastchar)
280  ) { //Inside an entry and non quoted brace is closing
281  $open --;
282  if ($open < 0) { //More are closed than opened
283  $valid = false;
284  }
285  if (0 == $open) { //End of entry
286  $entry = false;
287  $entrydata = $this->_parseEntry($buffer);
288  if (! $entrydata) {
295  } else {
296  $this->data[] = $entrydata;
297  }
298  $buffer = '';
299  }
300  }
301  if ($entry) { //Inside entry
302  $buffer .= $char;
303  }
304  $lastchar = $char;
305  }
306  //If open is one it may be possible that the last ending brace is missing
307  if (1 == $open) {
308  $entrydata = $this->_parseEntry($buffer);
309  if (! $entrydata) {
310  $valid = false;
311  } else {
312  $this->data[] = $entrydata;
313  $buffer = '';
314  $open = 0;
315  }
316  }
317  //At this point the open should be zero
318  if (0 != $open) {
319  $valid = false;
320  }
321  //Are there Multiple entries with the same cite?
322  if ($this->_options['validate']) {
323  $cites = array();
324  foreach ($this->data as $entry) {
325  $cites[] = $entry['cite'];
326  }
327  $unique = array_unique($cites);
328  if (sizeof($cites) != sizeof($unique)) { //Some values have not been unique!
329  $notuniques = array();
330  for ($i = 0; $i < sizeof($cites); $i ++) {
331  if ('' == $unique[$i]) {
332  $notuniques[] = $cites[$i];
333  }
334  }
335  $this->_generateWarning('WARNING_MULTIPLE_ENTRIES', implode(',', $notuniques));
336  }
337  }
338  if ($valid) {
339  $this->content = '';
340 
341  return true;
342  } else {
343  return PEAR::raiseError('Unbalanced parenthesis');
344  }
345  }
_parseEntry($entry)
Extracting the data of one content.
Definition: BibTex.php:370
$valid
_checkAt($entry)
Checking whether an at is outside an entry.
Definition: BibTex.php:546
& 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:928
+ 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 1109 of file BibTex.php.

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

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

References $ret, and PEAR\raiseError().

Referenced by Structures_BibTex().

208  {
209  $ret = true;
210  if (array_key_exists($option, $this->_options)) {
211  $this->_options[$option] = $value;
212  } else {
213  $ret = PEAR::raiseError('Unknown option ' . $option);
214  }
215 
216  return $ret;
217  }
& 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 150 of file BibTex.php.

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

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