Subversion Repositories oidplus

Rev

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