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.

231 lines
12 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./includes/cite.inc.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/includes/cite.inc.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 25-May-06, 15:19
  15. // Modified: $Date: 2012-02-27 20:25:30 +0000 (Mon, 27 Feb 2012) $
  16. // $Author: msteffens $
  17. // $Revision: 1337 $
  18. // This file contains functions
  19. // that are used when outputting
  20. // references as citations.
  21. // Include common transliteration/translation tables and search & replace patterns
  22. include 'includes/transtab_refbase_rtf.inc.php'; // include refbase markup -> RTF search & replace patterns
  23. include 'includes/transtab_refbase_pdf.inc.php'; // include refbase markup -> PDF search & replace patterns
  24. include 'includes/transtab_refbase_latex.inc.php'; // include refbase markup -> LaTeX search & replace patterns
  25. include 'includes/transtab_refbase_markdown.inc.php'; // include refbase markup -> Markdown search & replace patterns
  26. include 'includes/transtab_refbase_ascii.inc.php'; // include refbase markup -> plain text search & replace patterns
  27. if ($contentTypeCharset == "UTF-8") // variable '$contentTypeCharset' is defined in 'ini.inc.php'
  28. include_once 'includes/transtab_unicode_latex.inc.php'; // include Unicode -> LaTeX translation table
  29. else // we assume "ISO-8859-1" by default
  30. include_once 'includes/transtab_latin1_latex.inc.php'; // include Latin1 -> LaTeX translation table
  31. // --------------------------------------------------------------------
  32. // Print any section heading(s):
  33. function generateSectionHeading($yearsArray, $typeTitlesArray, $row, $citeOrder, $headingPrefix, $headingSuffix, $sectionMarkupPrefix, $sectionMarkupSuffix, $subSectionMarkupPrefix, $subSectionMarkupSuffix)
  34. {
  35. global $loc;
  36. $sectionHeading = "";
  37. if (!empty($row['year']))
  38. $yearHeading = $row['year'];
  39. else
  40. $yearHeading = $loc["notSpecified"];
  41. // List records in blocks sorted by record type:
  42. if (($citeOrder == "type") OR ($citeOrder == "type-year"))
  43. {
  44. $typeTitle = generateTypeTitle($row['type'], $row['thesis']); // assign an appropriate title to this record type
  45. if (!in_array($typeTitle, $typeTitlesArray)) // if this record's type title hasn't occurred already
  46. {
  47. $typeTitlesArray[$row['type']] = $typeTitle; // add this title to the array of type titles
  48. $sectionHeading .= $headingPrefix . $sectionMarkupPrefix . $typeTitle . $sectionMarkupSuffix . $headingSuffix; // print out a the current record type
  49. }
  50. // List records in sub-blocks sorted by year:
  51. if ($citeOrder == "type-year")
  52. {
  53. if (!isset($yearsArray[$typeTitle]) OR !in_array($yearHeading, $yearsArray[$typeTitle])) // if this record's year hasn't occurred already for this record's type
  54. {
  55. $yearsArray[$typeTitle][] = $yearHeading; // add it to the record-specific array of years
  56. $sectionHeading .= $headingPrefix . $subSectionMarkupPrefix . $yearHeading . $subSectionMarkupSuffix . $headingSuffix; // print out a the current year
  57. }
  58. }
  59. }
  60. // List records in blocks sorted by year:
  61. elseif ($citeOrder == "year")
  62. {
  63. if (!in_array($yearHeading, $yearsArray)) // if this record's year hasn't occurred already
  64. {
  65. $yearsArray[] = $yearHeading; // add it to the array of years
  66. $sectionHeading .= $headingPrefix . $sectionMarkupPrefix . $yearHeading . $sectionMarkupSuffix . $headingSuffix; // print out a the current year
  67. }
  68. }
  69. return array($yearsArray, $typeTitlesArray, $sectionHeading);
  70. }
  71. // --------------------------------------------------------------------
  72. // Assign an appropriate title to a given record or thesis type:
  73. function generateTypeTitle($recordType, $thesis)
  74. {
  75. global $contentTypeCharset; // defined in 'ini.inc.php'
  76. global $citeType;
  77. global $loc;
  78. global $availableTypeTitlesArray; // these variables are made globally available from within this function
  79. global $availableThesisTitlesArray;
  80. if (empty($thesis))
  81. {
  82. if (!isset($availableTypeTitlesArray))
  83. // Map record types with items of the global localization array ('$loc'):
  84. $availableTypeTitlesArray = array(
  85. "Journal Article" => "JournalArticles",
  86. "Abstract" => "Abstracts",
  87. "Book Chapter" => "BookContributions",
  88. "Book Whole" => "Monographs",
  89. "Conference Article" => "ConferenceArticles",
  90. "Conference Volume" => "ConferenceVolumes",
  91. "Journal" => "Journals",
  92. "Magazine Article" => "MagazineArticles",
  93. "Manual" => "Manuals",
  94. "Manuscript" => "Manuscripts",
  95. "Map" => "Maps",
  96. "Miscellaneous" => "Miscellaneous",
  97. "Newspaper Article" => "NewspaperArticles",
  98. "Patent" => "Patents",
  99. "Report" => "Reports",
  100. "Software" => "Software"
  101. );
  102. if (isset($recordType, $availableTypeTitlesArray))
  103. $typeTitle = $loc[$availableTypeTitlesArray[$recordType]];
  104. else
  105. $typeTitle = $loc["OtherPublications"];
  106. }
  107. else
  108. {
  109. if (!isset($availableThesisTitlesArray))
  110. // Map thesis types with items of the global localization array ('$loc'):
  111. $availableThesisTitlesArray = array(
  112. "Bachelor's thesis" => "Theses_Bachelor",
  113. "Master's thesis" => "Theses_Master",
  114. "Ph.D. thesis" => "Theses_PhD",
  115. "Diploma thesis" => "Theses_Diploma",
  116. "Doctoral thesis" => "Theses_Doctoral",
  117. "Habilitation thesis" => "Theses_Habilitation"
  118. );
  119. if (isset($thesis, $availableThesisTitlesArray))
  120. $typeTitle = $loc[$availableThesisTitlesArray[$thesis]];
  121. else
  122. $typeTitle = $loc["Theses_Other"];
  123. }
  124. if (!preg_match("/^html$/i", $citeType)) // for citation formats other than HTML:
  125. // apply dirty hack that reverses the HTML encoding of locales (which were HTML encoded globally in 'core.inc.php');
  126. // note that function 'html_entity_decode' doesn't support multibyte character sets (such as UTF-8) in PHP versions < 5
  127. // (see <http://www.php.net/manual/en/function.html-entity-decode.php>)
  128. $typeTitle = html_entity_decode($typeTitle, ENT_QUOTES, $contentTypeCharset);
  129. return $typeTitle;
  130. }
  131. // --------------------------------------------------------------------
  132. // Format page information:
  133. //
  134. // NOTES: - this function (and refbase in general) assumes following rules for the original formatting of page information in '$origPageInfo':
  135. // - single-page items are given as a page range with identical start & end numbers (e.g. "127-127")
  136. // - multi-page items are given as a page range where the end number is greater than the start number (e.g. "127-132")
  137. // - for multi-page items where only the start page is known, a hyphen is appended to the start page (e.g. "127-")
  138. // - total number of pages are given with a "pp" suffix (e.g. "498 pp"), see TODO
  139. // - the given page info is left as is if it does not match any of the above rules (e.g. a single page number is ambiguous since it
  140. // could mean a single page or the total number of pages)
  141. // - the function attempts to deal with page locators that contain letters (e.g. "A1 - A3" or "4a-4c") but, ATM, locator parts (e.g. "A1")
  142. // must contain at least one digit character & must not contain any whitespace
  143. //
  144. // TODO: - should we only use Unicode-aware regex expressions (i.e. always use '$space', '$digit' or '$word' instead of ' ', '\d' or '\w', etc)?
  145. // - recognize & process total number of pages
  146. // - for '$shortenPageRangeEnd=true', add support for page locators that contain letters (e.g. "A1 - A3" or "4a-4c")
  147. function formatPageInfo($origPageInfo, $pageRangeDelim = "-", $singlePagePrefix = "", $pageRangePrefix = "", $totalPagesPrefix = "", $singlePageSuffix = "", $pageRangeSuffix = "", $totalPagesSuffix = "", $shortenPageRangeEnd = false)
  148. {
  149. global $alnum, $alpha, $cntrl, $dash, $digit, $graph, $lower, $print, $punct, $space, $upper, $word, $patternModifiers; // defined in 'transtab_unicode_charset.inc.php' and 'transtab_latin1_charset.inc.php'
  150. // Check original page info for any recognized page locators, and process them appropriately:
  151. if (preg_match("/\w*\d+\w* *[$dash]+ *(?:\w*\d+\w*)?/$patternModifiers", $origPageInfo)) // the original page info contains a page range (like: "127-127", "127-132", "A1 - A3", "4a-4c", or "127-" if only start page given)
  152. {
  153. // Remove any whitespace around dashes or hyphens that indicate a page range:
  154. $origPageInfo = preg_replace("/(\w*\d+\w*) *([$dash]+) *(\w*\d+\w*)?(?=[^\w\d]|$)/$patternModifiers", "\\1\\2\\3", $origPageInfo);
  155. // Split original page info into its functional parts:
  156. // NOTE: ATM, we simply split on any whitespace characters, then process all parts with page ranges
  157. // (this will also reduce runs of whitespace to a single space)
  158. $partsArray = preg_split("/ +/", $origPageInfo);
  159. $partsCount = count($partsArray);
  160. for ($i=0; $i < $partsCount; $i++)
  161. {
  162. // Format parts with page ranges:
  163. // - single-page item:
  164. if (preg_match("/(\w*\d+\w*)[$dash]+\\1(?=[^\w\d]|$)/$patternModifiers", $partsArray[$i])) // this part contains a page range with identical start & end numbers (like: "127-127")
  165. $partsArray[$i] = preg_replace("/(\w*\d+\w*)[$dash]+\\1(?=[^\w\d]|$)/$patternModifiers", $singlePagePrefix . "\\1" . $singlePageSuffix, $partsArray[$i]);
  166. // - multi-page item:
  167. elseif (preg_match("/\w*\d+\w*[$dash]+(?:\w*\d+\w*)?(?=[^\w\d]|$)/$patternModifiers", $partsArray[$i])) // this part contains a page range (like: "127-132", or "127-" if only start page given)
  168. {
  169. // In case of '$shortenPageRangeEnd=true', we abbreviate ending page numbers so that digits aren't repeated unnecessarily:
  170. if ($shortenPageRangeEnd AND preg_match("/\d+[$dash]+\d+/$patternModifiers", $partsArray[$i])) // ATM, only digit-only page locators (like: "127-132") are supported
  171. {
  172. // NOTE: the logic of this 'if' clause doesn't work if the original page info contains something like "173-190; 195-195" (where, for the first page range, '$endPage' would be "190;" and not "190")
  173. list($startPage, $endPage) = preg_split("/[$dash]+/$patternModifiers", $partsArray[$i]);
  174. $countStartPage = strlen($startPage);
  175. $countEndPage = strlen($endPage);
  176. if(($countStartPage == $countEndPage) AND ($startPage < $endPage))
  177. {
  178. for ($j=0; $j < $countStartPage; $j++)
  179. {
  180. if (preg_match("/^" . substr($startPage, $j, 1) . "/", $endPage)) // if the ending page number has a digit that's identical to the starting page number (at the same digit offset)
  181. $endPage = substr($endPage, 1); // remove the first digit from the remaining ending page number
  182. else
  183. break;
  184. }
  185. }
  186. $partsArray[$i] = $pageRangePrefix . $startPage . $pageRangeDelim . $endPage . $pageRangeSuffix;
  187. }
  188. else // don't abbreviate ending page numbers:
  189. $partsArray[$i] = preg_replace("/(\w*\d+\w*)[$dash]+(\w*\d+\w*)?(?=[^\w\d]|$)/$patternModifiers", $pageRangePrefix . "\\1" . $pageRangeDelim . "\\2" . $pageRangeSuffix, $partsArray[$i]);
  190. }
  191. }
  192. $newPageInfo = join(" ", $partsArray); // merge again all parts
  193. }
  194. else
  195. $newPageInfo = $origPageInfo; // page info is ambiguous, so we don't mess with it
  196. return $newPageInfo;
  197. }
  198. ?>