Subversion Repositories oidplus

Rev

Rev 224 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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