Create absolute url from relative url in drupal - url

I have created an update script in hook_update_N where I have to create relative urls from absolute url.
For eg :
relative url = /files/test1.jpg
absolute url should be http://localhost.mysite/files/test1.jpg
I know these possible solutions,
file_create_url()
url()
$_SERVER['host'], $_SERVER['server_name']
But none of them is working in the update script.
Possible Reason : Script isn't executed from browser. So there isn't request from which you can resolve server name etc.
The above solutions work fine if I run the script from browser instead of terminal.
Is there any other way we can create absolute urls in hook_update_N so that it works when I run the script from terminal?

Try to use global $base_url and add to it your relative url
global $base_url;
$full_url = $base_url . $relative_url

Related

how to get fixed static images url for jenkins?

I generated the html webpage in jenkins jobs and try to success/failure icons inside.
But I noticed it is linked to the url with some random number 9b17c509 in the path
https://ci.jenkins.io/static/9b17c509/images/32x32/red.png
What I preferred is the fixed url for every jenkins instance like
https://ci.jenkins.io/static/images/32x32/blue.png
Any suggestion to solve it ?
BTW: I don't want to connect to external web for those images
9b17c509 looks like a cache.
We can use /images/32x32/blue.png
see https://ci.jenkins.io/images/32x32/blue.png
For a hudson.model.Result or the result name like "SUCCESS" you can get a ball icon with hudson.model.BallColor .
In Groovy
def iconUrl = (result as BallColor).getImageOf("16x16")

Socket IO . How to change url path

How to change url path of socket.io.js
my socket.io.js is located at
https://192.168.236.100/socket.io/socket.io.js
what if i want to change it like:
https://192.168.236.100/socket.conference/socket.io.js
thank you
It is not as simple as changing the this._path value or passing in the path option to the constructor because if you change that, it also affects how a socket.io client must connect to the server too, not only how the socket.io.js file is served.
The simplest way I could find to make the socket.io.js file appear to be coming from a different path and not change anything else is to just create a new route for it like this:
app.get("/socket.conference/socket.io.js", function(req, res) {
res.sendFile(path.join(__dirname, "node_modules/socket.io-client/socket.io.js"));
});
This assumes that socket.io is installed locally in the directory that your app file is running from. If it is installed somewhere differently, then you need to find where the /socket.io-client/socket.io.js path/file is and use the right path to that.
This works with this client <script> tag:
<script src="/socket.conference/socket.io.js"></script>

Get Symfony URL relative to the index file

I have a symfony project which, because of DNS issues, is http://<project-name>/ locally, but it needs to be http://<qa-host-name>/<project-name>;/ when hosted in a more QA level environment but it may be http://<domain-name>/ for production (so, I need this to work for both). Now, the images folder will always be relative to the <project-name> directory, so locally it will be http://<project-name>/my-smilie.png and on QA it will be http://<qa-host-name>/<project-name>/my-smilie.png
Since everything is relative to the URL of the index.php, I thought that Symfony would have something to create dynamic URLs which work even if the context is different so that my template.php could have something like
<?php echo image_url("my-smilie.png");
/*see below for potential implementation*/?>
and it would output http://<project-name>/my-smilie.png, http://<qa-host-name>/<project-name>/my-smilie.png, or http://<domain-name>/my-smilie.png. (Relative URLs are fine, but absolute would be better).
Below is an example of what I am looking for, but I feel like I am trying to re-invent the wheel and that Symfony has already accomplished this.
function image_url($img)
{
return get_base_url() . '/images/' . $img;
}
function get_base_url()
{
$par = dirname( $_SERVER[ 'SCRIPT_NAME' ] );
if($par == "/") $par = "";
return $par;
}
try public_path('images/smilie.jpg') function
public_path() manual
I just came across a situation where the public_path helper does not work terribly well, but there is an alternative:
// this populates $host with absolue URL of the parent directory
// of the Symfony script
$host = $request->isSecure()? 'http://':'https://';
$host .= $request->getHost() . $request->getRelativeUrlRoot();

system.io.directorynotfound -> But it works in Console

My files are referenced like so (it's all relative):
// WHERE YOU KEEP THE PAGE TITLE XML
public static string myPageTitleXML = "xml/pagetitles.xml";
and
using (StreamReader r = new StreamReader(myPageTitleXML))
{ //etc.. . .etc....etc..
}
I get system.io.directorynotfound, and "this problem needs to be shut down", when I double click the executable. But running it from the console works like a charm. What's wrong here?
I played around with attempting to set Environment.CurrentDirectory but couldn't get anything to work. Why should I have to do that anyway? It defeats the purpose of a relative path no?
responding.. .
"application" does not exist in the current context, i'll keep trying what people have mentioned, this is not a windows.form
testing
Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); gives error URI formats are not supported, as does Path.GetFullPath(). Server.MapPath results in an error as well, this is currently offline
Well assuming this directory is somewhere under the directory in which your code is executing, it sounds like you can use ..
Application.ExecutablePath()
or
Application.StartUpPath()
.. to get an idea as to what your application is seeing when it goes in search of an 'xml' directory with the 'pagetitles.xml' file in it.
If the directory returned by one of these methods does not point where you thought it did, you'll need to move the location of your application or the location of this folder so that it is within the same directory as the app.
Hope this gets you on the right path.
So, when you run it from double clicking the executable, is there a file named pagetitles.xml in a folder named xml, where xml is a folder in the same location as the executable?
It's certainly possible to use relative paths like this, but I wouldn't really recommend it. Instead, maybe use something like:
string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);
using (StreamReader r = new StreamReader(fileToOpen))
{
//etc.. . .etc....etc..
}
Is this ASP.NET code? If so then you probably need to do MapPath("xml/pagetitles.xml")

request.serverVariables() "URL" vs "Script_Name"

I am maintaining a classic asp application and while going over the code I came across two similar lines of code:
Request.ServerVariables("URL")
' Output: "/path/to/file.asp"
Request.ServerVariables("SCRIPT_NAME")
' Output: "/path/to/file.asp"
I don't get it... what is the difference? both of them ignore the URL rewriting that I have set up which puts the /path folder as the root document (the above URL is rewritten to "/to/file.asp")
More info:
The site is deployed on IIS 7
URL Gives the base portion of the URL, without any querystring or extra path information. For the raw URL, use HTTP_URL or UNENCODED_URL.
SCRIPT_NAME A virtual path to the script being executed. Can be used for self-referencing URLs.
See, http://www.requestservervariables.com/url
and /script_name for the definitions.
This could be a bug under IIS 7.
I could not get Request.ServerVariables("URL") and Request.ServerVariables("SCRIPT_NAME") to return different values. I've tried the cases where they were called from an included file (<!--#include file="file.asp"-->) or after a Server.Transfer.
Is this maybe there in case of Server.Transfer?
In the case where you do a server.transfer i think you would get different results
i.e. SCRIPT_NAME would be e.g. /path/to.transferredfile.asp whereas URL would remain as /path/to/file.asp

Resources