Assets indexing from plugin controller I am making a dashboard widget to trigger the assets indexing action.
I got the controller ready but I do not understand then how to trigger the action.

Should I go with AssetIndexTool? Then performAction()?
Or using ToolsController ?

I am really lost. Thanks for help!
I guess you would need to have your own method in your controller to fire up the indexing tool. From what it looks like, you need a few things to get going:


The classname of the tool i.e AssetIndex
An array of asset sources or * for them all
An indexing session id
An array of parameters to send to the tool


So I managed to get something working with the following code (this would go in your plugin controller)

public function actionUpdateIndex()
{
  // Get the AssetIndex tool
  $tool = craft()-&gt;components-&gt;getComponentByTypeAndClass(ComponentType::Tool, 'AssetIndex');

  // Generate an AssetIndex session id for us to use
  $sessionId = craft()-&gt;assetIndexing-&gt;getIndexingSessionId();

  // Set up some params
  $params = array(
    'start' =&gt; '1', // Tells the tool this is the initial request
    'sources' =&gt; '*', // All of them or [1,2,3] just ids1
  );

  // Get an initial response from the tool, will contain an array containing the assets to index
  $response = $tool-&gt;performAction($params);

  // See [1]
  foreach($response['batches'] as $key =&gt; $value)
  {
    foreach($value as $key =&gt; $batch)
    {
       // All the needed params are set in the array to do the index
       $tool-&gt;performAction($batch['params']);
    }
  }

}


Each time you call $tool-&gt;performAction($batch['params']); you will an array back, something like:

Array
(
 [success] =&gt; 1
)




[1] Because this is the initial request craft will return an array with a batches index, which we then have to do a few loops through, it looks something like this:

Array
(
 [batches] =&gt; Array
  (
   [0] =&gt; Array
    (
     [0] =&gt; Array
      (
       [params] =&gt; Array
        (
         [sessionId] =&gt; 6d08c5ba-169f-4b99-a7bd-cdc26c9428c7
         [sourceId] =&gt; 1
         [total] =&gt; 12
         [offset] =&gt; 0
         [process] =&gt; 1
        )

       )

      )

    )

 [total] =&gt; 0
)


Not sure if this is the absolute best way to do it, I was learning as I was going, but this worked for me so hopefully it'll work for you too :)