r15342 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r15341‎ | r15342 | r15343 >
Date:05:04, 5 July 2006
Author:tstarling
Status:old
Tags:
Comment:
Created $wgGenerateThumbnailOnParse, set to false to allow NFS stat calls to be suppressed.
Modified paths:
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/Image.php (modified) (history)
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -458,7 +458,7 @@
459459 function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false,
460460 $thumb = false, $manual_thumb = '' )
461461 {
462 - global $wgContLang, $wgUser, $wgThumbLimits;
 462+ global $wgContLang, $wgUser, $wgThumbLimits, $wgGenerateThumbnailOnParse;
463463
464464 $img = new Image( $nt );
465465 if ( !$img->allowInlineDisplay() && $img->exists() ) {
@@ -512,7 +512,7 @@
513513 if ( $height == false )
514514 $height = -1;
515515 if ( $manual_thumb == '') {
516 - $thumb = $img->getThumbnail( $width, $height );
 516+ $thumb = $img->getThumbnail( $width, $height, $wgGenerateThumbnailOnParse );
517517 if ( $thumb ) {
518518 // In most cases, $width = $thumb->width or $height = $thumb->height.
519519 // If not, we're scaling the image larger than it can be scaled,
@@ -563,7 +563,7 @@
564564 * $img is an Image object
565565 */
566566 function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
567 - global $wgStylePath, $wgContLang;
 567+ global $wgStylePath, $wgContLang, $wgGenerateThumbnailOnParse;
568568 $url = $img->getViewURL();
569569 $thumbUrl = '';
570570 $error = '';
@@ -588,7 +588,7 @@
589589 if ( $boxheight === false )
590590 $boxheight = -1;
591591 if ( '' == $manual_thumb ) {
592 - $thumb = $img->getThumbnail( $boxwidth, $boxheight );
 592+ $thumb = $img->getThumbnail( $boxwidth, $boxheight, $wgGenerateThumbnailOnParse );
593593 if ( $thumb ) {
594594 $thumbUrl = $thumb->getUrl();
595595 $boxwidth = $thumb->width;
Index: trunk/phase3/includes/Image.php
@@ -882,10 +882,13 @@
883883 *
884884 * @param integer $width maximum width of the generated thumbnail
885885 * @param integer $height maximum height of the image (optional)
 886+ * @param boolean $render True to render the thumbnail if it doesn't exist,
 887+ * false to just return the URL
 888+ *
886889 * @return ThumbnailImage or null on failure
887890 * @public
888891 */
889 - function getThumbnail( $width, $height=-1 ) {
 892+ function getThumbnail( $width, $height=-1, $render = true ) {
890893 if ($this->canRender()) {
891894 if ( $height > 0 ) {
892895 $this->load();
@@ -893,7 +896,23 @@
894897 $width = wfFitBoxWidth( $this->width, $this->height, $height );
895898 }
896899 }
897 - return $this->renderThumb( $width );
 900+ if ( $render ) {
 901+ return $this->renderThumb( $width );
 902+ } else {
 903+ // Don't render, just return the URL
 904+ if ( $this->validateThumbParams( $width, $height ) ) {
 905+ if ( $width == $this->width && $height == $this->height ) {
 906+ $url = $this->getURL();
 907+ } else {
 908+ list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
 909+ }
 910+ echo "Thumbnail requested, $url, $width x $height\n";
 911+ return new ThumbnailImage( $url, $width, $height );
 912+ } else {
 913+ echo "Bogus thumbnail, returning null";
 914+ return null;
 915+ }
 916+ }
898917 } else {
899918 // not a bitmap or renderable image, don't try.
900919 return $this->iconThumb();
@@ -918,38 +937,29 @@
919938 }
920939
921940 /**
922 - * Create a thumbnail of the image having the specified width.
923 - * The thumbnail will not be created if the width is larger than the
924 - * image's width. Let the browser do the scaling in this case.
925 - * The thumbnail is stored on disk and is only computed if the thumbnail
926 - * file does not exist OR if it is older than the image.
927 - * Returns an object which can return the pathname, URL, and physical
928 - * pixel size of the thumbnail -- or null on failure.
 941+ * Validate thumbnail parameters and fill in the correct height
929942 *
930 - * @return ThumbnailImage or null on failure
931 - * @private
 943+ * @param integer &$width Specified width (input/output)
 944+ * @param integer &$height Height (output only)
 945+ * @return false to indicate that an error should be returned to the user.
932946 */
933 - function renderThumb( $width, $useScript = true ) {
934 - global $wgUseSquid;
935 - global $wgSVGMaxSize, $wgMaxImageArea, $wgThumbnailEpoch;
 947+ function validateThumbParams( &$width, &$height ) {
 948+ global $wgSVGMaxSize, $wgMaxImageArea;
 949+
 950+ $this->load();
936951
937 - wfProfileIn( __METHOD__ );
938 -
939 - $width = intval( $width );
940 -
941 - $this->load();
942952 if ( ! $this->exists() )
943953 {
944954 # If there is no image, there will be no thumbnail
945 - wfProfileOut( __METHOD__ );
946 - return null;
 955+ return false;
947956 }
948 -
 957+
 958+ $width = intval( $width );
 959+
949960 # Sanity check $width
950961 if( $width <= 0 || $this->width <= 0) {
951962 # BZZZT
952 - wfProfileOut( __METHOD__ );
953 - return null;
 963+ return false;
954964 }
955965
956966 # Don't thumbnail an image so big that it will fill hard drives and send servers into swap
@@ -959,21 +969,53 @@
960970 $this->getMimeType() !== 'image/jpeg' &&
961971 $this->width * $this->height > $wgMaxImageArea )
962972 {
963 - wfProfileOut( __METHOD__ );
964 - return null;
 973+ return false;
965974 }
966975
967976 # Don't make an image bigger than the source, or wgMaxSVGSize for SVGs
968977 if ( $this->mustRender() ) {
969978 $width = min( $width, $wgSVGMaxSize );
970979 } elseif ( $width > $this->width - 1 ) {
971 - $thumb = new ThumbnailImage( $this->getURL(), $this->getWidth(), $this->getHeight() );
972 - wfProfileOut( __METHOD__ );
973 - return $thumb;
 980+ $width = $this->width;
 981+ $height = $this->height;
 982+ return true;
974983 }
975984
976985 $height = round( $this->height * $width / $this->width );
 986+ return true;
 987+ }
 988+
 989+ /**
 990+ * Create a thumbnail of the image having the specified width.
 991+ * The thumbnail will not be created if the width is larger than the
 992+ * image's width. Let the browser do the scaling in this case.
 993+ * The thumbnail is stored on disk and is only computed if the thumbnail
 994+ * file does not exist OR if it is older than the image.
 995+ * Returns an object which can return the pathname, URL, and physical
 996+ * pixel size of the thumbnail -- or null on failure.
 997+ *
 998+ * @return ThumbnailImage or null on failure
 999+ * @private
 1000+ */
 1001+ function renderThumb( $width, $useScript = true ) {
 1002+ global $wgUseSquid, $wgThumbnailEpoch;
9771003
 1004+ wfProfileIn( __METHOD__ );
 1005+
 1006+ $this->load();
 1007+ $height = -1;
 1008+ if ( !$this->validateThumbParams( $width, $height ) ) {
 1009+ # Validation error
 1010+ return null;
 1011+ }
 1012+
 1013+ if ( $width == $this->width && $height == $this->height ) {
 1014+ # validateThumbParams (or the user) wants us to return the unscaled image
 1015+ $thumb = new ThumbnailImage( $this->getURL(), $width, $height );
 1016+ wfProfileOut( __METHOD__ );
 1017+ return $thumb;
 1018+ }
 1019+
9781020 list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
9791021 if ( $isScriptUrl && $useScript ) {
9801022 // Use thumb.php to render the image
Index: trunk/phase3/includes/DefaultSettings.php
@@ -1380,6 +1380,14 @@
13811381 */
13821382 $wgIgnoreImageErrors = false;
13831383
 1384+/**
 1385+ * Allow thumbnail rendering on page view. If this is false, a valid
 1386+ * thumbnail URL is still output, but no file will be created at
 1387+ * the target location. This may save some time if you have a
 1388+ * thumb.php or 404 handler set up which is faster than the regular
 1389+ * webserver(s).
 1390+ */
 1391+$wgGenerateThumbnailOnParse = true;
13841392
13851393 /** Set $wgCommandLineMode if it's not set already, to avoid notices */
13861394 if( !isset( $wgCommandLineMode ) ) {