1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-10 00:37:02 +02:00

Various minor code updates

This commit is contained in:
Ryan Cramer
2022-05-10 08:24:50 -04:00
parent 14a9b92c70
commit bf80df04e9
7 changed files with 76 additions and 41 deletions

View File

@@ -8,7 +8,7 @@
*
* This is the most used object in the ProcessWire API.
*
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
* https://processwire.com
*
* @link http://processwire.com/api/variables/pages/ Offical $pages Documentation
@@ -211,7 +211,7 @@ class Pages extends Wire {
*
*/
public function init() {
$this->loader->getById($this->wire('config')->preloadPageIDs);
$this->loader->getById($this->wire()->config->preloadPageIDs);
}
/****************************************************************************************************************
@@ -746,7 +746,7 @@ class Pages extends Wire {
if($template instanceof Template) {
// cool, cool
} else if(is_int($template) || is_string($template)) {
$template = $this->wire('templates')->get($template);
$template = $this->wire()->templates->get($template);
} else {
$template = null;
}
@@ -1775,7 +1775,7 @@ class Pages extends Wire {
'action' => (string) $action,
'details' => (string) $details,
'result' => (string) $result
);
);
}
/**
@@ -1790,7 +1790,7 @@ class Pages extends Wire {
*
*/
public function getDebugLog($action = '') {
if(!$this->wire('config')->debug) return array();
if(!$this->wire()->config->debug) return array();
if(!$action) return $this->debugLog;
$debugLog = array();
foreach($this->debugLog as $item) if($item['action'] == $action) $debugLog[] = $item;
@@ -1798,7 +1798,7 @@ class Pages extends Wire {
}
/**
* Return a PageFinder object, ready to use
* Return a new PageFinder object, ready to use
*
* #pw-internal
*
@@ -1950,7 +1950,7 @@ class Pages extends Wire {
*
*/
public function executeQuery(\PDOStatement $query, $throw = true, $maxTries = 3) {
return $this->wire('database')->execute($query, $throw, $maxTries);
return $this->wire()->database->execute($query, $throw, $maxTries);
}
/**
@@ -1965,11 +1965,26 @@ class Pages extends Wire {
*
*/
public function __invoke($key) {
if(empty($key)) return $this; // no argument
if(is_int($key)) return $this->get($key); // page ID
if(is_array($key) && ctype_digit(implode('', $key))) return $this->getById($key); // array of page IDs
if(is_string($key) && strpos($key, '/') !== false && $this->sanitizer->pagePathName($key) === $key) return $this->get($key); // page path
return $this->find($key); // selector string or array
// no argument
if(empty($key)) return $this;
// page ID
if(is_int($key)) return $this->get($key);
// array of page IDs
if(is_array($key) && ctype_digit(implode('', $key))) {
return $this->getById($key);
}
// page path
if(is_string($key) && strpos($key, '/') !== false) {
if($this->wire()->sanitizer->pagePathName($key) === $key) {
return $this->get($key);
}
}
// selector string or array
return $this->find($key);
}
/**
@@ -1983,8 +1998,8 @@ class Pages extends Wire {
*
*/
public function log($str, Page $page) {
if(!in_array('pages', $this->wire('config')->logs)) return parent::___log();
if($this->wire('process') != 'ProcessPageEdit') $str .= " [From URL: " . $this->wire('input')->url() . "]";
if(!in_array('pages', $this->wire()->config->logs)) return parent::___log();
if($this->wire()->process != 'ProcessPageEdit') $str .= " [From URL: " . $this->wire()->input->url() . "]";
$options = array('name' => 'pages', 'url' => $page->path);
return parent::___log($str, $options);
}
@@ -2150,9 +2165,7 @@ class Pages extends Wire {
$str = "Saved page";
if(count($changes)) $str .= " (Changes: " . implode(', ', $changes) . ")";
$this->log($str, $page);
/** @var WireCache $cache */
$cache = $this->wire('cache');
$cache->maintenance($page);
$this->wire()->cache->maintenance($page);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->saved($page, $changes, $values);
}
@@ -2301,9 +2314,7 @@ class Pages extends Wire {
public function ___deleted(Page $page, array $options = array()) {
if($options) {}
if(empty($options['_deleteBranch'])) $this->log("Deleted page", $page);
/** @var WireCache $cache */
$cache = $this->wire('cache');
$cache->maintenance($page);
$this->wire()->cache->maintenance($page);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->deleted($page);
}

View File

@@ -92,7 +92,7 @@ class PagesLoader extends Wire {
}
/**
* Set whether loaded pages have their outputFormatting turn on or off
* Set whether loaded pages have their outputFormatting turned on or off
*
* By default, it is turned on.
*
@@ -102,7 +102,13 @@ class PagesLoader extends Wire {
public function setOutputFormatting($outputFormatting = true) {
$this->outputFormatting = $outputFormatting ? true : false;
}
/**
* Get whether loaded pages have their outputFormatting turned on or off
*
* @return bool
*
*/
public function getOutputFormatting() {
return $this->outputFormatting;
}
@@ -118,8 +124,14 @@ class PagesLoader extends Wire {
*/
public function setAutojoin($autojoin = true) {
$this->autojoin = $autojoin ? true : false;
}
}
/**
* Get whether autojoin is enabled for page loading queries
*
* @return bool
*
*/
public function getAutojoin() {
return $this->autojoin;
}
@@ -142,7 +154,7 @@ class PagesLoader extends Wire {
} else if($selector === '/' || $selector === 'path=/') {
// normalize selectors that indicate homepage to just be ID 1
$selector = (int) $this->wire('config')->rootPageID;
$selector = (int) $this->wire()->config->rootPageID;
} else if($selector[0] === '/') {
// if selector begins with a slash, it is referring to a path
@@ -166,7 +178,7 @@ class PagesLoader extends Wire {
}
} else if(!Selectors::stringHasOperator($selector)) {
// no operator indicates this is just referring to a page name
$sanitizer = $this->wire('sanitizer');
$sanitizer = $this->wire()->sanitizer;
if($sanitizer->pageNameUTF8($selector) === $selector) {
// sanitized value consistent with a page name
// optimize selector rather than determining value here
@@ -1503,7 +1515,7 @@ class PagesLoader extends Wire {
$path = trim($path, '/');
if($templatesID) {
$template = $this->wire('templates')->get($templatesID);
$template = $this->wire()->templates->get($templatesID);
if($template->slashUrls) $path .= '/';
}
@@ -1866,7 +1878,10 @@ class PagesLoader extends Wire {
if(empty($selector)) {
if(empty($options)) {
// optimize away a simple site-wide total count
return (int) $this->wire('database')->query("SELECT COUNT(*) FROM pages")->fetch(\PDO::FETCH_COLUMN);
$query = $this->wire()->database->query("SELECT COUNT(*) FROM pages");
$count = (int) $query->fetch(\PDO::FETCH_COLUMN);
$query->closeCursor();
return (int) $count;
} else {
// no selector string, but options specified
$selector = "id>0";
@@ -1923,8 +1938,8 @@ class PagesLoader extends Wire {
*/
public function getNativeColumns() {
if(empty($this->nativeColumns)) {
$query = $this->wire('database')->prepare("SELECT * FROM pages WHERE id=:id");
$query->bindValue(':id', $this->wire('config')->rootPageID, \PDO::PARAM_INT);
$query = $this->wire()->database->prepare("SELECT * FROM pages WHERE id=:id");
$query->bindValue(':id', $this->wire()->config->rootPageID, \PDO::PARAM_INT);
$query->execute();
$row = $query->fetch(\PDO::FETCH_ASSOC);
foreach(array_keys($row) as $colName) {
@@ -1948,13 +1963,14 @@ class PagesLoader extends Wire {
public function getNativeColumnValue($id, $column) {
$id = (is_object($id) ? (int) "$id" : (int) $id);
if($id < 1) return false;
$database = $this->wire('database');
$database = $this->wire()->database;
if($database->escapeCol($column) !== $column) throw new WireException("Invalid column name: $column");
$query = $database->prepare("SELECT `$column` FROM pages WHERE id=:id");
$query->bindValue(':id', $id, \PDO::PARAM_INT);
$query->execute();
$value = $query->fetchColumn();
$query->closeCursor();
if(ctype_digit("$value") && strpos($column, 'name') !== 0) $value = (int) $value;
return $value;
}

View File

@@ -156,13 +156,13 @@ class PagesLoaderCache extends Wire {
public function uncacheAll(Page $page = null, array $options = array()) {
if($page) {} // to ignore unused parameter inspection
$user = $this->wire('user');
$language = $this->wire('languages') ? $user->language : null;
$user = $this->wire()->user;
$language = $this->wire()->languages ? $user->language : null;
$cnt = 0;
$this->pages->sortfields(true); // reset
if($this->wire('config')->debug) {
if($this->wire()->config->debug) {
$this->pages->debugLog('uncacheAll', 'pageIdCache=' . count($this->pageIdCache) . ', pageSelectorCache=' .
count($this->pageSelectorCache));
}
@@ -306,8 +306,8 @@ class PagesLoaderCache extends Wire {
}
// cache non-default languages separately
if($this->wire('languages')) {
$language = $this->wire('user')->language;
if($this->wire()->languages) {
$language = $this->wire()->user->language;
if(!$language->isDefault()) {
$selector .= ", _lang=$language->id"; // for caching purposes only, not recognized by PageFinder
}

View File

@@ -816,6 +816,10 @@ class Templates extends WireSaveableItems {
$pageClass = "\\$pageClass";
} else {
// class is not available for instantiation
$this->warning(
"Template '$template' page class '$pageClass' is not available",
Notice::debug | Notice::superuser | Notice::admin
);
$pageClass = '';
// do not cache because maybe class will be available later
$cacheable = false;