private function getSortKeyForItem($item) { if ($item instanceof DIWikiPage) { $label = $this->store->getWikiPageSortKey($item); $value = $label; } else { $label = $item instanceof DIBlob ? $item->getString() : ''; $value = $item->getSortKey(); } return array($label, $value); }
/** * 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; }