<commit_before><?php
namespace Repositories\Indexers;

class Recipe
{
  const DEFAULT_INDEX = "recipe";
  const ID_FIELD = "repo_id";

  public function __construct()
  {
    \Event::listen("cataclysm.newObject", function ($repo, $object) {
      $this->getIndexes($repo, $object);
    });

    \Event::listen("cataclysm.finishedLoading", function($repo)
    {
      $this->finishLoading($repo);
    });
  }

  private function finishLoading($repo)
  {
    foreach($repo->all(self::DEFAULT_INDEX) as $id)
    {
      $recipe = $repo->get(self::DEFAULT_INDEX, $id);
      // search for all the items with the apropiate qualities
      if (isset($recipe->qualities)) {
        foreach ($recipe->qualities as $group) {
          foreach($repo->all("quality.$group->id") as $id=>$item) {
            $item = \App::make("Item");
            $item->load($repo->get("item", $id));
            if($item->qualityLevel($group->id)<$group->level)
              continue;
            $this->linkIndexes($repo, 'toolFor', $id, $recipe);
          }
        }
      }
    }
  }

  private function linkIndexes($repo, $key, $id, $recipe)
  {
    // NONCRAFT recipes go directly to the disassembly index, 
    // they are not needed anywhere else.
    if ($key=="recipes" 
      and $recipe->category=="CC_NONCRAFT") {
      $repo->addIndex("item.disassembly.$id", $recipe->repo_id, $recipe->repo_id);
      return;
    }

    // reversible recipes go to the disassembly index,
    // but they're used to craft, so process further indexes.
    if ($key=="recipes" 
      and isset($recipe->reversible) 
      and $recipe->reversible=="true") {
      $repo->addIndex("item.disassembly.$id", $recipe->repo_id, $recipe->repo_id);
    }

    if ($key=="toolFor") {
      // create a list of recipe categories, excluding NONCRAFT.
      if ($recipe->category!="CC_NONCRAFT")
        $repo->addIndex("item.categories.$id", $recipe->category, $recipe->category);

      // create a list of tools per category for this object.
      $repo->addIndex("item.toolForCategory.$id.$recipe->category", 
        $recipe->repo_id, $recipe->repo_id);
    }

    $repo->addIndex("item.$key.$id", $recipe->repo_id, $recipe->repo_id);
  }

  private function getIndexes($repo, $object)
  {
    if ($object->type=="recipe") {
      $recipe = $object;

      $repo->addIndex(self::DEFAULT_INDEX, $recipe->repo_id, $recipe->repo_id);

      if (isset($recipe->result)) {
        $this->linkIndexes($repo, "recipes", $recipe->result, $recipe);
        if (isset($recipe->book_learn)) {
          foreach($recipe->book_learn as $learn) {
            $this->linkIndexes($repo, "learn", $learn[0], $recipe);
          }
        }
      }

      if (isset($recipe->tools)) {
        foreach($recipe->tools as $group) {
          foreach($group as $tool) {
            list($id, $amount) = $tool;
            $this->linkIndexes($repo, "toolFor", $id, $recipe);
          }
        }
      }

      if (isset($recipe->components)) {
        foreach($recipe->components as $group) {
          foreach($group as $component) {
            list($id, $amount) = $component;
            $this->linkIndexes($repo, "toolFor", $id, $recipe);

            if($recipe->category=="CC_NONCRAFT" 
              and isset($recipe->reversible)
              and $recipe->reversible=="true")
            {           

              $repo->addIndex("item.disassembledFrom.$id", $recipe->repo_id, $recipe->repo_id);
            }
          }
        }
      }
    }
  }

  public function model()
  {
    return \App::make("Recipe");
  }
}
<commit_msg>Fix disassembly links.<commit_after><?php
namespace Repositories\Indexers;

class Recipe
{
  const DEFAULT_INDEX = "recipe";
  const ID_FIELD = "repo_id";

  public function __construct()
  {
    \Event::listen("cataclysm.newObject", function ($repo, $object) {
      $this->getIndexes($repo, $object);
    });

    \Event::listen("cataclysm.finishedLoading", function($repo)
    {
      $this->finishLoading($repo);
    });
  }

  private function finishLoading($repo)
  {
    foreach($repo->all(self::DEFAULT_INDEX) as $id)
    {
      $recipe = $repo->get(self::DEFAULT_INDEX, $id);
      // search for all the items with the apropiate qualities
      if (isset($recipe->qualities)) {
        foreach ($recipe->qualities as $group) {
          foreach($repo->all("quality.$group->id") as $id=>$item) {
            $item = \App::make("Item");
            $item->load($repo->get("item", $id));
            if($item->qualityLevel($group->id)<$group->level)
              continue;
            $this->linkIndexes($repo, 'toolFor', $id, $recipe);
          }
        }
      }
    }
  }

  private function linkIndexes($repo, $key, $id, $recipe)
  {
    // NONCRAFT recipes go directly to the disassembly index, 
    // they are not needed anywhere else.
    if ($key=="recipes" 
      and $recipe->category=="CC_NONCRAFT") {
      $repo->addIndex("item.disassembly.$id", $recipe->repo_id, $recipe->repo_id);
      return;
    }

    // reversible recipes go to the disassembly index,
    // but they're used to craft, so process further indexes.
    if ($key=="recipes" 
      and isset($recipe->reversible) 
      and $recipe->reversible=="true") {
      $repo->addIndex("item.disassembly.$id", $recipe->repo_id, $recipe->repo_id);
    }

    if ($key=="toolFor") {
      // create a list of recipe categories, excluding NONCRAFT.
      if ($recipe->category!="CC_NONCRAFT")
        $repo->addIndex("item.categories.$id", $recipe->category, $recipe->category);

      // create a list of tools per category for this object.
      $repo->addIndex("item.toolForCategory.$id.$recipe->category", 
        $recipe->repo_id, $recipe->repo_id);
    }

    $repo->addIndex("item.$key.$id", $recipe->repo_id, $recipe->repo_id);
  }

  private function getIndexes($repo, $object)
  {
    if ($object->type=="recipe") {
      $recipe = $object;

      $repo->addIndex(self::DEFAULT_INDEX, $recipe->repo_id, $recipe->repo_id);

      if (isset($recipe->result)) {
        $this->linkIndexes($repo, "recipes", $recipe->result, $recipe);
        if (isset($recipe->book_learn)) {
          foreach($recipe->book_learn as $learn) {
            $this->linkIndexes($repo, "learn", $learn[0], $recipe);
          }
        }
      }

      if (isset($recipe->tools)) {
        foreach($recipe->tools as $group) {
          foreach($group as $tool) {
            list($id, $amount) = $tool;
            $this->linkIndexes($repo, "toolFor", $id, $recipe);
          }
        }
      }

      if (isset($recipe->components)) {
        foreach($recipe->components as $group) {
          foreach($group as $component) {
            list($id, $amount) = $component;
            $this->linkIndexes($repo, "toolFor", $id, $recipe);

            if($recipe->category=="CC_NONCRAFT" 
              or (isset($recipe->reversible)
              and $recipe->reversible=="true"))
            {           
              $repo->addIndex("item.disassembledFrom.$id", $recipe->repo_id, $recipe->repo_id);
            }
          }
        }
      }
    }
  }

  public function model()
  {
    return \App::make("Recipe");
  }
}

{'fraction_non_alphanumeric': 0.17253864364294666, 'fraction_numerical': 0.0002785127419579446, 'mean_word_length': 2.4413033061811213, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6808107', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2289, 'n_words': 428}