. * * * User defined settings * ~~~~~~~~~~~~~~~~~~~~~ * If you wish to run TIPS on your own server, you will need to change the three settings below. * You will also need to make sure that PHP is running with the sessions module installed and the HttpRequest extension available. * TIPS can use a lot of memory, storage, and bandwidth -- make sure you keep an eye on these. * */ // How should TIPS identify itself to Entrez when requesting data? $ncbi_Tool = "TIPS"; /* * You will also need to edit 'lib/Variables.php' * * * Changelog * ~~~~~~~~~ * * ********************************************************************************************************************************************/ error_reporting(E_ALL); ini_set('display_errors', '8'); //ini_set('max_execution_time', '120'); require_once("lib/Variables.php"); include_once("lib/eSearch.php"); session_start(); if (isset($_POST['NewSession'])) session_unset(); // Check if this is a new search and, if so, delete old session variables $PhpEntrezTool = new Tips(); // Run the object class Tips { var $PostData; // array - Details from the search form var $Searches; // array - Search terms, results, and related data var $Coordinates; // array - Results, in the form of coordinates var $Stage; // array - Our position in the search var $Html; var $Title; var $ImgName; var $GraphTitle; function Tips() { $this->Html; if (isset($_POST['NewSession'])) { // Extract post data $_SESSION['began'] = microtime(true); // Set start time $this->PostData = array( "TrendsStart" => $_POST['TrendsStart'], "TrendsEnd" => $_POST['TrendsEnd'], "BreakDown" => $_POST['BreakDown'], "GraphWidth" => $_POST['GraphWidth'], "GraphHeight" => $_POST['GraphHeight'], "LineColours" => explode(",", $_POST['LineColours']), "BackGround" => $_POST['BackGround'], ); if (!empty($_POST['GraphTitle'])) $this->GraphTitle = $_POST['GraphTitle']; else $this->GraphTitle = "Publishing Trends"; $this->PostData['Normalize'] = false; if (!empty($_POST['Normalize'])) $this->PostData['Normalize'] = true; if ($this->PostData['TrendsEnd'] > date('Y')) $this->PostData['TrendsEnd'] = date('Y'); for ($i = 1; $i <= 10; $i++) { $index = "SearchString-" . $i; if (!empty($_POST[$index])) $this->Searches[$i]['SearchString'] = $_POST[$index]; } $this->Stage = array("searches", 1); } else { $this->PostData = $_SESSION['PostData']; $this->Searches = $_SESSION['Searches']; $this->Stage = $_SESSION['Stage']; } if ($this->Stage[0] == "searches") { foreach ($this->Searches as $k => $v) { //if (isset($this->Stage[1]) and ($this->Stage[1] > $k)) continue; if ($this->PostData['BreakDown'] == "month") $this->GetCountsByMonth($k); else $this->GetCountsByYear($k); $this->Stage[1] = $k + 1; } $this->Stage[0] = "post_search"; $this->Html .= "

Completed searches... building your graph...

"; $this->SaveReload(); } if (!empty($this->PostData['Normalize'])) $this->Normalize(); $this->MakePng(); if ($this->PostData['BreakDown'] == "month") $this->PrintResultsMonth(); else $this->PrintResultsYear(); } function GetCountsByMonth($Id) { $Year = $this->PostData['TrendsStart']; $Month = 1; while (true) { if (!isset($this->Searches[$Id]['Counts'][$Year][$Month])) { try { $MinDate = array($Year, $Month, 1); $MinDate = implode("/", $MinDate); $MaxDate = array($Year, $Month, lastday($Month, $Year)); $MaxDate = implode("/", $MaxDate); $eSearch = new eSearch( 'pubmed', $this->Searches[$Id]['SearchString'], $MinDate, $MaxDate, null, 'count' ); $eSearch->Run(2); $this->Searches[$Id]['Counts'][$Year][$Month] = $eSearch->Count; } catch (Exception $e) { $this->Html .= "

Warning: caught an exception when trying to retreive the XML data from PubMed. I am going to reload the page and hope that the problem goes away. The error given was: " . $e->getMessage() . "

\n"; $this->SaveReload(); exit; } } $Month += 1; if ($Month > 12) { $Year += 1; $Month = 1; } if ($Year > $this->PostData['TrendsEnd']) break; continue; } } function GetCountsByYear($Id) { $Year = $this->PostData['TrendsStart']; while (true) { if (!isset($this->Searches[$Id]['Counts'][$Year])) { try { $MinDate = array($Year, "01", "01"); $MinDate = implode("/", $MinDate); $MaxDate = array($Year, "12", "31"); $MaxDate = implode("/", $MaxDate); $eSearch = new eSearch( 'pubmed', $this->Searches[$Id]['SearchString'], $MinDate, $MaxDate, null, 'count' ); $eSearch->Run(2); $this->Searches[$Id]['Counts'][$Year] = $eSearch->Count; } catch (Exception $e) { $this->Html .= "

Warning: caught an exception when trying to retreive the XML data from PubMed. I am going to reload the page and hope that the problem goes away. The error given was: " . $e->getMessage() . "

\n"; $this->SaveReload(); exit; } } $Year += 1; if ($Year > $this->PostData['TrendsEnd']) break; continue; } } function Normalize() { $this->Html .= "

Normalizing...

"; if ($this->PostData['BreakDown'] == "year") { $Year = $this->PostData['TrendsStart']; while (true) { try { $MinDate = array($Year, "01", "01"); $MinDate = implode("/", $MinDate); $MaxDate = array($Year, "12", "31"); $MaxDate = implode("/", $MaxDate); $eSearch = new eSearch( 'pubmed', $MinDate . ':' . $MaxDate . '[PDAT]', $MinDate, $MaxDate, null, 'count' ); $eSearch->Run(2); $Normalize[$Year] = $eSearch->Count; } catch (Exception $e) { $this->Html .= "

Warning: caught an exception when trying to retreive the XML data from PubMed. I am going to reload the page and hope that the problem goes away. The error given was: " . $e->getMessage() . "

\n"; $this->SaveReload(); exit; } $Year += 1; if ($Year > $this->PostData['TrendsEnd']) { foreach ($this->Searches as $Id => &$Search) { foreach ($Search['Counts'] as $Y => &$C) { $C = ($C / $Normalize[$Y]) * 100; } } break; } else continue; } } /* elseif ($this->PostData['BreakDown'] == "month") { $Year = $this->PostData['TrendsStart']; $Month = 1; while (true) { try { $MinDate = array($Year, $Month, 1); $MinDate = implode("/", $MinDate); $MaxDate = array($Year, $Month, lastday($Month, $Year)); $MaxDate = implode("/", $MaxDate); $eSearch = new eSearch( 'pubmed', '*', $MinDate, $MaxDate, null, 'count' ); $eSearch->Run(2); $this->Searches[$Id]['Counts'][$Year][$Month] = $eSearch->Count; } catch (Exception $e) { $this->Html .= "

Warning: caught an exception when trying to retreive the XML data from PubMed. I am going to reload the page and hope that the problem goes away. The error given was: " . $e->getMessage() . "

\n"; $this->SaveReload(); exit; } $Month += 1; if ($Month > 12) { $Year += 1; $Month = 1; } if ($Year > $this->PostData['TrendsEnd']) break; continue; } } */ return true; } function MakePng() { Global $ncbi_CachePath; include_once("Image/Graph.php"); require_once 'Image/Canvas.php'; //$Graph =& Image_Graph::factory('graph', array($this->PostData['GraphWidth'], $this->PostData['GraphHeight'])); $Proportions = array( (int)$this->PostData['GraphWidth'], (int)$this->PostData['GraphHeight'] ); // create a PNG canvas and enable antialiasing (canvas implementation) $Canvas =& Image_Canvas::factory('png', array('width' => (int)$this->PostData['GraphWidth'], 'height' => (int)$this->PostData['GraphHeight'], 'antialias' => 'native')); // create the graph $Graph =& Image_Graph::factory('graph', $Canvas); //$Graph =& Image_Graph::factory('graph', $Proportions); // add a TrueType font putenv('GDFONTPATH=/usr/share/fonts/truetype'); $Font1 =& $Graph->addNew('font', 'Vera.ttf'); $Font1->setSize(11); $Graph->setFont($Font1); $Graph->add( Image_Graph::vertical( Image_Graph::factory('title',array($this->GraphTitle,14)), Image_Graph::vertical( $Plotarea = Image_Graph::factory('plotarea'), $Legend = Image_Graph::factory('legend'), 90 ), 5 ) ); $Font2 =& $Graph->addNew('font', 'Vera.ttf'); $Font2->setSize(8); $Graph->setFont($Font2); foreach ($this->Searches as $Id => $Search) { $Dataset[$Id] =& Image_Graph::factory('dataset'); $Dataset[$Id]->setName($Search['SearchString']); foreach ($Search['Counts'] as $Year => $Value) { if (is_array($Value)) { foreach ($Value as $Month => $Count) { $Dataset[$Id]->addPoint("{$Year}-" . ((strlen($Month) == 1) ? '0' . $Month : $Month), $Count); } } else $Dataset[$Id]->addPoint($Year, $Value); } $Plot[$Id] =& $Plotarea->addNew('line', &$Dataset[$Id]); $Plot[$Id]->setLineColor(trim($this->PostData['LineColours'][$Id-1])); } if (isset($this->PostData['BackGround'])) { if ($this->PostData['BackGround'] == "grey") $Plot[1]->setBackgroundColor( 'lightgrey' ); elseif ($this->PostData['BackGround'] == "lightgrey-grey") $Plot[1]->setBackgroundColor( Image_Graph::factory( 'gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'gray', 'lightgrey') ) ); elseif ($this->PostData['BackGround'] == "grey-lightgrey") $Plot[1]->setBackgroundColor( Image_Graph::factory( 'gradient', array(IMAGE_GRAPH_GRAD_VERTICAL, 'lightgrey', 'gray') ) ); elseif ($this->PostData['BackGround'] == "blue") $Plot[1]->setBackgroundColor( 'lightblue' ); } $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y); if (!empty($this->PostData['Normalize'])) $AxisY->setTitle('Publications (percentage of total)', 'vertical'); else $AxisY->setTitle('Publications', 'vertical'); $GridY =& $Plotarea->addNew('line_grid', IMAGE_GRAPH_AXIS_Y); $GridY->setLineColor('silver@0.5'); $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X); $AxisX->setFontAngle('90'); if (($this->PostData['BreakDown'] == "month") and (($this->PostData['TrendsEnd'] - $this->PostData['TrendsStart']) > 5)) $AxisX->setLabelInterval(12); $Graph->setBorderColor('black@0.2'); $Legend->setPlotarea($Plotarea); $this->ImgName = md5(date('Y-m-d H:i:s')); $filename = $ncbi_CachePath . "tips/" . $this->ImgName . ".png"; $Graph->done(array("filename" => $filename)); } /******************************************************************************************************************************************** * Function: PrintResultsMonth() and PrintResultsYear() * - Output HTML with a link to the image script which will produce the graph */ function PrintResultsMonth() { $this->Html .= "

ImgName . ".png\"/>

Cached images are regularly deleted: save a copy if you want to keep it!

\n"; $this->Html .= "\n"; foreach ($this->Searches as $k => $v) { $this->Html .= "\n"; } $this->Html .= "\n"; $Year = $this->PostData['TrendsStart']; $Month = 1; while (true) { $this->Html .= "\n"; foreach ($this->Searches as $k => $v) { $this->Html .= "\n"; } $this->Html .= "\n"; $Month += 1; if ($Month > 12) { $Year += 1; $Month = 1; } if ($Year > $this->PostData['TrendsEnd']) break; continue; } $this->Html .= "
 " . $v['SearchString'] . "
{$Year}-{$Month}" . $v['Counts'][$Year][$Month] . "
\n"; $this->Html .= "

Content from pubmed used under NLM terms of use (NLM disclaimer).

\n"; $this->Title = "TiPS: Search complete."; $this->OutputHtml(); } function PrintResultsYear() { $this->Html .= "

ImgName . ".png\"/>

Cached images are regularly deleted: save a copy if you want to keep it!

\n"; $this->Html .= "\n"; foreach ($this->Searches as $k => $v) { $this->Html .= "\n"; } $this->Html .= "\n"; $Year = $this->PostData['TrendsStart']; while (true) { $this->Html .= "\n"; foreach ($this->Searches as $k => $v) { $this->Html .= "\n"; } $this->Html .= "\n"; $Year += 1; if ($Year > $this->PostData['TrendsEnd']) break; continue; } $this->Html .= "
 " . $v['SearchString'] . "
{$Year}" . $v['Counts'][$Year] . "
\n"; $this->Html .= "

Content from pubmed used under NLM terms of use (NLM disclaimer).

\n"; $this->Title = "TiPS: Search complete."; $this->OutputHtml(); } /******************************************************************************************************************************************** * Function: SaveReload() * - Saves session variables and then outputs HTML with the reload clause set */ function SaveReload() { $_SESSION['PostData'] = $this->PostData; $_SESSION['Searches'] = $this->Searches; $_SESSION['Stage'] = $this->Stage; $this->Title = "Error!"; $this->OutputHtml(true); } /******************************************************************************************************************************************** * Function: OutputHtml( Reload ) * Formats the page as HTML */ function OutputHtml($Reload = false) { $Title = $this->Title; $Html = $this->Html; if ($Reload) $Reload = "\n"; else $Reload = ""; $NavBar = file_get_contents("NavBar.php"); $Time = round((microtime(true) - $_SESSION['began']), 2); // Calculate the time elapsed echo << $Title $Reload $NavBar

TiPSbeta: Trends in publishing science

$Html

... time elapsed: $Time seconds.

END; exit; } } // CC-NC-BY-SA -- There is probably a better way of doing things than this, but it will do for testing purposes function lastday($month = '', $year = '') { if (empty($month)) { $month = date('m'); } if (empty($year)) { $year = date('Y'); } $result = strtotime("{$year}-{$month}-01"); $result = strtotime('-1 second', strtotime('+1 month', $result)); return date('d', $result); } ?>