Make sure to pass the right types to LinkBegin
authorAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 14 Sep 2008 00:49:52 +0000 (00:49 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Sun, 14 Sep 2008 00:49:52 +0000 (00:49 +0000)
If Linker::link() is passed an invalid Title, now that it fails gracefully, we should fail gracefully before passing over to the hook.  In theory some hooks might want to override this, but it's unlikely, because any caller that passes a non-Title is probably buggy and should be fixed anyway.  This saves unexpected fatal errors and/or having to add "if( !$target instanceof Title ) return true;" to the beginning of every function hooking into this.

Also ensure that $options is an array before passing to the hook, just for convenience.

docs/hooks.txt
includes/Linker.php

index 440b251..9f6b3ab 100644 (file)
@@ -782,7 +782,7 @@ $target: the Title that the link is pointing to
        fault values, with a value of false meaning to suppress the attribute.
 &$query: the query string to add to the generated URL (the bit after the "?"),
        in associative array form, with keys and values unescaped.
-&$options: the options.  Can include 'known', 'broken', 'noclasses'.
+&$options: array of options.  Can include 'known', 'broken', 'noclasses'.
 &$ret: the value to return if your hook returns false.
 
 'LinkEnd': Used when generating internal and interwiki links in Linker::link(),
index ea7c387..54f5633 100644 (file)
@@ -170,6 +170,11 @@ class Linker {
         */
        public function link( $target, $text = null, $customAttribs = array(), $query = array(), $options = array() ) {
                wfProfileIn( __METHOD__ );
+               if( !$target instanceof Title ) {
+                       return "<!-- ERROR -->$text";
+               }
+               $options = (array)$options;
+
                $ret = null;
                if( !wfRunHooks( 'LinkBegin', array( $this, $target, &$text,
                &$customAttribs, &$query, &$options, &$ret ) ) ) {
@@ -177,11 +182,6 @@ class Linker {
                        return $ret;
                }
 
-               if( !$target instanceof Title ) {
-                       return "<!-- ERROR -->$text";
-               }
-               $options = (array)$options;
-
                # Normalize the Title if it's a special page
                $target = $this->normaliseSpecialPage( $target );