Source for file database.php

Documentation is available at database.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. JLoader::register('InstallerModel'__DIR__ . '/extension.php');
  13. JLoader::register('JoomlaInstallerScript'JPATH_ADMINISTRATOR '/components/com_admin/script.php');
  14.  
  15. /**
  16.  * Installer Manage Model
  17.  *
  18.  * @package     Joomla.Administrator
  19.  * @subpackage  com_installer
  20.  * @since       1.6
  21.  */
  22. {
  23.     protected $_context = 'com_installer.discover';
  24.  
  25.     /**
  26.      * Method to auto-populate the model state.
  27.      *
  28.      * Note. Calling getState in this method will result in recursion.
  29.      *
  30.      * @param   string  $ordering   An optional ordering field.
  31.      * @param   string  $direction  An optional direction (asc|desc).
  32.      *
  33.      * @return  void 
  34.      *
  35.      * @since   1.6
  36.      */
  37.     protected function populateState($ordering null$direction null)
  38.     {
  39.         $app JFactory::getApplication();
  40.         $this->setState('message'$app->getUserState('com_installer.message'));
  41.         $this->setState('extension_message'$app->getUserState('com_installer.extension_message'));
  42.         $app->setUserState('com_installer.message''');
  43.         $app->setUserState('com_installer.extension_message''');
  44.         parent::populateState('name''asc');
  45.     }
  46.  
  47.     /**
  48.      * Fixes database problems
  49.      *
  50.      * @return  void 
  51.      */
  52.     public function fix()
  53.     {
  54.         if (!$changeSet $this->getItems())
  55.         {
  56.             return false;
  57.         }
  58.         $changeSet->fix();
  59.         $this->fixSchemaVersion($changeSet);
  60.         $this->fixUpdateVersion();
  61.         $installer new JoomlaInstallerScript;
  62.         $installer->deleteUnexistingFiles();
  63.         $this->fixDefaultTextFilters();
  64.     }
  65.  
  66.     /**
  67.      * Gets the changeset object
  68.      *
  69.      * @return  JSchemaChangeset 
  70.      */
  71.     public function getItems()
  72.     {
  73.         $folder JPATH_ADMINISTRATOR '/components/com_admin/sql/updates/';
  74.  
  75.         try
  76.         {
  77.             $changeSet JSchemaChangeset::getInstance(JFactory::getDbo()$folder);
  78.         }
  79.         catch (RuntimeException $e)
  80.         {
  81.             JFactory::getApplication()->enqueueMessage($e->getMessage()'warning');
  82.             return false;
  83.         }
  84.         return $changeSet;
  85.     }
  86.  
  87.     /**
  88.      * Method to get a JPagination object for the data set.
  89.      *
  90.      * @return  boolean 
  91.      *
  92.      * @since   12.2
  93.      */
  94.     public function getPagination()
  95.     {
  96.         return true;
  97.     }
  98.  
  99.     /**
  100.      * Get version from #__schemas table
  101.      *
  102.      * @return  mixed  the return value from the query, or null if the query fails
  103.      *
  104.      * @throws Exception
  105.      */
  106.     public function getSchemaVersion()
  107.     {
  108.         $db JFactory::getDbo();
  109.         $query $db->getQuery(true)
  110.             ->select('version_id')
  111.             ->from($db->quoteName('#__schemas'))
  112.             ->where('extension_id = 700');
  113.         $db->setQuery($query);
  114.         $result $db->loadResult();
  115.  
  116.         return $result;
  117.     }
  118.  
  119.     /**
  120.      * Fix schema version if wrong
  121.      *
  122.      * @param   JSchemaChangeSet  $changeSet  Schema change set
  123.      *
  124.      * @return   mixed  string schema version if success, false if fail
  125.      */
  126.     public function fixSchemaVersion($changeSet)
  127.     {
  128.         // Get correct schema version -- last file in array
  129.         $schema $changeSet->getSchema();
  130.         $db JFactory::getDbo();
  131.         $result false;
  132.  
  133.         // Check value. If ok, don't do update
  134.         $version $this->getSchemaVersion();
  135.         if ($version == $schema)
  136.         {
  137.             $result $version;
  138.         }
  139.         else
  140.         {
  141.             // Delete old row
  142.             $query $db->getQuery(true)
  143.                 ->delete($db->quoteName('#__schemas'))
  144.                 ->where($db->quoteName('extension_id'' = 700');
  145.             $db->setQuery($query);
  146.             $db->execute();
  147.  
  148.             // Add new row
  149.             $query->clear()
  150.                 ->insert($db->quoteName('#__schemas'))
  151.                 ->set($db->quoteName('extension_id''= 700')
  152.                 ->set($db->quoteName('version_id''= ' $db->quote($schema));
  153.             $db->setQuery($query);
  154.             if ($db->execute())
  155.             {
  156.                 $result $schema;
  157.             }
  158.         }
  159.         return $result;
  160.     }
  161.  
  162.     /**
  163.      * Get current version from #__extensions table
  164.      *
  165.      * @return  mixed   version if successful, false if fail
  166.      */
  167.  
  168.     public function getUpdateVersion()
  169.     {
  170.         $table JTable::getInstance('Extension');
  171.         $table->load('700');
  172.         $cache new JRegistry($table->manifest_cache);
  173.         return $cache->get('version');
  174.     }
  175.  
  176.     /**
  177.      * Fix Joomla version in #__extensions table if wrong (doesn't equal JVersion short version)
  178.      *
  179.      * @return   mixed  string update version if success, false if fail
  180.      */
  181.     public function fixUpdateVersion()
  182.     {
  183.         $table JTable::getInstance('Extension');
  184.         $table->load('700');
  185.         $cache new JRegistry($table->manifest_cache);
  186.         $updateVersion $cache->get('version');
  187.         $cmsVersion new JVersion;
  188.         if ($updateVersion == $cmsVersion->getShortVersion())
  189.         {
  190.             return $updateVersion;
  191.         }
  192.         else
  193.         {
  194.             $cache->set('version'$cmsVersion->getShortVersion());
  195.             $table->manifest_cache $cache->toString();
  196.             if ($table->store())
  197.             {
  198.                 return $cmsVersion->getShortVersion();
  199.             }
  200.             else
  201.             {
  202.                 return false;
  203.             }
  204.  
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * For version 2.5.x only
  210.      * Check if com_config parameters are blank.
  211.      *
  212.      * @return  string  default text filters (if any)
  213.      */
  214.     public function getDefaultTextFilters()
  215.     {
  216.         $table JTable::getInstance('Extension');
  217.         $table->load($table->find(array('name' => 'com_config')));
  218.         return $table->params;
  219.     }
  220.     /**
  221.      * For version 2.5.x only
  222.      * Check if com_config parameters are blank. If so, populate with com_content text filters.
  223.      *
  224.      * @return  mixed  boolean true if params are updated, null otherwise
  225.      */
  226.     public function fixDefaultTextFilters()
  227.     {
  228.         $table JTable::getInstance('Extension');
  229.         $table->load($table->find(array('name' => 'com_config')));
  230.  
  231.         // Check for empty $config and non-empty content filters
  232.         if (!$table->params)
  233.         {
  234.             // Get filters from com_content and store if you find them
  235.             $contentParams JComponentHelper::getParams('com_content');
  236.             if ($contentParams->get('filters'))
  237.             {
  238.                 $newParams new JRegistry;
  239.                 $newParams->set('filters'$contentParams->get('filters'));
  240.                 $table->params = (string) $newParams;
  241.                 $table->store();
  242.                 return true;
  243.             }
  244.         }
  245.     }
  246. }

Documentation generated on Tue, 19 Nov 2013 14:57:52 +0100 by phpDocumentor 1.4.3