Showing posts with label symfony. Show all posts
Showing posts with label symfony. Show all posts

May 20, 2009

Add Syndication to symfony-driven Blog

If you have a standalone blog powered by symfony framework, and would like to add syndication with major blog services like Blogger or LiveJournal, you could use a PEAR “Services_Blogging” package.

Download



First, you download it and then install following this tutorial.

Extract the archive and put the contents in your symphony's lib/vendor/serviceblogging directory. You will also have to add the XML-RPC PEAR package. Download it, extract, then put XML_RPC_x.x.x folder in the servicesblogging directory (there must be a Services folder there) and rename from “XML_RPC_x.x.x” to “XML

Install



Now, install the package as a symfony plugin. For this, edit the config/ProjectConfiguration.php:

class ProjectConfiguration extends sfProjectConfiguration
{

static protected $sbLoaded = false;

static public function registerServicesBlogging() {
if (self::$sbLoaded) {
return;
}

set_include_path(sfConfig::get('sf_lib_dir').'/vendor/servicesblogging'
.PATH_SEPARATOR.get_include_path());
require_once sfConfig::get('sf_lib_dir').'/vendor/servicesblogging/Services/Blogging.php';
self::$sbLoaded = true;
}

public function setup()
{
}
}


Enjoy



Now in your php code you can do the following to add a new post:

$bl = Services_Blogging::factory(
"LiveJournal",
"username", "password",
"http://livejournal.com",
"/interface/xmlrpc");

$post = $bl->createNewPost();
$post->title = $title;
$post->content = $text;
$bl->savePost($post);


Disable Auto-formatting



A little something for livejournal users: you can disable auto-formatting if you pass a special parameter to livejournal XML-RPC service. To do this the dirty, way, edit Services/Blogging/Driver/LiveJournal.php file of the Services_Blogging package.

Specifically, in the the Driver.savePost() method, add props parameter to the RPC call value:

...
$value = new XML_RPC_Value(
array(
'username' => $this->userdata['rpc_user'],
'auth_method' => new XML_RPC_Value('challenge', 'string'),
'auth_challenge' => new XML_RPC_Value(
$authdata['challenge'], 'string'
),
'auth_response' => new XML_RPC_Value(
$authdata['response'] , 'string'
),

'subject' => new XML_RPC_Value(
$post->{Services_Blogging_Post::TITLE}
),
'event' => new XML_RPC_Value(
$post->{Services_Blogging_Post::CONTENT}
),
'lineendings' => new XML_RPC_Value('pc'),

'year' => new XML_RPC_Value(date('Y', $time), 'int'),
'mon' => new XML_RPC_Value(date('n', $time), 'int'),
'day' => new XML_RPC_Value(date('j', $time), 'int'),
'hour' => new XML_RPC_Value(date('G', $time), 'int'),
'min' => new XML_RPC_Value(date('i', $time), 'int'),

'props' => new XML_RPC_Value(
array('opt_preformatted' => new XML_RPC_VALUE(true, 'boolean')),
'struct'),
),
'struct'
);

...


That's it.

Jan 19, 2009

symfony's image_tag(): Handle With Care

Turns out, symfony's image_tag() helper is a sucker for memory.

It takes more than 34 megabytes of memory to render a page displaying a list of about 500 rows, each accompanied by a set of four or five icons, each icon's <img> tag created by image_tag() helper, with additional parameters provided like this:

image_tag("some_image.png", "alt=Lovely, title=Rita")

With those images coded explicitly, rendering takes just 8.5MB. A leak, maybe?

Dec 16, 2008

sfGuardPlugin: Database Prefix

With symfony, it's kinda funny to search for documentation, since it's widely distributed across wiki, book, and various external sources, like Propel website.

That is why when you need something small and easy, but you don't know how to do this, it's usually faster to try that yourself first, search for an answer later. This is actually not the way I prefer to do stuff, so I will share some experiences about how to do small stuff in symfony.

So, the problem is, I have a single database on my web hosting available. However, I came across a situation where I needed two separate sets of sfGuardUser-s. To achieve this, I had to go the usual way — add a prefix to the tables.

All I had to do was to add “prfx_” to all the tables mentioned in <project-name>/plugins/sfGuardPlugin/config/schema.yml and then rebuild stuff with propel:

symfony propel:build-all

or, if you want to keep your existing data, rename tables in database manually, and then...

symfony propel:build-sql
symfony propel:build-form
symfony propel:build-model

...and you're set.