5 Description: Minifies resources (Javascript and CSS)
7 Author: Craig Andrews <candrews@integralblue.com>
8 Author URI: http://candrews.integralblue.com/
12 * StatusNet - the distributed open-source microblogging tool
13 * Copyright (C) 2009, StatusNet, Inc.
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Affero General Public License for more details.
25 * You should have received a copy of the GNU Affero General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * @package MinifyPlugin
31 * @maintainer Craig Andrews <candrews@integralblue.com>
32 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
35 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
37 // We bundle the minify library...
38 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
40 class MinifyPlugin extends Plugin
42 private $minifyInlineJs = true;
43 private $minifyInlineCss = true;
45 const cacheKey = 'minify';
48 * Add Minification related paths to the router table
50 * Hook for RouterInitialized event.
52 * @return boolean hook return
54 function onStartInitializeRouter($m)
56 $m->connect('main/min',
57 array('action' => 'minify'));
61 function onLoginAction($action, &$login)
73 function onStartScriptElement($action,&$src,&$type) {
74 $url = parse_url($src);
75 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
77 if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
78 $src = $this->minifyUrl($src);
80 $src = $this->minifyUrl('js/'.$src);
85 function onStartCssLinkElement($action,&$src,&$theme,&$media) {
86 $allowThemeMinification =
87 is_null(common_config('theme', 'dir'))
88 && is_null(common_config('theme', 'path'))
89 && is_null(common_config('theme', 'server'));
90 $url = parse_url($src);
91 if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
94 $theme = common_config('site', 'theme');
96 if($allowThemeMinification && file_exists(INSTALLDIR.'/local/theme/'.$theme.'/'.$src)) {
97 $src = $this->minifyUrl('local/theme/'.$theme.'/'.$src);
98 } else if($allowThemeMinification && file_exists(INSTALLDIR.'/theme/'.$theme.'/'.$src)) {
99 $src = $this->minifyUrl('theme/'.$theme.'/'.$src);
100 }else if(file_exists(INSTALLDIR.'/'.$src)){
101 $src = $this->minifyUrl($src);
106 function onStartInlineScriptElement($action,&$code,&$type)
108 if($this->minifyInlineJs && $type=='text/javascript'){
109 $c = Cache::instance();
111 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
112 $out = $c->get($cacheKey);
115 $out = $this->minifyJs($code);
118 $c->set($cacheKey, $out);
126 function onStartStyleElement($action,&$code,&$type,&$media)
128 if($this->minifyInlineCss && $type=='text/css'){
129 $c = Cache::instance();
131 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
132 $out = $c->get($cacheKey);
135 $out = $this->minifyCss($code);
138 $c->set($cacheKey, $out);
146 function minifyUrl($src) {
147 return common_local_url('minify',null,array('f' => $src ,v => GNUSOCIAL_VERSION));
150 static function minifyJs($code) {
151 require_once('JSMin.php');
152 return JSMin::minify($code);
155 static function minifyCss($code, $options = array()) {
156 require_once('Minify/CSS.php');
157 return Minify_CSS::minify($code,$options);
160 function onPluginVersion(array &$versions)
162 $versions[] = array('name' => 'Minify',
163 'version' => GNUSOCIAL_VERSION,
164 'author' => 'Craig Andrews',
165 'homepage' => 'http://status.net/wiki/Plugin:Minify',
167 // TRANS: Plugin description.
168 _m('The Minify plugin minifies StatusNet\'s CSS and JavaScript, removing whitespace and comments.'));