]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/MinifyPlugin.php
Merge commit 'refs/merge-requests/157' of git://gitorious.org/statusnet/mainline...
[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     function onStartInitializeRouter($m)
55     {
56         $m->connect('main/min',
57                     array('action' => 'minify'));
58         return true;
59     }
60
61     function onAutoload($cls)
62     {
63         switch ($cls)
64         {
65          case 'MinifyAction':
66             require_once(INSTALLDIR.'/plugins/Minify/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
67             return false;
68          default:
69             return true;
70         }
71     }
72
73     function onLoginAction($action, &$login)
74     {
75         switch ($action)
76         {
77          case 'minify':
78             $login = true;
79             return false;
80          default:
81             return true;
82         }
83     }
84
85     function onStartScriptElement($action,&$src,&$type) {
86         $url = parse_url($src);
87         if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
88         {
89             if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
90                 $src = $this->minifyUrl($src);
91             } else {
92                 $src = $this->minifyUrl('js/'.$src);
93             }
94         }
95     }
96
97     function onStartCssLinkElement($action,&$src,&$theme,&$media) {
98         $allowThemeMinification =
99             is_null(common_config('theme', 'dir'))
100             && is_null(common_config('theme', 'path'))
101             && is_null(common_config('theme', 'server'));
102         $url = parse_url($src);
103         if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
104         {
105             if(!isset($theme)) {
106                 $theme = common_config('site', 'theme');
107             }
108             if($allowThemeMinification && file_exists(INSTALLDIR.'/local/theme/'.$theme.'/'.$src)) {
109                 $src = $this->minifyUrl('local/theme/'.$theme.'/'.$src);
110             } else if($allowThemeMinification && file_exists(INSTALLDIR.'/theme/'.$theme.'/'.$src)) {
111                 $src = $this->minifyUrl('theme/'.$theme.'/'.$src);
112             }else if(file_exists(INSTALLDIR.'/'.$src)){
113                 $src = $this->minifyUrl($src);
114             }
115         }
116     }
117
118     function onStartInlineScriptElement($action,&$code,&$type)
119     {
120         if($this->minifyInlineJs && $type=='text/javascript'){
121             $c = Cache::instance();
122             if (!empty($c)) {
123                 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
124                 $out = $c->get($cacheKey);
125             }
126             if(empty($out)) {
127                 $out = $this->minifyJs($code);
128             }
129             if (!empty($c)) {
130                 $c->set($cacheKey, $out);
131             }
132             if(!empty($out)) {
133                 $code = $out;
134             }
135         }
136     }
137
138     function onStartStyleElement($action,&$code,&$type,&$media)
139     {
140         if($this->minifyInlineCss && $type=='text/css'){
141             $c = Cache::instance();
142             if (!empty($c)) {
143                 $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
144                 $out = $c->get($cacheKey);
145             }
146             if(empty($out)) {
147                 $out = $this->minifyCss($code);
148             }
149             if (!empty($c)) {
150                 $c->set($cacheKey, $out);
151             }
152             if(!empty($out)) {
153                 $code = $out;
154             }
155         }
156     }
157
158     function minifyUrl($src) {
159         return common_local_url('minify',null,array('f' => $src ,v => STATUSNET_VERSION));
160     }
161
162     static function minifyJs($code) {
163         require_once('JSMin.php');
164         return JSMin::minify($code);
165     }
166
167     static function minifyCss($code, $options = array()) {
168         require_once('Minify/CSS.php');
169         return Minify_CSS::minify($code,$options);
170     }
171
172     function onPluginVersion(&$versions)
173     {
174         $versions[] = array('name' => 'Minify',
175                             'version' => STATUSNET_VERSION,
176                             'author' => 'Craig Andrews',
177                             'homepage' => 'http://status.net/wiki/Plugin:Minify',
178                             'rawdescription' =>
179                             // TRANS: Plugin description.
180                             _m('The Minify plugin minifies StatusNet\'s CSS and JavaScript, removing whitespace and comments.'));
181         return true;
182     }
183 }