public function requestOptionsToSqlConditionsProvider() { $provider = array(); # 0 $requestOptions = new RequestOptions(); $requestOptions->boundary = true; $provider[] = array($requestOptions, 'Foo', '', ' AND Foo >= '); # 1 $requestOptions = new RequestOptions(); $requestOptions->boundary = true; $requestOptions->addStringCondition('foobar', \SMWStringCondition::STRCOND_PRE); $provider[] = array($requestOptions, 'Foo', 'Bar', ' AND Foo >= AND Bar LIKE '); return $provider; }
private function selectPropertiesFromTable() { // the query needs to do the filtering of internal properties, else LIMIT is wrong $options = array('ORDER BY' => 'smw_sortkey'); $conditions = array('smw_namespace' => SMW_NS_PROPERTY, 'smw_iw' => ''); if ($this->requestOptions->limit > 0) { $options['LIMIT'] = $this->requestOptions->limit; $options['OFFSET'] = max($this->requestOptions->offset, 0); } if ($this->requestOptions->getStringConditions()) { $conditions[] = $this->store->getSQLConditions($this->requestOptions, '', 'smw_title', false); } $res = $this->store->getConnection('mw.db')->select($this->store->getObjectIds()->getIdTable(), array('smw_id', 'smw_title'), $conditions, __METHOD__, $options); return $res; }
private function doQueryPropertyTable() { // the query needs to do the filtering of internal properties, else LIMIT is wrong $options = array('ORDER BY' => 'smw_sortkey'); $conditions = array('smw_namespace' => SMW_NS_PROPERTY, 'smw_iw' => ''); if ($this->requestOptions->limit > 0) { $options['LIMIT'] = $this->requestOptions->limit; $options['OFFSET'] = max($this->requestOptions->offset, 0); } if ($this->requestOptions->getStringConditions()) { $conditions[] = $this->store->getSQLConditions($this->requestOptions, '', 'smw_sortkey', false); } $db = $this->store->getConnection('mw.db'); $res = $db->select(array($db->tableName(SQLStore::ID_TABLE), $db->tableName(SQLStore::PROPERTY_STATISTICS_TABLE)), array('smw_title', 'usage_count'), $conditions, __METHOD__, $options, array($db->tableName(SQLStore::ID_TABLE) => array('INNER JOIN', array('smw_id=p_id')))); return $res; }
/** * Transform input parameters into a suitable string of additional SQL * conditions. The parameter $valuecol defines the string name of the * column to which value restrictions etc. are to be applied. * * @since 1.8 * * @param RequestOptions|null $requestOptions * @param string $valueCol name of SQL column to which conditions apply * @param string $labelCol name of SQL column to which string conditions apply, if any * @param boolean $addAnd indicate whether the string should begin with " AND " if non-empty * * @return string */ public function getSQLConditionsFrom(RequestOptions $requestOptions = null, $valueCol = '', $labelCol = '', $addAnd = true) { $sqlConds = ''; if ($requestOptions === null) { return $sqlConds; } $connection = $this->store->getConnection('mw.db'); // Apply value boundary if ($valueCol !== '' && $requestOptions->boundary !== null) { if ($requestOptions->ascending) { $op = $requestOptions->include_boundary ? ' >= ' : ' > '; } else { $op = $requestOptions->include_boundary ? ' <= ' : ' < '; } $sqlConds .= ($addAnd ? ' AND ' : '') . $valueCol . $op . $connection->addQuotes($requestOptions->boundary); } // Apply string conditions if ($labelCol !== '') { foreach ($requestOptions->getStringConditions() as $strcond) { $string = str_replace('_', '\\_', $strcond->string); $condition = 'LIKE'; switch ($strcond->condition) { case StringCondition::COND_PRE: $string .= '%'; break; case StringCondition::COND_POST: $string = '%' . $string; break; case StringCondition::COND_MID: $string = '%' . $string . '%'; break; case StringCondition::COND_EQ: $condition = '='; break; } $conditionOperator = $strcond->isDisjunctiveCondition ? ' OR ' : ' AND '; $sqlConds .= ($addAnd || $sqlConds !== '' ? $conditionOperator : '') . "{$labelCol} {$condition} " . $connection->addQuotes($string); } } return $sqlConds; }
/** * Not in all cases can requestoptions be forwarded to the DB using * getSQLConditions() and getSQLOptions(): some data comes from caches * that do not respect the options yet. This method takes an array of * results (SMWDataItem objects) *of the same type* and applies the * given requestoptions as appropriate. * * @since 1.8 * @param array $data array of SMWDataItem objects * @param SMWRequestOptions|null $requestoptions * * @return SMWDataItem[] */ public function applyRequestOptionsTo(array $data, RequestOptions $requestoptions = null) { if ($data === array() || $requestoptions === null) { return $data; } $result = array(); $sortres = array(); $sampleDataItem = reset($data); $numeric = is_numeric($sampleDataItem->getSortKey()); $i = 0; foreach ($data as $item) { $ok = true; // keep datavalue only if this remains true if ($item instanceof DIWikiPage) { $label = $this->store->getWikiPageSortKey($item); $value = $label; } else { $label = $item instanceof DIBlob ? $item->getString() : ''; $value = $item->getSortKey(); } if ($requestoptions->boundary !== null) { // apply value boundary $strc = $numeric ? 0 : strcmp($value, $requestoptions->boundary); if ($requestoptions->ascending) { if ($requestoptions->include_boundary) { $ok = $numeric ? $value >= $requestoptions->boundary : $strc >= 0; } else { $ok = $numeric ? $value > $requestoptions->boundary : $strc > 0; } } else { if ($requestoptions->include_boundary) { $ok = $numeric ? $value <= $requestoptions->boundary : $strc <= 0; } else { $ok = $numeric ? $value < $requestoptions->boundary : $strc < 0; } } } foreach ($requestoptions->getStringConditions() as $strcond) { // apply string conditions switch ($strcond->condition) { case StringCondition::STRCOND_PRE: $ok = $ok && strpos($label, $strcond->string) === 0; break; case StringCondition::STRCOND_POST: $ok = $ok && strpos(strrev($label), strrev($strcond->string)) === 0; break; case StringCondition::STRCOND_MID: $ok = $ok && strpos($label, $strcond->string) !== false; break; } } if ($ok) { $result[$i] = $item; $sortres[$i] = $value; $i++; } } if ($requestoptions->sort) { $flag = $numeric ? SORT_NUMERIC : SORT_LOCALE_STRING; if ($requestoptions->ascending) { asort($sortres, $flag); } else { arsort($sortres, $flag); } $newres = array(); foreach ($sortres as $key => $value) { $newres[] = $result[$key]; } $result = $newres; } if ($requestoptions->limit > 0) { $result = array_slice($result, $requestoptions->offset, $requestoptions->limit); } else { $result = array_slice($result, $requestoptions->offset); } return $result; }
/** * Transform input parameters into a suitable string of additional SQL * conditions. The parameter $valuecol defines the string name of the * column to which value restrictions etc. are to be applied. * * @since 1.8 * @param SMWRequestOptions|null $requestoptions * @param string $valuecol name of SQL column to which conditions apply * @param string $labelcol name of SQL column to which string conditions apply, if any * @param boolean $addand indicate whether the string should begin with " AND " if non-empty * @return string */ public function getSQLConditions(SMWRequestOptions $requestoptions = null, $valuecol = '', $labelcol = '', $addand = true) { $sql_conds = ''; if ($requestoptions !== null) { $db = wfGetDB(DB_SLAVE); /// TODO avoid doing this here again, all callers should have one if ($valuecol !== '' && $requestoptions->boundary !== null) { // Apply value boundary. if ($requestoptions->ascending) { $op = $requestoptions->include_boundary ? ' >= ' : ' > '; } else { $op = $requestoptions->include_boundary ? ' <= ' : ' < '; } $sql_conds .= ($addand ? ' AND ' : '') . $valuecol . $op . $db->addQuotes($requestoptions->boundary); } if ($labelcol !== '') { // Apply string conditions. foreach ($requestoptions->getStringConditions() as $strcond) { $string = str_replace('_', '\\_', $strcond->string); switch ($strcond->condition) { case SMWStringCondition::STRCOND_PRE: $string .= '%'; break; case SMWStringCondition::STRCOND_POST: $string = '%' . $string; break; case SMWStringCondition::STRCOND_MID: $string = '%' . $string . '%'; break; } $sql_conds .= ($addand || $sql_conds !== '' ? ' AND ' : '') . $labelcol . ' LIKE ' . $db->addQuotes($string); } } } return $sql_conds; }
/** * Returns sql conditions of $requestoptions in an Array. * Warning! Does not support SMWAdvRequestOptions * * @param SMWRequestOptions $requestoptions * @param string $valuecol * @param string $labelcol * @return array */ public static function getSQLConditionsAsArray($requestoptions, $valuecol, $labelcol = NULL) { $sql_conds = array(); if ($requestoptions !== NULL) { $db =& wfGetDB(DB_SLAVE); if ($requestoptions->boundary !== NULL) { // apply value boundary if ($requestoptions->ascending) { if ($requestoptions->include_boundary) { $op = ' >= '; } else { $op = ' > '; } } else { if ($requestoptions->include_boundary) { $op = ' <= '; } else { $op = ' < '; } } $sql_conds[] = mysql_real_escape_string($valuecol) . $op . $db->addQuotes($requestoptions->boundary); } if ($labelcol !== NULL) { // apply string conditions foreach ($requestoptions->getStringConditions() as $strcond) { $string = str_replace(array('_', ' '), array('\\_', '\\_'), $strcond->string); switch ($strcond->condition) { case SMWStringCondition::STRCOND_PRE: $string .= '%'; break; case SMWStringCondition::STRCOND_POST: $string = '%' . $string; break; case SMWStringCondition::STRCOND_MID: $string = '%' . $string . '%'; break; } $sql_conds[] = 'UPPER(' . self::convertColumn(mysql_real_escape_string($labelcol)) . ') LIKE UPPER(' . $db->addQuotes($string) . ')'; } } } return $sql_conds; }
/** * @since 2.2 * * @return string */ public function getHash() { return __METHOD__ . '#' . ($this->requestOptions !== null ? $this->requestOptions->getHash() : ''); }
public function requestOptionsToApplyProvider() { $provider = array(); #0 $requestOptions = new RequestOptions(); $requestOptions->boundary = true; $provider[] = array(array(new \SMWDIBlob('Foo')), $requestOptions, array(new \SMWDIBlob('Foo'))); #1 $requestOptions = new RequestOptions(); $requestOptions->addStringCondition('Foo', StringCondition::STRCOND_PRE); $provider[] = array(array(new \SMWDIBlob('Foo')), $requestOptions, array(new \SMWDIBlob('Foo'))); #2 String not match $requestOptions = new RequestOptions(); $requestOptions->addStringCondition('Bar', StringCondition::STRCOND_POST); $provider[] = array(array(new \SMWDIBlob('Foo')), $requestOptions, array()); #3 Limit $requestOptions = new RequestOptions(); $requestOptions->limit = 1; $provider[] = array(array(new \SMWDIBlob('Foo'), new \SMWDIBlob('Bar')), $requestOptions, array(new \SMWDIBlob('Foo'))); #4 ascending $requestOptions = new RequestOptions(); $requestOptions->sort = true; $requestOptions->ascending = true; $provider[] = array(array(new \SMWDIBlob('Foo'), new \SMWDIBlob('Bar')), $requestOptions, array(new \SMWDIBlob('Bar'), new \SMWDIBlob('Foo'))); #5 descending $requestOptions = new RequestOptions(); $requestOptions->sort = true; $requestOptions->ascending = false; $provider[] = array(array(new \SMWDIBlob('Foo'), new \SMWDIBlob('Bar')), $requestOptions, array(new \SMWDIBlob('Foo'), new \SMWDIBlob('Bar'))); #6 descending $requestOptions = new RequestOptions(); $requestOptions->sort = true; $requestOptions->ascending = false; $provider[] = array(array(new \SMWDINumber(10), new \SMWDINumber(200)), $requestOptions, array(new \SMWDINumber(200), new \SMWDINumber(10))); return $provider; }
/** * Filters for properties containg the given hint as substring (case-insensitive) * Returns an property list with all entities found * * @return xml string */ function filterForProperties($propertyHints) { $reqfilter = new SMWRequestOptions(); $reqfilter->sort = true; //$reqfilter->limit = MAX_RESULTS; if (count($propertyHints) == 0) { return "<propertyList isEmpty=\"true\" textToDisplay=\"" . wfMsg('smw_ob_no_properties') . "\"/>"; } foreach ($propertyHints as $hint) { $reqfilter->addStringCondition($hint, SMWStringCondition::STRCOND_MID); } $reqfilter->isCaseSensitive = false; $foundProperties = smwfGetSemanticStore()->getPropertiesWithSchemaByName($reqfilter); return SMWOntologyBrowserXMLGenerator::encapsulateAsPropertyList($foundProperties); }