Jump to content
David Karich

Module: AIOM+ (All In One Minify) for CSS, LESS, JS and HTML

Recommended Posts

Calling AIOM/chmod experts...

Just today a whole website of mine using AIOM ceased to function.

Precicely 1 month after installing AIOM on the site, I was getting a 500 error. Looking at the PW log file I saw countless repetitions of...

2014-10-18 15:59:18	guest	http://***.com/?/	Error: 	Exception: The permissions (chmod) to delete old cache files could not be changed. (in /srv/***/www/***.com/public_html/site/modules/AllInOneMinify/AllInOneMinify.module line 646)

So, I guess one or more of my cached files had hit a 30 day expiry date.

I did a bit of investigating, and it seems that before deleting cache files, AIOM like to chmod 777 the file. If it can't chmod the file, it throws this error and stops the site loading. No logging into PW admin, nothing.

It seems similar to this problem here:

https://github.com/ryancramerdesign/ProcessWire/issues/648

And the exact line of code in question is highlighted by Ryan there. https://github.com/FlipZoomMedia/ProcessWire-AIOM-All-In-One-Minify/blob/AIOM%2B/AllInOneMinify.module#L641... It's line 646.

Is there any real need to change the permissions to 777 before deleting?

The problem in my case is that www-data doesn't own these files. I copied this installation over from another server, and the owner (chown) is actuall "admin". But I have group rw permission to www-data, so the files can be deleted. I think that should be okay, right?

So, what I did was comment line 646 and added the following three lines

        // ------------------------------------------------------------------------
        // Remove all files that are older than the maximum lifetime.
        // ------------------------------------------------------------------------
        foreach ($_cacheFiles as $_cacheFile) {
            if (((filemtime(wire('config')->paths->assets.self::$cacheDir.DIRECTORY_SEPARATOR.$_cacheFile->getFilename()) + self::$cacheMaxLifetime) < time() OR $force_all === true) AND is_file($_cacheFile)) {
                $_file  = wire('config')->paths->assets.self::$cacheDir.DIRECTORY_SEPARATOR.$_cacheFile->getFilename();
                if(chmod($_file, 0777) !== false) {
                    if(unlink($_file) === false) {
                        throw new WireException('The old cache files could not be deleted.');
                    }
                } else {
                    // throw new WireException('The permissions (chmod) to delete old cache files could not be changed.');
                    if(unlink($_file) === false) {
                        throw new WireException('The old cache files could not be deleted.');
                    }                    
                }
            }
        }

And now everything seems to work again.

Stuff like this sort of scares me. The last thing I want is for sites to completely and arbitrarily stop working!

I literally just had this error, and had to apply the fix mentioned. This was never fixed?

Share this post


Link to post
Share on other sites

I literally just had this error, and had to apply the fix mentioned. This was never fixed?

Please update AIOM to the latest version 3.2.3. This problem has been fixed with the chmod and uses ProcessWire-own functions for it. This should not happen anymore.

  • Like 1

Share this post


Link to post
Share on other sites

Hello,

I'm using an HTTP/2 server so the recommended way is to have separate CSS/JS without merging them but when I try for example to minimize 5 files, at least 1 file is getting duplicated. This is issue doesn't happen when I merge them.

Can you please fix it?

Thanks

Share this post


Link to post
Share on other sites

Hello,

I'm using an HTTP/2 server so the recommended way is to have separate CSS/JS without merging them but when I try for example to minimize 5 files, at least 1 file is getting duplicated. This is issue doesn't happen when I merge them.

Can you please fix it?

Thanks

Unfortunately I have no server with this configuration available to reproduce the problem. Are you sure that you do not have modules like Google Page Speed / SPDY activated? These do the minimization and compression automatically.

Share this post


Link to post
Share on other sites

EDIT: Yay, everything is fine ... turns out, as you can see I forgot the ; at the end of the lines ... stylus habit;

Hi,

I was just trying getting some .less files compiled but something doesn't work with @import.

I have an index.less file which shall import several sub-stylessheets - and that index.less is the only one given to the ::CSS() function:

@import "variables.less"
@import "layout/index.less"
@import "boxes/index.less"

but that just gives me:

#_____LESS_____ERROR_____REPORT_____ {content:"ParseError: Unexpected input in anonymous-file-0.less on line 7, column 1
05| @sth: #ffffff;
06| 
07| @import "/dev/site/templates/styles.less/variables"  // global variables
08| @import "/dev/site/templates/styles.less/layout/index.less"
09| @import "/dev/site/templates/styles.less/boxes/index.less"
10| "}

Am I doing something wrong or is this feature missing in AIOM?

Share this post


Link to post
Share on other sites

I would like to implement your module in my template engine. Unfortunately it doesn't allow paths outside of the template folder. Would be a big enhancement to accept and find the following. I couldn't get it work.

$styles = array();
// relative to the root paths like
$styles[] = '/site/modules/whatever/style.css';
$styles[] = '/wire/templates-admin/styles/font-awesome/css/font-awesome.min.css'; // I use this very often

// absolute paths
$styles[] = 'http://example.org/stylesheet.css';

$urls = AIOM::CSS($styles);
$output = "<link rel=\"stylesheet\" href=\"$urls\">\n";

Herewith I put this on the wishlist! :)  Maybe somebody else would like this too ???

The Option 'Allow directory traversal' doesn't really exist. It expects each stylesheet path relative to the templates folder if I understand the following codelines correctly.

        // ------------------------------------------------------------------------
        // Filter Directory Traversal (default: yes)
        // ------------------------------------------------------------------------
        $_path  = (self::$directoryTraversal !== true) ? str_ireplace(array('../', './', '%2e%2e%2f', '..%2F'), '', wire('config')->paths->templates.$_file) : wire('config')->paths->templates.$_file;

Maybe i missed something and you could help. Anyway a great module you made. Thanks for this.
 

Edited by kixe

Share this post


Link to post
Share on other sites

Just tried it. Just enable directory traversal in AIOM's module config and then use this path

$styles[] = '../../wire/templates-admin/styles/font-awesome/css/font-awesome.min.css';
  • Like 2

Share this post


Link to post
Share on other sites

I forked the module on github which allows the following paths for example:
styles/style.css (relative to template - default)
/site/folder/style.css (relative to root - NEW)
https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js (absolute path to external source via http request -NEW)

https://github.com/kixe/ProcessWire-AIOM-All-In-One-Minify

 

  • Like 3

Share this post


Link to post
Share on other sites

Sounds good thanks for sharing :)

Share this post


Link to post
Share on other sites

I forked the module on github which allows the following paths for example:

May I ask why it is a good idea to fork it, instead of including these features in the original module? Am I missing something perhaps? I'm not a GitHub guru, so I ask: doesn't it mean that these are two completely separate projects from now on?

Share this post


Link to post
Share on other sites

May I ask why it is a good idea to fork it, instead of including these features in the original module? Am I missing something perhaps? I'm not a GitHub guru, so I ask: doesn't it mean that these are two completely separate projects from now on?

I would say because kixe is not the author so he can't just implement those features in the official release, but if needed before the author can implement or pull a request he can just fork to make his own version  ;)

  • Like 3

Share this post


Link to post
Share on other sites

Unfortunately I have no server with this configuration available to reproduce the problem. Are you sure that you do not have modules like Google Page Speed / SPDY activated? These do the minimization and compression automatically.

It has nothing to do with the HTTP2 server. Just some css / js files are getting the same filename.

Share this post


Link to post
Share on other sites

Unfortunately I have no server with this configuration available to reproduce the problem. Are you sure that you do not have modules like Google Page Speed / SPDY activated? These do the minimization and compression automatically.

Should I replace the _getCacheName function with the one from here?

Share this post


Link to post
Share on other sites

May I ask why it is a good idea to fork it, instead of including these features in the original module? Am I missing something perhaps? I'm not a GitHub guru, so I ask: doesn't it mean that these are two completely separate projects from now on?

From the PW modules directory you will get always the original version. If the Author like this feature, he has the freedom to include it. I had to make these changes, to get it working properly with my template function library and I want to share this enhancement. Use it if you like it. :)

  • Like 1

Share this post


Link to post
Share on other sites

May I ask why it is a good idea to fork it, instead of including these features in the original module?

To sum up: this is the standard (github) way of contributing to other's repo.

Share this post


Link to post
Share on other sites

From the PW modules directory you will get always the original version. If the Author like this feature, he has the freedom to include it. I had to make these changes, to get it working properly with my template function library and I want to share this enhancement. Use it if you like it. :)

Thank you for the answer! Is it also standard not to reflect this in README.md and just copy it over? I mean, how can one tell apart the various forked versions? Just by looking at the "main pages" of the projects, they seem identical to me. Sorry if it happens to be an off-topic GitHub question.

  • Like 1

Share this post


Link to post
Share on other sites

That would be a good practice, but maybe even better having a separate CHANGELOG.md file to log changes. You can also check commit messages to see what has changed, provided that the author included one.

  • Like 2

Share this post


Link to post
Share on other sites

Thank you for the answer! Is it also standard not to reflect this in README.md and just copy it over? I mean, how can one tell apart the various forked versions? Just by looking at the "main pages" of the projects, they seem identical to me. Sorry if it happens to be an off-topic GitHub question.

Check the name of the repository at GitHub. In this case there's this line right below it:

That's a pretty good indication that this particular repository might be a fork :)

In the GitHub workflow if you fork another repository you can send a pull request containing all your changes to the original repository / author. If you've changed the README in the fork, those changes will also be included in the PR. That's one reason not to include indication about this being a fork or anything like that.

Of course you can get over this limitation by creating a separate branch for the changes intended for the PR, but that's extra work and most developers are just plain lazy :)

  • Like 4

Share this post


Link to post
Share on other sites

Thanks for educating me, teppo! I have never used GitHub (apart from downloading ZIP files and simple browsing) nor noticed the "forked from" line. Sooner or later I need to find the time to familiarize myself with GitHub, I suppose. And again, sorry for the off-topic question.

  • Like 3

Share this post


Link to post
Share on other sites

@kixe: Thanks! This can be quite useful sometimes. I have never noticed this list before.

Share this post


Link to post
Share on other sites

A feature request: Sass support (specifically SCSS). Thanks! :)

  • Like 3

Share this post


Link to post
Share on other sites

How about adding media query grouping like http://stackoverflow.com/a/18191985/3484824?

I've test implemented it at line 621 or maybe more accurately right before the "Write the minimized file to the file system" comment

// search media query in CSS
preg_match_all('#@media(.*?)\{(.+?}[ \n])\}#si',$_css,$match,PREG_SET_ORDER);//$MatchingString now hold all strings matching $pattern.

$media = array();

// group same media query
foreach ($match as $val)
{
    if (!isset($media[$val[1]])) $media[$val[1]] = '';

    $media[$val[1]] .= $val[2];
}

// delete media query of cache
$_css = preg_replace('#@media(.*?)\{(.+?}[ \n])\}#si', '', $_css);

// add groups of media query at the end of CSS
$final = $_css . "\n";
foreach ($media as $id => $val)
{
    $final .= "\n" . '@media' . $id . '{' . $val . '}' . "\n";
}
$_css = $final;

// ------------------------------------------------------------------------
// Write the minimized file to the file system.
// ------------------------------------------------------------------------

One optimization could be sort ordering of grouped media queries like min-width40em before min-width50em and so on..

OR

# Recursively descend and match all `{ .. }` blocks.
preg_match_all('#(?<media>(?:@media[^{]+)?){(??:[^{}]+)|(?R))*}#s', $_css, $captures);
# Loop through all matches by key.
foreach (array_keys($captures[0]) as $key) {
  # If the value does not contain a @media query, drop it from the array. Or
  # even if we found a @media query, make sure it was not within a string or
  # a comment.
  if (strpos($captures[0][$key], '@media') === FALSE || empty ($captures['media'][$key])) {
    unset ($captures[0][$key]);
  }
}
# Erase all @media queries from the input CSS.
foreach ($captures[0] as $query) {
  $_css = preg_replace('#\s*' . preg_quote($query, '#') . '#u', '', $_css);
}
# Utility function to create an array key from a media query.
function sanitize_media_query_for_key($query) {
  $key = trim($query);
  $key = preg_replace('#[^\w\d]+#u', '-', $key);
  $key = trim($key, '-');
  return $key;
}
# For each @media query, group CSS code together.
$groups = array();
foreach (array_keys($captures[0]) as $key) {
  $media = $captures['media'][$key];
  # The code for this block starts off with the @media query in it...
  $code  = $captures[0][$key];
  # ...which then gets stripped off...
  $code  = preg_replace('#' . preg_quote($media, '#') . '#u', '', $code);
  # ...and any leading or trailing curly bracket also gets removed.
  $code  = preg_replace('#^{|}$#u', '', $code);
  # If this is the first time the @media query is encountered, a new group is
  # created.
  $group_key = sanitize_media_query_for_key($media);
  array_key_exists($group_key, $groups) OR ($groups[$group_key] = array($media, '{'));
  $groups[$group_key][] = $code;
}
$result = NULL;
# Output is the input stripped of any @media queries...
$result = $result . rtrim($_css);
# ...followed by those @media queries grouped together.
foreach (array_keys($groups) as $key) {
  $result = $result . PHP_EOL . PHP_EOL . implode(NULL, $groups[$key]) . '}';
}
$_css = $result;

from https://gist.github.com/StanAngeloff/3164569

but still sorting the result could be interesting(?) Couldn't figure it out myself so far..but it's late..

Edited by Can

Share this post


Link to post
Share on other sites

i've just created a pull request that makes it possible to modify less variables. it also adds a check if the js/css file exists and throws a note either in console if tracy is installed or in the processwire log.

that's how you can modify your less variables:

<?php
$config->styles->append('less/theme.less');
$lessVars = array(
    'tm-primary-bg' => '#568AEA',
    'background-image' => 'url("' . $page->backgroundimage->url . '")',
);
?>
<link rel="stylesheet" href="<?php echo AIOM::CSS($config->styles->unique(), $lessVars); ?>">

https://github.com/FlipZoomMedia/ProcessWire-AIOM-All-In-One-Minify/pull/57

  • Like 3

Share this post


Link to post
Share on other sites

I've installed the module and it works fine.

But:

When I set Development Mode to "yes" the files get combined. Is there a way to disable this? I cannot track the root css files this way...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MoritzLost
      This is a new module that provides a simple solution to clearing all your cache layers at once, and an extensible interface to perform various cache-related actions.
      The simple motivation behind this module was that I was tired of manually clearing caches in several places after deploying a change on a live site. The basic purpose of this module is a simple Clear all caches link in the Setup menu which clears out all caches, no matter where they hide. You can customize what exactly the module does through it's configuration menu:
      Expire or delete all cache entries in the database, or selectively clear caches by namespace ($cache API) Clear the the template render cache. Clear out specific folders inside your site's cache directory (/site/assets/cache) Refresh version strings for static assets to bust client-side browser caches (this requires some setup, see the full documentation for details). This is the basic function of the module. However, you can also add different cache management action through the API and execute them through the module's interface. For this advanced usage, the module provides:
      An interface to see all available cache actions and execute them. A system log and logging output on the module page to see verify what the module is doing. A CacheControlTools class with utility functions to clear out different caches. An API to add cache actions, execute them programmatically and even modify the default action. Permission management, allowing you granular control over which user roles can execute which actions. The complete documentation can be found in the module's README.
      Beta release
      Note that I consider this a Beta release. Since the module is relatively aggressive in deleting some caches, I would advise you to install in on a test environment before using it on a live site.
      Let me know if you're getting any errors, have trouble using the module or if you have suggestions for improvement!
      In particular, can someone let me know if this module causes any problems with the ProCache module? I don't own or use it, so I can't check. As far as I can tell, ProCache uses a folder inside the cache directory to cache static pages, so my module should be able to clear the ProCache site cache as well, I'd appreciate it if someone can test that for me.
      Future plans
      If there is some interest in this, I plan to expand this to a more general cache management solution. I particular, I would like to add additional cache actions. Some ideas that came to mind:
      Warming up the template render cache for publicly accessible pages. Removing all active user sessions. Let me know if you have more suggestions!
      Links
      https://github.com/MoritzLost/ProcessCacheControl ProcessCacheControl in the Module directory

    • By joshua
      This module is (yet another) way for implementing a cookie management solution.
      Of course there are several other possibilities:
      - https://processwire.com/talk/topic/22920-klaro-cookie-consent-manager/
      - https://github.com/webmanufaktur/CookieManagementBanner
      - https://github.com/johannesdachsel/cookiemonster
      - https://www.oiljs.org/
      - ... and so on ...
      In this module you can configure which kind of cookie categories you want to manage:

      You can also enable the support for respecting the Do-Not-Track (DNT) header to don't annoy users, who already decided for all their browsing experience.
      Currently there are four possible cookie groups:
      - Necessary (always enabled)
      - Statistics
      - Marketing
      - External Media
      All groups can be renamed, so feel free to use other cookie group names. I just haven't found a way to implement a "repeater like" field as configurable module field ...
      When you want to load specific scripts ( like Google Analytics, Google Maps, ...) only after the user's content to this specific category of cookies, just use the following script syntax:
      <script type="optin" data-type="text/javascript" data-category="statistics" data-src="/path/to/your/statistic/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing" data-src="/path/to/your/mareketing/script.js"></script> <script type="optin" data-type="text/javascript" data-category="external_media" data-src="/path/to/your/external-media/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing">console.log("Inline scripts are also working!");</script> The type has to be "optin" to get recognized by PrivacyWire, the data-attributes are giving hints, how the script shall be loaded, if the data-category is within the cookie consents of the user. These scripts are loaded asynchronously after the user made the decision.
      If you want to give the users the possibility to change their consent, you can use the following Textformatter:
      [[privacywire-choose-cookies]] It's planned to add also other Textformatters to opt-out of specific cookie groups or delete the whole consent cookie.
      You can also add a custom link to output the banner again with a link / button with following class:
      <a href="#" class="privacywire-show-options">Show Cookie Options</a> <button class="privacywire-show-options">Show Cookie Options</button> This module is still in development, but we already use it on several production websites.
      You find it here: https://github.com/blaueQuelle/privacywire/tree/master
      Download: https://github.com/blaueQuelle/privacywire/archive/master.zip
      I would love to hear your feedback 🙂
      Edit: Updated URLs to master tree of git repo
       
    • By David Karich
      Admin Page Tree Multiple Sorting
      ClassName: ProcessPageListMultipleSorting
      Extend the ordinary sort of children of a template in the admin page tree with multiple properties. For each template, you can define your own rule. Write each template (template-name) in a row, followed by a colon and then the additional field names for sorting.
      Example: All children of the template "blog" to be sorted in descending order according to the date of creation, then descending by modification date, and then by title. Type:
      blog: -created, -modified, title  Installation
      Copy the files for this module to /site/modules/ProcessPageListMultipleSorting/ In admin: Modules > Check for new modules. Install Module "Admin Page Tree Multible Sorting". Alternative in ProcessWire 2.4+
      Login to ProcessWire backend and go to Modules Click tab "New" and enter Module Class Name: "ProcessPageListMultipleSorting" Click "Download and Install"   Compatibility   I have currently tested the module only under PW 2.6+, but think that it works on older versions too. Maybe someone can give a feedback.     Download   PW-Repo: http://modules.processwire.com/modules/process-page-list-multiple-sorting/ GitHub: https://github.com/FlipZoomMedia/Processwire-ProcessPageListMultipleSorting     I hope someone can use the module. Have fun and best regards, David
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version (has been tested up to v3.3), add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "https://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


×
×
  • Create New...