[ Content | Sidebar ]

How to pass custom parameters to a Facebook-”Share”-button
Twitter this post Share this post on Facebook

August 14th, 2011

I was searching for ways to implement a Facebook-”Share”-button in a website and came across a lot of suggestions that basically look like this:

<a title="Share this article/post/whatever on Facebook" 
     href="http://www.facebook.com/sharer.php?
     u=the url you want to share
     &t=title of the article/post/whatever" 
     target="_blank">
     <img src="your/path/to/facebook-icon.png" 
             alt="Share on Facebook" />
</a>

In the link you have the parameters “u” and “t” for the url and title you want to pass to Facebook.
But this does not really work as expected. When the “Share”-button is clicked, the correct url from the “u”-parameter is passed but not the title in the “t”-parameter.
What facebook actually uses for the title, description and images of the shared content are the meta-tags in the header of the page for “title” and “description” and the images it fetches by scanning the page.

But what if the header is the same on all the pages of the site?
Using this way of implementing a “Share”-button would deliver the same description and title for everything. So the shared url and images would be for a specific article and the shared text would always be the title and description for the whole site. Not good.

But even if it is not being advertised by Facebook
(everything there seems to just link to the documentation of the “Like”-Button which a) has a different functionality and b) might not be an option for some sites because of data privacy concerns) there actually is a way to specifically tell Facebook what url, title, description and image to use for the “Share”-button. It works like this:

<a title="Share this article/post/whatever on Facebook" 
    href="http://www.facebook.com/sharer.php?
    s=100
    &p[url]=the url you want to share
    &p[images][0]=the image you want to share
    &p[title]=the title you want to share
    &p[summary]=the description/summary you want to share" 
    target="_blank">
     <img src="your/path/to/facebook-icon.png" 
       alt="Share on Facebook" />
</a>


Groovy
Twitter this post Share this post on Facebook

April 27th, 2011

Some time ago i had the pleasure to write a little script in groovy. It was my first contact with groovy and a pleasure it was indeed. The whole less verbose (in comparison to java) approach with features like closures and dynamic typing makes it a fun language to script in. My intention was to write a script that deletes certain files in the workspace of a hudson-job after a build. Namely the remains of a coredump that seems to be haunting our test system sporadically. When this happens, the logs are archived and a notification mail is sent. But to not clutter up the disk and maybe also trigger false alarms in following builds the workspace needs to be cleared of the remains(core, log-file) of the coredump.

So this is how it can be done using groovy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// This script is used to clear the workspace of the remains of a coredump
// that might have happened during the previous build
 
def workspace = new File('.')
files = []
workspaceRoot.eachFileMatch(~/hs_err.pid.*\.log/) {files << it.name}
workspaceRoot.eachFileMatch(~/core/) {files << it.name} 
if (files.size() < 0) {
  println "Remains of a coredump have been found in the workspace." 
  println "They will be removed now."
  boolean allDeletedSuccessfully = true;
  files.each {deleteFile(it)}
  if (allDeletedSuccessfully)  {
    println "All files have been successfully removed."
  }
  else {
    println "Could not delete all files."
  }
}
 
def deleteFile(it) {
  File fileToDelete = new File(it);
  println "Trying to delete: " + fileToDelete;
  if (fileToDelete.delete()) {
    println "Deletion of file '" + fileToDelete + "' was successful."
  }
  else {
    allDeletedSuccessfully = false;
    println "Deletion of file '" + fileToDelete + "' FAILED."
  }
}

Post-build scripts in hudson are being executed in the workspace root by default. This is also the directory in which i want to search for the files to be removed. So this directory is defined as the starting point:

4
def workspace = new File('.')

Check if there are files that match the regular expressions( e.g. ‘hs_err_pid1234.log’ and ‘core’). If files are found then store their name in a list:

5
6
7
files = []
workspaceRoot.eachFileMatch(~/hs_err.pid.*\.log/) {files << it.name}
workspaceRoot.eachFileMatch(~/core/) {files << it.name}

So if the list is not empty then there are files that should be deleted. Each element in the list is the name of a file that will be deleted:

8
9
10
11
12
13
14
15
16
17
18
19
if (files.size() < 0) {
  println "Remains of a coredump have been found in the workspace." 
  println "They will be removed now."
  boolean allDeletedSuccessfully = true;
  files.each {deleteFile(it)}
  if (allDeletedSuccessfully)  {
    println "All files have been successfully removed."
  }
  else {
    println "Could not delete all files."
  }
}

In the method deleteFile each element of the list is handed over as a parameter and used to instantiate a new File object on which delete() can be called:

21
22
23
24
25
26
27
28
29
30
31
def deleteFile(it) {
  File fileToDelete = new File(it);
  println "Trying to delete: " + fileToDelete;
  if (fileToDelete.delete()) {
    println "Deletion of file '" + fileToDelete + "' was successful."
  }
  else {
    allDeletedSuccessfully = false;
    println "Deletion of file '" + fileToDelete + "' FAILED."
  }
}