Zotero plugin for DokuWiki

1. Juni 2010 um 22:48:11 - 619 Wörter - Beitrag Nr. 814

For managing my literature sources for reference lists I use Zotero and I really like it. It is a plugin for Firefox and supports synchronizing the database and the attachments to an online server. The only thing that was missing was a way to cite my sources from inside my DokuWiki. I wanted to be able to add citations to my texts like I do in LaTeX.

For this reason I created a plugin for DokuWiki that allows me to cite sources from my online Zotero database by using the syntax known from LaTeX. The plugin reads my reference list from the Zotero website and caches it on a local wiki page. After that I can cite a source simply by adding \cite{ShortName}. Take a look at my wiki (e.g. Programming Hints) for some examples.

DokuWiki Zotero Plugin 02
DokuWiki Zotero Plugin 01

For this to work, every source needs to have a short name as I did not want the cite key to be the Zotero ID but rather a more meaningful name. Almost every Zotero entry type has the field “short name” assigned to it so I used that for this purpose.

Here is an example of the steps to perform to be able to cite a Zotero source:

  1. I add a new entry to my Zotero database, e.g. Robert Martin: Clean Code (2008) and assign the short name Martin2008 to it.

    DokuWiki Zotero Plugin 04
  2. I synchronize my Zotero database with the Zotero server (because the plugin reads the data from the Zotero website).
  3. I write some text in my wiki and add \cite[p.123]{Martin2008}.
  4. I preview or save the current wiki page and the plugin tries to find the cited reference in its local cache and displays it. If the entry is not already in the cache, it automatically loads my newest Zotero sources from the website, caches them, and displays the cited source afterwards.

For the initial import of all my Zotero entries I added a script that parses the complete reference list on the Zotero website.

DokuWiki Zotero Plugin 03

Download

The plugin can be downloaded here: Download DokuWiki Zotero plugin or installed via the plugin manager.

Also see the DokuWiki Plugins page: zotero plugin.

Please see Install.txt for a short installation guide.

TYPO3: How to enable links to local files in RTE

18. Mai 2010 um 19:52:47 - 176 Wörter - Beitrag Nr. 808
Tags:

Today I needed to link to a local file on a network share from a page in TYPO3. This link did not start with the usual http:// but instead with file:///. The problem was, that the rich text editor (RTE) in the backend of TYPO3 always prepends “invalid” links with http:// and I ended up with http://file:///path-to-network-share, which of course did not work correctly.

I tried editing the RTE parse function in TYPOScript but that did not solve my problem. So, after a while of searching the internet I found the answer: append rtekeep="1" to the link like in the following example. This tells the RTE to leave the link untouched.

<a href="file:///path-to-network-share" rtekeep="1">Link to file</a>

(via tuga.at)

Using PHP to query Zotero’s REST API

21. April 2010 um 20:26:21 - 253 Wörter - Beitrag Nr. 796
Tags:,

Today I tried to query Zotero’s REST API with PHP to retrieve a list of my bibliography items for further processing. The basics are pretty easy setup using phpZotero and following this guide: Mashup Guide :: Zotero REST API: early developments.

However, I ran into some problems as the user id could be read by getUserId() but getUserItems() returned an empty item list. I found the solution after analyzing the request URLs: the user id is read using HTTP while all the other functions use HTTPS. So I simply added these two lines to httpRequest() to avoid checking the server certificate:

  1. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  2. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Furthermore, the name of the parameter for the API key is not apiKey but key. So the URL pointing to my bibliography items has to be https://api.zotero.org/users/{USERID}/items/?key={APIKEY} (you don’t need username). Although the API key is passed to phpZotero’s constructor, the class doesn’t use it in any of its HTTP calls, so I added this line to zoteroRequest():

  1. $request .= ‘?key=’ . $this->apiKey;

After theses small changes my items could be read successfully.

PowerShell 2.0: Windows Update KB968930 cannot be installed on Windows XP

Our WSUS server provided my computer with Microsoft Windows Update KB968930 (Windows Management Framework and PowerShell 2.0) but it could not be installed (without giving any helpful error message). After searching the web for quite some time, I found this solution to be working in my case:

  1. Deinstall PowerShell 1.0: Control Panel → Show Windows Updates → Windows PowerShell 1.0 → Remove
  2. Reboot the computer
  3. Install Windows Remote Management
  4. Install PowerShell 2.0
  5. Test installation: Start → Run → powershell → $PSVersionTable:
    PowerShell 2.0
  6. Get rid of the “New updates available” message in the system tray: Start → Run → wuauclt /detectnow

How often are functions returning an array for PHP’s foreach statement called?

18. März 2010 um 16:43:54 - 142 Wörter - Beitrag Nr. 783
Tags:

Today I was asked a question regarding PHP’s foreach statement: If the array through wich a script iterates is generated by a function, how often is this function called? In this example, will getArray() be called only once or three times?

  1. $c = 0;
  2. function getArray()
  3. {
  4.         global $c;
  5.         echo (++$c) . ". call to " . __FUNCTION__ . "\n";
  6.         return array(1, 2, 3);
  7. }
  8.  
  9. foreach (getArray() as $e)
  10. {
  11.         echo "ForEach: " . $e . "\n";
  12. }

I didn’t know the answer myself, although I presumed the function gets called only once. Interestingly, the PHP Manual doesn’t say a word about this question.

So, what’s the answer? The function gets called only once, as I suspected. Here’s the output of the code above:

1. call to getArray
ForEach: 1
ForEach: 2
ForEach: 3

bash: Get IP address of SSH remote user

14. März 2010 um 22:10:05 - 98 Wörter - Beitrag Nr. 780

Today I needed to find out the IP address of the currently logged on user (e.g. via SSH) on a Linux system to use it in a bash script. After searching the web for a simple tool for this task for quite some time, I finally came up with the following bash script to get me the needed information. Is there any way to obtain the IP address in a simpler way by using built in Linux tools?

  1. HOST=`who am i | sed -r "s/.*\((.*)\).*/\\1/"`
  2. IP=`host $HOST | sed -r "s/.* has address (.*)/\\1/"`

NUNIT: A Unit-Test-Framework for Natural

As part of my Master’s thesis about Test Driven Development (TDD) I developed a Unit-Test-Framework for the programming language Natural. This 4GL language from Software AG is in use for over 30 years now but the notion of automated tests has not been spread throughout the developer community. Perhaps now, with a framework for tests available, this will change ;-)

The Unit-Tests that can be run by NUNIT are subprograms and need to have the following structure. As you can see, the fixture’s and tests’ names are simply plain text to allow strings that are longer than 32 characters (the longest possible name for a Natural module) to provide meaningful names as in Behaviour Driven Development. The TestCases are parsed by NUNIT for this structure to make sure that all the needed parameters, subroutines etc. are available. To create a new TestCase all you have to do is copy the example source code and change the names and the implementation of the tests in the IF-branches.

  1. DEFINE DATA
  2. PARAMETER USING NUTESTP /* parameters for a single test
  3. LOCAL USING NUCONST /* NUNIT constants (error code etc.)
  4. LOCAL USING NUASSP /* parameters for the assertions
  5. END-DEFINE
  6. *
  7. NUTESTP.FIXTURE := ‘Example TestCase 1′ /* this TestCase’s fixture
  8. *
  9. INCLUDE NUTCTEMP /* the template method (SETUP -> TEST -> TEARDOWN)
  10. INCLUDE NUTCSTUB /* stub implementations of SETUP and TEARDOWN
  11. *
  12. DEFINE SUBROUTINE TEST /* the main test routine
  13. *
  14. **************************************************************
  15. IF NUTESTP.TEST EQ ‘comparing two equal numbers should pass’ /* name of first test
  16. **************************************************************
  17.   ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 2 2
  18. END-IF
  19. *
  20. **************************************************************
  21. IF NUTESTP.TEST EQ ‘comparing two different numbers should fail’ /* name of second test
  22. **************************************************************
  23.   ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 1 2
  24. END-IF
  25. *
  26. END-SUBROUTINE
  27. *
  28. END

The following diagram shows NUNIT’s architecture. I tried to make the adaption of NUNIT to a different platform than ours (Solaris with Natural modules saved in the file system) as easy as possible and encapsulated the OS-specific implementation into two modules that need to be changed accordingly. I have also included some basic assertions like ASSERT-NUM-EQUALS, ASSERT-STRING-EQUALS and ASSERT-ARRAY-EQUALS and some example TestCases in the source code you can download below.

Architecture of NUNIT

The TestCases’ names are defined in a user-specific workfile in the home directory (in the current configuration, but this may be changed e.g. to a DB READ). A run of all defined TestCases is as simple as calling the main program NUNIT and produces an output like the following (even with a “red bar” :-) ).

NUNIT test run (red bar)
NUNIT detailed test results

Notice the nice failure/error messages including the module names, line numbers etc. :-D

I developed NUNIT by applying TDD which means the framework tests itself. After you “installed” it (which means importing the NUNIT modules into a library of your choice, preferably a steplib if you plan on using it throughout your application) you can run a simple TESTNU to make sure NUNIT behaves as expected.

NUNIT self tests

All NUNIT modules include a detailed header comment so they should be pretty self-explanatory. But in fact, all you need to start are the programs TESTNU, NUNIT and NUSINGLE (with which you can run a single TestCase) and a TestCase like TCEXMPL1. Just try it out and let me know if you like it :-)

Downloads

How to mock System.Net.Sockets.Socket

In one of my C# projects I wanted to add tests for a class that uses System.Net.Sockets.Socket. The problem was that the class (FTPConnection) depended directly on Socket as shown in the following class diagram. So I had to find a way to get rid of this direct dependency and mock the Socket class to be able to test FTPConnection without connecting to a live FTP server for each test.

Direct dependency between FTPConnection and System.Net.Sockets.Socket

The problem with mocking System.Net.Sockets.Socket is that it implements no abstract interface and is also sealed (which means you cannot derive from the class). Therefore, you cannot mock the class directly with RhinoMocks (which otherwise would be able to mock the class directly without the need for an interface). So we have a (framework) class to whose source code we have no access (which we would need to add something like Socket : ISocket) and from wich we cannot derive a new class. How do we mock such a seemingly isolated class?

First of all, we need a common interface that we can use for mocking. The problem is that we have to make sure Socket implements this interface (let’s call it ISocket). How do we do that? We find a possible answer to that question in the GoF design patterns catalog:

The adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) translates one interface for a class into a compatible interface. (Wikipedia)

So we have to create an adapter class that implements ISocket and simply delegates all calls to its methods to an instance of Socket. Then we make sure FTPConnection uses the adapter class (or rather ISocket) instead of Socket directly and provide a constructor through which we can inject our own implementation of ISocket (e.g. the mocked one) as shown in the following class diagram.

Mocked System.Net.Sockets.Socket

It is quite a bit of effort to mock the Socket class (you have to define every needed method in ISocket and implement the delegation in SocketAdapter) but it definitely pays off! The resulting design is less coupled due to FTPConnection depending on ISocket instead of Socket directly and can be tested easily by providing a mocked socket that simulates valid or invalid FTP server responses. Examples of how to simulate FTP calls and responses using RhinoMocks are included in my example project below.

Example project in C#

I have created a small console application in C# according to the second class diagram above which demonstrates the mocking of System.Net.Sockets.Socket and produces the output shown in the following screenshot.

Console output of MockASocket

You can download the project here:

Hochzeitsfotos auf Bali

31. Oktober 2009 um 19:03:05 - 821 Wörter - Beitrag Nr. 707

Wir haben einen Tag unserer Hochzeitsreise nach Bali mit unserem Hochzeitsfotografen Michael Stange (und seiner Frau, sowie dem einheimischen Fahrer Nyoman) verbracht und wunderschöne Bilder am Strand, in den Reisfeldern und im Monkey Forest machen lassen. Hier sind die entsprechenden Blog-Einträge von Michael dazu:

Einige Fotos kann man (in hoher Auflösung) bei Flickr bewundern: Fotostream von michaelstange (26.10.2009).

Und ein paar davon gibt es auch hier zu sehen. Kommentare sind erwünscht :-D

Es war wirklich sehr heiß bei 35° Grad im Anzug und Kleid, obwohl man das auf den Bildern dank Puder nicht sieht ;-)

Das grüne Tuch, das z.B. auf dem vorletzten Bild zu sehen ist, ist ein Sarong. Den muss man auf Bali tragen, wenn man den mittleren Hof eines Tempels (in diesem Fall der Tempel im Monkey Forest) betreten möchte. Da der untere Teil des Körpers dort als unrein gilt (bzw. den Dämonen nahesteht), muss er durch den Sarong bedeckt und durch einen Gürtel vom oberen Körper abgegrenzt werden.

Die Affen im Monkey Forest heißen Makaken (wie passend zu meinem Namen ;-) ) und einer von ihnen hat hartnäckig versucht, die Perlen vom Kleid abzuknabbern, was ihm bei einer auch tatsächlich gelungen ist. Ansonsten waren die Affen aber friedlich und faul.

Sehr witzig war, dass Julia Roberts an dem Tag, an dem wir die Fotos gemacht haben, auch im Monkey Forest war (allerdings erst nachmittags als wir schon weg waren) und die einheimischen Besucher sie mit Jaqueline verwechselt haben, weshalb sie fast mehr Fotos von ihr gemacht haben als Michael. Wir Europäer/Amerikaner sehen für die Balineser wohl alle so “gleich” aus wie sie für uns ;-)

Hochzeitsfotos auf Bali 01
Hochzeitsfotos auf Bali 02
Hochzeitsfotos auf Bali 03
Hochzeitsfotos auf Bali 04
Hochzeitsfotos auf Bali 05
Hochzeitsfotos auf Bali 06
Hochzeitsfotos auf Bali 07
Hochzeitsfotos auf Bali 08
Hochzeitsfotos auf Bali 09
Hochzeitsfotos auf Bali 10

LaTeX: Zitierte Autorennamen im Fließtext in Kapitälchen schreiben

Um die Namen von Autoren, die in LaTeX mittels \citep oder \citet zitiert werden, im Fließtext (und nicht etwa im Literaturverzeichnis!) in Kapitälchen zu setzen, war bei mir eine winzige Anpassung in der Datei natdin.bst nötig (via mrunix.de).

Diese sorgt dafür, dass in die *.bbl-Datei die entsprechende Auszeichnung in die \bibitems eingetragen wird: In der Funktion output.bibitem musste der Output "\bibitem[" lediglich durch "\bibitem[\scshape{}" ersetzt werden.

Und schon sehen die Zitate aus wie hier:

LaTeX: Zitierte Autoren in Kapitälchen