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