1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/** 9 * 10 */ 11class ZoteroLib extends TikiDb_Bridge 12{ 13 /** 14 * @return bool 15 */ 16 function is_authorized() 17 { 18 $oauthlib = TikiLib::lib('oauth'); 19 return $oauthlib->is_authorized('zotero'); 20 } 21 22 /** 23 * @param $tag 24 * @param int $limit 25 * @return array|bool 26 */ 27 function get_references($tag, $limit = 25) 28 { 29 global $prefs; 30 31 $subset = null; 32 if ($tag) { 33 $subset = '/tags/' . rawurlencode($tag); 34 } 35 36 $arguments = [ 37 'content' => 'bib', 38 'limit' => $limit, 39 ]; 40 41 if (! empty($prefs['zotero_style'])) { 42 $arguments['style'] = $prefs['zotero_style']; 43 } 44 45 $oauthlib = TikiLib::lib('oauth'); 46 $response = $oauthlib->do_request( 47 'zotero', 48 [ 49 'url' => "https://api.zotero.org/groups/{$prefs['zotero_group_id']}$subset/items", 50 'get' => $arguments, 51 ] 52 ); 53 54 if ($response && $response->isSuccessful()) { 55 $feed = Zend\Feed\Reader\Reader::importString($response->getBody()); 56 57 $data = []; 58 foreach ($feed as $entry) { 59 $data[] = [ 60 'key' => basename($entry->getLink()), 61 'url' => $entry->getLink(), 62 'title' => $entry->getTitle(), 63 'content' => $entry->getDescription(), 64 ]; 65 } 66 67 return $data; 68 } 69 70 return false; 71 } 72 73 /** 74 * @param $tag 75 * @return bool|mixed 76 */ 77 function get_first_entry($tag) 78 { 79 if ($references = $this->get_references($tag, 1)) { 80 return reset($references); 81 } 82 83 return false; 84 } 85 86 /** 87 * @param $itemId 88 * @return array|bool 89 */ 90 function get_entry($itemId) 91 { 92 global $prefs; 93 94 $arguments = [ 95 'content' => 'bib', 96 ]; 97 98 if (! empty($prefs['zotero_style'])) { 99 $arguments['style'] = $prefs['zotero_style']; 100 } 101 102 $oauthlib = TikiLib::lib('oauth'); 103 $response = $oauthlib->do_request( 104 'zotero', 105 [ 106 'url' => "https://api.zotero.org/groups/{$prefs['zotero_group_id']}/items/" . urlencode($itemId), 107 'get' => $arguments, 108 ] 109 ); 110 111 if ($response->isSuccessful()) { 112 $entry = $response->getBody(); 113 $entry = str_replace('<entry ', '<feed xmlns="http://www.w3.org/2005/Atom"><entry ', $entry) . '</feed>'; 114 $feed = Zend\Feed\Reader\Reader::importString($entry); 115 116 foreach ($feed as $entry) { 117 return [ 118 'key' => basename($entry->getLink()), 119 'url' => $entry->getLink(), 120 'title' => $entry->getTitle(), 121 'content' => $entry->getDescription(), 122 ]; 123 } 124 } 125 126 return false; 127 } 128 129 /** 130 * @param $tag 131 * @return bool 132 */ 133 function get_formatted_references($tag) 134 { 135 global $prefs; 136 137 $subset = null; 138 if ($tag) { 139 $subset = '/tags/' . rawurlencode($tag); 140 } 141 142 $arguments = [ 143 'content' => 'bib', 144 'format' => 'bib', 145 'limit' => 500, 146 ]; 147 148 if (! empty($prefs['zotero_style'])) { 149 $arguments['style'] = $prefs['zotero_style']; 150 } 151 152 $oauthlib = TikiLib::lib('oauth'); 153 $response = $oauthlib->do_request( 154 'zotero', 155 [ 156 'url' => "https://api.zotero.org/groups/{$prefs['zotero_group_id']}$subset/items", 157 'get' => $arguments, 158 ] 159 ); 160 161 if ($response->isSuccessful()) { 162 $entry = $response->getBody(); 163 164 return $entry; 165 } 166 167 return false; 168 } 169} 170