aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrvelices <rv-github@modusoptimus.com>2010-12-06 20:52:44 +0000
committerrvelices <rv-github@modusoptimus.com>2010-12-06 20:52:44 +0000
commit1cbbe10bfd88f12f62b9cfc15e2ccd46d5e37b27 (patch)
tree0c376ec8354a7abb710c0f111fbcc42e6f6e137d
parent1e6cae89c30296045b21c39502c1a20a9538dad7 (diff)
combined script now really merges files
added option $conf['template_combine_files'] by default true to enable/disable file combining git-svn-id: http://piwigo.org/svn/trunk@8012 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r--admin/themes/default/template/element_set_global.tpl1
-rw-r--r--include/config_default.inc.php3
-rw-r--r--include/template.class.php135
-rw-r--r--plugins/admin_multi_view/controller.php24
-rw-r--r--plugins/admin_multi_view/is_admin.inc.php4
5 files changed, 116 insertions, 51 deletions
diff --git a/admin/themes/default/template/element_set_global.tpl b/admin/themes/default/template/element_set_global.tpl
index 24cea87ed..d0b9b0322 100644
--- a/admin/themes/default/template/element_set_global.tpl
+++ b/admin/themes/default/template/element_set_global.tpl
@@ -3,7 +3,6 @@
{footer_script}{literal}
pwg_initialization_datepicker("#date_creation_day", "#date_creation_month", "#date_creation_year", "#date_creation_linked_date", "#date_creation_action_set");
-</script>
{/literal}{/footer_script}
{combine_script id='jquery.fcbkcomplete' load='async' require='jquery' path='themes/default/js/plugins/jquery.fcbkcomplete.js'}
diff --git a/include/config_default.inc.php b/include/config_default.inc.php
index cab91814b..bb0293f72 100644
--- a/include/config_default.inc.php
+++ b/include/config_default.inc.php
@@ -480,6 +480,9 @@ $conf['template_compile_check'] = true;
// production environment.
$conf['template_force_compile'] = false;
+// activate merging of javascript / css files
+$conf['template_combine_files'] = true;
+
// this permit to show the php errors reporting (see INI 'error_reporting'
// for possible values)
// gives an empty value '' to deactivate
diff --git a/include/template.class.php b/include/template.class.php
index afc955096..698de6723 100644
--- a/include/template.class.php
+++ b/include/template.class.php
@@ -396,7 +396,7 @@ class Template {
{
$scripts = $this->scriptLoader->get_head_scripts();
$content = array();
- foreach ($scripts as $id => $script)
+ foreach ($scripts as $script)
{
$content[]=
'<script type="text/javascript" src="'
@@ -411,25 +411,39 @@ class Template {
if(!empty($this->css_by_priority))
{
ksort($this->css_by_priority);
- $combiner = new FileCombiner('css');
- foreach ($this->css_by_priority as $files)
- {
- foreach ($files as $file_ver)
- {
- $combiner->add( $file_ver[0], $file_ver[1] );
- }
- }
- if ( $combiner->combine( $out_file, $out_version) )
+
+ global $conf;
+ $css = array();
+ if ($conf['template_combine_files'])
+ {
+ $combiner = new FileCombiner('css');
+ foreach ($this->css_by_priority as $files)
+ {
+ foreach ($files as $file_ver)
+ $combiner->add( $file_ver[0], $file_ver[1] );
+ }
+ if ( $combiner->combine( $out_file, $out_version) )
+ $css[] = array($out_file, $out_version);
+ }
+ else
+ {
+ foreach ($this->css_by_priority as $files)
+ $css = array_merge($css, $files);
+ }
+
+ $content = array();
+ foreach( $css as $file_ver )
{
- $href = get_root_url() . $out_file;
- if ($out_version !== false)
- $href .= '?v' . ($out_version ? $out_version : PHPWG_VERSION);
+ $href = get_root_url() . $file_ver[0];
+ if ($file_ver[1] !== false)
+ $href .= '?v' . ($file_ver[1] ? $file_ver[1] : PHPWG_VERSION);
// trigger the event for eventual use of a cdn
- $href = trigger_event('combined_css', $href, $out_file, $out_version);
- $this->output = str_replace(self::COMBINED_CSS_TAG,
- '<link rel="stylesheet" type="text/css" href="'.$href.'">',
- $this->output );
+ $href = trigger_event('combined_css', $href, $file_ver[0], $file_ver[1]);
+ $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">';
}
+ $this->output = str_replace(self::COMBINED_CSS_TAG,
+ implode( "\n", $content ),
+ $this->output );
}
if ( count($this->html_head_elements) )
@@ -568,14 +582,12 @@ class Template {
}
else
{
- if (!$this->scriptLoader->did_head())
- fatal_error('get_combined_scripts didn\'t call header');
$scripts = $this->scriptLoader->get_footer_scripts();
- foreach ($scripts[0] as $id => $script)
+ foreach ($scripts[0] as $script)
{
$content[]=
'<script type="text/javascript" src="'
- . Template::make_script_src($script)
+ . self::make_script_src($script)
.'"></script>';
}
if (count($this->scriptLoader->inline_scripts))
@@ -595,7 +607,7 @@ class Template {
{
$content[]=
's=document.createElement(\'script\'); s.type = \'text/javascript\'; s.async = true; s.src = \''
- . Template::make_script_src($script)
+ . self::make_script_src($script)
.'\';';
$content[]= 'after = after.parentNode.insertBefore(s, after);';
}
@@ -610,7 +622,7 @@ class Template {
private static function make_script_src( $script )
{
$ret = '';
- if ( url_is_remote($script->path) )
+ if ( $script->is_remote() )
$ret = $script->path;
else
{
@@ -620,6 +632,8 @@ class Template {
$ret.= '?v'. ($script->version ? $script->version : PHPWG_VERSION);
}
}
+ // trigger the event for eventual use of a cdn
+ $ret = trigger_event('combined_script', $ret, $script);
return $ret;
}
@@ -638,7 +652,6 @@ class Template {
$order = (int)@$params['order'];
$version = isset($params['version']) ? $params['version'] : 0;
$this->css_by_priority[$order][] = array( $params['path'], $version);
- //var_export( $this->css_by_priority ); echo "<br>";
}
function func_get_combined_css($params, &$smarty)
@@ -829,13 +842,14 @@ final class Script
public $version;
public $extra = array();
- function Script($id, $load_mode, $precedents, $path, $version)
+ function Script($load_mode, $id, $path, $version, $precedents)
{
$this->id = $id;
$this->load_mode = $load_mode;
- $this->precedents = $precedents;
- $this->path = $path;
+ $this->id = $id;
+ $this->set_path($path);
$this->version = $version;
+ $this->precedents = $precedents;
}
function set_path($path)
@@ -843,7 +857,7 @@ final class Script
if (!empty($path))
$this->path = $path;
}
-
+
function is_remote()
{
return url_is_remote( $this->path );
@@ -901,7 +915,7 @@ class ScriptLoader
}
if (! isset( $this->registered_scripts[$id] ) )
{
- $script = new Script($id, $load_mode, $require, $path, $version);
+ $script = new Script($load_mode, $id, $path, $version, $require);
self::fill_well_known($id, $script);
$this->registered_scripts[$id] = $script;
}
@@ -945,16 +959,15 @@ class ScriptLoader
trigger_error("Script $id has an undefined path", E_USER_WARNING);
}
$this->did_head = true;
- return $this->head_done_scripts;
+ return self::do_combine($this->head_done_scripts, 0);
}
function get_footer_scripts()
{
- if (!$this->did_head)
+ /*if (!$this->did_head)
{
trigger_error("Attempt to write footer scripts without header scripts", E_USER_ERROR );
- }
-
+ }*/
$todo = array();
foreach( $this->registered_scripts as $id => $script)
{
@@ -970,17 +983,36 @@ class ScriptLoader
uasort($todo, array('ScriptLoader', 'cmp_by_mode_and_order'));
-
$result = array( array(), array() );
foreach( $todo as $id => $script)
{
$result[$script->load_mode-1][$id] = $script;
}
- return $result;
+ return array( self::do_combine($result[0],1), self::do_combine($result[1],2) );
}
+ private static function do_combine($scripts, $load_mode)
+ {
+ global $conf;
+ if (count($scripts)<2 or !$conf['template_combine_files'])
+ return $scripts;
+ $combiner = new FileCombiner('js');
+ foreach ($scripts as $script)
+ {
+ if ($script->is_remote()) fatal_error("NOT IMPLEMENTED");// TODO - we cannot combine remote scripts
+ $combiner->add( $script->path, $script->version );
+ }
+ if ( $combiner->combine( $out_file, $out_version) )
+ {
+ return array( 'combi' => new Script($load_mode, 'combi', $out_file, $out_version, array() ) );
+ }
+ return null;
+ }
+
+ // checks that if B depends on A, then B->load_mode >= A->load_mode in order to respect execution order
private static function check_load_dep($scripts)
{
+ global $conf;
do
{
$changed = false;
@@ -998,7 +1030,7 @@ class ScriptLoader
$scripts[$precedent]->load_mode = $load;
$changed = true;
}
- if ($load==2 && $scripts[$precedent]->load_mode==2 && $script->is_remote() )
+ if ($load==2 && $scripts[$precedent]->load_mode==2 && ($script->is_remote() or !$conf['template_combine_files']) )
{// we are async -> a predecessor cannot be async unlesss it can be merged; otherwise script execution order is not guaranteed
$scripts[$precedent]->load_mode = 1;
$changed = true;
@@ -1008,8 +1040,8 @@ class ScriptLoader
}
while ($changed);
}
-
-
+
+
private static function fill_well_known($id, $script)
{
if ( empty($script->path) && isset(self::$known_paths[$id]))
@@ -1048,10 +1080,10 @@ class ScriptLoader
{
$ret = $s1->load_mode - $s2->load_mode;
if ($ret) return $ret;
-
+
$ret = $s1->extra['order'] - $s2->extra['order'];
if ($ret) return $ret;
-
+
if ($s1->extra['order']==0 and ($s1->is_remote() xor $s2->is_remote()) )
{
return $s1->is_remote() ? -1 : 1;
@@ -1073,7 +1105,7 @@ final class FileCombiner
{
$this->type = $type;
}
-
+
static function clear_combined_files()
{
$dir = opendir(PHPWG_ROOT_PATH.self::OUT_SUB_DIR);
@@ -1099,7 +1131,6 @@ final class FileCombiner
function combine(&$out_file, &$out_version)
{
- //var_export($this);
if (count($this->files) == 0)
{
return false;
@@ -1139,11 +1170,9 @@ final class FileCombiner
{
$output .= "/* BEGIN $input_file */\n";
if ($this->type == "css")
- {
- $output .= $this->process_css($input_file);
- }
+ $output .= self::process_css($input_file);
else
- $output .= file_get_contents(PHPWG_ROOT_PATH . $input_file);
+ $output .= self::process_js($input_file);
$output .= "\n";
}
@@ -1154,7 +1183,17 @@ final class FileCombiner
return 2;
}
- private function process_css($file)
+ private static function process_js($file)
+ {
+ $js = file_get_contents(PHPWG_ROOT_PATH . $file);
+ if (strpos($file, '.min')===false and strpos($file, '.packed')===false )
+ {
+ //TODO minify javascript with some php lib from www...
+ }
+ return $js;
+ }
+
+ private static function process_css($file)
{
static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#";
$css = file_get_contents(PHPWG_ROOT_PATH . $file);
@@ -1180,7 +1219,7 @@ final class FileCombiner
foreach ($matches as $match)
{
$search[] = $match[0];
- $replace[] = $this->process_css(dirname($file) . "/$match[1]");
+ $replace[] = self::process_css(dirname($file) . "/$match[1]");
}
$css = str_replace($search, $replace, $css);
}
diff --git a/plugins/admin_multi_view/controller.php b/plugins/admin_multi_view/controller.php
index 88b22f2e2..d2cd612c2 100644
--- a/plugins/admin_multi_view/controller.php
+++ b/plugins/admin_multi_view/controller.php
@@ -11,6 +11,7 @@ if (!is_admin() or !function_exists('multiview_user_init') )
pwg_unset_session_var( 'multiview_show_queries' );
pwg_unset_session_var( 'multiview_debug_l10n' );
pwg_unset_session_var( 'multiview_debug_template' );
+ pwg_unset_session_var( 'multiview_template_combine_files' );
pwg_unset_session_var( 'multiview_no_history' );
?>
@@ -85,6 +86,16 @@ if ( isset($_GET['debug_template']) )
$refresh_main = true;
}
+if ( isset($_GET['template_combine_files']) )
+{
+ if ( $_GET['template_combine_files']==0 )
+ pwg_set_session_var( 'multiview_template_combine_files', 0 );
+ else
+ pwg_unset_session_var( 'multiview_template_combine_files' );
+ $refresh_main = true;
+}
+
+
if ( isset($_GET['no_history']) )
{
if ( $_GET['no_history']>0 )
@@ -194,6 +205,17 @@ if (!$conf['debug_template'])
}
// +-----------------------------------------------------------------------+
+// | template combine files |
+$template_combine_files_html='';
+if ($conf['template_combine_files'])
+{
+ if ( pwg_get_session_var( 'multiview_template_combine_files', 1 ) )
+ $template_combine_files_html.='<a href="'.$my_url.'?template_combine_files=0">Don\'t combine js&amp;css</a>';
+ else
+ $template_combine_files_html.='<a href="'.$my_url.'?template_combine_files=1">Combine js&amp;css</a>';
+}
+
+// +-----------------------------------------------------------------------+
// | no history |
$no_history_html='';
if ( !pwg_get_session_var( 'multiview_no_history', 0 ) )
@@ -224,7 +246,7 @@ if (window.opener==null) {
<tr><td>Lang</td><td><?php echo $lang_html; ?></td></tr>
</table>
-<?php echo implode( "<br/>\n", array($show_queries_html, $debug_l10n_html, $debug_template_html, $no_history_html) ); ?>
+<?php echo implode( "<br/>\n", array($show_queries_html, $debug_l10n_html, $debug_template_html, $template_combine_files_html, $no_history_html) ); ?>
<script type="text/javascript">
<?php
diff --git a/plugins/admin_multi_view/is_admin.inc.php b/plugins/admin_multi_view/is_admin.inc.php
index fbadada3a..df9e91276 100644
--- a/plugins/admin_multi_view/is_admin.inc.php
+++ b/plugins/admin_multi_view/is_admin.inc.php
@@ -25,7 +25,9 @@ if (! defined('MULTIVIEW_CONTROLLER') )
$conf['debug_l10n'] = true;
if (pwg_get_session_var( 'multiview_debug_template', 0 ))
$conf['debug_template'] = true;
- if (pwg_get_session_var( 'multiview_no_history', 0 ))
+ if (!pwg_get_session_var( 'multiview_template_combine_files', 1 ))
+ $conf['template_combine_files'] = false;
+ if (pwg_get_session_var( 'multiview_no_history', 0 ))
{
add_event_handler( 'pwg_log_allowed', create_function( '', 'return false;' ) );
}