Extension talk:RandomImage/Archive

Method renderHook not loading
An email was sent to Rob Church, this is just to document the bug and provide a fix.

When installed and tested on MediaWiki 1.10.0, and 1.11.0, the extension would not render an imagebox and provided the following warning: Warning: call_user_func_array [function.call-user-func-array]: Unable to call RandomImage::renderHook in ${IP}/mediawiki-1.11.1/includes/Parser.php on line 626

The fault appears to be in the declaration of the renderHook method on line 48 of RandomImage.php If you modify;

to; the method becomes accessible and the extension works. --Zven 01:55, 12 February 2008 (UTC)

Empty captions
If the is not used and there is no starting paragraph the image rendering will screw up the page. This seems to be because a caption defined as an empty character or spaces  will get stripped out by the method removeMagnifier when it removes the div tags , in the process creating a div tag that looks like this; This tag is malformed and depending on the content can cause the left side bar to screw up. Caption content is defined in the method getCaption in RandomImage.class.php;

Instead of defining spaces as ' ', a UTF-8 character '&&#35;32;' seems to work. I modified this function to which checks that the $text is not empty and sends the UTF-8 character instead of empty or spaces that get stripped forming a div tag like this; --Zven 11:13, 12 February 2008 (UTC)

Incorrectly used &lt;randomimage> causes fatal errors
If someone puts  in an article then that particular article/page can throw fatal errors, only revertable by manipulating the query string to provide &action=edit in a long form url request for that page. The problem seems to be in the method removeMagnifier in the file RandomImage.class.php when entities such as &#38;nbsp; are present in the variable. A hack seems to be to use the DOMDocument::LoadHTML static method rather than DOMDocument::LoadXML at about line 118. This hack stops the fatal error, but the malformed tag does not work properly unless you close it by specifying --Zven 02:05, 19 February 2008 (UTC)

Float='center' does not work
In README you mention 'center' as valid value for float. That's not right, it should be 'centre'.

RandomImage.class.php:                 if( in_array( $float, array( 'left', 'right', 'centre' ) ) )

-- Landa

Center contra centre
I think it must be center in the source since it sets the.

The README is correct but there is a bug in RandomImage.class.php.

So line 47 'centre' must be replaced by 'center'.

--Tomas 21:26, 29 April 2008 (UTC)
 * All of these bug fixes are being incorporated into a patch on --Zven 22:48, 21 May 2008 (UTC)

Category Possible?
How it is possible to use only pictures from category: XYZ? The Command   Did not work.

No captions?
Is there a way to omit any caption and just have the image? 71.170.103.181 06:42, 15 December 2008 (UTC) I agree with this desire. It would be good if this could be used without the "thumb" styling and without a caption. Any help modifying to get this would be greatly appreciated. --ViciousTheJester 12:31, 21 September 2010 (UTC)

DOCTYPE/ Invalid HTML output
For some reason, this extension is adding its own doctype declaration, wrapping itself into a html/body tag and exporting unneeded comment data. I've tried to remove this but can't find where it's coming from - most likely the parse function. I just want it to output the div and image code. --75.158.70.23 10:33, 15 March 2009 (UTC)
 * I confirm this bug! --66.131.178.187 13:15, 4 May 2009 (UTC)
 * In use here, I confirm this bug. --Jlhenry 13:26, 4 May 2009 (UTC)
 * Here is the answer I received from the developer of this extension:


 * I hope that this could help someone to find a fix. --Jlhenry 14:27, 4 May 2009 (UTC)


 * This all stuff is added by function removeMagnifier (and DOM parser), I'm not aware of purpose in removing this small "magnify" image. Easiest way is to remove this function - so you can change


 * Sauron 13:30, 4 January 2010 (UTC)

False russian encoding with MW 1.12
The string of the description of a picture in Russian, writes Extension in the spoilt kind. For example, Russian word "Ошибка" is written so: "ÐžÑˆÐ¸Ð±ÐºÐ° " --80.93.116.233 10:38, 12 April 2009 (UTC)
 * Problem in French too. --66.131.178.187 21:00, 3 May 2009 (UTC)

RandomImage in a category?
Could I use in order to show a random image taken from a defined category? --Airon90 15:25, 5 May 2009 (UTC)

Possible article links?
Needing to see if it is possible to create a "link" for the random images when the choice option is utilized. For instance, I am needing anytime THIS choice is shown, it links to THIS page? Any information on process or even info on if this is possible (or in future versions) would be appreciated. --M1shawhan 22:36, 30 June 2009 (UTC)

For russian encoding
public function render { $title = $this->pickImage; if( $title instanceof Title && $this->imageExists( $title ) ) { return //$this->removeMagnifier( 				$this->parser->recursiveTagParse( base64_decode ($this->buildMarkup( $title )) //)			);		}		return ''; }

protected function buildMarkup ( $title ) { $parts[] = $title->getPrefixedText; $parts[] = 'thumb'; if( $this->width !== false ) $parts[] = "{$this->width}px"; if( $this->float ) $parts[] = $this->float; $parts[] = $this->getCaption( $title ); return ( base64_encode (, $parts ) . )); }

Can anybody put this in svn?

Thanks a lot, this hack works for Chinese too! --Zhentg 05:56, 24 February 2010 (UTC)

This fix takes care of otherwise broken German encoding as well. It still isn't a built-in. --87.163.43.117 21:45, 27 May 2011 (UTC)

Possible to do w/o extension
I haven't used this particular extension, but in reading it over, I think that you can essentially achieve the same thing with a combination of Extension:VariablesExtension, Extension:ParserFunctions, and Extension:DynamicFunctions (which are 3 very common extensions that a lot of wikis use.) Here's the code:

[[File:{{#switch:{{#var:randimage}}
 * 1=file1.jpg
 * 2=file2.jpg|thumb|{{#switch:{{#var:randimage}}
 * 1=File 1 caption
 * 2=File 2 caption}}]]

--Duke33 17:43, 4 January 2010 (UTC)

Random image IN and NOT IN certain categories
Hi,

I have made some changes to allow select certain categories to which image should belong and to which it should now. So there are new tags Here is example how it may look  Here is diff code --- RandomImage.class.php	2010-10-18 15:14:57.879993513 +0200 +++ RandomImage.class.php.new	2010-10-23 18:00:11.539388048 +0200 @@ -15,6 +15,8 @@ 	private $caption = ''; private $choices = array; +	private $categories = array; +	private $nocategories = array; /** 	 * Constructor @@ -51,6 +53,16 @@ 			if( count( $choices ) > 0 ) $this->choices = $choices; } +		if( isset( $options['categories'] ) ) { +			$categories = explode( '|', $options['categories'] ); +			if( count( $categories ) > 0 ) +				$this->categories = $categories; +		} +		if( isset( $options['nocategories'] ) ) { +			$nocategories = explode( '|', $options['nocategories'] ); +			if( count( $nocategories ) > 0 ) +				$this->nocategories = $nocategories; +		} 	} 	/** @@ -184,8 +196,10 @@ 		wfProfileIn( __METHOD__ ); $dbr = wfGetDB( DB_SLAVE ); list( $table, $conds, $opts ) = $this->getExtraSelectOptions( $dbr ); +		list( $table2, $conds2 ) = $this->getCategoriesSelectOptions( $dbr ); + 		$res = $dbr->select( -			$table, +			array_merge(array_values($table2),array_values($table)), 			array( 'page_namespace', 'page_title', @@ -194,7 +208,7 @@ 				'page_namespace' => NS_IMAGE, 'page_is_redirect' => 0, 'page_random > '. $dbr->addQuotes( wfRandom ), -			) + $conds, +			) + $conds + $conds2, __METHOD__, array( 				'ORDER BY' => 'page_random', @@ -209,6 +223,26 @@ 		} 		return null; 	} + +	protected function getCategoriesSelectOptions( $dbr ) { +		$tables=array; +		$conds=array; +		if($this->categories || $this->nocategories){ +			$tables=array('categorylinks'); +			$conds=array(100=>'page_id=cl_from'); +		} +		if($this->categories){ +			$conds+=array( +					101=>"cl_to IN ('".implode("','",$this->categories)."')", +				); +		} +		if($this->nocategories){ +			$conds+=array( +201=>"page_id NOT IN (SELECT page_id FROM categorylinks, page WHERE page_namespace = '6' AND cl_from=page_id AND (cl_to IN ('".implode("','",$this->nocategories)."')))", +				); +		} +		return array($tables,$conds); +	} 	/** 	 * Get various options for database selection @@ -218,11 +252,14 @@ 	 */ 	protected function getExtraSelectOptions( $dbr ) { 		global $wgRandomImageStrict; + 		if( $wgRandomImageStrict ) { 			list( $image, $page ) = $dbr->tableNamesN( 'image', 'page' ); 			$ind = $dbr->useIndexClause( 'page_random' ); 			return array( -				"{$page} {$ind} LEFT JOIN {$image} ON img_name = page_title", +				array( +					"{$page} {$ind} LEFT JOIN {$image} ON img_name = page_title", +				), array( 					'img_major_mime' => 'image', 				), @@ -230,7 +267,7 @@ 			); 		} else { 			return array( -				'page', +				array('page'), array, array( 					'USE INDEX' => 'page_random', This diff code can be downloaded here or you can download entire RandomImage.class.php Please be aware that excluding images by nocategories tag can be quite expensive from database point of view (categories tag is not light either). Changes made (and somehow tested) on 1.16 Mediawiki and with wgRandomImageStrict enabled. 
 * categories
 * nocategories

Joker possible?
Something like

choices="abc*"

for "all images whose name begins by abc".

DOMDocument::loadHTML
I use Random Image 1.4 on my private Mediawiki 1.18.2 with PHP 5.3.8 and see following error message:

 Strict Standards: Non-static method DOMDocument::loadHTML should not be called statically, assuming $this from incompatible context in [...]\extensions\RandomImage\RandomImage.class.php on line 114 

Now I have changed a little in  and error message doesn't appear any more: Has anyone other ideas? --Netzschrauber (talk) 12:30, 1 May 2012 (UTC)