1*307c6980SAndreas Gohr<?php 2*307c6980SAndreas Gohr 3*307c6980SAndreas Gohrnamespace dokuwiki\plugin\doi\Resolver; 4*307c6980SAndreas Gohr 5*307c6980SAndreas Gohrabstract class AbstractResolver 6*307c6980SAndreas Gohr{ 7*307c6980SAndreas Gohr 8*307c6980SAndreas Gohr 9*307c6980SAndreas Gohr protected $defaultResult = [ 10*307c6980SAndreas Gohr 'id' => '', 11*307c6980SAndreas Gohr 'type' => '', 12*307c6980SAndreas Gohr 'title' => '', 13*307c6980SAndreas Gohr 'url' => '', 14*307c6980SAndreas Gohr 'authors' => [], 15*307c6980SAndreas Gohr 'publisher' => '', 16*307c6980SAndreas Gohr 'published' => '', 17*307c6980SAndreas Gohr 'journal' => '', 18*307c6980SAndreas Gohr 'volume' => '', 19*307c6980SAndreas Gohr 'issue' => '', 20*307c6980SAndreas Gohr 'page' => '', 21*307c6980SAndreas Gohr ]; 22*307c6980SAndreas Gohr 23*307c6980SAndreas Gohr /** 24*307c6980SAndreas Gohr * The extension used for the cache file 25*307c6980SAndreas Gohr * 26*307c6980SAndreas Gohr * Defaults to class name (without Resolver) in lowercase 27*307c6980SAndreas Gohr * 28*307c6980SAndreas Gohr * @return string 29*307c6980SAndreas Gohr */ 30*307c6980SAndreas Gohr protected function getCacheExtension() 31*307c6980SAndreas Gohr { 32*307c6980SAndreas Gohr return ''; 33*307c6980SAndreas Gohr } 34*307c6980SAndreas Gohr 35*307c6980SAndreas Gohr /** 36*307c6980SAndreas Gohr * Get a URL to use as a fallback if data fetching fails 37*307c6980SAndreas Gohr * 38*307c6980SAndreas Gohr * Should be a generic web serach for the given ID 39*307c6980SAndreas Gohr * 40*307c6980SAndreas Gohr * @param string|int $id 41*307c6980SAndreas Gohr * @return string 42*307c6980SAndreas Gohr */ 43*307c6980SAndreas Gohr abstract public function getFallbackURL($id); 44*307c6980SAndreas Gohr 45*307c6980SAndreas Gohr /** 46*307c6980SAndreas Gohr * Clean the given ID to a standard format 47*307c6980SAndreas Gohr * 48*307c6980SAndreas Gohr * @param string $id 49*307c6980SAndreas Gohr * @return string|int 50*307c6980SAndreas Gohr */ 51*307c6980SAndreas Gohr abstract public function cleanID($id); 52*307c6980SAndreas Gohr 53*307c6980SAndreas Gohr /** 54*307c6980SAndreas Gohr * Return the data in standard format 55*307c6980SAndreas Gohr * 56*307c6980SAndreas Gohr * @param string|int $id 57*307c6980SAndreas Gohr * @return array 58*307c6980SAndreas Gohr * @throws \Exception if getting data fails 59*307c6980SAndreas Gohr */ 60*307c6980SAndreas Gohr abstract public function getData($id); 61*307c6980SAndreas Gohr 62*307c6980SAndreas Gohr /** 63*307c6980SAndreas Gohr * Fetch the info for the given ID 64*307c6980SAndreas Gohr * 65*307c6980SAndreas Gohr * @param string|int $id 66*307c6980SAndreas Gohr * @return false|array 67*307c6980SAndreas Gohr */ 68*307c6980SAndreas Gohr abstract protected function fetchData($id); 69*307c6980SAndreas Gohr 70*307c6980SAndreas Gohr /** 71*307c6980SAndreas Gohr * Get the (potentially cached) data for the given ID 72*307c6980SAndreas Gohr * 73*307c6980SAndreas Gohr * Caches get refreshed when the resolver class has been updated 74*307c6980SAndreas Gohr * 75*307c6980SAndreas Gohr * @param string|int $id 76*307c6980SAndreas Gohr * @return array|false 77*307c6980SAndreas Gohr * @throws \Exception if getting data fails 78*307c6980SAndreas Gohr */ 79*307c6980SAndreas Gohr protected function fetchCachedData($id) 80*307c6980SAndreas Gohr { 81*307c6980SAndreas Gohr $class = get_class($this); 82*307c6980SAndreas Gohr $class = substr($class, strrpos($class, '\\') + 1); 83*307c6980SAndreas Gohr $file = DOKU_PLUGIN . 'doi/Resolver/' . $class . '.php'; 84*307c6980SAndreas Gohr 85*307c6980SAndreas Gohr $ext = trim($this->getCacheExtension(), '.'); 86*307c6980SAndreas Gohr if ($ext === '') { 87*307c6980SAndreas Gohr $ext = strtolower(str_replace('Resolver', '', $class)); 88*307c6980SAndreas Gohr } 89*307c6980SAndreas Gohr 90*307c6980SAndreas Gohr 91*307c6980SAndreas Gohr $cache = getCacheName($id, '.' . $ext . '.json'); 92*307c6980SAndreas Gohr if (@filemtime($cache) > filemtime($file)) { 93*307c6980SAndreas Gohr return json_decode(file_get_contents($cache), true); 94*307c6980SAndreas Gohr } 95*307c6980SAndreas Gohr 96*307c6980SAndreas Gohr $result = $this->fetchData($id); 97*307c6980SAndreas Gohr file_put_contents($cache, json_encode($result)); 98*307c6980SAndreas Gohr return $result; 99*307c6980SAndreas Gohr } 100*307c6980SAndreas Gohr} 101