database.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is addons.mozilla.org site.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * The Mozilla Foundation.
  19. * Portions created by the Initial Developer are Copyright (C) 2007
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Justin Scott <fligtar@mozilla.com> (Original Author)
  24. * Wil Clouser <wclouser@mozilla.com>
  25. *
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. // Include config file
  41. $root = dirname(dirname(__FILE__));
  42. require_once("{$root}/site/app/config/config.php");
  43. require_once("{$root}/site/app/config/constants.php");
  44. /**
  45. * Database class for services
  46. */
  47. class Database {
  48. var $write; // Writable database
  49. var $read; // Read-only database
  50. /**
  51. * Connect to databases
  52. */
  53. function Database($write_config=array(), $read_config=array()) {
  54. if (empty($write_config)) {
  55. $write_config = array(
  56. 'host' => DB_HOST,
  57. 'user' => DB_USER,
  58. 'pass' => DB_PASS,
  59. 'name' => DB_NAME
  60. );
  61. }
  62. if (empty($read_config)) {
  63. $read_config = array(
  64. 'host' => SHADOW_DB_HOST,
  65. 'user' => SHADOW_DB_USER,
  66. 'pass' => SHADOW_DB_PASS,
  67. 'name' => SHADOW_DB_NAME
  68. );
  69. }
  70. $this->connectWrite($write_config['host'], $write_config['user'], $write_config['pass'], $write_config['name']);
  71. $this->connectRead($read_config['host'], $read_config['user'], $read_config['pass'], $read_config['name']);
  72. }
  73. /**
  74. * Connects to read-only database
  75. */
  76. function connectRead($host, $username, $password, $database) {
  77. $this->read = mysql_connect($host, $username, $password) or die('Could not connect: '.mysql_error());
  78. mysql_select_db($database, $this->read) or die("Could not select read-only database {$database}");
  79. }
  80. /**
  81. * Connects to writable database
  82. */
  83. function connectWrite($host, $username, $password, $database) {
  84. $this->write = mysql_connect($host, $username, $password) or die('Could not connect: '.mysql_error());
  85. mysql_select_db($database, $this->write) or die("Could not select writable database {$database}");
  86. }
  87. /**
  88. * Performs query using read-only database by default
  89. *
  90. * @param str $qry the query to execute
  91. * @param bool $useWrite whether to use the writable database
  92. */
  93. function query($qry, $useWrite = false) {
  94. if (!$result = mysql_query($qry, ($useWrite ? $this->write : $this->read))) {
  95. trigger_error('MySQL Error '.mysql_errno().': '.mysql_error()."\nQuery was: [".$qry.']', E_USER_NOTICE);
  96. return false;
  97. }
  98. return $result;
  99. }
  100. /**
  101. * Sets the stats_updating config value to prevent the Stats Dashboard from
  102. * showing inaccurate data while a script is running.
  103. */
  104. function lockStats() {
  105. if ($this->query("UPDATE `config` SET `value`='1' WHERE `key`='stats_updating'", true))
  106. return true;
  107. else
  108. return false;
  109. }
  110. /**
  111. * Rests the stats_updating config value to make the Stats Dashboard function
  112. * again
  113. */
  114. function unlockStats() {
  115. if ($this->query("UPDATE `config` SET `value`='0' WHERE `key`='stats_updating'", true))
  116. return true;
  117. else
  118. return false;
  119. }
  120. /**
  121. * Close database connections
  122. */
  123. function close() {
  124. mysql_close($this->write);
  125. mysql_close($this->read);
  126. }
  127. }
  128. ?>