Subversion Repositories oidplus

Rev

Rev 360 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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
 
227 daniel-mar 20
class OIDplusDoi extends OIDplusObject {
21
        private $doi;
22
 
2 daniel-mar 23
        public function __construct($doi) {
16 daniel-mar 24
                // TODO: syntax checks
2 daniel-mar 25
                $this->doi = $doi;
26
        }
27
 
28
        public static function parse($node_id) {
29
                @list($namespace, $doi) = explode(':', $node_id, 2);
30
                if ($namespace !== 'doi') return false;
31
                return new self($doi);
32
        }
33
 
34
        public static function objectTypeTitle() {
360 daniel-mar 35
                return _L('Digital Object Identifier (DOI)');
2 daniel-mar 36
        }
37
 
38
        public static function objectTypeTitleShort() {
360 daniel-mar 39
                return _L('DOI');
2 daniel-mar 40
        }
41
 
42
        public static function ns() {
43
                return 'doi';
44
        }
45
 
46
        public static function root() {
47
                return 'doi:';
48
        }
49
 
50
        public function isRoot() {
51
                return $this->doi == '';
52
        }
53
 
247 daniel-mar 54
        public function nodeId($with_ns=true) {
55
                return $with_ns ? 'doi:'.$this->doi : $this->doi;
2 daniel-mar 56
        }
57
 
58
        public function addString($str) {
59
                if ($this->isRoot()) {
60
                        // Parent is root, so $str is the base DOI (10.xxxx)
61
                        $base = $str;
62
                        if (!self::validBaseDoi($base)) {
360 daniel-mar 63
                                throw new OIDplusException(_L('Invalid DOI %1 . It must have syntax 10.xxxx',$base));
2 daniel-mar 64
                        }
65
                        return 'doi:' . $base;
66
                } else if (self::validBaseDoi($this->doi)) {
67
                        // First level: We add a pubilcation to the base
68
                        return 'doi:' . $this->doi . '/' . $str;
69
                } else {
70
                        // We just add an additional string to the already existing publication, e.g. a graphic reference or chapter
71
                        return 'doi:' . $this->doi . $str;
72
                }
73
        }
74
 
75
        public function crudShowId(OIDplusObject $parent) {
76
                return $this->doi;
77
        }
78
 
79
        public function crudInsertPrefix() {
80
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
81
        }
82
 
83
        public function jsTreeNodeName(OIDplusObject $parent = null) {
84
                if ($parent == null) return $this->objectTypeTitle();
85
                $out = $this->doi;
86
                $ary = explode('/', $out, 2);
87
                if (count($ary) > 1) $out = $ary[1];
88
                return $out;
89
        }
90
 
91
        public function defaultTitle() {
360 daniel-mar 92
                return _L('DOI %1',$this->doi);
2 daniel-mar 93
        }
94
 
16 daniel-mar 95
        public function isLeafNode() {
96
                return false;
97
        }
98
 
34 daniel-mar 99
        public function getContentPage(&$title, &$content, &$icon) {
55 daniel-mar 100
                $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
101
 
2 daniel-mar 102
                if ($this->isRoot()) {
103
                        $title = OIDplusDoi::objectTypeTitle();
104
 
261 daniel-mar 105
                        $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
236 daniel-mar 106
                        if ($res->num_rows() > 0) {
360 daniel-mar 107
                                $content = _L('Please select an DOI in the tree view at the left to show its contents.');
2 daniel-mar 108
                        } else {
360 daniel-mar 109
                                $content = _L('Currently, no DOIs are registered in the system.');
2 daniel-mar 110
                        }
111
 
16 daniel-mar 112
                        if (!$this->isLeafNode()) {
113
                                if (OIDplus::authUtils()::isAdminLoggedIn()) {
360 daniel-mar 114
                                        $content .= '<h2>'._L('Manage your DOIs').'</h2>';
16 daniel-mar 115
                                } else {
360 daniel-mar 116
                                        $content .= '<h2>'._L('Available DOIs').'</h2>';
16 daniel-mar 117
                                }
118
                                $content .= '%%CRUD%%';
2 daniel-mar 119
                        }
120
                } else {
192 daniel-mar 121
                        $title = $this->getTitle();
122
 
2 daniel-mar 123
                        $pure = explode(':',$this->nodeId())[1];
360 daniel-mar 124
                        $content = '<h3><a target="_blank" href="https://dx.doi.org/'.htmlentities($pure).'">'._L('Resolve %1',htmlentities($pure)).' </a></h3>';
2 daniel-mar 125
 
360 daniel-mar 126
                        $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
2 daniel-mar 127
 
16 daniel-mar 128
                        if (!$this->isLeafNode()) {
129
                                if ($this->userHasWriteRights()) {
360 daniel-mar 130
                                        $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
16 daniel-mar 131
                                } else {
360 daniel-mar 132
                                        $content .= '<h2>'._L('Subsequent objects').'</h2>';
16 daniel-mar 133
                                }
134
                                $content .= '%%CRUD%%';
2 daniel-mar 135
                        }
136
                }
137
        }
138
 
139
        # ---
140
 
141
        public static function validBaseDoi($doi) {
386 daniel-mar 142
                $m = array();
2 daniel-mar 143
                return preg_match('@^10\.\d{4}$@', $doi, $m);
144
        }
20 daniel-mar 145
 
146
        public function one_up() {
38 daniel-mar 147
                $oid = $this->doi;
148
 
149
                $p = strrpos($oid, '/');
150
                if ($p === false) return $oid;
151
                if ($p == 0) return '/';
152
 
153
                $oid_up = substr($oid, 0, $p);
154
 
155
                return self::parse(self::ns().':'.$oid_up);
20 daniel-mar 156
        }
157
 
158
        public function distance($to) {
38 daniel-mar 159
                if (!is_object($to)) $to = OIDplusObject::parse($to);
160
                if (!($to instanceof $this)) return false;
161
 
162
                $a = $to->doi;
163
                $b = $this->doi;
164
 
165
                if (substr($a,0,1) == '/') $a = substr($a,1);
166
                if (substr($b,0,1) == '/') $b = substr($b,1);
167
 
168
                $ary = explode('/', $a);
169
                $bry = explode('/', $b);
170
 
171
                $min_len = min(count($ary), count($bry));
172
 
173
                for ($i=0; $i<$min_len; $i++) {
174
                        if ($ary[$i] != $bry[$i]) return false;
175
                }
176
 
177
                return count($ary) - count($bry);
20 daniel-mar 178
        }
360 daniel-mar 179
}