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.

144 lines
3.6 KiB

  1. <?php
  2. /**
  3. * Enum for citation types
  4. */
  5. abstract class RefbaseCitationType {
  6. // Minimal reference type (author, title, publication, year)
  7. const CT_MINIMAL = 0;
  8. // Request citation from refbase installation (using show.php interface)
  9. const CT_RB = 1;
  10. /**
  11. * Convert string to RefbaseCitationType
  12. */
  13. static public function decodeCitationType ( $str, & $citeStyle ) {
  14. if ( strtolower( $str ) == 'minimal' ) {
  15. return self::CT_MINIMAL;
  16. } elseif ( preg_match( '/rb-(.*)/', strtolower( $str ),
  17. $citeStyle ) ) {
  18. return self::CT_RB;
  19. } else {
  20. return null;
  21. }
  22. }
  23. }
  24. /**
  25. * Helper class to generate citation text
  26. */
  27. class RefbaseCitationCreator {
  28. /// Citation type
  29. private $citationType;
  30. /// Citation style (only with $citationType = CT_RB)
  31. private $citationStyle = "";
  32. /// Location of refbase installation (may differ from $dbHost if using https
  33. /// for instance)
  34. protected $refbaseURL = "";
  35. /**
  36. * Constructor
  37. */
  38. public function __construct( $citationTypeStr ) {
  39. global $wgRefbaseURL;
  40. $this->refbaseURL = $wgRefbaseURL;
  41. $this->citationType =
  42. RefbaseCitationType::decodeCitationType( $citationTypeStr,
  43. $citeStyle );
  44. if ( !empty( $citeStyle ) ) {
  45. $this->citationStyle = $citeStyle[1];
  46. }
  47. wfDebug('refbase-decode-in:' . $citationTypeStr . "\n");
  48. wfDebug('refbase-decode:' . $this->citationType . ", " . var_export($this->citationStyle,true)."\n");
  49. }
  50. /**
  51. * Create citation text
  52. */
  53. public function createCitation( $entry, & $cite ) {
  54. switch( $this->citationType ) {
  55. case RefbaseCitationType::CT_MINIMAL:
  56. $cite = $entry['author'] . ", " . $entry['title'] . ", " .
  57. $entry['publication'] . ", " . $entry['year'] . ".";
  58. break;
  59. case RefbaseCitationType::CT_RB:
  60. $url = $this->refbaseURL . "show.php?" .
  61. "record=" . "27711"//$entry['serial'] .
  62. "&submit=Cite&exportType=text&citeType=ASCII";
  63. if ( !empty( $this->citationStyle ) ) {
  64. $url .= "&citeStyle=" . $this->citationStyle;
  65. }
  66. wfDebug('refbase-getcite:' . $url . "\n");
  67. // Get citation from url (add http authentication if desired)
  68. global $wgRefbaseURLAuth;
  69. if ( !empty( $wgRefbaseURLAuth ) ) {
  70. if ( strcmp( strtolower( $wgRefbaseURLAuth ),
  71. 'default' ) == 0 ) {
  72. if ( isset( $_SERVER['PHP_AUTH_USER'] ) &&
  73. isset( $_SERVER['PHP_AUTH_PW'] ) ) {
  74. $username = $_SERVER['PHP_AUTH_USER'];
  75. $password = $_SERVER['PHP_AUTH_PW'];
  76. $authStr = "Authorization: Basic " .
  77. base64_encode( "$username:$password" );
  78. } else {
  79. $authStr = '';
  80. }
  81. } else {
  82. preg_match( "/([^:]*):(.*)$/", $wgRefbaseURLAuth, $out);
  83. $username = $out[1];
  84. $password = $out[2];
  85. $authStr = "Authorization: Basic " .
  86. base64_encode( "$username:$password" );
  87. }
  88. $param = array( 'http' => array( 'header' => $authStr ) );
  89. $context = stream_context_create( $param );
  90. $cite = trim( file_get_contents( $url, false, $context ) );
  91. } else {
  92. $cite = trim( file_get_contents( $url ) );
  93. }
  94. break;
  95. default:
  96. $cite = wfMessage( 'refbase-error-citation-type' )->text();
  97. }
  98. return true;
  99. }
  100. /*
  101. * Get list of required fields to produce the citation in the desired format
  102. */
  103. public function getFieldList() {
  104. switch( $this->citationType ) {
  105. case RefbaseCitationType::CT_MINIMAL:
  106. $fieldList = array( 'author',
  107. 'title',
  108. 'publication',
  109. 'year' );
  110. break;
  111. case RefbaseCitationType::CT_RB:
  112. $fieldList = array( 'serial' );
  113. break;
  114. default:
  115. $fieldList = array();
  116. }
  117. return $fieldList;
  118. }
  119. }