WebInstallerLanguage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 WebInstallerLanguage extends WebInstallerPage {
  22. /**
  23. * @return string|null
  24. */
  25. public function execute() {
  26. global $wgLang;
  27. $r = $this->parent->request;
  28. $userLang = $r->getVal( 'uselang' );
  29. $contLang = $r->getVal( 'ContLang' );
  30. $languages = Language::fetchLanguageNames( null, 'mwfile' );
  31. $lifetime = intval( ini_get( 'session.gc_maxlifetime' ) );
  32. if ( !$lifetime ) {
  33. $lifetime = 1440; // PHP default
  34. }
  35. if ( $r->wasPosted() ) {
  36. # Do session test
  37. if ( $this->parent->getSession( 'test' ) === null ) {
  38. $requestTime = $r->getVal( 'LanguageRequestTime' );
  39. if ( !$requestTime ) {
  40. // The most likely explanation is that the user was knocked back
  41. // from another page on POST due to session expiry
  42. $msg = 'config-session-expired';
  43. } elseif ( time() - $requestTime > $lifetime ) {
  44. $msg = 'config-session-expired';
  45. } else {
  46. $msg = 'config-no-session';
  47. }
  48. $this->parent->showError( $msg, $wgLang->formatTimePeriod( $lifetime ) );
  49. } else {
  50. if ( isset( $languages[$userLang] ) ) {
  51. $this->setVar( '_UserLang', $userLang );
  52. }
  53. if ( isset( $languages[$contLang] ) ) {
  54. $this->setVar( 'wgLanguageCode', $contLang );
  55. }
  56. return 'continue';
  57. }
  58. } elseif ( $this->parent->showSessionWarning ) {
  59. # The user was knocked back from another page to the start
  60. # This probably indicates a session expiry
  61. $this->parent->showError( 'config-session-expired',
  62. $wgLang->formatTimePeriod( $lifetime ) );
  63. }
  64. $this->parent->setSession( 'test', true );
  65. if ( !isset( $languages[$userLang] ) ) {
  66. $userLang = $this->getVar( '_UserLang', 'en' );
  67. }
  68. if ( !isset( $languages[$contLang] ) ) {
  69. $contLang = $this->getVar( 'wgLanguageCode', 'en' );
  70. }
  71. $this->startForm();
  72. $s = Html::hidden( 'LanguageRequestTime', time() ) .
  73. $this->getLanguageSelector( 'uselang', 'config-your-language', $userLang,
  74. $this->parent->getHelpBox( 'config-your-language-help' ) ) .
  75. $this->getLanguageSelector( 'ContLang', 'config-wiki-language', $contLang,
  76. $this->parent->getHelpBox( 'config-wiki-language-help' ) );
  77. $this->addHTML( $s );
  78. $this->endForm( 'continue', false );
  79. return null;
  80. }
  81. /**
  82. * Get a "<select>" for selecting languages.
  83. *
  84. * @param string $name
  85. * @param string $label
  86. * @param string $selectedCode
  87. * @param string $helpHtml
  88. *
  89. * @return string
  90. */
  91. public function getLanguageSelector( $name, $label, $selectedCode, $helpHtml = '' ) {
  92. $output = $helpHtml;
  93. $select = new XmlSelect( $name, $name, $selectedCode );
  94. $select->setAttribute( 'tabindex', $this->parent->nextTabIndex() );
  95. $languages = Language::fetchLanguageNames( null, 'mwfile' );
  96. foreach ( $languages as $code => $lang ) {
  97. $select->addOption( "$code - $lang", $code );
  98. }
  99. $output .= $select->getHTML();
  100. return $this->parent->label( $label, $name, $output );
  101. }
  102. }