Removes rows that match a filter

@param *string|null $table   The table name
@param array|string $filters Filters to test against

@return AbstractSql
7144
public function deleteRows($table, $filters = null)
    {
        $query = $this->getDeleteQuery($table);

        //array('post_id=%s AND post_title IN %s', 123, array('asd'));
        if (is_array($filters)) {
            //can be array of arrays
            if (is_array($filters[0])) {
                foreach ($filters as $i => $filter) {
                    if (is_array($filters)) {
                        $format = array_shift($filter);

                        //reindex filters
                        $filter = array_values($filter);

                        //bind filters
                        foreach ($filter as $i => $value) {
                            $filter[$i] = $this->bind($value);
                        }

                        //combine
                        $query->where(vsprintf($format, $filter));
                    }
                }
            } else {
                $format = array_shift($filters);

                //reindex filters
                $filters = array_values($filters);

                //bind filters
                foreach ($filters as $i => $value) {
                    $filters[$i] = $this->bind($value);
                }

                //combine
                $query->where(vsprintf($format, $filters));
            }
        } else {
            $query->where($filters);
        }

        //run the query
        $this->query($query, $this->getBinds());

        return $this;
    }
851078
private function createFilteredQuery(array $filters)
    {
        $qb = $this->createQuery('entity');

        foreach ($filters as $field => $filter) {
            $qb->andWhere(is_array($filter)
                    ? sprintf('entity.%s in (:%s)', $field, $field)
                    : sprintf('entity.%s = :%s', $field, $field)
                )
                ->setParameter(sprintf(':%s', $field), $filter)
            ;
        }

        return $qb->getQuery();
    }
729107
public function setFilters(array $filters): AbstractRepository
    {
        foreach ($filters as $f => $v) {
            $this->filterBy($f, $v);
        }

        return $this;
    }
1063562
public function deleteFrom($table, $where = null)
    {
        $this->clear();

        if (isset($where)) {
            $this->where = "WHERE\n\t(" . $where . ")";
        }

        $this->start = "DELETE";
        $this->from($table);
        return $this;
    }
1077708
public function where($filters, $values = null)
    {
        if ($filters === null) {
            return $this;
        }

        foreach (is_array($filters) ? $filters : [$filters => $values] as $filter => $value) {
            if (is_int($filter)) {
                $this->whereExpr($value);
            } elseif (is_array($value)) {
                if (preg_match('#([~@!<>|=%]+)$#', $filter, $match)) {
                    $operator = $match[1];
                    $field = substr($filter, 0, -strlen($operator));
                    if ($operator === '~=') {
                        if (count($value) !== 2) {
                            throw new MisuseException(['`value of :filter` filter is invalid', 'filter' => $filter]);
                        }
                        $this->whereBetween($field, $value[0], $value[1]);
                    } elseif ($operator === '@=') {
                        $this->whereDateBetween($field, $value[0], $value[1]);
                    } elseif ($operator === '|=') {
                        $this->_filters[] = [$field => ['$in' => $value]];
                    } elseif ($operator === '!=' || $operator === '<>') {
                        $this->whereNotIn($field, $value);
                    } elseif ($operator === '=') {
                        $this->whereIn($field, $value);
                    } elseif ($operator === '%=') {
                        $this->whereMod($field, $value[0], $value[1]);
                    } else {
                        throw new MisuseException(['unknown `:operator` operator', 'operator' => $operator]);
                    }
                } elseif (!$value || isset($value[0])) {
                    $this->whereIn($filter, $value);
                } else {
                    $this->_filters[] = [$filter => $value];
                }
            } elseif (preg_match('#^([\w\.]+)\s*([<>=!^$*~,@dm?]*)$#', $filter, $matches) === 1) {
                list(, $field, $operator) = $matches;

                if (strpos($operator, '?') !== false) {
                    $value = is_string($value) ? trim($value) : $value;
                    if ($value === '' || $value === null) {
                        continue;
                    }
                    $operator = substr($operator, 0, -1);
                }

                if ($operator === '') {
                    $operator = '=';
                }

                if (in_array($operator, ['=', '~=', '!=', '<>', '>', '>=', '<', '<='], true)) {
                    $this->whereCmp($field, $operator, $value);
                } elseif ($operator === '^=') {
                    $this->whereStartsWith($field, $value);
                } elseif ($operator === '$=') {
                    $this->whereEndsWith($field, $value);
                } elseif ($operator === '*=') {
                    $this->whereContains($field, $value);
                } elseif ($operator === ',=') {
                    $this->whereInset($field, $value);
                } elseif ($operator === '@d=') {
                    $this->whereDate($field, $value);
                } elseif ($operator === '@m=') {
                    $this->whereMonth($field, $value);
                } else {
                    throw new MisuseException(['unknown `:operator` operator', 'operator' => $operator]);
                }
            } elseif (strpos($filter, ',') !== false && preg_match('#^[\w,\.]+$#', $filter)) {
                $this->where1v1($filter, $value);
            } else {
                throw new InvalidValueException(['unknown mongodb query `filter` filter', 'filter' => $filter]);
            }
        }

        return $this;
    }
934894
public function filter($table)
    {
        foreach ($table->getRows() as $key => $row) {
            //instead search must handle
            // - negative search with -piwik
            // - exact match with ""
            // see (?!pattern) 	A subexpression that performs a negative lookahead search, which matches the search string at any point where a string not matching pattern begins.
            $value = $row->getColumn($this->columnToFilter);
            if ($value === false) {
                $value = $row->getMetadata($this->columnToFilter);
            }
            if (!self::match($this->patternToSearchQuoted, $value, $this->invertedMatch)) {
                $table->deleteRow($key);
            }
        }
    }
1187390
public function filter($table)
    {
        $rows = $table->getRows();

        foreach ($rows as $key => $row) {
            // A row is deleted if
            // 1 - its label doesn't contain the pattern
            // AND 2 - the label is not found in the children
            $patternNotFoundInChildren = false;

            $subTable = $row->getSubtable();
            if (!$subTable) {
                $patternNotFoundInChildren = true;
            } else {
                // we delete the row if we couldn't find the pattern in any row in the
                // children hierarchy
                if ($this->filter($subTable) == 0) {
                    $patternNotFoundInChildren = true;
                }
            }

            if ($patternNotFoundInChildren
                && !Pattern::match($this->patternToSearchQuoted, $row->getColumn($this->columnToFilter), $invertedMatch = false)
            ) {
                $table->deleteRow($key);
            }
        }

        return $table->getRowsCount();
    }
24850
public function delete($table = null)
    {
        $this->_dirty();
        $this->_type = 'delete';
        if ($table !== null) {
            $this->from($table);
        }

        return $this;
    }
858921
public function filter($filters)
    {
        $filtered = [];
        foreach ($this->objectArr as $obj) {
            $success = true;
            foreach ($filters as $prop => $value) {
                if ($obj->$prop !== $value) {
                    $success = false;
                    break;
                }
            }
            if ($success) {
                $filtered[] = $obj;
            }
        }
        return $filtered;
    }
620973
public function delete($table, $where = null)
    {
        if (isset($where)) {
            $where = " WHERE " . $where;
        }

        $this->sql = "DELETE FROM "
            . $this->prefix
            . $table
            . $where
            . ";\n";
    }
500263
public function withFilters(array $filters = null)
    {
        if ($filters) {
            foreach ($filters as $filter) {
                switch ($filter['type']) {
                    case 'select':
                        $this->select($filter['fields']);
                        break;
                    case 'andWhere':
                        $this->andWhere($filter['field'], $filter['operation'], $filter['value']);
                        break;
                    case 'orWhere':
                        $this->orWhere($filter['field'], $filter['operation'], $filter['value']);
                        break;
                    case 'andHaving':
                        $this->andHaving($filter['field'], $filter['operation'], $filter['value']);
                        break;
                    case 'orHaving':
                        $this->orHaving($filter['field'], $filter['operation'], $filter['value']);
                        break;
                    case 'addGroupBy':
                        $this->addGroupBy($filter['field']);
                        break;
                    case 'addOrderBy':
                        $this->addOrderBy($filter['field'], $filter['order']);
                        break;
                    case 'fkAddOrderBy':
                        $this->fkAddOrderBy($filter['field'], $filter['fkField'], $filter['order']);
                        break;
                    case 'paginate':
                        if (isset($filter['page'])) {
                            $this->paginate($filter['limit'], $filter['page']);
                        } else {
                            $this->paginate($filter['limit']);
                        }
                        break;
                }
            }
        }

        return $this;
    }
552447
public function filter($table)
    {
        $this->enableRecursive(true);

        foreach ($table->getRows() as $row) {
            $row->deleteMetadata($this->metadataToRemove);

            $this->filterSubTable($row);
        }
    }
693152
public function filter($table)
    {
        $row = new DataTableSummaryRow($table);
        $row->setColumn('label', $this->labelSummaryRow);
        $table->addSummaryRow($row);
    }
447872
public function delete(string $table): Query\Delete
    {
        $query = new Query\Delete();
        $query->delete($table);
        return $query;
    }
1168080
public function filter($table)
    {
        // set of all column names in the pivoted table mapped with the sum of all column
        // values. used later in truncating and ordering the pivoted table's columns.
        $columnSet = array();

        // if no pivot column was set, use the first one found in the row
        if (empty($this->pivotColumn)) {
            $this->pivotColumn = $this->getNameOfFirstNonLabelColumnInTable($table);
        }

        Log::debug("PivotByDimension::%s: pivoting table with pivot column = %s", __FUNCTION__, $this->pivotColumn);

        foreach ($table->getRows() as $row) {
            $row->setColumns(array('label' => $row->getColumn('label')));

            $associatedTable = $this->getIntersectedTable($table, $row);
            if (!empty($associatedTable)) {
                foreach ($associatedTable->getRows() as $columnRow) {
                    $pivotTableColumn = $columnRow->getColumn('label');

                    $columnValue = $this->getColumnValue($columnRow, $this->pivotColumn);

                    if (isset($columnSet[$pivotTableColumn])) {
                        $columnSet[$pivotTableColumn] += $columnValue;
                    } else {
                        $columnSet[$pivotTableColumn] = $columnValue;
                    }

                    $row->setColumn($pivotTableColumn, $columnValue);
                }

                Common::destroy($associatedTable);
                unset($associatedTable);
            }
        }

        Log::debug("PivotByDimension::%s: pivoted columns set: %s", __FUNCTION__, $columnSet);

        $others = Piwik::translate('General_Others');
        $defaultRow = $this->getPivotTableDefaultRowFromColumnSummary($columnSet, $others);

        Log::debug("PivotByDimension::%s: un-prepended default row: %s", __FUNCTION__, $defaultRow);

        // post process pivoted datatable
        foreach ($table->getRows() as $row) {
            // remove subtables from rows
            $row->removeSubtable();
            $row->deleteMetadata('idsubdatatable_in_db');

            // use default row to ensure column ordering and add missing columns/aggregate cut-off columns
            $orderedColumns = $defaultRow;
            foreach ($row->getColumns() as $name => $value) {
                if (isset($orderedColumns[$name])) {
                    $orderedColumns[$name] = $value;
                } else {
                    $orderedColumns[$others] += $value;
                }
            }
            $row->setColumns($orderedColumns);
        }

        $table->clearQueuedFilters(); // TODO: shouldn't clear queued filters, but we can't wait for them to be run
                                      //       since generic filters are run before them. remove after refactoring
                                      //       processed metrics.

        // prepend numerals to columns in a queued filter (this way, disable_queued_filters can be used
        // to get machine readable data from the API if needed)
        $prependedColumnNames = $this->getOrderedColumnsWithPrependedNumerals($defaultRow, $others);

        Log::debug("PivotByDimension::%s: prepended column name mapping: %s", __FUNCTION__, $prependedColumnNames);

        $table->queueFilter(function (DataTable $table) use ($prependedColumnNames) {
            foreach ($table->getRows() as $row) {
                $row->setColumns(array_combine($prependedColumnNames, $row->getColumns()));
            }
        });
    }
563951
public static function applyQueryFilters($query, $filters, $columns, $table)
	{
		// Wrap in a complex where so we don't break soft delete checks
		$query->where(
			function ($query) use ($filters, $columns, $table) {
				self::filterQuery($query, $filters, $columns, $table);
			});
	}
386239
public function filter(array $filters)
	{
		foreach ($filters as $filter) {
			if ($filter->isValueSet()) {
				if ($filter->hasConditionCallback()) {
					$data = (array) call_user_func_array(
						$filter->getConditionCallback(),
						[$this->data, $filter->getValue()]
					);
					$this->setData($data);
				} else {
					$data = array_filter($this->data, function ($row) use ($filter) {
						return $this->applyFilter($row, $filter);
					});
					$this->setData($data);
				}
			}
		}

		return $this;
	}