1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Some optimizations to PagesLoader::find() and findShortcut() methods.

This commit is contained in:
Ryan Cramer
2016-12-19 07:00:25 -05:00
parent aab97a0b00
commit 315251fa04

View File

@@ -114,16 +114,21 @@ class PagesLoader extends Wire {
if(empty($selector)) return $this->pages->newPageArray($loadOptions); if(empty($selector)) return $this->pages->newPageArray($loadOptions);
if(!empty($options['lazy'])) return false; if(!empty($options['lazy'])) return false;
$value = false;
$filter = empty($options['findOne']);
if(is_array($selector)) { if(is_array($selector)) {
if(ctype_digit(implode('', array_keys($selector))) && !is_array(reset($selector)) && ctype_digit(implode('', $selector))) { if(ctype_digit(implode('', array_keys($selector))) && !is_array(reset($selector)) && ctype_digit(implode('', $selector))) {
// if given a regular array of page IDs, we delegate that to getById() method, but with access/visibility control // if given a regular array of page IDs, we delegate that to getById() method, but with access/visibility control
return $this->filterListable( $value = $this->getById($selector, $loadOptions);
$this->getById($selector), $filter = true;
(isset($options['include']) ? $options['include'] : ''),
$loadOptions);
} }
} else if(is_int($selector)) {
$value = $this->getById(array($selector), $loadOptions);
} else if(is_string($selector) || is_int($selector)) { } else if(is_string($selector) || is_int($selector)) {
// normalize selectors that indicate homepage to just be ID 1 // normalize selectors that indicate homepage to just be ID 1
@@ -137,18 +142,26 @@ class PagesLoader extends Wire {
if(ctype_digit("$selector") || strpos($selector, "id=") === 0) { if(ctype_digit("$selector") || strpos($selector, "id=") === 0) {
// if selector is just a number, or a string like "id=123" then we're going to do a shortcut // if selector is just a number, or a string like "id=123" then we're going to do a shortcut
$s = str_replace("id=", '', $selector); $s = str_replace("id=", '', $selector);
if(ctype_digit("$s")) { if(ctype_digit(str_replace('|', '', "$s"))) {
$value = $this->getById(array((int) $s), $loadOptions); $a = explode('|', $s);
if(empty($options['findOne'])) $value = $this->filterListable( foreach($a as $k => $v) $a[$k] = (int) $v;
$value, (isset($options['include']) ? $options['include'] : ''), $loadOptions); $value = $this->getById($a, $loadOptions);
if($this->debug) $this->pages->debugLog('find', $selector . " [optimized]", $value);
return $value;
} }
} }
} }
} }
return false; if($value) {
if($filter) {
$includeMode = isset($options['include']) ? $options['include'] : '';
$value = $this->filterListable($value, $includeMode, $loadOptions);
}
if($this->debug) {
$this->pages->debugLog('find', $selector . " [optimized]", $value);
}
}
return $value;
} }
/** /**
@@ -189,9 +202,11 @@ class PagesLoader extends Wire {
$debug = $this->debug && !$lazy; $debug = $this->debug && !$lazy;
$cachePages = isset($options['cache']) ? (bool) $options['cache'] : true; $cachePages = isset($options['cache']) ? (bool) $options['cache'] : true;
if(!$cachePages && !isset($loadOptions['cache'])) $loadOptions['cache'] = false; if(!$cachePages && !isset($loadOptions['cache'])) $loadOptions['cache'] = false;
$pages = $this->findShortcut($selector, $options, $loadOptions);
if($pages) return $pages; if($loadPages) {
$pages = $this->findShortcut($selector, $options, $loadOptions);
if($pages) return $pages;
}
if($selector instanceof Selectors) { if($selector instanceof Selectors) {
$selectors = $selector; $selectors = $selector;
@@ -228,7 +243,7 @@ class PagesLoader extends Wire {
$pagesInfo = $pageFinder->find($selectors, $options); $pagesInfo = $pageFinder->find($selectors, $options);
} }
if($this->debug && empty($loadOptions['caller'])) { if($debug && empty($loadOptions['caller'])) {
$loadOptions['caller'] = "$caller($selectorString)"; $loadOptions['caller'] = "$caller($selectorString)";
} }