ReadOnlyMode.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use Wikimedia\Rdbms\LoadBalancer;
  3. /**
  4. * A service class for fetching the wiki's current read-only mode.
  5. * To obtain an instance, use MediaWikiServices::getReadOnlyMode().
  6. *
  7. * @since 1.29
  8. */
  9. class ReadOnlyMode {
  10. /** @var ConfiguredReadOnlyMode */
  11. private $configuredReadOnly;
  12. /** @var LoadBalancer */
  13. private $loadBalancer;
  14. public function __construct( ConfiguredReadOnlyMode $cro, LoadBalancer $loadBalancer ) {
  15. $this->configuredReadOnly = $cro;
  16. $this->loadBalancer = $loadBalancer;
  17. }
  18. /**
  19. * Check whether the wiki is in read-only mode.
  20. *
  21. * @return bool
  22. */
  23. public function isReadOnly() {
  24. return $this->getReason() !== false;
  25. }
  26. /**
  27. * Check if the site is in read-only mode and return the message if so
  28. *
  29. * This checks the configuration and registered DB load balancers for
  30. * read-only mode. This may result in DB connection being made.
  31. *
  32. * @return string|bool String when in read-only mode; false otherwise
  33. */
  34. public function getReason() {
  35. $reason = $this->configuredReadOnly->getReason();
  36. if ( $reason !== false ) {
  37. return $reason;
  38. }
  39. $reason = $this->loadBalancer->getReadOnlyReason();
  40. if ( $reason !== false && $reason !== null ) {
  41. return $reason;
  42. }
  43. return false;
  44. }
  45. /**
  46. * Set the read-only mode, which will apply for the remainder of the
  47. * request or until a service reset.
  48. *
  49. * @param string|null $msg
  50. */
  51. public function setReason( $msg ) {
  52. $this->configuredReadOnly->setReason( $msg );
  53. }
  54. /**
  55. * Clear the cache of the read only file
  56. */
  57. public function clearCache() {
  58. $this->configuredReadOnly->clearCache();
  59. }
  60. }