]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/MinifyPlugin.php
afe6edf5fd73f56ffd218d1ee4f5580289caa7b0
[quix0rs-gnu-social.git] / plugins / Minify / MinifyPlugin.php
1 <?php
2 /*
3 StatusNet Plugin: 0.9
4 Plugin Name: Minify
5 Description: Minifies resources (Javascript and CSS)
6 Version: 0.1
7 Author: Craig Andrews <candrews@integralblue.com>
8 Author URI: http://candrews.integralblue.com/
9 */
10
11 /*
12  * StatusNet - the distributed open-source microblogging tool
13  * Copyright (C) 2009, StatusNet, Inc.
14  *
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.
19  *
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.
24  *
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/>.
27  */
28
29 /**
30  * @package MinifyPlugin
31  * @maintainer Craig Andrews <candrews@integralblue.com>
32  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33  */
34
35 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
36
37 // We bundle the minify library...
38 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
39
40 class MinifyPlugin extends Plugin
41 {
42     private $minifyInlineJs = true;
43     private $minifyInlineCss = true;
44
45     const cacheKey = 'minify';
46
47     /**
48      * Add Minification related paths to the router table
49      *
50      * Hook for RouterInitialized event.
51      *
52      * @return boolean hook return
53      */
54
55     function onStartInitializeRouter($m)
56     {
57         $m->connect('main/min',
58                     array('action' => 'minify'));
59         return true;
60     }
61
62     function onAutoload($cls)
63     {
64         switch ($cls)
65         {
66          case 'MinifyAction':
67             require_once(INSTALLDIR.'/plugins/Minify/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
68             return false;
69          default:
70             return true;
71         }
72     }
73
74     function onLoginAction($action, &$login)
75     {
76         switch ($action)
77         {
78          case 'minify':
79             $login = true;
80             return false;
81          default:
82             return true;
83         }
84     }
85
86     function onStartScriptElement($action,&$src,&$type) {
87         $url = parse_url($src);
88         if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
89         {
90             if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
91                 $src = $this->minifyUrl($src);
92             } else {
93                 $src = $this->minifyUrl('js/'.$src);
94             }
95         }
96     }
97
98     function onStartCssLinkElement($action,&$src,&$theme,&$media) {
99         $allowThemeMinification =
100             is_null(common_config('theme', 'dir'))
101             && is_null(common_config('theme', 'path'))
102             && is_null(common_config('theme', 'server'));
103         $url = parse_url($src);
104         if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
105         {
106             if(!isset($theme)) {
107                 $theme = common_config('site', 'theme');
108             }
109             if($allowThemeMinification && file_exists(INSTALLDIR.'/local/theme/'.$theme.'/'.$src)) {
110                 $src = $this->minifyUrl('local/theme/'.$theme.'/'.$src);
111             } else if($allowThemeMinification && file_exists(INSTALLDIR.'/theme/'.$theme.'/'.$src)) {
112                 $src = $this->minifyUrl('theme/'.$theme.'/'.$src);
113             }else if(file_exists(INSTALLDIR.'/'.$src)){
114                 $src = $this->minifyUrl($src);
115             }
116         }
117     }
118
119     function onStartInlineScriptElement($action,&$code,&$type)
120     {
121         if($this->minifyInlineJs && $type=='text/javascript'){
122             $c = Cache::instance();
123             if (!empty($c)) {
124                 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
125                 $out = $c->get($cacheKey);
126             }
127             if(empty($out)) {
128                 $out = $this->minifyJs($code);
129             }
130             if (!empty($c)) {
131                 $c->set($cacheKey, $out);
132             }
133             if(!empty($out)) {
134                 $code = $out;
135             }
136         }
137     }
138
139     function onStartStyleElement($action,&$code,&$type,&$media)
140     {
141         if($this->minifyInlineCss && $type=='text/css'){
142             $c = Cache::instance();
143             if (!empty($c)) {
144                 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
145                 $out = $c->get($cacheKey);
146             }
147             if(empty($out)) {
148                 $out = $this->minifyCss($code);
149             }
150             if (!empty($c)) {
151                 $c->set($cacheKey, $out);
152             }
153             if(!empty($out)) {
154                 $code = $out;
155             }
156         }
157     }
158
159     function minifyUrl($src) {
160         return common_local_url('minify',null,array('f' => $src ,v => STATUSNET_VERSION));
161     }
162
163     static function minifyJs($code) {
164         require_once('JSMin.php');
165         return JSMin::minify($code);
166     }
167
168     static function minifyCss($code, $options = array()) {
169         require_once('Minify/CSS.php');
170         return Minify_CSS::minify($code,$options);
171     }
172
173     function onPluginVersion(&$versions)
174     {
175         $versions[] = array('name' => 'Minify',
176                             'version' => STATUSNET_VERSION,
177                             'author' => 'Craig Andrews',
178                             'homepage' => 'http://status.net/wiki/Plugin:Minify',
179                             'rawdescription' =>
180                             _m('The Minify plugin minifies your CSS and Javascript, removing whitespace and comments.'));
181         return true;
182     }
183 }
184