We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Quote from: exside at Dec 11, 2012, 04:07 AM
    Have a look at that: https://github.com/modxcms/revolution/pull/428

    I use it in one of my installations (though it's a core modification which should be incorporated in my eyes) and it works...maybe that would also solve your problem?
    This is a partial solution. It does not account for the fact that the caching configuration could be different per Context. And with some additional logic, this could be the solution for 2.3.x.

    For support in 2.2.x, you can create a service class or function you can reuse in your Plugins that approximate this logic.


      • 40045
      • 534 Posts
      That sounds great, would love to see that (even extended...) in 2.3.x!

      Quote from: opengeek at Dec 10, 2012, 06:55 PM
      No, because Resources can be forwarded to across Contexts, or (a rarely used feature) even exist in multiple Contexts.

      regarding to the statement "even exist in multiple contexts", how is that ment ie. how can I have resource (the same one??) in multiple contexts? (sry for the maybe stupid question =D)
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: exside at Dec 11, 2012, 07:28 AM
        That sounds great, would love to see that (even extended...) in 2.3.x!

        Quote from: opengeek at Dec 10, 2012, 06:55 PM
        No, because Resources can be forwarded to across Contexts, or (a rarely used feature) even exist in multiple Contexts.

        regarding to the statement "even exist in multiple contexts", how is that ment ie. how can I have resource (the same one??) in multiple contexts? (sry for the maybe stupid question =D)
        It's not a stupid question; it's actually a hidden feature.

        There is a class/table in MODX called modContextResource that can define additional Contexts for a Resource beyond the context_key. It is used when building the Context cache data, there is just no current way to manage (or visualize) this data in the manager UX. You can use it though, and it does work; one of the original sites we developed while developing Revolution needed this functionality, and we provided the ability with a checkbox TV that used a Snippet to list the Contexts the Resource should be saved in by a simple Plugin.
          • 40045
          • 534 Posts
          ok, that's very powerful =))...have to wrap my head around this concept and think about in what scenarios this could be useful (thought about translation/internationalisation, but that wont work because content / language of content changes...or you had to specify the content via lexicons?? then it could work for all languages with one resource??? managing this would be another topic...=D).

          Maybe (if possible) you can shed a light on what use case that specific site used this feature for? (just as example when to use this).

          And yes, I also don't see a way to nicely visualise this in the manager ux, maybe just a normal resource with the same id and an identifier? Something like

          web
              resource name (1) // -> original resource with id = 1
              resource name (2)
          context 2
              resource name (1*) // -> reference to resource with id = 1 and * as identifier for a "cloned" (not actually...just didn't find a better word =D) resource
              resource name (3)
          
            • 22303 MODX Staff
            • 10,725 Posts
            The original use case was publishing a single news article to multiple subdomain sites, though no translations were involved. I think to really become a useful feature, it needs some additional metadata, e.g. it would be useful to define a different parent per Context. Using Symlinks across Contexts solved this for now, but that is a slightly different approach that still involves multiple Resources to exist.
              • 3749
              • 24,544 Posts
              @OpenGeek: Rather than hack the core code, I got this to work -- comments welcome:

              $ck = $resource->getCacheKey();
                  $mgrCtx = $modx->context->get('key');
                  $cKey = str_replace($mgrCtx, $ctx, $ck);
                  // my_debug('CK: ' . $ck);
                  $modx->cacheManager->delete($cKey, array(
                      xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                      xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                          $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                      xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                          $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                      )
                  );
                  /* see if resource exists in any other contexts */
                  $ctxResources = $modx->getCollection('modContextResource', array('resource' => $docId));
                  
                  if (!empty ($ctxResources)) {
                      foreach ($ctxResources as $ctxResource) {
                          /* @var $ctxResource modContextResource */
                          $key = $ctxResource->get('context_key');
                          $cKey = str_replace($mgrCtx, $key, $ck);
                          $modx->cacheManager->delete($cKey, array(
                              xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                              xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                                  $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                              xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                                  $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                             )
                          );
                          
                      }
                  }
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 40045
                • 534 Posts
                @Bob: Also a bit hacky to use str_replace =D, but for sure better than hacking the core (wasn't my intention, just wanted to bring that pull request into view, think something in this direction could easily make it into the core).

                to the code: where is $ctx coming from if it's a plugin?
                  • 16430
                  • 217 Posts
                  If I simplify a bit this code to this (just one context):
                  $ck = $modx->resource->getCacheKey();      // web/resources/xxx
                  
                      $modx->cacheManager->delete($ck, array(
                          xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                          xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                              $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                          xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                              $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                          )
                      );


                  It does nothing, doesnt clear cache
                  Why dont we just delete those bloody files using this?:
                  $ck = $modx->resource->getCacheKey();  
                  $path = MODX_CORE_PATH . 'cache/resource/' . $ck . '.cache.php';    
                      if ($path && file_exists($path)) {
                      unlink($path);
                  }
                  
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: kudykam at May 29, 2013, 08:49 AM
                    If I simplify a bit this code to this (just one context):
                    $ck = $modx->resource->getCacheKey();      // web/resources/xxx
                    
                        $modx->cacheManager->delete($ck, array(
                            xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
                            xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null,
                                $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
                            xPDO::OPT_CACHE_FORMAT => (integer)$modx->getOption('cache_resource_format', null,
                                $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP))
                            )
                        );


                    It does nothing, doesnt clear cache
                    I just tested this code and the individual Resource cache files were deleted as expected. I also tested using other cache handlers (APC + memcache) and these worked as well.

                    Quote from: kudykam at May 29, 2013, 08:49 AM

                    Why dont we just delete those bloody files using this?:
                    $ck = $modx->resource->getCacheKey();  
                    $path = MODX_CORE_PATH . 'cache/resource/' . $ck . '.cache.php';    
                        if ($path && file_exists($path)) {
                        unlink($path);
                    }
                    
                    We don't do that because that will only work when the cache_handler is configured as xPDOFileCache. Change it to another class, and attempting to remove cache files like that will not work.
                      • 16430
                      • 217 Posts
                      Well it didnt work because a wrong path, it has to be:
                      $ck = MODX_CORE_PATH . "cache/resource/" . $modx->resource->getCacheKey();