Source for file adapter.php
Documentation is available at adapter.php
* @package Joomla.Administrator
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Prototype adapter class for the Finder indexer package.
* @package Joomla.Administrator
* The context is somewhat arbitrary but it must be unique or there will be
* conflicts when managing plugin/indexer state. A good best practice is to
* use the plugin name suffix as the context. For example, if the plugin is
* named 'plgFinderContent', the context could be 'Content'.
* The sublayout to use when rendering the results.
* The mime type of the content the adapter indexes.
* The access level of an item before save.
* The access level of a category before save.
* The type of content the adapter indexes.
* The type id of the content.
* The field the published state is stored in.
* Method to instantiate the indexer adapter.
* @param object &$subject The object to observe.
* @param array $config An array that holds the plugin configuration.
// Get the database object.
// Call the parent constructor.
// Add the content type if it doesn't exist and is set.
// Check for a layout override.
if ($this->params->get('layout'))
// Get the indexer object
* Method to get the adapter state and push it into the indexer.
* @return boolean True on success.
* @throws Exception on error.
// Get the indexer state.
// Get the number of content items.
// Add the content count to the total number of items.
$iState->totalItems +=
$total;
// Populate the indexer state information for the adapter.
$iState->pluginState[$this->context]['total'] =
$total;
$iState->pluginState[$this->context]['offset'] =
0;
// Set the indexer state.
* Method to prepare for the indexer to be run. This method will often
* be used to include dependencies and things of that nature.
* @return boolean True on success.
* @throws Exception on error.
// Get the indexer and adapter state.
$aState =
$iState->pluginState[$this->context];
// Check the progress of the indexer and the adapter.
if ($iState->batchOffset ==
$iState->batchSize ||
$aState['offset'] ==
$aState['total'])
* Method to index a batch of content items. This method can be called by
* the indexer many times throughout the indexing process depending on how
* much content is available for indexing. It is important to track the
* progress correctly so we can display it to the user.
* @return boolean True on success.
* @throws Exception on error.
// Get the indexer and adapter state.
$aState =
$iState->pluginState[$this->context];
// Check the progress of the indexer and the adapter.
if ($iState->batchOffset ==
$iState->batchSize ||
$aState['offset'] ==
$aState['total'])
// Get the batch offset and size.
$offset = (int)
$aState['offset'];
$limit = (int)
($iState->batchSize -
$iState->batchOffset);
// Get the content items to index.
$items =
$this->getItems($offset, $limit);
// Iterate through the items and index them.
for ($i =
0, $n =
count($items); $i <
$n; $i++
)
$this->index($items[$i]);
// Update the indexer state.
$aState['offset'] =
$offset;
$iState->pluginState[$this->context] =
$aState;
* Method to change the value of a content item's property in the links
* table. This is used to synchronize published and access states that
* are changed when not editing an item directly.
* @param string $id The ID of the item to change.
* @param string $property The property that is being changed.
* @param integer $value The new value of that property.
* @return boolean True on success.
* @throws Exception on database error.
protected function change($id, $property, $value)
// Check for a property we know how to handle.
if ($property !==
'state' &&
$property !==
'access')
// Get the url for the content id.
// Update the content items.
$query =
$this->db->getQuery(true)
->update($this->db->quoteName('#__finder_links'))
->set($this->db->quoteName($property) .
' = ' . (int)
$value)
->where($this->db->quoteName('url') .
' = ' .
$item);
$this->db->setQuery($query);
* Method to index an item.
* @param FinderIndexerResult $item The item to index as a FinderIndexerResult object.
* @return boolean True on success.
* @throws Exception on database error.
abstract protected function index(FinderIndexerResult $item);
* Method to reindex an item.
* @param integer $id The ID of the item to reindex.
* @return boolean True on success.
* @throws Exception on database error.
* Method to remove an item from the index.
* @param string $id The ID of the item to remove.
* @return boolean True on success.
* @throws Exception on database error.
protected function remove($id)
// Get the link ids for the content items.
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('link_id'))
->from($this->db->quoteName('#__finder_links'))
->where($this->db->quoteName('url') .
' = ' .
$url);
$this->db->setQuery($query);
$items =
$this->db->loadColumn();
foreach ($items as $item)
* Method to setup the adapter before indexing.
* @return boolean True on success, false on failure.
* @throws Exception on database error.
abstract protected function setup();
* Method to update index data on category access level changes
* @param JTable $row A JTable object
$query->where('c.id = ' . (int)
$row->id);
$this->db->setQuery($query);
$items =
$this->db->loadObjectList();
// Adjust the access level for each item within the category.
foreach ($items as $item)
$temp =
max($item->access, $row->access);
$this->change((int)
$item->id, 'access', $temp);
* Method to update index data on category access level changes
* @param array $pks A list of primary key ids of the content that has changed state.
* @param integer $value The value of the state that the content has been changed to.
* The item's published state is tied to the category
* published state so we need to look up all published states
* before we change anything.
$query->where('c.id = ' . (int)
$pk);
// Get the published states.
$this->db->setQuery($query);
$items =
$this->db->loadObjectList();
// Adjust the state for each item within the category.
foreach ($items as $item)
$this->change($item->id, 'state', $temp);
* Method to check the existing access level for categories
* @param JTable $row A JTable object
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('access'))
->from($this->db->quoteName('#__categories'))
->where($this->db->quoteName('id') .
' = ' . (int)
$row->id);
$this->db->setQuery($query);
// Store the access level to determine if it changes
* Method to check the existing access level for items
* @param JTable $row A JTable object
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('access'))
->from($this->db->quoteName($this->table))
->where($this->db->quoteName('id') .
' = ' . (int)
$row->id);
$this->db->setQuery($query);
// Store the access level to determine if it changes
* Method to get the number of content items available to index.
* @return integer The number of content items available to index.
* @throws Exception on database error.
// Check if the query is valid.
// Tweak the SQL query to make the total lookup faster.
// Get the total number of content items to index.
$this->db->setQuery($query);
$return = (int)
$this->db->loadResult();
* Method to get a content item to index.
* @param integer $id The id of the content item.
* @return FinderIndexerResult A FinderIndexerResult object.
* @throws Exception on database error.
// Get the list query and add the extra WHERE clause.
$query->where('a.id = ' . (int)
$id);
// Get the item to index.
$this->db->setQuery($query);
$row =
$this->db->loadAssoc();
// Convert the item to a result object.
$item->layout =
$this->layout;
* Method to get a list of content items to index.
* @param integer $offset The list offset.
* @param integer $limit The list limit.
* @param JDatabaseQuery $query A JDatabaseQuery object. [optional]
* @return array An array of FinderIndexerResult objects.
* @throws Exception on database error.
protected function getItems($offset, $limit, $query =
null)
// Get the content items to index.
$rows =
$this->db->loadAssocList();
// Convert the items to result objects.
// Convert the item to a result object.
$item->mime =
$this->mime;
$item->layout =
$this->layout;
// Set the extension if present
if (isset
($row->extension))
$item->extension =
$row->extension;
// Add the item to the stack.
* Method to get the SQL query used to retrieve the list of content items.
* @param mixed $query A JDatabaseQuery object. [optional]
* @return JDatabaseQuery A database object.
// Check if we can use the supplied SQL query.
* Method to get the plugin type
* @param integer $id The plugin ID
* @return string The plugin type
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('element'))
->from($this->db->quoteName('#__extensions'))
->where($this->db->quoteName('extension_id') .
' = ' . (int)
$id);
$this->db->setQuery($query);
$type =
$this->db->loadResult();
* Method to get a SQL query to load the published and access states for
* an article and category.
* @return JDatabaseQuery A database object.
$query =
$this->db->getQuery(true);
// Item and category published state
$query->select('a.' .
$this->state_field .
' AS state, c.published AS cat_state');
// Item and category access levels
$query->select('a.access, c.access AS cat_access')
->from($this->table .
' AS a')
->join('LEFT', '#__categories AS c ON c.id = a.catid');
* Method to get the query clause for getting items to update by time.
* @param string $time The modified timestamp.
* @return JDatabaseQuery A database object.
// Build an SQL query based on the modified time.
$query =
$this->db->getQuery(true)
->where('a.modified >= ' .
$this->db->quote($time));
* Method to get the query clause for getting items to update by id.
* @param array $ids The ids to load.
* @return JDatabaseQuery A database object.
// Build an SQL query based on the item ids.
$query =
$this->db->getQuery(true)
->where('a.id IN(' .
implode(',', $ids) .
')');
* Method to get the type id for the adapter content.
* @return integer The numeric type id for the content.
* @throws Exception on database error.
// Get the type id from the database.
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('id'))
->from($this->db->quoteName('#__finder_types'))
->where($this->db->quoteName('title') .
' = ' .
$this->db->quote($this->type_title));
$this->db->setQuery($query);
$result = (int)
$this->db->loadResult();
* Method to get the URL for the item. The URL is how we look up the link
* @param integer $id The id of the item.
* @param string $extension The extension the category is in.
* @param string $view The view for the URL.
* @return string The URL of the item.
protected function getURL($id, $extension, $view)
return 'index.php?option=' .
$extension .
'&view=' .
$view .
'&id=' .
$id;
* Method to get the page title of any menu item that is linked to the
* content item, if it exists and is set.
* @param string $url The url of the item.
* @return mixed The title on success, null if not found.
* @throws Exception on database error.
$groups =
implode(',', $user->getAuthorisedViewLevels());
// Build a query to get the menu params.
$query =
$this->db->getQuery(true)
->select($this->db->quoteName('params'))
->from($this->db->quoteName('#__menu'))
->where($this->db->quoteName('link') .
' = ' .
$this->db->quote($url))
->where($this->db->quoteName('published') .
' = 1')
->where($this->db->quoteName('access') .
' IN (' .
$groups .
')');
// Get the menu params from the database.
$this->db->setQuery($query);
$params =
$this->db->loadResult();
// Instantiate the params.
// Get the page title if it is set.
$return =
$params->page_title;
* Method to update index data on access level changes
* @param JTable $row A JTable object
$query->where('a.id = ' . (int)
$row->id);
$this->db->setQuery($query);
$item =
$this->db->loadObject();
$temp =
max($row->access, $item->cat_access);
$this->change((int)
$row->id, 'access', $temp);
* Method to update index data on published state changes
* @param array $pks A list of primary key ids of the content that has changed state.
* @param integer $value The value of the state that the content has been changed to.
* The item's published state is tied to the category
* published state so we need to look up all published states
* before we change anything.
$query->where('a.id = ' . (int)
$pk);
// Get the published states.
$this->db->setQuery($query);
$item =
$this->db->loadObject();
$this->change($pk, 'state', $temp);
* Method to update index data when a plugin is disabled
* @param array $pks A list of primary key ids of the content that has changed state.
// Since multiple plugins may be disabled at a time, we need to check first
// that we're handling the appropriate one for the context
// Get all of the items to unindex them
$this->db->setQuery($query);
$items =
$this->db->loadColumn();
foreach ($items as $item)
* Method to translate the native content states into states that the
* @param integer $item The item state.
* @param integer $category The category state. [optional]
* @return integer The translated indexer state.
// If category is present, factor in its states as well
// Published and archived items only should return a published state
// All other states should return a unpublished state
Documentation generated on Tue, 19 Nov 2013 14:53:24 +0100 by phpDocumentor 1.4.3