Source for file language.php

Documentation is available at language.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Installer
  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
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.base.adapterinstance');
  13. jimport('joomla.filesystem.folder');
  14.  
  15. /**
  16.  * Language installer
  17.  *
  18.  * @package     Joomla.Libraries
  19.  * @subpackage  Installer
  20.  * @since       3.1
  21.  */
  22. {
  23.     /**
  24.      * Core language pack flag
  25.      *
  26.      * @var    boolean 
  27.      * @since  12.1
  28.      */
  29.     protected $core = false;
  30.  
  31.     /**
  32.      * Custom install method
  33.      *
  34.      * Note: This behaves badly due to hacks made in the middle of 1.5.x to add
  35.      * the ability to install multiple distinct packs in one install. The
  36.      * preferred method is to use a package to install multiple language packs.
  37.      *
  38.      * @return  boolean  True on success
  39.      *
  40.      * @since   3.1
  41.      */
  42.     public function install()
  43.     {
  44.         $source $this->parent->getPath('source');
  45.  
  46.         if (!$source)
  47.         {
  48.             $this->parent
  49.                 ->setPath(
  50.                 'source',
  51.                 ($this->parent->extension->client_id JPATH_ADMINISTRATOR JPATH_SITE'/language/' $this->parent->extension->element
  52.             );
  53.         }
  54.         $this->manifest $this->parent->getManifest();
  55.  
  56.         // Get the client application target
  57.         if ($cname = (string) $this->manifest->attributes()->client)
  58.         {
  59.             // Attempt to map the client to a base path
  60.             $client JApplicationHelper::getClientInfo($cnametrue);
  61.  
  62.             if ($client === null)
  63.             {
  64.                 $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'JText::sprintf('JLIB_INSTALLER_ERROR_UNKNOWN_CLIENT_TYPE'$cname)));
  65.  
  66.                 return false;
  67.             }
  68.             $basePath $client->path;
  69.             $clientId $client->id;
  70.             $element $this->manifest->files;
  71.  
  72.             return $this->_install($cname$basePath$clientId$element);
  73.         }
  74.         else
  75.         {
  76.             // No client attribute was found so we assume the site as the client
  77.             $cname 'site';
  78.             $basePath JPATH_SITE;
  79.             $clientId 0;
  80.             $element $this->manifest->files;
  81.  
  82.             return $this->_install($cname$basePath$clientId$element);
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Install function that is designed to handle individual clients
  88.      *
  89.      * @param   string   $cname     Cname @todo: not used
  90.      * @param   string   $basePath  The base name.
  91.      * @param   integer  $clientId  The client id.
  92.      * @param   object   &$element  The XML element.
  93.      *
  94.      * @return  boolean 
  95.      *
  96.      * @since  3.1
  97.      */
  98.     protected function _install($cname$basePath$clientId&$element)
  99.     {
  100.         $this->manifest $this->parent->getManifest();
  101.  
  102.         // Get the language name
  103.         // Set the extensions name
  104.         $name JFilterInput::getInstance()->clean((string) $this->manifest->name'cmd');
  105.         $this->set('name'$name);
  106.  
  107.         // Get the Language tag [ISO tag, eg. en-GB]
  108.         $tag = (string) $this->manifest->tag;
  109.  
  110.         // Check if we found the tag - if we didn't, we may be trying to install from an older language package
  111.         if (!$tag)
  112.         {
  113.             $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'JText::_('JLIB_INSTALLER_ERROR_NO_LANGUAGE_TAG')));
  114.  
  115.             return false;
  116.         }
  117.  
  118.         $this->set('tag'$tag);
  119.  
  120.         // Set the language installation path
  121.         $this->parent->setPath('extension_site'$basePath '/language/' $tag);
  122.  
  123.         // Do we have a meta file in the file list?  In other words... is this a core language pack?
  124.         if ($element && count($element->children()))
  125.         {
  126.             $files $element->children();
  127.  
  128.             foreach ($files as $file)
  129.             {
  130.                 if ((string) $file->attributes()->file == 'meta')
  131.                 {
  132.                     $this->core = true;
  133.                     break;
  134.                 }
  135.             }
  136.         }
  137.  
  138.         // If the language directory does not exist, let's create it
  139.         $created false;
  140.  
  141.         if (!file_exists($this->parent->getPath('extension_site')))
  142.         {
  143.             if (!$created JFolder::create($this->parent->getPath('extension_site')))
  144.             {
  145.                 $this->parent
  146.                     ->abort(
  147.                     JText::sprintf(
  148.                         'JLIB_INSTALLER_ABORT',
  149.                         JText::sprintf('JLIB_INSTALLER_ERROR_CREATE_FOLDER_FAILED'$this->parent->getPath('extension_site'))
  150.                     )
  151.                 );
  152.  
  153.                 return false;
  154.             }
  155.         }
  156.         else
  157.         {
  158.             // Look for an update function or update tag
  159.             $updateElement $this->manifest->update;
  160.  
  161.             // Upgrade manually set or update tag detected
  162.             if ($this->parent->isUpgrade(|| $updateElement)
  163.             {
  164.                 // Transfer control to the update function
  165.                 return $this->update();
  166.             }
  167.             elseif (!$this->parent->isOverwrite())
  168.             {
  169.                 // Overwrite is set
  170.                 // We didn't have overwrite set, find an update function or find an update tag so lets call it safe
  171.                 if (file_exists($this->parent->getPath('extension_site')))
  172.                 {
  173.                     // If the site exists say so.
  174.                     JLog::add(
  175.                         JText::sprintf('JLIB_INSTALLER_ABORT'JText::sprintf('JLIB_INSTALLER_ERROR_FOLDER_IN_USE'$this->parent->getPath('extension_site'))),
  176.                         JLog::WARNING'jerror'
  177.                     );
  178.                 }
  179.                 else
  180.                 {
  181.                     // If the admin exists say so.
  182.                     JLog::add(
  183.                         JText::sprintf('JLIB_INSTALLER_ABORT'JText::sprintf('JLIB_INSTALLER_ERROR_FOLDER_IN_USE'$this->parent->getPath('extension_administrator'))),
  184.                         JLog::WARNING'jerror'
  185.                     );
  186.                 }
  187.                 return false;
  188.             }
  189.         }
  190.  
  191.         /*
  192.          * If we created the language directory we will want to remove it if we
  193.          * have to roll back the installation, so let's add it to the installation
  194.          * step stack
  195.          */
  196.         if ($created)
  197.         {
  198.             $this->parent->pushStep(array('type' => 'folder''path' => $this->parent->getPath('extension_site')));
  199.         }
  200.  
  201.         // Copy all the necessary files
  202.         if ($this->parent->parseFiles($element=== false)
  203.         {
  204.             // Install failed, rollback changes
  205.             $this->parent->abort();
  206.  
  207.             return false;
  208.         }
  209.  
  210.         // Parse optional tags
  211.         $this->parent->parseMedia($this->manifest->media);
  212.  
  213.         // Copy all the necessary font files to the common pdf_fonts directory
  214.         $this->parent->setPath('extension_site'$basePath '/language/pdf_fonts');
  215.         $overwrite $this->parent->setOverwrite(true);
  216.  
  217.         if ($this->parent->parseFiles($this->manifest->fonts=== false)
  218.         {
  219.             // Install failed, rollback changes
  220.             $this->parent->abort();
  221.  
  222.             return false;
  223.         }
  224.         $this->parent->setOverwrite($overwrite);
  225.  
  226.         // Get the language description
  227.         $description = (string) $this->manifest->description;
  228.  
  229.         if ($description)
  230.         {
  231.             $this->parent->set('message'JText::_($description));
  232.         }
  233.         else
  234.         {
  235.             $this->parent->set('message''');
  236.         }
  237.  
  238.         // Add an entry to the extension table with a whole heap of defaults
  239.         $row JTable::getInstance('extension');
  240.         $row->set('name'$this->get('name'));
  241.         $row->set('type''language');
  242.         $row->set('element'$this->get('tag'));
  243.  
  244.         // There is no folder for languages
  245.         $row->set('folder''');
  246.         $row->set('enabled'1);
  247.         $row->set('protected'0);
  248.         $row->set('access'0);
  249.         $row->set('client_id'$clientId);
  250.         $row->set('params'$this->parent->getParams());
  251.         $row->set('manifest_cache'$this->parent->generateManifestCache());
  252.  
  253.         if (!$row->store())
  254.         {
  255.             // Install failed, roll back changes
  256.             $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'$row->getError()));
  257.  
  258.             return false;
  259.         }
  260.  
  261.         // Clobber any possible pending updates
  262.         $update JTable::getInstance('update');
  263.         $uid $update->find(array('element' => $this->get('tag')'type' => 'language''client_id' => '''folder' => ''));
  264.  
  265.         if ($uid)
  266.         {
  267.             $update->delete($uid);
  268.         }
  269.  
  270.         return $row->get('extension_id');
  271.     }
  272.  
  273.     /**
  274.      * Custom update method
  275.      *
  276.      * @return  boolean  True on success, false on failure
  277.      *
  278.      * @since   3.1
  279.      */
  280.     public function update()
  281.     {
  282.         $xml $this->parent->getManifest();
  283.  
  284.         $this->manifest $xml;
  285.  
  286.         $cname $xml->attributes()->client;
  287.  
  288.         // Attempt to map the client to a base path
  289.         $client JApplicationHelper::getClientInfo($cnametrue);
  290.  
  291.         if ($client === null || (empty($cname&& $cname !== 0))
  292.         {
  293.             $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'JText::sprintf('JLIB_INSTALLER_ERROR_UNKNOWN_CLIENT_TYPE'$cname)));
  294.  
  295.             return false;
  296.         }
  297.         $basePath $client->path;
  298.         $clientId $client->id;
  299.  
  300.         // Get the language name
  301.         // Set the extensions name
  302.         $name = (string) $this->manifest->name;
  303.         $name JFilterInput::getInstance()->clean($name'cmd');
  304.         $this->set('name'$name);
  305.  
  306.         // Get the Language tag [ISO tag, eg. en-GB]
  307.         $tag = (string) $xml->tag;
  308.  
  309.         // Check if we found the tag - if we didn't, we may be trying to install from an older language package
  310.         if (!$tag)
  311.         {
  312.             $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'JText::_('JLIB_INSTALLER_ERROR_NO_LANGUAGE_TAG')));
  313.  
  314.             return false;
  315.         }
  316.  
  317.         $this->set('tag'$tag);
  318.  
  319.         // Set the language installation path
  320.         $this->parent->setPath('extension_site'$basePath '/language/' $tag);
  321.  
  322.         // Do we have a meta file in the file list?  In other words... is this a core language pack?
  323.         if (count($xml->files->children()))
  324.         {
  325.             foreach ($xml->files->children(as $file)
  326.             {
  327.                 if ((string) $file->attributes()->file == 'meta')
  328.                 {
  329.                     $this->core = true;
  330.                     break;
  331.                 }
  332.             }
  333.         }
  334.  
  335.         // Copy all the necessary files
  336.         if ($this->parent->parseFiles($xml->files=== false)
  337.         {
  338.             // Install failed, rollback changes
  339.             $this->parent->abort();
  340.  
  341.             return false;
  342.         }
  343.  
  344.         // Parse optional tags
  345.         $this->parent->parseMedia($xml->media);
  346.  
  347.         // Copy all the necessary font files to the common pdf_fonts directory
  348.         $this->parent->setPath('extension_site'$basePath '/language/pdf_fonts');
  349.         $overwrite $this->parent->setOverwrite(true);
  350.  
  351.         if ($this->parent->parseFiles($xml->fonts=== false)
  352.         {
  353.             // Install failed, rollback changes
  354.             $this->parent->abort();
  355.  
  356.             return false;
  357.         }
  358.         $this->parent->setOverwrite($overwrite);
  359.  
  360.         // Get the language description and set it as message
  361.         $this->parent->set('message'(string) $xml->description);
  362.  
  363.         /**
  364.          * ---------------------------------------------------------------------------------------------
  365.          * Finalization and Cleanup Section
  366.          * ---------------------------------------------------------------------------------------------
  367.          */
  368.  
  369.         // Clobber any possible pending updates
  370.         $update JTable::getInstance('update');
  371.         $uid $update->find(array('element' => $this->get('tag')'type' => 'language''client_id' => $clientId));
  372.  
  373.         if ($uid)
  374.         {
  375.             $update->delete($uid);
  376.         }
  377.  
  378.         // Update an entry to the extension table
  379.         $row JTable::getInstance('extension');
  380.         $eid $row->find(array('element' => strtolower($this->get('tag'))'type' => 'language''client_id' => $clientId));
  381.  
  382.         if ($eid)
  383.         {
  384.             $row->load($eid);
  385.         }
  386.         else
  387.         {
  388.             // Set the defaults
  389.  
  390.             // There is no folder for language
  391.             $row->set('folder''');
  392.             $row->set('enabled'1);
  393.             $row->set('protected'0);
  394.             $row->set('access'0);
  395.             $row->set('client_id'$clientId);
  396.             $row->set('params'$this->parent->getParams());
  397.         }
  398.         $row->set('name'$this->get('name'));
  399.         $row->set('type''language');
  400.         $row->set('element'$this->get('tag'));
  401.         $row->set('manifest_cache'$this->parent->generateManifestCache());
  402.  
  403.         if (!$row->store())
  404.         {
  405.             // Install failed, roll back changes
  406.             $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT'$row->getError()));
  407.  
  408.             return false;
  409.         }
  410.  
  411.         return $row->get('extension_id');
  412.     }
  413.  
  414.     /**
  415.      * Custom uninstall method
  416.      *
  417.      * @param   string  $eid  The tag of the language to uninstall
  418.      *
  419.      * @return  mixed  Return value for uninstall method in component uninstall file
  420.      *
  421.      * @since   3.1
  422.      */
  423.     public function uninstall($eid)
  424.     {
  425.         // Load up the extension details
  426.         $extension JTable::getInstance('extension');
  427.         $extension->load($eid);
  428.  
  429.         // Grab a copy of the client details
  430.         $client JApplicationHelper::getClientInfo($extension->get('client_id'));
  431.  
  432.         // Check the element isn't blank to prevent nuking the languages directory...just in case
  433.         $element $extension->get('element');
  434.  
  435.         if (empty($element))
  436.         {
  437.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_UNINSTALL_ELEMENT_EMPTY')JLog::WARNING'jerror');
  438.  
  439.             return false;
  440.         }
  441.  
  442.         // Check that the language is not protected, Normally en-GB.
  443.         $protected $extension->get('protected');
  444.  
  445.         if ($protected == 1)
  446.         {
  447.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_UNINSTALL_PROTECTED')JLog::WARNING'jerror');
  448.  
  449.             return false;
  450.         }
  451.  
  452.         // Verify that it's not the default language for that client
  453.         $params JComponentHelper::getParams('com_languages');
  454.  
  455.         if ($params->get($client->name== $element)
  456.         {
  457.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_UNINSTALL_DEFAULT')JLog::WARNING'jerror');
  458.  
  459.             return false;
  460.         }
  461.  
  462.         // Construct the path from the client, the language and the extension element name
  463.         $path $client->path '/language/' $element;
  464.  
  465.         // Get the package manifest object and remove media
  466.         $this->parent->setPath('source'$path);
  467.  
  468.         // We do findManifest to avoid problem when uninstalling a list of extension: getManifest cache its manifest file
  469.         $this->parent->findManifest();
  470.         $this->manifest $this->parent->getManifest();
  471.         $this->parent->removeFiles($this->manifest->media);
  472.  
  473.         // Check it exists
  474.         if (!JFolder::exists($path))
  475.         {
  476.             // If the folder doesn't exist lets just nuke the row as well and presume the user killed it for us
  477.             $extension->delete();
  478.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_UNINSTALL_PATH_EMPTY')JLog::WARNING'jerror');
  479.  
  480.             return false;
  481.         }
  482.  
  483.         if (!JFolder::delete($path))
  484.         {
  485.             // If deleting failed we'll leave the extension entry in tact just in case
  486.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_UNINSTALL_DIRECTORY')JLog::WARNING'jerror');
  487.  
  488.             return false;
  489.         }
  490.  
  491.         // Remove the extension table entry
  492.         $extension->delete();
  493.  
  494.         // Setting the language of users which have this language as the default language
  495.         $db JFactory::getDbo();
  496.         $query $db->getQuery(true)
  497.             ->from('#__users')
  498.             ->select('*');
  499.         $db->setQuery($query);
  500.         $users $db->loadObjectList();
  501.  
  502.         if ($client->name == 'administrator')
  503.         {
  504.             $param_name 'admin_language';
  505.         }
  506.         else
  507.         {
  508.             $param_name 'language';
  509.         }
  510.  
  511.         $count 0;
  512.  
  513.         foreach ($users as $user)
  514.         {
  515.             $registry new JRegistry;
  516.             $registry->loadString($user->params);
  517.  
  518.             if ($registry->get($param_name== $element)
  519.             {
  520.                 $registry->set($param_name'');
  521.                 $query->clear()
  522.                     ->update('#__users')
  523.                     ->set('params=' $db->quote($registry))
  524.                     ->where('id=' . (int) $user->id);
  525.                 $db->setQuery($query);
  526.                 $db->execute();
  527.                 $count++;
  528.             }
  529.         }
  530.         if (!empty($count))
  531.         {
  532.             JLog::add(JText::plural('JLIB_INSTALLER_NOTICE_LANG_RESET_USERS'$count)JLog::NOTICE'jerror');
  533.         }
  534.  
  535.         // All done!
  536.         return true;
  537.     }
  538.  
  539.     /**
  540.      * Custom discover method
  541.      * Finds language files
  542.      *
  543.      * @return  boolean  True on success
  544.      *
  545.      * @since  3.1
  546.      */
  547.     public function discover()
  548.     {
  549.         $results array();
  550.         $site_languages JFolder::folders(JPATH_SITE '/language');
  551.         $admin_languages JFolder::folders(JPATH_ADMINISTRATOR '/language');
  552.  
  553.         foreach ($site_languages as $language)
  554.         {
  555.             if (file_exists(JPATH_SITE '/language/' $language '/' $language '.xml'))
  556.             {
  557.                 $manifest_details JInstaller::parseXMLInstallFile(JPATH_SITE '/language/' $language '/' $language '.xml');
  558.                 $extension JTable::getInstance('extension');
  559.                 $extension->set('type''language');
  560.                 $extension->set('client_id'0);
  561.                 $extension->set('element'$language);
  562.                 $extension->set('folder''');
  563.                 $extension->set('name'$language);
  564.                 $extension->set('state'-1);
  565.                 $extension->set('manifest_cache'json_encode($manifest_details));
  566.                 $extension->set('params''{}');
  567.                 $results[$extension;
  568.             }
  569.         }
  570.         foreach ($admin_languages as $language)
  571.         {
  572.             if (file_exists(JPATH_ADMINISTRATOR '/language/' $language '/' $language '.xml'))
  573.             {
  574.                 $manifest_details JInstaller::parseXMLInstallFile(JPATH_ADMINISTRATOR '/language/' $language '/' $language '.xml');
  575.                 $extension JTable::getInstance('extension');
  576.                 $extension->set('type''language');
  577.                 $extension->set('client_id'1);
  578.                 $extension->set('element'$language);
  579.                 $extension->set('folder''');
  580.                 $extension->set('name'$language);
  581.                 $extension->set('state'-1);
  582.                 $extension->set('manifest_cache'json_encode($manifest_details));
  583.                 $extension->set('params''{}');
  584.                 $results[$extension;
  585.             }
  586.         }
  587.         return $results;
  588.     }
  589.  
  590.     /**
  591.      * Custom discover install method
  592.      * Basically updates the manifest cache and leaves everything alone
  593.      *
  594.      * @return  integer  The extension id
  595.      *
  596.      * @since   3.1
  597.      */
  598.     public function discover_install()
  599.     {
  600.         // Need to find to find where the XML file is since we don't store this normally
  601.         $client JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  602.         $short_element $this->parent->extension->element;
  603.         $manifestPath $client->path '/language/' $short_element '/' $short_element '.xml';
  604.         $this->parent->manifest $this->parent->isManifest($manifestPath);
  605.         $this->parent->setPath('manifest'$manifestPath);
  606.         $this->parent->setPath('source'$client->path '/language/' $short_element);
  607.         $this->parent->setPath('extension_root'$this->parent->getPath('source'));
  608.         $manifest_details JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
  609.         $this->parent->extension->manifest_cache json_encode($manifest_details);
  610.         $this->parent->extension->state 0;
  611.         $this->parent->extension->name $manifest_details['name'];
  612.         $this->parent->extension->enabled 1;
  613.  
  614.         // @todo remove code: $this->parent->extension->params = $this->parent->getParams();
  615.         try
  616.         {
  617.             $this->parent->extension->store();
  618.         }
  619.         catch (RuntimeException $e)
  620.         {
  621.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_LANG_DISCOVER_STORE_DETAILS')JLog::WARNING'jerror');
  622.  
  623.             return false;
  624.         }
  625.         return $this->parent->extension->get('extension_id');
  626.     }
  627.  
  628.     /**
  629.      * Refreshes the extension table cache
  630.      *
  631.      * @return  boolean result of operation, true if updated, false on failure
  632.      *
  633.      * @since   3.1
  634.      */
  635.     public function refreshManifestCache()
  636.     {
  637.         $client JApplicationHelper::getClientInfo($this->parent->extension->client_id);
  638.         $manifestPath $client->path '/language/' $this->parent->extension->element '/' $this->parent->extension->element '.xml';
  639.         $this->parent->manifest $this->parent->isManifest($manifestPath);
  640.         $this->parent->setPath('manifest'$manifestPath);
  641.         $manifest_details JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
  642.         $this->parent->extension->manifest_cache json_encode($manifest_details);
  643.         $this->parent->extension->name $manifest_details['name'];
  644.  
  645.         if ($this->parent->extension->store())
  646.         {
  647.             return true;
  648.         }
  649.         else
  650.         {
  651.             JLog::add(JText::_('JLIB_INSTALLER_ERROR_MOD_REFRESH_MANIFEST_CACHE')JLog::WARNING'jerror');
  652.  
  653.             return false;
  654.         }
  655.     }
  656. }
  657.  
  658. /**
  659.  * Deprecated class placeholder. You should use JInstallerAdapterLanguage instead.
  660.  *
  661.  * @package     Joomla.Libraries
  662.  * @subpackage  Installer
  663.  * @since       3.1
  664.  * @deprecated  4.0
  665.  * @codeCoverageIgnore
  666.  */
  667. {
  668. }

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