Use interwiki cache directly to resolve transwiki import sources
[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 * @throws Exception
36 */
37 private static function cssSerializeString( $value ) {
38 if ( strstr( $value, "\0" ) ) {
39 throw new Exception( "Invalid character in CSS string" );
40 }
41 $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
42 $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
43 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
44 }, $value );
45 return '"' . $value . '"';
46 }
47
48 /**
49 * Get language-specific LESS variables for this module.
50 *
51 * @return array
52 */
53 private function getLessVars( ResourceLoaderContext $context ) {
54 $language = Language::factory( $context->getLanguage() );
55
56 // This is very conveniently formatted and we can pass it right through
57 $vars = $language->getImageFiles();
58
59 // lessc tries to be helpful and parse our variables as LESS source code
60 foreach ( $vars as $key => &$value ) {
61 $value = self::cssSerializeString( $value );
62 }
63
64 return $vars;
65 }
66
67 /**
68 * @return bool
69 */
70 public function enableModuleContentVersion() {
71 return true;
72 }
73
74 /**
75 * Get a LESS compiler instance for this module.
76 *
77 * Set our variables in it.
78 *
79 * @throws MWException
80 * @param ResourceLoaderContext $context
81 * @return lessc
82 */
83 protected function getLessCompiler( ResourceLoaderContext $context = null ) {
84 $compiler = parent::getLessCompiler();
85 $compiler->setVariables( $this->getLessVars( $context ) );
86 return $compiler;
87 }
88 }