Subversion Repositories oidplus

Rev

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