Subversion Repositories oidplus

Rev

Rev 1121 | 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 {
1130 daniel-mar 27
        /**
28
         * @var string
29
         */
635 daniel-mar 30
        private $doi;
31
 
1116 daniel-mar 32
        /**
1130 daniel-mar 33
         * @param string $doi
1116 daniel-mar 34
         */
1130 daniel-mar 35
        public function __construct(string $doi) {
635 daniel-mar 36
                // TODO: syntax checks
37
                $this->doi = $doi;
38
        }
39
 
1116 daniel-mar 40
        /**
41
         * @param string $node_id
42
         * @return OIDplusDoi|null
43
         */
44
        public static function parse(string $node_id)/*: ?OIDplusDoi*/ {
635 daniel-mar 45
                @list($namespace, $doi) = explode(':', $node_id, 2);
1116 daniel-mar 46
                if ($namespace !== self::ns()) return null;
635 daniel-mar 47
                return new self($doi);
48
        }
49
 
1116 daniel-mar 50
        /**
51
         * @return string
52
         */
53
        public static function objectTypeTitle(): string {
635 daniel-mar 54
                return _L('Digital Object Identifier (DOI)');
55
        }
56
 
1116 daniel-mar 57
        /**
58
         * @return string
59
         */
60
        public static function objectTypeTitleShort(): string {
635 daniel-mar 61
                return _L('DOI');
62
        }
63
 
1116 daniel-mar 64
        /**
65
         * @return string
66
         */
67
        public static function ns(): string {
635 daniel-mar 68
                return 'doi';
69
        }
70
 
1116 daniel-mar 71
        /**
72
         * @return string
73
         */
74
        public static function root(): string {
860 daniel-mar 75
                return self::ns().':';
635 daniel-mar 76
        }
77
 
1116 daniel-mar 78
        /**
79
         * @return bool
80
         */
81
        public function isRoot(): bool {
635 daniel-mar 82
                return $this->doi == '';
83
        }
84
 
1116 daniel-mar 85
        /**
86
         * @param bool $with_ns
87
         * @return string
88
         */
89
        public function nodeId(bool $with_ns=true): string {
859 daniel-mar 90
                return $with_ns ? self::root().$this->doi : $this->doi;
635 daniel-mar 91
        }
92
 
1116 daniel-mar 93
        /**
94
         * @param string $str
95
         * @return string
96
         * @throws OIDplusException
97
         */
98
        public function addString(string $str): string {
635 daniel-mar 99
                if ($this->isRoot()) {
100
                        // Parent is root, so $str is the base DOI (10.xxxx)
101
                        $base = $str;
102
                        if (!self::validBaseDoi($base)) {
103
                                throw new OIDplusException(_L('Invalid DOI %1 . It must have syntax 10.xxxx',$base));
104
                        }
859 daniel-mar 105
                        return self::root() . $base;
635 daniel-mar 106
                } else if (self::validBaseDoi($this->doi)) {
107
                        // First level: We add a pubilcation to the base
859 daniel-mar 108
                        return self::root() . $this->doi . '/' . $str;
635 daniel-mar 109
                } else {
110
                        // We just add an additional string to the already existing publication, e.g. a graphic reference or chapter
859 daniel-mar 111
                        return self::root() . $this->doi . $str;
635 daniel-mar 112
                }
113
        }
114
 
1116 daniel-mar 115
        /**
116
         * @param OIDplusObject $parent
117
         * @return string
118
         */
119
        public function crudShowId(OIDplusObject $parent): string {
635 daniel-mar 120
                return $this->doi;
121
        }
122
 
1116 daniel-mar 123
        /**
124
         * @return string
125
         * @throws OIDplusException
126
         */
127
        public function crudInsertPrefix(): string {
635 daniel-mar 128
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
129
        }
130
 
1116 daniel-mar 131
        /**
132
         * @param OIDplusObject|null $parent
133
         * @return string
134
         */
135
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
635 daniel-mar 136
                if ($parent == null) return $this->objectTypeTitle();
137
                $out = $this->doi;
138
                $ary = explode('/', $out, 2);
139
                if (count($ary) > 1) $out = $ary[1];
140
                return $out;
141
        }
142
 
1116 daniel-mar 143
        /**
144
         * @return string
145
         */
146
        public function defaultTitle(): string {
635 daniel-mar 147
                return _L('DOI %1',$this->doi);
148
        }
149
 
1116 daniel-mar 150
        /**
151
         * @return bool
152
         */
153
        public function isLeafNode(): bool {
635 daniel-mar 154
                return false;
155
        }
156
 
1116 daniel-mar 157
        /**
158
         * @param string $title
159
         * @param string $content
160
         * @param string $icon
161
         * @return void
162
         * @throws OIDplusException
163
         */
164
        public function getContentPage(string &$title, string &$content, string &$icon) {
801 daniel-mar 165
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 166
 
167
                if ($this->isRoot()) {
168
                        $title = OIDplusDoi::objectTypeTitle();
169
 
170
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
790 daniel-mar 171
                        if ($res->any()) {
635 daniel-mar 172
                                $content = _L('Please select an DOI in the tree view at the left to show its contents.');
173
                        } else {
174
                                $content = _L('Currently, no DOIs are registered in the system.');
175
                        }
176
 
177
                        if (!$this->isLeafNode()) {
178
                                if (OIDplus::authUtils()->isAdminLoggedIn()) {
179
                                        $content .= '<h2>'._L('Manage your DOIs').'</h2>';
180
                                } else {
181
                                        $content .= '<h2>'._L('Available DOIs').'</h2>';
182
                                }
183
                                $content .= '%%CRUD%%';
184
                        }
185
                } else {
186
                        $title = $this->getTitle();
187
 
188
                        $pure = explode(':',$this->nodeId())[1];
189
                        $content = '<h3><a target="_blank" href="https://dx.doi.org/'.htmlentities($pure).'">'._L('Resolve %1',htmlentities($pure)).' </a></h3>';
190
 
191
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
192
 
193
                        if (!$this->isLeafNode()) {
194
                                if ($this->userHasWriteRights()) {
928 daniel-mar 195
                                        $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
635 daniel-mar 196
                                } else {
928 daniel-mar 197
                                        $content .= '<h2>'._L('Subordinate objects').'</h2>';
635 daniel-mar 198
                                }
199
                                $content .= '%%CRUD%%';
200
                        }
201
                }
202
        }
203
 
204
        # ---
205
 
1116 daniel-mar 206
        /**
207
         * @param string $doi
208
         * @return bool
209
         */
210
        public static function validBaseDoi(string $doi): bool {
635 daniel-mar 211
                $m = array();
212
                return preg_match('@^10\.\d{4}$@', $doi, $m);
213
        }
214
 
1116 daniel-mar 215
        /**
216
         * @return OIDplusDoi|null
217
         */
218
        public function one_up()/*: ?OIDplusDoi*/ {
635 daniel-mar 219
                $oid = $this->doi;
220
 
221
                $p = strrpos($oid, '/');
713 daniel-mar 222
                if ($p === false) return self::parse($oid);
223
                if ($p == 0) return self::parse('/');
635 daniel-mar 224
 
225
                $oid_up = substr($oid, 0, $p);
226
 
227
                return self::parse(self::ns().':'.$oid_up);
228
        }
229
 
1116 daniel-mar 230
        /**
1130 daniel-mar 231
         * @param OIDplusObject|string $to
1116 daniel-mar 232
         * @return int|null
233
         */
635 daniel-mar 234
        public function distance($to) {
235
                if (!is_object($to)) $to = OIDplusObject::parse($to);
1121 daniel-mar 236
                if (!$to) return null;
1116 daniel-mar 237
                if (!($to instanceof $this)) return null;
635 daniel-mar 238
 
239
                $a = $to->doi;
240
                $b = $this->doi;
241
 
242
                if (substr($a,0,1) == '/') $a = substr($a,1);
243
                if (substr($b,0,1) == '/') $b = substr($b,1);
244
 
245
                $ary = explode('/', $a);
246
                $bry = explode('/', $b);
247
 
248
                $min_len = min(count($ary), count($bry));
249
 
250
                for ($i=0; $i<$min_len; $i++) {
1116 daniel-mar 251
                        if ($ary[$i] != $bry[$i]) return null;
635 daniel-mar 252
                }
253
 
254
                return count($ary) - count($bry);
255
        }
256
 
1116 daniel-mar 257
        /**
258
         * @return string
259
         */
260
        public function getDirectoryName(): string {
635 daniel-mar 261
                if ($this->isRoot()) return $this->ns();
262
                return $this->ns().'_'.md5($this->nodeId(false));
263
        }
800 daniel-mar 264
 
1116 daniel-mar 265
        /**
266
         * @param string $mode
267
         * @return string
268
         */
269
        public static function treeIconFilename(string $mode): string {
800 daniel-mar 270
                return 'img/'.$mode.'_icon16.png';
271
        }
713 daniel-mar 272
}