<commit_before><?php
/*"******************************************************************************************************
*   (c) 2007-2012 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$                                  *
********************************************************************************************************/

/**
 * Central class to access the package-management subsystem.
 *
 * @author <EMAIL>
 * @since 4.0
 * @package module_packagemanager
 */
class class_module_packagemanager_manager {

    const STR_TYPE_MODULE = "MODULE";
    const STR_TYPE_ELEMENT = "ELEMENT";
    const STR_TYPE_TEMPLATE = "TEMPLATE";


    public static $arrLatestVersion = null;

    /**
     * Queries the local filesystem in order to find all packages available.
     * This may include packages of all providers.
     *
     * @return class_module_packagemanager_metadata[]
     */
    public function getAvailablePackages() {
        $arrReturn = array();

        $objModuleProvider = new class_module_packagemanager_packagemanager_module();
        $arrReturn = array_merge($arrReturn, $objModuleProvider->getInstalledPackages());

        usort($arrReturn, function(class_module_packagemanager_metadata $objA, class_module_packagemanager_metadata $objB) {
            return strcmp($objA->getStrTitle(), $objB->getStrTitle());
        });

        $objPackageProvider = new class_module_packagemanager_packagemanager_template();
        $arrReturn = array_merge($objPackageProvider->getInstalledPackages(), $arrReturn);

        return $arrReturn;
    }

    /**
     * Searches the current local packages for a single, given package.
     * If not found, null is returned.
     *
     * @param $strName
     *
     * @return class_module_packagemanager_metadata|null
     */
    public function getPackage($strName) {
        $arrAvailable = $this->getAvailablePackages();
        foreach($arrAvailable as $objOnePackage) {
            if($objOnePackage->getStrTitle() == $strName)
                return $objOnePackage;
        }

        return null;
    }

    /**
     * Loads the matching packagemanager for a given path.
     *
     * @param $strPath
     *
     * @return interface_packagemanager_packagemanager|null<commit_msg>NEW: the list of packages is orderd by : type (template before element/module), installation status (locally available updates before other packages)<commit_after><?php
/*"******************************************************************************************************
*   (c) 2007-2012 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$                                  *
********************************************************************************************************/

/**
 * Central class to access the package-management subsystem.
 *
 * @author <EMAIL>
 * @since 4.0
 * @package module_packagemanager
 */
class class_module_packagemanager_manager {

    const STR_TYPE_MODULE = "MODULE";
    const STR_TYPE_ELEMENT = "ELEMENT";
    const STR_TYPE_TEMPLATE = "TEMPLATE";


    public static $arrLatestVersion = null;

    /**
     * Queries the local filesystem in order to find all packages available.
     * This may include packages of all providers.
     *
     * @return class_module_packagemanager_metadata[]
     */
    public function getAvailablePackages() {
        $arrReturn = array();

        $objModuleProvider = new class_module_packagemanager_packagemanager_module();
        $arrReturn = array_merge($arrReturn, $objModuleProvider->getInstalledPackages());
        $objManager = new class_module_packagemanager_manager();

        $objPackageProvider = new class_module_packagemanager_packagemanager_template();
        $arrReturn = array_merge($objPackageProvider->getInstalledPackages(), $arrReturn);


        usort($arrReturn, function(class_module_packagemanager_metadata $objA, class_module_packagemanager_metadata $objB) use ($objManager) {

            $objHandlerA = $objManager->getPackageManagerForPath($objA->getStrPath());
            $objHandlerB = $objManager->getPackageManagerForPath($objB->getStrPath());

            if($objA->getStrType() == class_module_packagemanager_manager::STR_TYPE_TEMPLATE && $objB->getStrType() != class_module_packagemanager_manager::STR_TYPE_TEMPLATE)
                return -1;
            else if($objA->getStrType() != class_module_packagemanager_manager::STR_TYPE_TEMPLATE && $objB->getStrType() == class_module_packagemanager_manager::STR_TYPE_TEMPLATE)
                return 1;

            if($objHandlerA->isInstallable() && $objHandlerB->isInstallable()) {
                return strcmp($objA->getStrTitle(), $objB->getStrTitle());
            }


            if($objHandlerA->isInstallable() && !$objHandlerB->isInstallable()) {
                return -1;
            }

            if(!$objHandlerA->isInstallable() && $objHandlerB->isInstallable()) {
                return 1;
            }

            return strcmp($objA->getStrTitle(), $objB->getStrTitle());
        });

        return $arrReturn;
    }

    /**
     * Searches the current local packages for a single, given package.
     * If not found, null is returned.
     *
     * @param $strName
     *
     * @return class_module_packagemanager_metadata|null
     */
    public function getPackage($strName) {
        $arrAvailable = $this->getAvailablePackages();
        foreach($arrAvailable as $objOnePackage) {
            if($objOnePackage->getStrTitle() == $strName)
                return $objOnePackage;
        }

        return null;
    }

    /**
     * Loads the matching packagemanager for a given path.
     *
     * @param $strPath
     *
     * @return interface_packagemanager_packagemanager|null
{'fraction_non_alphanumeric': 0.19230769230769232, 'fraction_numerical': 0.004487179487179487, 'mean_word_length': 2.48464544946957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7250452', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1583, 'n_words': 325}