From 128586ed0b833faa9530be1ff8ae086c6fd496b9 Mon Sep 17 00:00:00 2001 From: kmorimatsu Date: Sun, 18 Jan 2009 23:50:07 +0000 Subject: [PATCH] NP_PubMed v 0.2.0 Sorting methods supported. Now supports PNAS! git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@770 1ca29b6e-896d-4ea0-84a5-967f57386b96 --- NP_PubMed/trunk/NP_PubMed.php | 149 ++++++++++++++++++++++++++---- NP_PubMed/trunk/pubmed/index.php | 37 ++++++-- NP_PubMed/trunk/pubmed/index.xml | 3 +- NP_PubMed/trunk/pubmed/template.php | 61 +++++++++--- NP_PubMed/trunk/pubmed/templates/PNAS.php | 4 +- NP_PubMed/trunk/pubmed/templates/edit.php | 79 ++++++++++++++++ 6 files changed, 293 insertions(+), 40 deletions(-) create mode 100644 NP_PubMed/trunk/pubmed/templates/edit.php diff --git a/NP_PubMed/trunk/NP_PubMed.php b/NP_PubMed/trunk/NP_PubMed.php index 906af14..6f6e1fc 100644 --- a/NP_PubMed/trunk/NP_PubMed.php +++ b/NP_PubMed/trunk/NP_PubMed.php @@ -3,7 +3,7 @@ class NP_PubMed extends NucleusPlugin { function getName() { return 'NP_PubMed'; } function getMinNucleusVersion() { return 330; } function getAuthor() { return 'Katsumi'; } - function getVersion() { return '0.2.0'; } + function getVersion() { return '0.2.1'; } function getURL() {return 'http://japan.nucleuscms.org/wiki/plugins:authors:katsumi';} function getDescription() { return $this->getName().' plugin
'. @@ -23,7 +23,7 @@ class NP_PubMed extends NucleusPlugin { ' id int(11) not null auto_increment,'. ' manuscriptid int(11) not null default 0,'. ' itemid int(11) not null default 0,'. - ' sort int(11) not null default 0, '. + //' sort int(11) not null default 0, '. ' PRIMARY KEY id(id),'. ' UNIQUE KEY manuscriptid(manuscriptid,itemid) '. ') TYPE=MyISAM;'); @@ -32,7 +32,7 @@ class NP_PubMed extends NucleusPlugin { ' userid int(11) not null default 0,'. ' manuscriptname varchar(200) not null default "New Manuscript",'. ' templatename varchar(200) not null default "default",'. - //' sortdata text not null default "",'. + ' sorttext text not null default "",'. ' PRIMARY KEY manuscriptid(manuscriptid) '. ') TYPE=MyISAM;'); } @@ -41,6 +41,16 @@ class NP_PubMed extends NucleusPlugin { foreach($this->getTableList() as $table) sql_query('DROP TABLE IF EXISTS '.$table); } } + function init(){ + global $member; + $manuid=requestVar('manuscriptid'); + if (!$manuid) return; + if ($member->isAdmin()) return; + $ok=quickQuery('SELECT COUNT(*) as result FROM '.sql_table('plugin_pubmed_manuscripts'). + ' WHERE manuscriptid='.(int)$manuid. + ' AND userid='.(int)$member->getID() ); + if (!$ok) exit("You aren't allowed to manage this maniscript!"); + } function event_QuickMenu(&$data) { global $member,$CONF; $blogid=$CONF['DefaultBlog']; @@ -155,7 +165,7 @@ class NP_PubMed extends NucleusPlugin { $manuscripts=array(); while($row=mysql_fetch_row($res)) $manuscripts[$row[0]]=0; if (is_array($request)) foreach($request as $key=>$value) $manuscripts[$key]=(int)$value; - $res=sql_query('SELECT r.manuscriptid as manuscriptid, r.sort as sort FROM '. + $res=sql_query('SELECT r.manuscriptid as manuscriptid FROM '. sql_table('plugin_pubmed_manuscripts').' as m,'. sql_table('plugin_pubmed_references').' as r'. ' WHERE m.manuscriptid=r.manuscriptid'. @@ -242,15 +252,8 @@ class NP_PubMed extends NucleusPlugin { if (!$blog) return; $msid=intGetVar('manuscriptid'); if (!$msid) return; - $query='SELECT i.ibody as body, i.ititle as title, i.imore as more, r.sort as sort'. - ' FROM '.sql_table('item').' as i, '. - sql_table('plugin_pubmed_references').' as r,'. - sql_table('plugin_pubmed_manuscripts').' as m'. - ' WHERE i.inumber=r.itemid'. - ' AND r.manuscriptid='.(int)$msid. - ' AND m.manuscriptid='.(int)$msid. - ' AND m.userid='.(int)$mid. - ' ORDER BY i.ititle ASC'; + $action=postVar('batchaction'); + if ($action) $this->batchAction($action,$_POST['batch'],$msid); // Construct template object. require_once($this->getDirectory().'template.php'); @@ -260,18 +263,31 @@ class NP_PubMed extends NucleusPlugin { ' AND userid='.(int)$mid); $row=mysql_fetch_assoc($res); if (!$row) return; + echo '

Template: '.htmlspecialchars($row['templatename'])."

\n"; + if ($p1=='edit') $row['templatename']='edit'; $tobj=PUBMED_TEMPLATE_BASE::getTemplate($row['templatename']); if (!$tobj) { echo 'The template, "'.htmlspecialchars($row['templatename']).'" cannot be found'; - break; + return; } + $tobj->setSortText($row['sorttext']); + // Set all the data. + $query='SELECT i.inumber as itemid, i.ibody as body, i.ititle as title, i.imore as more'. + ' FROM '.sql_table('item').' as i, '. + sql_table('plugin_pubmed_references').' as r,'. + sql_table('plugin_pubmed_manuscripts').' as m'. + ' WHERE i.inumber=r.itemid'. + ' AND r.manuscriptid='.(int)$msid. + ' AND m.manuscriptid='.(int)$msid. + ' AND m.userid='.(int)$mid. + ' ORDER BY i.ititle ASC'; $res=sql_query($query); while($row=mysql_fetch_assoc($res)){ - $tobj->setData($row['more'],$row['sort']); + $pmid=$tobj->setData($row['more'],$row['itemid']); } // Sort the papers - if (!$tobj->manualSort()) $tobj->sortPapers(); + $tobj->sortPapers(); // Let's parse, finally. $tobj->parse_all(); break; @@ -445,5 +461,106 @@ if((document.location+'').indexOf('#pubmed')>0) np_pubmed_timer=setInterval("np_ checkTicket()) { + echo '

Invalid or expired ticket!

'; + return; + } + // Clean up batch data + foreach($batch as $key=>$itemid) $batch[$key]=(int)$itemid; + // Get citation information + $res=sql_query('SELECT * FROM '. sql_table('plugin_pubmed_references'). + ' WHERE manuscriptid='.(int)$msid); + $references=array(); + while($row=mysql_fetch_assoc($res)){ + $references[]=(int)$row['itemid']; + } + $sort=array(); + $sorttext=quickQuery('SELECT sorttext as result FROM '. sql_table('plugin_pubmed_manuscripts'). + ' WHERE manuscriptid='.(int)$msid); + foreach(explode(',',$sorttext) as $itemid){ + $itemid=(int)(trim($itemid)); + if (in_array($itemid,$references)) $sort[]=$itemid; + } + foreach($references as $itemid=>$row){ + if (!in_array($itemid,$sort)) $sort[]=$itemid; + } + // Take action + switch($action){ + case 'delete': + $in=''; + foreach($batch as $itemid){ + if (strlen($in)) $in.=','; + $in.=(int)$itemid; + } + sql_query('DELETE FROM '. sql_table('plugin_pubmed_references'). + ' WHERE itemid in ('.$in.')'. + ' AND manuscriptid='.(int)$msid); + echo '

Deleted references: '.$in.'

'; + return; + case 'moveup': + for($i=0;$iaction_manuscriptlist(); } - + private function _getSortMethod($tempname){ + // Note that $tempname is valid once. + static $ret; + if (isset($ret)) return $ret; + require_once(dirname(__FILE__).'/template.php'); + $tobj=PUBMED_TEMPLATE_BASE::getTemplate($tempname); + if (!$tobj) return false; + $tobj->setSortText(''); + $tobj->sortPapers(); + if ($tobj->getSortText()=='authorname') $ret='authorname'; + else $ret='manual'; + return $ret; + } private function _checkmanuscriptname($mname,$id=0){ global $member; $mid=$member->getID(); @@ -376,12 +389,20 @@ class PubMedAdmin extends BaseActions { $template=$row['templatename']; if (postVar('sure')=='yes') { $template=postVar('templatename'); - sql_query('UPDATE '.sql_table('plugin_pubmed_manuscripts').' SET'. - ' manuscriptname="'.addslashes($mname).'",'. - ' templatename="'.addslashes($template).'"'. - ' WHERE manuscriptid='.(int)$manuscriptid. - ' AND userid='.(int)$mid); - $this->template_parse('editmanuscript',array('mname'=>$mname),'notice'); + $sorttext=postVar('sorttext'); + $sortmethod=$this->_getSortMethod($template); + if ($sortmethod) { + if ($sortmethod=='authorname') $sorttext='authorname'; + sql_query('UPDATE '.sql_table('plugin_pubmed_manuscripts').' SET'. + ' manuscriptname="'.addslashes($mname).'",'. + ' templatename="'.addslashes($template).'",'. + ' sorttext="'.addslashes($sorttext).'"'. + ' WHERE manuscriptid='.(int)$manuscriptid. + ' AND userid='.(int)$mid); + $this->template_parse('editmanuscript',array('mname'=>$mname),'notice'); + } else { + echo "The template '".htmlspecialchars($template)."' does not exist."; + } return $this->action_manuscriptlist(); } // Get template files diff --git a/NP_PubMed/trunk/pubmed/index.xml b/NP_PubMed/trunk/pubmed/index.xml index 250087d..ccdf69c 100644 --- a/NP_PubMed/trunk/pubmed/index.xml +++ b/NP_PubMed/trunk/pubmed/index.xml @@ -209,8 +209,9 @@ Are you sure?   - <%ticket%> + + diff --git a/NP_PubMed/trunk/pubmed/template.php b/NP_PubMed/trunk/pubmed/template.php index 583422f..7e547fc 100644 --- a/NP_PubMed/trunk/pubmed/template.php +++ b/NP_PubMed/trunk/pubmed/template.php @@ -9,12 +9,8 @@ class PUBMED_TEMPLATE_BASE { * Default methods follow. * These methods will be overrided in PUBMED_TEMPLATE class. */ - public function manualSort(){ - // If the order of manuscript is manually sorted (PNAS etc), - // this method must return true. - return false; - } public function sortPapers(){ + // Either manualSort or sortByAuthorName $this->sortByAuthorName(); } public function parse_header(){ @@ -61,7 +57,7 @@ END; /* * Following methods shouldn't be overrided. */ - protected $sortdata=array(),$data=array(); + protected $sorteddata=array(),$data=array(); public static final function getTemplate($template){ // Static method. // Define PUBLED_TEMPLATE class @@ -72,22 +68,49 @@ END; $obj = new PUBMED_TEMPLATE; return $obj; } - public final function setData($more,$sort){ + protected $sortarray=false; + public final function setSortText($text){ + if ($text=='authorname') { + $this->sortarray=false; + return; + } + $array=array(); + $i=0; + foreach(explode(',',$text) as $itemid){ + $array[(int)$itemid]=$i; + $i++; + } + $this->sortarray=$array; + } + public final function getSortText(){ + if ($this->sortarray===false) return 'authorname'; + $ret=''; + $s=$this->sorteddata; + $itemid=$this->itemid; + ksort($s); + foreach($s as $pmid){ + if (strlen($ret)) $ret.=','; + $ret.=$itemid[$pmid]; + } + return $ret; + } + protected $itemid=array(); // PMID <-> itemid translation table. + public final function setData($more,$itemid){ // $more is the $item->more. if (!preg_match('#]*>([\s\S]*?)#',$more,$m)) return; $xml="\r\n\r\n$m[1]\r\n"; $xml=simplexml_load_string($xml); $pmid=(int)$xml->PMID; $this->data[$pmid]=$xml; - if (!isset($this->sortdata[$sort])) $this->sortdata[$sort]=$pmid; - else $this->sortdata[]=$pmid; + $this->sorteddata[]=$pmid; + $this->itemid[$pmid]=$itemid; } public final function parse_all(){ echo $this->parse_header(); $num=0; - $sortdata=$this->sortdata; - ksort($sortdata); - foreach($sortdata as $pmid) { + $sorteddata=$this->sorteddata; + ksort($sorteddata); + foreach($sorteddata as $pmid) { $xml=$this->data[$pmid]; $num++; // Get year @@ -116,7 +139,19 @@ END; /* Sort methods follow. * note that these methods will be called from "sortPapers" method */ + public final function manualSort(){ + $itemid=$this->itemid; + $position=$this->sortarray; + $newarray=array(); + foreach($this->sorteddata as $pmid){ + $i=$itemid[$pmid]; + if (isset($position[$i])) $newarray[$position[$i]]=$pmid; + else $newarray[]=$pmid; + } + $this->sorteddata=$newarray; + } public final function sortByAuthorName(){ + $this->sortarray=false; $citations=array(); $papers=array(); $i=0; @@ -166,7 +201,7 @@ END; // Let's get the result. $result=array(); foreach($papers as $pmid) $result[]=$pmid; - $this->sortdata=$result; + $this->sorteddata=$result; } protected final function month($month){ if (is_numeric($month)) return (int)$month; diff --git a/NP_PubMed/trunk/pubmed/templates/PNAS.php b/NP_PubMed/trunk/pubmed/templates/PNAS.php index 7062f97..890938e 100644 --- a/NP_PubMed/trunk/pubmed/templates/PNAS.php +++ b/NP_PubMed/trunk/pubmed/templates/PNAS.php @@ -1,8 +1,8 @@ manualSort(); } public function _parse_header(){ return '

    '; diff --git a/NP_PubMed/trunk/pubmed/templates/edit.php b/NP_PubMed/trunk/pubmed/templates/edit.php new file mode 100644 index 0000000..8e202a7 --- /dev/null +++ b/NP_PubMed/trunk/pubmed/templates/edit.php @@ -0,0 +1,79 @@ +sortarray=='authorname') $this->sortByAuthorName(); + else $this->manualSort(); + } + public function parse_header(){ + global $manager; + // Construct ticket hidden + ob_start(); + $manager->AddTicketHidden(); + $ticket=ob_get_contents(); + ob_end_clean(); + // Construct options + $options=<<- select action - + +END; + if ($this->sortarray!==false) $options.=<<Move up + + + +END; + // Everything done. Return the result. + return << +{$ticket} + +the selected items. +    + +    +( + Select all - + Unselect all +) + +END; + } + public function parse($num,$pmid,$xml,$authors,$year,$journal,$volume,$pages,$title){ + $itemid=$this->itemid[$pmid]; + if (isset($_POST['batch'])) $checked = in_array($itemid,$_POST['batch']) ? ' checked="checked"' : ''; + else $checked=''; + return << + +{$this->parse_authors($authors)} ({$year}) {$journal} {$volume}, {$pages} +

    + +END; + } + public function parse_footer(){ + return << + + +END; + } + +} \ No newline at end of file -- 2.11.0