Rev 1086 | Rev 1121 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusDoi extends OIDplusObject { |
27 | private $doi; |
||
28 | |||
1116 | daniel-mar | 29 | /** |
30 | * @param $doi |
||
31 | */ |
||
635 | daniel-mar | 32 | public function __construct($doi) { |
33 | // TODO: syntax checks |
||
34 | $this->doi = $doi; |
||
35 | } |
||
36 | |||
1116 | daniel-mar | 37 | /** |
38 | * @param string $node_id |
||
39 | * @return OIDplusDoi|null |
||
40 | */ |
||
41 | public static function parse(string $node_id)/*: ?OIDplusDoi*/ { |
||
635 | daniel-mar | 42 | @list($namespace, $doi) = explode(':', $node_id, 2); |
1116 | daniel-mar | 43 | if ($namespace !== self::ns()) return null; |
635 | daniel-mar | 44 | return new self($doi); |
45 | } |
||
46 | |||
1116 | daniel-mar | 47 | /** |
48 | * @return string |
||
49 | */ |
||
50 | public static function objectTypeTitle(): string { |
||
635 | daniel-mar | 51 | return _L('Digital Object Identifier (DOI)'); |
52 | } |
||
53 | |||
1116 | daniel-mar | 54 | /** |
55 | * @return string |
||
56 | */ |
||
57 | public static function objectTypeTitleShort(): string { |
||
635 | daniel-mar | 58 | return _L('DOI'); |
59 | } |
||
60 | |||
1116 | daniel-mar | 61 | /** |
62 | * @return string |
||
63 | */ |
||
64 | public static function ns(): string { |
||
635 | daniel-mar | 65 | return 'doi'; |
66 | } |
||
67 | |||
1116 | daniel-mar | 68 | /** |
69 | * @return string |
||
70 | */ |
||
71 | public static function root(): string { |
||
860 | daniel-mar | 72 | return self::ns().':'; |
635 | daniel-mar | 73 | } |
74 | |||
1116 | daniel-mar | 75 | /** |
76 | * @return bool |
||
77 | */ |
||
78 | public function isRoot(): bool { |
||
635 | daniel-mar | 79 | return $this->doi == ''; |
80 | } |
||
81 | |||
1116 | daniel-mar | 82 | /** |
83 | * @param bool $with_ns |
||
84 | * @return string |
||
85 | */ |
||
86 | public function nodeId(bool $with_ns=true): string { |
||
859 | daniel-mar | 87 | return $with_ns ? self::root().$this->doi : $this->doi; |
635 | daniel-mar | 88 | } |
89 | |||
1116 | daniel-mar | 90 | /** |
91 | * @param string $str |
||
92 | * @return string |
||
93 | * @throws OIDplusException |
||
94 | */ |
||
95 | public function addString(string $str): string { |
||
635 | daniel-mar | 96 | if ($this->isRoot()) { |
97 | // Parent is root, so $str is the base DOI (10.xxxx) |
||
98 | $base = $str; |
||
99 | if (!self::validBaseDoi($base)) { |
||
100 | throw new OIDplusException(_L('Invalid DOI %1 . It must have syntax 10.xxxx',$base)); |
||
101 | } |
||
859 | daniel-mar | 102 | return self::root() . $base; |
635 | daniel-mar | 103 | } else if (self::validBaseDoi($this->doi)) { |
104 | // First level: We add a pubilcation to the base |
||
859 | daniel-mar | 105 | return self::root() . $this->doi . '/' . $str; |
635 | daniel-mar | 106 | } else { |
107 | // We just add an additional string to the already existing publication, e.g. a graphic reference or chapter |
||
859 | daniel-mar | 108 | return self::root() . $this->doi . $str; |
635 | daniel-mar | 109 | } |
110 | } |
||
111 | |||
1116 | daniel-mar | 112 | /** |
113 | * @param OIDplusObject $parent |
||
114 | * @return string |
||
115 | */ |
||
116 | public function crudShowId(OIDplusObject $parent): string { |
||
635 | daniel-mar | 117 | return $this->doi; |
118 | } |
||
119 | |||
1116 | daniel-mar | 120 | /** |
121 | * @return string |
||
122 | * @throws OIDplusException |
||
123 | */ |
||
124 | public function crudInsertPrefix(): string { |
||
635 | daniel-mar | 125 | return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1); |
126 | } |
||
127 | |||
1116 | daniel-mar | 128 | /** |
129 | * @param OIDplusObject|null $parent |
||
130 | * @return string |
||
131 | */ |
||
132 | public function jsTreeNodeName(OIDplusObject $parent = null): string { |
||
635 | daniel-mar | 133 | if ($parent == null) return $this->objectTypeTitle(); |
134 | $out = $this->doi; |
||
135 | $ary = explode('/', $out, 2); |
||
136 | if (count($ary) > 1) $out = $ary[1]; |
||
137 | return $out; |
||
138 | } |
||
139 | |||
1116 | daniel-mar | 140 | /** |
141 | * @return string |
||
142 | */ |
||
143 | public function defaultTitle(): string { |
||
635 | daniel-mar | 144 | return _L('DOI %1',$this->doi); |
145 | } |
||
146 | |||
1116 | daniel-mar | 147 | /** |
148 | * @return bool |
||
149 | */ |
||
150 | public function isLeafNode(): bool { |
||
635 | daniel-mar | 151 | return false; |
152 | } |
||
153 | |||
1116 | daniel-mar | 154 | /** |
155 | * @param string $title |
||
156 | * @param string $content |
||
157 | * @param string $icon |
||
158 | * @return void |
||
159 | * @throws OIDplusException |
||
160 | */ |
||
161 | public function getContentPage(string &$title, string &$content, string &$icon) { |
||
801 | daniel-mar | 162 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 163 | |
164 | if ($this->isRoot()) { |
||
165 | $title = OIDplusDoi::objectTypeTitle(); |
||
166 | |||
167 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 168 | if ($res->any()) { |
635 | daniel-mar | 169 | $content = _L('Please select an DOI in the tree view at the left to show its contents.'); |
170 | } else { |
||
171 | $content = _L('Currently, no DOIs are registered in the system.'); |
||
172 | } |
||
173 | |||
174 | if (!$this->isLeafNode()) { |
||
175 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
176 | $content .= '<h2>'._L('Manage your DOIs').'</h2>'; |
||
177 | } else { |
||
178 | $content .= '<h2>'._L('Available DOIs').'</h2>'; |
||
179 | } |
||
180 | $content .= '%%CRUD%%'; |
||
181 | } |
||
182 | } else { |
||
183 | $title = $this->getTitle(); |
||
184 | |||
185 | $pure = explode(':',$this->nodeId())[1]; |
||
186 | $content = '<h3><a target="_blank" href="https://dx.doi.org/'.htmlentities($pure).'">'._L('Resolve %1',htmlentities($pure)).' </a></h3>'; |
||
187 | |||
188 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
189 | |||
190 | if (!$this->isLeafNode()) { |
||
191 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 192 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 193 | } else { |
928 | daniel-mar | 194 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 195 | } |
196 | $content .= '%%CRUD%%'; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | # --- |
||
202 | |||
1116 | daniel-mar | 203 | /** |
204 | * @param string $doi |
||
205 | * @return bool |
||
206 | */ |
||
207 | public static function validBaseDoi(string $doi): bool { |
||
635 | daniel-mar | 208 | $m = array(); |
209 | return preg_match('@^10\.\d{4}$@', $doi, $m); |
||
210 | } |
||
211 | |||
1116 | daniel-mar | 212 | /** |
213 | * @return OIDplusDoi|null |
||
214 | */ |
||
215 | public function one_up()/*: ?OIDplusDoi*/ { |
||
635 | daniel-mar | 216 | $oid = $this->doi; |
217 | |||
218 | $p = strrpos($oid, '/'); |
||
713 | daniel-mar | 219 | if ($p === false) return self::parse($oid); |
220 | if ($p == 0) return self::parse('/'); |
||
635 | daniel-mar | 221 | |
222 | $oid_up = substr($oid, 0, $p); |
||
223 | |||
224 | return self::parse(self::ns().':'.$oid_up); |
||
225 | } |
||
226 | |||
1116 | daniel-mar | 227 | /** |
228 | * @param $to |
||
229 | * @return int|null |
||
230 | */ |
||
635 | daniel-mar | 231 | public function distance($to) { |
232 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
1116 | daniel-mar | 233 | if (!($to instanceof $this)) return null; |
635 | daniel-mar | 234 | |
235 | $a = $to->doi; |
||
236 | $b = $this->doi; |
||
237 | |||
238 | if (substr($a,0,1) == '/') $a = substr($a,1); |
||
239 | if (substr($b,0,1) == '/') $b = substr($b,1); |
||
240 | |||
241 | $ary = explode('/', $a); |
||
242 | $bry = explode('/', $b); |
||
243 | |||
244 | $min_len = min(count($ary), count($bry)); |
||
245 | |||
246 | for ($i=0; $i<$min_len; $i++) { |
||
1116 | daniel-mar | 247 | if ($ary[$i] != $bry[$i]) return null; |
635 | daniel-mar | 248 | } |
249 | |||
250 | return count($ary) - count($bry); |
||
251 | } |
||
252 | |||
1116 | daniel-mar | 253 | /** |
254 | * @return string |
||
255 | */ |
||
256 | public function getDirectoryName(): string { |
||
635 | daniel-mar | 257 | if ($this->isRoot()) return $this->ns(); |
258 | return $this->ns().'_'.md5($this->nodeId(false)); |
||
259 | } |
||
800 | daniel-mar | 260 | |
1116 | daniel-mar | 261 | /** |
262 | * @param string $mode |
||
263 | * @return string |
||
264 | */ |
||
265 | public static function treeIconFilename(string $mode): string { |
||
800 | daniel-mar | 266 | return 'img/'.$mode.'_icon16.png'; |
267 | } |
||
713 | daniel-mar | 268 | } |