Thaura: Porting Legacy PHP5 and PHP7 Project to PHP8 Part 4

Continuing the series, more info about altering project internals.

I think of this like a caterpillar going into a cocoon, having enzymes totally disintegrate its guts, and then produce a butterfly, which will go out and mate with other butterfiles. It’s a metamorphosis. Except, with this software, you don’t see any visible changes once it’s changed.

So that’s not a good analogy.

Maybe it’s more like an electric car, which looks as ugly as the gasoline cars, or even worse, but has completely different insides. It’s better on the inside.

Another bad analogy.

It’s like you shoved a seed into a pile of manure, and it’s started to grow, but you still can’t see the plant. It just looks like a pile of shit. But there’s good stuff in there.

Yes, that’s it.

I’m going to do another release of software.

I’ll just call it r2 or release 2….


Here’s what happened. It took all day. The reasons were noted in the email I sent out, but the actual delays were as follows:

The instructions that the LLM wrote were not easy to understand. In retrospect, I should have just hand-written them.

(Then, those notes could have become part of the LLMs knowledge, and could have influenced it to write better documents.)

Cloning the database was smooth, but getting the live database container updated to a newer version of MariaDB took extra time.

Cloning the release from Git was quick. Moving it into place was quick. (Name the new clone directory blahblah.new, then mv blahblah to blahbla.old, then mv blahblah.new blahblah. Restart Nginx, restart the php container.)

Configuring the Nginx proxy took a LOT of effort, because I did it too soon. The examples the LLM created were wrong, as well, but had enough info that I could make it work.

Reconfiguring the containers to run the app was pretty simple, but I had forgotten that I changed the paths where I wanted the files mounted. This sent me down some rabbitholes related to Nginx config. (I should have held back on changing Nginx.)

Overall, it wasn’t that bad, except for the Nginx configuration.

A New, Nonfunctional API

I made an API to feed the mobile app, and it didn’t work. Digging into the code, was a minor horror. The LLM wrote code well, but the overall “separation of concerns” and keeping the code in the right places, wasn’t so great.

I forget what the fix was, but it eventually worked. It was a combination of fixing some obsolete SQL code, and making an Nginx config.

PHP Attack Scripts

The PHP security hole drama has led to a bit more attack against the sites, particularly scans for remote shells written in PHP.

That just overloads the server, even when the scripts aren’t there.

So I had to do some tricks to detect these requests, and block them. This took a few hours.

Get Rid of phpinfo()

If you’re running PHP in a container, and use .env to pass passwords into your container, it’s critical to find and remove all instances of scripts that use the phpinfo() function.

That function dumps the environment variables to the page, leaking credentials. This command will recursively find the string “phpinfo” in all your files.

grep -r phpinfo *

Archives

One of my big goals was to get the site into shape for archving. This entails being able to easily download the entire site, using tools like wget.

Generating HTML files as final output is one step toward this goal. While archivers handle .php file endings, they like .html more, and with .html, you can view archives with a plain web browser.

With .html files, you can “archive” by copying over all the files. You won’t need all this software to operate the archived site.

Indexes

Generating complete indexes to the news articles and events allows webcrawlers to get at everything.

This is also a risk, because lots of bots are out there scraping the site, and a complete index will potentially increase their downloading, and risk overloading the site.

Page generation helps with this, because serving up a generated HTML page takes a tiny fraction of the resources of running a PHP file. (Example: on an older computer, a very heavy site like WordPress can top out at 30 requests per second, while a similar HTML site can reach into the 10k requests per second.)

Bots I’ve seen scraping the site: Google, Bing, Claude, Baidu, OpenAI, Ahrefs, Yandex.

Twig Templates

To this end, instead of trying to change the page generation within the existing code, I had the LLM add Twig. It seemed to know more about Twig than Blade, so I went with Twig.

The LLM set up a parallel template structure that copied parts of the existing templates, and then ported the entire site to use it, but didn’t actually remove all of it, because some of the templates we had were full of PHP code.

The Twig templates mixed with the old template system are a mess; they’re completely enmeshed, to replicate the old site.

Eventually, almost the entire site will be generated with Twig.

Eventually, this will be replaced with new layouts that aren’t mixed with the old code.

I suspect I’m going to need to start the process by hand, writing my own templates.

Order and Memory Capacity

This did get me thinking about the issue of why we think some code is well organized, and some code is not.

Part of it is our limited capacity to comprehend large, complicated systems.

To handle big systems, we use structures, like lists, hierarchies, “black boxing”, making things modular, and using repetitive patterns.

The LLM, unlike people, has a different kind of “memory” or “attention” or capacity to deal with large amounts of information. So, while the code looks like a mess to me, it still makes sense to the LLM.

Here’s some computer code that vibe coding produced:

function rotated(p){ // rotate cells 90deg clockwise, re-normalize to 0
  const maxX = Math.max(...p.cells.map(c=>c.x));
  const cells = p.cells.map(c=>({x:c.y, y:maxX-c.x, ch:c.ch}));
  const minX = Math.min(...cells.map(c=>c.x)), minY = Math.min(...cells.map(c=>c.y));
  return cells.map(c=>({x:c.x-minX, y:c.y-minY, ch:c.ch}));
}

Almost any programmer has written code like that, but how many enjoy reading code like that.

Conversely, computer code is extremely brittle. A single character out of place, and parts may break. The LLM can write code, but more importantly, it can transform vague instructions and turn them into concrete goals, like people can. So it’s like a programming language that’s a human language.

The challenge is to get the LLM to conform to (the declining) human need to keep code organized, for human comprehension.

admin
Author: admin

This is the server’s system administrator. This site is undergoing some changes.