public function run($request) { $confirm = $request->getVar('run') ? true : false; $force = $request->getVar('force') ? true : false; $since = $request->getVar('older'); if (!$since) { echo "Please specify an 'older' param with a date older than which to prune (in strtotime friendly format)<br/>\n"; return; } $since = strtotime($since); if (!$since) { echo "Please specify an 'older' param with a date older than which to prune (in strtotime friendly format)<br/>\n"; return; } if ($since > strtotime('-3 months') && !$force) { echo "To cleanup data more recent than 3 months, please supply the 'force' parameter as well as the run parameter, swapping to dry run <br/>\n"; $confirm = false; } $since = date('Y-m-d H:i:s', $since); $items = DataChangeRecord::get()->filter('Created:LessThan', $since); $max = $items->max('ID'); echo "Pruning records older than {$since} (ID {$max})<br/>\n"; if ($confirm && $max) { $query = new SQLQuery('*', 'DataChangeRecord', '"ID" < \'' . $max . '\''); $query->setDelete(true); $query->execute(); } else { echo "Dry run performed, please supply the run=1 parameter to actually execute the deletion!<br/>\n"; } }
private function checkBlogEntryPermissions() { $authorsId = array(); $sqlQuery = new SQLQuery(); $sqlQuery->setFrom('SiteTree_versions'); $sqlQuery->selectField('AuthorID'); $sqlQuery->addWhere('RecordID = ' . $this->ID); $sqlQuery->setOrderBy('ID DESC'); $rawSQL = $sqlQuery->sql(); $result = $sqlQuery->execute(); foreach ($result as $row) { $authorsId[] = $row['AuthorID']; } $sqlQuery->setDelete(true); if (in_array(Member::currentUser()->ID, $authorsId) || $this->parent->OwnerID == Member::currentUser()->ID || Permission::check('ADMIN')) { return true; } else { return false; } }
/** * Delete this data object. * $this->onBeforeDelete() gets called. * Note that in Versioned objects, both Stage and Live will be deleted. * @uses DataExtension->augmentSQL() */ public function delete() { $this->brokenOnDelete = true; $this->onBeforeDelete(); if ($this->brokenOnDelete) { user_error("{$this->class} has a broken onBeforeDelete() function. Make sure that you call parent::onBeforeDelete().", E_USER_ERROR); } // Deleting a record without an ID shouldn't do anything if (!$this->ID) { throw new Exception("DataObject::delete() called on a DataObject without an ID"); } // TODO: This is quite ugly. To improve: // - move the details of the delete code in the DataQuery system // - update the code to just delete the base table, and rely on cascading deletes in the DB to do the rest // obviously, that means getting requireTable() to configure cascading deletes ;-) $srcQuery = DataList::create($this->class, $this->model)->where("ID = {$this->ID}")->dataQuery()->query(); foreach ($srcQuery->queriedTables() as $table) { $query = new SQLQuery("*", array('"' . $table . '"')); $query->setWhere("\"ID\" = {$this->ID}"); $query->setDelete(true); $query->execute(); } // Remove this item out of any caches $this->flushCache(); $this->onAfterDelete(); $this->OldID = $this->ID; $this->ID = 0; }
/** * Create a database table to replay the site tree creation, based on the chronological order of the site tree version table. */ protected function setupStructure() { if (!DB::getConn() instanceof MySQLDatabase) { exit('This task currently only supports <strong>MySQL</strong>...'); } $replaceArray = self::$db_columns; unset($replaceArray['FullURL']); $this->replaceColumnString = implode(',', array_keys($replaceArray)); $tableList = DB::tableList(); if (self::$use_temporary_table || !in_array(self::$default_table, $tableList)) { $options = self::$use_temporary_table ? array('temporary' => true) : null; $this->replayTable = DB::createTable(self::$default_table, self::$db_columns, null, $options); } else { // Delete all records from the table. $query = new SQLQuery('', self::$default_table); $query->setDelete(true); $query->execute(); } }
/** * Remove the given item from this list. * Note that for a ManyManyList, the item is never actually deleted, only the join table is affected * @param $itemID The item it */ public function removeByID($itemID) { if (!is_numeric($itemID)) { throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID"); } $query = new SQLQuery("*", array("\"{$this->joinTable}\"")); $query->setDelete(true); if ($filter = $this->foreignIDFilter()) { $query->setWhere($filter); } else { user_error("Can't call ManyManyList::remove() until a foreign ID is set", E_USER_WARNING); } $query->addWhere("\"{$this->localKey}\" = {$itemID}"); $query->execute(); }
/** * Remove all items from this many-many join. To remove a subset of items, * filter it first. * * @return void */ public function removeAll() { $base = ClassInfo::baseDataClass($this->dataClass()); // Remove the join to the join table to avoid MySQL row locking issues. $query = $this->dataQuery(); $query->removeFilterOn($query->getQueryParam('Foreign.Filter')); $query = $query->query(); $query->setSelect("\"{$base}\".\"ID\""); $from = $query->getFrom(); unset($from[$this->joinTable]); $query->setFrom($from); $query->setDistinct(false); // ensure any default sorting is removed, ORDER BY can break DELETE clauses $query->setOrderBy(null, null); // Use a sub-query as SQLite does not support setting delete targets in // joined queries. $delete = new SQLQuery(); $delete->setDelete(true); $delete->setFrom("\"{$this->joinTable}\""); $delete->addWhere($this->foreignIDFilter()); $delete->addWhere("\"{$this->joinTable}\".\"{$this->localKey}\" IN ({$query->sql()})"); $delete->execute(); }
/** * Test deprecation of SQLQuery::setDelete/getDelete */ public function testDeprecatedSetDelete() { // Temporarily disable deprecation Deprecation::notification_version(null); $query = new SQLQuery(); $query->setSelect(array('"SQLQueryTest_DO"."Name"')); $query->setFrom('"SQLQueryTest_DO"'); $query->setWhere(array('"SQLQueryTest_DO"."Name"' => 'Andrew')); // Check SQL for select $this->assertSQLEquals(<<<EOS SELECT "SQLQueryTest_DO"."Name" FROM "SQLQueryTest_DO" WHERE ("SQLQueryTest_DO"."Name" = ?) EOS , $query->sql($parameters)); $this->assertEquals(array('Andrew'), $parameters); // Check setDelete works $query->setDelete(true); $this->assertSQLEquals(<<<EOS DELETE FROM "SQLQueryTest_DO" WHERE ("SQLQueryTest_DO"."Name" = ?) EOS , $query->sql($parameters)); $this->assertEquals(array('Andrew'), $parameters); // Check that setDelete back to false restores the state $query->setDelete(false); $this->assertSQLEquals(<<<EOS SELECT "SQLQueryTest_DO"."Name" FROM "SQLQueryTest_DO" WHERE ("SQLQueryTest_DO"."Name" = ?) EOS , $query->sql($parameters)); $this->assertEquals(array('Andrew'), $parameters); }