/** * Returns an list of spots with the mastercollection id inserted into the * collectionInfo variable of the actual spot record. It will create the * master collection record if necessary * * @param array $spotList * @param bool $isRecursive * @return array */ public function getCollectionIdList(array $spotList, $isRecursive = false) { $toFetch = array(); if (!$isRecursive) { $this->_conn->beginTransaction(); } # if /* * This is a very crude way to prevent us from running * out of memory. */ if (count(self::$mc_CacheList) > 250000 && count($spotList) < 250000) { self::$mc_CacheList = array(); self::$startedWithFullCacheLoad = false; } // if // fetch from cache where possible foreach ($spotList as &$spot) { if ($spot['collectionInfo'] !== null) { $title = $spot['collectionInfo']->getTitle(); $catType = $spot['collectionInfo']->getCatType(); $year = $spot['collectionInfo']->getYear(); if ($this->isInLocalCache($title, $catType, $year)) { $spot['collectionInfo']->setMcId($this->getMcIdFromLocalCache($title, $catType, $year)); /* * Try to find this specific collection id. If it isn't matched, * we know for sure the collection is not in the database, because * we always retrieve the list of collections when retrieving the * master collection. */ $spot['collectionInfo'] = $this->matchCreateSpecificCollection($spot['collectionInfo'], $spot['stamp'], $spot['id']); } else { $toFetch[$spot['collectionInfo']->getTitle()] = array('cattype' => $spot['collectionInfo']->getCatType(), 'year' => $spot['collectionInfo']->getYear()); } // else } // if } // foreach unset($spot); /* * Update the local collection information with the latest stamp */ $this->updateMcCollectionStamp(); // get remaining titles from database if (!empty($toFetch)) { $this->loadCollectionCache($toFetch); /* * Loop through all titles once more, and if we still do not have * a cache record for these, create the mastercollection record. */ foreach ($toFetch as $key => $val) { /* * Make sure the master title record is in the db, if we didn't get it * the first time, create it now. */ if (!$this->isInLocalCache($key, $val['cattype'], $val['year'])) { # echo 'Creating mastercollections: (' . $key . '),(' . $val['cattype'] . '),('. $val['year'] . ')' . PHP_EOL; $this->_conn->exec('INSERT INTO mastercollections(title, cattype, year, tmdb_id, tvrage_id) VALUES (:title, :cattype, :year, NULL, NULL)', array(':title' => array($key, PDO::PARAM_STR), ':cattype' => array($val['cattype'], PDO::PARAM_INT), ':year' => array($val['year'], PDO::PARAM_INT))); // add the newly generated mastercollection to the local cache $this->addMcToLocalCache($this->_conn->lastInsertId('mastercollections'), $key, $val['cattype'], $val['year']); } // if } // foreach /* * We call ourselves recursively. This is an ugly solution, but * we are rather lazy to not further optimize this until necessary. * * The above code garantuees us both the specific collection and master * record are set, so the second time this is run, the toFetch list * will always be empty, so we never recurse more than once. You can * get out your pitchforks now. */ $spotList = $this->getCollectionIdList($spotList, true); } // if if (!$isRecursive) { /* * We try one more time to see if we can re-arrange the movies * with the year NULL */ foreach ($spotList as &$spot) { if ($spot['collectionInfo'] !== null) { $collInfo = $spot['collectionInfo']; /* * Is there any other year available yet? If so, use that one. */ $fixedYear = $this->fixCollectionYear($collInfo->getTitle(), $collInfo->getCatType(), $collInfo->getYear()); if ($fixedYear !== null) { $collInfo->setYear($fixedYear); $spot['collectionInfo'] = $this->matchCreateSpecificCollection($collInfo, $spot['stamp'], $spot['id']); } // if } } // foreach unset($spot); $this->_conn->commit(); } // if return $spotList; }