0
votes

I have migrated a MediaWiki from one environment to another. Everything works fine, except for the fact that a few users have hard-coded external links (which actually link to other pages within the same wiki). For example, the old wiki was on ://foo/wiki and the new is at ://foo.com. On pages of the migrated wiki there are external links on the page text like ://foo/wiki/somepage. So when they click the link on the migrated wiki they get taken back to the old wiki and are directed to ://foo/wiki/somepage -- which is not the correct behavior (it should go to ://foo.com/somepage). In the "externallinks" table in MySQL I have run the two statements on a single link to see if the change would update the link:

UPDATE `foowiki`.`externallinks`
SET el_to = REPLACE(el_to, '://foo/wiki', '://www.foo.com')
WHERE el_to LIKE '%://foo/wiki/somepage%';

UPDATE `foowiki`.`externallinks`
SET el_index = REPLACE(el_index, '://foo./wiki', '://com.foo.www.')
WHERE el_index LIKE '%://foo./wiki/somepage%';

When I restart the site and go to the page with the sample link I'm trying to update, the link is still the same (i.e., it links to the old wiki environment).

Is there a quick and easy way to replace all external links (in the text) with the correct prefix (i.e. go from ://foo/wiki/somerandompage to ://foo.com/somerandompage)? Maybe there is an even cleaner way to do it, instead of updating all the records in a MySQL table, such as intercepting the HTTP requests in LocalSettings.php and doing a single substring replace there?

2
Try this preg_replace('/:\/\/foo\/wiki/i', '://foo.com', $record_content); [assuming $record_content contains the text with old links]. When you echo the record or inside an html do this replacement. - Sony Mathew
No code solution: Export the database to your computer, open it with notepad++, do a search and replace (ctrl+h), re-import the DB. (Remember, always backup...). - Pedro Lobito
externallinks is a sort of cache. You need to change the links in the wikitext (the text table) and then force a reparse of all pages. There are better ways to change the links than direct DB manipulation, though. - Tgr

2 Answers

0
votes

Solution A (no code):

  1. Dump the table:
    mysqldump -u user -p database table > dump.sql
  2. Open it with notepad++
  3. Do a search and replace:
    ctrl+h
  4. Re-import the table:
    mysqlimport -u user -p database table < dump.sql

Solution B, use sed:

  1. Dump the table:
    mysqldump -u user -p database table > dump.sql
  2. Find and replace using sed:
    find /path/to/dump.sql -type f -exec sed -i 's/:\/\/foo\/wiki\//:\/\/foo.com\//g' {} \;
  3. Re-import the table:
    mysqlimport -u user -p database table < dump.sql

PS: always backup.

0
votes

Rather than trying to alter the database (the layout is quite complex), I would try the extension Replace_Text. It is stable, well tested, and made just for situations like these.

BTW, you have a colon prepending you protocol relative URLs. You probably mean to write //www.foo.com.

If you still want to hack the DB, I believe it is the archive and text tables you want to look at. After doing your work there, you will have to rebuild the external links table based on that.