<commit_before><?php

namespace Islandora\CollectionService\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Islandora\Chullo\Uuid\IUuidGenerator;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CollectionController {

    protected $uuidGenerator;

    public function __construct(IUuidGenerator $uuidGenerator) {
        $this->uuidGenerator = $uuidGenerator;
    }

    public function create(Application $app, Request $request, $id) {
        $tx = $request->query->get('tx', "");

        //Check for format
        $format = NULL;
        try {
          $format = \EasyRdf_Format::getFormat($contentType = $request->headers->get('Content-Type', 'text/turtle'));
        } catch (\EasyRdf_Exception $e) {
          $app->abort(415, $e->getMessage());
        }

        //Now check if body can be parsed in that format
        if ($format) { //EasyRdf_Format
          //@see http://www.w3.org/2011/rdfa-context/rdfa-1.1 for defaults
          \EasyRdf_Namespace::set('pcdm', 'http://pcdm.org/models#');
          \EasyRdf_Namespace::set('nfo', 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/v1.2/');
          \EasyRdf_Namespace::set('isl', 'http://www.islandora.ca/ontologies/2016/02/28/isl/v1.0/');
          \EasyRdf_Namespace::set('ldp', 'http://www.w3.org/ns/ldp');

          //Fake IRI, default LDP one for current resource "<>" is not a valid IRI!
          $fakeUuid = $this->uuidGenerator->generateV5("derp");
          $fakeIri = new \EasyRdf_ParsedUri('urn:uuid:' . $fakeUuid);

          $graph = new \EasyRdf_Graph();
          try {
            $graph->parse($request->getContent(), $format->getName(), $fakeIri);
          } catch (\EasyRdf_Exception $e) {
            $app->abort(415, $e->getMessage());
          }
          //Add a pcmd:Collection type
          $graph->resource($fakeIri, 'pcdm:Collection');

          //Check if we got an UUID inside posted RDF. We won't validate it here because it's the caller responsability
          if (NULL != $graph->countValues($fakeIri, 'nfo:uuid')) {
            $existingUuid = $graph->getLiteral($fakeIri, 'nfo:uuid');
            error_log("Has a UUID of " . $existingUuid);
            if (NULL != $graph->countValues($fakeIri, 'isl:hasURN')) {
              $graph->delete($fakeIri, 'isl:hasURN');
            }
            $graph->addResource($fakeIri, 'isl:hasURN', 'urn:uuid:'.$existingUuid); //Testing an Islandora Ontology!
          } else {
            //No UUID from the caller in RDF, lets put something there
            // Need a random UUID because there wasn't one provided.
            $newUuid = $this->uuidGenerator->generateV4();
            if (NULL != $graph->countValues($fakeIri, 'isl:hasURN')) {
              $graph->delete($fakeIri, 'isl:hasURN');
            }
            $graph->addLiteral($fakeIri,"nfo:uuid",$newUuid); //Keeps compat for now with other services
            $graph->addResource($fakeIri,"isl:hasURN",'urn:uuid:'.$newUuid); //Testing an Islandora Ontology
          }
          //Restore LDP <> IRI on serialised graph
          $pcmd_collection_rdf= str_replace($fakeIri, '', $graph->serialise('turtle'));
        }

        $urlRoute = $request->getUriForPath('/islandora/resource/');

        $subRequestPost = Request::create($urlRoute.$id, 'POST', array(), $request->cookies->all(), array(), $request->server->all(), $pcmd_collection_rdf);
        $subRequestPost->query->set('tx', $tx);
        $subRequestPost->headers->set('Content-Type', 'text/turtle');<commit_msg>Left an error_log in there<commit_after><?php

namespace Islandora\CollectionService\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Islandora\Chullo\Uuid\IUuidGenerator;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CollectionController {

    protected $uuidGenerator;

    public function __construct(IUuidGenerator $uuidGenerator) {
        $this->uuidGenerator = $uuidGenerator;
    }

    public function create(Application $app, Request $request, $id) {
        $tx = $request->query->get('tx', "");

        //Check for format
        $format = NULL;
        try {
          $format = \EasyRdf_Format::getFormat($contentType = $request->headers->get('Content-Type', 'text/turtle'));
        } catch (\EasyRdf_Exception $e) {
          $app->abort(415, $e->getMessage());
        }

        //Now check if body can be parsed in that format
        if ($format) { //EasyRdf_Format
          //@see http://www.w3.org/2011/rdfa-context/rdfa-1.1 for defaults
          \EasyRdf_Namespace::set('pcdm', 'http://pcdm.org/models#');
          \EasyRdf_Namespace::set('nfo', 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo/v1.2/');
          \EasyRdf_Namespace::set('isl', 'http://www.islandora.ca/ontologies/2016/02/28/isl/v1.0/');
          \EasyRdf_Namespace::set('ldp', 'http://www.w3.org/ns/ldp');

          //Fake IRI, default LDP one for current resource "<>" is not a valid IRI!
          $fakeUuid = $this->uuidGenerator->generateV5("derp");
          $fakeIri = new \EasyRdf_ParsedUri('urn:uuid:' . $fakeUuid);

          $graph = new \EasyRdf_Graph();
          try {
            $graph->parse($request->getContent(), $format->getName(), $fakeIri);
          } catch (\EasyRdf_Exception $e) {
            $app->abort(415, $e->getMessage());
          }
          //Add a pcmd:Collection type
          $graph->resource($fakeIri, 'pcdm:Collection');

          //Check if we got an UUID inside posted RDF. We won't validate it here because it's the caller responsability
          if (NULL != $graph->countValues($fakeIri, 'nfo:uuid')) {
            $existingUuid = $graph->getLiteral($fakeIri, 'nfo:uuid');
            // Delete isl:hasURN to make it match the uuid
            if (NULL != $graph->countValues($fakeIri, 'isl:hasURN')) {
              $graph->delete($fakeIri, 'isl:hasURN');
            }
            $graph->addResource($fakeIri, 'isl:hasURN', 'urn:uuid:'.$existingUuid); //Testing an Islandora Ontology!
          } else {
            //No UUID from the caller in RDF, lets put something there
            // Need a random UUID because there wasn't one provided.
            $newUuid = $this->uuidGenerator->generateV4();
            if (NULL != $graph->countValues($fakeIri, 'isl:hasURN')) {
              $graph->delete($fakeIri, 'isl:hasURN');
            }
            $graph->addLiteral($fakeIri,"nfo:uuid",$newUuid); //Keeps compat for now with other services
            $graph->addResource($fakeIri,"isl:hasURN",'urn:uuid:'.$newUuid); //Testing an Islandora Ontology
          }
          //Restore LDP <> IRI on serialised graph
          $pcmd_collection_rdf= str_replace($fakeIri, '', $graph->serialise('turtle'));
        }

        $urlRoute = $request->getUriForPath('/islandora/resource/');

        $subRequestPost = Request::create($urlRoute.$id, 'POST', array(), $request->cookies->all(), array(), $request->server->all(), $pcmd_collection_rdf);
        $subRequestPost->query->set('tx', $tx);
        $subRequestPost->headers->set('Content-Type', 'text/turtle');
{'fraction_non_alphanumeric': 0.15921362314827633, 'fraction_numerical': 0.009968157275370345, 'mean_word_length': 2.735263702171665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1459335', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2208, 'n_words': 442}