diff --git a/class/NodeJsonFetcher.php b/class/NodeJsonFetcher.php new file mode 100644 index 0000000..71071c2 --- /dev/null +++ b/class/NodeJsonFetcher.php @@ -0,0 +1,145 @@ +downloadNode($nid); + $store = array(); + if (!isset($node->author)) { + echo "No author for node ... devdrupal quirk!?!" . PHP_EOL; + continue; + } + $author = $node->author; + $this->fetchResource($store, $author); + + $taxonomies = $node->taxonomy_vocabulary_9; + foreach ($taxonomies as $resource) { + $this->fetchResource($store, $resource); + } + $comments = $node->comments; + foreach ($comments as $comment) { + // We could use $comment->uri if using Accept header and getting response + $result = $this->fetchResource($store, $comment); + $this->fetchResource($store, $result->author); + } + + foreach ($node->field_issue_files as $file) { + // $issue_file is a compound object of display + file + $result = $this->fetchResource($store, $file->file); + $this->fetchResource($store, $result->owner); + } + $node->ALL_META_DATA = $store; + return $node; + } + + /** + * Fetch and store resource as json. + * + * @param array &$store + * In memory structure of all already fetched resources. + * @param object $resource + * A drupal.org RestWs result. + */ + function fetchResource(&$store, $resource) { + $uri = $resource->resource . '/' . $resource->id; + $result = $this->fetchJson($uri); + $store[$resource->resource][$resource->id] = $result; + return $result; + } + + public function downloadNode($id) { + return $this->fetchJson("node/$id"); + } + + function fetchJson($uri) { + if (!$content = cache_get($uri)) { + $content = $this->fetchUri($uri); + } + //$this->assertJson($content, "Fetched json from $uri"); + $data = json_decode($content); + cache_set($uri, $data); + return $data; + } + + function fetchUri($uri) { + static $count = 0; + $count++; + + $url = $this->buildJsonUri($uri); + $options = array( + 'http' => array( + 'method' => "GET", + 'header' => "Accept-language: en\r\nAccept: application/json\r\n" . + "Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net + "User-Agent: Drush\r\n", + ), + // Is this the way to allow https invalid certificate? + // current status: NO! + // http://www.php.net/manual/en/context.ssl.php + 'ssl' => array( + 'allow_self_signed' => TRUE, + 'verify_peer' => FALSE, + ), + ); + //drush_download_file($url); + drush_log("Fetching ($count): $uri"); + $context = stream_context_create($options); + $file = @file_get_contents($url, false, $context); + if (FALSE === $file) { + $error = json_encode(error_get_last()); + //var_dump($error); + return $error; + } + return $file; + } + + /** + * Generate the full URL to fetch the given $uri. + * + * Depending on the Rest endpoint this could end with .json + * + * @param string $uri + * The resource URI given like node/12 or user/12 + */ + function buildJsonUri($uri) { + return $this->site . '/' . $uri . '.json'; + //return $this->site . '/' . $uri; + } + +} + +function cache_set($uri, $content) { + file_put_contents(__DIR__ . '/../tests/data/download/' . $uri . '.json', json_encode($content, JSON_PRETTY_PRINT)); +} + +function cache_get($uri) { + $path = __DIR__ . '/../tests'; + @mkdir($path); + @mkdir($path . '/data'); + @mkdir($path . '/data/download'); + @mkdir($path . '/data/download/node'); + @mkdir($path . '/data/download/comment'); + @mkdir($path . '/data/download/user'); + @mkdir($path . '/data/download/file'); + @mkdir($path . '/data/download/taxonomy_term'); + + $file = $path . '/data/download/' . $uri . '.json'; + // this fails on my system: why? + // mkdir(dirname($file), 0777, TRUE); + if (file_exists($file)) { + return file_get_contents($file); + } +} diff --git a/iqInternalTest.php b/iqInternalTest.php new file mode 100644 index 0000000..42bda36 --- /dev/null +++ b/iqInternalTest.php @@ -0,0 +1,68 @@ +assertFileExists($uri); + $data = file_get_contents($uri); + $this->assertFalse(empty($data), "Data is read"); + $json = json_decode($data, TRUE); + + $prepared = _drush_iq_parse_json($data); + $this->assertFalse(empty($prepared), "Processed into our dataset"); + } + + /** + * Fetches a set of nodes and their related resources. + * + * Note the list of issues changes depending on the $this->site used. + */ + public function testDownloadIssueData() { + // Not all nodes are on test env + // Run jQuery on an issue overview page + // list = [];jQuery("a[href^='/node/2']").each(function(){list.push(jQuery(this).attr('href').substring(6));}); list.join(","); + $nid_list = "2278541,2259261,2278415,2282599,2281609,2283281,2283293,2274167,2283969,2284025,2246665,2275499,2284017,2281419,2260043,2278715,2281475,2283367,2283851,2278965,2283977,2281659,2281699,2283929,2252713,2280195,2262275,2283877,2281325,2256679,2277981,2281905,2261083,2277281,2247779,2283637,2283301,2256521,2277753,2282161,2283717,2254153,2283711,2283703,2283553,2283675,2282519,2283601,2262247,2249089"; + $nids = explode(",", $nid_list); + + $fetcher = new NodeJsonFetcher(); + + foreach ($nids as $nid) { + $node = $fetcher->fetchFullNode($nid); + print_r($node); + echo json_encode($node, JSON_PRETTY_PRINT); + echo PHP_EOL; + } + } + + +}