Subversion Repositories oidplus

Rev

Rev 928 | Rev 1086 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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