Skip to content

Commit

Permalink
Finder: Add debug features for indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackwar committed Jan 19, 2022
1 parent 1a85bf3 commit 10ca120
Show file tree
Hide file tree
Showing 15 changed files with 1,695 additions and 31 deletions.
20 changes: 20 additions & 0 deletions administrator/components/com_finder/forms/indexer.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset name="form">
<field
name="plugin"
type="plugins"
label="COM_FINDER_FIELD_FINDER_PLUGIN_LABEL"
folder="finder"
required="true"
/>

<field
name="id"
type="text"
label="JGLOBAL_FIELD_ID_LABEL"
size="10"
required="true"
/>
</fieldset>
</form>
133 changes: 117 additions & 16 deletions administrator/components/com_finder/src/Controller/IndexerController.php
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Session\Session;
use Joomla\Component\Finder\Administrator\Indexer\Debugindexer;
use Joomla\Component\Finder\Administrator\Indexer\Indexer;
use Joomla\Component\Finder\Administrator\Response\Response;

Expand Down Expand Up @@ -158,22 +159,6 @@ public function batch()
// Import the finder plugins.
PluginHelper::importPlugin('finder');

/*
* We are going to swap out the raw document object with an HTML document
* in order to work around some plugins that don't do proper environment
* checks before trying to use HTML document functions.
*/
$lang = Factory::getLanguage();

// Get the document properties.
$attributes = array (
'charset' => 'utf-8',
'lineend' => 'unix',
'tab' => ' ',
'language' => $lang->getTag(),
'direction' => $lang->isRtl() ? 'rtl' : 'ltr'
);

// Start the indexer.
try
{
Expand Down Expand Up @@ -310,4 +295,120 @@ public static function sendResponse($data = null)
// Send the JSON response.
echo json_encode($response);
}

/**
* Method to call a specific indexing plugin and return debug info
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function debug()
{
// Check for a valid token. If invalid, send a 403 with the error message.
if (!Session::checkToken('request'))
{
static::sendResponse(new \Exception(Text::_('JINVALID_TOKEN_NOTICE'), 403));

return;
}

// We don't want this form to be cached.
$this->app->allowCache(false);

// Put in a buffer to silence noise.
ob_start();

// Remove the script time limit.
@set_time_limit(0);

// Get the indexer state.
Indexer::resetState();
$state = Indexer::getState();

// Reset the batch offset.
$state->batchOffset = 0;

// Update the indexer state.
Indexer::setState($state);

// Start the indexer.
try
{
// Import the finder plugins.
require_once JPATH_ADMINISTRATOR . '/components/com_finder/src/Indexer/Debugadapter.php';
$plugin = Factory::getApplication()->bootPlugin($this->app->input->get('plugin'), 'finder');
$plugin->setIndexer(new Debugindexer);
$plugin->debug($this->app->input->get('id'));

$output = '';

// Create list of attributes
$output .= '<fieldset><legend>' . Text::_('COM_FINDER_INDEXER_FIELDSET_ATTRIBUTES') . '</legend>';
$output .= '<dl class="row">';

foreach (Debugindexer::$item as $key => $value)
{
$output .= '<dt class="col-sm-2">' . $key . '</dt><dd class="col-sm-10">' . $value . '</dd>';
}

$output .= '</dl>';
$output .= '</fieldset>';

$output .= '<fieldset><legend>' . Text::_('COM_FINDER_INDEXER_FIELDSET_ELEMENTS') . '</legend>';
$output .= '<dl class="row">';

foreach (Debugindexer::$item->getElements() as $key => $element)
{
$output .= '<dt class="col-sm-2">' . $key . '</dt><dd class="col-sm-10">' . $element . '</dd>';
}

$output .= '</dl>';
$output .= '</fieldset>';

$output .= '<fieldset><legend>' . Text::_('COM_FINDER_INDEXER_FIELDSET_INSTRUCTIONS') . '</legend>';
$output .= '<dl class="row">';
$contexts = [
1 => 'Title context',
2 => 'Text context',
3 => 'Meta context',
4 => 'Path context',
5 => 'Misc context'
];

foreach (Debugindexer::$item->getInstructions() as $key => $element)
{
$output .= '<dt class="col-sm-2">' . $contexts[$key] . '</dt><dd class="col-sm-10">' . json_encode($element) . '</dd>';
}

$output .= '</dl>';
$output .= '</fieldset>';

$output .= '<fieldset><legend>' . Text::_('COM_FINDER_INDEXER_FIELDSET_TAXONOMIES') . '</legend>';
$output .= '<dl class="row">';

foreach (Debugindexer::$item->getTaxonomy() as $key => $element)
{
$output .= '<dt class="col-sm-2">' . $key . '</dt><dd class="col-sm-10">' . json_encode($element) . '</dd>';
}

$output .= '</dl>';
$output .= '</fieldset>';

// Get the indexer state.
$state = Indexer::getState();
$state->start = 0;
$state->complete = 0;
$state->rendered = $output;

echo json_encode($state);
}

// Catch an exception and return the response.
catch (\Exception $e)
{
// Send the response.
static::sendResponse($e);
}
}
}

0 comments on commit 10ca120

Please sign in to comment.