Feed.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /*
  3. @nom: Feed
  4. @auteur: Idleman (http://blog.idleman.fr)
  5. @description: Classe de gestion des flux RSS/ATOM
  6. */
  7. class Feed extends MysqlEntity{
  8. protected $id,$name,$url,$events=array(),$description,$website,$folder,$lastupdate,$isverbose,$lastSyncInError;
  9. protected $TABLE_NAME = 'feed';
  10. protected $object_fields =
  11. array(
  12. 'id'=>'key',
  13. 'name'=>'string',
  14. 'description'=>'longstring',
  15. 'website'=>'longstring',
  16. 'url'=>'longstring',
  17. 'lastupdate'=>'string',
  18. 'folder'=>'integer',
  19. 'isverbose'=>'boolean',
  20. 'lastSyncInError'=>'boolean',
  21. );
  22. protected $object_fields_index =
  23. array(
  24. 'folder'=>'index'
  25. );
  26. protected $error = '';
  27. function __construct($name=null,$url=null){
  28. $this->name = $name;
  29. $this->url = $url;
  30. parent::__construct();
  31. }
  32. /** @TODO: ne faire qu'un seul chargement avec SimplePie et récupérer les
  33. même informations. Mettre le chargement en cache au moins d'une méthode
  34. loadLeed() qui ne chargera qu'une seule fois. Voire même en déclenchement
  35. retardé, au dernier moment. */
  36. function getInfos(){
  37. $xml = @simplexml_load_file($this->url);
  38. if($xml!=false){
  39. $n = $xml->xpath('channel/title'); $this->name = $n[0];
  40. $d = $xml->xpath('channel/description'); $this->description = $d[0];
  41. $w = $xml->xpath('channel/link'); $this->website = $w[0];
  42. }
  43. }
  44. function getError() { return $this->error; }
  45. function getLastSyncInError() { return $this->lastSyncInError; }
  46. /*@TODO: fournir un extrait quand il 'y a pas de description. De même pour les médias.
  47. @TODO: SimplePie remplace "é" par "&eacute;", il ne devrait pas le faire.
  48. J'ai testé set_stupidly_fast(true) sans succès.
  49. Il encadre les descriptions avec <div>, absents dans le source du flux.
  50. @TODO: la vérification de doublon est sous la responsabilité de l'appelant.
  51. Il serait peut-être utile de faire une méthode add() qui vérifie, plante si
  52. nécessaire, et appelle parse(). Impossible de vérifier dans parse() même
  53. car elle est appelée aussi pour autre chose que l'ajout.
  54. */
  55. function parse($syncId,&$nbEvents =0, $enableCache=true, $forceFeed=false){
  56. $nbEvents = 0;
  57. assert('is_int($syncId) && $syncId>0');
  58. if (empty($this->id) || 0 == $this->id) {
  59. /* Le flux ne dispose pas pas d'id !. Ça arrive si on appelle
  60. parse() sans avoir appelé save() pour un nouveau flux.
  61. @TODO: un create() pour un nouveau flux ? */
  62. $msg = 'Empty or null id for a feed! '
  63. .'See '.__FILE__.' on line '.__LINE__;
  64. error_log($msg, E_USER_ERROR);
  65. die($msg); // Arrêt, sinon création événements sans flux associé.
  66. }
  67. $feed = new SimplePie();
  68. $feed->enable_cache($enableCache);
  69. $feed->force_feed($forceFeed);
  70. $feed->set_feed_url($this->url);
  71. $feed->set_useragent('Mozilla/4.0 Leed (LightFeed Aggregator) '.LEED_VERSION_NAME.' by idleman http://projet.idleman.fr/leed');
  72. $this->lastSyncInError = 0;
  73. $this->lastupdate = $_SERVER['REQUEST_TIME'];
  74. if (!$feed->init()) {
  75. $this->error = $feed->error;
  76. $this->lastSyncInError = 1;
  77. $this->save();
  78. return false;
  79. }
  80. $feed->handle_content_type(); // UTF-8 par défaut pour SimplePie
  81. if($this->name=='') $this->name = $feed->get_title();
  82. if($this->name=='') $this->name = $this->url;
  83. $this->website = $feed->get_link();
  84. $this->description = $feed->get_description();
  85. $items = $feed->get_items();
  86. $eventManager = new Event();
  87. $events = array();
  88. $iEvents = 0;
  89. foreach($items as $item){
  90. // Ne retient que les 100 premiers éléments de flux.
  91. if ($iEvents++>=100) break;
  92. // Si le guid existe déjà, on évite de le reparcourir.
  93. $alreadyParsed = $eventManager->load(array('guid'=>$item->get_id(), 'feed'=>$this->id));
  94. if (isset($alreadyParsed)&&($alreadyParsed!=false)) {
  95. $events[]=$alreadyParsed->getId();
  96. continue;
  97. }
  98. // Initialisation des informations de l'événement (élt. de flux)
  99. $event = new Event();
  100. $event->setSyncId($syncId);
  101. $event->setGuid($item->get_id());
  102. $event->setTitle($item->get_title());
  103. $event->setPubdate(
  104. ''==$item->get_date()
  105. ? $this->lastupdate
  106. : $item->get_date()
  107. );
  108. $event->setCreator(
  109. ''==$item->get_author()
  110. ? ''
  111. : $item->get_author()->name
  112. );
  113. $event->setLink($item->get_permalink());
  114. $event->setFeed($this->id);
  115. $event->setUnread(1); // inexistant, donc non-lu
  116. $enclosure = $this->getEnclosureHtml($item->get_enclosure());
  117. $event->setContent($item->get_content().$enclosure);
  118. $event->setDescription($item->get_description().$enclosure);
  119. if(trim($event->getDescription())=='')
  120. $event->setDescription(
  121. substr($event->getContent(),0,300)
  122. .'…<br><a href="'.$event->getLink()
  123. .'">Lire la suite de l\'article</a>'
  124. );
  125. if(trim($event->getContent())=='')
  126. $event->setContent($event->getDescription());
  127. $event->setCategory($item->get_category());
  128. $event->save();
  129. $nbEvents++;
  130. }
  131. $listid = "";
  132. foreach($events as $item){
  133. $listid.=','.$item;
  134. }
  135. $query='UPDATE `'.MYSQL_PREFIX.'event` SET syncId='.$syncId.' WHERE id in (0'.$listid.');';
  136. $myQuery = $this->customQuery($query);
  137. $this->save();
  138. return true;
  139. }
  140. protected function getEnclosureHtml($enclosure) {
  141. global $i18n;
  142. $html = '';
  143. if($enclosure!=null && $enclosure->link!=''){
  144. $enclosureName = substr(
  145. $enclosure->link,
  146. strrpos($enclosure->link, '/')+1,
  147. strlen($enclosure->link)
  148. );
  149. $enclosureArgs = strpos($enclosureName, '?');
  150. if($enclosureArgs!==false)
  151. $enclosureName = substr($enclosureName,0,$enclosureArgs);
  152. $enclosureFormat = isset($enclosure->handler)
  153. ? $enclosure->handler
  154. : substr($enclosureName, strrpos($enclosureName,'.')+1);
  155. $html ='<div class="enclosure"><h1>Fichier média :</h1>';
  156. $enclosureType = $enclosure->get_type();
  157. if (strpos($enclosureType, 'image/') === 0) {
  158. $html .= '<img src="' . $enclosure->link . '" />';
  159. } elseif (strpos($enclosureType, 'audio/') === 0) {
  160. $html .= '<audio src="' . $enclosure->link . '" preload="none" controls>'._t('BROWSER_AUDIO_ELEMENT_NOT_SUPPORTED').'</audio>';
  161. } elseif (strpos($enclosureType, 'video/') === 0) {
  162. $html .= '<video src="' . $enclosure->link . '" preload="none" controls>'._t('BROWSER_VIDEO_ELEMENT_NOT_SUPPORTED').'</video>';
  163. } else {
  164. $html .= '<a href="'.$enclosure->link.'"> '.$enclosureName.'</a>';
  165. }
  166. $html .= ' <span>(Format '.strtoupper($enclosureFormat).', '.Functions::convertFileSize($enclosure->length).')</span></div>';
  167. }
  168. return $html;
  169. }
  170. function removeOldEvents($maxEvent, $currentSyncId){
  171. if ($maxEvent<=0) return;
  172. $eventManager = new Event();
  173. $nbLines = $eventManager->rowCount(array(
  174. 'feed'=>$this->id,
  175. 'unread'=>0,
  176. 'favorite'=>0,
  177. ));
  178. $limit = $nbLines - $maxEvent;
  179. if ($limit<=0) return;
  180. $tableEvent = '`'.MYSQL_PREFIX."event`";
  181. $query = "
  182. DELETE FROM {$tableEvent} WHERE feed={$this->id} AND favorite!=1 AND unread!=1 AND syncId!={$currentSyncId} ORDER BY pubdate ASC LIMIT {$limit}
  183. ";
  184. ///@TODO: escape the variables inside mysql
  185. $this->customQuery($query);
  186. }
  187. function setId($id){
  188. $this->id = $id;
  189. }
  190. function getDescription(){
  191. return $this->description;
  192. }
  193. function setDescription($description){
  194. $this->description = $description;
  195. }
  196. function getWebSite(){
  197. return $this->website;
  198. }
  199. function setWebSite($website){
  200. $this->website = $website;
  201. }
  202. function getId(){
  203. return $this->id;
  204. }
  205. function getUrl(){
  206. return $this->url;
  207. }
  208. function setUrl($url){
  209. $this->url = $url;
  210. }
  211. function getName(){
  212. return (trim($this->name)!='' ? $this->name:$this->url);
  213. }
  214. function setName($name){
  215. $this->name = $name;
  216. }
  217. function getEvents($start=0,$limit=10000,$order,$columns='*',$filter=false){
  218. $filter['feed'] = $this->getId();
  219. $eventManager = new Event();
  220. $events = $eventManager->loadAllOnlyColumn($columns,$filter,$order,$start.','.$limit);
  221. return $events;
  222. }
  223. function countUnreadEvents(){
  224. $unreads = array();
  225. $results = Feed::customQuery("SELECT COUNT(`".MYSQL_PREFIX."event`.`id`), `".MYSQL_PREFIX."event`.`feed` FROM `".MYSQL_PREFIX."event` WHERE `".MYSQL_PREFIX."event`.`unread` = 1 GROUP BY `".MYSQL_PREFIX."event`.`feed`") ;
  226. if($results!=false){
  227. $total = 0;
  228. while($item = $results->fetch_array()){
  229. $unreads[$item[1]] = $item[0];
  230. $total += $item[0];
  231. }
  232. $unreads['total'] = $total;
  233. }
  234. return $unreads;
  235. }
  236. function getFeedsPerFolder(){
  237. $feedsFolderMap = array();
  238. $feedsIdMap = array();
  239. $results = Feed::customQuery("SELECT `".MYSQL_PREFIX."feed`.`name` AS name, `".MYSQL_PREFIX."feed`.`id` AS id, `".MYSQL_PREFIX."feed`.`url` AS url, `".MYSQL_PREFIX."folder`.`id` AS folder FROM `".MYSQL_PREFIX."feed` INNER JOIN `".MYSQL_PREFIX."folder` ON ( `".MYSQL_PREFIX."feed`.`folder` = `".MYSQL_PREFIX."folder`.`id` ) ORDER BY `".MYSQL_PREFIX."feed`.`name` ;");
  240. if($results!=false){
  241. while($item = $results->fetch_array()){
  242. $name = $item['name'];
  243. $feedsIdMap[$item['id']]['name'] = $name;
  244. $feedsFolderMap[$item['folder']][$item['id']]['id'] = $item['id'];
  245. $feedsFolderMap[$item['folder']][$item['id']]['name'] = $name;
  246. $feedsFolderMap[$item['folder']][$item['id']]['url'] = $item['url'];
  247. }
  248. }
  249. $feeds['folderMap'] = $feedsFolderMap;
  250. $feeds['idMap'] = $feedsIdMap;
  251. return $feeds;
  252. }
  253. function getFolder(){
  254. return $this->folder;
  255. }
  256. function setFolder($folder){
  257. $this->folder = $folder;
  258. }
  259. function getLastupdate(){
  260. return $this->lastUpdate;
  261. }
  262. function setLastupdate($lastupdate){
  263. $this->lastupdate = $lastupdate;
  264. }
  265. function getIsverbose(){
  266. return $this->isverbose;
  267. }
  268. function setIsverbose($isverbose){
  269. $this->isverbose = $isverbose;
  270. }
  271. /** @returns vrai si l'url n'est pas déjà connue .*/
  272. function notRegistered() {
  273. return $this->rowCount(array('url' => $this->url)) == 0;
  274. }
  275. public function synchronize($feeds, $syncTypeStr, $commandLine, $configurationManager, $start) {
  276. $currentDate = date('d/m/Y H:i:s');
  277. if (!$commandLine) {
  278. echo "<p>{$syncTypeStr} {$currentDate}</p>\n";
  279. echo "<dl>\n";
  280. } else {
  281. echo "{$syncTypeStr}\t{$currentDate}\n";
  282. }
  283. $maxEvents = $configurationManager->get('feedMaxEvents');
  284. $nbErrors = 0;
  285. $nbOk = 0;
  286. $nbTotal = 0;
  287. $localTotal = 0; // somme de tous les temps locaux, pour chaque flux
  288. $nbTotalEvents = 0;
  289. $syncId = time();
  290. $enableCache = ($configurationManager->get('synchronisationEnableCache')=='')?0:$configurationManager->get('synchronisationEnableCache');
  291. $forceFeed = ($configurationManager->get('synchronisationForceFeed')=='')?0:$configurationManager->get('synchronisationForceFeed');
  292. foreach ($feeds as $feed) {
  293. $nbEvents = 0;
  294. $nbTotal++;
  295. $startLocal = microtime(true);
  296. $parseOk = $feed->parse($syncId,$nbEvents, $enableCache, $forceFeed);
  297. $parseTime = microtime(true)-$startLocal;
  298. $localTotal += $parseTime;
  299. $parseTimeStr = number_format($parseTime, 3);
  300. if ($parseOk) { // It's ok
  301. $errors = array();
  302. $nbTotalEvents += $nbEvents;
  303. $nbOk++;
  304. } else {
  305. // tableau au cas où il arrive plusieurs erreurs
  306. $errors = array($feed->getError());
  307. $nbErrors++;
  308. }
  309. $feedName = Functions::truncate($feed->getName(),30);
  310. $feedUrl = $feed->getUrl();
  311. $feedUrlTxt = Functions::truncate($feedUrl, 30);
  312. if ($commandLine) {
  313. echo date('d/m/Y H:i:s')."\t".$parseTimeStr."\t";
  314. echo "{$feedName}\t{$feedUrlTxt}\n";
  315. } else {
  316. if (!$parseOk)
  317. echo '<div class="errorSync">';
  318. else
  319. echo '<div>';
  320. echo "<dt><i>{$parseTimeStr}s</i> | <a target='_blank' rel='noopener noreferrer' href='{$feedUrl}'>{$feedName}</a></dt>\n";
  321. }
  322. foreach($errors as $error) {
  323. if ($commandLine)
  324. echo "$error\n";
  325. else
  326. echo "<dd>$error</dd>\n";
  327. }
  328. if (!$parseOk && !$commandLine) echo '</div>';
  329. // if ($commandLine) echo "\n";
  330. $feed->removeOldEvents($maxEvents, $syncId);
  331. }
  332. assert('$nbTotal==$nbOk+$nbErrors');
  333. $totalTime = microtime(true)-$start;
  334. assert('$totalTime>=$localTotal');
  335. $totalTimeStr = number_format($totalTime, 3);
  336. $currentDate = date('d/m/Y H:i:s');
  337. if ($commandLine) {
  338. echo "\t{$nbErrors}\t"._t('ERRORS')."\n";
  339. echo "\t{$nbOk}\t"._t('GOOD')."\n";
  340. echo "\t{$nbTotal}\t"._t('AT_TOTAL')."\n";
  341. echo "\t$currentDate\n";
  342. echo "\t$nbTotalEvents\n";
  343. echo "\t{$totalTimeStr}\t"._t('SECONDS')."\n";
  344. } else {
  345. echo "</dl>\n";
  346. echo "<div id='syncSummary'\n";
  347. echo "<p>"._t('SYNCHRONISATION_COMPLETE')."</p>\n";
  348. echo "<ul>\n";
  349. echo "<li>{$nbErrors}\t"._t('ERRORS')."\n";
  350. echo "<li>{$nbOk}\t"._t('GOOD')."\n";
  351. echo "<li>{$nbTotal}\t"._t('AT_TOTAL')."\n";
  352. echo "<li>{$totalTimeStr}\t"._t('SECONDS')."\n";
  353. echo "<li>{$nbTotalEvents}\t"._t('NEW_ARTICLES')."\n";
  354. echo "</ul>\n";
  355. echo "</div>\n";
  356. }
  357. if (!$commandLine) {
  358. echo '</div></body></html>';
  359. }
  360. }
  361. }
  362. ?>