first commit
This commit is contained in:
21
plugins/extension/finder/finder.xml
Normal file
21
plugins/extension/finder/finder.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="extension" method="upgrade">
|
||||
<name>plg_extension_finder</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2018-06</creationDate>
|
||||
<copyright>(C) 2019 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>4.0.0</version>
|
||||
<description>PLG_EXTENSION_FINDER_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\Extension\Finder</namespace>
|
||||
<files>
|
||||
<folder plugin="finder">services</folder>
|
||||
<folder>src</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_extension_finder.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_extension_finder.sys.ini</language>
|
||||
</languages>
|
||||
</extension>
|
||||
46
plugins/extension/finder/services/provider.php
Normal file
46
plugins/extension/finder/services/provider.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Extension.finder
|
||||
*
|
||||
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\Database\DatabaseInterface;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Plugin\Extension\Finder\Extension\Finder;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new Finder(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('extension', 'finder')
|
||||
);
|
||||
$plugin->setDatabase($container->get(DatabaseInterface::class));
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
190
plugins/extension/finder/src/Extension/Finder.php
Normal file
190
plugins/extension/finder/src/Extension/Finder.php
Normal file
@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Extension.Finder
|
||||
*
|
||||
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\Extension\Finder\Extension;
|
||||
|
||||
use Joomla\CMS\Installer\Installer;
|
||||
use Joomla\CMS\Plugin\CMSPlugin;
|
||||
use Joomla\Component\Finder\Administrator\Indexer\Helper;
|
||||
use Joomla\Database\DatabaseAwareTrait;
|
||||
use Joomla\Database\ParameterType;
|
||||
use Joomla\String\StringHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Finder extension plugin
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
final class Finder extends CMSPlugin
|
||||
{
|
||||
use DatabaseAwareTrait;
|
||||
|
||||
/**
|
||||
* Add common words to finder after language got installed
|
||||
*
|
||||
* @param Installer $installer Installer object
|
||||
* @param integer $eid Extension Identifier
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function onExtensionAfterInstall($installer, $eid)
|
||||
{
|
||||
if (!$eid) {
|
||||
return;
|
||||
}
|
||||
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true)
|
||||
->select($db->quoteName(['element', 'client_id']))
|
||||
->from($db->quoteName('#__extensions'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('extension_id') . ' = :eid',
|
||||
$db->quoteName('type') . ' = ' . $db->quote('language'),
|
||||
]
|
||||
)
|
||||
->bind(':eid', $eid, ParameterType::INTEGER);
|
||||
|
||||
$extension = $db->setQuery($query)->loadObject();
|
||||
|
||||
if ($extension) {
|
||||
$this->addCommonWords($extension);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common words to finder after language got updated
|
||||
*
|
||||
* @param Installer $installer Installer object
|
||||
* @param integer $eid Extension identifier
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function onExtensionAfterUpdate($installer, $eid)
|
||||
{
|
||||
$this->onExtensionAfterInstall($installer, $eid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove common words to finder after language got uninstalled
|
||||
*
|
||||
* @param Installer $installer Installer instance
|
||||
* @param integer $eid Extension id
|
||||
* @param boolean $removed Installation result
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function onExtensionAfterUninstall($installer, $eid, $removed)
|
||||
{
|
||||
// Check that the language was successfully uninstalled.
|
||||
if ($eid && $removed && $installer->extension->type === 'language') {
|
||||
$this->removeCommonWords($installer->extension);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add common words from a txt file to com_finder
|
||||
*
|
||||
* @param object $extension Extension object
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function addCommonWords($extension)
|
||||
{
|
||||
if ($extension->client_id == 0) {
|
||||
$path = JPATH_SITE . '/language/' . $extension->element . '/com_finder.commonwords.txt';
|
||||
} else {
|
||||
$path = JPATH_ADMINISTRATOR . '/language/' . $extension->element . '/com_finder.commonwords.txt';
|
||||
}
|
||||
|
||||
if (!file_exists($path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->removeCommonWords($extension);
|
||||
|
||||
$file_content = file_get_contents($path);
|
||||
$words = explode("\n", $file_content);
|
||||
$words = array_map(
|
||||
function ($word) {
|
||||
// Remove comments
|
||||
if (StringHelper::strpos($word, ';') !== false) {
|
||||
$word = StringHelper::substr($word, 0, StringHelper::strpos($word, ';'));
|
||||
}
|
||||
|
||||
return $word;
|
||||
},
|
||||
$words
|
||||
);
|
||||
|
||||
$words = array_filter(array_map('trim', $words));
|
||||
$words = array_unique($words);
|
||||
$db = $this->getDatabase();
|
||||
$query = $db->getQuery(true);
|
||||
|
||||
$lang = Helper::getPrimaryLanguage($extension->element);
|
||||
|
||||
$query->insert($db->quoteName('#__finder_terms_common'))
|
||||
->columns($db->quoteName(['term', 'language', 'custom']));
|
||||
|
||||
foreach ($words as $word) {
|
||||
$bindNames = $query->bindArray([$word, $lang], ParameterType::STRING);
|
||||
|
||||
$query->values(implode(',', $bindNames) . ', 0');
|
||||
}
|
||||
|
||||
try {
|
||||
$db->setQuery($query);
|
||||
$db->execute();
|
||||
} catch (\Exception $ex) {
|
||||
// It would be nice if the common word is stored to the DB, but it isn't super important
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove common words of a language from com_finder
|
||||
*
|
||||
* @param object $extension Extension object
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function removeCommonWords($extension)
|
||||
{
|
||||
$db = $this->getDatabase();
|
||||
$lang = Helper::getPrimaryLanguage($extension->element);
|
||||
|
||||
$query = $db->getQuery(true);
|
||||
$query->delete($db->quoteName('#__finder_terms_common'))
|
||||
->where(
|
||||
[
|
||||
$db->quoteName('language') . ' = :lang',
|
||||
$db->quoteName('custom') . ' = 0',
|
||||
]
|
||||
)
|
||||
->bind(':lang', $lang);
|
||||
|
||||
$db->setQuery($query);
|
||||
$db->execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user