ConfiguredReadOnlyMode.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * A read-only mode service which does not depend on LoadBalancer.
  4. * To obtain an instance, use MediaWikiServices::getConfiguredReadOnlyMode().
  5. *
  6. * @since 1.29
  7. */
  8. class ConfiguredReadOnlyMode {
  9. /** @var Config */
  10. private $config;
  11. /** @var string|bool|null */
  12. private $fileReason;
  13. /** @var string|null */
  14. private $overrideReason;
  15. public function __construct( Config $config ) {
  16. $this->config = $config;
  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. * Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
  28. *
  29. * @return string|bool String when in read-only mode; false otherwise
  30. */
  31. public function getReason() {
  32. if ( $this->overrideReason !== null ) {
  33. return $this->overrideReason;
  34. }
  35. $confReason = $this->config->get( 'ReadOnly' );
  36. if ( $confReason !== null ) {
  37. return $confReason;
  38. }
  39. if ( $this->fileReason === null ) {
  40. // Cache for faster access next time
  41. $readOnlyFile = $this->config->get( 'ReadOnlyFile' );
  42. if ( is_file( $readOnlyFile ) && filesize( $readOnlyFile ) > 0 ) {
  43. $this->fileReason = file_get_contents( $readOnlyFile );
  44. } else {
  45. $this->fileReason = false;
  46. }
  47. }
  48. return $this->fileReason;
  49. }
  50. /**
  51. * Set the read-only mode, which will apply for the remainder of the
  52. * request or until a service reset.
  53. *
  54. * @param string|null $msg
  55. */
  56. public function setReason( $msg ) {
  57. $this->overrideReason = $msg;
  58. }
  59. /**
  60. * Clear the cache of the read only file
  61. */
  62. public function clearCache() {
  63. $this->fileReason = null;
  64. }
  65. }