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.

139 lines
3.4 KiB

  1. <?php
  2. /**
  3. * Refbase database connector
  4. */
  5. class RefbaseConnector {
  6. /// Database location
  7. private $dbHost = "";
  8. /// Database name
  9. private $dbName = "";
  10. /// Database user
  11. private $dbUser = "";
  12. /// Database password
  13. private $dbPass = "";
  14. /// Character set
  15. private $dbCharset = "";
  16. /// Reference table
  17. private $dbRefTable = "";
  18. /// User data table (for cite key entry)
  19. private $dbUserDataTable = "";
  20. /// Method to access database (mysql or PDO)
  21. private $dbAccessMethod = "";
  22. /**
  23. * Constructor
  24. */
  25. public function __construct() {
  26. global $wgRefbaseDbHost;
  27. global $wgRefbaseDbName;
  28. global $wgRefbaseDbUser;
  29. global $wgRefbaseDbPass;
  30. global $wgRefbaseDbRefTable;
  31. global $wgRefbaseDbUserDataTable;
  32. global $wgRefbaseDbCharset;
  33. global $wgRefbaseDbAccessMethod;
  34. // Read from global configuration
  35. $this->dbHost = $wgRefbaseDbHost;
  36. $this->dbName = $wgRefbaseDbName;
  37. $this->dbUser = $wgRefbaseDbUser;
  38. $this->dbPass = $wgRefbaseDbPass;
  39. $this->dbRefTable = $wgRefbaseDbRefTable;
  40. $this->dbUserDataTable = $wgRefbaseDbUserDataTable;
  41. $this->dbCharset = $wgRefbaseDbCharset;
  42. $this->dbAccessMethod = $wgRefbaseDbAccessMethod;
  43. }
  44. /**
  45. * Query by serial number or cite key entry
  46. */
  47. public function getEntry( $input, $tagTypeList, & $outputEntry,
  48. $fieldList ) {
  49. // List of fields to extract (prefix 'r.' to each element)
  50. $fieldPref = $fieldList;
  51. array_walk( $fieldPref, function ( &$value, $key) {
  52. $value="r.$value";
  53. } );
  54. $fieldPref = join(",", $fieldPref);
  55. $flagFound = false;
  56. for ( $i = 0; $i < count($tagTypeList) && ! $flagFound; $i++ ) {
  57. $tagType = $tagTypeList[$i];
  58. // Query string
  59. $queryStr = "";
  60. if ( $tagType === 'citekey' ) {
  61. $queryStr = "SELECT $fieldPref " .
  62. "FROM " . $this->dbRefTable . " r " .
  63. "INNER JOIN " . $this->dbUserDataTable . " u " .
  64. "ON r.serial = u.record_id " .
  65. "WHERE u.cite_key='$input'";
  66. } else {
  67. $queryStr = "SELECT $fieldPref " .
  68. "FROM " . $this->dbRefTable . " r " .
  69. "WHERE r.serial='$input'";
  70. }
  71. if ( strtolower( $this->dbAccessMethod ) === 'pdo' ) {
  72. // Connect and query
  73. $link = new PDO( 'mysql:host=' . $this->dbHost . ';dbname=' .
  74. $this->dbName . ';charset=' . $this->dbCharset,
  75. $this->dbUser, $this->dbPass );
  76. $dbexec = $link->prepare( $queryStr );
  77. try {
  78. // Perform query
  79. $outputEntry = $dbexec->execute();
  80. } catch( PDOException $ex ) {
  81. $outputEntry = wfMessage( 'refbase-error-dbquery' )->text() .
  82. $ex->getMessage();
  83. return false;
  84. }
  85. $outputEntry = $dbexec->fetch(PDO::FETCH_ASSOC);
  86. } elseif ( strtolower( $this->dbAccessMethod ) === 'mysql' ) {
  87. $link = mysqli_connect( $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName ) or die("db error");
  88. if ( !$link ) {
  89. $outputEntry = wfMessage( 'refbase-error-mysqlconn' )->text();
  90. return false;
  91. }
  92. $result = mysqli_query($link, $queryStr );
  93. if ( !$result ) {
  94. $outputEntry = wfMessage( 'refbase-error-dbquery' )->text() .
  95. mysqli_error($link);
  96. return false;
  97. }
  98. $outputEntry = mysqli_fetch_array($result);
  99. }
  100. if ( !empty( $outputEntry ) ) {
  101. $flagFound = true;
  102. }
  103. }
  104. if ( empty( $outputEntry ) ) {
  105. $outputEntry = wfMessage( 'refbase-error-notfound' )->text();
  106. return false;
  107. }
  108. return true;
  109. }
  110. }