Log in

View Full Version : serve template from filesystem instead of DB


fxdigi-cash
09-22-2014, 09:59 PM
Hi,

I'm trying to serve the template from filesystem instead of DB so I followed this short tut here: http://www.vbulletin.com/forum/forum/customizing-vbulletin/vbulletin-templates-graphics-styles/407190-templates-from-database-to-file-system

there is another tut has been around for a while, but it doesn't work anymore since vb has made a lot of updates and changes: http://www.go4expert.com/articles/export-vbulletin-templates-html-files-t25345/

what I did is the following:

1- created a folder in the root named template_dump with CHmod 0777
2- turned on your debug mode $config['Misc']['debug'] = true;
3- created the files visiting this url on my forum: http://FORUM_NAME.com/admincp/template.php?do=createfiles&dostyleid=your_active_style (https://sport4ever.maktoob.com/admincp/template.php?do=createfiles&dostyleid=your_active_style)

4- moved the folder template_dump to under includes ( includes/vb_templates )
5- in includes/class_core.php --> replaced protected static function fetch_template($template_name) with can protected static function file_get_contents (DIR. '/includes/vb_templates/'.$template_name.'htm')

after all that my forum doesn't work... sounds like there is something wrong...

any idea what's going on? :confused:

Thanks

Update: I made a mistake posting in vb3 section where this actually supposed to be on vb4

tpearl5
09-23-2014, 12:58 AM
Did you do your active style and -1 for the master style? Otherwise you will only get templates that are different from the master style.

Not to mention you can't just replace the function with "file_get_contents" - now you have a function named "file_get_contents".

I would play around with the fetch_template_start hook and try putting in the code from the second page:

if ($template == '<<< FILE >>>')
{
$template = addslashes(implode('', file("./templates/$template_name.htm")));
$vbulletin->templatecache["$template_name"] = $template;
}

(note that $templatename is now $template_name)

Then create the child style '<<< FILE >>>'

nerbert
09-23-2014, 01:30 AM
I would restore your file includes/class_core.php back to original and create this plugin for fetch_template_start:


if (!$fetched)
{
if (isset($vbulletin->templatecache["$template_name"]))
{
$template = $vbulletin->templatecache["$template_name"];
}
else
{
self::$template_queries[$template_name] = true;
$fetch_tid = intval($templateassoc["$template_name"]);
if (!$fetch_tid)
{
$gettemp = array('template' => '');
}
else
{
$gettemp = $vbulletin->db->query_first_slave("
SELECT template
FROM " . TABLE_PREFIX . "template
WHERE templateid = $fetch_tid
");
}
$template = $gettemp['template'];
$vbulletin->templatecache["$template_name"] = $template;
}
}

if (!isset(self::$template_usage[$template_name]))
{
self::$template_usage[$template_name] = 1;
}
else
{
self::$template_usage[$template_name]++;
}

/////////($hook = vBulletinHook::fetch_hook('fetch_template_complete ')) ? eval($hook) : false;

return $template;


That code is a straight copy/paste out of the file itself with the fetch_template_complete hook commented out (Idon't think you can have a hook in a plugin). Now the plugin code finishes the function (method, for you purists) and everything should run.

Now try playing around with commenting out the database query and replacing it with file_get_contents(). You should actually open, lock, read and unlock the file instead of just reading it with f_g_c()

fxdigi-cash
09-23-2014, 05:00 AM
I would restore your file includes/class_core.php back to original and create this plugin for fetch_template_start:


if (!$fetched)
{
if (isset($vbulletin->templatecache["$template_name"]))
{
$template = $vbulletin->templatecache["$template_name"];
}
else
{
self::$template_queries[$template_name] = true;
$fetch_tid = intval($templateassoc["$template_name"]);
if (!$fetch_tid)
{
$gettemp = array('template' => '');
}
else
{
$gettemp = $vbulletin->db->query_first_slave("
SELECT template
FROM " . TABLE_PREFIX . "template
WHERE templateid = $fetch_tid
");
}
$template = $gettemp['template'];
$vbulletin->templatecache["$template_name"] = $template;
}
}

if (!isset(self::$template_usage[$template_name]))
{
self::$template_usage[$template_name] = 1;
}
else
{
self::$template_usage[$template_name]++;
}

/////////($hook = vBulletinHook::fetch_hook('fetch_template_complete ')) ? eval($hook) : false;

return $template;
That code is a straight copy/paste out of the file itself with the fetch_template_complete hook commented out (Idon't think you can have a hook in a plugin). Now the plugin code finishes the function (method, for you purists) and everything should run.

Now try playing around with commenting out the database query and replacing it with file_get_contents(). You should actually open, lock, read and unlock the file instead of just reading it with f_g_c()

Thanks for the solution...

I'm somehow new to this so not sure what database query I should be replacing with file_get_contents()

can you give an example?

--------------- Added 1411452481 at 1411452481 ---------------

Did you do your active style and -1 for the master style? Otherwise you will only get templates that are different from the master style.

Not to mention you can't just replace the function with "file_get_contents" - now you have a function named "file_get_contents".

I would play around with the fetch_template_start hook and try putting in the code from the second page:

if ($template == '<<< FILE >>>')
{
$template = addslashes(implode('', file("./templates/$template_name.htm")));
$vbulletin->templatecache["$template_name"] = $template;
}
(note that $templatename is now $template_name)

Then create the child style '<<< FILE >>>'


Thanks mate for the quick solution.

I have done the steps as you instructed me. now only thing is how do I know it is working or not?? how do I test it?

kh99
09-23-2014, 10:58 AM
That code is a straight copy/paste out of the file itself with the fetch_template_complete hook commented out (Idon't think you can have a hook in a plugin). Now the plugin code finishes the function (method, for you purists) and everything should run.

I don't think that quite works as expected because calling return in a plugin doesn't return from the function that calls it, it provides a return value from the 'eval' call (which is ignored). But as you can see in the code above, there's a "fetched" variable, and if the plugin sets that to true then the database query (or cache lookup) won't be done. So I think you'd want your plugin to include only the code from inside the "if", then set $fetched to true.

Edit: I think tpearl's code would work because the code in fetch_template() gets it from the cache if it's set.

fxdigi-cash
09-23-2014, 02:05 PM
I don't think that quite works as expected because calling return in a plugin doesn't return from the function that calls it, it provides a return value from the 'eval' call (which is ignored). But as you can see in the code above, there's a "fetched" variable, and if the plugin sets that to true then the database query (or cache lookup) won't be done. So I think you'd want your plugin to include only the code from inside the "if", then set $fetched to true.

Edit: I think tpearl's code would work because the code in fetch_template() gets it from the cache if it's set.

Thanks mate for the confirmation. I used tpearl's code now as I want to test how it performs. I believe setting the debug mode will tell me whether things work or not...

not sure of another way on how to test it though.

any idea?? :(

tpearl5
09-23-2014, 02:19 PM
yep, setting debug mode should tell you where the templates are being read from in the bottom dropdown...

--------------- Added 1411485646 at 1411485646 ---------------

I don't think that quite works as expected because calling return in a plugin doesn't return from the function that calls it, it provides a return value from the 'eval' call (which is ignored). But as you can see in the code above, there's a "fetched" variable, and if the plugin sets that to true then the database query (or cache lookup) won't be done. So I think you'd want your plugin to include only the code from inside the "if", then set $fetched to true.

Edit: I think tpearl's code would work because the code in fetch_template() gets it from the cache if it's set.

I was about to say the same thing.


fyi, vb optimise has a feature to do all of this and/or read templates from the cache of your choice.

Max Taxable
09-23-2014, 05:09 PM
fyi, vb optimise has a feature to do all of this and/or read templates from the cache of your choice.That's exactly what I use, and do.... And had forgotten about it!

nerbert
09-23-2014, 09:11 PM
I don't think that quite works as expected because calling return in a plugin doesn't return from the function that calls it, it provides a return value from the 'eval' call (which is ignored). But as you can see in the code above, there's a "fetched" variable, and if the plugin sets that to true then the database query (or cache lookup) won't be done. So I think you'd want your plugin to include only the code from inside the "if", then set $fetched to true.

Edit: I think tpearl's code would work because the code in fetch_template() gets it from the cache if it's set.Well, you got me curous so I tested it


if (!$fetched)
{
if (isset($vbulletin->templatecache["$template_name"]))
{
$template = $vbulletin->templatecache["$template_name"];
}
else
{
self::$template_queries[$template_name] = true;
$fetch_tid = intval($templateassoc["$template_name"]);
if (!$fetch_tid)
{
$gettemp = array('template' => '');
}
else
{
$gettemp = $vbulletin->db->query_first_slave("
SELECT template
FROM " . TABLE_PREFIX . "template
WHERE templateid = $fetch_tid
");
}
$template = $gettemp['template'];
$vbulletin->templatecache["$template_name"] = $template;
}
}

if (!isset(self::$template_usage[$template_name]))
{
self::$template_usage[$template_name] = 1;
}
else
{
self::$template_usage[$template_name]++;
}

($hook = vBulletinHook::fetch_hook('fetch_template_complete ')) ? eval($hook) : false;
echo 'before return<br>';
return $template;
echo 'after return';


And in hook fetch_template_complete I just put in

echo 'test hook<br>';

and got many echos of

test hook
before return
test hook
test hook
before return
test hook
test hook

kh99
09-23-2014, 10:25 PM
Well yeah, "return" ends the plugin execution, but what I was saying is that it doesn't end the execution of fetch_template(), so if your plugin code doesn't set $fetched to true or set $vbulletin->templatecache["$template_name"], then fetch_template will go on and do it's work. And even if the plugin does one of those things, I don't think you'd want to increment the usage count since that will happen in any case.

Edit: I think I misunderstood the above post - what I was saying is if you were to for instance create a plugin that just contained a return, that wouldn't stop the rest of fetch_template from running. It's not exactly the same as insertng the code at the point the hook is eval'd. Also if the hook is called inside a loop you can't break or continue that loop from the plugin.

Maybe you knew all that and you assumed the code would be changed to remove that stuff, but I thought it could be clarified. Or maybe I'm missing something.

nerbert
09-23-2014, 11:25 PM
I see what you mean. That explains why "test hook" shows twice in succession. It's surprising a hook works inside a plugin. Before I uncommented the hook it showed only once between every "before return". But I don't think I'm going to try hook insertion that way in my projects.