includes/Linker.php: Added hook for "Media:" links
authorRobert Vogel <vogel@hallowelt.biz>
Tue, 25 Mar 2014 10:56:41 +0000 (11:56 +0100)
committerRobert Vogel <vogel@hallowelt.biz>
Mon, 14 Apr 2014 12:39:54 +0000 (14:39 +0200)
The static method 'Linker::makeMediaLinkFile' produced a HTML string
without usage of MediaWiki 'Html' class. It also lacked a hook to allow
modifications of the output HTML.

I altered the implementation and added a hook that provides a signature
and behaviour similar to the existing 'LinkerMakeExternalLink' and
'LinkBegin'/'LinkEnd' hooks. It provides all available context information
and allows modification of single attributes or the output HTML as a
whole. I have updated the 'docs/hooks.txt' file to provide proper
documentation.

Change-Id: I6d7769298a4ca6cbbf807fcebb91fb0d2222f8d8

docs/hooks.txt
includes/Linker.php

index ff1d6a1..3044fff 100644 (file)
@@ -1547,6 +1547,14 @@ before the return.
 &$attribs: the attributes to be applied.
 $linkType: The external link type
 
+'LinkerMakeMediaLinkFile': At the end of Linker::makeMediaLinkFile() just
+before the return.
+$title: the Title object that the link is pointing to
+$file: the File object or false if broken link
+&$html: the link text
+&$attribs: the attributes to be applied
+&$ret: the value to return if your hook returns false
+
 'LinksUpdate': At the beginning of LinksUpdate::doUpdate() just before the
 actual update.
 &$linksUpdate: the LinksUpdate object
index 094a304..67db78a 100644 (file)
@@ -998,12 +998,26 @@ class Linker {
                        $url = self::getUploadUrl( $title );
                        $class = 'new';
                }
-               $alt = htmlspecialchars( $title->getText(), ENT_QUOTES );
+
+               $alt = $title->getText();
                if ( $html == '' ) {
                        $html = $alt;
                }
-               $u = htmlspecialchars( $url );
-               return "<a href=\"{$u}\" class=\"$class\" title=\"{$alt}\">{$html}</a>";
+
+               $ret = '';
+               $attribs = array(
+                       'href' => $url,
+                       'class' => $class,
+                       'title' => $alt
+               );
+
+               if ( !wfRunHooks( 'LinkerMakeMediaLinkFile',
+                       array( $title, $file, &$html, &$attribs, &$ret ) ) ) {
+                       wfDebug( "Hook LinkerMakeMediaLinkFile changed the output of link with url {$url} and text {$html} to {$ret}\n", true );
+                       return $ret;
+               }
+
+               return Html::rawElement( 'a', $attribs, $html );
        }
 
        /**