How do you generate a Short URL during entity creation?

billyburly

Joined: 2007-08-08
Posts: 3
Posted: Mon, 2009-07-20 19:41

I am working on a module for some Gallery2 facebook integration (things like having a link to a new album being put in the users feed) and would like to be able to support Short URLs if the users Gallery2 installation supports them. I have found some documentation on how to check if the rewrite module is enabled and working but that is about it. When I make any calls to the generateUrl function I always receive back a long URL. Is there any way to accomplish this?

First I tried:

$urlGenerator = $gallery->getUrlGenerator();
$url = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $entity->id), array('forceSessionId' => false, 'forceFullUrl' => true));

I have also tried:

$urlGenerator = new RewriteUrlGenerator();
$urlGenerator->init(!empty($configBaseUri) ? $configBaseUri : null);
$url = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $entity->id), array('forceSessionId' => false, 'forceFullUrl' => true));

Both of these only generate long URLs for me. Is what I am trying to do even possible with the Gallery2 API? Or have I just missed something?

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Mon, 2009-07-20 20:24

Your module has to setup rules
@see GalleryModule::getRewriteRules

an example /modules/tags/module.inc

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
mridgwel

Joined: 2007-01-27
Posts: 215
Posted: Mon, 2009-07-20 20:41

It is possible. I'm assuming you want to use the existing rule 'core.ShowItem' as thats what you used in the example. You can also use permalinks if they've been defined, but that has a different rule.

If you want to have your own short URL's then you'd need to define the rules in your module.

You do have to get the parameters exactly as the rewriter is expecting. I've used the g->url from a theme to produce a good url:

{g->url arg1="view=core.ShowItem" arg2="itemId=`$theme.item.id`" forceFullUrl=true}

by the look of it what you're doing is correct.

$url = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $child->getId()));

_________
Mark
ACCU - www.accu.org - Professionalism in Programming.

 
billyburly

Joined: 2007-08-08
Posts: 3
Posted: Thu, 2009-07-23 18:30

I am trying to get the Short URL for an album that is in gallery. I have an event listener setup to get information when an album is created and send it to my server via POST. However, the call to generateUrl() has only been giving me long URLs. I would prefer to use the short ones when available. Also facebook's API does not allow you to use dynamic images in certain areas. Thus, the Short URLs are useful to me, they do not look like dynamic images to facebook and I would be able to incorporate images into other parts of the facebook app.

I'd prefer to not have to go through the theme engine, and have a page to display the short URL for me. That would mean I would have to send the POST data to my server, then my server would have to go and retrieve more data from the users server, before sending it off to facebook. I would like to avoid having my server request information from the user as much as possible. This would minimize the load on both my server and the users server.

I do not need to make short URLs for my module. Although there was plenty of information on accomplishing that, and little if not none on what I am trying to do.

I'm not sure if what I am trying to do is possible, but I assume it is somehow. From what I've seen all of Gallery2 is modular and I'd guess the theme engine uses the rewrite module to make the short URLs. I see no reason why I cannot do that as well.

EDIT:
I have done some more work and discovered that for some reason when I call generateUrl() during the initial creation of an entity, it gives back only a long URL. Any time after this it returns a short URL. I am using an event handler that executes on the event GalleryEntity::save. I then check to see if its an album and the creation time and modification time match. For some reason I cannot figure out, at that time generateUrl() only gives long URLs. Are certain things just not available during this event?

I would prefer to get this working. I don't want to have to have a process constantly running on my server waiting for data to be received, so that it can go out and request some additional data. I want to send along all the data I need at once.

Does anyone know why Gallery is behaving in this manner?