Localisation updates from https://translatewiki.net.
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderEditToolbarModule.php
1 <?php
2 /**
3 * Resource loader module for the edit toolbar.
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 */
22
23 /**
24 * ResourceLoader module for the edit toolbar.
25 *
26 * @since 1.24
27 */
28 class ResourceLoaderEditToolbarModule extends ResourceLoaderFileModule {
29 /**
30 * Serialize a string (escape and quote) for use as a CSS string value.
31 * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
32 *
33 * @param string $value
34 * @return string
35 */
36 private static function cssSerializeString( $value ) {
37 if ( strstr( $value, "\0" ) ) {
38 throw new Exception( "Invalid character in CSS string" );
39 }
40 $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
41 $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
42 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
43 }, $value );
44 return '"' . $value . '"';
45 }
46
47 /**
48 * Get language-specific LESS variables for this module.
49 *
50 * @return array
51 */
52 private function getLessVars( ResourceLoaderContext $context ) {
53 $language = Language::factory( $context->getLanguage() );
54
55 // This is very conveniently formatted and we can pass it right through
56 $vars = $language->getImageFiles();
57
58 // lessc tries to be helpful and parse our variables as LESS source code
59 foreach ( $vars as $key => &$value ) {
60 $value = self::cssSerializeString( $value );
61 }
62
63 return $vars;
64 }
65
66 /**
67 * @param ResourceLoaderContext $context
68 * @return int UNIX timestamp
69 */
70 public function getModifiedTime( ResourceLoaderContext $context ) {
71 return max(
72 parent::getModifiedTime( $context ),
73 $this->getHashMtime( $context )
74 );
75 }
76
77 /**
78 * @param ResourceLoaderContext $context
79 * @return string Hash
80 */
81 public function getModifiedHash( ResourceLoaderContext $context ) {
82 return md5(
83 parent::getModifiedHash( $context ) .
84 serialize( $this->getLessVars( $context ) )
85 );
86 }
87
88 /**
89 * Get a LESS compiler instance for this module.
90 *
91 * Set our variables in it.
92 *
93 * @throws MWException
94 * @param ResourceLoaderContext $context
95 * @return lessc
96 */
97 protected function getLessCompiler( ResourceLoaderContext $context = null ) {
98 $compiler = parent::getLessCompiler();
99 $compiler->setVariables( $this->getLessVars( $context ) );
100 return $compiler;
101 }
102 }