selfpublish /plugins/sfFeed2Plugin/lib/sfFeedPeer.class.php

Language PHP Lines 556
MD5 Hash 572fe0ec662a7545ca1a8a2be4256594 Estimated Cost $10,567 (why?)
Repository http://selfpublish.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php

/*
 * This file is part of the sfFeed2 package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 * (c) 2004-2007 Francois Zaninotto <francois.zaninotto@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfFeedPeer.
 *
 * @package    sfFeed2
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author     Francois Zaninotto <francois.zaninotto@symfony-project.com>
 */
class sfFeedPeer
{
  /**
   * Retrieve a new sfFeed implementation instance.
   *
   * @param string A sfFeed implementation name
   *
   * @return sfFeed A sfFeed implementation instance
   *
   * @throws sfFactoryException If a new syndication feed implementation instance cannot be created
   */
  public static function newInstance($format = '')
  {
    try
    {
      $class = 'sf'.ucfirst($format).'Feed';

      // the class exists
      $object = new $class();

      if (!($object instanceof sfFeed))
      {
          // the class name is of the wrong type
          $error = 'Class "%s" is not of the type sfFeed';
          $error = sprintf($error, $class);

          throw new sfFactoryException($error);
      }

      return $object;
    }
    catch (sfException $e)
    {
      $e->printStackTrace();
    }
  }

  /**
   * Retrieve a new sfFeed implementation instance, populated from a web feed.
   * The class of the returned instance depends on the nature of the web feed.
   * This method uses the sfWebBrowser plugin.
   *
   * @param string A web feed URI
   *
   * @return sfFeed A sfFeed implementation instance
   */
  public static function createFromWeb($uri, $options = array())
  {
    if(isset($options['adapter']))
    {
      $browser = new sfWebBrowser(array(), $options['adapter'], isset($options['adapter_options']) ? $options['adapter_options'] : array());
    }
    else
    {
      $browser = new sfWebBrowser();
    }
    $browser->setUserAgent(isset($options['userAgent']) ? $options['userAgent'] : 'sfFeedReader/0.9');
    if($browser->get($uri)->responseIsError())
    {
      $error = 'The given URL (%s) returns an error (%s: %s)';
      $error = sprintf($error, $uri, $browser->getResponseCode(), $browser->getResponseMessage());
      throw new Exception($error);
    }
    $feedString = $browser->getResponseText();

    return self::createFromXml($feedString, $uri);
  }

  /**
   * Retrieve a new sfFeed implementation instance, populated from a xml feed.
   * The class of the returned instance depends on the nature of the xml feed.
   *
   * @param string $feedString a feed as xml string
   * @param string A web feed URI
   *
   * @return sfFeed A sfFeed implementation instance
   */
  public static function createFromXml($feedString, $uri)
  {
    $feedClass = '';
    if(preg_match('/xmlns=[\"\'](http:\/\/www\.w3\.org\/2005\/Atom|http:\/\/purl\.org\/atom)/', $feedString))
    {
      $feedClass = 'sfAtom1Feed';
    }
    if(strpos($feedString, '<rss') !== false)
    {
      $feedClass = 'sfRssFeed';
    }
    if(strpos($feedString, '<rdf:RDF') !== false)
    {
      $feedClass = 'sfRss10Feed';
    }

    if($feedClass)
    {
      $object = new $feedClass();
      $object->setFeedUrl($uri);
      $object->fromXml($feedString);

      return $object;
    }
    else
    {
      throw new Exception('Impossible to decode feed format');
    }
  }

  /**
   * Merge the items from several feeds and retrieve a sfFeed instance.
   * Populated with all the items, and sorted.
   *
   * @param array an array of sfFeed objects
   * @param array an associative array of feed parameters
   *
   * @return sfFeed A sfFeed implementation instance
   */
  public static function aggregate($feeds, $parameters = array())
  {
    // merge all items
    $feed_items = array();
    foreach($feeds as $feed)
    {
      foreach($feed->getItems() as $item)
      {
        $index = is_integer($item->getPubDate()) || ctype_digit($item->getPubDate()) ?  $item->getPubDate() : 0;
        while(isset($feed_items[$index]))
        {
          $index++;
        }
        $feed_items[$index] = $item;
      }
    }

    // when specified, sort items chronologically instead of reverse
    if (isset($parameters['sort']) && 'chronological' == $parameters['sort'])
    {
      ksort($feed_items);
    }
    else 
    {
      // default behaviour: sort in reverse chronological order 
      krsort($feed_items);
    }
    
    // limit the number of feed items to be added
    if(isset($parameters['limit']))
    {
      $feed_items = array_slice($feed_items, 0, $parameters['limit']);
    }

    // create a feed with these items
    $feed = self::newInstance(isset($parameters['format']) ? $parameters['format'] : '');
    $feed->initialize($parameters);
    foreach($feed_items as $item)
    {
      $origin_feed = clone $item->getFeed();
      $origin_feed->setItems();
      $feed->addItem($item);
      $item->setFeed($origin_feed);
    }

    return $feed;
  }

  /**
   * Populates a feed with items based on objects.
   * Inspects the available methods of the objects to populate items properties.
   *
   * @param array an array of objects
   * @param string A route name for building the URIs to the items
   * @param array An associative array of options
   *
   * @return sfFeed the current sfFeed object
   */
  public static function convertObjectsToItems($objects, $options = array())
  {
    $items = array();
    foreach($objects as $object)
    {
      $item = new sfFeedItem();

      // For each item property, check if an object method is provided,
      // and if not, guess it. Here is what it does for the link property
      if(isset($options['methods']['link']))
      {
        if($options['methods']['link'])
        {
          $item->setLink(call_user_func(array($object, $options['methods']['link'])));
        }
        else
        {
          $item->setLink('');
        }
      }
      else
      {
        $routeName = (isset($options['routeName'])) ? $options['routeName'] : '';
        $fallbackUrl = (isset($options['fallbackUrl'])) ? $options['fallbackUrl'] : '';
        $item->setLink(self::getItemFeedLink($object, $routeName, $fallbackUrl));
      }

      // For the other properties, it can be automated
      // Not as readable but definitely more concise
      $details = array('title', 'description', 'content', 'authorEmail', 'authorName', 'authorLink', 'pubdate', 'comments', 'uniqueId', 'enclosure', 'categories');
      foreach ($details as $detail)
      {
        $itemMethod = 'set'.ucfirst($detail);
        if (isset($options['methods'][$detail]))
        {
          if ($options['methods'][$detail])
          {
            if (is_array($options['methods'][$detail]))
            {
              call_user_func(array($item, $itemMethod), call_user_func_array(array($object, $options['methods'][$detail][0]), $options['methods'][$detail][1]));
            }
            else
            {
              call_user_func(array($item, $itemMethod), call_user_func(array($object, $options['methods'][$detail])));
            }
          }
          else
          {
            call_user_func(array($item, $itemMethod), '');
          }
        }
        else
        {
          call_user_func(array($item, $itemMethod), call_user_func(array('sfFeedPeer', 'getItemFeed'.ucfirst($detail)), $object));
        }
      }

      $items[] = $item;
    }

    return $items;
  }

  /**
   * Creates and populates a feed with items based on objects
   * This is a proxy method that combines calls to newInstance() and convertObjectsToItems()
   *
   * @param array an array of objects
   * @param array an associative array of feed parameters
   *
   * @return sfFeed A sfFeed implementation instance, containing the parameters and populated with the objects
   */
  public static function createFromObjects($objects, $options = array())
  {
    $feed = self::newInstance(isset($options['format']) ? $options['format'] : '');
    $feed->initialize($options);
    $options['fallbackUrl'] = $feed->getLink();
    $feed->addItems(self::convertObjectsToItems($objects, $options));

    return $feed;
  }

  private static function getItemFeedTitle($item)
  {
    foreach (array('getFeedTitle', 'getTitle', 'getName', '__toString') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    return '';
  }

  private static function getItemFeedLink($item, $routeName = '', $fallback_url = '')
  {
    if ($routeName)
    {
      if (method_exists('sfRouting', 'getInstance'))
      {
        $route = sfRouting::getInstance()->getRouteByName($routeName);
        $url = $route[0];
        $paramNames = $route[2];
        $defaults = $route[4];
      }
      else
      {
        $routes = sfContext::getInstance()->getRouting()->getRoutes();
        $route = $routes[substr($routeName, 1)];
        if($route instanceof sfRoute)
        {
          $url = $route->getPattern();
          $paramNames = array_keys($route->getVariables());
          $defaults = $route->getDefaults();
        }
        else
        {
          $url = $route[0];
          $paramNames = array_keys($route[2]);
          $defaults = $route[3];
        }
      }

      // we get all parameters
      $params = array();
      foreach ($paramNames as $paramName)
      {
        $value = null;
        $name = ucfirst(sfInflector::camelize($paramName));

        $found = false;
        foreach (array('getFeed'.$name, 'get'.$name) as $methodName)
        {
          if (method_exists($item, $methodName))
          {
            $value = $item->$methodName();
            $found = true;
            break;
          }
        }

        if (!$found)
        {
          if (array_key_exists($paramName, $defaults))
          {
            $value = $defaults[$paramName];
          }
          else
          {
            $error = 'Cannot find a "getFeed%s()" or "get%s()" method for object "%s" to generate URL with the "%s" route';
            $error = sprintf($error, $name, $name, get_class($item), $routeName);
            throw new sfException($error);
          }
        }

        $params[] = $paramName.'='.$value;
      }

      return sfContext::getInstance()->getController()->genUrl($routeName.($params ? '?'.implode('&', $params) : ''), true);
    }

    foreach (array('getFeedLink', 'getLink', 'getUrl') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return sfContext::getInstance()->getController()->genUrl($item->$methodName(), true);
      }
    }

    if ($fallback_url)
    {
      return sfContext::getInstance()->getController()->genUrl($fallback_url, true);
    }
    else
    {
      return sfContext::getInstance()->getController()->genUrl('/', true);
    }
  }

  private static function getItemFeedDescription($item)
  {
    foreach (array('getFeedDescription', 'getDescription', 'getBody') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    return '';
  }

  private static function getItemFeedContent($item)
  {
    foreach (array('getFeedContent', 'getContent', 'getHtmlBody', 'getBody') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    return '';
  }

  private static function getItemFeedUniqueId($item)
  {
    foreach (array('getFeedUniqueId', 'getUniqueId', 'getId') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    return '';
  }

  private static function getItemFeedAuthorEmail($item)
  {
    foreach (array('getFeedAuthorEmail', 'getAuthorEmail') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    // author as an object link
    if ($author = self::getItemFeedAuthor($item))
    {
      foreach (array('getEmail', 'getMail') as $methodName)
      {
        if (method_exists($author, $methodName))
        {
          return $author->$methodName();
        }
      }
    }

    return '';
  }

  private static function getItemFeedAuthorName($item)
  {
    foreach (array('getFeedAuthorName', 'getAuthorName') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    // author as an object link
    if ($author = self::getItemFeedAuthor($item))
    {
      foreach (array('getName', '__toString') as $methodName)
      {
        if (method_exists($author, $methodName))
        {
          return $author->$methodName();
        }
      }
    }

    return '';
  }

  private static function getItemFeedAuthorLink($item)
  {
    foreach (array('getFeedAuthorLink', 'getAuthorLink') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    // author as an object link
    if ($author = self::getItemFeedAuthor($item))
    {
      foreach (array('getLink') as $methodName)
      {
        if (method_exists($author, $methodName))
        {
          return $author->$methodName();
        }
      }
    }

    return '';
  }

  private static function getItemFeedAuthor($item)
  {
    foreach (array('getAuthor', 'getUser', 'getPerson') as $methodName)
    {
      if (method_exists($item, $methodName) && is_object($item->$methodName()))
      {
        return $item->$methodName();
      }
    }

    return null;
  }

  private static function getItemFeedPubdate($item)
  {
    foreach (array('getFeedPubdate', 'getPubdate', 'getCreatedAt', 'getDate', 'getPublishedAt') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName('U');
      }
    }

    return '';
  }

  private static function getItemFeedComments($item)
  {
    foreach (array('getFeedComments', 'getComments') as $methodName)
    {
      if (method_exists($item, $methodName))
      {
        return $item->$methodName();
      }
    }

    return '';
  }

  private static function getItemFeedCategories($item)
  {
    foreach (array('getFeedCategories', 'getCategories') as $methodName)
    {
      if (method_exists($item, $methodName) && is_array($item->$methodName()))
      {
        $cats = array();
        foreach ($item->$methodName() as $category)
        {
          $cats[] = (string) $category;
        }

        return $cats;
      }
    }

    return array();
  }

  private static function getItemFeedEnclosure($item)
  {
    if (method_exists($item, 'getFeedEnclosure'))
    {
      return $item->getFeedEnclosure();
    }

    return '';
  }

}
Back to Top