示例#1
0
文件: tupac.inc.php 项目: Kett/tupac
function checkCache()
{
    // Check if we hgave cache
    if (!file_exists($GLOBALS['tupac_directory'] . '/tupac_data')) {
        // If any of the main files is missing, regenerate cache
        Std::rm($GLOBALS['tupac_directory'] . '/tupac_data');
        Std::rm($GLOBALS['tupac_directory'] . '/tupac_filelist');
        sendMessage('generatingCache', array());
        // Gather information
        $GLOBALS['data'] = new PacmanData();
        $GLOBALS['data']->updateList();
    } else {
        // Read Cache
        $GLOBALS['data'] = unserialize(getFileContents($GLOBALS['tupac_directory'] . '/tupac_data'));
        if ($GLOBALS['data'] === false) {
            // Unserialized failed
            sendMessage('generatingCacheCorrupted', array());
            Std::rm($GLOBALS['tupac_directory'] . '/tupac_data');
            Std::rm($GLOBALS['tupac_directory'] . '/tupac_filelist');
            $GLOBALS['data'] = new PacmanData();
            $GLOBALS['data']->updateList();
        } else {
            switch ($GLOBALS['data']->verifyIntegrity()) {
                case 'cache_updated':
                    sendMessage('generatingCacheTupacUpdated', array());
                    Std::rm($GLOBALS['tupac_directory'] . '/tupac_data');
                    Std::rm($GLOBALS['tupac_directory'] . '/tupac_filelist');
                    $GLOBALS['data'] = new PacmanData();
                    $GLOBALS['data']->updateList();
                    break;
                case 'database_outdated':
                    //sendMessage('generatingCacheDatabaseUpdated',array());
                    $anyChanges = false;
                    // Get currently available repos
                    $available_repos = array();
                    $dh = getRepoDirList();
                    foreach ($dh as $repo) {
                        if (!substr($repo, 0, 1) == '.' && is_dir(getRepoDir($repo))) {
                            $available_repos[$repo] = false;
                        }
                    }
                    // The database needs to be updated
                    $mustRecheckLocal = false;
                    $repo_list = $GLOBALS['data']->repo_list;
                    foreach ($repo_list as $repo => $repoData) {
                        unset($available_repos[$repo]);
                        if ($repo == 'local' && $GLOBALS['data']->verification[$repo] != PacmanData::verifyRepo($repo)) {
                            // We check local separately, since it is not a repo.
                            sendMessage('repoIsUpdated', array('%r' => $repo));
                            $anyChanges = true;
                            $installed = true;
                            $available_packages = array();
                            // get current packages
                            $dh = opendir(getRepoDir($repo));
                            while (FALSE !== ($package = readdir($dh))) {
                                if (substr($package, 0, 1) != '.' && is_dir(getRepoDir($repo) . $package)) {
                                    $packageName = preg_replace('/\\-[0-9a-z\\._A-Z]*\\-[0-9a-z\\._A-Z]*$/', '', $package);
                                    $available_packages[$packageName] = $package;
                                }
                            }
                            closedir($dh);
                            $installed_packages = $GLOBALS['data']->installed_packages;
                            // Walk packages of the current repo
                            foreach ($installed_packages as $packageName => $packageData) {
                                if (!$available_packages[$packageName]) {
                                    // The package is not installed anymore
                                    sendMessage('removePackage', array('%p' => $packageName));
                                    unset($repo_list['local'][$packageName]);
                                    foreach ($repo_list as $tmpRepo => $tmpRepoData) {
                                        if ($tmpRepoData[$packageName]) {
                                            $repo_list[$tmpRepo][$packageName]['installed'] = 'false';
                                        }
                                    }
                                    unset($installed_packages[$packageName]);
                                } elseif ($packageData['dir'] != $available_packages[$packageName]) {
                                    // Package updated. Since it is the list of installed packages, set its version to installed version
                                    sendMessage('updatePackage', array('%p' => $packageName));
                                    $installed_packages[$packageName] = PacmanData::getPackageData($repo, $available_packages[$packageName], 'true');
                                    $installed_info = $installed_packages[$packageName]['version'];
                                    $installed_packages[$packageName]['installed'] = $installed_info;
                                    foreach ($repo_list as $tmpRepo => $tmpRepoData) {
                                        if ($tmpRepoData[$packageName]) {
                                            $repo_list[$tmpRepo][$packageName]['installed'] = $installed_info;
                                            // in local installed version equals to avaiable version
                                            if ($repo == 'local') {
                                                $repo_list[$tmpRepo][$packageName]['version'] = $installed_info;
                                            }
                                        }
                                    }
                                }
                                unset($available_packages[$packageName]);
                            }
                            // Add new packages. This is, packages that are listed in the repo directory but that aren't marked as installed.
                            foreach ($available_packages as $packageName => $package) {
                                if ($package) {
                                    $packageFound = false;
                                    sendMessage('addPackage', array('%p' => $packageName));
                                    $installed_packages[$packageName] = PacmanData::getPackageData($repo, $available_packages[$packageName], 'true');
                                    $installed_info = $installed_packages[$packageName]['version'];
                                    $installed_packages[$packageName]['installed'] = $installed_info;
                                    foreach ($repo_list as $tmpRepo => $tmpRepoData) {
                                        if ($tmpRepo != 'local' && $tmpRepoData[$packageName]) {
                                            $repo_list[$tmpRepo][$packageName]['installed'] = $installed_info;
                                            $packageFound = true;
                                        }
                                    }
                                    // It wasn't in any repo. Let's add it to the pseudorepo local
                                    if (!$packageFound) {
                                        $repo_list['local'][$packageName] = $installed_packages[$packageName];
                                    }
                                }
                            }
                            $GLOBALS['data']->installed_packages = $installed_packages;
                            $GLOBALS['data']->verification[$repo] = PacmanData::verifyRepo($repo);
                            continue;
                        }
                        if (!is_dir(getRepoDir($repo))) {
                            sendMessage('repoIsGone', array('%r' => $repo));
                            unset($repo_list[$repo]);
                            foreach ($repoData as $packageName => $packageData) {
                                if ($GLOBALS['data']->installed_packages[$packageName]) {
                                    $packageFound = false;
                                    foreach ($repo_list as $tmpRepo => $tmpRepoData) {
                                        if ($tmpRepo != 'local' && $tmpRepoData[$packageName]) {
                                            $packageFound = true;
                                            break;
                                        }
                                    }
                                    if (!$packageFound) {
                                        $repo_list['local'][$packageName] = $packageData;
                                    }
                                }
                            }
                            $anyChanges = true;
                        } else {
                            if ($GLOBALS['data']->verification[$repo] != PacmanData::verifyRepo($repo)) {
                                sendMessage('repoIsUpdated', array('%r' => $repo));
                                $anyChanges = true;
                                $available_packages = array();
                                // get current packages
                                $dh = opendir(getRepoDir($repo));
                                while (FALSE !== ($package = readdir($dh))) {
                                    if (substr($package, 0, 1) != '.' && is_dir(getRepoDir($repo) . $package)) {
                                        $packageName = preg_replace('/\\-[0-9a-z\\._A-Z]*\\-[0-9a-z\\._A-Z]*$/', '', $package);
                                        $available_packages[$packageName] = $package;
                                    }
                                }
                                closedir($dh);
                                // Walk packages of the current repo
                                foreach ($repoData as $packageName => $packageData) {
                                    if (!$available_packages[$packageName]) {
                                        // The package doesn't exist anymore in the directory of the repo
                                        sendMessage('removePackage', array('%p' => $packageName));
                                        if ($packageData['installed'] != 'false') {
                                            // since it is installed, we have to move it to the pseudorepo local
                                            $repo_list['local'][$packageName] = $repo_list[$repo][$packageName];
                                            $packageFound = false;
                                            foreach ($repo_list as $tmpRepo => $tmpRepoData) {
                                                if ($tmpRepo != 'local' && $tmpRepoData[$packageName]) {
                                                    $packageFound = true;
                                                }
                                            }
                                            if (!$packageFound) {
                                                $repo_list['local'][$packageName] = $packageData;
                                            }
                                        }
                                        unset($repo_list[$repo][$packageName]);
                                    } elseif ($packageData['dir'] != $available_packages[$packageName]) {
                                        // Package updated
                                        sendMessage('updatePackage', array('%p' => $packageName));
                                        $installed_info = $packageData['installed'];
                                        $repo_list[$repo][$packageName] = PacmanData::getPackageData($repo, $available_packages[$packageName], $installed_info);
                                    }
                                    unset($available_packages[$packageName]);
                                }
                                // Add new packages. This is, packages that are listed in the repo directory but that aren't available in the repo.
                                foreach ($available_packages as $packageName => $package) {
                                    if ($package) {
                                        sendMessage('addPackage', array('%p' => $packageName));
                                        $repo_list[$repo][$packageName] = PacmanData::getPackageData($repo, $package, $installed);
                                        // Add install information
                                        if ($repo_list['local'][$packageName]) {
                                            $GLOBALS['data']->installed_packages[$packageName] = $repo_list['local'][$packageName];
                                        }
                                        if ($GLOBALS['data']->installed_packages[$packageName]) {
                                            unset($repo_list['local'][$packageName]);
                                            $repo_list[$repo][$packageName]['installed'] = $GLOBALS['data']->installed_packages[$packageName]['version'];
                                            // remove it from the pseudo repo local
                                            if ($repo_list['local'][$packageName]) {
                                                unset($repo_list['local'][$packageName]);
                                            }
                                        } else {
                                            $repo_list[$repo][$packageName]['installed'] = 'false';
                                        }
                                    }
                                }
                                $GLOBALS['data']->verification[$repo] = PacmanData::verifyRepo($repo);
                            }
                        }
                    }
                    $GLOBALS['data']->repo_list = $repo_list;
                    foreach ($available_repos as $repo => $dummy) {
                        sendMessage('repoIsNew', array('%r' => $repo));
                        $GLOBALS['data']->getRepoData($repo);
                        $mustRecheckLocal = true;
                    }
                    if ($mustRecheckLocal) {
                        $anyChanges = true;
                        sendMessage('recheckingLocal', array());
                        // we copy installed packages to pseudorepo local
                        $GLOBALS['data']->repo_list['local'] = array_merge($GLOBALS['data']->installed_packages, $GLOBALS['data']->repo_list['local']);
                        $GLOBALS['data']->recheckLocal();
                    }
                    if ($anyChanges) {
                        sendMessage('saving', array());
                        $GLOBALS['data']->saveData();
                    }
                    break;
                default:
                    // We never get here
                    //sendMessage('reusingCache',array());
                    break;
            }
        }
    }
}