a Cache thing

1 view
Skip to first unread message

Massimiliano

unread,
Oct 15, 2008, 4:47:37 AM10/15/08
to habari-users
Hi. I'm trying to port a plugin from the Other Blog Engine to Habari.
It enables LaTeX code into posts, so I think it won't be of any
interest, except to fellow physicists. Anyway, I can't make my code
work.

http://pastoid.com/bcg

Line 77 creates a LaTeX image with the help of a public server. If I
put the generated URL into a post, it works. But I don't want to load
the image and leech the server's bandwidth every time, so I need to
grab the image and cache it.
Lines 80-82 request the image with RemoteRequest and store the content
in the variable $response. Then, lines 85-87 put $response into the
cache. But I get this error:

Warning: fopen(/user/cache/tex_c9faf6ead2cd2c2187bd943488de1d0a.png)
[function.fopen]: failed to open stream: No such file or directory in
user/plugins/latex/latex.plugin.php line 83

I am a PHP newbie. The manual says that I need allow_url_fopen
enabled: my hosting provider has disabled it. I don't know if it's the
issue, but I need a workaround. So I substitute lines 85-87 with

Cache::set( $cached_image, $response );
$retrieved_image = Cache::get( $cached_image );

and a file "foo.data" is created successfully in my cache.
But now, how can I make transform this foo.data into an image, so that
I can put it in my entry?

Massimiliano
(iMassimiliano on IRC)

Ali B.

unread,
Oct 15, 2008, 7:08:05 AM10/15/08
to habari...@googlegroups.com
Why would you want to retrieve the image directly from the cache folder? You are already caching the response itself, so you can simply retrieve it. Something to do would be like the following:

If Cache::set('latex_response') {
    $response= Cache::get('latex_response');
}
else {
    $request = new RemoteRequest( $generated_image );
    $request->execute();
    $response = $request->get_response_body();
}

// User the response variable here to construct the img tag... and proceed.
--
Ali B / dmondark
http://www.awhitebox.com

Ali B.

unread,
Oct 15, 2008, 7:11:09 AM10/15/08
to habari...@googlegroups.com
Ok. I mixed up a couple of things in the sample code.Sorry about that. Here's what it should be:

If Cache::has('latex_response') {

    $response= Cache::get('latex_response');
}
else {
    $request = new RemoteRequest( $generated_image );
    $request->execute();
    $response = $request->get_response_body();
    Cache::set('latex_response', $response) ;

Massimiliano

unread,
Oct 15, 2008, 8:17:20 AM10/15/08
to habari-users
On 15 Ott, 13:08, "Ali B." <dmond...@gmail.com> wrote:
> Why would you want to retrieve the image directly from the cache folder? You
> are already caching the response itself, so you can simply retrieve it.

Because the <img> tag requires a URI in the src attribute. The
original plugin constructs a $variable that is the link to the cached
file. If I directly provide the $response variable, I'm providing the
actual file (unless I'm missing something about RemoteRequest). In
fact I get a string like

���t `�p41<� ��tZ��� b#o$3Bz�aqe Yv@
x �1�)& ' '|$�) �

If I simply return $response, I get a string like

GIF89a)� �������������FFF���ttt\\\��� 444 $$$ ������!
� ����,����)� �� � �A@ 3 � � G5�(�!I� � � �Av
|� �Ch�A,F ��p 6S��tJ%T �N@�0���"E&�>���t `�p41<� ��tZ��� b#o
$3Bz�aqe Yv@
x �1�)& ' '|$�) �;

I can't think of other ways to return $response. BTW, now I'm using
the condition that you suggested :)
Massimiliano

Arthus Erea

unread,
Oct 15, 2008, 12:00:14 PM10/15/08
to habari...@googlegroups.com
What about using a data url?

Balazs Koren

unread,
Oct 15, 2008, 12:55:15 PM10/15/08
to habari...@googlegroups.com

Massimiliano,

I can't help you in programming, but I'd love to test the plugin, if it helps.

Balazs

Massimiliano

unread,
Oct 15, 2008, 5:13:25 PM10/15/08
to habari-users
On 15 Ott, 18:00, Arthus Erea <arthus.e...@gmail.com> wrote:
> What about using a data url?

Thanks Arthus, data URLs do what I need. But I read on Wikipedia (hey,
it's the first result on Google) that they are not supported on IE7.
Moreover, now I see that backslashes are stripped from the input code,
somewhere in its way to be parsed. May this be a RemoteRequest
feature?

On 15 Ott, 18:55, Balazs Koren <balazs.ko...@gmail.com> wrote:
> I can't help you in programming, but I'd love to test the plugin, if it helps.

Sure! The most recent code, awfully scruffy and with the old bits
commented out, is at http://pastoid.com/bcj
But as I said, backslashes are somehow stripped from the code, I have
yet to figure out where and why. So $$ \mu $$ currently returns "mu"
instead of "µ".
Massimiliano

Geoffrey Sneddon

unread,
Oct 15, 2008, 5:15:10 PM10/15/08
to habari...@googlegroups.com

On 15 Oct 2008, at 18:00, Arthus Erea wrote:

> What about using a data url?

Data URIs (they aren't URLs, but that's pedantry) for images means
either using pct-encoding (e.g., %20) or base64 — both of which are
vastly inefficient, and whose bandwidth usage will outweight any gain
by caching. Then you have the problem of IE < 8.


--
Geoffrey Sneddon
<http://gsnedders.com/>

Matt Read

unread,
Oct 16, 2008, 10:52:03 PM10/16/08
to habari...@googlegroups.com
On Wed, Oct 15, 2008 at 8:17 AM, Massimiliano
<massimilian...@gmail.com> wrote:
> Because the <img> tag requires a URI in the src attribute. The
> original plugin constructs a $variable that is the link to the cached
> file. If I directly provide the $response variable, I'm providing the
> actual file (unless I'm missing something about RemoteRequest). In
> fact I get a string like
>
> ���t `�p41<� ��tZ��� b#o$3Bz�aqe Yv@
> x �1�)& ' '|$�) �
>
> If I simply return $response, I get a string like
>
> GIF89a)� �������������FFF���ttt\\\��� 444 $$$ ������!
> � ����,����)� �� � �A@ 3 � � G5�(�!I� � � �Av
> |� �Ch�A,F ��p 6S��tJ%T �N@�0���"E&�>���t `�p41<� ��tZ��� b#o
> $3Bz�aqe Yv@
> x �1�)& ' '|$�) �;
>
> I can't think of other ways to return $response. BTW, now I'm using
> the condition that you suggested :)
> Massimiliano

If you don't want to create your own cache of the image files, you
would need to do a proxy of sorts. Your plugin would basically add a
rewrite rule then echo the appropriate Cache::get call for the
requested uri. So you might have /latex/some-hash-string.png, and
plugin retrieve 'some-hash-string' from cache. This means for every
request you load Habari twice, which is not great...

The other way, as I mentioned, is to create your own cache dir and
save the generated images there, linking to them directly, thus not
loading Habari twice.

note: You SHOULD NEVER NEVER NEVER link directly to habari's FileCache
data; in fact on my server I set cache dirs to a non-public facing
folder (not in docroot)... in my case define('FILE_CACHE_LOCATION',
'/var/www/mattread/cache/habari/'); in config.php.


--
Matt Read
http://mattread.com

Reply all
Reply to author
Forward
0 new messages