Automatic publications lists from UKADS
2010 January 8
On this site my publications list is created automatically whenever it is viewed. This is possible using a little bit of php and the XML listing ability of the (UK) ADS server. I first developed this method to show the collected publications for all members of the University of Portsmouth’s Institute of Cosmology and Gravitation webpage.
My code, which you are welcome to adapt and use for your own purposes is given below. Note that if you are going to use this on a page that is accessed often then you should probably implement some form of caching.
<?php
function authors_process($authors, $surnames)
{
$nmax = 5;
$n = 0;
$m = 0;
$limited = array();
$showname = array();
foreach($authors as $a) {
$ns = explode(',', $a);
$surname = $ns[0];
$showname = false;
if ($surnames != null) {
foreach ($surnames as $s) {
if ($s == $surname) {
$surname = '' . $s . '';
$showname = true;
break;
}
}
}
if ($n < $nmax) {
$limited[] = $surname;
$m = $n;
} else if ($showname) {
if ($n + 1 != $m) {
$limited[] = '...';
}
$limited[] = $surname;
$m = $n;
}
$n = $n + 1;
}
if ($n + 1 != $m) {
$limited[] = 'et al.';
}
return implode(', ', $limited);
}
$ukads_url = 'http://ukads.nottingham.ac.uk/';
$ukads_url .= 'cgi-bin/nph-abs_connect?';
$ukads_url .= 'db_key=AST&db_key=PRE&qform=AST&';
$ukads_url .= 'start_nr=1&arxiv_sel=astro-ph&arxiv_sel=gr-qc&';
$ukads_url .= 'start_mon=&start_year=&nr_to_return=100&start_nr=1&';
$ukads_url .= 'jou_pick=ALL&article_sel=YES&ref_stems=&ALL&';
$ukads_url .= 'sort=NDATE&data_type=SHORT_XML&author=';
$n = 'Bamford, Steven P';
$names = array(urlencode($n));
$surnames = array('Bamford');
$ukads_url .= implode('%0D%0A', $names);
$ukads_url_normal = str_replace('SHORT_XML', 'SHORT', $ukads_url);
echo 'Automatically retrieved from UKADS.'."\n";
//Using normal php method
//$xmlstr = file_get_contents($ukads_url);
//Using Wordpress method:
$xmlstr = wp_remote_fopen($ukads_url);
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->record as $record) {
$authors = authors_process($record->author, $surnames);
$pubdate = explode(' ', $record->pubdate);
$year = $pubdate[1];
$link = '';
foreach ($record->link as $l) {
if ($l['type'] == 'ABSTRACT') {
$link = $l->url;
break;
}
}
echo "\n-----\n";
echo '[' . $record->title . '](' . $link . ")
\n";
echo $authors, ", ", $year, ",
\n", $record->journal, "\n";
}
?>
3 Responses
leave one →
Neat!
Now I just need to write some papers…
For the dummies: what is wp_remote_fopen() [line 57] and where would you get it?
Thanks for the good question Bo!
wp_remote_fopen() is a function provided by Wordpress. It should ‘just work’ for Wordpress users (assuming they have got php in posts working, e.g. using Exec-PHP.)
For non-Wordpress users, I originally used the file_get_contents PHP function. Note that this will often require some permissions to be set in the PHP configuration.
I’ve added the relevant line (commented out, which the code prettifier misleadingly capitalises) in the posted code.