TinyMCE, relative URL's and base href

TinyMCE, relative URL's and base href

Thomas van den Heuvel གིས-
Number of replies: 3

After some issues with TinyMCE and the way it deals with URL's I decided to do some digging around.

What I want to do
I want to be able to add relative URL's within WYSIWYG-content. I am using TinyMCE as my text-editor-of-choice inside Moodle 2. I want to use relative URL's because:
1.) the site / domain where I edit content might not necessarily be the site / domain through which this content is made available to the public, so if I am editing a site on domain A it will probably not display properly on domain B with full URL's; this is not a highly unlikely scenario is it? I can imagine you host different courses on one installation made available via different domains?
2.) I might want to move the entire site at some point without having to worry about some absolute URL's still being stuck on the old domain, I assume there is no "intelligent logic" that will automatically alter the domain-part of URL's as they are stored inside the WYSIWYG-texts right? Because how would this logic be able to distinguish between a full URL which is supposed to be an internal link and a "proper" external URL?

What I am thinking of doing
The only thing I have gathered sofar to accomplish the use (and enforcing) of relative URL's -which leaves external URL's (URL's clearly pointing to a different domain) alone, TinyMCE is at least that intelligent- is setting the relative_urls parameter value in lib/editor/tinymce/lib.php to true.

However...
Relative URL's still go awry because there is no base href present in the theme layouts (in the head part the following method is called: $OUTPUT->standard_head_html(), this does not contain a base href). I would have expected the <base href="" /> tag would be present by default through the standard_head_html() method or through $CFG->additionalhtmlhead inside that method, with $CFG->wwwroot as its default value. If this would be available by default (would be a nice addition, and I cannot think of a reason why this would break functionality) relative URL's would work "out of the box".

So my question is
Once I set the relative_urls parameter to true, what should I do? I want to use / enforce relative URL's in my entire Moodle installation without having to alter all my layouts so I either:
1.) add <base href="" /> to the standard_head_html() method with $CFG->wwwroot as its default value; I know this is "hacking" the core, but I feel like this should be present by default in Moodle
2.) add <base href="" /> to $CFG->additionalhtmlhead though I don't feel as if this is the right place to put such a "standard" thing

EDIT: Actually, the base href should perhaps even be one of the first elements in the head-tag, so it can precede relative calls to CSS and JavaScript files and such?

Which solution should I take, are there any other solutions to this problem (making relative URL's work / enforcing these)?

Thanks in advance for shedding any light on this topic.

དཔྱ་སྙོམས་ཀྱི་སྐུགས་ཚུ།: -
In reply to Thomas van den Heuvel

Re: TinyMCE, relative URL's and base href

Tim Hunt གིས-
Core developers གི་པར Documentation writers གི་པར Particularly helpful Moodlers གི་པར Peer reviewers གི་པར Plugin developers གི་པར

You don't need to do any of this.

1) If you move a course from one site to another you will be using Moodle's backup and restore system. That automatically re-writes the absolute URLs. If is actually very easy for the system to search through the conent and find any occurrences of http://old.site.com/moodle/ and replace that with http://moodle.new.com/ - or whatever. Relative URLs are much harder to recognise.

Even more, suppose in your content, you put a link to something like http://old.site.com/moodle/mod/forum/view.php?id=123, Moodle will also recognise that, and change the link to http://moodle.new.com/mod/forum/view.php?id=456 - or whatever the new id is.

2) Similarly, if you move your entire Moodle site to another URL, then there is a script /admin/replace.php which updates all the URLs for you.

So, you are trying to solve a non-problem.

In reply to Tim Hunt

Re: TinyMCE, relative URL's and base href

Thomas van den Heuvel གིས-

Thanks for the feedback, I will use this approach for now, but I still think some work can be saved by avoiding absolute URL's.

The way I see it is thus: Moodle goes about a certain way addressing their own "internal" pages. This creates a "problem" when moving your site or whatever. Moodle also provides a solution to this problem by offering tools to correct the issues that might arise from this approach. Either way, some additional work needs to be done.

Why not eliminate the problem at its source? If you use relative URL's (or at least eliminate absolute URL's in some way) you can just move your site (code, database, content), change the config file and you should be ready to go? Obviously, you should stick to the guidelines as set in the Migration article; it will not become super easy all of a sudden, but at least easier by eliminating steps from the migration process?

Also, if you use a development environment, you are pretty much forced to run these scripts / perform these additional steps when you want to update your development database / content with dumps from the live environment. This is overhead which can be avoided.

What if you forget to run the script to convert the URL's and you're clicking away on your local site after synching the content with live and you're unaware you actually slipped into the live domain while you are experimenting with (live) content. Oops?

All I am saying is I can see disadvantages using absolute URL's and benefits not using them.

In reply to Thomas van den Heuvel

Re: TinyMCE, relative URL's and base href

Tim Hunt གིས-
Core developers གི་པར Documentation writers གི་པར Particularly helpful Moodlers གི་པར Peer reviewers གི་པར Plugin developers གི་པར

Well, have you ever done any Moodle development? You are looking at the problem from the outside, and reaching very superficial conclusions. That is not a criticism of you. Wait until you have had to fix some bugs in the guts of the backup/restore system (hopefully you will never have to མིག་ཁྱབ་) and then you will understand why things work the way they do.