WebInstallerExistingWiki.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Deployment
  20. */
  21. class WebInstallerExistingWiki extends WebInstallerPage {
  22. /**
  23. * @return string
  24. */
  25. public function execute() {
  26. // If there is no LocalSettings.php, continue to the installer welcome page
  27. $vars = Installer::getExistingLocalSettings();
  28. if ( !$vars ) {
  29. return 'skip';
  30. }
  31. // Check if the upgrade key supplied to the user has appeared in LocalSettings.php
  32. if ( $vars['wgUpgradeKey'] !== false
  33. && $this->getVar( '_UpgradeKeySupplied' )
  34. && $this->getVar( 'wgUpgradeKey' ) === $vars['wgUpgradeKey']
  35. ) {
  36. // It's there, so the user is authorized
  37. $status = $this->handleExistingUpgrade( $vars );
  38. if ( $status->isOK() ) {
  39. return 'skip';
  40. } else {
  41. $this->startForm();
  42. $this->parent->showStatusBox( $status );
  43. $this->endForm( 'continue' );
  44. return 'output';
  45. }
  46. }
  47. // If there is no $wgUpgradeKey, tell the user to add one to LocalSettings.php
  48. if ( $vars['wgUpgradeKey'] === false ) {
  49. if ( $this->getVar( 'wgUpgradeKey', false ) === false ) {
  50. $secretKey = $this->getVar( 'wgSecretKey' ); // preserve $wgSecretKey
  51. $this->parent->generateKeys();
  52. $this->setVar( 'wgSecretKey', $secretKey );
  53. $this->setVar( '_UpgradeKeySupplied', true );
  54. }
  55. $this->startForm();
  56. $this->addHTML( $this->parent->getInfoBox(
  57. wfMessage( 'config-upgrade-key-missing', "<pre dir=\"ltr\">\$wgUpgradeKey = '" .
  58. $this->getVar( 'wgUpgradeKey' ) . "';</pre>" )->plain()
  59. ) );
  60. $this->endForm( 'continue' );
  61. return 'output';
  62. }
  63. // If there is an upgrade key, but it wasn't supplied, prompt the user to enter it
  64. $r = $this->parent->request;
  65. if ( $r->wasPosted() ) {
  66. $key = $r->getText( 'config_wgUpgradeKey' );
  67. if ( !$key || $key !== $vars['wgUpgradeKey'] ) {
  68. $this->parent->showError( 'config-localsettings-badkey' );
  69. $this->showKeyForm();
  70. return 'output';
  71. }
  72. // Key was OK
  73. $status = $this->handleExistingUpgrade( $vars );
  74. if ( $status->isOK() ) {
  75. return 'continue';
  76. } else {
  77. $this->parent->showStatusBox( $status );
  78. $this->showKeyForm();
  79. return 'output';
  80. }
  81. } else {
  82. $this->showKeyForm();
  83. return 'output';
  84. }
  85. }
  86. /**
  87. * Show the "enter key" form
  88. */
  89. protected function showKeyForm() {
  90. $this->startForm();
  91. $this->addHTML(
  92. $this->parent->getInfoBox( wfMessage( 'config-localsettings-upgrade' )->plain() ) .
  93. '<br />' .
  94. $this->parent->getTextBox( [
  95. 'var' => 'wgUpgradeKey',
  96. 'label' => 'config-localsettings-key',
  97. 'attribs' => [ 'autocomplete' => 'off' ],
  98. ] )
  99. );
  100. $this->endForm( 'continue' );
  101. }
  102. /**
  103. * @param string[] $names
  104. * @param mixed[] $vars
  105. *
  106. * @return Status
  107. */
  108. protected function importVariables( $names, $vars ) {
  109. $status = Status::newGood();
  110. foreach ( $names as $name ) {
  111. if ( !isset( $vars[$name] ) ) {
  112. $status->fatal( 'config-localsettings-incomplete', $name );
  113. }
  114. $this->setVar( $name, $vars[$name] );
  115. }
  116. return $status;
  117. }
  118. /**
  119. * Initiate an upgrade of the existing database
  120. *
  121. * @param mixed[] $vars Variables from LocalSettings.php
  122. *
  123. * @return Status
  124. */
  125. protected function handleExistingUpgrade( $vars ) {
  126. // Check $wgDBtype
  127. if ( !isset( $vars['wgDBtype'] ) ||
  128. !in_array( $vars['wgDBtype'], Installer::getDBTypes() )
  129. ) {
  130. return Status::newFatal( 'config-localsettings-connection-error', '' );
  131. }
  132. // Set the relevant variables from LocalSettings.php
  133. $requiredVars = [ 'wgDBtype' ];
  134. $status = $this->importVariables( $requiredVars, $vars );
  135. $installer = $this->parent->getDBInstaller();
  136. $status->merge( $this->importVariables( $installer->getGlobalNames(), $vars ) );
  137. if ( !$status->isOK() ) {
  138. return $status;
  139. }
  140. $this->setVar( '_InstallUser', $vars['wgDBadminuser'] ?? $vars['wgDBuser'] );
  141. $this->setVar( '_InstallPassword', $vars['wgDBadminpassword'] ?? $vars['wgDBpassword'] );
  142. // Test the database connection
  143. $status = $installer->getConnection();
  144. if ( !$status->isOK() ) {
  145. // Adjust the error message to explain things correctly
  146. $status->replaceMessage( 'config-connection-error',
  147. 'config-localsettings-connection-error' );
  148. return $status;
  149. }
  150. // All good
  151. $this->setVar( '_ExistingDBSettings', true );
  152. // Copy $wgAuthenticationTokenVersion too, if it exists
  153. $this->setVar( 'wgAuthenticationTokenVersion',
  154. $vars['wgAuthenticationTokenVersion'] ?? null
  155. );
  156. return $status;
  157. }
  158. }