Random Images

From www.ChopStork.com

Jump to: navigation, search

My friends and I use this site too share a plethora of dumb pictures that only we find amusing and worthwile. So I wanted to extend the random page functionality for images articles. To accomplish this, I had to do the following:

  1. Modify the Image class to set the cur_random field to a random number for new (uploaded) images.
  2. Randomize all the extisting image articles (ie set the cur_random field to a random number).
  3. Create a Random Image Special Page.


See Also: Photo Album Scripts


Contents


Getting Help

Send me an E-Mail if you have questions, comments, concerns, or exclaimations. Or post help about these scripts to Help:Wiki Development Help or use the talk pages. DO NOT post help to the front page of this wiki.

Modifications to Image.php

The changes were made to the wfRecordUpload() function between lines 744 and 780.

<nowiki>
# Line ~744
wfSeedRandom();
$rand = wfRandom();

# Line ~777
$dbw->insert( 'cur',
	array(
		'cur_id' => $seqVal,
		'cur_namespace' => NS_IMAGE,
		'cur_title' => $name,
		'cur_comment' => $desc,
		'cur_user' => $wgUser->getID(),
		'cur_user_text' => $wgUser->getName(),
		'cur_timestamp' => $dbw->timestamp($now),
		'cur_is_new' => 1,
		'cur_random' => $rand,	# Enable random images - L. Bunselmeyer 4/30/2005
		'cur_text' => $textdesc,
		'inverse_timestamp' => $won,
		'cur_touched' => $dbw->timestamp($now)
	), $fname
);
</nowiki></pre>

Randomize Images Maintenance Script

Run this script at the command line to set cur_random to a random number on all articles in the image namespace.

> cd /path/to/mediawiki/maintenance/
> php randomizeImages.php

The Wrapper: randomizeImages.php

<?php
/**
 * @todo document
 * @package MediaWiki
 * @subpackage Maintenance
 */

/** */
require_once( "commandLine.inc" );
require_once( "randomizeImages.inc" );

error_reporting( E_ALL & (~E_NOTICE) );


if ($argv[2]) {
	$start = (int)$argv[2];
} else {
	$start = 1;
}

randomizeImages();

exit();

?>

The Script: randomizeImages.inc

<?php
/**
 * @todo document
 * @package MediaWiki
 * @subpackage Maintenance
 */

/** */

function randomizeImages() {
	global $wgUser, $wgTitle, $wgArticle, $wgEnablePersistentLC, $wgLinkCache, $wgOut;

	$fname = "randomizeImages";

	print("Randomizing Image Articles.\n");

	$dbw =& wfGetDB( DB_MASTER );

	# get table names for this installation	
	$cur = $dbw->tableName( 'cur' );

	# db query statement: get all image articles with no random number
	$sqlget = "SELECT cur_id, cur_namespace, cur_random FROM $cur WHERE (cur_random = 0) AND (cur_namespace=" . NS_IMAGE . ")";

	# run query and get reference to results
	$res = $dbw->query( $sqlget, $fname );

	# cycle through all the image articles
	while( $x = $dbw->fetchObject ( $res ) ) 
	{
		# generate random number
		wfSeedRandom();
		$rand = wfRandom();

		# set update values
		$values = array('cur_random' => $rand);
		$conds = array('cur_id' => $x->cur_id);

		# peform update
		$dbw->update( 'cur', $values, $conds, $fname );	

		# print acknowledgement
		print("Updated Image Article " . $x->cur_id . " (" . $rand . ").\n");
	}

	$dbw->freeResult($res);	
	
	# were done!
	print("Randomize Complete.\n");
}
?>

Random Image Special Page

Once you've randomized new and existing images, create this special page to display random images. Here's SpecialRandomimage.php. It's just like SpecialRandompage.php except modified to work on the image namespace.

Installation

Also, I did the following to intall SpecialRandompage.php and add it too the navigational links...

1. Added SpecialRandompage.php to the $wgSpecialPages array in SpecialPage.php.

'Randomimage'      => new SpecialPage( 'Randomimage' )

2. Add an element to $wgNavigationLinks in LocalSettings.php (or DefaultSettings.php)

array( 'text'=>'randompage',	'href'=>'randompage-url' )

3. Created 'randompage' and 'randompage-url' messages in the Mediawiki namespace.

The Code

SpecialRandomimage.php

<?php
/**
 * @package MediaWiki
 * @subpackage SpecialPage
 */

/**
 * Constructor
 */
function wfSpecialRandomimage() {
	global $wgOut, $wgTitle, $wgArticle, $wgExtraRandompageSQL;
	$fname = 'wfSpecialRandomimage';

	# NOTE! We use a literal constant in the SQL instead of the RAND()
	# function because RAND() will return a different value for every row
	# in the table. That's both very slow and returns results heavily
	# biased towards low values, as rows later in the table will likely
	# never be reached for comparison.
	#
	# Using a literal constant means the whole thing gets optimized on
	# the index, and the comparison is both fast and fair.
	
	# interpolation and sprintf() can muck up with locale-specific decimal separator
	$randstr = wfRandom();
	
	$db =& wfGetDB( DB_SLAVE );
	$use_index = $db->useIndexClause( 'cur_random' );
	$cur = $db->tableName( 'cur' );

	if ( $wgExtraRandompageSQL ) {
		$extra = "AND ($wgExtraRandompageSQL)";
	} else {
		$extra = '';
	}
	$sqlget = "SELECT cur_id,cur_title
		FROM $cur $use_index
		WHERE cur_namespace=6 AND cur_is_redirect=0 $extra
		AND cur_random>$randstr
		ORDER BY cur_random
		LIMIT 1";
	$res = $db->query( $sqlget, $fname );
	
	$title = null;
	if( $s = $db->fetchObject( $res ) ) {
		$title =& Title::makeTitle( NS_IMAGE, $s->cur_title );
	}	
	if( is_null( $title ) ) {
		# That's not supposed to happen :)
		$title =& Title::newFromText( wfMsg( 'mainpage' ) );
	}
	$wgOut->reportTime(); # for logfile
	$wgOut->redirect( $title->getFullUrl() );
}

?>
Personal tools