OSDN Git Service

NP_PubMed v0.1.8
authorkmorimatsu <kmorimatsu@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Mon, 22 Dec 2008 08:45:48 +0000 (08:45 +0000)
committerkmorimatsu <kmorimatsu@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Mon, 22 Dec 2008 08:45:48 +0000 (08:45 +0000)
template engine constructed.
Now supports Cell!

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@756 1ca29b6e-896d-4ea0-84a5-967f57386b96

NP_PubMed/trunk/NP_PubMed.php
NP_PubMed/trunk/pubmed/index.php
NP_PubMed/trunk/pubmed/template.php [new file with mode: 0644]
NP_PubMed/trunk/pubmed/templates/Cell.php [new file with mode: 0644]
NP_PubMed/trunk/pubmed/templates/PNAS.php [new file with mode: 0644]
NP_PubMed/trunk/pubmed/templates/default.php [new file with mode: 0644]
NP_PubMed/trunk/spring/skinbackup.xml

index d847f6d..8298d5f 100644 (file)
@@ -3,8 +3,8 @@ class NP_PubMed extends NucleusPlugin {
        function getName() { return 'NP_PubMed'; }\r
        function getMinNucleusVersion() { return 330; }\r
        function getAuthor()  { return 'Katsumi'; }\r
-       function getVersion() { return '0.1.7'; }\r
-       function getURL() {return 'http://hp.vector.co.jp/authors/VA016157/';}\r
+       function getVersion() { return '0.1.8'; }\r
+       function getURL() {return 'http://japan.nucleuscms.org/wiki/plugins:authors:katsumi';}\r
        function getDescription() {\r
                return $this->getName().' plugin<br />'.\r
                        'This plugin uses the query service of "Entrez Programming Utilities".<br />'.\r
@@ -31,8 +31,8 @@ class NP_PubMed extends NucleusPlugin {
                        ' manuscriptid int(11) not null auto_increment,'.\r
                        ' userid int(11) not null default 0,'.\r
                        ' manuscriptname varchar(200) not null default "New Manuscript",'.\r
-                       ' templatename varchar(200) not null default "default.template",'.\r
-                       ' sortmethod varchar(40) not null default "author",'.\r
+                       ' templatename varchar(200) not null default "default",'.\r
+                       ' sortdata text not null default "",'.\r
                        ' PRIMARY KEY manuscriptid(manuscriptid) '.\r
                        ') TYPE=MyISAM;');\r
        }\r
@@ -64,7 +64,7 @@ class NP_PubMed extends NucleusPlugin {
                );\r
        }\r
        function event_PreUpdateItem(&$data){\r
-               $ret=$this->event_PreAddItem(&$data);\r
+               $ret=$this->event_PreAddItem($data);\r
                $this->_updateManuscriptData($data['itemid']);\r
                return $ret;\r
        }\r
@@ -188,6 +188,12 @@ class NP_PubMed extends NucleusPlugin {
                global $CONF,$manager,$blog,$member;\r
                $mid=$member->getID();\r
                switch($mode=strtolower($mode)){\r
+               case 'getvar':\r
+                       echo htmlspecialchars(getVar($p1),ENT_QUOTES);\r
+                       break;\r
+               case 'postvar':\r
+                       echo htmlspecialchars(postVar($p1),ENT_QUOTES);\r
+                       break;\r
                case 'searchlink':\r
                        if (!$this->isAdmin()) return;\r
                        if (!$blog) return;\r
@@ -231,30 +237,50 @@ class NP_PubMed extends NucleusPlugin {
                        echo '<a href="http://www.ncbi.nlm.nih.gov/PubMed/" onclick="window.open(this.href,\'PubMed\');return false;">'.\r
                                htmlspecialchars(strlen($p1)?$p1:'PubMed',ENT_QUOTES).'</a>';\r
                        break;\r
-               case 'parse': case 'pageswitch':\r
+               case 'parse':\r
+                       if (!$mid) return;\r
                        if (!$blog) return;\r
-                       global $startpos;\r
-                       $startpos=(int)$startpos;\r
-                       $limit=(int)$p2;\r
-                       $template=addslashes($p1);\r
-                       $query=$blog->getSqlBlog('');\r
-                       $query=preg_replace('/ORDER[\s]+BY[\s][\s\.a-z0-9_]*(ASC|DESC)$/i','ORDER BY i.ititle ASC',$query);\r
-                       switch($mode){\r
-                       case 'pageswitch':\r
-                               $ps=&$manager->getPlugin('NP_PageSwitch');\r
-                               $ps->setQuery($query);\r
-                               break;\r
-                       case 'parse':\r
-                       default:\r
-                               $blog->showUsingQuery($template, $query.' LIMIT '.$startpos.','.$limit, '', 1, 1);\r
+                       $msid=intGetVar('manuscriptid');\r
+                       if (!$msid) return;\r
+                       $query='SELECT i.ibody as body, i.ititle as title, i.imore as more'.\r
+                               ' FROM '.sql_table('item').' as i, '.\r
+                                       sql_table('plugin_pubmed_references').' as r,'.\r
+                                       sql_table('plugin_pubmed_manuscripts').' as m'.\r
+                               ' WHERE i.inumber=r.itemid'.\r
+                               ' AND r.manuscriptid='.(int)$msid.\r
+                               ' AND m.manuscriptid='.(int)$msid.\r
+                               ' AND m.userid='.(int)$mid.\r
+                               ' ORDER BY i.ititle ASC';\r
+                       \r
+                       // Construct template object.\r
+                       require_once($this->getDirectory().'template.php');\r
+                       $res=sql_query('SELECT *'.\r
+                               ' FROM '.sql_table('plugin_pubmed_manuscripts').\r
+                               ' WHERE manuscriptid='.(int)$msid.\r
+                               ' AND userid='.(int)$mid);\r
+                       $row=mysql_fetch_assoc($res);\r
+                       if (!$row) return;\r
+                       $tobj=PUBMED_TEMPLATE_BASE::getTemplate($row['templatename'],$row['sortdata']);\r
+                       if (!$tobj) {\r
+                               echo 'The template, "'.htmlspecialchars($row['templatename']).'" cannot be found';\r
                                break;\r
                        }\r
+                       // Set all the data.\r
+                       $res=sql_query($query);\r
+                       while($row=mysql_fetch_assoc($res)){\r
+                               $tobj->setData($row['more']);\r
+                       }\r
+                       // Sort the papers\r
+                       $tobj->sortPapers();\r
+                       // Let's parse, finally.\r
+                       $tobj->parse_all();\r
+                       break;\r
                case 'manuscriptlist':\r
+                       \r
                        if (!$mid) return;\r
                        if (!$blog) return;\r
                        $blogid=$blog->getID();\r
                        $template =& $manager->getTemplate($p1);\r
-                       //print_r($template['CATLIST_LISTITEM']);exit;\r
                        $res=sql_query('SELECT manuscriptname as name, manuscriptid as id'.\r
                                ' FROM '.sql_table('plugin_pubmed_manuscripts').\r
                                ' WHERE userid='.(int)$mid);\r
index 9864067..fddbcb3 100644 (file)
@@ -269,14 +269,13 @@ class PubMedAdmin {
                global $member,$manager;\r
                $mid=$member->getID();\r
                echo '<a href="'.$this->oPluginAdmin->plugin->getAdminURL().'?blogid='.(int)$this->blogid.'&amp;action=manuscriptlist">Refresh</a><br />';\r
-               echo '<table><tr><th>manuscript</th><th>template</th><th>sort method</th><th colspan="2">&nbsp;</th></tr>';\r
+               echo '<table><tr><th>manuscript</th><th>template</th><th colspan="2">&nbsp;</th></tr>';\r
                $res=sql_query('SELECT * FROM '.sql_table('plugin_pubmed_manuscripts').\r
                        ' WHERE userid='.(int)$mid);\r
                while($row=mysql_fetch_assoc($res)){\r
                        echo '<tr>';\r
                        echo '<td>'.htmlspecialchars($row['manuscriptname']).'</td>';\r
                        echo '<td>'.htmlspecialchars($row['templatename']).'</td>';\r
-                       echo '<td>'.htmlspecialchars($row['sortmethod']).'</td>';\r
 ?>\r
 <td><form method="post" action="">\r
 <input type="hidden" name="action" value="deletemanuscript" />\r
@@ -379,24 +378,26 @@ Are you sure?&nbsp;&nbsp;
                if (!$mname) $mname=$row['manuscriptname'];\r
                $mname=$this->_checkmanuscriptname($mname,$manuscriptid);\r
                if (!$mname) return $this->manuscriptlist();\r
-               switch($sort=postVar('sortmethod')){\r
-               case 'author':\r
-               case 'manual':\r
-                       break;\r
-               default:\r
-                       $sort=$row['sortmethod'];\r
-               }\r
-               $template='default.template';\r
+               $template=$row['templatename'];\r
                if (postVar('sure')=='yes') {\r
+                       $template=postVar('templatename');\r
                        sql_query('UPDATE '.sql_table('plugin_pubmed_manuscripts').' SET'.\r
                                ' manuscriptname="'.addslashes($mname).'",'.\r
-                               ' templatename="'.addslashes($template).'",'.\r
-                               ' sortmethod="'.addslashes($sort).'"'.\r
+                               ' templatename="'.addslashes($template).'"'.\r
                                ' WHERE manuscriptid='.(int)$manuscriptid.\r
                                ' AND userid='.(int)$mid);\r
                        return $this->manuscriptlist();\r
                }\r
-               \r
+               // Get template files\r
+               $templates=array();\r
+               $d=dir(dirname(__FILE__).'/templates/');\r
+               while (false !== ($entry = $d->read())) {\r
+                       if (!preg_match('/^(.+)\.php$/',$entry,$m)) continue;\r
+                       if ($m[1]!='default') $templates[]=$m[1];\r
+               }\r
+               sort($templates);\r
+               array_unshift($templates,'default');\r
+$d->close(); \r
 ?>\r
 <form method="post" action="">\r
 <input type="hidden" name="action" value="editmanuscript" />\r
@@ -408,13 +409,19 @@ Are you sure?&nbsp;&nbsp;
 <tr><td>Manuscript name:</td>\r
 <td><input type="text" name="manuscriptname" value="<?php echo htmlspecialchars($mname); ?>" /></td></tr>\r
 <tr><td>Template:</td>\r
-<td><input type="text" name="tamplatename" value="<?php echo htmlspecialchars($template); ?>" /></td></tr>\r
-<tr><td>Sort method:</td>\r
-<td><select name="sortmethod">\r
-<option value="author" <?php if ($sort=='author') echo 'selected="selected"'; ?> >author</option>\r
-<option value="manual" <?php if ($sort=='manual') echo 'selected="selected"'; ?> >manual</option>\r
+<!-- td><input type="text" name="templatename" value="<?php echo htmlspecialchars($template); ?>" /></td></tr -->\r
+<td><select name="templatename">\r
+<?php\r
+               foreach($templates as $temp){\r
+                       $temp=htmlspecialchars($temp,ENT_QUOTES);\r
+                       echo '<option value="'.$temp.'"'.\r
+                               ($template==$temp ? ' selected="selected"' : '').\r
+                               '>'.$temp."</option>\n";\r
+               }\r
+?>\r
 </select></td>\r
-</tr></table>\r
+</tr>\r
+</table>\r
 <input type="submit" value="Edit" />\r
 </form>\r
 <?php\r
diff --git a/NP_PubMed/trunk/pubmed/template.php b/NP_PubMed/trunk/pubmed/template.php
new file mode 100644 (file)
index 0000000..f098750
--- /dev/null
@@ -0,0 +1,205 @@
+<?php\r
+\r
+class PUBMED_TEMPLATE_BASE {\r
+       /*\r
+        * This class must be inhelited by PUBMED_TEMPLATE class.\r
+        * Note that PHP 5 is needed.\r
+        */\r
+       /* \r
+        * Default methods follow.\r
+        * These methods will be overrided in PUBMED_TEMPLATE class.\r
+        */\r
+       public function sortPapers(){\r
+               $this->sortByAuthorName();\r
+       }\r
+       public function parse_header(){\r
+               return '<br />';\r
+       }\r
+       public function parse_footer(){\r
+               return '<br />';\r
+       }\r
+       public function parse($num,$pmid,$xml,$authors,$year,$journal,$volume,$pages,$title){\r
+               return <<<END\r
+\r
+{$this->parse_authors($authors)} ({$year})<br />\r
+<i>{$journal}</i> <b>{$volume}</b>, {$pages}<br />\r
+{$title}<br />\r
+<br />\r
+END;\r
+       }\r
+       public function parse_authors($authors, $and=', and '){\r
+               $num=count($authors);\r
+               switch($num){\r
+                       case 1:\r
+                               $result=$this->parse_author($authors[0]);\r
+                               break;\r
+                       case 2:\r
+                               $result=$this->parse_author($authors[0]).$and.$this->parse_author($authors[1]);\r
+                               break;\r
+                       default:\r
+                               $result=$this->parse_author($authors[0]);\r
+                               for($i=1;$i<$num;$i++){\r
+                                       if ($i==1) $result.=', ';\r
+                                       elseif ($i==$num-1) $result.=$and;\r
+                                       $result.=$this->parse_author($authors[$i]);\r
+                               }\r
+                               break;\r
+               }\r
+               return htmlspecialchars($result);\r
+       }\r
+       public function parse_author($author){\r
+               $result=$author->LastName.', ';\r
+               $initials=$author->Initials;\r
+               for($i=0;$i<strlen($initials);$i++) $result.=substr($initials,$i,1).'.';\r
+               return $result;\r
+       }\r
+       /*\r
+        * Following methods shouldn't be overrided.\r
+        */\r
+       protected $sorttext='',$sortdata=array(),$data=array();\r
+       public static function getTemplate($template,$sorttext){\r
+               // Static method.\r
+               // Define PUBLED_TEMPLATE class\r
+               if (!preg_match('/^[a-z0-9A-Z_]+$/',$template)) exit('Bad template name!');\r
+               $file=dirname(__FILE__)."/templates/{$template}.php";\r
+               if (!file_exists($file)) return false;\r
+               require_once($file);\r
+               $obj = new PUBMED_TEMPLATE;\r
+               $obj->sorttext=$sorttext;\r
+               return $obj;\r
+       }\r
+       public final function setData($more){\r
+               // $more is the $item->more.\r
+               if (!preg_match('#<MedlineCitation[^>]*>([\s\S]*?)</MedlineCitation>#',$more,$m)) return;\r
+               $xml="<?xml version='1.0'?>\r\n<document>\r\n$m[1]\r\n</document>";\r
+               $xml=simplexml_load_string($xml);\r
+               $pmid=(int)$xml->PMID;\r
+               $this->data[$pmid]=$xml;\r
+               //echo $xml->Article->AuthorList->Author[0]->LastName;\r
+               //echo "<br />\n";\r
+       }\r
+       public final function parse_all(){\r
+               echo $this->parse_header();\r
+               $num=0;\r
+               foreach($this->sortdata as $pmid) {\r
+                       $xml=$this->data[$pmid];\r
+                       $num++;\r
+                       // Get year\r
+                       $year=$xml->Article->Journal->JournalIssue->PubDate->Year;\r
+                       // Get journal name\r
+                       $journal=$xml->Article->Journal->ISOAbbreviation;\r
+                       // Get volume\r
+                       $volume=$xml->Article->Journal->JournalIssue->Volume;\r
+                       // Get paper title\r
+                       $title=$xml->Article->ArticleTitle;\r
+                       if (substr($title,-1,1)!=='.') $title.='.';\r
+                       // Get the start and end pages\r
+                       $pages=explode('-',(string)$xml->Article->Pagination->MedlinePgn);\r
+                       $pages=$pages[0].'-'.substr($pages[0],0,strlen($pages[0])-strlen($pages[1])).$pages[1];\r
+                       // Let's parse the citation\r
+                       echo $this->parse( (int)$num,(int)$pmid,$xml,$xml->Article->AuthorList->Author\r
+                               ,htmlspecialchars($year) // Don't use (int) because it may be like 2008a\r
+                               ,htmlspecialchars($journal)\r
+                               ,(int)$volume\r
+                               ,htmlspecialchars($pages)\r
+                               ,htmlspecialchars($title)\r
+                               );\r
+               }\r
+               echo $this->parse_footer();\r
+       }\r
+       /* Sort methods follow.\r
+        * note that these methods will be called from "sortPapers" method\r
+        */\r
+       public final function manualSort(){\r
+               // Set the sortdata array.\r
+               $lines=preg_split('/[\r\n]/',$this->sorttext,-1);\r
+               $result=array();\r
+               foreach($lines as $line){\r
+                       if (!preg_match('/^[\s]*PMID:[\s]*([0-9]+)/',$line,$m)) continue;\r
+                       $result[]=$m[1];\r
+               }\r
+               $this->sortdata=$result;\r
+       }\r
+       public final function sortByAuthorName(){\r
+               $citations=array();\r
+               $papers=array();\r
+               $i=0;\r
+               foreach($this->data as $pmid=>&$xml){\r
+                       $i++;\r
+                       // Get date\r
+                       $year=(int)$xml->Article->Journal->JournalIssue->PubDate->Year;\r
+                       $month=(string)$xml->Article->Journal->JournalIssue->PubDate->Month;\r
+                       $month=$this->month($month);\r
+                       $month=$month<10 ? "0$month" : "$month";\r
+                       $day=(int)$xml->Article->Journal->JournalIssue->PubDate->Day;\r
+                       $day=$day<10 ? "0$day" : "$day";\r
+                       $date="$year-$month-$day";\r
+                       // Get Authors\r
+                       $authors=$xml->Article->AuthorList->Author;\r
+                       $firstauthor=$authors[0]->LastName.', '.$authors[0]->Initials;\r
+                       $authornum=count($authors);\r
+                       switch($authornum){\r
+                               case 1:\r
+                                       $citation=$authors[0]->LastName.", $year";\r
+                                       break;\r
+                               case 2:\r
+                                       $citation=$authors[0]->LastName.' and '.$authors[1]->LastName.", $year";\r
+                                       break;\r
+                               default:\r
+                                       $citation=$authors[0]->LastName." et al., $year";\r
+                                       break;\r
+                       }\r
+                       if (!isset($citations[$citation])) $citations[$citation]=array();\r
+                       $citations[$citation][]=$pmid;\r
+                       // Construct the sort key and cache data\r
+                       $key="$firstauthor $year $citation $date $i";\r
+                       $papers[$key]=$pmid;\r
+               }\r
+               // Modify Year (for example, 2008 => 2008a, 2008b, etc.\r
+               $abc='abcdefghijklmnopqrstuvwxyz';\r
+               foreach($citations as $key=>$value){\r
+                       if (count($value)<2) continue;\r
+                       for ($i=0;$i<count($value);$i++){\r
+                               $pmid=$value[$i];\r
+                               $year=(int)$this->data[$pmid]->Article->Journal->JournalIssue->PubDate->Year;\r
+                               $this->data[$pmid]->Article->Journal->JournalIssue->PubDate->Year=(string)$year.substr($abc,$i,1);\r
+                       }\r
+               }\r
+               // Sort the data\r
+               ksort($papers);\r
+               // Let's get the result.\r
+               $result=array();\r
+               foreach($papers as $pmid) $result[]=$pmid;\r
+               $this->sortdata=$result;\r
+       }\r
+       protected final function month($month){\r
+               if (is_numeric($month)) return (int)$month;\r
+               switch(strtolower($month)){\r
+                       case 'january':\r
+                       case 'jan': return 1;\r
+                       case 'february':\r
+                       case 'feb': return 2;\r
+                       case 'march':\r
+                       case 'mar': return 3;\r
+                       case 'april':\r
+                       case 'apr': return 4;\r
+                       case 'may': return 5;\r
+                       case 'june':\r
+                       case 'jun': return 6;\r
+                       case 'july':\r
+                       case 'jul': return 7;\r
+                       case 'august':\r
+                       case 'aug': return 8;\r
+                       case 'september':\r
+                       case 'sep': return 9;\r
+                       case 'october':\r
+                       case 'oct': return 10;\r
+                       case 'november':\r
+                       case 'nov': return 11;\r
+                       case 'december':\r
+                       case 'dec': return 12;\r
+                       default: return 0;\r
+               }\r
+       }\r
+       \r
+}
\ No newline at end of file
diff --git a/NP_PubMed/trunk/pubmed/templates/Cell.php b/NP_PubMed/trunk/pubmed/templates/Cell.php
new file mode 100644 (file)
index 0000000..c3630b2
--- /dev/null
@@ -0,0 +1,12 @@
+<?php\r
+\r
+class PUBMED_TEMPLATE extends PUBMED_TEMPLATE_BASE {\r
+       public function parse($num,$pmid,$xml,$authors,$year,$journal,$volume,$pages,$title){\r
+               return <<<END\r
+\r
+{$this->parse_authors($authors)} ({$year}). {$title} {$journal} <i>{$volume}</i>, {$pages}<br />\r
+<br />\r
+\r
+END;\r
+       }\r
+}
\ No newline at end of file
diff --git a/NP_PubMed/trunk/pubmed/templates/PNAS.php b/NP_PubMed/trunk/pubmed/templates/PNAS.php
new file mode 100644 (file)
index 0000000..8e4389c
--- /dev/null
@@ -0,0 +1,19 @@
+<?php\r
+\r
+class PUBMED_TEMPLATE extends PUBMED_TEMPLATE_BASE {\r
+       public function parse($num,$pmid,$xml,$authors,$year,$journal,$volume,$pages,$title){\r
+               $year=(int)$year;\r
+               return <<<END\r
+\r
+{$this->parse_authors($authors,', & ')} ({$year}). {$journal} <b>{$volume}</b>, {$pages}<br />\r
+<br />\r
+\r
+END;\r
+       }\r
+       public function parse_author($author){\r
+               $result=$author->LastName.', ';\r
+               $initials=$author->Initials;\r
+               for($i=0;$i<strlen($initials);$i++) $result.=substr($initials,$i,1).'. ';\r
+               return substr($result,0,strlen($result)-1);\r
+       }\r
+}
\ No newline at end of file
diff --git a/NP_PubMed/trunk/pubmed/templates/default.php b/NP_PubMed/trunk/pubmed/templates/default.php
new file mode 100644 (file)
index 0000000..d1d2b69
--- /dev/null
@@ -0,0 +1,5 @@
+<?php\r
+\r
+class PUBMED_TEMPLATE extends PUBMED_TEMPLATE_BASE {\r
+       /* the default template uses everything from BASE class */\r
+}
\ No newline at end of file
index db6d078..f3fa06c 100644 (file)
@@ -209,18 +209,9 @@ Website: <a href="<%member(url)%>"><%member(url)%></a>
 <div id="container">\r
 <div class="content">\r
 \r
-<%PubMed(PageSwitch,,50)%>\r
-<%if(PageSwitch,limit,50)%>\r
-<div style="text-align:right;">Pages:\r
-<%PageSwitch(prev,prev)%>\r
-<%PageSwitch(index,2)%>\r
-<%PageSwitch(next,next)%>\r
-Page <%PageSwitch(num)%> of <%PageSwitch(total)%>\r
-</div>\r
-<%endif%>\r
-\r
-<%PubMed(parse,spring/index,50)%>\r
+<%PubMed(parse)%>\r
 \r
+<%endif%>\r
 </div>\r
 </div>\r
 \r