1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best way to patch files using only PHP

Discussion in 'PHP' started by Eriksrocks, Jul 25, 2010.

  1. #1
    Before I get to the question let me explain what I am trying to do. I am working on a project that involves fixing bugs in a certain piece of web software and then distributing the fixes along with a script that will apply the fixes/patches to the user's installation of the software automatically. The core of this script will be the patching code that will take the fixes and apply them to the relevant files.

    I am looking for opinions as to the best way to patch files using PHP. I was originally thinking of distributing the fixes as diff files. I've found a couple of pure-PHP implementations of the Unix utility patch online, and on *nix environments we could always use the shell execution functions to access patch directly. But then someone else on the project brought up an interesting point: Diff files are based on specific line numbers. It is easily conceivable that somebody wanting to use our patches could have already modified their files in some way, adding some custom code, fixing something else, whatever, and that would throw off the line numbers. I assume that in this case, patch would not work correctly because the line numbers would be different. We could always use simple find and replace functions, but I am wary of just doing a simple "find and replace" because of the possibility of replacing something we don't want to replace and the possibility of overlapping patches. In addition, diff files are easy to make, understand, and distribute.

    He also said:
    However I was unable to find what he was referring to either in the PHP manual or elsewhere online.

    So, what do you think is the best way to patch files with PHP, keeping in mind my situation in which files could already be slightly modified? I could really use some help on this one. Thanks. :)
     
    Eriksrocks, Jul 25, 2010 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Maybe you should try some Unix programs for editing in system php functions.
     
    gibex, Jul 25, 2010 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    I would focus my effort on the people who have not modified the code in any way first. The diff system you're thinking of will work just fine for those people.

    I wouldn't put much effort into trying to work around other peoples modifications. If you try to patch around their modifications, you're going to create a support nightmare for yourself. Instead, I would setup an easy to use system which is invoked any time an unknown alteration is discovered. This system will show the expected section of code, the actual section of code, what the patch would do to the expected section of code, and some options for what to do such as replace it, ignore the change, and a textarea which can be used to enter a combined patch for that section. If they can alter their code, they can decide what to do. If they are paying someone to modify their code, they can pay someone to make the decisions for them.

    The bottom line is, you are obligated to support your product. You are not obligated to support their derivative of your product. Providing a means for them to apply patches around their own modifications is already going above and beyond.
     
    joebert, Jul 25, 2010 IP