[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/extension/joomla/ -> joomla.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Extension.Joomla
   6   *
   7   * @copyright   (C) 2010 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\Language\Text;
  15  use Joomla\CMS\Plugin\CMSPlugin;
  16  use Joomla\Database\DatabaseDriver;
  17  use Joomla\Database\ParameterType;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Joomla! master extension plugin.
  25   *
  26   * @since  1.6
  27   */
  28  class PlgExtensionJoomla extends CMSPlugin
  29  {
  30      /**
  31       * @var    DatabaseDriver
  32       *
  33       * @since  4.0.0
  34       */
  35      protected $db;
  36  
  37      /**
  38       * @var    integer
  39       *
  40       * @since  1.6
  41       */
  42      private $eid = 0;
  43  
  44      /**
  45       * @var    Installer
  46       *
  47       * @since  1.6
  48       */
  49      private $installer = null;
  50  
  51      /**
  52       * Load the language file on instantiation.
  53       *
  54       * @var    boolean
  55       *
  56       * @since  3.1
  57       */
  58      protected $autoloadLanguage = true;
  59  
  60      /**
  61       * Adds an update site to the table if it doesn't exist.
  62       *
  63       * @param   string   $name        The friendly name of the site
  64       * @param   string   $type        The type of site (e.g. collection or extension)
  65       * @param   string   $location    The URI for the site
  66       * @param   boolean  $enabled     If this site is enabled
  67       * @param   string   $extraQuery  Any additional request query to use when updating
  68       *
  69       * @return  void
  70       *
  71       * @since   1.6
  72       */
  73      private function addUpdateSite($name, $type, $location, $enabled, $extraQuery = '')
  74      {
  75          // Look if the location is used already; doesn't matter what type you can't have two types at the same address, doesn't make sense
  76          $db    = $this->db;
  77          $query = $db->getQuery(true);
  78  
  79          $query->select($db->quoteName('update_site_id'))
  80              ->from($db->quoteName('#__update_sites'))
  81              ->where($db->quoteName('location') . ' = :location')
  82              ->bind(':location', $location);
  83  
  84          $db->setQuery($query);
  85  
  86          $update_site_id = (int) $db->loadResult();
  87  
  88          // If it doesn't exist, add it!
  89          if (!$update_site_id) {
  90              $enabled = (int) $enabled;
  91              $query->clear()
  92                  ->insert($db->quoteName('#__update_sites'))
  93                  ->columns($db->quoteName(['name', 'type', 'location', 'enabled', 'extra_query']))
  94                  ->values(':name, :type, :location, :enabled, :extra_query')
  95                  ->bind(':name', $name)
  96                  ->bind(':type', $type)
  97                  ->bind(':location', $location)
  98                  ->bind(':enabled', $enabled, ParameterType::INTEGER)
  99                  ->bind(':extra_query', $extraQuery);
 100  
 101              $db->setQuery($query);
 102  
 103              if ($db->execute()) {
 104                  // Link up this extension to the update site
 105                  $update_site_id = $db->insertid();
 106              }
 107          }
 108  
 109          // Check if it has an update site id (creation might have failed)
 110          if ($update_site_id) {
 111              // Look for an update site entry that exists
 112              $query->clear()
 113                  ->select($db->quoteName('update_site_id'))
 114                  ->from($db->quoteName('#__update_sites_extensions'))
 115                  ->where(
 116                      [
 117                          $db->quoteName('update_site_id') . ' = :updatesiteid',
 118                          $db->quoteName('extension_id') . ' = :extensionid',
 119                      ]
 120                  )
 121                  ->bind(':updatesiteid', $update_site_id, ParameterType::INTEGER)
 122                  ->bind(':extensionid', $this->eid, ParameterType::INTEGER);
 123  
 124              $db->setQuery($query);
 125  
 126              $tmpid = (int) $db->loadResult();
 127  
 128              if (!$tmpid) {
 129                  // Link this extension to the relevant update site
 130                  $query->clear()
 131                      ->insert($db->quoteName('#__update_sites_extensions'))
 132                      ->columns($db->quoteName(['update_site_id', 'extension_id']))
 133                      ->values(':updatesiteid, :eid')
 134                      ->bind(':updatesiteid', $update_site_id, ParameterType::INTEGER)
 135                      ->bind(':eid', $this->eid, ParameterType::INTEGER);
 136  
 137                  $db->setQuery($query);
 138                  $db->execute();
 139              }
 140          }
 141      }
 142  
 143      /**
 144       * Handle post extension install update sites
 145       *
 146       * @param   Installer  $installer  Installer object
 147       * @param   integer    $eid        Extension Identifier
 148       *
 149       * @return  void
 150       *
 151       * @since   1.6
 152       */
 153      public function onExtensionAfterInstall($installer, $eid)
 154      {
 155          if ($eid) {
 156              $this->installer = $installer;
 157              $this->eid = (int) $eid;
 158  
 159              // After an install we only need to do update sites
 160              $this->processUpdateSites();
 161          }
 162      }
 163  
 164      /**
 165       * Handle extension uninstall
 166       *
 167       * @param   Installer  $installer  Installer instance
 168       * @param   integer    $eid        Extension id
 169       * @param   boolean    $removed    Installation result
 170       *
 171       * @return  void
 172       *
 173       * @since   1.6
 174       */
 175      public function onExtensionAfterUninstall($installer, $eid, $removed)
 176      {
 177          // If we have a valid extension ID and the extension was successfully uninstalled wipe out any
 178          // update sites for it
 179          if ($eid && $removed) {
 180              $db    = $this->db;
 181              $query = $db->getQuery(true);
 182              $eid   = (int) $eid;
 183  
 184              $query->delete($db->quoteName('#__update_sites_extensions'))
 185                  ->where($db->quoteName('extension_id') . ' = :eid')
 186                  ->bind(':eid', $eid, ParameterType::INTEGER);
 187  
 188              $db->setQuery($query);
 189              $db->execute();
 190  
 191              // Delete any unused update sites
 192              $query->clear()
 193                  ->select($db->quoteName('update_site_id'))
 194                  ->from($db->quoteName('#__update_sites_extensions'));
 195  
 196              $db->setQuery($query);
 197              $results = $db->loadColumn();
 198  
 199              if (is_array($results)) {
 200                  // So we need to delete the update sites and their associated updates
 201                  $updatesite_delete = $db->getQuery(true);
 202                  $updatesite_delete->delete($db->quoteName('#__update_sites'));
 203  
 204                  $updatesite_query = $db->getQuery(true);
 205                  $updatesite_query->select($db->quoteName('update_site_id'))
 206                      ->from($db->quoteName('#__update_sites'));
 207  
 208                  // If we get results back then we can exclude them
 209                  if (count($results)) {
 210                      $updatesite_query->whereNotIn($db->quoteName('update_site_id'), $results);
 211                      $updatesite_delete->whereNotIn($db->quoteName('update_site_id'), $results);
 212                  }
 213  
 214                  // So let's find what update sites we're about to nuke and remove their associated extensions
 215                  $db->setQuery($updatesite_query);
 216                  $update_sites_pending_delete = $db->loadColumn();
 217  
 218                  if (is_array($update_sites_pending_delete) && count($update_sites_pending_delete)) {
 219                      // Nuke any pending updates with this site before we delete it
 220                      // @todo: investigate alternative of using a query after the delete below with a query and not in like above
 221                      $query->clear()
 222                          ->delete($db->quoteName('#__updates'))
 223                          ->whereIn($db->quoteName('update_site_id'), $update_sites_pending_delete);
 224  
 225                      $db->setQuery($query);
 226                      $db->execute();
 227                  }
 228  
 229                  // Note: this might wipe out the entire table if there are no extensions linked
 230                  $db->setQuery($updatesite_delete);
 231                  $db->execute();
 232              }
 233  
 234              // Last but not least we wipe out any pending updates for the extension
 235              $query->clear()
 236                  ->delete($db->quoteName('#__updates'))
 237                  ->where($db->quoteName('extension_id') . ' = :eid')
 238                  ->bind(':eid', $eid, ParameterType::INTEGER);
 239  
 240              $db->setQuery($query);
 241              $db->execute();
 242          }
 243      }
 244  
 245      /**
 246       * After update of an extension
 247       *
 248       * @param   Installer  $installer  Installer object
 249       * @param   integer    $eid        Extension identifier
 250       *
 251       * @return  void
 252       *
 253       * @since   1.6
 254       */
 255      public function onExtensionAfterUpdate($installer, $eid)
 256      {
 257          if ($eid) {
 258              $this->installer = $installer;
 259              $this->eid = (int) $eid;
 260  
 261              // Handle any update sites
 262              $this->processUpdateSites();
 263          }
 264      }
 265  
 266      /**
 267       * Processes the list of update sites for an extension.
 268       *
 269       * @return  void
 270       *
 271       * @since   1.6
 272       */
 273      private function processUpdateSites()
 274      {
 275          $manifest      = $this->installer->getManifest();
 276          $updateservers = $manifest->updateservers;
 277  
 278          if ($updateservers) {
 279              $children = $updateservers->children();
 280          } else {
 281              $children = [];
 282          }
 283  
 284          if (count($children)) {
 285              foreach ($children as $child) {
 286                  $attrs = $child->attributes();
 287                  $this->addUpdateSite((string) $attrs['name'], (string) $attrs['type'], trim($child), true, $this->installer->extraQuery);
 288              }
 289          } else {
 290              $data = trim((string) $updateservers);
 291  
 292              if ($data !== '') {
 293                  // We have a single entry in the update server line, let us presume this is an extension line
 294                  $this->addUpdateSite(Text::_('PLG_EXTENSION_JOOMLA_UNKNOWN_SITE'), 'extension', $data, true);
 295              }
 296          }
 297      }
 298  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer