MinifyPlugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * StatusNet - the distributed open-source microblogging tool
  12. * Copyright (C) 2009, StatusNet, Inc.
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * @package MinifyPlugin
  29. * @maintainer Craig Andrews <candrews@integralblue.com>
  30. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  31. */
  32. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  33. // We bundle the minify library...
  34. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
  35. class MinifyPlugin extends Plugin
  36. {
  37. private $minifyInlineJs = true;
  38. private $minifyInlineCss = true;
  39. const cacheKey = 'minify';
  40. /**
  41. * Add Minification related paths to the router table
  42. *
  43. * Hook for RouterInitialized event.
  44. *
  45. * @return boolean hook return
  46. */
  47. function onStartInitializeRouter($m)
  48. {
  49. $m->connect('main/min',
  50. array('action' => 'minify'));
  51. return true;
  52. }
  53. function onLoginAction($action, &$login)
  54. {
  55. switch ($action)
  56. {
  57. case 'minify':
  58. $login = true;
  59. return false;
  60. default:
  61. return true;
  62. }
  63. }
  64. function onStartScriptElement($action,&$src,&$type) {
  65. $url = parse_url($src);
  66. if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
  67. {
  68. if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
  69. $src = $this->minifyUrl($src);
  70. } else {
  71. $src = $this->minifyUrl('js/'.$src);
  72. }
  73. }
  74. }
  75. function onStartCssLinkElement($action,&$src,&$theme,&$media) {
  76. $allowThemeMinification =
  77. is_null(common_config('theme', 'dir'))
  78. && is_null(common_config('theme', 'path'))
  79. && is_null(common_config('theme', 'server'));
  80. $url = parse_url($src);
  81. if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
  82. {
  83. if(!isset($theme)) {
  84. $theme = common_config('site', 'theme');
  85. }
  86. if($allowThemeMinification && file_exists(INSTALLDIR.'/local/theme/'.$theme.'/'.$src)) {
  87. $src = $this->minifyUrl('local/theme/'.$theme.'/'.$src);
  88. } else if($allowThemeMinification && file_exists(INSTALLDIR.'/theme/'.$theme.'/'.$src)) {
  89. $src = $this->minifyUrl('theme/'.$theme.'/'.$src);
  90. }else if(file_exists(INSTALLDIR.'/'.$src)){
  91. $src = $this->minifyUrl($src);
  92. }
  93. }
  94. }
  95. function onStartInlineScriptElement($action,&$code,&$type)
  96. {
  97. if($this->minifyInlineJs && $type=='text/javascript'){
  98. $c = Cache::instance();
  99. if (!empty($c)) {
  100. $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
  101. $out = $c->get($cacheKey);
  102. }
  103. if(empty($out)) {
  104. $out = $this->minifyJs($code);
  105. }
  106. if (!empty($c)) {
  107. $c->set($cacheKey, $out);
  108. }
  109. if(!empty($out)) {
  110. $code = $out;
  111. }
  112. }
  113. }
  114. function onStartStyleElement($action,&$code,&$type,&$media)
  115. {
  116. if($this->minifyInlineCss && $type=='text/css'){
  117. $c = Cache::instance();
  118. if (!empty($c)) {
  119. $cacheKey = Cache::key(self::cacheKey . ':' . crc32($code));
  120. $out = $c->get($cacheKey);
  121. }
  122. if(empty($out)) {
  123. $out = $this->minifyCss($code);
  124. }
  125. if (!empty($c)) {
  126. $c->set($cacheKey, $out);
  127. }
  128. if(!empty($out)) {
  129. $code = $out;
  130. }
  131. }
  132. }
  133. function minifyUrl($src) {
  134. return common_local_url('minify',null,array('f' => $src ,v => GNUSOCIAL_VERSION));
  135. }
  136. static function minifyJs($code) {
  137. require_once('JSMin.php');
  138. return JSMin::minify($code);
  139. }
  140. static function minifyCss($code, $options = array()) {
  141. require_once('Minify/CSS.php');
  142. return Minify_CSS::minify($code,$options);
  143. }
  144. function onPluginVersion(&$versions)
  145. {
  146. $versions[] = array('name' => 'Minify',
  147. 'version' => GNUSOCIAL_VERSION,
  148. 'author' => 'Craig Andrews',
  149. 'homepage' => 'http://status.net/wiki/Plugin:Minify',
  150. 'rawdescription' =>
  151. // TRANS: Plugin description.
  152. _m('The Minify plugin minifies StatusNet\'s CSS and JavaScript, removing whitespace and comments.'));
  153. return true;
  154. }
  155. }