Merge "Add Special:Login and Special:Logout as aliases."
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderLanguageDataModule.php
1 <?php
2 /**
3 * Resource loader module for populating language specific data.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Santhosh Thottingal
22 * @author Timo Tijhof
23 */
24
25 /**
26 * ResourceLoader module for populating language specific data.
27 */
28 class ResourceLoaderLanguageDataModule extends ResourceLoaderModule {
29
30 protected $language;
31 protected $targets = array( 'desktop', 'mobile' );
32 /**
33 * Get the grammar forms for the site content language.
34 *
35 * @return array
36 */
37 protected function getSiteLangGrammarForms() {
38 return $this->language->getGrammarForms();
39 }
40
41 /**
42 * Get the plural forms for the site content language.
43 *
44 * @return array
45 */
46 protected function getPluralRules() {
47 return $this->language->getPluralRules();
48 }
49
50 /**
51 * Get the digit groupin Pattern for the site content language.
52 *
53 * @return array
54 */
55 protected function getDigitGroupingPattern() {
56 return $this->language->digitGroupingPattern();
57 }
58
59 /**
60 * Get the digit transform table for the content language
61 *
62 * @return array
63 */
64 protected function getDigitTransformTable() {
65 return $this->language->digitTransformTable();
66 }
67
68 /**
69 * Get seperator transform table required for converting
70 * the . and , sign to appropriate forms in site content language.
71 *
72 * @return array
73 */
74 protected function getSeparatorTransformTable() {
75 return $this->language->separatorTransformTable();
76 }
77
78
79 /**
80 * Get all the dynamic data for the content language to an array
81 *
82 * @return array
83 */
84 protected function getData() {
85 return array(
86 'digitTransformTable' => $this->getDigitTransformTable(),
87 'separatorTransformTable' => $this->getSeparatorTransformTable(),
88 'grammarForms' => $this->getSiteLangGrammarForms(),
89 'pluralRules' => $this->getPluralRules(),
90 'digitGroupingPattern' => $this->getDigitGroupingPattern(),
91 );
92 }
93
94 /**
95 * @param $context ResourceLoaderContext
96 * @return string: JavaScript code
97 */
98 public function getScript( ResourceLoaderContext $context ) {
99 $this->language = Language::factory( $context->getLanguage() );
100 return Xml::encodeJsCall( 'mw.language.setData', array(
101 $this->language->getCode(),
102 $this->getData()
103 ) );
104 }
105
106 /**
107 * @param $context ResourceLoaderContext
108 * @return array|int|Mixed
109 */
110 public function getModifiedTime( ResourceLoaderContext $context ) {
111 $this->language = Language::factory( $context->getLanguage() );
112 $cache = wfGetCache( CACHE_ANYTHING );
113 $key = wfMemcKey( 'resourceloader', 'langdatamodule', 'changeinfo' );
114
115 $data = $this->getData();
116 $hash = md5( serialize( $data ) );
117
118 $result = $cache->get( $key );
119 if ( is_array( $result ) && $result['hash'] === $hash ) {
120 return $result['timestamp'];
121 }
122 $timestamp = wfTimestamp();
123 $cache->set( $key, array(
124 'hash' => $hash,
125 'timestamp' => $timestamp,
126 ) );
127 return $timestamp;
128 }
129
130 /**
131 * @return array
132 */
133 public function getDependencies() {
134 return array( 'mediawiki.language.init' );
135 }
136 }