Migrating from one URL to another -- how to change links in HTML resources not in the database

Migrating from one URL to another -- how to change links in HTML resources not in the database

by Jeffrey Silverman -
Number of replies: 2
Howdy, Moodlers. So, /admin/replace.php will fix your URLs in the database as the final step of a site migration.

However, what about HTML files in a site's course's moodledata area? (i.e. Course files)

If, say, an HTML file has a link to an image that resides within a course's files area, one must update all the files and reupload them into the moodledata folder in the right place. Is there an existing script or tool that will help with this?

Example:

OLD HTML FILE

<html>
<body>
<p align="center"><img src="http://old.moodle.server.edu/file.php?file=/42/thing/image.jpg"></p>
</body>
</html>

MUST CHANGE TO

<html>
<body>
<p align="center"><img src="http://new.moodle-hosting-provider.com/file.php?file=/97/thing/image.jpg"></p>
</body>
</html>


I am going to write a short perl script to do this for HTML files but no point in reinventing the wheel if one exists already. However I could not find anything anywhere.

Thanks!
Average of ratings: Useful (1)
In reply to Jeffrey Silverman

Re: Migrating from one URL to another -- how to change links in HTML resources not in the database

by Richard Enison -
JS,

I gather these HTML files did not come with Moodle but were created by you and/or your associates, students, etc. and uploaded to the data directory. Well, you would not have this problem if these files had been created by the preferred method, as described on Moodle doc page Web page, using the HTML editor to insert images. I just tried that with my Moodle and do you want to know what it did? It didn't create an HTML file at all. Instead, it put the HTML (yes, with the server IP included in the img tag) into the alltext column of the row in the mdl_resource table in the database. So if I ever migrate to another server, replace.php will take care of all those references.

In the meantime, if you want to know if there is an existing script to do a global search and replace of all occurrences of a character string in a bunch of text files in one or more directories, there are such scripts:
  1. If you are using Linux/Unix/etc., you can probably do it in one line using a pipe containing several standard *nix commands, based chiefly on grep. I think I have seen examples of this in this forum, but offhand I don't have a link to one. If you can't find it in the Moodle forums, ask my old friend Google.
  2. About 10 years ago I wrote a Microsoft Word (wash my mouth out with soap) macro in VBA that did that, because at the time I didn't seem to be able to get Microsoft Front Page 98 to do it. Either I was mistaken that it doesn't do it, or they have improved the program, but I'm pretty sure you can do it with a relatively recent version of Front Page if you happen to have one. I wouldn't buy it just for that.
  3. If you have your files on a (horror of all horrors) Windows server, you can use the search command of Windows Explorer to find all occurrances of a character string in a directory tree of text files. But they had better be recognized by Windows as text files, which means they need to have the extension .txt. So you will need to temporarily rename all your .htm (or .html, .php, etc.) files as .txt files. And it doesn't do replacement. Also, in MS Word you can do the same thing with Word files, so temporarily rename your files as .doc files.
  4. I have a global search PHP script on my PC that I got from http://www.terraserver.de/ about a half a year ago. It lets you choose what directories to search in, what types of files to search, etc. I don't think it has a replace option but I'm sure you could make a minor modification to it so it would.
RLE
In reply to Richard Enison

Re: Migrating from one URL to another -- how to change links in HTML resources not in the database

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
If you have a copy of Dreamweaver you do a find and replace on all files...