Permalink
Browse files

Add collectioncreator for adding things to collections automatically

  • Loading branch information...
1 parent 4fc89f7 commit df313c5b1b00f9159fb373bb392cd2076f553ba7 @holtzermann17 holtzermann17 committed Nov 10, 2013
Showing with 86 additions and 14 deletions.
  1. +62 −0 scripts/collectioncreator.drush
  2. +24 −14 scripts/nodecreator.drush
@@ -0,0 +1,62 @@
+#!/usr/local/bin/drush
+
+/* 1. Would be interesting to take a path and upload all of the items
+ in that path to a collection.
+*/
+
+$filepath = drush_shift();
+
+global $user;
+
+$manifest = $filepath . "/MANIFEST";
+$manifestHandle = fopen($manifest, 'r') or die("Error: can't open file " . $manifest);
+
+$metadata = $filepath . "/METADATA";
+$metadataHandle = fopen($metadata, 'r') or die("Error: can't open file " . $metadata);
+
+// Read the contents of the (simple) metadata file
+
+$title = rtrim(array_pop(explode(":",fgets($metadataHandle))), "\n");
+print_r("title: $title.".PHP_EOL);
+$description = rtrim(array_pop(explode(":",fgets($metadataHandle))),"\n");
+print_r("description: $description.".PHP_EOL);
+
+// create the collection
+
+$e = entity_create( 'node', array( 'type' => "collection" ) );
+node_object_prepare( $e );
+
+$collection = entity_metadata_wrapper( 'node', $e );
+$lang = $e->language;
+
+$collection->title = $title;
+
+$e->body['en'][0]['value'] = $description ;
+$e->body['und'][0]['format'] = 'tex_editor' ;
+
+$e->revision = 1; // Create new revision
+$e->log = "Added using script"; // Log message
+
+// Make sure the collection belongs to the PMBooks author
+$user->uid = 1000683;
+$e->uid = 1000683;
+$e->revision_uid = 1000683;
+
+$collection->save();
+$collectionId = $collection->getIdentifier();
+
+print_r("created a new collection with NID: $collectionId.".PHP_EOL);
+
+// Now add all the files in the manifest as new articles
+
+while (($line = fgets($manifestHandle)) !== FALSE ) {
+
+ $filename = rtrim($line, "\n");
+ print_r("Adding ".$filename." to the collection.".PHP_EOL);
+ $process_retval = exec("/home/planetary/beta/scripts/nodecreator.drush ". $filepath ." ". $filename);
+ $new_nid = rtrim(array_pop(explode(": ",$process_retval)), "\n");
+
+ print_r("Created new content with NID: " . $new_nid . ".".PHP_EOL);
+ // Now, somehow, we have to add the new nid to the $collectionId
+ collection_confirm_attachment_body($collectionId, $new_nid);
+}
@@ -1,15 +1,17 @@
#!/usr/local/bin/drush
-/* 1. Takes ONE parameter, which is the "basename" of the set of files
- to base the node on. The script will look for three files:
- basename.body, basename.pre, and basename.meta
+/* 1. Takes TWO parameters:
- The directory to look in is hardcoded in this file, for now.
+ - The directory to look in
+
+ - the "basename" of the set of files to base the node on.
+ The script will look for three files:
+ basename.body, basename.pre, and basename.meta
[See http://drush.ws/docs/shellscripts.html for more details on the
anatomy of drush scripts.]
- Sample metadata file:
+ Sample metadata file (basename.meta):
nodetype:article|exercise (assume article is the default, if nothing specified)
title:Test LaTeX Article From File
@@ -19,19 +21,19 @@
*/
+$filepath = drush_shift();
$basename = drush_shift();
-
-$base_file_path = "/home/shared-folder/HoTT-book/diced/";
-//$base_file_path = "/home/planetary/beta/scripts/";
-$body = $base_file_path . $basename . ".body";
+//$filepath = "/home/shared-folder/HoTT-book/diced/";
+//$filepath = "/home/planetary/beta/scripts/";
+$body = $filepath . '/' . $basename . ".body";
$bodyHandle = fopen($body, 'r') or die("Error: can't open file " . $body);
$body_contents = fread($bodyHandle, filesize($body));
-$preamble = $base_file_path . $basename . ".pre";
+$preamble = $filepath . '/' . $basename . ".pre";
$preambleHandle = fopen($preamble, 'r') or die("Error: can't open file " . $preamble);
$preamble_contents = fread($preambleHandle, filesize($preamble));
-$metadata = $base_file_path . $basename . ".meta";
+$metadata = $filepath . '/' . $basename . ".meta";
$metadataHandle = fopen($metadata, 'r') or die("Error: can't open file " . $metadata);
global $user;
@@ -53,6 +55,8 @@ print_r("msc: $msc.".PHP_EOL);
$section = rtrim(array_pop(explode(":",fgets($metadataHandle))),"\n");
print_r("section: $section.".PHP_EOL);
+$articleId=NULL;
+$problemId=NULL;
if ($nodetype=='article') {
$e = entity_create( 'node', array( 'type' => $nodetype ) );
@@ -85,7 +89,7 @@ if ($nodetype=='article') {
$article->save();
$articleId = $article->getIdentifier();
- print_r("new NID: $articleId.".PHP_EOL);
+ //print_r("new NID: $articleId.".PHP_EOL);
} else if ($nodetype=='exercise') {
$nodetype='problem';
@@ -119,7 +123,7 @@ if ($nodetype=='article') {
$problem->save();
$problemId = $problem->getIdentifier();
- print_r("new NID: $problemId.".PHP_EOL);
+ //print_r("new NID: $problemId.".PHP_EOL);
}
@@ -128,7 +132,13 @@ $cache = _drutexml_cache_read($key);
print_r("Cache Length: ".strlen($cache).PHP_EOL);
-return $articleID;
+if($articleId){
+ $newnid = $articleId;
+} else {
+ $newnid = $problemId;
+}
+
+print_r("New NID: " . $newnid . PHP_EOL);
/*
Output: "I created Node with this NID" > nodecreator.log

0 comments on commit df313c5

Please sign in to comment.