コード例 #1
0
 function update()
 {
     $items = array();
     $cachefile = '/tmp/feide-services.json';
     if (file_exists($cachefile)) {
         $services = json_decode(file_get_contents($cachefile), true);
     } else {
         $url = 'https://api.feide.no/spinfo/0/all';
         $data = file_get_contents($url);
         file_put_contents($cachefile, $data);
         $services = json_decode($data, true);
     }
     /**
      * Load special file based upon Kind ID to entityID from kundeportalen / KIND.
      */
     global $BASE;
     $mapFile = dirname($BASE) . '/etc/entityIDmapping.json';
     $entityIDmapping = json_decode(file_get_contents($mapFile), true);
     /**
      * Load metadata...
      */
     $metadataFile = dirname($BASE) . '/var/dynamic/metadata-prod/saml20-sp-remote.php';
     require_once $metadataFile;
     $metadataId2entityID = array();
     foreach ($metadata as $entityID => $meta) {
         if (isset($meta['feide:kindID'])) {
             if (!isset($metadataId2entityID[$meta['feide:kindID']])) {
                 $metadataId2entityID[$meta['feide:kindID']] = array();
             }
             $metadataId2entityID[$meta['feide:kindID']][] = $entityID;
             // echo "entity id " . $meta['feide:kindID'] . '    ' . $entityID . "\n";
         }
     }
     // print_r($metadataId2entityID);
     // exit;
     $logocache = new LogoCache('feide');
     $items = array();
     foreach ($services as $k => $d) {
         // If we found a mapping from Kind ID to entities based upon metadat, use that
         if (isset($metadataId2entityID[$d['id']])) {
             $d['entityIDs'] = $metadataId2entityID[$d['id']];
             // If not, then use the mapping found in the external file.
         } else {
             $entityIDs = $this->getEntityIDs($d['id'], $entityIDmapping);
             if (!empty($entityIDs)) {
                 $d['entityIDs'] = $entityIDs;
             } else {
                 continue;
             }
             foreach ($entityIDs as $entityID) {
                 if (isset($metadata[$entityID]) && isset($metadata[$entityID]['logo'])) {
                     // feide/splogos/eu-supply_logo.jpg
                     if (preg_match('|feide/splogos/(.*)|', $metadata[$entityID]['logo'], $matches)) {
                         $d['logo'] = $matches[1];
                     }
                 }
             }
         }
         // $entityIDhash = sha1(join(',', $entityIDs));
         if (isset($d['logo'])) {
             // echo " " . $d['id'] . " logo => " . $d['logo'] . "\n";
             $src = dirname($BASE) . '/var/dynamic/splogos/' . $d['logo'];
             if (!file_exists($src)) {
                 DiscoUtils::error('Could not find local logo file ' . $src);
             } else {
                 $id = 'feide:sp:' . $d['id'];
                 $meta = array('type' => 'feide-sp');
                 if (count($entityIDs) === 1) {
                     $meta['entityID'] = $entityIDs[0];
                 }
                 // $id, $src, $meta, $localFile = false)
                 $ok = $logocache->getLogoURL($id, $src, $meta, true);
                 // $ok = $logocache->getLogo($this->entityId, $this->feed, $cl);
                 if (!empty($ok)) {
                     $d['icon'] = $ok;
                 }
             }
         } else {
             DiscoUtils::debug('No logo available for ' . $d['id']);
             // echo " " . $d['id'] . " ----- ----- ----- \n";
         }
         // echo "Dump"; print_r($d);
         // if ($i++ > 3) exit;
         // print_r($services[$k]);
         $item = new FeideService($d);
         $items[] = $item;
     }
     function arrSum($a, $b)
     {
         $res = array();
         foreach ($a as $k => $v) {
             if (isset($b[$k]) && isset($a[$k])) {
                 $res[] = $a[$k] + $b[$k];
             } else {
                 if (isset($a[$k])) {
                     $res[] = $a[$k];
                 } else {
                     $res[] = $b[$k];
                 }
             }
         }
         return $res;
     }
     // exit;
     $stats = $this->getStatisticsTotal();
     $statsfull = $this->getStatsAllWeeks(array_keys($stats));
     // print_r($statsfull); exit;
     foreach ($items as $item) {
         $entityIDs = $item->get('entityIDs');
         $total = 0;
         foreach ($entityIDs as $entityID) {
             if (isset($stats[$entityID])) {
                 $total += $stats[$entityID];
             }
         }
         $item->set('statistics', $total);
         $fullStat = array();
         foreach ($entityIDs as $entityID) {
             if (isset($statsfull[$entityID])) {
                 $fullStat = arrSum($statsfull[$entityID], $total);
                 // $total += $stats[$entityID];
             }
         }
         $item->set('statistics-full', $fullStat);
         // $item->save();
         // print_r($item); exit;
     }
     // exit;
     foreach ($items as $item) {
         $item->save();
         // print_r($item); exit;
     }
 }
コード例 #2
0
ファイル: FeedItem.php プロジェクト: NIIF/DiscoJuice-Backend
 protected function getLogo(&$data, $m)
 {
     $logocache = new LogoCache();
     // print_r($m);
     if (!empty($m['UIInfo']) && !empty($m['UIInfo']['Logo'])) {
         $cl = self::getPreferredLogo($m['UIInfo']['Logo']);
         // error_log('Preferred logo: ' . var_export($cl, true));
         if (!empty($cl)) {
             $src = $cl['url'];
             $id = sha1($this->feed . '|' . $this->entityId);
             $meta = array('entityId' => $this->entityId, 'feed' => $this->feed);
             $ok = $logocache->getLogoURL($id, $src, $meta);
             // $ok = $logocache->getLogo($this->entityId, $this->feed, $cl);
             if (!empty($ok)) {
                 $data['icon'] = $ok;
             }
             // $cached = $logocache->getCachedLogo($cl);
             // if (!empty($cached)) {
             // 	DiscoUtils::debug("Got logo stored locally: " . $cached);
             // 	// $data['icon'] = $cached;
             // }
         }
     } else {
         DiscoUtils::debug('Skipping logo for  ' . $data['title']['en'] . ' because it was missing');
     }
 }