function run($request)
 {
     increase_time_limit_to();
     $self = get_class($this);
     $verbose = isset($_GET['verbose']);
     if (isset($_GET['class']) && isset($_GET['id'])) {
         $item = DataObject::get($_GET['class'])->byID($_GET['id']);
         if (!$item || !$item->exists()) {
             die('not found: ' . $_GET['id']);
         }
         $item->rebuildVFI();
         echo "done";
         return;
     }
     if (isset($_GET['link'])) {
         $item = SiteTree::get_by_link($_GET['link']);
         if (!$item || !$item->exists()) {
             die('not found: ' . $_GET['link']);
         }
         $item->rebuildVFI();
         echo "done";
         return;
     }
     if (isset($_GET['start'])) {
         $this->runFrom($_GET['class'], $_GET['start'], $_GET['field']);
     } else {
         foreach (array('framework', 'sapphire') as $dirname) {
             $script = sprintf("%s%s{$dirname}%scli-script.php", BASE_PATH, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
             if (file_exists($script)) {
                 break;
             }
         }
         $classes = VirtualFieldIndex::get_classes_with_vfi();
         foreach ($classes as $class) {
             if (isset($_GET['class']) && $class != $_GET['class']) {
                 continue;
             }
             $singleton = singleton($class);
             $query = $singleton->get($class);
             $dtaQuery = $query->dataQuery();
             $sqlQuery = $dtaQuery->getFinalisedQuery();
             $singleton->extend('augmentSQL', $sqlQuery, $dtaQuery);
             $total = $query->count();
             $startFrom = isset($_GET['startfrom']) ? $_GET['startfrom'] : 0;
             $field = isset($_GET['field']) ? $_GET['field'] : '';
             echo "Class: {$class}, total: {$total}\n\n";
             for ($offset = $startFrom; $offset < $total; $offset += $this->stat('recordsPerRequest')) {
                 echo "{$offset}..";
                 $cmd = "php {$script} dev/tasks/{$self} class={$class} start={$offset} field={$field}";
                 if ($verbose) {
                     echo "\n  Running '{$cmd}'\n";
                 }
                 $res = $verbose ? passthru($cmd) : `{$cmd}`;
                 if ($verbose) {
                     echo "  " . preg_replace('/\\r\\n|\\n/', '$0  ', $res) . "\n";
                 }
             }
         }
     }
 }
 public function testStaticAttributes()
 {
     VirtualFieldIndex::build('Product');
     foreach (Product::get() as $p) {
         $p->publish('Stage', 'Live');
     }
     $c = $this->objFromFixture('ProductCategory', 'c3');
     $c->publish('Stage', 'Live');
     // set up some attributes
     $p1 = $this->objFromFixture('Product', 'p1');
     $p2 = $this->objFromFixture('Product', 'p2');
     $pat1 = $this->objFromFixture('ProductAttributeType', 'pat1');
     $pat1v1 = $this->objFromFixture('ProductAttributeValue', 'pat1v1');
     $pat1v2 = $this->objFromFixture('ProductAttributeValue', 'pat1v2');
     $p1->StaticAttributeTypes()->add($pat1);
     $p1->StaticAttributeValues()->add($pat1v1);
     $p1->StaticAttributeValues()->add($pat1v2);
     $p2->StaticAttributeTypes()->add($pat1);
     $p2->StaticAttributeValues()->add($pat1v1);
     // Should be able to filter by an attribute
     $attkey = 'ATT' . $pat1->ID;
     $prods = FacetHelper::inst()->addFiltersToDataList($c->ProductsShowable(), array($attkey => $pat1v1->ID));
     $this->assertEquals(2, $prods->count(), 'Should be 2 products for v1');
     $prods = FacetHelper::inst()->addFiltersToDataList($c->ProductsShowable(), array($attkey => $pat1v2->ID));
     $this->assertEquals(1, $prods->count(), 'Should be 1 product for v2');
     // Should be able to facet by ATT1 explicitly
     $facets = FacetHelper::inst()->buildFacets($c->ProductsShowable(), array($attkey => array('Label' => 'By Color', 'Type' => ShopSearch::FACET_TYPE_LINK)));
     $this->assertEquals(1, $facets->count(), 'Should be 1 facet');
     $f1 = $facets->First();
     $this->assertEquals(2, $f1->Values->count(), 'Should be 2 values');
     $this->assertEquals('Red', $f1->Values->First()->Label);
     $this->assertEquals(2, $f1->Values->First()->Count);
     $this->assertEquals('Green', $f1->Values->Last()->Label);
     $this->assertEquals(1, $f1->Values->Last()->Count);
     // Should be able to facet by auto_facet_attributes
     $facets = FacetHelper::inst()->buildFacets($c->ProductsShowable(), array(), true);
     $this->assertEquals(1, $facets->count(), 'Should be 1 facet');
     $f1 = $facets->First();
     $this->assertEquals(2, $f1->Values->count(), 'Should be 2 values');
     $this->assertEquals('Red', $f1->Values->First()->Label);
     $this->assertEquals(2, $f1->Values->First()->Count);
     $this->assertEquals('Green', $f1->Values->Last()->Label);
     $this->assertEquals(1, $f1->Values->Last()->Count);
 }