Subversion Repositories oidplus

Rev

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.         /**
  30.          * @param $doi
  31.          */
  32.         public function __construct($doi) {
  33.                 // TODO: syntax checks
  34.                 $this->doi = $doi;
  35.         }
  36.  
  37.         /**
  38.          * @param string $node_id
  39.          * @return OIDplusDoi|null
  40.          */
  41.         public static function parse(string $node_id)/*: ?OIDplusDoi*/ {
  42.                 @list($namespace, $doi) = explode(':', $node_id, 2);
  43.                 if ($namespace !== self::ns()) return null;
  44.                 return new self($doi);
  45.         }
  46.  
  47.         /**
  48.          * @return string
  49.          */
  50.         public static function objectTypeTitle(): string {
  51.                 return _L('Digital Object Identifier (DOI)');
  52.         }
  53.  
  54.         /**
  55.          * @return string
  56.          */
  57.         public static function objectTypeTitleShort(): string {
  58.                 return _L('DOI');
  59.         }
  60.  
  61.         /**
  62.          * @return string
  63.          */
  64.         public static function ns(): string {
  65.                 return 'doi';
  66.         }
  67.  
  68.         /**
  69.          * @return string
  70.          */
  71.         public static function root(): string {
  72.                 return self::ns().':';
  73.         }
  74.  
  75.         /**
  76.          * @return bool
  77.          */
  78.         public function isRoot(): bool {
  79.                 return $this->doi == '';
  80.         }
  81.  
  82.         /**
  83.          * @param bool $with_ns
  84.          * @return string
  85.          */
  86.         public function nodeId(bool $with_ns=true): string {
  87.                 return $with_ns ? self::root().$this->doi : $this->doi;
  88.         }
  89.  
  90.         /**
  91.          * @param string $str
  92.          * @return string
  93.          * @throws OIDplusException
  94.          */
  95.         public function addString(string $str): string {
  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.                         }
  102.                         return self::root() . $base;
  103.                 } else if (self::validBaseDoi($this->doi)) {
  104.                         // First level: We add a pubilcation to the base
  105.                         return self::root() . $this->doi . '/' . $str;
  106.                 } else {
  107.                         // We just add an additional string to the already existing publication, e.g. a graphic reference or chapter
  108.                         return self::root() . $this->doi . $str;
  109.                 }
  110.         }
  111.  
  112.         /**
  113.          * @param OIDplusObject $parent
  114.          * @return string
  115.          */
  116.         public function crudShowId(OIDplusObject $parent): string {
  117.                 return $this->doi;
  118.         }
  119.  
  120.         /**
  121.          * @return string
  122.          * @throws OIDplusException
  123.          */
  124.         public function crudInsertPrefix(): string {
  125.                 return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
  126.         }
  127.  
  128.         /**
  129.          * @param OIDplusObject|null $parent
  130.          * @return string
  131.          */
  132.         public function jsTreeNodeName(OIDplusObject $parent = null): string {
  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.  
  140.         /**
  141.          * @return string
  142.          */
  143.         public function defaultTitle(): string {
  144.                 return _L('DOI %1',$this->doi);
  145.         }
  146.  
  147.         /**
  148.          * @return bool
  149.          */
  150.         public function isLeafNode(): bool {
  151.                 return false;
  152.         }
  153.  
  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) {
  162.                 $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  163.  
  164.                 if ($this->isRoot()) {
  165.                         $title = OIDplusDoi::objectTypeTitle();
  166.  
  167.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  168.                         if ($res->any()) {
  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()) {
  192.                                         $content .= '<h2>'._L('Create or change subordinate objects').'</h2>';
  193.                                 } else {
  194.                                         $content .= '<h2>'._L('Subordinate objects').'</h2>';
  195.                                 }
  196.                                 $content .= '%%CRUD%%';
  197.                         }
  198.                 }
  199.         }
  200.  
  201.         # ---
  202.  
  203.         /**
  204.          * @param string $doi
  205.          * @return bool
  206.          */
  207.         public static function validBaseDoi(string $doi): bool {
  208.                 $m = array();
  209.                 return preg_match('@^10\.\d{4}$@', $doi, $m);
  210.         }
  211.  
  212.         /**
  213.          * @return OIDplusDoi|null
  214.          */
  215.         public function one_up()/*: ?OIDplusDoi*/ {
  216.                 $oid = $this->doi;
  217.  
  218.                 $p = strrpos($oid, '/');
  219.                 if ($p === false) return self::parse($oid);
  220.                 if ($p == 0) return self::parse('/');
  221.  
  222.                 $oid_up = substr($oid, 0, $p);
  223.  
  224.                 return self::parse(self::ns().':'.$oid_up);
  225.         }
  226.  
  227.         /**
  228.          * @param $to
  229.          * @return int|null
  230.          */
  231.         public function distance($to) {
  232.                 if (!is_object($to)) $to = OIDplusObject::parse($to);
  233.                 if (!$to) return null;
  234.                 if (!($to instanceof $this)) return null;
  235.  
  236.                 $a = $to->doi;
  237.                 $b = $this->doi;
  238.  
  239.                 if (substr($a,0,1) == '/') $a = substr($a,1);
  240.                 if (substr($b,0,1) == '/') $b = substr($b,1);
  241.  
  242.                 $ary = explode('/', $a);
  243.                 $bry = explode('/', $b);
  244.  
  245.                 $min_len = min(count($ary), count($bry));
  246.  
  247.                 for ($i=0; $i<$min_len; $i++) {
  248.                         if ($ary[$i] != $bry[$i]) return null;
  249.                 }
  250.  
  251.                 return count($ary) - count($bry);
  252.         }
  253.  
  254.         /**
  255.          * @return string
  256.          */
  257.         public function getDirectoryName(): string {
  258.                 if ($this->isRoot()) return $this->ns();
  259.                 return $this->ns().'_'.md5($this->nodeId(false));
  260.         }
  261.  
  262.         /**
  263.          * @param string $mode
  264.          * @return string
  265.          */
  266.         public static function treeIconFilename(string $mode): string {
  267.                 return 'img/'.$mode.'_icon16.png';
  268.         }
  269. }
  270.