r24826 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24825‎ | r24826 | r24827 >
Date:15:49, 15 August 2007
Author:robchurch
Status:old
Tags:
Comment:
* Major refactor; move class into own file, update for PHP 5 etc.
* Introduce option to confine selection to a user-supplied, pipe-delimited list
Modified paths:
  • /trunk/extensions/RandomImage/README (modified) (history)
  • /trunk/extensions/RandomImage/RandomImage.class.php (added) (history)
  • /trunk/extensions/RandomImage/RandomImage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RandomImage/RandomImage.php
@@ -11,6 +11,7 @@
1212
1313 if( defined( 'MEDIAWIKI' ) ) {
1414
 15+ $wgAutoloadClasses['RandomImage'] = dirname( __FILE__ ) . '/RandomImage.class.php';
1516 $wgExtensionFunctions[] = 'efRandomImage';
1617 $wgExtensionCredits['parserhook'][] = array(
1718 'name' => 'RandomImage',
@@ -31,96 +32,9 @@
3233 */
3334 function efRandomImage() {
3435 global $wgParser;
35 - $wgParser->setHook( 'randomimage', 'efRenderRandomImage' );
 36+ $wgParser->setHook( 'randomimage', 'RandomImage::hook' );
3637 }
3738
38 - /**
39 - * Extension rendering function
40 - *
41 - * @param $text Text inside tags
42 - * @param $args Tag arguments
43 - * @param $parser Parent parser
44 - * @return string
45 - */
46 - function efRenderRandomImage( $input, $args, &$parser ) {
47 - global $wgRandomImageNoCache;
48 - if( $wgRandomImageNoCache )
49 - $parser->disableCache();
50 - $image = new RandomImage( $args, $parser );
51 - return $image->render( $input );
52 - }
53 -
54 - class RandomImage {
55 -
56 - var $parser;
57 -
58 - var $width = 'thumb';
59 - var $float = false;
60 -
61 - function RandomImage( $options, &$parser ) {
62 - $this->parser =& $parser;
63 - $this->setOptions( $options );
64 - }
65 -
66 - function setOptions( $options ) {
67 - if( isset( $options['size'] ) ) {
68 - $s = $options['size'];
69 - if( is_numeric( $s ) )
70 - $this->width = intval( $s ) . 'px'; # FIXME: Does "px" have a magic word equiv?
71 - }
72 - if( isset( $options['float'] ) ) {
73 - $float = strtolower( $options['float'] );
74 - # FIXME: Get the real magic words
75 - if( in_array( $float, array( 'left', 'right', 'centre' ) ) )
76 - $this->float = $float;
77 - }
78 - }
79 -
80 - function pickImage() {
81 - $dbr = wfGetDB( DB_SLAVE );
82 - $page = $dbr->tableName( 'page' );
83 - $nspc = NS_IMAGE;
84 - $rand = wfRandom();
85 - $index = $dbr->useIndexClause( 'page_random' );
86 - $sql = "SELECT page_title FROM {$page} {$index} WHERE page_namespace = {$nspc}
87 - AND page_is_redirect = 0 AND page_random > {$rand} ORDER BY page_random LIMIT 1";
88 - $res = $dbr->query( $sql, 'RandomImage::pickImage' );
89 - if( $row = $dbr->fetchObject( $res ) ) {
90 - $ret = Title::makeTitleSafe( $nspc, $row->page_title );
91 - } else {
92 - $ret = false;
93 - }
94 - $dbr->freeResult( $res );
95 - return $ret;
96 - }
97 -
98 - function imageMarkup( &$title, $caption ) {
99 - global $wgContLang;
100 - $elements[] = $title->getPrefixedText();
101 - $elements[] = $this->width;
102 - if( $this->float )
103 - $elements[] = $this->float;
104 - if( $caption )
105 - $elements[] = $caption;
106 - return '[[' . implode( '|', $elements ) . ']]';
107 - }
108 -
109 - function render( $caption ) {
110 - $title = $this->pickImage();
111 - # One more attempt
112 - if( !$title )
113 - $title = $this->pickImage();
114 - if( $title ) {
115 - $wiki = $this->imageMarkup( $title, $caption );
116 - $output = $this->parser->parse( $wiki, $this->parser->getTitle(), $this->parser->getOptions(), false, false );
117 - return $output->getText();
118 - } else {
119 - return '';
120 - }
121 - }
122 -
123 - }
124 -
12539 } else {
12640 echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
12741 exit( 1 );
Index: trunk/extensions/RandomImage/README
@@ -1,7 +1,7 @@
22 RANDOM IMAGE EXTENSION
33
4 - Version 1.2
5 - © 2006 Rob Church
 4+ Version 1.3
 5+ © 2006-2007 Rob Church
66
77 This is free software licenced under the GNU General Public Licence. Please
88 see http://www.gnu.org/copyleft/gpl.html for further details, including the
@@ -9,16 +9,6 @@
1010
1111 == Overview ==
1212
13 - 1. Introduction
14 - 2. Requirements
15 - 3. Installing the extension
16 - 4. Inserting a random image
17 - 5. Caching
18 - 6. Change log
19 - 7. Feedback
20 -
21 -== 1. Introduction ==
22 -
2313 The Random Image extension adds a <randomimage> tag to the MediaWiki parser
2414 which allows for randomisation of multimedia content on a page.
2515
@@ -26,39 +16,44 @@
2717 inserted at the location of the tag. Users can select a custom size, float
2818 and provide a caption for the resulting image.
2919
30 -== 2. Requirements ==
 20+== Requirements ==
3121
32 -The RandomImage extension requires MediaWiki 1.7.0 or above.
 22+This version of the RandomImage extension requires MediaWiki 1.10.0 or above.
3323
34 -== 3. Installing the extension ==
 24+== Installation ==
3525
36 -To install the extension, place all extension files into a RandomImage
37 -directory within your MediaWiki extensions/ directory, then edit
38 -LocalSettings.php and add the following line
 26+To install the extension, place all extension files into a `RandomImage`
 27+directory within your MediaWiki `extensions` directory, then edit
 28+`LocalSettings.php` and add the following line
3929
4030 require_once( "{$IP}/extensions/RandomImage/RandomImage.php" );
4131
4232 Installation can be verified through the Special:Version page on the wiki.
4333
44 -== 4. Inserting a random image ==
 34+== Use ==
4535
46 -The tag supports two basic forms:
47 -
48 -* Single, i.e. <randomimage />, which produces an image without a caption
49 -* Container, i.e. <randomimage></randomimage>, where text inside the tags
50 - is used as a caption
51 -
52 -In addition, both tags support two additional attributes; size and float.
53 -The default size is the user's thumbnail size. Provide a numerical value
54 -to override the size, e.g.
 36+The tag can be used in two forms:
5537
56 - <randomimage size="300" />
57 -
58 -Float allows overriding the float direction. You can combine attributes as
59 -with most normal HTML-style tags.
 38+ <randomimage />
 39+ Produces an image without a caption
 40+ <randomimage></randomimage>
 41+ Produces an image with a caption (text inside the tags)
 42+
 43+The tag also supports the following attributes:
6044
61 -== 5. Caching ==
 45+ size
 46+ Set the image width (default is the user's thumbnail width)
 47+ float
 48+ Float the image; valid values are 'left', 'center' and 'right'
 49+ choices
 50+ Confines image selection to a pipe-delimited list of images
 51+
 52+For example:
6253
 54+ <randomimage size="100" float="left" choices="Apple.jpg|Pear.jpg" />
 55+
 56+== Caching ==
 57+
6358 By default, the output is cached in the regular parser cache. This can be
6459 advantageous on larger sites which need to use caching to reduce the effects
6560 of load, however, it means that content is not 100% dynamic.
@@ -68,22 +63,27 @@
6964 the extension file). This will affect all users, and might incur additional
7065 overhead on pageviews.
7166
72 -== 6. Change log ==
 67+== Change log ==
7368
74 -09/06/2006
75 -Version 1.0
76 - Initial release
77 -
78 -17/12/2006
 69+August 15, 2007
 70+Version 1.3
 71+ Major refactor; class split into own file
 72+ Add option to confine image selection to user-defined choices
 73+
 74+June 6, 2007
 75+Version 1.2
 76+ Bug fix and minor code tweaks
 77+ If no image is selected in the first pass, a further attempt is made
 78+
 79+December 17, 2006
7980 Version 1.1
8081 Introduced $wgRandomImageNoCache to disable parser cache on pages using
8182 the tag; defaults to off
 83+
 84+June 9, 2006
 85+Version 1.0
 86+ Initial release
8287
83 -04/06/2007
84 -Version 1.2
85 - Bug fix and minor code tweaks
86 - If no image is selected in the first pass, a further attempt is made
87 -
88 -== 7. Feedback ==
 88+== Feedback ==
8989
90 -All feedback, bug reports, etc. welcome via <robchur@gmail.com>.
\ No newline at end of file
 90+All feedback, bug reports, etc. welcome via robchur {at} gmail {dot} com.
\ No newline at end of file
Index: trunk/extensions/RandomImage/RandomImage.class.php
@@ -0,0 +1,173 @@
 2+<?php
 3+
 4+/**
 5+ * Class file for the RandomImage extension
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Rob Church <robchur@gmail.com>
 9+ */
 10+class RandomImage {
 11+
 12+ private $parser = null;
 13+
 14+ private $width = 'thumb';
 15+ private $float = false;
 16+ private $caption = '';
 17+
 18+ private $choices = array();
 19+
 20+ /**
 21+ * Constructor
 22+ *
 23+ * @param Parser $parser Parent parser
 24+ * @param array $options Initial options
 25+ * @param string $caption Caption text
 26+ */
 27+ public function __construct( $parser, $options, $caption ) {
 28+ $this->parser = $parser;
 29+ $this->caption = $caption;
 30+ $this->setOptions( $options );
 31+ }
 32+
 33+ /**
 34+ * Extract applicable options from tag attributes
 35+ *
 36+ * @param array $options Tag attributes
 37+ */
 38+ protected function setOptions( $options ) {
 39+ if( isset( $options['size'] ) ) {
 40+ $size = intval( $options['size'] );
 41+ if( $size > 0 )
 42+ $this->width = "{$size}px";
 43+ }
 44+ if( isset( $options['float'] ) ) {
 45+ $float = strtolower( $options['float'] );
 46+ // TODO: Use magic words instead
 47+ if( in_array( $float, array( 'left', 'right', 'centre' ) ) )
 48+ $this->float = $float;
 49+ }
 50+ if( isset( $options['choices'] ) ) {
 51+ $choices = explode( '|', $options['choices'] );
 52+ if( count( $choices ) > 0 )
 53+ $this->choices = $choices;
 54+ }
 55+ }
 56+
 57+ /**
 58+ * Render a random image
 59+ *
 60+ * @return string
 61+ */
 62+ public function render() {
 63+ $title = $this->pickImage();
 64+ if( $title instanceof Title ) {
 65+ $file = wfFindFile( $title );
 66+ if( $file->exists() ) {
 67+ return $this->parser->parse(
 68+ $this->buildMarkup( $file ),
 69+ $this->parser->getTitle(),
 70+ $this->parser->getOptions(),
 71+ false,
 72+ false
 73+ )->getText();
 74+ }
 75+ }
 76+ return '';
 77+ }
 78+
 79+ /**
 80+ * Prepare image markup for the given image
 81+ *
 82+ * @param File $image Image
 83+ * @return string
 84+ */
 85+ protected function buildMarkup( $image ) {
 86+ $parts[] = $image->getTitle()->getPrefixedText();
 87+ $parts[] = $this->width;
 88+ if( $this->float )
 89+ $parts[] = $this->float;
 90+ if( $this->caption )
 91+ $parts[] = $this->caption;
 92+ return '[[' . implode( '|', $parts ) . ']]';
 93+ }
 94+
 95+ /**
 96+ * Select a random image
 97+ *
 98+ * @return Title
 99+ */
 100+ protected function pickImage() {
 101+ if( count( $this->choices ) > 0 ) {
 102+ return $this->pickFromChoices();
 103+ } else {
 104+ $pick = $this->pickFromDatabase();
 105+ if( !$pick instanceof Title )
 106+ $pick = $this->pickFromDatabase();
 107+ return $pick;
 108+ }
 109+ }
 110+
 111+ /**
 112+ * Select a random image from the choices given
 113+ *
 114+ * @return Title
 115+ */
 116+ protected function pickFromChoices() {
 117+ $name = count( $this->choices ) > 1
 118+ ? $this->choices[ array_rand( $this->choices ) ]
 119+ : $this->choices[0];
 120+ return Title::makeTitleSafe( NS_IMAGE, $name );
 121+ }
 122+
 123+ /**
 124+ * Select a random image from the database
 125+ *
 126+ * @return Title
 127+ */
 128+ protected function pickFromDatabase() {
 129+ wfProfileIn( __METHOD__ );
 130+ $dbr = wfGetDB( DB_SLAVE );
 131+ $res = $dbr->select(
 132+ 'page',
 133+ array(
 134+ 'page_namespace',
 135+ 'page_title',
 136+ ),
 137+ array(
 138+ 'page_namespace' => NS_IMAGE,
 139+ 'page_is_redirect' => 0,
 140+ 'page_random > ' . $dbr->addQuotes( wfRandom() ),
 141+ ),
 142+ __METHOD__,
 143+ array(
 144+ 'USE INDEX' => 'page_random',
 145+ 'ORDER BY' => 'page_random',
 146+ 'LIMIT' => 1,
 147+ )
 148+ );
 149+ wfProfileOut( __METHOD__ );
 150+ if( $dbr->numRows( $res ) > 0 ) {
 151+ $row = $dbr->fetchObject( $res );
 152+ $dbr->freeResult( $res );
 153+ return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
 154+ }
 155+ return null;
 156+ }
 157+
 158+ /**
 159+ * Parser hook callback
 160+ *
 161+ * @param string $input Tag input
 162+ * @param array $args Tag attributes
 163+ * @param Parser $parser Parent parser
 164+ * @return string
 165+ */
 166+ public static function hook( $input, $args, $parser ) {
 167+ global $wgRandomImageNoCache;
 168+ if( $wgRandomImageNoCache )
 169+ $parser->disableCache();
 170+ $random = new RandomImage( $parser, $args, $input );
 171+ return $random->render();
 172+ }
 173+
 174+}
\ No newline at end of file
Property changes on: trunk/extensions/RandomImage/RandomImage.class.php
___________________________________________________________________
Added: svn:eol-style
1175 + native

Status & tagging log