Source for file joomla.php

Documentation is available at joomla.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Extension.Joomla
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Joomla! master extension plugin.
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Extension.Joomla
  17.  * @since       1.6
  18.  */
  19. class PlgExtensionJoomla extends JPlugin
  20. {
  21.     /**
  22.      * @var    integer Extension Identifier
  23.      * @since  1.6
  24.      */
  25.     private $eid 0;
  26.  
  27.     /**
  28.      * @var    JInstaller Installer object
  29.      * @since  1.6
  30.      */
  31.     private $installer null;
  32.  
  33.     /**
  34.      * Load the language file on instantiation.
  35.      *
  36.      * @var    boolean 
  37.      * @since  3.1
  38.      */
  39.     protected $autoloadLanguage = true;
  40.  
  41.     /**
  42.      * Adds an update site to the table if it doesn't exist.
  43.      *
  44.      * @param   string   $name      The friendly name of the site
  45.      * @param   string   $type      The type of site (e.g. collection or extension)
  46.      * @param   string   $location  The URI for the site
  47.      * @param   boolean  $enabled   If this site is enabled
  48.      *
  49.      * @return  void 
  50.      *
  51.      * @since   1.6
  52.      */
  53.     private function addUpdateSite($name$type$location$enabled)
  54.     {
  55.         $db JFactory::getDbo();
  56.  
  57.         // 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
  58.         $query $db->getQuery(true)
  59.             ->select('update_site_id')
  60.             ->from('#__update_sites')
  61.             ->where('location = ' $db->quote($location));
  62.         $db->setQuery($query);
  63.         $update_site_id = (int) $db->loadResult();
  64.  
  65.         // If it doesn't exist, add it!
  66.         if (!$update_site_id)
  67.         {
  68.             $query->clear()
  69.                 ->insert('#__update_sites')
  70.                 ->columns(array($db->quoteName('name')$db->quoteName('type')$db->quoteName('location')$db->quoteName('enabled')))
  71.                 ->values($db->quote($name', ' $db->quote($type', ' $db->quote($location', ' . (int) $enabled);
  72.             $db->setQuery($query);
  73.             if ($db->execute())
  74.             {
  75.                 // Link up this extension to the update site
  76.                 $update_site_id $db->insertid();
  77.             }
  78.         }
  79.  
  80.         // Check if it has an update site id (creation might have faileD)
  81.         if ($update_site_id)
  82.         {
  83.             // Look for an update site entry that exists
  84.             $query->clear()
  85.                 ->select('update_site_id')
  86.                 ->from('#__update_sites_extensions')
  87.                 ->where('update_site_id = ' $update_site_id)
  88.                 ->where('extension_id = ' $this->eid);
  89.             $db->setQuery($query);
  90.             $tmpid = (int) $db->loadResult();
  91.             if (!$tmpid)
  92.             {
  93.                 // Link this extension to the relevant update site
  94.                 $query->clear()
  95.                     ->insert('#__update_sites_extensions')
  96.                     ->columns(array($db->quoteName('update_site_id')$db->quoteName('extension_id')))
  97.                     ->values($update_site_id ', ' $this->eid);
  98.                 $db->setQuery($query);
  99.                 $db->execute();
  100.             }
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Handle post extension install update sites
  106.      *
  107.      * @param   JInstaller  $installer  Installer object
  108.      * @param   integer     $eid        Extension Identifier
  109.      *
  110.      * @return  void 
  111.      *
  112.      * @since   1.6
  113.      */
  114.     public function onExtensionAfterInstall($installer$eid )
  115.     {
  116.         if ($eid)
  117.         {
  118.             $this->installer $installer;
  119.             $this->eid $eid;
  120.  
  121.             // After an install we only need to do update sites
  122.             $this->processUpdateSites();
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Handle extension uninstall
  128.      *
  129.      * @param   JInstaller  $installer  Installer instance
  130.      * @param   integer     $eid        Extension id
  131.      * @param   integer     $result     Installation result
  132.      *
  133.      * @return  void 
  134.      *
  135.      * @since   1.6
  136.      */
  137.     public function onExtensionAfterUninstall($installer$eid$result)
  138.     {
  139.         if ($eid)
  140.         {
  141.             // Wipe out any update_sites_extensions links
  142.             $db JFactory::getDbo();
  143.             $query $db->getQuery(true)
  144.                 ->delete('#__update_sites_extensions')
  145.                 ->where('extension_id = ' $eid);
  146.             $db->setQuery($query);
  147.             $db->execute();
  148.  
  149.             // Delete any unused update sites
  150.             $query->clear()
  151.                 ->select('update_site_id')
  152.                 ->from('#__update_sites_extensions');
  153.             $db->setQuery($query);
  154.             $results $db->loadColumn();
  155.  
  156.             if (is_array($results))
  157.             {
  158.                 // So we need to delete the update sites and their associated updates
  159.                 $updatesite_delete $db->getQuery(true);
  160.                 $updatesite_delete->delete('#__update_sites');
  161.                 $updatesite_query $db->getQuery(true);
  162.                 $updatesite_query->select('update_site_id')
  163.                     ->from('#__update_sites');
  164.  
  165.                 // If we get results back then we can exclude them
  166.                 if (count($results))
  167.                 {
  168.                     $updatesite_query->where('update_site_id NOT IN (' implode(','$results')');
  169.                     $updatesite_delete->where('update_site_id NOT IN (' implode(','$results')');
  170.                 }
  171.  
  172.                 // So let's find what update sites we're about to nuke and remove their associated extensions
  173.                 $db->setQuery($updatesite_query);
  174.                 $update_sites_pending_delete $db->loadColumn();
  175.  
  176.                 if (is_array($update_sites_pending_delete&& count($update_sites_pending_delete))
  177.                 {
  178.                     // Nuke any pending updates with this site before we delete it
  179.                     // TODO: investigate alternative of using a query after the delete below with a query and not in like above
  180.                     $query->clear()
  181.                         ->delete('#__updates')
  182.                         ->where('update_site_id IN (' implode(','$update_sites_pending_delete')');
  183.                     $db->setQuery($query);
  184.                     $db->execute();
  185.                 }
  186.  
  187.                 // Note: this might wipe out the entire table if there are no extensions linked
  188.                 $db->setQuery($updatesite_delete);
  189.                 $db->execute();
  190.  
  191.             }
  192.  
  193.             // Last but not least we wipe out any pending updates for the extension
  194.             $query->clear()
  195.                 ->delete('#__updates')
  196.                 ->where('extension_id = '$eid);
  197.             $db->setQuery($query);
  198.             $db->execute();
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * After update of an extension
  204.      *
  205.      * @param   JInstaller  $installer  Installer object
  206.      * @param   integer     $eid        Extension identifier
  207.      *
  208.      * @return  void 
  209.      *
  210.      * @since   1.6
  211.      */
  212.     public function onExtensionAfterUpdate($installer$eid)
  213.     {
  214.         if ($eid)
  215.         {
  216.             $this->installer $installer;
  217.             $this->eid $eid;
  218.  
  219.             // handle any update sites
  220.             $this->processUpdateSites();
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Processes the list of update sites for an extension.
  226.      *
  227.      * @return  void 
  228.      *
  229.      * @since   1.6
  230.      */
  231.     private function processUpdateSites()
  232.     {
  233.         $manifest        $this->installer->getManifest();
  234.         $updateservers    $manifest->updateservers;
  235.  
  236.         if ($updateservers)
  237.         {
  238.             $children $updateservers->children();
  239.         }
  240.         else
  241.         {
  242.             $children array();
  243.         }
  244.  
  245.         if (count($children))
  246.         {
  247.             foreach ($children as $child)
  248.             {
  249.                 $attrs $child->attributes();
  250.                 $this->addUpdateSite($attrs['name']$attrs['type']trim($child)true);
  251.             }
  252.         }
  253.         else
  254.         {
  255.             $data trim((string) $updateservers);
  256.  
  257.             if (strlen($data))
  258.             {
  259.                 // We have a single entry in the update server line, let us presume this is an extension line
  260.                 $this->addUpdateSite(JText::_('PLG_EXTENSION_JOOMLA_UNKNOWN_SITE')'extension'$datatrue);
  261.             }
  262.         }
  263.     }
  264. }

Documentation generated on Tue, 19 Nov 2013 15:06:05 +0100 by phpDocumentor 1.4.3