How to create a local copy of app/code/core/Mage/

Last modified by dd_gi on Fri, June 25, 2010 09:05
Source|Old Revisions  

This is an old revision of the document!

When searching through the forums (thank goodness for the forums) you will see posts that recommend you edit a “local” copy of a given Mangento core file such as app/code/core/Mage/Catalog/Model/Layer/Filter/Price.php

The recommendation to edit a local copy of a core file is so that when you upgrade to a new version of Magento the core file you edited will be overwritten and you will lose your changes.

How the local folder works

Core files in the local folder override the same files in the core folder. For example, if you copy app/code/core/Mage/Catalog/Model/Layer/Filter/Price.php to app/code/local/Mage/Catalog/Model/Layer/Filter/Price.php then Magento will use app/code/local/Mage/Catalog/Model/Layer/Filter/Price.php

The complication is that the app/code/local/* folders are not created by default, so if you would attempt to copy the file in an ssh session etc. the copy would fail since the enclosing folders have not been created.

How to create the local folder structure

You could just create folders as need, for example if you wanted to create a local copy of app/code/core/Mage/Catalog/Model/Layer/Filter/Price.php you would first create the local folder if it did not exist, then inside local you would create a Mage folder and so on.

With PHP 5 and up you can create a complete directory structure with one mkdir command as it now has a recursive option, as well you can set permissions during that process.

To make that task easier I created a PHP script to duplicate the /app/code/core/Mage/ folder structure to /app/code/local/Mage/ and set the permissions to 777 (world readable,writeable,executable) during creation.

If world WRX is too open for your security structure please modify that value accordingly. If you are curious about what other values are acceptable, Google for “online chmod calculator”.

Note that the script creates a folder only if it has not been created, if a local folder or subfolder exists now it does not create it nor does it modify any contents, it simply creates folders under /app/code/local/Mage/ if they do not exist.

How to use the script

First, create a new file named createlocal.php within your app/code/ folder within your Magento root folder. Copy the following code and paste into that file, then open it within a web browser using http://yourdomain/yourmagentorootfolder/createlocal.php and you should see lines of folders stating creating:... or exists: for each folder/subfolder, then finally you will see a process complete. message.

The script

  1. <pre>
  2. <?php
  3. /*
  4. Purpose: To duplicate the app/code/core/Mage/ folder
  5.          (folders only, no files) structure
  6.          to /app/code/local/Mage/
  7. */
  8. error_reporting(E_ALL);
  9. function dupCodeDirsNoFiles($dir) {
  10.     global $root;
  11.     $listing = opendir($dir);
  12.     while(($entry = readdir($listing)) !== false) {
  13.         if ($entry != "." && $entry != "..") {
  14.             $coreItem = "$dir/$entry";
  15.             if (is_dir($coreItem)) {
  16.                 $localItem=str_replace("$root/core/Mage","$root/local/Mage",$coreItem);
  17.                 if(!file_exists($localItem)){
  18.                     echo "creating: $localItemrn";
  19.                     $old_umask = umask(0);
  20.                     mkdir($localItem,0777,true);
  21.                     umask($old_umask);
  22.                 } else {
  23.                     echo "  exists: $localItemrn";
  24.                 }
  25.                 dupCodeDirsNoFiles($coreItem);
  26.             }
  27.         }
  28.     }
  29. }
  30. if(file_exists(getcwd().'/app/code/core/Mage/')){
  31.     dupCodeDirsNoFiles(getcwd().'/app/code/core/Mage');
  32.     echo "process complete.";
  33. } else {
  34.     echo "Please verify that you have this script in the root folder of your Magento installation.";
  35. }
  36. ?>