Created
November 24, 2012 00:00
-
-
Save ostephens/4137769 to your computer and use it in GitHub Desktop.
Procedure to clear references from RefWorks a/c via api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Removes all references from temp Refworks account that have not been modified for 30 minutes | |
* Then removes all empty folders | |
* Used by cron to clear down RefWorks temp account | |
*/ | |
public static function cleartemp() { | |
GLOBAL $SESSION; | |
$return = ""; | |
//kill any previous api sessions | |
parent::destroy_session(); | |
$rwun = references_lib::get_setting('tempun'); | |
$rwpw = references_lib::get_setting('temppw'); | |
if (!parent::check_session('',$rwun,$rwpw)) { | |
//failed | |
$return .= "Unable to authenticate for RefWorks temp account"; | |
return $return; | |
} | |
// Get all references and look for those older than an hour | |
$params = array('pgnum'=>1,'pgsize'=>1000,'style'=>0); | |
$result = parent::call_api('retrieve','all',$params); | |
// What if $result is false? | |
if ($result!==false) { | |
$resxml=new domDocument(); | |
$resxml->loadXML($result); | |
//get xml element that contains the references | |
$referencelist = $resxml->getElementsByTagName('reference'); | |
$references = array(); | |
// Ugly time hack required because RW returns US Eastern Time converted to "epoch time" rather than actual epoch time | |
$dt = new DateTime(NULL, new DateTimeZone('America/New_York')); | |
$rwclockoff = $dt->getOffset(); | |
for ($a=0, $max=$referencelist->length; $a<$max; $a++) { | |
$modifiedtime = $referencelist->item($a)->getElementsByTagName('md')->item(0)->nodeValue; | |
$id = $referencelist->item($a)->getAttribute('id'); | |
$mod_time = $modifiedtime/1000+$rwclockoff; | |
$timer = microtime(true); | |
$age = $timer-$mod_time; | |
if($age > 1800) { | |
$ids[] = $id; | |
} | |
} | |
// Create XML of reference IDs for deletion | |
$xml = ''; | |
if (count($ids) > 0) { | |
$xml .= '<RWRequest class="reference" method="get">'; | |
foreach ($ids as $id) { | |
$xml .= '<id>'.$id.'</id>'; | |
} | |
$xml .= '</RWRequest>'; | |
} | |
if (strlen($xml) > 0) { | |
// Need to send this xml for deletion | |
$result = parent::call_api('reference','delete','',$xml); | |
if ($result!==false) { | |
$return .= "Deleted ".count($ids)." references."; | |
} else { | |
$return .= "Could not delete references."; | |
} | |
} else { | |
$return .= "Did not find any references to delete"; | |
} | |
// Now have deleted references older than half an hour, time to clean up any empty folders | |
// Get all folders | |
$result = parent::call_api('folders','all',array('pgnum'=>1,'pgsize'=>1000)); | |
if (!$result) { | |
return false; | |
} | |
$resxml=new domDocument(); | |
$resxml->loadXML($result); | |
$folderlist = $resxml->getElementsByTagName('Folders'); | |
$folders = array(); | |
// Only interested in folders that have no references in them | |
for ($a=0, $max=$folderlist->length; $a<$max; $a++) { | |
if ($folderlist->item($a)->getAttribute('type') == 'user' && $folderlist->item($a)->getAttribute('nRefs') == '0') { | |
$folders[] = $folderlist->item($a)->nodeValue; | |
} | |
} | |
if ($folders == false) { | |
$folders = array(); | |
} | |
foreach ($folders as $folder) { | |
parent::call_api('folders','delete',array('oldValue'=>$folder)); | |
} | |
$return .= " Tried to delete ".count($folders)." empty folders"; | |
} else { | |
$return .= "Could not retrieve any references from RefWorks temp account"; | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment