I have a site I’m migrating from straight HTML to Joomla, and I have a bunch of HTML files I want to load into it. I wrote a perl script that automates all this. It reads the HTML file to determine the title of the page, it looks at the file’s modification date to determine the “publication date” for Joomla, and then it makes a MySQL database connection and executes the query.
This is a quick script I hacked together in about an hour. It probably works, but it is not for the faint of heart. If you barely understand Joomla, and you’ve never looked at perl before, and you’re working from a Windows PC, go elsewhere. This will be a lot of trouble for you and I won’t help you. If you’re using a Mac or Linux, or you’re comfortable running Perl on Windows, this is pretty straightforward.
- Download the script.
- Make sure you have the Perl prerequisites:
You must edit the script itself and change about ten things. That sounds like a lot, but they’re all obvious and straightforward. You edit these 10 lines:
$db::user = "dbuser"; $db::passwd = "dbpass"; $db::database = "joomla"; $db::hostname = "localhost"; $db::port = "3306"; $db::tablename = "jos_content"; $j::state = 1; $j::section = 1; $j::category = 1; $j::creator = 62;
The database stuff is obvious (you have to tell it what database server to connect to, what the database name is, the table name, and the credentials to use. The “state” is whether you want it published or not. I don’t know what the value needs to be to be “not published.” I’m going to guess 0. The category and section numbers should be easy to figure out from the category and section lists in your Joomla administrator interface. Finally, you need to figure out what your creator ID is. 62 is always admin. I don’t know how to figure out the right number for your particular user. Inspecting your MySQL database and looking at a post that is similar is a good way to find out these values.
Create a directory
This script takes exactly one directory name (not file names) as its one and only argument. Put each file that you want uploaded into the directory. If you want some files going into one category and other files going into another category, create several directories. Run the script on one directory, then edit it to change the category, and run it on the other directory. See? It’s crude, but effective.
Run the script
The script is called UploadToJoomla.pl. Invoke it (when you’re ready, by typing
perl UploadToJoomla.pl directory
(where directory is the location of the files to upload).
Limitations and Things to Know
- It takes the file’s modification date (the date that you see when you run ls or DIR) and makes that the creation date for the article. I.e., in Joomla, that’s the date that the article will appear to have been written. If you do as I suggest above, and you make copies of files in different directories, you’ll create a bunch of new files whose modification date is right now. Moving files from place to place is the only safe way to preserve their modification dates. That’s mv on FreeBSD / UNIX / Linux, REN on DOS / Windows, and dragging and dropping stuff around on MacOS and Windows.
- For those of you on a UNIX-like system, it does not handle symbolic links. They’re ignored.
- It does not go into subdirectories. That was a conscious choice on my part. It would be quite easy to modify the script to check if the entry in a directory is another directory, and then decend into it by calling processDir(). That’s left as an exercise for the reader.
- The script looks for the
tag to determine the article title. If it doesn’t find that tag, then it just assigns a single word “Article” as the title and summary.
- The article Title and Summary will be the same. All of the file’s content goes into the Intro Text. There will be no additional text.
- The entire content of the HTML file, even bits that Joomla doesn’t want (e.g., tags, tags, etc.) will get sucked into the database. As far as I can tell, Joomla handles this very gracefully. None of those things come out when the article is displayed on the site.
I don’t do a lot of programming for the public domain. Mainly I hack stuff until it works for me, then I move along. I hope this is helpful for people, but understand that it’s not production-quality software. If you have trouble, you can email me, but I might not be able to help you. Please realize that I literally never use Windows. So if you email with a problem about running it on Windows, I’ll be more lost than you are.
The code is released under the Perl Artistic License, which pretty much means you can do lots of stuff with it as long as you keep it all free (as in beer) and open.