<commit_before><?php
/**
 * @copyright	Copyright (C) 2007 - 2014 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		Mozilla Public License, version 2.0
 * @link		http://github.com/joomlatools/joomla-console for the canonical source repository
 */

namespace Joomlatools\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Joomlatools\Console\Joomla\Bootstrapper;

class ExtensionUninstall extends SiteAbstract
{
    protected $extensions = array();

    protected $type = '';

    protected function configure()
    {
        parent::configure();

        $this
            ->setName('extension:uninstall')
            ->setDescription('Uninstall un-protected extensions from a site')
            ->addArgument(
                'extensions',
                InputArgument::REQUIRED | InputArgument::IS_ARRAY,
                'A array of extensions to un-install from the site... n.b. only unprotected extensions can be uninstalled'
            )->addOption(
                'type',
                't',
                InputOption::VALUE_OPTIONAL,
                'Specify the type of extension to remove'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        parent::execute($input, $output);

        $this->extensions = $input->getArgument('extensions');
        $this->type = $input->getOption('type');

        $this->check($input, $output);
        $this->install($input, $output);
    }

    public function check(InputInterface $input, OutputInterface $output)
    {
        if (!file_exists($this->target_dir)) {
            throw new \RuntimeException(sprintf('Site not found: %s', $this->site));
        }
    }

    public function install(InputInterface $input, OutputInterface $output)
    {
        $app = Bootstrapper::getApplication($this->target_dir);

        ob_start();

        $installer = $app->getInstaller();

        foreach($this->extensions as $uninstall)
        {
            $dbo = \JFactory::getDbo();
            $query = \JFactory::getDbo()->getQuery(true)
                ->select('*')
                ->from('#__extensions')
                ->where('protected = 0');

            if(strlen($this->type)){
                $query->where($dbo->quoteName('type') . " = " . $dbo->quote($this->type), 'OR');
            }

            $query->where("(" .$dbo->quoteName('name') ." = " . $dbo->quote($uninstall) . ")");

            $dbo->setQuery($query);
            $extension = $dbo->loadObject();

            if(!count($extension))
            {
                throw new \RuntimeException(sprintf('Extension Uninstall: %s extension not found / Or you are trying to uninstall a core extension.',  $uninstall));
                return;
            }

            $result = $installer->uninstall($extension->type, $extension->extension_id);

            if($result){
                $output->writeln('<info>' . $extension->name . ' extension deleted </info>');
            }else
                throw new \RuntimeException(sprintf('Extension Uninstall: Problem deleting %s extension', $uninstall));
        }

        ob_end_clean();
    }
}<commit_msg>re #99 - if we are limiting the db search by name = '' then we don't need the type<commit_after><?php
/**
 * @copyright	Copyright (C) 2007 - 2014 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		Mozilla Public License, version 2.0
 * @link		http://github.com/joomlatools/joomla-console for the canonical source repository
 */

namespace Joomlatools\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Joomlatools\Console\Joomla\Bootstrapper;

class ExtensionUninstall extends SiteAbstract
{
    protected $extensions = array();

    protected function configure()
    {
        parent::configure();

        $this
            ->setName('extension:uninstall')
            ->setDescription('Uninstall un-protected extensions from a site')
            ->addArgument(
                'extensions',
                InputArgument::REQUIRED | InputArgument::IS_ARRAY,
                'A array of extensions to un-install from the site... n.b. only unprotected extensions can be uninstalled'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        parent::execute($input, $output);

        $this->extensions = $input->getArgument('extensions');

        $this->check($input, $output);
        $this->unInstall($input, $output);
    }

    public function check(InputInterface $input, OutputInterface $output)
    {
        if (!file_exists($this->target_dir)) {
            throw new \RuntimeException(sprintf('Site not found: %s', $this->site));
        }
    }

    public function unInstall(InputInterface $input, OutputInterface $output)
    {
        $app = Bootstrapper::getApplication($this->target_dir);

        ob_start();

        $installer = $app->getInstaller();

        foreach($this->extensions as $uninstall)
        {
            $dbo = \JFactory::getDbo();
            $query = \JFactory::getDbo()->getQuery(true)
                ->select('*')
                ->from('#__extensions')
                ->where($dbo->quoteName('protected') .' = ' . $dbo->quote(0));

            $query->where($dbo->quoteName('name') ." = " . $dbo->quote($uninstall));

            $dbo->setQuery($query);
            $extension = $dbo->loadObject();

            if(!count($extension))
            {
                throw new \RuntimeException(sprintf('Extension Uninstall: %s extension not found / Or you are trying to uninstall a core extension.',  $uninstall));
                return;
            }

            $result = $installer->uninstall($extension->type, $extension->extension_id);

            if($result){
                $output->writeln('<info>' . $extension->name . ' extension deleted </info>');
            }else
                throw new \RuntimeException(sprintf('Extension Uninstall: Problem deleting %s extension', $uninstall));
        }

        ob_end_clean();
    }
}
{'fraction_non_alphanumeric': 0.13720565149136577, 'fraction_numerical': 0.0037676609105180532, 'mean_word_length': 2.3762586115527293, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '412239', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1684, 'n_words': 378}