Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / resourceloader / ResourceLoaderEditToolbarModule.php
blobd79174cd74d6a1e54bf1b8de20d1c15e45313ef1
1 <?php
2 /**
3 * Resource loader module for the edit toolbar.
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.
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.
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
20 * @file
23 /**
24 * ResourceLoader module for the edit toolbar.
26 * @since 1.24
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
33 * @param string $value
34 * @return string
35 * @throws Exception
37 private static function cssSerializeString( $value ) {
38 if ( strstr( $value, "\0" ) ) {
39 throw new Exception( "Invalid character in CSS string" );
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 . '"';
48 /**
49 * Get language-specific LESS variables for this module.
51 * @return array
53 private function getLessVars( ResourceLoaderContext $context ) {
54 $language = Language::factory( $context->getLanguage() );
56 // This is very conveniently formatted and we can pass it right through
57 $vars = $language->getImageFiles();
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 );
64 return $vars;
67 /**
68 * @param ResourceLoaderContext $context
69 * @return int UNIX timestamp
71 public function getModifiedTime( ResourceLoaderContext $context ) {
72 return max(
73 parent::getModifiedTime( $context ),
74 $this->getHashMtime( $context )
78 /**
79 * @param ResourceLoaderContext $context
80 * @return string Hash
82 public function getModifiedHash( ResourceLoaderContext $context ) {
83 return md5(
84 parent::getModifiedHash( $context ) .
85 serialize( $this->getLessVars( $context ) )
89 /**
90 * Get a LESS compiler instance for this module.
92 * Set our variables in it.
94 * @throws MWException
95 * @param ResourceLoaderContext $context
96 * @return lessc
98 protected function getLessCompiler( ResourceLoaderContext $context = null ) {
99 $compiler = parent::getLessCompiler();
100 $compiler->setVariables( $this->getLessVars( $context ) );
101 return $compiler;