silverstripe-picasaweb 

silverstripe-picasaweb Commit Details

Date:2013-05-06 21:47:08 (7 years 11 months ago)
Author:Nicola Fontana
Branch:master
Commit:64ee7fbab405de33ce8a70115f061d6d6fbdec10
Parents: 4cd750b7b69706c2ab60ae340e98818dafaa7fc8
Message:Refactored and documented code

Actually the Picasaweb static class has only three entry points:

1. render()
For rendering from Picasaweb components (user, album and photo id);
2. renderUrl()
A render() version that accepts Picasaweb URL instead of components;
3. shortcode()
The callback to be used for shortcode handling.
Changes:
Mcode/Picasaweb.php (11 diffs)

File differences

code/Picasaweb.php
22
33
44
5
6
7
58
69
10
11
12
713
814
915
......
1319
1420
1521
16
17
1822
1923
2024
......
2529
2630
2731
28
29
3032
3133
3234
......
142144
143145
144146
145
146
147
147
148148
149149
150150
......
221221
222222
223223
224
224
225225
226226
227227
228228
229229
230
230
231231
232232
233233
......
241241
242242
243243
244
245
246
247
248
249
250
251
244252
245253
246254
......
259267
260268
261269
262
263
270
271
264272
265273
266
274
267275
268276
269
277
270278
271
279
272280
273281
274282
275283
276284
277285
278
279
286
287
280288
281289
282290
......
290298
291299
292300
293
294
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
295323
296324
297325
298
299
300
301
302
303
304
326
327
328
329
330
331
332
333
334
305335
306
307336
308337
309338
310
311
312
313
314
339
340
341
315342
316
343
317344
318345
319346
......
324351
325352
326353
327
354
328355
329356
330
331
357
358
332359
333
360
334361
335362
336363
......
342369
343370
344371
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359372
360373
361374
362
375
363376
364
377
365378
366379
367
368
380
381
382
383
384
385
386
387
388
389
390
391
392
393
369394
370395
371396
......
376401
377402
378403
404
405
406
407
408
409
410
411
412
379413
380414
381415
382416
383417
384418
385
419
386420
387421
388422
423
424
425
426
427
428
429
430
431
389432
390433
391434
class Picasaweb {
/**
* Flag to enable or disable the photo rendering.
*/
public static $render_photo = true;
/**
* Flag to enable or disable the album rendering.
*/
public static $render_album = true;
/**
* You can use a string value identifying the field to use,
* such as 'thumbnail1' or 'thumbnail2', or an integer value to
* request a specific size, such as 288.
*
* @return String|Int The thumbnail image or the requested size.
*/
public static $thumbnail = 'thumbnail3';
* You can use a string value identifying the field to use,
* such as 'normal' or 'thumbnail3', or an integer value to
* request a specific size, such as 800.
*
* @return String The zoomed image or the requested size.
*/
public static $zoomed = 'normal';
"/s$new_size/$1",
$thumbnail->src);
return new ArrayData(array(
'src' => $url
));
return new ArrayData(array('src' => $url));
}
/**
* @param SimpleXMLElement $element The source element.
* @return ArrayData Album data.
*/
static function albumMetadata(SimpleXMLElement $element) {
private static function albumMetadata(SimpleXMLElement $element) {
if (empty($element))
return null;
$gphoto = $element->children('http://schemas.google.com/photos/2007');
return new ArrayData(array(
$data = new ArrayData(array(
'self' => self::xquery($element, 'link[@rel="self"]/@href'),
'userid' => (string) $gphoto->user,
'icon' => (string) $element->icon,
'numphotos' => (string) $gphoto->numphotos,
'nickname' => (string) $gphoto->nickname
));
// Add the photo list
$items = new ArrayList();
foreach ($element->entry as $entry)
$items->push(self::photoMetadata($entry));
$data->setField('items', $items);
return $data;
}
/**
/**
* Build a Picasaweb URL.
*
* Entry point for building the canonical Picasaweb URL from its
* components.
* Entry point for building the Picasaweb URL from its components
* conforming to the Picasaweb API version 2.0.
*
* @param String $user The Picasaweb user
* @param String $album The album id (or its canonical title),
* @param String $album The album id (or its URL title),
* optional for photo URLs.
* @param String $photo The photo id: leave it null for albums.
* @return String The URL or null on errors.
* @return String The API URL or null on errors.
*/
private static function url($user, $album, $photo = null) {
private static function apiUrl($user, $album, $photo = null) {
$url = null;
// The user is always required by the Picasaweb API
if (isset($user)) {
$domain = 'http://picasaweb.google.com';
$user = "/user/$user";
isset($album) and $album = self::isId($album) ? "/albumid/$album" : "/album/$album";
isset($photo) and $photo = self::isId($photo) ? "/photoid/$photo" : "/photo/$photo";
empty($album) or $album = self::isId($album) ? "/albumid/$album" : "/album/$album";
empty($photo) or $photo = self::isId($photo) ? "/photoid/$photo" : "/photo/$photo";
if (isset($photo)) {
// Photo URL
return $url;
}
public static function photo($url) {
$tree = self::xmlFetch($url);
/**
* Render a Picasaweb photo or album.
*
* Given the Picasaweb components, renders the requested element
* as an HTML chunk that represents the specific album or photo.
* If $photo is not empty, a photo will be rendered, otherwise an
* album will be.
*
* In case of error (the Picasaweb server is down, the API has
* changed or $url is invalid), null is returned.
*
* @param String $user The Picasaweb user
* @param String $album The album id (or its URL title),
* optional for photo URLs.
* @param String $photo The photo id: leave it null for albums.
* @param Array $fields Associative array of additional fields
* to pass to the HTML renderer.
* @return String A ready to use HTML chunk or null on errors.
*/
public static function render($user, $album, $photo = null, $fields = array()) {
$api_url = self::apiUrl($user, $album, $photo);
$tree = self::xmlFetch($api_url);
if (empty($tree))
return null;
return self::photoMetadata($tree);
}
public static function album($url) {
$tree = self::xmlFetch($url);
if (empty($tree))
return null;
if (empty($photo)) {
// Album rendering
$data = self::albumMetadata($tree);
$viewer = new SSViewer('Picasaweb_album');
} else {
// Photo rendering
$data = self::photoMetadata($tree);
$viewer = new SSViewer('Picasaweb_photo');
}
$data = self::albumMetadata($tree);
if (empty($data))
return null;
// Add the photo list
$items = new ArrayList();
foreach ($tree->entry as $element)
$items->push(self::photoMetadata($element));
$data->setField('items', $items);
// Merge additional fields passed in from the caller
foreach ($fields as $key => $value)
$data->setField($key, $value);
return $data;
return $data->renderWith($viewer);
}
/**
* depends on the format of the URL: if it has a fragment identifier
* it is a photo, otherwise it is an album.
*
* In case of error (the Picasaweb server is down, the API has
* In case of error (the Picasaweb server is down, the API has
* changed or $url is invalid), null is returned.
*
* @param String $url The URL to render.
* @returns String A ready to use HTML chunk or null on errors.
* @param String $url The URL to render.
* @return String A ready to use HTML chunk or null on errors.
*/
public static function render($url) {
public static function renderUrl($url) {
// Try to clean up a bit the Picasaweb URL mess
$url = preg_replace('"\?[^#]*"', '', $url);
$album = @$match[2];
$photo = @$match[4];
if (empty($user) || empty($album)) {
// Both user and album are required
return null;
} elseif (empty($photo)) {
$viewer = new SSViewer('Picasaweb_album');
$data = self::album(self::url($user, $album));
} else {
$viewer = new SSViewer('Picasaweb_photo');
$data = self::photo(self::url($user, $album, $photo));
}
if (is_null($data))
return null;
// $url cannot be embedded as is: if included in a template it
// will be expanded recursively. This is the rationale behind
// the following scheme stripping.
$data->setField('url', preg_replace('"https?://"', '', $url));
$fields = array('url' => preg_replace('"https?://"', '', $url));
return $data->renderWith($viewer);
return self::render($user, $album, $photo, $fields);
}
public static function shortcode($arguments, $url) {
$result = self::render($url);
/**
* Callback for shortcode handling.
*
* This allows to enable Picasaweb shortcodes on SilverStripe by
* adding the following code to your _config.php:
*
* ShortcodeParser::get()->register('picasa', array('Picasaweb', 'shortcode'));
*
* @param Array $args Associative array of key/value pairs.
* @param String $url Text contained inside the tag.
* @return String The rendered HTML chunk or null on errors.
*/
public static function shortcode($args, $url) {
$result = self::renderUrl($url);
if (is_null($result)) {
$url = htmlentities($url, ENT_COMPAT, 'UTF-8');
return "<div>Invalid PicasaWeb url given ($url)</div>";
class PicasawebExtension extends DataExtension {
/**
* Callback for Picasaweb URL expansion.
*
* Used by onBeforeWrite() to expand *only* the Picasaweb links.
*
* @param Array $matches The result of preg_match().
* @return String The rendered HTML chunk for Picasaweb
* links the original link otherwise.
*/
private static function renderer($matches) {
// Paranoic check, because in PHP we don't trust...
if (! isset($matches, $matches[0]))
return '';
$url = $matches[0];
$html = Picasaweb::render($url);
$html = Picasaweb::renderUrl($url);
return isset($html) ? $html : $url;
}
/**
* Expand Picasaweb links before the database registration.
*
* Checks all the (http) links and expands to a suitable HTML
* chunk (customizable throught templates) the Picasaweb ones.
*
* A Picasaweb link is supposed to be a *user* link, that is the
* URL shown while browsing the Picasaweb site.
*/
function onBeforeWrite() {
parent::onBeforeWrite();

Archive Download the corresponding diff file

Branches