Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to keep log file - update SetupPageName #1600

Open
wants to merge 2 commits into
base: devns
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions wire/core/FileLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,11 @@ public function prune($bytes) {
* Prune log file to specified number of bytes (from the end)
*
* @param int $bytes
* @param bool $split
* @return int|bool positive integer on success, 0 if no prune necessary, or boolean false on failure.
*
*/
public function pruneBytes($bytes) {
public function pruneBytes($bytes, $split = false) {

$filename = $this->logFilename;

Expand All @@ -450,7 +451,7 @@ public function pruneBytes($bytes) {
fclose($fpr);

if($cnt) {
unlink($filename);
$split? rename($filename, $this->path.date('ymdhis').'-'.substr(strrchr($filename, "/"), 1)):unlink($filename);
rename("$filename.new", $filename);
$this->wire('files')->chmod($filename);
} else {
Expand Down
169 changes: 108 additions & 61 deletions wire/core/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,73 @@ public function ___setupNew(Page $page) {
}
}


/**
* create a page name based on 'Name Format Children' in settings of parent page template
*
* @param Page $page
* @param string $format use dot syntax if value has subfields or is an object
* @return string/ bool If a name was generated it is returned. false in case of error or warning
*
*/
public function createFromFormat(Page $page, $format) {

$error = false;
$warning = false;
$pageName = '';

// case 1: multiple fields
if (strpos($format,' ')) {
// space not allowed in name so we can use it as separator
$formats = explode(' ',$format);
foreach ($formats as $_format) {
$_pageName = $this->createFromFormat($page, $_format);
if ($_pageName == false) return false;
$pageName .= '-'.$_pageName;
}
}

// case 2: title default
else if ($format == 'title' && strlen($page->title)) $pageName .= '-'.$page->title;

// case 3: date()
else if (strpos($format, 'date(') === 0 ) {
if (preg_match('/(date\()([^()]+)(\))/',$format,$matches)) $date = date($matches[2]);
if (!$date) {
$error = true;
} else $pageName .= '-'.$date;

// case 4: string could be determined with $page->get() we take this
} else if (strlen($page->get($format))) {
$value = $page->get($format);
if (is_int($value) && wire('fields')->get($format)->type instanceof FieldtypeDatetime) {
// for now we convert timestamp in a specific format, later we add this to module settings
$value = date('y-m-d',$value);
}
$pageName .= '-'.$value;
}

// case 5: check if field exists, set flag for warning or error and return false
else {
$_format = strpos($format,'.')?strstr($format,'.',true):$format;
if($page->template->hasField($_format)) {
if (strpos($page->name, $this->untitledPageName) === 0) $warning = true;
} else $error = true;
}

// output warning or error
$format_error = __("Syntax Error: 'Name Format Children' in settings of template '%s' (%s)");
if ($error === true) $this->error(sprintf($format_error, $page->parent->template->name, $format));
$format_warning = __("Pagename '%s' will switch if value of field '%s' is populated!");
if ($warning === true) $this->warning(sprintf($format_warning, $page->name, $format));

if (!$error && !$warning) return ltrim($pageName,'-');
return false;

}

/**
*
* Auto-assign a page name to this page
*
* Typically this would be used only if page had no name or if it had a temporary untitled name.
Expand All @@ -1013,12 +1079,13 @@ public function ___setupNew(Page $page) {
*
* @param Page $page
* @param array $options
* - format: Optionally specify the format to use, or leave blank to auto-determine.
* @return string If a name was generated it is returned. If no name was generated blank is returned.
* - format: Optionally specify the format to use, or leave blank to auto-determine. Use title if set or datetime string
*
* @return string If a name was generated it is returned. If no name was generated blank is returned.
*
*/
public function ___setupPageName(Page $page, array $options = array()) {

$defaults = array(
'format' => '',
);
Expand Down Expand Up @@ -1047,7 +1114,7 @@ public function ___setupPageName(Page $page, array $options = array()) {
$parent = $page->parent();
if($parent && $parent->id) $format = $parent->template->childNameFormat;
}

if(!strlen($format)) {
if(strlen($page->title)) {
// default format is title
Expand All @@ -1060,80 +1127,60 @@ public function ___setupPageName(Page $page, array $options = array()) {

$pageName = '';

if(strlen($format)) {
// @todo add option to auto-gen name from any page property/field

if($format == 'title') {
if(strlen($page->title)) $pageName = $page->title;
else $pageName = $this->untitledPageName;

} else if(!ctype_alnum($format) && !preg_match('/^[-_a-zA-Z0-9]+$/', $format)) {
// it is a date format
$pageName = date($format);
} else {

// predefined format
$pageName = $format;
}
// childNameFormat is set
if (strlen($format)) {
$pageName = $this->createFromFormat($page,$format);
if ($pageName == false) $pageName = $this->untitledPageName;
}

} else if(strlen($page->title)) {
$pageName = $page->title;
else if (strlen($page->title)) $pageName = $page->title;

} else {
else {
// no name will be assigned
$pageName = '';
}

if($pageName == $this->untitledPageName && strpos($page->name, $this->untitledPageName) === 0) {
// page already has untitled name, and there's no need to re-assign the untitled name
return '';
}

$name = '';
if(strlen($pageName)) {
// make the name unique

if (strlen($pageName)) {
$pageName = $this->wire('sanitizer')->pageName($pageName, Sanitizer::translate);
$numChildren = $page->parent->numChildren();
$n = 0;

do {
$name = $pageName;
if($n > 0) {
$nStr = "-" . ($numChildren + $n);
if(strlen($name) + strlen($nStr) > self::nameMaxLength) $name = substr($name, 0, self::nameMaxLength - strlen($nStr));
$name .= $nStr;
}
$n++;
} while($n < 100 && $this->count("parent=$page->parent, name=$name, include=all"));

$page->name = $name;
$page->set('_hasAutogenName', true); // for savePageQuery, provides adjustName behavior for new pages
$page->name = $this->makeUnique($page, $pageName);
$page->set('_hasAutogenName', true); // for savePageQuery, provides adjustName behavior for new pages
return $page->name;
}

return $name;
return '';
}

/**
* Save a page object and it's fields to database.
*
* If the page is new, it will be inserted. If existing, it will be updated.
*
* This is the same as calling $page->save()
*
* If you want to just save a particular field in a Page, use $page->save($fieldName) instead.
*
* create a unique name with -n appendix
*
* @param Page $page
* @param array $options Optional array with the following optional elements:
* 'uncacheAll' => boolean - Whether the memory cache should be cleared (default=true)
* 'resetTrackChanges' => boolean - Whether the page's change tracking should be reset (default=true)
* 'quiet' => boolean - When true, modified date and modified_users_id won't be updated (default=false)
* 'adjustName' => boolean - Adjust page name to ensure it is unique within its parent (default=false)
* 'forceID' => integer - use this ID instead of an auto-assigned on (new page) or current ID (existing page)
* 'ignoreFamily' => boolean - Bypass check of allowed family/parent settings when saving (default=false)
* @return bool True on success, false on failure
* @throws WireException
*
* @param string $pageName
* @return string $pageName-n
*
*/
public function makeUnique(Page $page, $pageName) {

$pageName = $this->wire('sanitizer')->pageName($pageName, Sanitizer::translate);
$numChildren = $page->parent->numChildren();
$n = 0;

do {
$name = $pageName;
if($n > 0) {
$nStr = "-" . ($numChildren + $n);
if(strlen($name) + strlen($nStr) > self::nameMaxLength) $name = substr($name, 0, self::nameMaxLength - strlen($nStr));
$name .= $nStr;
}
$n++;
} while($n < 100 && $this->count("parent=$page->parent, name=$name, include=all"));

return $name;
}

public function ___save(Page $page, $options = array()) {

$defaultOptions = array(
Expand Down