Merge "ResourceLoaderLanguageDataModule: clean up"
[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 /**
31 * Get the grammer forms for the site content language.
32 *
33 * @return array
34 */
35 protected function getSiteLangGrammarForms() {
36 global $wgContLang;
37 return $wgContLang->getGrammarForms();
38 }
39
40 /**
41 * @param $context ResourceLoaderContext
42 * @return string: Javascript code
43 */
44 public function getScript( ResourceLoaderContext $context ) {
45 global $wgContLang;
46
47 return Xml::encodeJsCall( 'mw.language.setData', array(
48 $wgContLang->getCode(),
49 array( 'grammarForms' => $this->getSiteLangGrammarForms() )
50 ) );
51 }
52
53 /**
54 * @param $context ResourceLoaderContext
55 * @return array|int|Mixed
56 */
57 public function getModifiedTime( ResourceLoaderContext $context ) {
58 $cache = wfGetCache( CACHE_ANYTHING );
59 $key = wfMemcKey( 'resourceloader', 'langdatamodule', 'changeinfo' );
60
61 $forms = $this->getSiteLangGrammarForms();
62 $hash = md5( serialize( $forms ) );
63
64 $result = $cache->get( $key );
65 if ( is_array( $result ) && $result['hash'] === $hash ) {
66 return $result['timestamp'];
67 }
68 $timestamp = wfTimestamp();
69 $cache->set( $key, array(
70 'hash' => $hash,
71 'timestamp' => $timestamp,
72 ) );
73 return $timestamp;
74 }
75
76 /**
77 * @return array
78 */
79 public function getDependencies() {
80 return array( 'mediawiki.language.init' );
81 }
82 }