Mercurial > hg > digitalobjects
comparison digitalobjects.import.php @ 0:6f6e07baad80 default tip
initial
| author | Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 02 Jun 2015 08:57:48 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:6f6e07baad80 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 function digitalcollection_import_from_file($filename,$nid){ | |
| 4 | |
| 5 | |
| 6 $handle = fopen($filename, "r"); | |
| 7 $contents = fread($handle, filesize($filename)); | |
| 8 fclose($handle); | |
| 9 | |
| 10 $node = node_load($nid); | |
| 11 | |
| 12 foreach(explode("\n",$contents) as $line){ | |
| 13 $node->field_objid[LANGUAGE_NONE][]['objid']=$line; | |
| 14 print $line; | |
| 15 } | |
| 16 | |
| 17 node_save($node); | |
| 18 } | |
| 19 | |
| 20 | |
| 21 | |
| 22 function digitalobjects_importPagesFromFile(){ | |
| 23 $filename = "/tmp/maps.json"; | |
| 24 # $filename = "/tmp/echo.json"; | |
| 25 $handle = fopen($filename, "r"); | |
| 26 $contents = fread($handle, filesize($filename)); | |
| 27 fclose($handle); | |
| 28 | |
| 29 $pages = drupal_json_decode($contents); | |
| 30 | |
| 31 $cnt=0; | |
| 32 foreach ($pages as $key => $page){ | |
| 33 | |
| 34 $page = base64_decode($page); | |
| 35 digitalobjects_importPage($page,$key); | |
| 36 $cnt++; | |
| 37 | |
| 38 } | |
| 39 | |
| 40 | |
| 41 } | |
| 42 | |
| 43 | |
| 44 function digitalobjects_importPage($page,$key){ | |
| 45 | |
| 46 $baseUrl = "http://localhost:18080/echo_nav/echo_pages/content/scientific_revolution/harriot/maps/"; | |
| 47 | |
| 48 $fnames = explode("/",$key); | |
| 49 | |
| 50 if ($fnames[1] != "maps"){ | |
| 51 dpm($fnames[0]); | |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 $fname = str_replace(".pt","1_1.png",$fnames[sizeof($fnames)-1]); | |
| 56 | |
| 57 $image = file_get_contents($baseUrl . $fname); // string | |
| 58 $file = file_save_data($image, 'public://' . $fname,FILE_EXISTS_REPLACE); | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 $node = new stdClass(); | |
| 64 $node->type = 'map_page'; | |
| 65 node_object_prepare($node); | |
| 66 | |
| 67 $node->body['und'][0]['value'] = check_markup($page,'full_html'); | |
| 68 | |
| 69 $node->body['und'][0]['format'] = 'full_html'; | |
| 70 | |
| 71 $node->path['alias']="harriot/maps/" . $fnames[sizeof($fnames)-1]; | |
| 72 | |
| 73 $node->title = "_".$key; | |
| 74 | |
| 75 | |
| 76 $node->language = LANGUAGE_NONE; | |
| 77 | |
| 78 | |
| 79 $node->field_echopath['und'][0]['value']=$key; | |
| 80 | |
| 81 $node->field_image[LANGUAGE_NONE]['0']['fid'] = $file->fid; | |
| 82 $node = node_submit($node); | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 /*$node->field_admintag['und'][0] = array ( | |
| 88 'tid' => $tid, | |
| 89 | |
| 90 );*/ | |
| 91 // Try to set your custom field | |
| 92 | |
| 93 $node = node_submit($node); | |
| 94 | |
| 95 node_save($node); | |
| 96 | |
| 97 } | |
| 98 | |
| 99 | |
| 100 | |
| 101 function digitalobjects_importCollectionFromFile() { | |
| 102 | |
| 103 | |
| 104 $filename = "/tmp/china.json"; | |
| 105 $handle = fopen($filename, "r"); | |
| 106 $contents = fread($handle, filesize($filename)); | |
| 107 fclose($handle); | |
| 108 | |
| 109 $collection = drupal_json_decode($contents); | |
| 110 digitalobjects_importCollection($collection); | |
| 111 } | |
| 112 | |
| 113 function digitalobjects_importCollection($collection,$tid){ | |
| 114 | |
| 115 | |
| 116 //schaue ob die collection schon gibt | |
| 117 $qs = "select count(*) as n from field_data_field_echopath where field_echopath_value='". $collection['echo_path'] ."'"; | |
| 118 $results = db_query($qs); | |
| 119 foreach($results as $result){ | |
| 120 | |
| 121 if ($result->n> 0){ | |
| 122 return; | |
| 123 } | |
| 124 } | |
| 125 dpm($collection['echo_path']); | |
| 126 $node = new stdClass(); | |
| 127 $node->type = 'digitalcollection'; | |
| 128 node_object_prepare($node); | |
| 129 | |
| 130 $node->body['und'][0]['value'] = check_markup($collection['description'],'full_html'); | |
| 131 | |
| 132 $node->body['und'][0]['format'] = 'full_html'; | |
| 133 | |
| 134 | |
| 135 $node->title = $collection['title']; | |
| 136 | |
| 137 | |
| 138 $node->language = LANGUAGE_NONE; | |
| 139 | |
| 140 | |
| 141 $node->field_echopath['und'][0]['value']=$collection['echo_path']; | |
| 142 $node->field_label['und'][0]['value']=$collection['label']; | |
| 143 $node = node_submit($node); | |
| 144 | |
| 145 | |
| 146 $cnt=0; | |
| 147 foreach ($collection['content'] as $cont){ | |
| 148 print $cont; | |
| 149 if (($cont != "") && ($cont !=null)){ | |
| 150 $node->field_objid['und'][$cnt]['objid']=$cont; | |
| 151 $cnt++; | |
| 152 | |
| 153 | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 $node->field_admintag['und'][0] = array ( | |
| 158 'tid' => $tid, | |
| 159 | |
| 160 ); | |
| 161 // Try to set your custom field | |
| 162 | |
| 163 $node = node_submit($node); | |
| 164 | |
| 165 node_save($node); | |
| 166 | |
| 167 } | |
| 168 | |
| 169 function digitalobjects_importCollections($tid) { | |
| 170 #filename = "/tmp/export.json"; | |
| 171 $filename = "/tmp/groups.json"; | |
| 172 # $filename = "/tmp/echo.json"; | |
| 173 $handle = fopen($filename, "r"); | |
| 174 $contents = fread($handle, filesize($filename)); | |
| 175 fclose($handle); | |
| 176 | |
| 177 $collections = drupal_json_decode($contents); | |
| 178 | |
| 179 $cnt=0; | |
| 180 foreach ($collections as $key => $collection){ | |
| 181 digitalobjects_importCollection($collection,$tid); | |
| 182 $cnt++; | |
| 183 | |
| 184 } | |
| 185 | |
| 186 } | |
| 187 | |
| 188 /* suche all e nodes mit echopath = $path */ | |
| 189 function digitalobjects_searchCollectionByPath($path){ | |
| 190 $results = db_query("SELECT * FROM {field_data_field_echopath} n where field_echopath_value ='" . $path . "'"); | |
| 191 | |
| 192 return $results; | |
| 193 } | |
| 194 | |
| 195 function digitalobjects_importStructure(){ | |
| 196 $filename = "/Library/WebServer/Documents/drupal-dev/testData/exportAllCollectionsAndSubCollectionAsJSON"; | |
| 197 # $filename = "/tmp/echo.json"; | |
| 198 $handle = fopen($filename, "r"); | |
| 199 $contents = fread($handle, filesize($filename)); | |
| 200 fclose($handle); | |
| 201 | |
| 202 $collections = drupal_json_decode($contents); | |
| 203 | |
| 204 foreach ($collections as $parentCollection => $value){ | |
| 205 /* finde node id von parent */ | |
| 206 $nodes = digitalobjects_searchCollectionByPath($parentCollection); | |
| 207 if (sizeof($nodes) == 0){ //parent existiert nicht | |
| 208 //generate parent | |
| 209 } else { | |
| 210 foreach($nodes as $node){ // sollte eigentlich nur eine geben aber wenn mehrere gibt, dann eben überall die entsprechenden einträge machen. | |
| 211 // gehe jetzt durch content | |
| 212 | |
| 213 $cnt=0; | |
| 214 foreach($value['content'] as $content){ | |
| 215 $childnodes = digitalobjects_searchCollectionByPath($content); | |
| 216 if (sizeof($nodes) == 0){ // gibt es nicht | |
| 217 //erzeuge | |
| 218 }else{ | |
| 219 | |
| 220 $nodeid = $node->entity_id; | |
| 221 $entities = entity_load("node",array($nodeid)); // hole die entities | |
| 222 foreach($entities as $entity){ | |
| 223 foreach($childnodes as $childnode){ // trage jetzt die childnodes ein. | |
| 224 | |
| 225 $newnode ="node/".$childnode->entity_id; | |
| 226 $qs = "SELECT count(*) as n FROM {field_data_field_collid} n where field_collid_objid ='" . $newnode . "' and entity_id ='". $nodeid . "'"; | |
| 227 $res = db_query($qs); | |
| 228 | |
| 229 foreach($res as $result){ | |
| 230 dpm($result); | |
| 231 dpm($qs); | |
| 232 if ($result->n == 0){ //gibt es noch nicht | |
| 233 | |
| 234 $entity->field_collid['und'][]['objid']=$newnode; | |
| 235 } | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 $entity = node_submit($entity); | |
| 240 | |
| 241 node_save($entity); | |
| 242 } | |
| 243 } | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 } | |
| 251 |
