Merge "mediawiki.searchSuggest: Show full article title as a tooltip for each suggestion"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / RandomImageGenerator.php
index 877e353..e3eea4c 100644 (file)
@@ -1,20 +1,23 @@
 <?php
-
 /**
  * RandomImageGenerator -- does what it says on the tin.
- * Requires Imagick, the ImageMagick library for PHP, or the command line equivalent (usually 'convert').
+ * Requires Imagick, the ImageMagick library for PHP, or the command line
+ * equivalent (usually 'convert').
  *
- * Because MediaWiki tests the uniqueness of media upload content, and filenames, it is sometimes useful to generate
- * files that are guaranteed (or at least very likely) to be unique in both those ways.
- * This generates a number of filenames with random names and random content (colored triangles)
+ * Because MediaWiki tests the uniqueness of media upload content, and
+ * filenames, it is sometimes useful to generate files that are guaranteed (or
+ * at least very likely) to be unique in both those ways. This generates a
+ * number of filenames with random names and random content (colored triangles).
  *
- * It is also useful to have fresh content because our tests currently run in a "destructive" mode, and don't create a fresh new wiki for each
- * test run.
- * Consequently, if we just had a few static files we kept re-uploading, we'd get lots of warnings about matching content or filenames,
- * and even if we deleted those files, we'd get warnings about archived files.
+ * It is also useful to have fresh content because our tests currently run in a
+ * "destructive" mode, and don't create a fresh new wiki for each test run.
+ * Consequently, if we just had a few static files we kept re-uploading, we'd
+ * get lots of warnings about matching content or filenames, and even if we
+ * deleted those files, we'd get warnings about archived files.
  *
- * This can also be used with a cronjob to generate random files all the time -- I use it to have a constant, never ending supply when I'm
- * testing interactively.
+ * This can also be used with a cronjob to generate random files all the time.
+ * I use it to have a constant, never ending supply when I'm testing
+ * interactively.
  *
  * @file
  * @author Neil Kandalgaonkar <neilk@wikimedia.org>
@@ -25,7 +28,6 @@
  * Can fetch a random image, or also write a number of them to disk with random filenames.
  */
 class RandomImageGenerator {
-
        private $dictionaryFile;
        private $minWidth = 400;
        private $maxWidth = 800;
@@ -34,10 +36,11 @@ class RandomImageGenerator {
        private $shapesToDraw = 5;
 
        /**
-        * Orientations: 0th row, 0th column, Exif orientation code, rotation 2x2 matrix that is opposite of orientation
-        * n.b. we do not handle the 'flipped' orientations, which is why there is no entry for 2, 4, 5, or 7. Those
-        * seem to be rare in real images anyway
-        * (we also would need a non-symmetric shape for the images to test those, like a letter F)
+        * Orientations: 0th row, 0th column, Exif orientation code, rotation 2x2
+        * matrix that is opposite of orientation. N.b. we do not handle the
+        * 'flipped' orientations, which is why there is no entry for 2, 4, 5, or 7.
+        * Those seem to be rare in real images anyway (we also would need a
+        * non-symmetric shape for the images to test those, like a letter F).
         */
        private static $orientations = array(
                array(
@@ -67,7 +70,9 @@ class RandomImageGenerator {
        );
 
        public function __construct( $options = array() ) {
-               foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight', 'shapesToDraw' ) as $property ) {
+               foreach ( array( 'dictionaryFile', 'minWidth', 'minHeight',
+                       'maxWidth', 'maxHeight', 'shapesToDraw' ) as $property
+               ) {
                        if ( isset( $options[$property] ) ) {
                                $this->$property = $options[$property];
                        }
@@ -89,12 +94,14 @@ class RandomImageGenerator {
                        }
                }
                if ( !isset( $this->dictionaryFile ) ) {
-                       throw new Exception( "RandomImageGenerator: dictionary file not found or not specified properly" );
+                       throw new Exception( "RandomImageGenerator: dictionary file not "
+                               . "found or not specified properly" );
                }
        }
 
        /**
-        * Writes random images with random filenames to disk in the directory you specify, or current working directory
+        * Writes random images with random filenames to disk in the directory you
+        * specify, or current working directory.
         *
         * @param int $number Number of filenames to write
         * @param string $format Optional, must be understood by ImageMagick, such as 'jpg' or 'gif'
@@ -128,11 +135,15 @@ class RandomImageGenerator {
                        global $wgExiv2Command;
                        if ( class_exists( 'Imagick' ) && $wgExiv2Command && is_executable( $wgExiv2Command ) ) {
                                return 'writeImageWithApi';
-                       } elseif ( $wgUseImageMagick && $wgImageMagickConvertCommand && is_executable( $wgImageMagickConvertCommand ) ) {
+                       } elseif ( $wgUseImageMagick
+                               && $wgImageMagickConvertCommand
+                               && is_executable( $wgImageMagickConvertCommand )
+                       ) {
                                return 'writeImageWithCommandLine';
                        }
                }
-               throw new Exception( "RandomImageGenerator: could not find a suitable method to write images in '$format' format" );
+               throw new Exception( "RandomImageGenerator: could not find a suitable "
+                       . "method to write images in '$format' format" );
        }
 
        /**
@@ -163,9 +174,11 @@ class RandomImageGenerator {
 
        /**
         * Generate data representing an image of random size (within limits),
-        * consisting of randomly colored and sized upward pointing triangles against a random background color
-        * (This data is used in the writeImage* methods).
-        * @return {Mixed}
+        * consisting of randomly colored and sized upward pointing triangles
+        * against a random background color. (This data is used in the
+        * writeImage* methods).
+        *
+        * @return mixed
         */
        public function getImageSpec() {
                $spec = array();
@@ -264,8 +277,10 @@ class RandomImageGenerator {
 
                $image = new Imagick();
                /**
-                * If the format is 'jpg', will also add a random orientation -- the image will be drawn rotated with triangle points
-                * facing in some direction (0, 90, 180 or 270 degrees) and a countering rotation should turn the triangle points upward again
+                * If the format is 'jpg', will also add a random orientation -- the
+                * image will be drawn rotated with triangle points facing in some
+                * direction (0, 90, 180 or 270 degrees) and a countering rotation
+                * should turn the triangle points upward again.
                 */
                $orientation = self::$orientations[0]; // default is normal orientation
                if ( $format == 'jpg' ) {
@@ -289,8 +304,10 @@ class RandomImageGenerator {
 
                $image->writeImage( $filename );
 
-               // because the above setImageOrientation call doesn't work... nor can I get an external imagemagick binary to do this either...
-               // hacking this for now (only works if you have exiv2 installed, a program to read and manipulate exif)
+               // because the above setImageOrientation call doesn't work... nor can I
+               // get an external imagemagick binary to do this either... Hacking this
+               // for now (only works if you have exiv2 installed, a program to read
+               // and manipulate exif).
                if ( $wgExiv2Command ) {
                        $cmd = wfEscapeShellArg( $wgExiv2Command )
                                . " -M "
@@ -360,7 +377,8 @@ class RandomImageGenerator {
        }
 
        /**
-        * Based on an image specification, write such an image to disk, using the command line ImageMagick program ('convert').
+        * Based on an image specification, write such an image to disk, using the
+        * command line ImageMagick program ('convert').
         *
         * Sample command line:
         *  $ convert -size 100x60 xc:rgb(90,87,45) \
@@ -369,8 +387,8 @@ class RandomImageGenerator {
         *   -draw 'fill rgb(240,12,32)  circle 50,21 50,3'  filename.png
         *
         * @param array $spec Spec describing background and shapes to draw
-        * @param string $format File format to write (unused by this method but kept so it has the same signature as
-        * writeImageWithApi)
+        * @param string $format File format to write (unused by this method but
+        *   kept so it has the same signature as writeImageWithApi).
         * @param string $filename Filename to write to
         *
         * @return bool
@@ -410,7 +428,8 @@ class RandomImageGenerator {
        }
 
        /**
-        * Get an array of random pairs of random words, like array( array( 'foo', 'bar' ), array( 'quux', 'baz' ) );
+        * Get an array of random pairs of random words, like
+        * array( array( 'foo', 'bar' ), array( 'quux', 'baz' ) );
         *
         * @param int $number Number of pairs
         * @return array two-element arrays
@@ -447,8 +466,9 @@ class RandomImageGenerator {
                }
 
                /*
-                * This algorithm obtains N random lines from a file in one single pass. It does this by replacing elements of
-                * a fixed-size array of lines, less and less frequently as it reads the file.
+                * This algorithm obtains N random lines from a file in one single pass.
+                * It does this by replacing elements of a fixed-size array of lines,
+                * less and less frequently as it reads the file.
                 */
                $fh = fopen( $filepath, "r" );
                if ( !$fh ) {