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.

113 lines
3.3 KiB

  1. <?php
  2. /**
  3. * Refbase hooks and parser
  4. */
  5. class RefbaseHooks {
  6. /**
  7. * Register <refbase> hook
  8. */
  9. public static function efRefbaseParserInit( $parser ) {
  10. $parser->setHook( 'refbase',
  11. 'RefbaseHooks::efRefbaseRender' );
  12. return true;
  13. }
  14. /**
  15. * Define special formatting for this tag
  16. */
  17. private static function makeOutputString ( $str ) {
  18. return $str;
  19. }
  20. /**
  21. * Add <pre></pre> tags around error message and return
  22. */
  23. private static function makeErrorOutputString ( $errMsg ) {
  24. $errMsg = "Refbase: <br/>" . $errMsg;
  25. $preMsg = Html::openElement( 'pre' ) . $errMsg .
  26. Html::closeElement( 'pre' );
  27. return self::makeOutputString( $preMsg );
  28. }
  29. /**
  30. * Main function: parse input and create HTML table with events
  31. */
  32. public static function efRefbaseRender( $input, array $args,
  33. Parser $parser,
  34. PPFrame $frame ) {
  35. // Global parameters
  36. global $wgRefbaseDefaultTagType;
  37. global $wgRefbaseDefaultOutputType;
  38. global $wgRefbaseDefaultCitationType;
  39. // Read arguments
  40. if ( isset( $args['tagtype'] ) ) {
  41. $tagType = $args['tagtype'];
  42. } else {
  43. $tagType = $wgRefbaseDefaultTagType;
  44. }
  45. if ( ! ( strtolower( $tagType ) === 'serial' ) &&
  46. ! ( strtolower( $tagType ) === 'citekey' ) ) {
  47. $errStr = wfMessage( 'refbase-error-tagtype' )->text();
  48. return self::makeErrorOutputString( $errStr );
  49. }
  50. if ( isset( $args['output'] ) ) {
  51. $outputType = $args['output'];
  52. } else {
  53. $outputType = $wgRefbaseDefaultOutputType;
  54. }
  55. if ( ! ( strtolower( $outputType ) === 'cite_journal' ) &&
  56. ! ( strtolower( $outputType ) === 'link' ) &&
  57. ! ( strtolower( $outputType ) === 'cite' ) ) {
  58. $errStr = wfMessage( 'refbase-error-outputtype' )->text();
  59. return self::makeErrorOutputString( $errStr );
  60. }
  61. if ( isset( $args['citationtype'] ) ) {
  62. $citationType = $args['citationtype'];
  63. } else {
  64. $citationType = $wgRefbaseDefaultCitationType;
  65. }
  66. if ( ! ( strtolower( $citationType ) === 'minimal' ) &&
  67. ! ( strtolower( substr( $citationType, 0, 3 ) ) === 'rb-' ) ) {
  68. $errStr = wfMessage( 'refbase-error-citation-type' )->text();
  69. return self::makeErrorOutputString( $errStr );
  70. }
  71. // Order tag types
  72. switch ( strtolower( $tagType ) ) {
  73. case 'serial':
  74. $tagTypeList = array( 'serial', 'citekey' );
  75. break;
  76. case 'citekey':
  77. $tagTypeList = array( 'citekey', 'serial' );
  78. break;
  79. }
  80. // Instantiate renderer based on options
  81. $refbaseRenderer = RefbaseRenderer::create( $outputType,
  82. $citationType );
  83. // Request list of fields to extract
  84. $fieldList = $refbaseRenderer->getFieldList();
  85. // Perform database query to get entry
  86. $refbaseDbConnector = new RefbaseConnector();
  87. $entry = "";
  88. if ( !$refbaseDbConnector->getEntry( $input, $tagTypeList, $entry,
  89. $fieldList ) ) {
  90. return self::makeErrorOutputString( $entry );
  91. }
  92. // Generate output
  93. $citekey = $input;
  94. $renderOpts = array( 'citekey' => $citekey );
  95. $outputStr = "";
  96. if ( !$refbaseRenderer->render( $entry, $outputStr, $renderOpts ) ) {
  97. return self::makeErrorOutputString( $outputStr );
  98. }
  99. $outputStr = $parser->recursiveTagParse( $outputStr );
  100. return self::makeOutputString( $outputStr );
  101. }
  102. }