PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/modx/modparser095.class.php

http://github.com/modxcms/revolution
PHP | 112 lines | 102 code | 1 blank | 9 comment | 1 complexity | 6976657118d717fb02dd797bd02f24d4 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /* Include the base modParser class */
  11. include_once (strtr(realpath(dirname(__FILE__)) . '/modparser.class.php', '\\', '/'));
  12. /**
  13. * An extension of the MODX parser to support legacy MODX tags.
  14. *
  15. * Use of this class is only necessary if you have a site that contains legacy
  16. * MODX tags. It provides facilities for translating the legacy tags, as well
  17. * as for supporting legacy behavior of the onParseDocument event used in many
  18. * legacy MODX plugins.
  19. *
  20. * @package modx
  21. * @deprecated
  22. */
  23. class modParser095 extends modParser {
  24. function __construct(xPDO &$modx) {
  25. parent::__construct($modx);
  26. $this->modx->deprecated('2.7.0', 'Switch to the standard modParser. If you\'re still actively using 0.9.5-style templates, use the modTranslator utility for a one-time conversion.', 'modParser095');
  27. }
  28. /**
  29. * An array of translation strings from migrating from Evolution
  30. * @var array $tagTranslation
  31. */
  32. public $tagTranslation= array (
  33. '[[++' => array ('[(', ')]', '++'),
  34. '[[$' => array ('{{', '}}', '$'),
  35. '[[*' => array ('[*', '*]', '*'),
  36. '[[~' => array ('[~', '~]', '~'),
  37. '[[+' => array ('[+', '+]', '+'),
  38. '[[!' => array ('[!', '!]', '!'),
  39. );
  40. /**
  41. * Collects MODX legacy tags and translates them to the new tag format.
  42. *
  43. * @param string &$content The content in which legacy tags are to be
  44. * replaced.
  45. * @param array $tokens An optional array of tag tokens on which to exclude
  46. * translation of the tags.
  47. * @param boolean $echo
  48. * @return void The content is operated on by reference.
  49. */
  50. public function translate(& $content, $tokens= array (), $echo= false) {
  51. $newSuffix= ']]';
  52. $matchCount= 0;
  53. foreach ($this->tagTranslation as $newPrefix => $legacyTags) {
  54. $tagMap= array ();
  55. $oldPrefix= $legacyTags[0];
  56. $oldSuffix= $legacyTags[1];
  57. $token= $legacyTags[2];
  58. if (!empty ($tokens) && is_array($tokens)) {
  59. if (in_array($token, $tokens)) {
  60. continue;
  61. }
  62. }
  63. $tags= array ();
  64. if ($matches= $this->collectElementTags($content, $tags, $oldPrefix, $oldSuffix)) {
  65. // $this->modx->cacheManager->writeFile(MODX_BASE_PATH . 'parser095.translate.log', 'Translating ' . $oldPrefix . $oldSuffix . ' to ' . $newPrefix . ']] ('.$matches.' matches): ' . print_r($tags, 1) . "\n", 'a');
  66. foreach ($tags as $tag) {
  67. $tagMap[$tag[0]]= $newPrefix . $tag[1] . $newSuffix;
  68. }
  69. }
  70. if (!empty ($tagMap)) {
  71. $matchCount+= count($tagMap);
  72. $content= str_replace(array_keys($tagMap), array_values($tagMap), $content);
  73. if ($echo) {
  74. echo "[TRANSLATED TAGS] " . print_r($tagMap, 1) . "\n";
  75. }
  76. }
  77. }
  78. return $matchCount;
  79. }
  80. /**
  81. * Adds the legacy tag translation and legacy OnParseDocument event support.
  82. * @param string $parentTag
  83. * @param $content
  84. * @param bool $processUncacheable
  85. * @param bool $removeUnprocessed
  86. * @param string $prefix
  87. * @param string $suffix
  88. * @param array $tokens
  89. * @param bool $echo
  90. * @return string
  91. *
  92. */
  93. public function processElementTags($parentTag, & $content, $processUncacheable= false, $removeUnprocessed= false, $prefix= "[[", $suffix= "]]", $tokens= array (), $echo= false) {
  94. // invoke OnParseDocument event
  95. $this->modx->documentOutput = $content; // store source code so plugins can
  96. $this->modx->invokeEvent('OnParseDocument'); // work on it via $modx->documentOutput
  97. $content = $this->modx->documentOutput;
  98. $ignoretokens= array ();
  99. // if (!$processUncacheable) {
  100. // $ignoretokens[]= '+';
  101. // $ignoretokens[]= '++';
  102. // }
  103. // if (!$processUncacheable || ($processUncacheable && !$removeUnprocessed)) {
  104. // $ignoretokens[]= '!';
  105. // }
  106. while ($this->translate($content, $ignoretokens, $echo)) {}
  107. return parent :: processElementTags($parentTag, $content, $processUncacheable, $removeUnprocessed, $prefix, $suffix, $tokens);
  108. }
  109. }