Archive for the ‘english’ Category

How to use MySQL Views in Symfony?

Tuesday, January 15th, 2008

Although MySQL views tend to be sometimes (ok, let’s be sincere - they usually are) ineffective, we can face a situation where we want to use them. Neither Symfony, nor Propel support natively views, but I found recently a little workaround for it.

Let’s assume we’ve got in our schema.yml:

entry:
  id:
  title: varchar(255)
  body: longvarchar
  pub_date: { type: timestamp, index: true }

which, after running propel-build-all, will give us two following classes (files) in lib/model:

Entry.php
EntryPeer.php

We want to use a view, that shows entries from recent week, we so run the query:

CREATE VIEW entry_recent AS SELECT * FROM entry
WHERE pub_date > CURRENT_DATE - INTERVAL 7 DAY;

The question is how to get objects of Entry type from entry_recent table?

Solution.

Add an aditional table in schema.yml with skipSql=true attribute – it will result in generating php classes, but propel won’t generate any SQL code.

entry_recent:
  _attributes: { skipSql: true, readOnly: true }
  id:
  title: varchar(255)
  body: longvarchar
  pub_date: { type: timestamp, index: true }

It’s important that entry_recent has always the same columns as entry.
After propel-build-all we have two additional classes:

EntryRecent.php
EntryRecentPeer.php

Now we open EntryRecent.php and change the base class from BaseEntryRecent to Entry.

class EntryRecent extends BaseEntryRecent Entry
{
}

And that’s it.

When we want to use our view, we simply use EntryRecentPeer methods to retrieve objects – doSelect(), doSelectOne() methods will return us EntryRecent objects, but their superclass is Entry, so we can use all methods that we had previously wrtiten in Entry.php.

What’s more we can update our objects (normally we are not able to update views), because they will use BaseEntry class instead of BaseEntryRecent.

How to get video thumbnails from dailymotion?

Sunday, January 6th, 2008

You can get thumbnails from YouTube easily using their API, but Dailymotion does not have API or any other intuitive way to download jpeg from specific video. I made a little research and found a solution, that at least work for me. When you are scanning webpages for videos you find embed tags with videos similiar to the one below:

<embed src="http://www.dailymotion.com/swf/IHnP80wxWqFSlg4ms" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="363" width="425"></embed>

At first I thought that I can get thumbnail using swf parameter IHnP80wxWqFSlg4ms. But it was wrong. We need first get the effective URL (after HTTP 302, or HTTP Location header forwarding). We can do it using libcurl:

function getEffectiveUrl($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_exec($ch);>

  $newurl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
  curl_close($ch);

  return urldecode($newurl);
}

Our effective URL looks like this:

http://www.dailymotion.com/flash/flvplayer.swf?rev=1198003823&
statEnabled=1&selfURL=http://www.dailymotion.com/Gregouze7
/video/x2a39c_just-jack-starz-in-their-eyes_music&
(it is really long!)

Now we can get from the effective URL two important information - key and title using the following simple regular expression:

#video/([a-z0-9]+)_([a-z0-9-]+)#i"

So our key is x2a39c. We fetch the thumbnail from:

http://www.dailymotion.com/thumbnail/320×240/video/[key]

And that’s it. There also other sizes available like 160×120.

(In)dependent woman or (in)dependent man.

Saturday, January 5th, 2008

Todays women wants more rights, become more independent. Sincerely I don’t care much about it, even think that they do the right thing, but this is a little bit too much:

“I don’t do any housework. No I don’t have a housekeeper who does it all for me, but I have a boyfriend, which is much cheaper”

Another side of independence

So I prepared an answer:

“When your woman wants you to work in the kitchen… remember never give up your real work and passion. Be yourself!”

Be yourself!

PS. I received the first picture from my girlfriend - think it means no good for me?

sfFeed2Plugin patch

Saturday, January 5th, 2008

I wrote a small patch for sfFeed2Plugin (verson 0.9.4) to get feed’s generator field and comment feeds for each post.

The generator is an identificator of software that generated feed. It is quite obvious and both RSS and Atom formats support it.

However, comment feeds are a little bit more tricky.

In Atom you get something like that:

<entry>
...
<link rel='replies' type='text/html'
href='http://blog.example.com/post-title/#comments'
title='Comments'/>
<link rel='replies' type='application/atom+xml'
href='http://blog.example.com/post-title/feed/'
title='Comment feed'/>
...
</entry>

So we need replies link which has a mime type application/atom+xml.

In RSS it is more straighforward:

<item>
...
<wfw:commentRss>
http://blog.example.com/post-title/feed/
</wfw:commentRss>
...
</item>

We simply get the wfw:commentRss field.

So how to use patched sfFeed2Plugin?

It is really easy, there is one additional method in sfFeed class called getGenerator(), and one in sfFeedItem class called getCommentFeed():

<?php
$feed = sfFeedPeer::createFromWeb($url);
echo $feed->getGenerator()."\n";
foreach($feed->getItems() as $item) {
echo $item->getCommentFeed()."\n";
}
?>

How to apply the patch?

$ cd sfFeed2Plugin/
$ patch -p1 < sfFeed2Plugin-094-mauser.patch

Download patch