You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
6.6 KiB

  1. <?php
  2. /**
  3. * Refbase helper functions (cleanup strings, author extraction)
  4. */
  5. class RefbaseTools {
  6. /// Character translation table
  7. private static $transtab_refbase_html = array(
  8. "/__(?!_)(.+?)__/" => "<u>\\1</u>", // the pattern for underline (__...__) must come before the one for italic (_..._)
  9. "/_(.+?)_/" => "<i>\\1</i>",
  10. "/\\*\\*(.+?)\\*\\*/" => "<b>\\1</b>",
  11. "/\\[super:(.+?)\\]/i" => "<sup>\\1</sup>",
  12. "/\\[sub:(.+?)\\]/i" => "<sub>\\1</sub>",
  13. "/\\[permil\\]/" => "&permil;",
  14. "/\\[infinity\\]/" => "&infin;",
  15. "/\\[alpha\\]/" => "&alpha;",
  16. "/\\[beta\\]/" => "&beta;",
  17. "/\\[gamma\\]/" => "&gamma;",
  18. "/\\[delta\\]/" => "&delta;",
  19. "/\\[epsilon\\]/" => "&epsilon;",
  20. "/\\[zeta\\]/" => "&zeta;",
  21. "/\\[eta\\]/" => "&eta;",
  22. "/\\[theta\\]/" => "&theta;",
  23. "/\\[iota\\]/" => "&iota;",
  24. "/\\[kappa\\]/" => "&kappa;",
  25. "/\\[lambda\\]/" => "&lambda;",
  26. "/\\[mu\\]/" => "&mu;",
  27. "/\\[nu\\]/" => "&nu;",
  28. "/\\[xi\\]/" => "&xi;",
  29. "/\\[omicron\\]/" => "&omicron;",
  30. "/\\[pi\\]/" => "&pi;",
  31. "/\\[rho\\]/" => "&rho;",
  32. "/\\[sigmaf\\]/" => "&sigmaf;",
  33. "/\\[sigma\\]/" => "&sigma;",
  34. "/\\[tau\\]/" => "&tau;",
  35. "/\\[upsilon\\]/" => "&upsilon;",
  36. "/\\[phi\\]/" => "&phi;",
  37. "/\\[chi\\]/" => "&chi;",
  38. "/\\[psi\\]/" => "&psi;",
  39. "/\\[omega\\]/" => "&omega;",
  40. "/\\[Alpha\\]/" => "&Alpha;",
  41. "/\\[Beta\\]/" => "&Beta;",
  42. "/\\[Gamma\\]/" => "&Gamma;",
  43. "/\\[Delta\\]/" => "&Delta;",
  44. "/\\[Epsilon\\]/" => "&Epsilon;",
  45. "/\\[Zeta\\]/" => "&Zeta;",
  46. "/\\[Eta\\]/" => "&Eta;",
  47. "/\\[Theta\\]/" => "&Theta;",
  48. "/\\[Iota\\]/" => "&Iota;",
  49. "/\\[Kappa\\]/" => "&Kappa;",
  50. "/\\[Lambda\\]/" => "&Lambda;",
  51. "/\\[Mu\\]/" => "&Mu;",
  52. "/\\[Nu\\]/" => "&Nu;",
  53. "/\\[Xi\\]/" => "&Xi;",
  54. "/\\[Omicron\\]/" => "&Omicron;",
  55. "/\\[Pi\\]/" => "&Pi;",
  56. "/\\[Rho\\]/" => "&Rho;",
  57. "/\\[Sigma\\]/" => "&Sigma;",
  58. "/\\[Tau\\]/" => "&Tau;",
  59. "/\\[Upsilon\\]/" => "&Upsilon;",
  60. "/\\[Phi\\]/" => "&Phi;",
  61. "/\\[Chi\\]/" => "&Chi;",
  62. "/\\[Psi\\]/" => "&Psi;",
  63. "/\\[Omega\\]/" => "&Omega;",
  64. "/(?:\"|&quot;)(.+?)(?:\"|&quot;)/" => "&ldquo;\\1&rdquo;",
  65. "/ +- +/" => " &#8211; "
  66. );
  67. // EXTRACT AUTHOR'S LAST NAME
  68. // this function takes the contents of the author field and will extract the last name of a particular author (specified by position)
  69. // (e.g., setting '$authorPosition' to "1" will return the 1st author's last name)
  70. // Note: this function assumes that:
  71. // 1. within one author object, there's only *one* delimiter separating author name & initials!
  72. // 2. author objects are stored in the db as "<author_name><author_initials_delimiter><author_initials>", i.e., initials follow *after* the author's name!
  73. // Required Parameters:
  74. // 1. pattern describing delimiter that separates different authors
  75. // 2. pattern describing delimiter that separates author name & initials (within one author)
  76. // 3. position of the author whose last name shall be extracted (e.g., "1" will return the 1st author's last name)
  77. // 4. contents of the author field
  78. public static function extractAuthorsLastName( $oldBetweenAuthorsDelim, $oldAuthorsInitialsDelim, $authorPosition, $authorContents ) {
  79. $authorsArray = preg_split( "/" . $oldBetweenAuthorsDelim . "/", $authorContents ); // get a list of all authors for this record
  80. $authorPosition = $authorPosition - 1; // php array elements start with "0", so we decrease the authors position by 1
  81. $singleAuthor = $authorsArray[$authorPosition]; // for the author in question, extract the full author name (last name & initials)
  82. $singleAuthorArray = preg_split( "/" . $oldAuthorsInitialsDelim . "/", $singleAuthor ); // then, extract author name & initials to separate list items
  83. $singleAuthorsLastName = $singleAuthorArray[0]; // extract this author's last name into a new variable
  84. return $singleAuthorsLastName;
  85. }
  86. // EXTRACT AUTHOR'S GIVEN NAME
  87. // this function takes the contents of the author field and will extract the given name of a particular author (specified by position)
  88. // (e.g., setting '$authorPosition' to "1" will return the 1st author's given name)
  89. // Required Parameters:
  90. // 1. pattern describing delimiter that separates different authors
  91. // 2. pattern describing delimiter that separates author name & initials (within one author)
  92. // 3. position of the author whose last name shall be extracted (e.g., "1" will return the 1st author's last name)
  93. // 4. contents of the author field
  94. public static function extractAuthorsGivenName( $oldBetweenAuthorsDelim, $oldAuthorsInitialsDelim, $authorPosition, $authorContents ) {
  95. $authorsArray = preg_split( "/" . $oldBetweenAuthorsDelim . "/", $authorContents ); // get a list of all authors for this record
  96. $authorPosition = $authorPosition - 1; // php array elements start with "0", so we decrease the authors position by 1
  97. $singleAuthor = $authorsArray[$authorPosition]; // for the author in question, extract the full author name (last name & initials)
  98. $singleAuthorArray = preg_split( "/" . $oldAuthorsInitialsDelim . "/", $singleAuthor ); // then, extract author name & initials to separate list items
  99. if ( !empty($singleAuthorArray[1]) ) {
  100. $singleAuthorsGivenName = $singleAuthorArray[1]; // extract this author's last name into a new variable
  101. } else {
  102. $singleAuthorsGivenName = '';
  103. }
  104. return $singleAuthorsGivenName;
  105. }
  106. // Perform search & replace actions on the given text input:
  107. // ('$includesSearchPatternDelimiters' must be a boolean value that specifies whether the leading and trailing slashes
  108. // are included within the search pattern ['true'] or not ['false'])
  109. public static function searchReplaceText( $sourceString, $includesSearchPatternDelimiters ) {
  110. // apply the search & replace actions defined in '$transtab_refbase_html' to the text passed in '$sourceString':
  111. foreach ( self::$transtab_refbase_html as $searchString => $replaceString ) {
  112. if ( !$includesSearchPatternDelimiters ) {
  113. $searchString = "/" . $searchString . "/"; // add search pattern delimiters
  114. }
  115. if ( preg_match($searchString, $sourceString ) ) {
  116. $sourceString = preg_replace( $searchString, $replaceString, $sourceString );
  117. }
  118. }
  119. return $sourceString;
  120. }
  121. }