2

I have recently migrated a Magento website which had been using merge_css_files successfully.

On the new server, however, I cannot get it to work and would very much appreciate any advice on the matter. The system will create the CSS files in media/css but they are always blank (unpopulated). I suspect that this might be something to do with the server itself as it worked previously on the old server.

I have already toggled Merge CSS on and off, flushed the caches multiple times, deleted the files from the media/css folder, updated all file and folder permissions, checked the ownership in FTP.

Perhaps you have experienced the same or could advise on a possible solution?

  • I may be way off here, but is there space on the disk? – Marius Sep 4 '13 at 13:59
  • Did you maybe miss the trailing slash at the end of the (new?) base url on the new server? I had once a similar issue... – Anna Völkl Sep 4 '13 at 14:00
  • 4
    This really seems like a permissions thing. Create but not append. – benmarks Sep 4 '13 at 14:28
  • @benmarks - I agree. Also is the media folder a symlink? or a mount to another physical drive? – ProxiBlue Sep 10 '13 at 13:04
  • Thanks for the replies. In response: Marius, plenty of space left. Anna Volkl, base urls trailing slash is already in place. benmarks, all premissions are set correctly. I even ran a test & set absolutely everything to 777, still the issue continued. @ProxiBlue, I'm not sure what a symlink/mounted to another physical drive is. Simply put the cms is installed on a shared/cloud hosting server. Everthing is where you would expect a default installation to be. I even tried installing a brand new and fresh Magento install on the cloud server and it still couldn't create the css files properly. – Daniel Patterson Sep 19 '13 at 9:39
2

Simply put the cms is installed on a shared/cloud hosting server

FWIW, that statement says two different things. You did do a great debugging thing by checking with a vanilla install.

The only thing you can do is debug Mage_Core_Model_Design_Package::_mergeFiles() and/or Mage_Core_Helper_Data::mergeFiles(). I believe the latter of these is where execution is breaking out.

Also, ensure that display_errors and developer mode are enabled (I would just do this in index.php temporarily given the environment issues). Also ensure to check error logs in var/log/. You may want to verify that session & cache files are able to write to the var directory as well.

Edit:

Before you debug, confirm that the site looks as you would expect with merging disabled.

Since you are using FTP you want to keep debug iterations to a minimum. So, you should use Magento's native logging:

Mage::log(__LINE__,null,'merge.log',true);

You'll want to sprinkle this throughout the methods in question. I'd start with the mergeFiles() method from the core helper:

public function mergeFiles(array $srcFiles, $targetFile = false, $mustMerge = false,
    $beforeMergeCallback = null, $extensionsFilter = array())
{
Mage::log(__METHOD__,null,'merge.log',true); //for some context
    try {
        // check whether merger is required
        $shouldMerge = $mustMerge || !$targetFile;
        if (!$shouldMerge) {
Mage::log(__LINE__,null,'merge.log',true);
            if (!file_exists($targetFile)) {
                $shouldMerge = true;
            } else {
                $targetMtime = filemtime($targetFile);
                foreach ($srcFiles as $file) {
                    if (!file_exists($file) || @filemtime($file) > $targetMtime) {
                        $shouldMerge = true;
                        break;
                    }
                }
            }
        }

        // merge contents into the file
        if ($shouldMerge) {
Mage::log(__LINE__,null,'merge.log',true);
            if ($targetFile && !is_writeable(dirname($targetFile))) {
                // no translation intentionally
                throw new Exception(sprintf('Path %s is not writeable.', dirname($targetFile)));
            }

            // filter by extensions
            if ($extensionsFilter) {
                if (!is_array($extensionsFilter)) {
                    $extensionsFilter = array($extensionsFilter);
                }
                if (!empty($srcFiles)){
                    foreach ($srcFiles as $key => $file) {
                        $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                        if (!in_array($fileExt, $extensionsFilter)) {
                            unset($srcFiles[$key]);
                        }
                    }
                }
            }
            if (empty($srcFiles)) {
                // no translation intentionally
                throw new Exception('No files to compile.');
            }

            $data = '';
            foreach ($srcFiles as $file) {
                if (!file_exists($file)) {
                    continue;
                }
                $contents = file_get_contents($file) . "\n";
                if ($beforeMergeCallback && is_callable($beforeMergeCallback)) {
                    $contents = call_user_func($beforeMergeCallback, $file, $contents);
                }
                $data .= $contents;
            }
            if (!$data) {
                // no translation intentionally
                throw new Exception(sprintf("No content found in files:\n%s", implode("\n", $srcFiles)));
            }
            if ($targetFile) {
                file_put_contents($targetFile, $data, LOCK_EX);
            } else {
                return $data; // no need to write to file, just return data
            }
        }

        return true; // no need in merger or merged into file successfully
    } catch (Exception $e) {
        Mage::logException($e);
    }
    return false;
}

If you do not see var/log/merge.log being created, first drop that call in at the very end of index.php; if you still don't see it then there is something amiss with your environment and you will need your host to help. If you do see it but it contains no content from the helper, then you need to step back to the _mergeFiles() call in the design package model. Have fun!

  • Thanks very much for your reply @benmarks. I can ensure you that the var folder is writable. I don't understand how to debug the model and helper you mentioned above. After the vanilla install I knew it was a server issue but I have no idea what to do with it or how to work around it. I will look into debugging and displaying errors ... but again, I don't know how to take that forward either. Thanks again for your input and help! – Daniel Patterson Sep 19 '13 at 13:04
  • Updated with some debugging steps. – benmarks Sep 19 '13 at 13:26
  • I'm afraid I might need a little more guidance. I found and updated the code above in /app/code/core/Mage/Core/Helper/Data.php. I refreshed the caches, toggled the Merge CSS files on (still broken) and then off again. Emptied the caches again and did not see any file called merge.log in 'var/log'. Am I going about this right? – Daniel Patterson Sep 19 '13 at 15:15
  • Add that logging call to the absolute end of index.php and see if the file gets written. If it doesn't then you have an environment issue. – benmarks Sep 19 '13 at 15:53
  • Thanks @benmarks. I added Mage::log(LINE,null,'merge.log',true); to index.php and it created merge.log in var/log. This is what the contents of that file say: 2013-09-19T15:55:26+00:00 DEBUG (7): 88 – Daniel Patterson Sep 19 '13 at 15:56
2

I have just done some research hope this helps:

RESOURCE 1

Magento Merged CSS Clear Not Working + Solution

If you are having an issue with Magento and the optional Merge CSS Files (beta) turned on and it not updating with recent CSS changes, you may appreciate this.

Unlike other cached elements that are stored in var/cache, Magento stores the cached and minified CSS files in media/css

If you simply delete the contents of this folder, your CSS changes should kick in as Magento is forced to rebuild these files.

There is an option in cache management to flush CSS storage but for some reason this wasn’t working for me. Could be a permissions issue or something else I am not sure, we simply needed to get this change implemented (emergency live site fix) so flushing this folder manually did the trick.

Hope it helps someone else looking for Magento merged CSS clear not working, Magento CSS cache, Magento CSS Files beta

[Source of content]

RESOURCE 2

To make changes in core configurations and have them stick (local.xml, base urls, etc.) you need to clear your Magento code cache, often by actually manually deleting the mage--? subfolders in var/cache.

Cached configuration can cause all sorts of headaches including having the base urls still pointing back to the original Magento installation (different folders, different server) despite the fact that you can use phpMyAdmin and see that the rows have been updated in the database. Configuration will not be reread from the database until the cached configuration information is deleted.

Another place where stuff gets stuck is in your APC op-code cache if you’re using it. Use the apc.php administration page to clear that as well. Stopping and restarting Apache will achieve the same result

And last of all, IF YOU DO NOT HAVE PERMISSIONS SET PROPERLY ON var/cache AND var/tmp SO THEY’RE BOTH READABLE AND WRITABLE TO YOUR WEB SERVER, Magento may store the cached information in the system /tmp directory which means that no amount of clearing Magento’s cache will make the cached configs go away until someone reboots the server, the system /tmp directory gets cleared and Magento seemingly automagically reads its configs. Or you're told about it and you have access to delete the Magento cache out of /tmp. Here's some documentation of this happening in the system temporary storage folder off of root (see images)

Note: If you save a backup copy of your local.xml file, make sure to rename it to something like local.xml.sv1 or you will find that Magento still reads it.

[Source of content]

I hope either of these methods help, if you have transfered domains, im guessing its number 2 more than anything however if you are shared that may be a problem as you wont have full server access, I hope either can help.

And as you have said you have cleared VAR/ and MEDIA/ You will be right with the server config and i would look at resource 2.

  • Thanks @Adam, I'm sure that in the majority of cases that either of the above would work. It's also worth noting that you should try to manually clear everything in the Media/CSS folder too but none of these scenarios works for me as I think it might be a server issue. – Daniel Patterson Sep 20 '13 at 15:50
  • do you have access to the server? because it could be cached in /tmp as noted in resource two, and if you have a shared hosting you will NOT have access to this folder, in which you need to request to your hosts to check that folder and clear anything magento related within that folder. – Adam Kernig Sep 20 '13 at 15:56
  • Thanks @Adam, it is shared. I'll ask the host to clear the tmp folder and see what happens. – Daniel Patterson Sep 24 '13 at 8:04
  • Clearing the server /tmp folder did not change anything. – Daniel Patterson Sep 24 '13 at 10:30
1

Did your template break after merging? If not, then I'd refresh the FTP window. For example, Transit from Panic caches the results when you change folders, so when you change back after adding/deleting files, the folder still shows the old contents. But if the template breaks after merging, this won't be the issue. Also check "Current Configuration Scope:" that you're in the right store and that all stores are checked to "Use Website".

1

It is realted to the fact that your media folder is referenced by a symlink and/or any distributed file system. (generally media/ folder is shared between multipe servers)

The problem resides in the function below because of the LOCK_EX: in app/code/core/Mage/Core/Helper/Data.php : Line :753

if (!$data) {
    // no translation intentionally
    throw new Exception(sprintf("No content found in files:\n%s", implode("\n", $srcFiles)));
   }
   if ($targetFile) {
      file_put_contents($targetFile, $data, LOCK_EX); // THE PROBLEM IS JUST HERE
   } else {
       return $data; // no need to write to file, just return data
   }

The problem is with the use of file_put_contents($targetFile, $data, LOCK_EX)

changing it with use of flock should solve the problem :

    $fp = fopen($targetFile, "w+");
    if (flock($fp, LOCK_EX | LOCK_NB)) {
        //Mage::log("INFO : Acquire Exclusive lock on " . $targetFile, null, 'merge_js_css_debug.log');
        ftruncate($fp, 0); // Erase Content
        fwrite($fp, $data); // Write data
        fflush($fp); // Free content befor unlock
        flock($fp, LOCK_UN); // Unleach lock
        //Mage::log("INFO : Write complete on " . $targetFile);
    } else {
        Mage::log("ERROR : Impossible to lock File " . $targetFile . "<br>");
    }
    fclose($fp);

So As final solution you can rewrite the subject core file (app/code/core/Mage/Core/Helper/Data.php) like below :

    if (!$data) {
    // no translation intentionally
    throw new Exception(sprintf("No content found in files:\n%s", implode("\n", $srcFiles)));
   }
   if ($targetFile) {
    $fp = fopen($targetFile, "w+");
    if (flock($fp, LOCK_EX | LOCK_NB)) {
        //Mage::log("INFO : Acquire Exclusive lock on " . $targetFile, null, 'merge_js_css_debug.log');
        ftruncate($fp, 0); // Erase Content
        fwrite($fp, $data); // Write data
        fflush($fp); // Free content befor unlock
        flock($fp, LOCK_UN); // Unleach lock
        //Mage::log("INFO : Write complete on " . $targetFile);
    } else {
        Mage::log("ERROR : Impossible to lock File " . $targetFile . "<br>");
    }
    fclose($fp);
   } else {
       return $data; // no need to write to file, just return data
   }
1

For me, Claudflare was a problem.

Go to cPanel or Claudflare dashboard and just purge cache. Then the change will be magically applied. I spent hours to figure this out.

It seems like not many people using Magento had the same problem as me. I found this solution on Drupal forum.

I hope this might help somebody.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.