PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/installer/WebInstallerLanguage.php

https://bitbucket.org/andersus/querytalogo
PHP | 123 lines | 72 code | 14 blank | 37 comment | 11 complexity | 08ae0ccab2e1648a22e578f41781e918 MD5 | raw file
Possible License(s): LGPL-3.0, MPL-2.0-no-copyleft-exception, JSON, MIT, CC0-1.0, BSD-3-Clause, Apache-2.0, BSD-2-Clause, LGPL-2.1, GPL-2.0
  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();
  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. global $wgExtraLanguageCodes;
  93. $output = $helpHtml;
  94. $select = new XmlSelect( $name, $name, $selectedCode );
  95. $select->setAttribute( 'tabindex', $this->parent->nextTabIndex() );
  96. $unwantedLanguageCodes = $wgExtraLanguageCodes +
  97. LanguageCode::getDeprecatedCodeMapping();
  98. $languages = Language::fetchLanguageNames();
  99. ksort( $languages );
  100. foreach ( $languages as $code => $lang ) {
  101. if ( isset( $unwantedLanguageCodes[$code] ) ) {
  102. continue;
  103. }
  104. $select->addOption( "$code - $lang", $code );
  105. }
  106. $output .= $select->getHTML();
  107. return $this->parent->label( $label, $name, $output );
  108. }
  109. }