diff options
author | Thomas Lange <code@nerdmind.de> | 2024-02-05 20:08:08 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2024-02-05 20:08:08 +0100 |
commit | 00bf20ed68fb8b833967c207bb3689dc96098073 (patch) | |
tree | 1dae0756bbeeefd315a52281de553869972d68e8 /core/script | |
parent | 8b1314e416ada633ae6c866c9fac7124e6d3a3e4 (diff) | |
download | blog-00bf20ed68fb8b833967c207bb3689dc96098073.tar.gz blog-00bf20ed68fb8b833967c207bb3689dc96098073.tar.xz blog-00bf20ed68fb8b833967c207bb3689dc96098073.zip |
Add content tag converter script
This script is used to convert the old *content tag* syntax to the newer
*content functions* syntax which has been introduced in July 2021.
See the wiki page for *content tags* for more details.
Diffstat (limited to 'core/script')
-rw-r--r-- | core/script/convert_content_tags.php | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/core/script/convert_content_tags.php b/core/script/convert_content_tags.php new file mode 100644 index 0000000..7b879a3 --- /dev/null +++ b/core/script/convert_content_tags.php @@ -0,0 +1,142 @@ +<?php +use ORM\EntityInterface; +require '../application.php'; + +#=========================================================================== +# Convert old *content tags* to new *content functions* syntax +#=========================================================================== +function convertContentTags(string $text, &$globalMatches): string { + $url_tags_pattern = '/\{(?<tag>BASE|FILE)\[\"(?<arg>[^"]+)\"\]\}/'; + $entity_tags_pattern_partial = '\{(?<tag>POST|PAGE|USER)\[(?<arg>[0-9]+)\]\}'; + $entity_tags_mdlinks_pattern = '/ + \[(?<text>[^\]]+)\] + \( + \s*'.$entity_tags_pattern_partial.'\s* + (?:\s + (?<qmark>["\']) + (?<title>(?:(?:(?!\k<qmark>)).)*) + \k<qmark>\s* + )? + \) + /x'; + + // Convert (BASE|FILE) tags + $text = preg_replace_callback($url_tags_pattern, + function($matches) use(&$globalMatches) { + $formatted = sprintf('{%s_URL: "%s"}', + $matches['tag'], $matches['arg']); + + $globalMatches[] = [ + $matches[0], $formatted]; + + return $formatted; + }, $text); + + // Convert (POST|PAGE|USER) tags found inside Markdown syntax + $text = preg_replace_callback($entity_tags_mdlinks_pattern, + function($matches) use(&$globalMatches) { + $format = '{%s: %d, "%s"}'; + $params = [ + $matches['tag'], + $matches['arg'], + str_replace('"', '\\"', $matches['text']) + ]; + + if($linkTitle = $matches['title'] ?? FALSE) { + $q = $matches['qmark']; + $format = '{%s: %d, "%s", '.$q.'%s'.$q.'}'; + $params[] = $linkTitle; + } + + $formatted = sprintf($format, ...$params); + + $globalMatches[] = [ + $matches[0], $formatted]; + + return $formatted; + }, $text); + + // Convert (POST|PAGE|USER) tags found anywhere else + $text = preg_replace_callback(sprintf('/%s/x', $entity_tags_pattern_partial), + function($matches) use(&$globalMatches) { + $formatted = sprintf('{%s_URL: %s}', + $matches['tag'], $matches['arg']); + + $globalMatches[] = [ + $matches[0], $formatted]; + + return $formatted; + }, $text); + + return $text; +} + +#=========================================================================== +# Print matches for a specific entity +#=========================================================================== +function printMatches(EntityInterface $Entity, array &$matches): void { + printf("%s\n", str_repeat('-', 100)); + printf("%s (#%d):\n", get_class($Entity), $Entity->getID()); + printf("»%s«\n", $Entity->get('name') ?? $Entity->get('fullname')); + printf("%s\n", str_repeat('-', 100)); + + foreach($matches as $match) { + printf("<-- %s\n", $match[0]); + printf("--> %s\n\n", $match[1]); + } +} + +#=========================================================================== +# Send Cache-Control header and change Content-Type to plain text +#=========================================================================== +HTTP::responseHeader('Cache-Control', 'no-cache'); +HTTP::responseHeader(HTTP::HEADER_CONTENT_TYPE, HTTP::CONTENT_TYPE_TEXT); + +#=========================================================================== +# Set "commit" variable based on GET parameter or CLI argument +#=========================================================================== +$commit = FALSE; +if(isset($_GET['commit']) OR + (isset($argv[1]) AND $argv[1] === 'commit')) { + $commit = TRUE; +} + +#=========================================================================== +# Print header information +#=========================================================================== +if(!$commit) { + printf("%s\n", str_repeat('%', 100)); + print("!!! DRY-RUN !!!\n"); + print("To commit the changes, pass the CLI argument 'commit'!\n"); + printf("%s\n\n", str_repeat('%', 100)); +} + +foreach(['Category', 'Page', 'Post', 'User'] as $entityName) { + $Repository = Application::getRepository($entityName); + + foreach($Repository->getAll() as $Entity) { + unset($matches); + + $content = $Entity->get('body'); + $content = convertContentTags($content, $matches); + + if($matches) { + $foundMatches = TRUE; + + if($commit) { + $Entity->set('body', $content); + $Repository->update($Entity); + } else { + printMatches($Entity, $matches); + } + } + } +} + +if(!isset($foundMatches)) { + print("Found no matches for old content tags in your entities' content. Nothing to do!"); +} else { + $commit && print("Replace operation complete!"); +} + +print("\n"); |