<commit_before><?php

/**
 * @author <<EMAIL>>
 * @license BSD License http://www.silverstripe.org/bsd-license
 */
class RestrictedDataList extends Extension {
	
	public function restrict($perm = 'View') {
		$restrictFilter = function ($item) use ($perm) {
			if ($item->hasExtension('Restrictable') && $item->checkPerm($perm)) {
				return true;
			} else if ($item->hasMethod('can' . $perm)) {
				$method = 'can' . $perm;
				return $item->$method();
			} else {
				return $item->canView();
			}
		};
		
		$unrestrictedCount = $this->owner->count();
		
		$list = $this->owner->filterByCallback($restrictFilter);
		
		$listCount = $list->count();
		
		if ($unrestrictedCount == $listCount) {
			return $list;
		}
		
		// otherwise we need to get the 
		$limitInfo = $this->owner->dataQuery()->getFinalisedQuery()->getLimit();
		if (isset($limitInfo['limit'])) {
			$limit = $limitInfo['limit'];
		} else {
			$limit = 0;
		}
		
		if ($limit && $unrestrictedCount == $limit) {
			$targetNumber = (int) trim($limit);
			$count = $list->count();
			$lastCount = $count;
			$newOffset = $limitInfo['start'];
			while ($count < $targetNumber) {
				$nextOffset = $newOffset = $newOffset + $targetNumber;
				$nextList = $this->owner->limit($targetNumber, $newOffset);
				foreach ($nextList as $item) {
					$nextOffset++;
					if ($item->hasExtension('Restrictable')) {
						if ($item->checkPerm($perm)) {
							$list->push($item);
						}
					} else if ($item->canView()) {
						$list->push($item);
					}

					if ($list->count() >= $targetNumber) {
						break;
					}
				}

				$count = $list->count();
				// if we haven't actually increased, we'll just bail here
				if ($lastCount == $count) {
					break;
				}
			}
			
			$list->QueryOffset = $nextOffset;
		}
		
		
		
		return $list;
	}
<commit_msg>FIX Further fixes to how the QueryOffset is calculated and added into the restricted list<commit_after><?php

/**
 * @author <<EMAIL>>
 * @license BSD License http://www.silverstripe.org/bsd-license
 */
class RestrictedDataList extends Extension {
	
	public function restrict($perm = 'View') {
		$restrictFilter = function ($item) use ($perm) {
			if ($item->hasExtension('Restrictable') && $item->checkPerm($perm)) {
				return true;
			} else if ($item->hasMethod('can' . $perm)) {
				$method = 'can' . $perm;
				return $item->$method();
			} else {
				return $item->canView();
			}
		};
		
		$unrestrictedCount = $this->owner->count();
		
		$list = $this->owner->filterByCallback($restrictFilter);
		
		$listCount = $list->count();
		
		// otherwise we need to get the 
		$limitInfo = $this->owner->dataQuery()->getFinalisedQuery()->getLimit();
		if (isset($limitInfo['limit'])) {
			$limit = $limitInfo['limit'];
		} else {
			$limit = 0;
		}

		if ($limit) {
			$list->QueryOffset = $limitInfo['start'] + $limit;
		}

		if ($unrestrictedCount == $listCount) {
			return $list;
		}

		if ($limit && $unrestrictedCount == $limit) {
			$targetNumber = (int) trim($limit);
			$count = $list->count();
			$lastCount = $count;
			$newOffset = $limitInfo['start'];
			while ($count < $targetNumber) {
				$nextOffset = $newOffset = $newOffset + $targetNumber;
				$nextList = $this->owner->limit($targetNumber, $newOffset);
				foreach ($nextList as $item) {
					$nextOffset++;
					if ($item->hasExtension('Restrictable')) {
						if ($item->checkPerm($perm)) {
							$list->push($item);
						}
					} else if ($item->canView()) {
						$list->push($item);
					}

					if ($list->count() >= $targetNumber) {
						break;
					}
				}

				$count = $list->count();
				// if we haven't actually increased, we'll just bail here
				if ($lastCount == $count) {
					break;
				}
			}
			
			$list->QueryOffset = $nextOffset;
		}
		
		return $list;
	}

{'fraction_non_alphanumeric': 0.1887443589062915, 'fraction_numerical': 0.0005309264666843643, 'mean_word_length': 3.0213447171824974, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7199948', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1213, 'n_words': 265}