/**
  * This method will insert all URLs that exists in this object into the
  * database by calling the StaticPagesQueue
  *
  * @return type
  */
 public function insertIntoDB()
 {
     $arraycopy = $this->getArrayCopy();
     usort($arraycopy, array($this, 'sortOnPriority'));
     foreach ($arraycopy as $array) {
         StaticPagesQueue::add_to_queue($array[0], $array[1]);
     }
     StaticPagesQueue::push_urls_to_db();
     $this->exchangeArray(array());
 }
 public function testRemoveDuplicates()
 {
     Injector::inst()->get('URLArrayObject')->addUrls(array('test1' => 1, 'test2' => 1, 'test3' => 1));
     Injector::inst()->get('URLArrayObject')->addUrls(array('test2' => 2, 'test3' => 2));
     Injector::inst()->get('URLArrayObject')->addUrls(array('test2' => 3));
     $test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\'');
     $test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
     $test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
     $this->assertEquals(1, $test1Objs->Count());
     $this->assertEquals(3, $test2Objs->Count());
     $this->assertEquals(2, $test3Objs->Count());
     StaticPagesQueue::remove_duplicates($test1Objs->First()->ID);
     $test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\'');
     $test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
     $test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
     $this->assertEquals(1, $test1Objs->Count(), 'Keeps original instance without any duplicates found');
     $this->assertEquals(3, $test2Objs->Count(), 'Doesnt remove unrelated duplicates');
     $this->assertEquals(2, $test3Objs->Count(), 'Doesnt remove unrelated duplicates');
     StaticPagesQueue::remove_duplicates($test2Objs->First()->ID);
     $test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\'');
     $this->assertEquals(1, $test2Objs->Count(), 'Removing a single duplicate');
     StaticPagesQueue::remove_duplicates($test3Objs->First()->ID);
     $test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\'');
     $this->assertEquals(1, $test3Objs->Count(), 'Removing multiple duplicates');
 }
 /**
  * This will push all the currently cached insert statements to be pushed 
  * into the database
  *
  * @return void
  */
 public static function push_urls_to_db()
 {
     foreach (self::$insert_statements as $stmt) {
         $insertSQL = 'INSERT INTO "StaticPagesQueue" ("Created", "LastEdited", "Priority", "URLSegment") VALUES ' . $stmt;
         DB::query($insertSQL);
     }
     self::remove_old_cache(self::$urls);
     // Flush the cache so DataObject::get works correctly
     if (!empty(self::$insert_statements) && DB::affectedRows()) {
         singleton(__CLASS__)->flushCache();
     }
     self::$insert_statements = array();
 }
 /**
  * Generic callback, to catch uncaught exceptions when they bubble up to the top of the call chain.
  *
  * @ignore
  * @param Exception $exception
  */
 public static function exception_handler($exception)
 {
     StaticPagesQueue::has_error(self::$current_url);
     $errno = E_USER_ERROR;
     $type = get_class($exception);
     $message = "Uncaught " . $type . ": " . $exception->getMessage();
     $file = $exception->getFile();
     $line = $exception->getLine();
     $context = $exception->getTrace();
     Debug::fatalHandler($errno, $message, $file, $line, $context);
 }