r14680 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r14679‎ | r14680 | r14681 >
Date:14:34, 9 June 2006
Author:robchurch
Status:old
Tags:
Comment:
Parser hook extension adds a <randomimage> tag
Modified paths:
  • /trunk/extensions/RandomImage (added) (history)
  • /trunk/extensions/RandomImage/RandomImage (added) (history)
  • /trunk/extensions/RandomImage/RandomImage.php (added) (history)

Diff [purge]

Index: trunk/extensions/RandomImage/RandomImage
@@ -0,0 +1,74 @@
 2+RANDOM IMAGE EXTENSION
 3+
 4+ Version 1.0
 5+ © 2006 Rob Church
 6+
 7+This is free software licenced under the GNU General Public Licence. Please
 8+see http://www.gnu.org/copyleft/gpl.html for further details, including the
 9+full text and terms of the licence.
 10+
 11+== Overview ==
 12+
 13+ 1. Introduction
 14+ 2. Requirements
 15+ 3. Installing the extension
 16+ 4. Inserting a random image
 17+ 5. Change log
 18+ 6. Feedback
 19+
 20+== 1. Introduction ==
 21+
 22+The Random Image extension adds a <randomimage> tag to the MediaWiki parser
 23+which allows for randomisation of multimedia content on a page.
 24+
 25+When the tag is rendered, a random image is selected from those uploaded and
 26+inserted at the location of the tag. Users can select a custom size, float
 27+and provide a caption for the resulting image.
 28+
 29+At present, the output is cached in the parser cache alongside other parsed
 30+content. This is advantageous in some cases, since it will prevent the database
 31+from being crippled through repeated requests; however, it does mean that
 32+the content is not 100% dynamic. The image will change when the parser cache is
 33+expired, either automatically or by purging the page, allowing for rotation
 34+of content.
 35+
 36+== 2. Requirements ==
 37+
 38+The Random Image extension has been tested against MediaWiki 1.7alpha and 1.6 and
 39+should work with MediaWiki 1.5.
 40+
 41+== 3. Installing the extension ==
 42+
 43+To install the Random Image extension, first upload/copy the extension file
 44+RandomImage.php into your MediaWiki extensions directory. Then edit your
 45+LocalSettings.php file and add the following line:
 46+
 47+ require_once( 'extensions/RandomImage.php' );
 48+
 49+Save the file (uploading if applicable) to complete the installation.
 50+
 51+== 4. Inserting a random image ==
 52+
 53+The tag supports two basic forms:
 54+
 55+* Single, i.e. <randomimage />, which produces an image without a caption
 56+* Container, i.e. <randomimage></randomimage>, where text inside the tags
 57+ is used as a caption
 58+
 59+In addition, both tags support two additional attributes; size and float.
 60+The default size is the user's thumbnail size. Provide a numerical value
 61+to override the size, e.g.
 62+
 63+ <randomimage size="300" />
 64+
 65+Float allows overriding the float direction. You can combine attributes as
 66+with most normal HTML-style tags.
 67+
 68+== 5. Change log ==
 69+
 70+1.0
 71+ Initial release
 72+
 73+== 6. Feedback ==
 74+
 75+All feedback, bug reports, etc. welcome via <robchur@gmail.com>.
\ No newline at end of file
Index: trunk/extensions/RandomImage/RandomImage.php
@@ -0,0 +1,104 @@
 2+<?php
 3+
 4+/**
 5+ * Parser hook extension to add a <randomimage> tag
 6+ * Affected by caching, but that's probably acceptable (and useful) here
 7+ *
 8+ * @package MediaWiki
 9+ * @subpackage Extensions
 10+ * @author Rob Church <robchur@gmail.com>
 11+ * @copyright © 2006 Rob Church
 12+ * @licence GNU General Public Licence 2.0
 13+ */
 14+
 15+if( defined( 'MEDIAWIKI' ) ) {
 16+
 17+ $wgExtensionFunctions[] = 'efRandomImage';
 18+ $wgExtensionCredits['parserhook'][] = array( 'name' => 'RandomImage', 'author' => 'Rob Church' );
 19+
 20+ function efRandomImage() {
 21+ global $wgParser;
 22+ $wgParser->setHook( 'randomimage', 'efRenderRandomImage' );
 23+ }
 24+
 25+ function efRenderRandomImage( $input, $args, &$parser ) {
 26+ $image = new RandomImage( $args, $parser );
 27+ return $image->render( $input );
 28+ }
 29+
 30+ class RandomImage {
 31+
 32+ var $parser;
 33+
 34+ var $width = 'thumb';
 35+ var $float = false;
 36+
 37+ function RandomImage( $options, &$parser ) {
 38+ $this->parser =& $parser;
 39+ $this->setOptions( $options );
 40+ }
 41+
 42+ function setOptions( $options ) {
 43+ if( isset( $options['size'] ) ) {
 44+ $s = $options['size'];
 45+ if( is_numeric( $s ) )
 46+ $this->width = intval( $s ) . 'px'; # FIXME: Does "px" have a magic word equiv?
 47+ }
 48+ if( isset( $options['float'] ) ) {
 49+ $f = strtolower( $options['float'] );
 50+ wfDebugLog( 'randomimage', 'Float is ' . $f );
 51+ # FIXME: Get the real magic words
 52+ if( array_search( $f, array( 'left', 'right', 'centre' ) ) !== false )
 53+ $this->float = $f;
 54+ }
 55+ }
 56+
 57+ function pickImage() {
 58+ $dbr =& wfGetDB( DB_SLAVE );
 59+ $page = $dbr->tableName( 'page' );
 60+ $nspc = NS_IMAGE;
 61+ $rand = wfRandom();
 62+ $index = $dbr->useIndexClause( 'page_random' );
 63+ $sql = "SELECT page_title FROM {$page} {$index} WHERE page_namespace = {$nspc}
 64+ AND page_is_redirect = 0 AND page_random > {$rand}";
 65+ $res = $dbr->query( $sql, 'RandomImage::pickImage' );
 66+ if( $row = $dbr->fetchObject( $res ) ) {
 67+ $ret = Title::makeTitleSafe( $nspc, $row->page_title );
 68+ } else {
 69+ $ret = false;
 70+ }
 71+ $dbr->freeResult( $res );
 72+ return $ret;
 73+ }
 74+
 75+ function imageMarkup( &$title, $caption ) {
 76+ global $wgContLang;
 77+ $elements[] = $title->getPrefixedText();
 78+ $elements[] = $this->width;
 79+ if( $this->float )
 80+ $elements[] = $this->float;
 81+ if( $caption )
 82+ $elements[] = $caption;
 83+ return '[[' . implode( '|', $elements ) . ']]';
 84+ }
 85+
 86+ function render( $caption ) {
 87+ $title = $this->pickImage();
 88+ if( $title ) {
 89+ $wiki = $this->imageMarkup( $title, $caption );
 90+ $output = $this->parser->parse( $wiki, $this->parser->mTitle, $this->parser->mOptions, false, false );
 91+ return $output->getText();
 92+ } else {
 93+ wfDebugLog( 'randomimage', 'Image picker returned false.' );
 94+ return '';
 95+ }
 96+ }
 97+
 98+ }
 99+
 100+} else {
 101+ echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
 102+ die( 1 );
 103+}
 104+
 105+?>
\ No newline at end of file
Property changes on: trunk/extensions/RandomImage/RandomImage.php
___________________________________________________________________
Added: svn:eol-style
1106 + native

Status & tagging log