[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage Extension.Finder 6 * 7 * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 10 * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 11 */ 12 13 use Joomla\CMS\Installer\Installer; 14 use Joomla\CMS\Plugin\CMSPlugin; 15 use Joomla\Component\Finder\Administrator\Indexer\Helper; 16 use Joomla\Database\DatabaseDriver; 17 use Joomla\Database\ParameterType; 18 use Joomla\String\StringHelper; 19 20 // phpcs:disable PSR1.Files.SideEffects 21 \defined('_JEXEC') or die; 22 // phpcs:enable PSR1.Files.SideEffects 23 24 /** 25 * Finder extension plugin 26 * 27 * @since 4.0.0 28 */ 29 class PlgExtensionFinder extends CMSPlugin 30 { 31 /** 32 * Database object 33 * 34 * @var DatabaseDriver 35 * @since 4.0.0 36 */ 37 protected $db; 38 39 /** 40 * Add common words to finder after language got installed 41 * 42 * @param Installer $installer Installer object 43 * @param integer $eid Extension Identifier 44 * 45 * @return void 46 * 47 * @since 4.0.0 48 */ 49 public function onExtensionAfterInstall($installer, $eid) 50 { 51 if (!$eid) { 52 return; 53 } 54 55 $extension = $this->getLanguage($eid); 56 57 if ($extension) { 58 $this->removeCommonWords($extension); 59 $this->addCommonWords($extension); 60 } 61 } 62 63 /** 64 * Add common words to finder after language got updated 65 * 66 * @param Installer $installer Installer object 67 * @param integer $eid Extension identifier 68 * 69 * @return void 70 * 71 * @since 4.0.0 72 */ 73 public function onExtensionAfterUpdate($installer, $eid) 74 { 75 $this->onExtensionAfterInstall($installer, $eid); 76 } 77 78 /** 79 * Remove common words to finder after language got uninstalled 80 * 81 * @param Installer $installer Installer instance 82 * @param integer $eid Extension id 83 * @param boolean $removed Installation result 84 * 85 * @return void 86 * 87 * @since 4.0.0 88 */ 89 public function onExtensionAfterUninstall($installer, $eid, $removed) 90 { 91 // Check that the language was successfully uninstalled. 92 if ($eid && $removed && $installer->extension->type === 'language') { 93 $this->removeCommonWords($installer->extension); 94 } 95 } 96 97 /** 98 * Get an object of information if the handled extension is a language 99 * 100 * @param integer $eid Extension id 101 * 102 * @return object 103 * 104 * @since 4.0.0 105 */ 106 protected function getLanguage($eid) 107 { 108 $db = $this->db; 109 $eid = (int) $eid; 110 111 $query = $db->getQuery(true) 112 ->select($db->quoteName(['element', 'client_id'])) 113 ->from($db->quoteName('#__extensions')) 114 ->where( 115 [ 116 $db->quoteName('extension_id') . ' = :eid', 117 $db->quoteName('type') . ' = ' . $db->quote('language'), 118 ] 119 ) 120 ->bind(':eid', $eid, ParameterType::INTEGER); 121 122 $db->setQuery($query); 123 $extension = $db->loadObject(); 124 125 return $extension; 126 } 127 128 /** 129 * Add common words from a txt file to com_finder 130 * 131 * @param object $extension Extension object 132 * 133 * @return void 134 * 135 * @since 4.0.0 136 */ 137 protected function addCommonWords($extension) 138 { 139 if ($extension->client_id == 0) { 140 $path = JPATH_SITE . '/language/' . $extension->element . '/' . $extension->element . '.com_finder.commonwords.txt'; 141 } else { 142 $path = JPATH_ADMINISTRATOR . '/language/' . $extension->element . '/' . $extension->element . '.com_finder.commonwords.txt'; 143 } 144 145 if (!file_exists($path)) { 146 return; 147 } 148 149 $file_content = file_get_contents($path); 150 $words = explode("\n", $file_content); 151 $words = array_map( 152 function ($word) { 153 // Remove comments 154 if (StringHelper::strpos($word, ';') !== false) { 155 $word = StringHelper::substr($word, 0, StringHelper::strpos($word, ';')); 156 } 157 158 return $word; 159 }, 160 $words 161 ); 162 163 $words = array_filter(array_map('trim', $words)); 164 $db = $this->db; 165 $query = $db->getQuery(true); 166 167 require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/helper.php'; 168 169 $lang = Helper::getPrimaryLanguage($extension->element); 170 171 $query->insert($db->quoteName('#__finder_terms_common')) 172 ->columns($db->quoteName(['term', 'language', 'custom'])); 173 174 foreach ($words as $word) { 175 $bindNames = $query->bindArray([$word, $lang], ParameterType::STRING); 176 177 $query->values(implode(',', $bindNames) . ', 0'); 178 } 179 180 try { 181 $db->setQuery($query); 182 $db->execute(); 183 } catch (Exception $ex) { 184 // It would be nice if the common word is stored to the DB, but it isn't super important 185 } 186 } 187 188 /** 189 * Remove common words of a language from com_finder 190 * 191 * @param object $extension Extension object 192 * 193 * @return void 194 * 195 * @since 4.0.0 196 */ 197 protected function removeCommonWords($extension) 198 { 199 $db = $this->db; 200 201 require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/helper.php'; 202 203 $lang = Helper::getPrimaryLanguage($extension->element); 204 205 $query = $db->getQuery(true); 206 $query->delete($db->quoteName('#__finder_terms_common')) 207 ->where( 208 [ 209 $db->quoteName('language') . ' = :lang', 210 $db->quoteName('custom') . ' = 0', 211 ] 212 ) 213 ->bind(':lang', $lang); 214 215 $db->setQuery($query); 216 $db->execute(); 217 } 218 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |