Plugin Directory

Changeset 2525480


Ignore:
Timestamp:
05/03/2021 05:21:52 PM (5 years ago)
Author:
foadyousefi
Message:

New release 0.3.3

Location:
redisearch
Files:
1 deleted
34 edited
46 copied

Legend:

Unmodified
Added
Removed
  • redisearch/tags/0.3.3/README.md

    r2455651 r2525480  
    5353
    5454### Changelog
     55
     56##### 0.3.3
     57* **Added**: Ability to connect via UNIX sockets
     58
    5559
    5660##### 0.3.2
  • redisearch/tags/0.3.3/readme.txt

    r2455651 r2525480  
    66Tags: search, redisearch, redis, fuzzy, aggregation, searching, autosuggest, suggest, advanced search, woocommerce
    77Requires at least: 5.0
    8 Tested up to: 5.6
    9 Stable tag: 0.3.2
     8Tested up to: 5.7
     9Stable tag: 0.3.3
    1010Requires PHP: 7.2
    1111License: GPLv2 or later
     
    6666
    6767== Changelog ==
     68
     69= 0.3.3 =
     70* Added: Ability to connect via UNIX sockets
    6871
    6972= 0.3.2 =
  • redisearch/tags/0.3.3/src/Admin/Admin.php

    r2455651 r2525480  
    151151  public static function RedisServerConf() {
    152152    Fields::add( 'header', null, __( 'Redis server configurations', 'wp-redisearch' ) );
    153     Fields::add( 'text', 'wp_redisearch_server', __( 'Redis server', 'wp-redisearch' ), __( 'Redis server url, usually it is 127.0.0.1', 'wp-redisearch' ) );
     153    Fields::add( 'select', 'wp_redisearch_connection_scheme', __( 'Server communication scheme', 'wp-redisearch' ), __( 'Choose Redis server communication scheme.', 'wp-redisearch'), array('tcp' => 'TCP', 'unix' => 'UNIX') );
     154    Fields::add( 'text', 'wp_redisearch_server', __( 'Redis server/path', 'wp-redisearch' ), __( 'Redis server url, usually it is 127.0.0.1 and in case of using UNIX socket, the socket path.', 'wp-redisearch' ) );
    154155    Fields::add( 'text', 'wp_redisearch_port', __( 'Redis port', 'wp-redisearch' ), __( 'Redis port number, by default it is 6379', 'wp-redisearch' ) );
    155156    Fields::add( 'password', 'wp_redisearch_password', __( 'Redis server password', 'wp-redisearch' ), __( 'If your redis server is not password protected, leave this field blank', 'wp-redisearch' ) );
  • redisearch/tags/0.3.3/src/RediSearch/Client.php

    r2452159 r2525480  
    1919      Settings::RedisPort(),
    2020      Settings::RedisPassword(),
    21       0
     21      0,
     22      Settings::RedisScheme()
    2223    );
    2324  }
  • redisearch/tags/0.3.3/src/RedisRaw/PredisAdapter.php

    r2452159 r2525480  
    1717  public $redis;
    1818 
    19   public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null): RedisRawClientInterface {
    20     $this->redis = new Client([
    21       'scheme' => 'tcp',
    22       'host' => $hostname,
    23       'port' => $port,
    24       'database' => $db,
    25       'password' => $password,
    26     ]);
     19  public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null, $scheme = 'tcp'): RedisRawClientInterface {
     20    $clientArgs = array();
     21    if ( $scheme === 'tcp' ) {
     22      $clientArgs = array(
     23      'scheme'    => 'tcp',
     24      'host'      => $hostname,
     25      'port'      => $port,
     26      'database'  => $db,
     27      'password'  => $password,
     28      );
     29    } else {
     30      $clientArgs = array(
     31        'scheme'    => 'unix',
     32        'path'      => $host
     33      );
     34    }
     35    $this->redis = new Client( $clientArgs );
    2736    $this->redis->connect();
    2837    return $this;
  • redisearch/tags/0.3.3/src/Settings.php

    r2452159 r2525480  
    4545   
    4646    return isset( $redis_server ) && !empty( $redis_server ) ? $redis_server : '127.0.0.1';
     47  }
     48
     49  /**
     50  * Using UNIX socket for communicating with Redis server or not.
     51  * @since    0.1.0
     52  * @param
     53  * @return bool    $unixServer
     54  */
     55  public static function RedisScheme() {
     56    /**
     57     * First we try to get the WP_REDIS_SCHEME option from wp-config.php
     58     * @since 0.3.3
     59     */
     60    $redis_server = ( defined( 'WP_REDIS_SCHEME' ) ) ? WP_REDIS_SCHEME : get_option( 'wp_redisearch_connection_scheme' );
     61
     62    return get_option( 'wp_redisearch_connection_scheme' );
    4763  }
    4864
  • redisearch/tags/0.3.3/vendor/foadyousefi/seven-fields/src/Fields/Fields.php

    r2013762 r2525480  
    7777          update_option( $name, $value );
    7878          break;
     79        case 'select':
     80          update_option( $name, $value );
     81          break;
    7982        case 'checkbox':
    8083          $value = isset( $value ) ? true : false;
  • redisearch/tags/0.3.3/vendor/foadyousefi/seven-fields/src/Fields/Select.php

    r1949960 r2525480  
    3939    self::$description = $description;
    4040    self::$options = $options;
     41    // Save value in as wp_option in database
     42    parent::save( self::$name, 'select' );
    4143    return $this->field_html();
    4244  }
  • redisearch/tags/0.3.3/vendor/front/redisearch/src/RedisRaw/PredisAdapter.php

    r2452159 r2525480  
    1717  public $redis;
    1818 
    19   public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null): RedisRawClientInterface {
    20     $this->redis = new Client([
    21       'scheme' => 'tcp',
    22       'host' => $hostname,
    23       'port' => $port,
    24       'database' => $db,
    25       'password' => $password,
    26     ]);
     19  public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null, $scheme = 'tcp'): RedisRawClientInterface {
     20    $clientArgs = array(
     21      'database'  => $db,
     22      'password'  => $password
     23    );
     24    if ( $scheme === 'tcp' ) {
     25      $clientArgs = array(
     26        'scheme'    => 'tcp',
     27        'port'      => $port,
     28        'host'      => $hostname
     29      );
     30    } else if ( $scheme === 'unix' ) {
     31      $clientArgs = array(
     32        'scheme'    => 'unix',
     33        'path'      => $host
     34      );
     35    }
     36    $this->redis = new Client( $clientArgs );
    2737    $this->redis->connect();
    2838    return $this;
  • redisearch/tags/0.3.3/vendor/front/redisearch/src/Setup.php

    r2452159 r2525480  
    1717   * @return PredisAdapter
    1818   */
    19   public static function connect( $server = '127.0.0.1', $port = 6379, $password = null, $database = 0 ) {
     19  public static function connect( $server = '127.0.0.1', $port = 6379, $password = null, $database = 0, $scheme = 'tcp' ) {
    2020    // Connect to server
    21     $client = ( new PredisAdapter() )->connect( $server, $port, $database, $password );
     21    $client = ( new PredisAdapter() )->connect( $server, $port, $database, $password, $database, $scheme );
    2222    return $client;
    2323  }
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/src/Client.php

    r2002534 r2525480  
    55use Closure;
    66use Exception;
     7use stdClass;
    78
    89use Vaites\ApacheTika\Clients\CLIClient;
    910use Vaites\ApacheTika\Clients\WebClient;
     11use Vaites\ApacheTika\Metadata\Metadata;
     12use Vaites\ApacheTika\Metadata\MetadataInterface;
    1013
    1114/**
     
    1316 *
    1417 * @author  David Martínez <contacto@davidmartinez.net>
    15  * @link    http://wiki.apache.org/tika/TikaJAXRS
    16  * @link    https://tika.apache.org/1.10/formats.html
     18 * @link    https://tika.apache.org/1.24/formats.html
    1719 */
    1820abstract class Client
    1921{
    20     /**
    21      * List of supported Apache Tika versions
     22    protected const MODE = null;
     23
     24    /**
     25     * Checked flag
     26     *
     27     * @var bool
     28     */
     29    protected $checked = false;
     30
     31    /**
     32     * Response using callbacks
     33     *
     34     * @var string
     35     */
     36    protected $response = null;
     37
     38    /**
     39     * Platform (unix or win)
     40     *
     41     * @var string
     42     */
     43    protected $platform = null;
     44
     45    /**
     46     * Cached responses to avoid multiple request for the same file.
    2247     *
    2348     * @var array
    2449     */
    25     protected static $supportedVersions =
    26     [
    27         '1.7', '1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.19.1'
    28     ];
    29 
    30     /**
    31      * Response using callbacks
    32      *
    33      * @var string
    34      */
    35     protected $response = null;
    36 
    37     /**
    38      * Cached responses to avoid multiple request for the same file.
    39      *
    40      * @var array
    41      */
    4250    protected $cache = [];
    4351
    4452    /**
     53     * Text encoding
     54     *
     55     * @var string|null
     56     */
     57    protected $encoding = null;
     58
     59    /**
    4560     * Callback called on secuential read
    4661     *
    47      * @var \Closure
     62     * @var callable|null
    4863     */
    4964    protected $callback = null;
    5065
    5166    /**
     67     * Enable or disable appending when using callback
     68     *
     69     * @var bool
     70     */
     71    protected $callbackAppend = true;
     72
     73    /**
    5274     * Size of chunks for callback
    5375     *
     
    6486
    6587    /**
    66      * Get a class instance
    67      *
    68      * @param   string  $param1     path or host
    69      * @param   int     $param2     Java binary path or port for web client
    70      * @param   array   $options    options for cURL request
    71      * @return  \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
    72      * @throws  \Exception
    73      */
    74     public static function make($param1 = null, $param2 = null, $options = [])
    75     {
    76         if (preg_match('/\.jar$/', func_get_arg(0)))
    77         {
    78             return new CLIClient($param1, $param2);
     88     * Configure client
     89     */
     90    public function __construct()
     91    {
     92        $this->platform = defined('PHP_WINDOWS_VERSION_MAJOR') ? 'win' : 'unix';
     93    }
     94
     95    /**
     96     * Get a class instance throwing an exception if check fails
     97     *
     98     * @param string     $param1 path or host
     99     * @param string|int $param2 Java binary path or port for web client
     100     * @param array      $options options for cURL request
     101     * @param bool       $check check JAR file or server connection
     102     * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
     103     * @throws \Exception
     104     */
     105    public static function make(string $param1 = null, $param2 = null, array $options = [], bool $check = true): Client
     106    {
     107        if(preg_match('/\.jar$/', func_get_arg(0)))
     108        {
     109            return new CLIClient($param1, $param2, $check);
    79110        }
    80111        else
    81112        {
    82             return new WebClient($param1, $param2, $options);
    83         }
     113            return new WebClient($param1, $param2, $options, $check);
     114        }
     115    }
     116
     117    /**
     118     * Get a class instance delaying the check
     119     *
     120     * @param string $param1 path or host
     121     * @param int    $param2 Java binary path or port for web client
     122     * @param array  $options options for cURL request
     123     * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
     124     * @throws \Exception
     125     */
     126    public static function prepare($param1 = null, $param2 = null, $options = []): Client
     127    {
     128        return self::make($param1, $param2, $options, false);
     129    }
     130
     131    /**
     132     * Get the encoding
     133     */
     134    public function getEncoding(): ?string
     135    {
     136        return $this->encoding;
     137    }
     138
     139    /**
     140     * Set the encoding
     141     *
     142     * @throws \Exception
     143     */
     144    public function setEncoding(string $encoding): self
     145    {
     146        if(!empty($encoding))
     147        {
     148            $this->encoding = $encoding;
     149        }
     150        else
     151        {
     152            throw new Exception('Invalid encoding');
     153        }
     154
     155        return $this;
    84156    }
    85157
    86158    /**
    87159     * Get the callback
    88      *
    89      * @return  \Closure|null
    90      */
    91     public function getCallback()
     160     */
     161    public function getCallback(): ?Closure
    92162    {
    93163        return $this->callback;
     
    97167     * Set the callback (callable or closure) for call on secuential read
    98168     *
    99      * @param   mixed   $callback
    100      * @return  $this
    101      * @throws  \Exception
    102      */
    103     public function setCallback($callback)
    104     {
    105         if($callback instanceof Closure)
    106         {
     169     * @throws \Exception
     170     */
     171    public function setCallback(callable $callback, bool $append = true): self
     172    {
     173        if($callback instanceof Closure || is_array($callback))
     174        {
     175            $this->callbackAppend = (bool) $append;
    107176            $this->callback = $callback;
    108177        }
    109         elseif(is_callable($callback))
    110         {
    111             $this->callback = function($chunk) use($callback)
     178        elseif(is_string($callback))
     179        {
     180            $this->callbackAppend = (bool) $append;
     181            $this->callback = function($chunk) use ($callback)
    112182            {
    113183                return call_user_func_array($callback, [$chunk]);
     
    124194    /**
    125195     * Get the chunk size
    126      *
    127      * @return  int
    128      */
    129     public function getChunkSize()
     196     */
     197    public function getChunkSize(): int
    130198    {
    131199        return $this->chunkSize;
     
    135203     * Set the chunk size for secuential read
    136204     *
    137      * @param   int     $size
    138      * @return  $this
    139      * @throws  \Exception
    140      */
    141     public function setChunkSize($size)
    142     {
    143         if(static::MODE == 'cli' && is_numeric($size))
    144         {
    145             $this->chunkSize = (int)$size;
    146         }
    147         elseif(static::MODE == 'web')
     205     * @throws \Exception
     206     */
     207    public function setChunkSize(int $size): self
     208    {
     209        if(static::MODE == 'cli')
     210        {
     211            $this->chunkSize = $size;
     212        }
     213        else
    148214        {
    149215            throw new Exception('Chunk size is not supported on web mode');
    150216        }
    151         else
    152         {
    153             throw new Exception("$size is not a valid chunk size");
    154         }
    155217
    156218        return $this;
     
    159221    /**
    160222     * Get the remote download flag
    161      *
    162      * @return  bool
    163      */
    164     public function getDownloadRemote()
     223     */
     224    public function getDownloadRemote(): bool
    165225    {
    166226        return $this->downloadRemote;
     
    169229    /**
    170230     * Set the remote download flag
    171      *
    172      * @param   bool    $download
    173      * @return  $this
    174      */
    175     public function setDownloadRemote($download)
     231     */
     232    public function setDownloadRemote(bool $download): self
    176233    {
    177234        $this->downloadRemote = (bool) $download;
     
    183240     * Gets file metadata
    184241     *
    185      * @param   string  $file
    186      * @return  \Vaites\ApacheTika\Metadata\Metadata
    187      * @throws  \Exception
    188      */
    189     public function getMetadata($file)
    190     {
    191         return $this->request('meta', $file);
     242     * @throws \Exception
     243     */
     244    public function getMetadata(string $file): MetadataInterface
     245    {
     246        $response = $this->parseJsonResponse($this->request('meta', $file));
     247
     248        if($response instanceof stdClass == false)
     249        {
     250            throw new Exception("Unexpected metadata response for $file");
     251        }
     252
     253        return Metadata::make($response, $file);
     254    }
     255
     256    /**
     257     * Gets recursive file metadata where the returned array indexes are the file name.
     258     *
     259     * Example: for a sample.zip with an example.doc file, the return array looks like if be defined as:
     260     *
     261     *  [
     262     *      'sample.zip' => new Metadata()
     263     *      'sample.zip/example.doc' => new DocumentMetadata()
     264     *  ]
     265     *
     266     * @link https://cwiki.apache.org/confluence/display/TIKA/TikaServer#TikaServer-RecursiveMetadataandContent
     267     * @throws \Exception
     268     */
     269    public function getRecursiveMetadata(string $file, ?string $format = 'ignore'): array
     270    {
     271        if(in_array($format, ['text', 'html', 'ignore']) == false)
     272        {
     273            throw new Exception("Unknown recursive type (must be text, html, ignore or null)");
     274        }
     275
     276        $response = $this->parseJsonResponse($this->request("rmeta/$format", $file));
     277
     278        if(is_array($response) == false)
     279        {
     280            throw new Exception("Unexpected metadata response for $file");
     281        }
     282
     283        $metadata = [];
     284
     285        foreach($response as $item)
     286        {
     287            $name = basename($file);
     288            if(isset($item->{'X-TIKA:embedded_resource_path'}))
     289            {
     290                $name .= $item->{'X-TIKA:embedded_resource_path'};
     291            }
     292
     293            $metadata[$name] = Metadata::make($item, $file);
     294        }
     295
     296        return $metadata;
    192297    }
    193298
     
    195300     * Detect language
    196301     *
    197      * @param   string  $file
    198      * @return  string
    199      * @throws  \Exception
    200      */
    201     public function getLanguage($file)
     302     * @throws \Exception
     303     */
     304    public function getLanguage(string $file): string
    202305    {
    203306        return $this->request('lang', $file);
     
    207310     * Detect MIME type
    208311     *
    209      * @param   string  $file
    210      * @return  string
    211      * @throws \Exception
    212      */
    213     public function getMIME($file)
     312     * @throws \Exception
     313     */
     314    public function getMIME(string $file): string
    214315    {
    215316        return $this->request('mime', $file);
     
    219320     * Extracts HTML
    220321     *
    221      * @param   string  $file
    222      * @param   mixed   $callback
    223      * @return  string
    224      * @throws  \Exception
    225      */
    226     public function getHTML($file, $callback = null)
     322     * @throws \Exception
     323     */
     324    public function getHTML(string $file, callable $callback = null, bool $append = true): string
    227325    {
    228326        if(!is_null($callback))
    229327        {
    230             $this->setCallback($callback);
     328            $this->setCallback($callback, $append);
    231329        }
    232330
     
    235333
    236334    /**
     335     * Extracts XHTML
     336     *
     337     * @throws \Exception
     338     */
     339    public function getXHTML(string $file, callable $callback = null, bool $append = true): string
     340    {
     341        if(!is_null($callback))
     342        {
     343            $this->setCallback($callback, $append);
     344        }
     345
     346        return $this->request('xhtml', $file);
     347    }
     348
     349    /**
    237350     * Extracts text
    238351     *
    239      * @param   string  $file
    240      * @param   mixed   $callback
    241      * @return  string
    242      * @throws  \Exception
    243      */
    244     public function getText($file, $callback = null)
     352     * @throws \Exception
     353     */
     354    public function getText(string $file, callable $callback = null, bool $append = true): string
    245355    {
    246356        if(!is_null($callback))
    247357        {
    248             $this->setCallback($callback);
     358            $this->setCallback($callback, $append);
    249359        }
    250360
     
    255365     * Extracts main text
    256366     *
    257      * @param   string  $file
    258      * @param   mixed   $callback
    259      * @return  string
    260      * @throws  \Exception
    261      */
    262     public function getMainText($file, $callback = null)
     367     * @throws \Exception
     368     */
     369    public function getMainText(string $file, callable $callback = null, bool $append = true): string
    263370    {
    264371        if(!is_null($callback))
    265372        {
    266             $this->setCallback($callback);
     373            $this->setCallback($callback, $append);
    267374        }
    268375
     
    271378
    272379    /**
    273      * Returns the supported MIME types
    274      *
    275      * @return  string
    276      * @throws  \Exception
    277      */
    278     public function getSupportedMIMETypes()
    279     {
    280         return $this->request('mime-types');
    281     }
    282 
    283     /**
    284      * Returns the available detectors
    285      *
    286      * @return  string
    287      * @throws  \Exception
    288      */
    289     public function getAvailableDetectors()
    290     {
    291         return $this->request('detectors');
    292     }
    293 
    294     /**
    295      * Returns the available parsers
    296      *
    297      * @return  string
    298      * @throws  \Exception
    299      */
    300     public function getAvailableParsers()
    301     {
    302         return $this->request('parsers');
    303     }
    304 
    305     /**
    306380     * Returns current Tika version
    307381     *
    308      * @return  string
    309      * @throws  \Exception
    310      */
    311     public function getVersion()
     382     * @throws \Exception
     383     */
     384    public function getVersion(): string
    312385    {
    313386        return $this->request('version');
     
    317390     * Return the list of Apache Tika supported versions
    318391     *
    319      * @return array
    320      */
    321     public static function getSupportedVersions()
    322     {
    323         return self::$supportedVersions;
     392     * @throws \Exception
     393     */
     394    public function getSupportedVersions(): array
     395    {
     396        static $versions = null;
     397
     398        if(is_null($versions))
     399        {
     400            $composer = json_decode(file_get_contents(dirname(__DIR__) . '/composer.json'), true);
     401            $versions = $composer['extra']['supported-versions'] ?? null;
     402
     403            if(empty($versions))
     404            {
     405                throw new Exception("An error ocurred trying to read package's composer.json file");
     406            }
     407        }
     408
     409        return $versions;
     410    }
     411
     412    /**
     413     * Sets the checked flag
     414     */
     415    public function setChecked(bool $checked): self
     416    {
     417        $this->checked = (bool) $checked;
     418
     419        return $this;
     420    }
     421
     422    /**
     423     * Checks if instance is checked
     424     */
     425    public function isChecked(): bool
     426    {
     427        return $this->checked;
     428    }
     429
     430    /**
     431     * Check if a response is cached
     432     */
     433    protected function isCached(string $type, string $file): bool
     434    {
     435        return isset($this->cache[sha1($file)][$type]);
     436    }
     437
     438    /**
     439     * Get a cached response
     440     */
     441    protected function getCachedResponse(string $type, string $file)
     442    {
     443        return $this->cache[sha1($file)][$type] ?? null;
     444    }
     445
     446    /**
     447     * Check if a request type must be cached
     448     */
     449    protected function isCacheable(string $type): bool
     450    {
     451        return in_array($type, ['lang', 'meta']);
     452    }
     453
     454    /**
     455     * Caches a response
     456     */
     457    protected function cacheResponse(string $type, $response, string $file): bool
     458    {
     459        $this->cache[sha1($file)][$type] = $response;
     460
     461        return true;
    324462    }
    325463
    326464    /**
    327465     * Checks if a specific version is supported
    328      *
    329      * @param   string  $version
    330      * @return  bool
    331      */
    332     public static function isVersionSupported($version)
    333     {
    334         return in_array($version, self::getSupportedVersions());
     466     */
     467    public function isVersionSupported(string $version): bool
     468    {
     469        return in_array($version, $this->getSupportedVersions());
     470    }
     471
     472    /**
     473     * Check if a mime type is supported
     474     *
     475     * @param string $mime
     476     * @return bool
     477     * @throws \Exception
     478     */
     479    public function isMIMETypeSupported(string $mime): bool
     480    {
     481        return array_key_exists($mime, $this->getSupportedMIMETypes());
    335482    }
    336483
     
    338485     * Check the request before executing
    339486     *
    340      * @param   string  $type
    341      * @param   string  $file
    342      * @return  string
    343      * @throws  \Exception
    344      */
    345     public function checkRequest($type, $file)
     487     * @throws \Exception
     488     */
     489    public function checkRequest(string $type, string $file = null): ?string
    346490    {
    347491        // no checks for getters
     
    349493        {
    350494            //
    351         }
    352         // invalid local file
     495        } // invalid local file
    353496        elseif(!preg_match('/^http/', $file) && !file_exists($file))
    354497        {
    355498            throw new Exception("File $file can't be opened");
    356         }
    357         // invalid remote file
     499        } // invalid remote file
    358500        elseif(preg_match('/^http/', $file) && !preg_match('/200/', get_headers($file)[0]))
    359501        {
    360502            throw new Exception("File $file can't be opened", 2);
    361         }
    362         // download remote file if required only for integrated downloader
     503        } // download remote file if required only for integrated downloader
    363504        elseif(preg_match('/^http/', $file) && $this->downloadRemote)
    364505        {
     
    370511
    371512    /**
     513     * Parse the response returned by Apache Tika
     514     *
     515     * @return mixed
     516     * @throws \Exception
     517     */
     518    protected function parseJsonResponse(string $response)
     519    {
     520        // an empty response throws an error
     521        if(empty($response) || trim($response) == '')
     522        {
     523            throw new Exception('Empty response');
     524        }
     525
     526        // decode the JSON response
     527        $json = json_decode($response);
     528
     529        // exceptions if metadata is not valid
     530        if(json_last_error())
     531        {
     532            $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Error parsing JSON response';
     533
     534            throw new Exception($message, json_last_error());
     535        }
     536
     537        return $json;
     538    }
     539
     540    /**
    372541     * Download file to a temporary folder
    373542     *
    374      * @link    https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
    375      * @param   string  $file
    376      * @return  string
    377      * @throws  \Exception
    378      */
    379     protected function downloadFile($file)
     543     * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
     544     * @throws \Exception
     545     */
     546    protected function downloadFile(string $file): string
    380547    {
    381548        $dest = tempnam(sys_get_temp_dir(), 'TIKA');
     
    411578
    412579    /**
     580     * Must return the supported MIME types
     581     *
     582     * @throws \Exception
     583     */
     584    abstract public function getSupportedMIMETypes(): array;
     585
     586    /**
     587     * Must return the available detectors
     588     *
     589     * @throws \Exception
     590     */
     591    abstract public function getAvailableDetectors(): array;
     592
     593    /**
     594     * Must return the available parsers
     595     *
     596     * @throws \Exception
     597     */
     598    abstract public function getAvailableParsers(): array;
     599
     600    /**
     601     * Check Java binary, JAR path or server connection
     602     */
     603    abstract public function check(): void;
     604
     605    /**
    413606     * Configure and make a request and return its results.
    414607     *
    415      * @param   string  $type
    416      * @param   string  $file
    417      * @return  string
    418      * @throws  \Exception
    419      */
    420     abstract public function request($type, $file);
     608     * @throws \Exception
     609     */
     610    abstract public function request(string $type, string $file = null): string;
    421611}
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/src/Clients/CLIClient.php

    r2002534 r2525480  
    66
    77use Vaites\ApacheTika\Client;
    8 use Vaites\ApacheTika\Metadata\Metadata;
    98
    109/**
     
    1211 *
    1312 * @author  David Martínez <contacto@davidmartinez.net>
    14  * @link    http://wiki.apache.org/tika/TikaJAXRS
    15  * @link    https://tika.apache.org/1.12/formats.html
     13 * @link    https://tika.apache.org/1.23/gettingstarted.html#Using_Tika_as_a_command_line_utility
    1614 */
    1715class CLIClient extends Client
    1816{
    19     const MODE = 'cli';
     17    protected const MODE = 'cli';
    2018
    2119    /**
     
    3432
    3533    /**
     34     * Java arguments
     35     *
     36     * @var string
     37     */
     38    protected $javaArgs = null;
     39
     40    /**
    3641     * Configure client
    3742     *
    38      * @param   string  $path
    39      * @param   string  $java
    40      *
    41      * @throws Exception
    42      */
    43     public function __construct($path = null, $java = null)
    44     {
     43     * @throws \Exception
     44     */
     45    public function __construct(string $path = null, string $java = null, bool $check = true)
     46    {
     47        parent::__construct();
     48
    4549        if($path)
    4650        {
     
    5256            $this->setJava($java);
    5357        }
     58
     59        if($check === true)
     60        {
     61            $this->check();
     62        }
    5463    }
    5564
    5665    /**
    5766     * Get the path
    58      *
    59      * @return  null|string
    60      */
    61     public function getPath()
     67     */
     68    public function getPath(): ?string
    6269    {
    6370        return $this->path;
     
    6673    /**
    6774     * Set the path
    68      *
    69      * @param   string  $path
    70      * @return  $this
    71      */
    72     public function setPath($path)
     75     */
     76    public function setPath($path): self
    7377    {
    7478        $this->path = $path;
     
    7983    /**
    8084     * Get the Java path
    81      *
    82      * @return  null|int
    83      */
    84     public function getJava()
     85     */
     86    public function getJava(): ?string
    8587    {
    8688        return $this->java;
     
    8991    /**
    9092     * Set the Java path
    91      *
    92      * @param   string    $java
    93      * @return  $this
    94      */
    95     public function setJava($java)
     93     */
     94    public function setJava(string $java): self
    9695    {
    9796        $this->java = $java;
     
    101100
    102101    /**
     102     * Get the Java arguments
     103     */
     104    public function getJavaArgs(): ?string
     105    {
     106        return $this->javaArgs;
     107    }
     108
     109    /**
     110     * Set the Java arguments
     111     *
     112     * NOTE: to modify child process jvm args, prepend "J" to each argument (-JXmx4g)
     113     */
     114    public function setJavaArgs(string $args): self
     115    {
     116        $this->javaArgs = $args;
     117
     118        return $this;
     119    }
     120
     121    /**
     122     * Returns the supported MIME types
     123     *
     124     * NOTE: the data provided by the CLI must be parsed: mime type has no spaces, aliases go next prefixed with spaces
     125     *
     126     * @throws \Exception
     127     */
     128    public function getSupportedMIMETypes(): array
     129    {
     130        $mime = null;
     131        $mimeTypes = [];
     132
     133        $response = preg_split("/\n/", $this->request('mime-types'));
     134
     135        foreach($response as $line)
     136        {
     137            if(preg_match('/^\w+/', $line))
     138            {
     139                $mime = trim($line);
     140                $mimeTypes[$mime] = ['alias' => []];
     141            }
     142            else
     143            {
     144                [$key, $value] = preg_split('/:\s+/', trim($line));
     145
     146                if($key == 'alias')
     147                {
     148                    $mimeTypes[$mime]['alias'][] = $value;
     149                }
     150                else
     151                {
     152                    $mimeTypes[$mime][$key] = $value;
     153                }
     154            }
     155        }
     156
     157
     158        return $mimeTypes;
     159    }
     160
     161    /**
     162     * Returns the available detectors
     163     *
     164     * @throws \Exception
     165     */
     166    public function getAvailableDetectors(): array
     167    {
     168        $detectors = [];
     169
     170        $split = preg_split("/\n/", $this->request('detectors'));
     171
     172        $parent = null;
     173        foreach($split as $line)
     174        {
     175            if(preg_match('/composite/i', $line))
     176            {
     177                $parent = trim(preg_replace('/\(.+\):/', '', $line));
     178                $detectors[$parent] = ['children' => [], 'composite' => true, 'name' => $parent];
     179            }
     180            else
     181            {
     182                $child = trim($line);
     183                $detectors[$parent]['children'][$child] = ['composite' => false, 'name' => $child];
     184            }
     185        }
     186
     187        return $detectors;
     188    }
     189
     190    /**
     191     * Returns the available parsers
     192     *
     193     * @throws \Exception
     194     */
     195    public function getAvailableParsers(): array
     196    {
     197        $parsers = [];
     198
     199        $split = preg_split("/\n/", $this->request('parsers'));
     200        array_shift($split);
     201
     202        $parent = null;
     203        foreach($split as $line)
     204        {
     205            if(preg_match('/composite/i', $line))
     206            {
     207                $parent = trim(preg_replace('/\(.+\):/', '', $line));
     208
     209                $parsers[$parent] = ['children' => [], 'composite' => true, 'name' => $parent, 'decorated' => false];
     210            }
     211            else
     212            {
     213                $child = trim($line);
     214
     215                $parsers[$parent]['children'][$child] = ['composite' => false, 'name' => $child, 'decorated' => false];
     216            }
     217        }
     218
     219        return $parsers;
     220    }
     221
     222    /**
     223     * Check Java binary, JAR path or server connection
     224     *
     225     * @throws \Exception
     226     */
     227    public function check(): void
     228    {
     229        if($this->isChecked() === false)
     230        {
     231            // Java command must not return an error
     232            try
     233            {
     234                $this->exec(($this->java ?: 'java') . ' -version');
     235            }
     236            catch(Exception $exception)
     237            {
     238                throw new Exception('Java command not found');
     239            }
     240
     241            // JAR path must exists
     242            if(file_exists($this->path) === false)
     243            {
     244                throw new Exception('Apache Tika app JAR not found');
     245            }
     246
     247            $this->setChecked(true);
     248        }
     249    }
     250
     251    /**
    103252     * Configure and make a request and return its results
    104253     *
    105      * @param   string  $type
    106      * @param   string  $file
    107      * @return string
    108      * @throws  \Exception
    109      */
    110     public function request($type, $file = null)
    111     {
     254     * @throws \Exception
     255     */
     256    public function request(string $type, string $file = null): string
     257    {
     258        // check if not checked
     259        $this->check();
     260
    112261        // check if is cached
    113         if(isset($this->cache[sha1($file)][$type]))
    114         {
    115             return $this->cache[sha1($file)][$type];
     262        if($file !== null && $this->isCached($type, $file))
     263        {
     264            return $this->getCachedResponse($type, $file);
    116265        }
    117266
     
    120269
    121270        // check the request
    122         $file = parent::checkRequest($type, $file);
     271        $file = $this->checkRequest($type, $file);
    123272
    124273        // add last argument
     
    130279        // build command
    131280        $jar = escapeshellarg($this->path);
    132         $command = ($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments);
     281        $command = trim(($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments) . " {$this->javaArgs}");
    133282
    134283        // run command
     
    136285
    137286        // metadata response
    138         if($type == 'meta')
     287        if(in_array(preg_replace('/\/.+/', '', $type), ['meta', 'rmeta']))
    139288        {
    140289            // fix for invalid? json returned only with images
    141290            $response = str_replace(basename($file) . '"}{', '", ', $response);
    142291
    143             // on Windows, response comes in another charset
    144             if(defined('PHP_WINDOWS_VERSION_MAJOR'))
    145             {
    146                 $response = utf8_encode($response);
    147             }
    148 
    149             $response = Metadata::make($response, $file);
     292            // on Windows, response must be encoded to UTF8
     293            $response = $this->platform == 'win' ? utf8_encode($response) : $response;
    150294        }
    151295
    152296        // cache certain responses
    153         if(in_array($type, ['lang', 'meta']))
    154         {
    155             $this->cache[sha1($file)][$type] = $response;
     297        if($this->isCacheable($type))
     298        {
     299            $this->cacheResponse($type, $response, $file);
    156300        }
    157301
     
    162306     * Run the command and return its results
    163307     *
    164      * @param   string  $command
    165      * @return  null|string
    166      * @throws  \Exception
    167      */
    168     public function exec($command)
     308     * @throws \Exception
     309     */
     310    public function exec(string $command): ?string
    169311    {
    170312        // run command
     
    187329                }
    188330
    189                 $this->response .= $chunk;
     331                if($this->callbackAppend === true)
     332                {
     333                    $this->response .= $chunk;
     334                }
    190335            }
    191336            fclose($pipes[1]);
     
    205350     * Get the arguments to run the command
    206351     *
    207      * @param   string  $type
    208      * @param   string  $file
    209      * @return  array
    210352     * @throws  Exception
    211353     */
    212     protected function getArguments($type, $file = null)
    213     {
    214         // parameters for command
    215         $arguments = [];
     354    protected function getArguments(string $type, string $file = null): array
     355    {
     356        $arguments = $this->encoding ? ["--encoding={$this->encoding}"] : [];
     357
    216358        switch($type)
    217359        {
     
    256398                break;
    257399
     400            case 'rmeta/ignore':
     401                $arguments[] = '--metadata --jsonRecursive';
     402                break;
     403
     404            case 'rmeta/html':
     405                $arguments[] = '--html --jsonRecursive';
     406                break;
     407
     408            case 'rmeta/text':
     409                $arguments[] = '--text --jsonRecursive';
     410                break;
     411
     412            case 'xhtml':
     413                $arguments[] = '--xml';
     414                break;
     415
    258416            default:
    259                 throw new Exception("Unknown type $type");
     417                throw new Exception($file ? "Unknown type $type for $file" : "Unknown type $type");
    260418        }
    261419
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/src/Clients/WebClient.php

    r2002534 r2525480  
    66
    77use Vaites\ApacheTika\Client;
    8 use Vaites\ApacheTika\Metadata\Metadata;
    98
    109/**
     
    1211 *
    1312 * @author  David Martínez <contacto@davidmartinez.net>
    14  * @link    http://wiki.apache.org/tika/TikaJAXRS
    15  * @link    https://tika.apache.org/1.12/formats.html
     13 * @link    https://cwiki.apache.org/confluence/display/TIKA/TikaServer
    1614 */
    1715class WebClient extends Client
    1816{
    19     const MODE = 'web';
     17    protected const MODE = 'web';
    2018
    2119    /**
     
    3129     * @var string
    3230     */
    33     protected $host = '127.0.0.1';
     31    protected $host = null;
    3432
    3533    /**
     
    3836     * @var int
    3937     */
    40     protected $port = 9998;
     38    protected $port = null;
    4139
    4240    /**
     
    5452    protected $options =
    5553    [
    56         CURLINFO_HEADER_OUT    => true,
    57         CURLOPT_HTTPHEADER     => [],
    58         CURLOPT_PUT            => true,
    59         CURLOPT_RETURNTRANSFER => true,
    60         CURLOPT_TIMEOUT        => 5,
     54        CURLINFO_HEADER_OUT     => true,
     55        CURLOPT_HTTPHEADER      => [],
     56        CURLOPT_PUT             => true,
     57        CURLOPT_RETURNTRANSFER  => true,
     58        CURLOPT_TIMEOUT         => 5
    6159    ];
    6260
     
    6462     * Configure class and test if server is running
    6563     *
    66      * @param   string  $host
    67      * @param   int     $port
    68      * @param   array   $options
    69      * @throws  \Exception
    70      */
    71     public function __construct($host = null, $port = null, $options = [])
    72     {
    73         if($host)
     64     * @throws \Exception
     65     */
     66    public function __construct(string $host = null, int $port = null, array $options = [], bool $check = true)
     67    {
     68        parent::__construct();
     69
     70        if(is_string($host) && filter_var($host, FILTER_VALIDATE_URL))
     71        {
     72            $this->setUrl($host);
     73        }
     74        elseif($host)
    7475        {
    7576            $this->setHost($host);
    7677        }
    7778
    78         if($port)
     79        if(is_numeric($port))
    7980        {
    8081            $this->setPort($port);
     
    8889        $this->setDownloadRemote(true);
    8990
    90         $this->getVersion(); // exception if not running
     91        if($check === true)
     92        {
     93            $this->check();
     94        }
     95    }
     96
     97    /**
     98     * Get the base URL
     99     */
     100    public function getUrl(): string
     101    {
     102        return sprintf('http://%s:%d', $this->host, $this->port ?: 9998);
     103    }
     104
     105    /**
     106     * Set the host and port using an URL
     107     */
     108    public function setUrl(string $url): self
     109    {
     110        $url = parse_url($url);
     111
     112        $this->setHost($url['host']);
     113
     114        if(isset($url['port']))
     115        {
     116            $this->setPort($url['port']);
     117        }
     118
     119        return $this;
    91120    }
    92121
    93122    /**
    94123     * Get the host
    95      *
    96      * @return  null|string
    97      */
    98     public function getHost()
     124     */
     125    public function getHost(): ?string
    99126    {
    100127        return $this->host;
     
    103130    /**
    104131     * Set the host
    105      *
    106      * @param   string  $host
    107      * @return  $this
    108      */
    109     public function setHost($host)
     132     */
     133    public function setHost(string $host): self
    110134    {
    111135        $this->host = $host;
     
    116140    /**
    117141     * Get the port
    118      *
    119      * @return  null|int
    120      */
    121     public function getPort()
     142     */
     143    public function getPort(): ?int
    122144    {
    123145        return $this->port;
     
    126148    /**
    127149     * Set the port
    128      *
    129      * @param   int     $port
    130      * @return  $this
    131      */
    132     public function setPort($port)
     150     */
     151    public function setPort(int $port): self
    133152    {
    134153        $this->port = $port;
     
    139158    /**
    140159     * Get the number of retries
    141      *
    142      * @return  int
    143      */
    144     public function getRetries()
     160     */
     161    public function getRetries(): int
    145162    {
    146163        return $this->retries;
     
    149166    /**
    150167     * Set the number of retries
    151      *
    152      * @param   int     $retries
    153      * @return  $this
    154      */
    155     public function setRetries($retries)
     168     */
     169    public function setRetries(int $retries): self
    156170    {
    157171        $this->retries = $retries;
     
    162176    /**
    163177     * Get all the options
    164      *
    165      * @return  null|array
    166      */
    167     public function getOptions()
     178     */
     179    public function getOptions(): array
    168180    {
    169181        return $this->options;
     
    173185     * Get an specified option
    174186     *
    175      * @param   string  $key
    176187     * @return  mixed
    177188     */
    178     public function getOption($key)
    179     {
    180         return isset($this->options[$key]) ? $this->options[$key] : null;
     189    public function getOption(int $key)
     190    {
     191        return $this->options[$key] ?? null;
    181192    }
    182193
     
    184195     * Set a cURL option to be set with curl_setopt()
    185196     *
    186      * @link    http://php.net/manual/en/curl.constants.php
    187      * @link    http://php.net/manual/en/function.curl-setopt.php
    188      * @param   string  $key
    189      * @param   mixed   $value
    190      * @return  $this
    191      * @throws  \Exception
    192      */
    193     public function setOption($key, $value)
     197     * @link http://php.net/manual/en/curl.constants.php
     198     * @link http://php.net/manual/en/function.curl-setopt.php
     199     * @throws \Exception
     200     */
     201    public function setOption(int $key, $value): self
    194202    {
    195203        if(in_array($key, [CURLINFO_HEADER_OUT, CURLOPT_PUT, CURLOPT_RETURNTRANSFER]))
     
    206214     * Set the cURL options
    207215     *
    208      * @param   array   $options
    209      * @return  $this
    210      * @throws  \Exception
    211      */
    212     public function setOptions($options)
     216     * @throws \Exception
     217     */
     218    public function setOptions(array $options): self
    213219    {
    214220        foreach($options as $key => $value)
     
    222228    /**
    223229     * Get the timeout value for cURL
    224      *
    225      * @return  int
    226      */
    227     public function getTimeout()
     230     */
     231    public function getTimeout(): int
    228232    {
    229233        return $this->getOption(CURLOPT_TIMEOUT);
     
    233237     * Set the timeout value for cURL
    234238     *
    235      * @param   int     $value
    236      * @return  $this
    237      * @throws  \Exception
    238      */
    239     public function setTimeout($value)
     239     * @throws \Exception
     240     */
     241    public function setTimeout(int $value): self
    240242    {
    241243        $this->setOption(CURLOPT_TIMEOUT, (int) $value);
     
    245247
    246248    /**
     249     * Returns the supported MIME types
     250     *
     251     * @throws \Exception
     252     */
     253    public function getSupportedMIMETypes(): array
     254    {
     255        $mimeTypes = json_decode($this->request('mime-types'), true);
     256
     257        ksort($mimeTypes);
     258
     259        return $mimeTypes;
     260    }
     261
     262    /**
     263     * Returns the available detectors
     264     *
     265     * @throws \Exception
     266     */
     267    public function getAvailableDetectors(): array
     268    {
     269        $detectors = [json_decode($this->request('detectors'), true)];
     270
     271        foreach($detectors as $index => $parent)
     272        {
     273            $detectors[$parent['name']] = $parent;
     274
     275            if(isset($parent['children']))
     276            {
     277                foreach($parent['children'] as $subindex => $child)
     278                {
     279                    $detectors[$parent['name']]['children'][$child['name']] = $child;
     280
     281                    unset($detectors[$parent['name']]['children'][$subindex]);
     282                }
     283            }
     284
     285            unset($detectors[$index]);
     286        }
     287
     288        return $detectors;
     289    }
     290
     291    /**
     292     * Returns the available parsers
     293     *
     294     * @throws \Exception
     295     */
     296    public function getAvailableParsers(): array
     297    {
     298        $parsers = [json_decode($this->request('parsers'), true)];
     299
     300        foreach($parsers as $index => $parent)
     301        {
     302            $parsers[$parent['name']] = $parent;
     303
     304            if(isset($parent['children']))
     305            {
     306                foreach($parent['children'] as $subindex => $child)
     307                {
     308                    $parsers[$parent['name']]['children'][$child['name']] = $child;
     309
     310                    unset($parsers[$parent['name']]['children'][$subindex]);
     311                }
     312            }
     313
     314            unset($parsers[$index]);
     315        }
     316
     317        return $parsers;
     318    }
     319
     320    /**
     321     * Check if server is running
     322     *
     323     * @throws \Exception
     324     */
     325    public function check(): void
     326    {
     327        if($this->isChecked() === false)
     328        {
     329            $this->setChecked(true);
     330
     331            // throws an exception if server is unreachable or can't connect
     332            $this->request('version');
     333        }
     334    }
     335
     336    /**
    247337     * Configure, make a request and return its results
    248338     *
    249      * @param   string  $type
    250      * @param   string  $file
    251      * @return  string
    252      * @throws  \Exception
    253      */
    254     public function request($type, $file = null)
     339     * @throws \Exception
     340     */
     341    public function request(string $type, string $file = null): string
    255342    {
    256343        static $retries = [];
    257344
     345        // check if not checked
     346        $this->check();
     347
    258348        // check if is cached
    259         if(isset($this->cache[sha1($file)][$type]))
    260         {
    261             return $this->cache[sha1($file)][$type];
     349        if($file !== null && $this->isCached($type, $file))
     350        {
     351            return $this->getCachedResponse($type, $file);
    262352        }
    263353        elseif(!isset($retries[sha1($file)]))
     
    267357
    268358        // parameters for cURL request
    269         list($resource, $headers) = $this->getParameters($type, $file);
     359        [$resource, $headers] = $this->getParameters($type, $file);
    270360
    271361        // check the request
    272         $file = parent::checkRequest($type, $file);
     362        $file = $this->checkRequest($type, $file);
    273363
    274364        // cURL options
     
    282372
    283373        // cURL init and options
    284         $options[CURLOPT_URL] = "http://{$this->host}:{$this->port}" . "/$resource";
     374        $options[CURLOPT_URL] = $this->getUrl() . "/$resource";
    285375
    286376        // get the response and the HTTP status code
    287         list($response, $status) = $this->exec($options);
     377        [$response, $status] = $this->exec($options);
     378
     379        // reduce memory usage closing cURL resource
     380        if(isset($options[CURLOPT_INFILE]) && is_resource($options[CURLOPT_INFILE]))
     381        {
     382            fclose($options[CURLOPT_INFILE]);
     383        }
    288384
    289385        // request completed successfully
    290386        if($status == 200)
    291387        {
    292             if($type == 'meta')
     388            // cache certain responses
     389            if($this->isCacheable($type))
    293390            {
    294                 $response = Metadata::make($response, $file);
     391                $this->cacheResponse($type, $response, $file);
    295392            }
    296 
    297             // cache certain responses
    298             if(in_array($type, ['lang', 'meta']))
    299             {
    300                 $this->cache[sha1($file)][$type] = $response;
    301             }
    302         }
    303         // request completed successfully but result is empty
     393        } // request completed successfully but result is empty
    304394        elseif($status == 204)
    305395        {
    306396            $response = null;
    307         }
    308         // retry on request failed with error 500
     397        } // retry on request failed with error 500
    309398        elseif($status == 500 && $retries[sha1($file)]--)
    310399        {
    311400            $response = $this->request($type, $file);
    312         }
    313         // other status code is an error
     401        } // other status code is an error
    314402        else
    315403        {
    316             $this->error($status, $resource);
     404            $this->error($status, $resource, $file);
    317405        }
    318406
     
    323411     * Make a request to Apache Tika Server
    324412     *
    325      * @param   array   $options
    326      * @return  array
    327      * @throws  \Exception
    328      */
    329     protected function exec(array $options = [])
     413     * @throws \Exception
     414     */
     415    protected function exec(array $options = []): array
    330416    {
    331417        // cURL init and options
    332418        $curl = curl_init();
    333419
    334         // we avoid curl_setopt_array($curl, $options) because extrange Windows behaviour (issue #8)
     420        // we avoid curl_setopt_array($curl, $options) because strange Windows behaviour (issue #8)
    335421        foreach($options as $option => $value)
    336422        {
     
    338424        }
    339425
    340         // make the request
     426        // make the request directly
    341427        if(is_null($this->callback))
    342428        {
    343             $this->response = curl_exec($curl);
    344         }
     429            $this->response = curl_exec($curl) ?: '';
     430        } // with a callback, the response is appended on each block inside the callback
    345431        else
    346432        {
     
    362448     * Throws an exception for an error status code
    363449     *
    364      * @codeCoverageIgnore
    365      *
    366      * @param   int       $status
    367      * @param   string    $resource
    368      * @throws  \Exception
    369      */
    370     protected function error($status, $resource)
     450     * @throws \Exception
     451     */
     452    protected function error(int $status, string $resource, string $file = null): void
    371453    {
    372454        switch($status)
     
    374456            //  method not allowed
    375457            case 405:
    376                 throw new Exception('Method not allowed', 405);
     458                $message = 'Method not allowed';
    377459                break;
    378460
    379461            //  unsupported media type
    380462            case 415:
    381                 throw new Exception('Unsupported media type', 415);
     463                $message = 'Unsupported media type';
    382464                break;
    383465
    384466            //  unprocessable entity
    385467            case 422:
    386                 throw new Exception('Unprocessable document', 422);
     468                $message = 'Unprocessable document';
     469
     470                // using remote files require Tika server to be launched with specific options
     471                if($this->downloadRemote == false && preg_match('/^http/', $file))
     472                {
     473                    $message .= ' (is server launched using "-enableUnsecureFeatures -enableFileUrl" arguments?)';
     474                }
     475
    387476                break;
    388477
    389478            // server error
    390479            case 500:
    391                 throw new Exception('Error while processing document', 500);
     480                $message = 'Error while processing document';
    392481                break;
    393482
    394483            // unexpected
    395484            default:
    396                 throw new Exception("Unexpected response for /$resource ($status)", 501);
    397         }
     485                $message = "Unexpected response for /$resource ($status)";
     486                $status = 501;
     487        }
     488
     489        throw new Exception($message, $status);
    398490    }
    399491
     
    401493     * Get the parameters to make the request
    402494     *
    403      * @link    https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
    404      * @param   string  $type
    405      * @param   string  file
    406      * @return  array
    407      * @throws  \Exception
    408      */
    409     protected function getParameters($type, $file = null)
     495     * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
     496     * @throws \Exception
     497     */
     498    protected function getParameters(string $type, string $file = null): array
    410499    {
    411500        $headers = [];
     501        $callback = null;
    412502
    413503        if(!empty($file) && preg_match('/^http/', $file))
     
    433523                break;
    434524
     525            case 'detectors':
     526            case 'parsers':
    435527            case 'meta':
    436                 $resource = 'meta';
     528            case 'mime-types':
     529            case 'rmeta/html':
     530            case 'rmeta/ignore':
     531            case 'rmeta/text':
     532                $resource = $type;
    437533                $headers[] = 'Accept: application/json';
     534                $callback = function($response)
     535                {
     536                    return json_decode($response, true);
     537                };
    438538                break;
    439539
     
    448548                break;
    449549
    450             case 'detectors':
    451             case 'parsers':
    452             case 'mime-types':
    453550            case 'version':
    454551                $resource = $type;
    455552                break;
    456553
     554            case 'xhtml':
     555                throw new Exception("Tika Server does not support XHTML output");
     556                break;
     557
    457558            default:
    458559                throw new Exception("Unknown type $type");
    459560        }
    460561
    461         return [$resource, $headers];
     562        return [$resource, $headers, $callback];
    462563    }
    463564
     
    465566     * Get the cURL options
    466567     *
    467      * @param   string  $type
    468      * @param   string  file
    469      * @return  array
    470      * @throws  \Exception
    471      */
    472     protected function getCurlOptions($type, $file = null)
     568     * @throws \Exception
     569     */
     570    protected function getCurlOptions(string $type, string $file = null): array
    473571    {
    474572        // base options
     
    480578            $callback = $this->callback;
    481579
    482             $options[CURLOPT_WRITEFUNCTION] = function($handler, $data) use($callback)
     580            $options[CURLOPT_WRITEFUNCTION] = function($handler, $data) use ($callback)
    483581            {
    484                 $this->response .= $data;
     582                if($this->callbackAppend === true)
     583                {
     584                    $this->response .= $data;
     585                }
    485586
    486587                $callback($data);
     
    495596        {
    496597            //
    497         }
    498         // local file options
     598        } // local file options
    499599        elseif($file && file_exists($file) && is_readable($file))
    500600        {
    501601            $options[CURLOPT_INFILE] = fopen($file, 'r');
    502602            $options[CURLOPT_INFILESIZE] = filesize($file);
    503         }
    504         // other options for specific requests
    505         elseif(in_array($type,  ['detectors', 'mime-types', 'parsers', 'version']))
     603        } // other options for specific requests
     604        elseif(in_array($type, ['detectors', 'mime-types', 'parsers', 'version']))
    506605        {
    507606            $options[CURLOPT_PUT] = false;
    508         }
    509         // file not accesible
     607        } // file not accesible
    510608        else
    511609        {
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/tests/BaseTest.php

    r2002534 r2525480  
    33namespace Vaites\ApacheTika\Tests;
    44
    5 use PHPUnit_Framework_TestCase;
     5use Exception;
     6
     7use PHPUnit\Framework\TestCase;
    68
    79/**
    810 * Common test functionality
    911 */
    10 abstract class BaseTest extends PHPUnit_Framework_TestCase
     12abstract class BaseTest extends TestCase
    1113{
    1214    /**
     
    4042    /**
    4143     * Get env variables
    42      *
    43      * @param null      $name
    44      * @param array     $data
    45      * @param string    $dataName
    46      */
    47     public function __construct($name = null, array $data = array(), $dataName = '')
     44     *
     45     * @throws \Exception
     46     */
     47    public function __construct(string $name = null, array $data = array(), $dataName = '')
    4848    {
    4949        self::$version = getenv('APACHE_TIKA_VERSION');
    5050        self::$binaries = getenv('APACHE_TIKA_BINARIES');
    5151
     52        if(empty(self::$version))
     53        {
     54            throw new Exception('APACHE_TIKA_VERSION environment variable not defined');
     55        }
     56
    5257        parent::__construct($name, $data, $dataName);
    5358    }
     
    5661     * Version test
    5762     */
    58     public function testVersion()
     63    public function testVersion(): void
    5964    {
    6065        $this->assertEquals('Apache Tika ' . self::$version, self::$client->getVersion());
     
    6469     * Metadata test
    6570     *
    66      * @dataProvider    documentProvider
    67      *
    68      * @param   string $file
    69      * @param   string $class
    70      * @throws  \Exception
    71      */
    72     public function testMetadata($file, $class = 'Metadata')
     71     * @dataProvider documentProvider
     72     */
     73    public function testMetadata(string $file, string $class = 'Metadata'): void
     74    {
     75        $this->assertInstanceOf("\\Vaites\\ApacheTika\\Metadata\\$class", self::$client->getMetadata($file));
     76    }
     77
     78    /**
     79     * Metadata test
     80     *
     81     * @dataProvider documentProvider
     82     */
     83    public function testDocumentMetadata(string $file, string $class = 'DocumentMetadata'): void
     84    {
     85        $this->testMetadata($file, $class);
     86    }
     87
     88    /**
     89     * Metadata title test
     90     *
     91     * @dataProvider documentProvider
     92     */
     93    public function testDocumentMetadataTitle(string $file): void
     94    {
     95        $this->assertEquals('Lorem ipsum dolor sit amet', self::$client->getMetadata($file)->title);
     96    }
     97
     98    /**
     99     * Metadata author test
     100     *
     101     * @dataProvider documentProvider
     102     */
     103    public function testDocumentMetadataAuthor(string $file): void
     104    {
     105        $this->assertEquals('David Martínez', self::$client->getMetadata($file)->author);
     106    }
     107
     108    /**
     109     * Metadata dates test
     110     *
     111     * @dataProvider documentProvider
     112     */
     113    public function testDocumentMetadataCreated(string $file): void
     114    {
     115        $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->created);
     116    }
     117
     118    /**
     119     * Metadata dates test
     120     *
     121     * @dataProvider documentProvider
     122     */
     123    public function testDocumentMetadataUpdated(string $file): void
     124    {
     125        $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->updated);
     126    }
     127
     128    /**
     129     * Metadata keywords test
     130     *
     131     * @dataProvider documentProvider
     132     */
     133    public function testDocumentMetadataKeywords(string $file): void
     134    {
     135        $this->assertContains('ipsum', self::$client->getMetadata($file)->keywords);
     136    }
     137
     138    /**
     139     * Recursive text metadata test
     140     *
     141     * @dataProvider recursiveProvider
     142     */
     143    public function testTextRecursiveMetadata(string $file): void
     144    {
     145        $nested = 'sample8.zip/sample1.doc';
     146
     147        $metadata = self::$client->getRecursiveMetadata($file, 'text');
     148
     149        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', $metadata[$nested]->content ?? 'ERROR');
     150    }
     151
     152    /**
     153     * Recursive HTML metadata test
     154     *
     155     * @dataProvider recursiveProvider
     156     */
     157    public function testHtmlRecursiveMetadata(string $file): void
     158    {
     159        $nested = 'sample8.zip/sample1.doc';
     160
     161        $metadata = self::$client->getRecursiveMetadata($file, 'html');
     162
     163        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', $metadata[$nested]->content ?? 'ERROR');
     164    }
     165
     166    /**
     167     * Recursive ignore metadata test
     168     *
     169     * @dataProvider ocrProvider
     170     */
     171    public function testIgnoreRecursiveMetadata(string $file): void
     172    {
     173        $metadata = self::$client->getRecursiveMetadata($file, 'ignore');
     174
     175        $this->assertNull(array_shift($metadata)->content);
     176    }
     177
     178    /**
     179     * Language test
     180     *
     181     * @dataProvider documentProvider
     182     */
     183    public function testDocumentLanguage(string $file): void
     184    {
     185        $this->assertRegExp('/^[a-z]{2}$/', self::$client->getLanguage($file));
     186    }
     187
     188    /**
     189     * MIME test
     190     *
     191     * @dataProvider documentProvider
     192     */
     193    public function testDocumentMIME(string $file): void
     194    {
     195        $this->assertNotEmpty(self::$client->getMIME($file));
     196    }
     197
     198    /**
     199     * HTML test
     200     *
     201     * @dataProvider documentProvider
     202     */
     203    public function testDocumentHTML(string $file): void
     204    {
     205        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', self::$client->getHTML($file));
     206    }
     207
     208    /**
     209     * Text test
     210     *
     211     * @dataProvider documentProvider
     212     */
     213    public function testDocumentText(string $file): void
     214    {
     215        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', self::$client->getText($file));
     216    }
     217
     218    /**
     219     * Main text test
     220     *
     221     * @dataProvider documentProvider
     222     */
     223    public function testDocumentMainText(string $file): void
     224    {
     225        $this->assertStringContainsString('Lorem ipsum dolor sit amet', self::$client->getMainText($file));
     226    }
     227
     228    /**
     229     * Metadata test
     230     *
     231     * @dataProvider imageProvider
     232     */
     233    public function testImageMetadata(string $file, string $class = 'ImageMetadata'): void
     234    {
     235        $this->testMetadata($file, $class);
     236    }
     237
     238    /**
     239     * Metadata width test
     240     *
     241     * @dataProvider imageProvider
     242     */
     243    public function testImageMetadataWidth(string $file): void
     244    {
     245        $meta = self::$client->getMetadata($file);
     246
     247        $this->assertEquals(1600, $meta->width, basename($file));
     248    }
     249
     250    /**
     251     * Metadata height test
     252     *
     253     * @dataProvider imageProvider
     254     */
     255    public function testImageMetadataHeight(string $file): void
     256    {
     257        $meta = self::$client->getMetadata($file);
     258
     259        $this->assertEquals(900, $meta->height, basename($file));
     260    }
     261
     262    /**
     263     * OCR test
     264     *
     265     * @dataProvider ocrProvider
     266     */
     267    public function testImageOCR(string $file): void
     268    {
     269        $text = self::$client->getText($file);
     270
     271        $this->assertRegExp('/voluptate/i', $text);
     272    }
     273
     274    /**
     275     * Text callback test
     276     *
     277     * @dataProvider callbackProvider
     278     */
     279    public function testTextCallback(string $file): void
     280    {
     281        BaseTest::$shared = 0;
     282
     283        self::$client->getText($file, [$this, 'callableCallback']);
     284
     285        $this->assertGreaterThan(1, BaseTest::$shared);
     286    }
     287
     288    /**
     289     * Text callback test
     290     *
     291     * @dataProvider callbackProvider
     292     */
     293    public function testTextCallbackWithoutAppend(string $file): void
     294    {
     295        BaseTest::$shared = 0;
     296
     297        $response = self::$client->getText($file, [$this, 'callableCallback'], false);
     298
     299        $this->assertEmpty($response);
     300    }
     301
     302    /**
     303     * Main text callback test
     304     *
     305     * @dataProvider callbackProvider
     306     */
     307    public function testMainTextCallback(string $file): void
     308    {
     309        BaseTest::$shared = 0;
     310
     311        self::$client->getMainText($file, function()
     312        {
     313            BaseTest::$shared++;
     314        });
     315
     316        $this->assertGreaterThan(1, BaseTest::$shared);
     317    }
     318
     319    /**
     320     * Main text callback test
     321     *
     322     * @dataProvider callbackProvider
     323     */
     324    public function testHtmlCallback(string $file): void
     325    {
     326        BaseTest::$shared = 0;
     327
     328        self::$client->getHtml($file, function()
     329        {
     330            BaseTest::$shared++;
     331        });
     332
     333        $this->assertGreaterThan(1, BaseTest::$shared);
     334    }
     335
     336    /**
     337     * Remote file test with integrated download
     338     *
     339     * @dataProvider remoteProvider
     340     */
     341    public function testRemoteDocumentText(string $file): void
     342    {
     343        $this->assertStringContainsString('Rationis enim perfectio est virtus', self::$client->getText($file));
     344    }
     345
     346    /**
     347     * Remote file test with internal downloader
     348     *
     349     * @dataProvider remoteProvider
     350     */
     351    public function testDirectRemoteDocumentText(string $file): void
    73352    {
    74353        $client =& self::$client;
    75354
    76         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    77         {
    78             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    79         }
    80         else
    81         {
    82             $this->assertInstanceOf("\\Vaites\\ApacheTika\\Metadata\\$class", self::$client->getMetadata($file));
    83         }
    84     }
    85 
    86     /**
    87      * Metadata test
    88      *
    89      * @dataProvider    documentProvider
    90      *
    91      * @param   string $file
    92      * @param   string $class
    93      * @throws  \Exception
    94      */
    95     public function testDocumentMetadata($file, $class = 'DocumentMetadata')
     355        $client->setDownloadRemote(false);
     356
     357        $this->assertStringContainsString('Rationis enim perfectio est virtus', $client->getText($file));
     358    }
     359
     360    /**
     361     * Encoding tests
     362     *
     363     * @dataProvider encodingProvider
     364     */
     365    public function testEncodingDocumentText(string $file): void
    96366    {
    97367        $client =& self::$client;
    98368
    99         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    100         {
    101             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    102         }
    103         else
    104         {
    105             $this->testMetadata($file, $class);
    106         }
    107     }
    108 
    109     /**
    110      * Metadata title test
    111      *
    112      * @dataProvider    documentProvider
    113      *
    114      * @param   string $file
    115      * @throws  \Exception
    116      */
    117     public function testDocumentMetadataTitle($file)
    118     {
    119         $client =& self::$client;
    120 
    121         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    122         {
    123             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    124         }
    125         else
    126         {
    127             $this->assertEquals('Lorem ipsum dolor sit amet', self::$client->getMetadata($file)->title);
    128         }
    129     }
    130 
    131     /**
    132      * Metadata author test
    133      *
    134      * @dataProvider    documentProvider
    135      *
    136      * @param   string $file
    137      * @throws  \Exception
    138      */
    139     public function testDocumentMetadataAuthor($file)
    140     {
    141         $client =& self::$client;
    142 
    143         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    144         {
    145             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    146         }
    147         else
    148         {
    149             $this->assertEquals('David Martínez', self::$client->getMetadata($file)->author);
    150         }
    151     }
    152 
    153     /**
    154      * Metadata dates test
    155      *
    156      * @dataProvider    documentProvider
    157      *
    158      * @param   string $file
    159      * @throws  \Exception
    160      */
    161     public function testDocumentMetadataCreated($file)
    162     {
    163         $client =& self::$client;
    164 
    165         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    166         {
    167             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    168         }
    169         else
    170         {
    171             $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->created);
    172         }
    173     }
    174 
    175     /**
    176      * Metadata dates test
    177      *
    178      * @dataProvider    documentProvider
    179      *
    180      * @param   string $file
    181      * @throws  \Exception
    182      */
    183     public function testDocumentMetadataUpdated($file)
    184     {
    185         $client =& self::$client;
    186 
    187         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    188         {
    189             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    190         }
    191         else
    192         {
    193             $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->updated);
    194         }
    195     }
    196 
    197     /**
    198      * Metadata keywords test
    199      *
    200      * @dataProvider    documentProvider
    201      *
    202      * @param   string $file
    203      * @throws  \Exception
    204      */
    205     public function testDocumentMetadataKeywords($file)
    206     {
    207         $client =& self::$client;
    208 
    209         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    210         {
    211             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    212         }
    213         else
    214         {
    215             $this->assertContains('ipsum', self::$client->getMetadata($file)->keywords);
    216         }
    217     }
    218 
    219     /**
    220      * Language test
    221      *
    222      * @dataProvider    documentProvider
    223      *
    224      * @param   string $file
    225      * @throws  \Exception
    226      */
    227     public function testDocumentLanguage($file)
    228     {
    229         $client =& self::$client;
    230 
    231         if($client::MODE == 'web' && version_compare(self::$version, '1.9') < 0)
    232         {
    233             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks REST language identification');
    234         }
    235         else
    236         {
    237             $this->assertRegExp('/^[a-z]{2}$/', self::$client->getLanguage($file));
    238         }
    239     }
    240 
    241     /**
    242      * MIME test
    243      *
    244      * @dataProvider    documentProvider
    245      *
    246      * @param   string $file
    247      * @throws  \Exception
    248      */
    249     public function testDocumentMIME($file)
    250     {
    251         $client =& self::$client;
    252 
    253         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    254         {
    255             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    256         }
    257         else
    258         {
    259             $this->assertNotEmpty(self::$client->getMIME($file));
    260         }
    261     }
    262 
    263     /**
    264      * HTML test
    265      *
    266      * @dataProvider    documentProvider
    267      *
    268      * @param   string $file
    269      * @throws  \Exception
    270      */
    271     public function testDocumentHTML($file)
    272     {
    273         $client =& self::$client;
    274 
    275         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    276         {
    277             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    278         }
    279         else
    280         {
    281             $this->assertContains('Zenonis est, inquam, hoc Stoici', self::$client->getHTML($file));
    282         }
    283     }
    284 
    285     /**
    286      * Text test
    287      *
    288      * @dataProvider    documentProvider
    289      *
    290      * @param   string $file
    291      * @throws  \Exception
    292      */
    293     public function testDocumentText($file)
    294     {
    295         $client =& self::$client;
    296 
    297         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    298         {
    299             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    300         }
    301         else
    302         {
    303             $this->assertContains('Zenonis est, inquam, hoc Stoici', self::$client->getText($file));
    304         }
    305     }
    306 
    307     /**
    308      * Main text test
    309      *
    310      * @dataProvider    documentProvider
    311      *
    312      * @param   string $file
    313      * @throws  \Exception
    314      */
    315     public function testDocumentMainText($file)
    316     {
    317         $client =& self::$client;
    318 
    319         if($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    320         {
    321             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks main content extraction');
    322         }
    323         else
    324         {
    325             $this->assertContains('Lorem ipsum dolor sit amet', self::$client->getMainText($file));
    326         }
    327     }
    328 
    329     /**
    330      * Metadata test
    331      *
    332      * @dataProvider    imageProvider
    333      *
    334      * @param   string $file
    335      * @param   string $class
    336      * @throws  \Exception
    337      */
    338     public function testImageMetadata($file, $class = 'ImageMetadata')
    339     {
    340         $client =& self::$client;
    341 
    342         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    343         {
    344             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    345         }
    346         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    347         {
    348             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    349         }
    350         else
    351         {
    352             $this->testMetadata($file, $class);
    353         }
    354     }
    355 
    356     /**
    357      * Metadata width test
    358      *
    359      * @dataProvider    imageProvider
    360      *
    361      * @param   string $file
    362      * @throws  \Exception
    363      */
    364     public function testImageMetadataWidth($file)
    365     {
    366         $client =& self::$client;
    367 
    368         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    369         {
    370             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    371         }
    372         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    373         {
    374             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    375         }
    376         else
    377         {
    378             $meta = self::$client->getMetadata($file);
    379 
    380             $this->assertEquals(1600, $meta->width, basename($file));
    381         }
    382     }
    383 
    384     /**
    385      * Metadata height test
    386      *
    387      * @dataProvider    imageProvider
    388      *
    389      * @param   string $file
    390      * @throws  \Exception
    391      */
    392     public function testImageMetadataHeight($file)
    393     {
    394         $client =& self::$client;
    395 
    396         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    397         {
    398             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    399         }
    400         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    401         {
    402             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    403         }
    404         else
    405         {
    406             $meta = self::$client->getMetadata($file);
    407 
    408             $this->assertEquals(900, $meta->height, basename($file));
    409         }
    410     }
    411 
    412     /**
    413      * OCR test
    414      *
    415      * @dataProvider    ocrProvider
    416      *
    417      * @param   string $file
    418      * @throws  \Exception
    419      */
    420     public function testImageOCR($file)
    421     {
    422         $client =& self::$client;
    423 
    424         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    425         {
    426             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    427         }
    428         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    429         {
    430             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    431         }
    432         else
    433         {
    434             $text = self::$client->getText($file);
    435 
    436             $this->assertRegExp('/voluptate/i', $text);
    437         }
    438     }
    439 
    440     /**
    441      * Text callback test
    442      *
    443      * @dataProvider    callbackProvider
    444      *
    445      * @param   string $file
    446      * @throws  \Exception
    447      */
    448     public function testTextCallback($file)
    449     {
    450         $client =& self::$client;
    451        
    452     if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    453         {
    454             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    455         }
    456         else
    457         {
    458             BaseTest::$shared = 0;
    459 
    460             self::$client->getText($file, [$this, 'callableCallback']);
    461 
    462             $this->assertGreaterThan(1, BaseTest::$shared);
    463         }
    464     }
    465 
    466     /**
    467      * Main text callback test
    468      *
    469      * @dataProvider    callbackProvider
    470      *
    471      * @param   string $file
    472      * @throws  \Exception
    473      */
    474     public function testMainTextCallback($file)
    475     {
    476         $client =& self::$client;
    477 
    478         if($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    479         {
    480             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks main content extraction');
    481         }
    482         else
    483         {
    484             BaseTest::$shared = 0;
    485 
    486             self::$client->getMainText($file, function ($chunk) {
    487                 BaseTest::$shared++;
    488             });
    489 
    490             $this->assertGreaterThan(1, BaseTest::$shared);
    491         }
    492     }
    493 
    494     /**
    495      * Main text callback test
    496      *
    497      * @dataProvider    callbackProvider
    498      *
    499      * @param   string $file
    500      * @throws  \Exception
    501      */
    502     public function testHtmlCallback($file)
    503     {
    504         $client =& self::$client;
    505 
    506         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    507         {
    508             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    509         }
    510         else
    511         {
    512             BaseTest::$shared = 0;
    513 
    514             self::$client->getHtml($file, function($chunk)
    515             {
    516                 BaseTest::$shared++;
    517             });
    518 
    519             $this->assertGreaterThan(1, BaseTest::$shared);
    520         }
    521     }
    522 
    523     /**
    524      * Remote file test with integrated download
    525      *
    526      * @dataProvider    remoteProvider
    527      *
    528      * @param   string $file
    529      * @throws  \Exception
    530      */
    531     public function testRemoteDocumentText($file)
    532     {
    533         $client =& self::$client;
    534 
    535         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    536         {
    537             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    538         }
    539         else
    540         {
    541             $this->assertContains('This is a small demonstration .pdf file', $client->getText($file));
    542         }
    543     }
    544 
    545     /**
    546      * Remote file test with internal downloader
    547      *
    548      * @dataProvider    remoteProvider
    549      *
    550      * @param   string $file
    551      * @throws  \Exception
    552      */
    553     public function testDirectRemoteDocumentText($file)
    554     {
    555         $client =& self::$client;
    556 
    557         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    558         {
    559             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    560         }
    561         elseif($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    562         {
    563             $this->markTestSkipped('Apache Tika between 1.10 and 1.14 doesn\'t supports unsecure features');
    564         }
    565         else
    566         {
    567             $client->setDownloadRemote(false);
    568 
    569             $this->assertContains('This is a small demonstration .pdf file', $client->getText($file));
    570         }
     369        $client->setEncoding('UTF-8');
     370
     371        $this->assertThat($client->getText($file), $this->logicalAnd
     372        (
     373            $this->stringContains('L’espéranto'),
     374            $this->stringContains('世界語'),
     375            $this->stringContains('Эспера́нто')
     376        ));
    571377    }
    572378
    573379    /**
    574380     * Test available detectors
    575      *
    576      * @throws  \Exception
    577      */
    578     public function testAvailableDetectors()
    579     {
    580         $this->assertContains('org.apache.tika.mime.MimeTypes', self::$client->getAvailableDetectors());
     381     */
     382    public function testAvailableDetectors(): void
     383    {
     384        $detectors = self::$client->getAvailableDetectors();
     385
     386        $this->assertArrayHasKey('org.apache.tika.detect.DefaultDetector', $detectors);
    581387    }
    582388
    583389    /**
    584390     * Test available parsers
    585      *
    586      * @throws  \Exception
    587      */
    588     public function testAvailableParsers()
    589     {
    590         $this->assertContains('org.apache.tika.parser.DefaultParser', self::$client->getAvailableParsers());
     391     */
     392    public function testAvailableParsers(): void
     393    {
     394        $parsers = self::$client->getAvailableParsers();
     395
     396        $this->assertArrayHasKey('org.apache.tika.parser.DefaultParser', $parsers);
    591397    }
    592398
    593399    /**
    594400     * Test supported MIME types
    595      *
    596      * @throws  \Exception
    597      */
    598     public function testSupportedMIMETypes()
    599     {
    600         $this->assertContains('application/x-pdf', self::$client->getSupportedMIMETypes());
     401     */
     402    public function testSupportedMIMETypes(): void
     403    {
     404        $this->assertArrayHasKey('application/pdf', self::$client->getSupportedMIMETypes());
     405    }
     406
     407
     408    /**
     409     * Test supported MIME types
     410     */
     411    public function testIsMIMETypeSupported(): void
     412    {
     413        $this->assertTrue(self::$client->isMIMETypeSupported('application/pdf'));
    601414    }
    602415
    603416    /**
    604417     * Static method to test callback
    605      *
    606      * @param   string  $chunk
    607      */
    608     public static function callableCallback($chunk)
     418     */
     419    public static function callableCallback(): void
    609420    {
    610421        BaseTest::$shared++;
     
    613424    /**
    614425     * Document file provider
    615      *
    616      * @return  array
    617      */
    618     public function documentProvider()
     426     */
     427    public function documentProvider(): array
    619428    {
    620429        return $this->samples('sample1');
     
    623432    /**
    624433     * Image file provider
    625      *
    626      * @return array
    627      */
    628     public function imageProvider()
     434     */
     435    public function imageProvider(): array
    629436    {
    630437        return $this->samples('sample2');
     
    633440    /**
    634441     * File provider for OCR testing
    635      *
    636      * @return array
    637      */
    638     public function ocrProvider()
     442     */
     443    public function ocrProvider(): array
    639444    {
    640445        return $this->samples('sample3');
     
    643448    /**
    644449     * File provider for callback testing
    645      *
    646      * @return array
    647      */
    648     public function callbackProvider()
     450     */
     451    public function callbackProvider(): array
    649452    {
    650453        return $this->samples('sample5');
     
    653456    /**
    654457     * File provider for remote testing
    655      *
    656      * @return array
    657      */
    658     public function remoteProvider()
     458     */
     459    public function remoteProvider(): array
    659460    {
    660461        return
    661462        [
    662463            [
    663                 'http://www.africau.edu/images/default/sample.pdf'
     464                'https://raw.githubusercontent.com/vaites/php-apache-tika/master/samples/sample6.pdf'
    664465            ]
    665466        ];
     
    667468
    668469    /**
     470     * File provider for encoding testing
     471     */
     472    public function encodingProvider(): array
     473    {
     474        return $this->samples('sample7');
     475    }
     476
     477    /**
     478     * File provider for recursive testing
     479     */
     480    public function recursiveProvider(): array
     481    {
     482        return $this->samples('sample8');
     483    }
     484
     485    /**
    669486     * File provider using "samples" folder
    670      *
    671      * @param   string $sample
    672      * @return  array
    673      */
    674     protected function samples($sample)
     487     */
     488    protected function samples(string $sample): array
    675489    {
    676490        $samples = [];
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/tests/CLITest.php

    r2002534 r2525480  
    1313     * Create shared instances of clients
    1414     */
    15     public static function setUpBeforeClass()
     15    public static function setUpBeforeClass(): void
    1616    {
    1717        self::$client = Client::make(self::getPathForVersion(self::$version));
     
    1919
    2020    /**
     21     * XHTML test
     22     *
     23     * @dataProvider encodingProvider
     24     */
     25    public function testDocumentHTML(string $file): void
     26    {
     27        $this->assertStringStartsWith('<?xml version="1.0"', self::$client->getXHTML($file));
     28    }
     29
     30    /**
    2131     * Set path test
    2232     */
    23     public function testSetPath()
     33    public function testSetPath(): void
    2434    {
    25         $client = Client::make(self::getPathForVersion('1.0'));
     35        $path = self::getPathForVersion(self::$version);
    2636
    27         $this->assertEquals(self::getPathForVersion('1.0'), $client->getPath());
     37        $client = Client::make($path);
     38
     39        $this->assertEquals($path, $client->getPath());
    2840    }
    2941
     
    3143     * Set Java test
    3244     */
    33     public function testSetPort()
     45    public function testSetBinary(): void
    3446    {
    35         $client = Client::make(self::getPathForVersion('1.0'), '/opt/jdk/bin/java');
     47        $path = self::getPathForVersion(self::$version);
    3648
    37         $this->assertEquals('/opt/jdk/bin/java', $client->getJava());
     49        $client = Client::make($path, 'java');
     50
     51        $this->assertEquals('java', $client->getJava());
     52    }
     53
     54    /**
     55     * Set Java test
     56     */
     57    public function testSetArguments(): void
     58    {
     59        $path = self::getPathForVersion(self::$version);
     60
     61        $client = Client::make($path);
     62        $client->setJavaArgs('-JXmx4g');
     63
     64        $this->assertEquals('-JXmx4g', $client->getJavaArgs());
     65    }
     66
     67    /**
     68     * Test delayed check
     69     */
     70    public function testDelayedCheck(): void
     71    {
     72        $path = self::getPathForVersion(self::$version);
     73
     74        $client = Client::prepare('/nonexistent/path/to/apache-tika.jar');
     75        $client->setPath($path);
     76
     77        $this->assertStringEndsWith(self::$version, $client->getVersion());
    3878    }
    3979
    4080    /**
    4181     * Get the full path of Tika app for a specified version
    42      *
    43      * @param   string  $version
    44      * @return  string
    4582     */
    46     private static function getPathForVersion($version)
     83    private static function getPathForVersion(string $version): string
    4784    {
    4885        return self::$binaries . "/tika-app-{$version}.jar";
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/tests/CommonTest.php

    r2002534 r2525480  
    11<?php namespace Vaites\ApacheTika\Tests;
    22
    3 use PHPUnit_Framework_TestCase;
     3use PHPUnit\Framework\TestCase;
    44
    55use Vaites\ApacheTika\Client;
     
    88 * Common test functionality
    99 */
    10 class CommonTest extends PHPUnit_Framework_TestCase
     10class CommonTest extends TestCase
    1111{
    1212    /**
     
    3333    /**
    3434     * Get env variables
    35      *
    36      * @param null      $name
    37      * @param array     $data
    38      * @param string    $dataName
    39      * @throws \Exception
    4035     */
    41     public function __construct($name = null, array $data = array(), $dataName = '')
     36    public function __construct(string $name = null, array $data = array(), $dataName = '')
    4237    {
    4338        self::$version = getenv('APACHE_TIKA_VERSION');
     
    5146     * Set chunk size test
    5247     */
    53     public function testSetChunkSize()
     48    public function testSetChunkSize(): void
    5449    {
    5550        self::$client->setChunkSize(42);
     
    5954
    6055    /**
     56     * Set download remote
     57     */
     58    public function testDownloadRemote(): void
     59    {
     60        self::$client->setDownloadRemote(true);
     61
     62        $this->assertTrue(self::$client->getDownloadRemote());
     63    }
     64
     65    /**
    6166     * Set callback (closure) test
    6267     */
    63     public function testSetClosureCallback()
     68    public function testSetClosureCallback(): void
    6469    {
    6570        self::$client->setCallback(function($chunk)
     
    7479     * Set callback (callable) test
    7580     */
    76     public function testSetCallableCallback()
     81    public function testSetCallableCallback(): void
    7782    {
    7883        self::$client->setCallback('trim');
     
    8489     * Get supported versions test
    8590     */
    86     public function testGetSupportedVersions()
     91    public function testGetSupportedVersions(): void
    8792    {
    88         $this->assertTrue(in_array('1.10', Client::getSupportedVersions()));
     93        $this->assertTrue(in_array(self::$version, self::$client->getSupportedVersions()));
    8994    }
    9095
     
    9297     * Is version supported vtest
    9398     */
    94     public function testIsVersionSupported()
     99    public function testIsVersionSupported(): void
    95100    {
    96         $this->assertTrue(Client::isVersionSupported('1.10'));
     101        $this->assertTrue(self::$client->isVersionSupported(self::$version));
    97102    }
    98103}
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/tests/ErrorTest.php

    r2002534 r2525480  
    22
    33use Exception;
    4 use PHPUnit_Framework_TestCase;
     4
     5use PHPUnit\Framework\TestCase;
    56
    67use Vaites\ApacheTika\Client;
     
    1011 * Error tests
    1112 */
    12 class ErrorTest extends PHPUnit_Framework_TestCase
     13class ErrorTest extends TestCase
    1314{
    1415    /**
     
    2829    /**
    2930     * Get env variables
    30      *
    31      * @param null      $name
    32      * @param array     $data
    33      * @param string    $dataName
    34      */
    35     public function __construct($name = null, array $data = array(), $dataName = '')
     31     */
     32    public function __construct(string $name = null, array $data = array(), $dataName = '')
    3633    {
    3734        self::$version = getenv('APACHE_TIKA_VERSION');
     
    4441     * Test wrong command line mode path
    4542     */
    46     public function testAppPath()
    47     {
    48         try
    49         {
    50             $client = Client::make('/nonexistent/path/to/apache-tika.jar');
     43    public function testAppPath(): void
     44    {
     45        try
     46        {
     47            $client = Client::prepare('/nonexistent/path/to/apache-tika.jar');
    5148            $client->getVersion();
    5249        }
    5350        catch(Exception $exception)
    5451        {
    55             $this->assertContains('Unexpected exit value', $exception->getMessage());
     52            $this->assertStringContainsString('Apache Tika app JAR not found', $exception->getMessage());
    5653        }
    5754    }
     
    6057     * Test unexpected exit value for command line mode
    6158     */
    62     public function testAppExitValue()
     59    public function testAppExitValue(): void
    6360    {
    6461        $path = self::getPathForVersion(self::$version);
     
    7673            rename($path . '.bak', $path);
    7774
    78             $this->assertContains('Unexpected exit value', $exception->getMessage());
     75            $this->assertStringContainsString('Unexpected exit value', $exception->getMessage());
    7976        }
    8077    }
     
    8380     * Test invalid Java binary path for command line mode
    8481     */
    85     public function testAppJavaBinary()
     82    public function testJavaBinary(): void
    8683    {
    8784        $path = self::getPathForVersion(self::$version);
     
    9491        catch(Exception $exception)
    9592        {
    96             $this->assertContains('Unexpected exit value', $exception->getMessage());
     93            $this->assertStringContainsString('Java command not found', $exception->getMessage());
    9794        }
    9895    }
     
    10198     * Test wrong server
    10299     */
    103     public function testServerConnection()
    104     {
    105         try
    106         {
    107             Client::make('localhost', 9997);
    108 
    109             $this->fail();
    110         }
    111         catch(Exception $exception)
    112         {
    113             $this->assertEquals(7, $exception->getCode());
     100    public function testServerConnection(): void
     101    {
     102        try
     103        {
     104            $client = Client::prepare('localhost', 9997);
     105            $client->getVersion();
     106
     107            $this->fail();
     108        }
     109        catch(Exception $exception)
     110        {
     111            $this->assertThat($exception->getCode(), $this->logicalOr
     112            (
     113                $this->equalTo(CURLE_COULDNT_CONNECT),
     114                $this->equalTo(CURLE_OPERATION_TIMEDOUT)
     115            ));
    114116        }
    115117    }
     
    118120     * Test wrong request options
    119121     */
    120     public function testRequestOptions()
    121     {
    122         try
    123         {
    124             $client = Client::make('localhost', 9998, [CURLOPT_PROXY => 'localhost']);
     122    public function testRequestOptions(): void
     123    {
     124        try
     125        {
     126            $client = Client::make('localhost', 9998);
    125127            $client->request('bad');
    126128
     
    129131        catch(Exception $exception)
    130132        {
    131             $this->assertEquals(7, $exception->getCode());
     133            $this->assertStringContainsString('Unknown type bad', $exception->getMessage());
    132134        }
    133135    }
     
    136138     * Test invalidrequest options
    137139     */
    138     public function testRequestRestrictedOptions()
    139     {
    140         try
    141         {
    142             $client = Client::make('localhost', 9998, [CURLOPT_PUT => false]);
     140    public function testRequestRestrictedOptions(): void
     141    {
     142        try
     143        {
     144            Client::make('localhost', 9998, [CURLOPT_PUT => false]);
    143145        }
    144146        catch(Exception $exception)
     
    149151
    150152    /**
     153     * Test wrong recursive metadata type
     154     */
     155    public function testRequestMetadataType(): void
     156    {
     157        try
     158        {
     159            $client = Client::make('localhost', 9998);
     160            $client->getRecursiveMetadata(dirname(__DIR__) . '/samples/sample3.png', 'bad');
     161
     162            $this->fail();
     163        }
     164        catch(Exception $exception)
     165        {
     166            $this->assertStringContainsString('Unknown recursive type', $exception->getMessage());
     167        }
     168    }
     169
     170    /**
    151171     * Test unsupported media type
    152      */
    153     public function testUnsupportedMedia()
     172     *
     173     * NOTE: return value was changed in version 1.23
     174     *
     175     * @link    https://github.com/apache/tika/blob/master/CHANGES.txt
     176     */
     177    public function testUnsupportedMedia(): void
    154178    {
    155179        try
     
    162186        catch(Exception $exception)
    163187        {
    164             $this->assertEquals(415, $exception->getCode());
    165         }
    166     }
    167 
    168     /**
    169      * Test invalid callback
    170      */
    171     public function testInvalidCallback()
    172     {
    173         try
    174         {
    175             $client = Client::make(self::$binaries . '/tika-app-' . self::$version . '.jar');
    176             $client->setCallback('unknown_function');
    177         }
    178         catch(Exception $exception)
    179         {
    180             $this->assertContains('Invalid callback', $exception->getMessage());
     188            if(version_compare(self::$version, '1.23') < 0)
     189            {
     190                $this->assertEquals(415, $exception->getCode());
     191            }
     192            else
     193            {
     194                $this->assertEquals(0, $exception->getCode());
     195            }
     196        }
     197    }
     198
     199    /**
     200     * Test unknown recursive metadata type
     201     */
     202    public function testUnknownRecursiveMetadataType(): void
     203    {
     204        try
     205        {
     206            $client = Client::make('localhost', 9998);
     207            $client->getRecursiveMetadata('example.doc', 'error');
     208
     209            $this->fail();
     210        }
     211        catch(Exception $exception)
     212        {
     213            $this->assertStringContainsString('Unknown recursive type', $exception->getMessage());
    181214        }
    182215    }
     
    185218     * Test invalid chunk size
    186219     */
    187     public function testInvalidChunkSize()
    188     {
    189         try
    190         {
    191             $client = Client::make(self::$binaries . '/tika-app-' . self::$version . '.jar');
    192             $client->setChunkSize('string');
    193         }
    194         catch(Exception $exception)
    195         {
    196             $this->assertContains('is not a valid chunk size', $exception->getMessage());
    197         }
    198     }
    199 
    200     /**
    201      * Test invalid chunk size
    202      */
    203     public function testUnsupportedChunkSize()
     220    public function testUnsupportedChunkSize(): void
    204221    {
    205222        try
     
    210227        catch(Exception $exception)
    211228        {
    212             $this->assertContains('Chunk size is not supported', $exception->getMessage());
    213         }
    214     }
    215 
    216     /**
    217      * Test invalid metadata
    218      */
    219     public function testInvalidMetadata()
    220     {
    221         try
    222         {
    223             $metadata = Metadata::make('InvalidJsonString', './samples/sample1.doc');
    224         }
    225         catch(Exception $exception)
    226         {
    227             $this->assertEquals(JSON_ERROR_SYNTAX, $exception->getCode());
    228         }
    229     }
    230 
    231     /**
    232      * Test empty metadata
    233      */
    234     public function testEmptyMetadata()
    235     {
    236         try
    237         {
    238             $metadata = Metadata::make('', './samples/sample1.doc');
    239         }
    240         catch(Exception $exception)
    241         {
    242             $this->assertContains('Empty response', $exception->getMessage());
     229            $this->assertStringContainsString('Chunk size is not supported', $exception->getMessage());
    243230        }
    244231    }
     
    248235     *
    249236     * @dataProvider    parameterProvider
    250      *
    251      * @param   array   $parameters
    252      */
    253     public function testRequestType($parameters)
     237     */
     238    public function testRequestType(array $parameters): void
    254239    {
    255240        try
     
    262247        catch(Exception $exception)
    263248        {
    264             $this->assertContains('Unknown type bad', $exception->getMessage());
     249            $this->assertStringContainsString('Unknown type bad', $exception->getMessage());
    265250        }
    266251    }
     
    270255     *
    271256     * @dataProvider    parameterProvider
    272      *
    273      * @param   array   $parameters
    274      */
    275     public function testLocalFile($parameters)
     257     */
     258    public function testLocalFile(array $parameters): void
    276259    {
    277260        try
     
    292275     *
    293276     * @dataProvider    parameterProvider
    294      *
    295      * @param   array   $parameters
    296      */
    297     public function testRemoteFile($parameters)
     277     */
     278    public function testRemoteFile(array $parameters): void
    298279    {
    299280        try
     
    312293    /**
    313294     * Client parameters provider
    314      *
    315      * @return array
    316      */
    317     public function parameterProvider()
     295     */
     296    public function parameterProvider(): array
    318297    {
    319298        return
     
    326305    /**
    327306     * Get the full path of Tika app for a specified version
    328      *
    329      * @param   string  $version
    330      * @return  string
    331      */
    332     private static function getPathForVersion($version)
     307     */
     308    private static function getPathForVersion(string $version): string
    333309    {
    334310        return self::$binaries . "/tika-app-{$version}.jar";
  • redisearch/tags/0.3.3/vendor/vaites/php-apache-tika/tests/WebTest.php

    r2002534 r2525480  
    1515     * Start Tika server and create shared instance of clients
    1616     */
    17     public static function setUpBeforeClass()
     17    public static function setUpBeforeClass(): void
    1818    {
    1919        self::$client = Client::make('localhost', 9998, [CURLOPT_TIMEOUT => 30]);
     
    2323     * cURL multiple options test
    2424     */
    25     public function testCurlOptions()
     25    public function testCurlOptions(): void
    2626    {
    2727        $client = Client::make('localhost', 9998, [CURLOPT_TIMEOUT => 3]);
     
    3434     * cURL single option test
    3535     */
    36     public function testCurlSingleOption()
     36    public function testCurlSingleOption(): void
    3737    {
    3838        $client = Client::make('localhost', 9998)->setOption(CURLOPT_TIMEOUT, 3);
     
    4444     * cURL timeout option test
    4545     */
    46     public function testCurlTimeoutOption()
     46    public function testCurlTimeoutOption(): void
    4747    {
    4848        $client = Client::make('localhost', 9998)->setTimeout(3);
     
    5454     * cURL headers test
    5555     */
    56     public function testCurlHeaders()
     56    public function testCurlHeaders(): void
    5757    {
    5858        $header = 'Content-Type: image/jpeg';
     
    6767     * Set host test
    6868     */
    69     public function testSetHost()
     69    public function testSetHost(): void
    7070    {
    7171        $client = Client::make('localhost', 9998);
     
    7878     * Set port test
    7979     */
    80     public function testSetPort()
     80    public function testSetPort(): void
    8181    {
    8282        $client = Client::make('localhost', 9998);
     
    8787
    8888    /**
     89     * Set url host test
     90     */
     91    public function testSetUrlHost(): void
     92    {
     93        $client = Client::make('http://localhost:9998');
     94
     95        $this->assertEquals('localhost', $client->getHost());
     96    }
     97
     98    /**
     99     * Set url port test
     100     */
     101    public function testSetUrlPort(): void
     102    {
     103        $client = Client::make('http://localhost:9998');
     104
     105        $this->assertEquals(9998, $client->getPort());
     106    }
     107
     108    /**
    89109     * Set retries test
    90110     */
    91     public function testRetriesPort()
     111    public function testSetRetries(): void
    92112    {
    93113        $client = Client::make('localhost', 9998);
     
    96116        $this->assertEquals(5, $client->getRetries());
    97117    }
     118
     119    /**
     120     * Test delayed check
     121     */
     122    public function testDelayedCheck(): void
     123    {
     124        $client = Client::prepare('localhost', 9997);
     125        $client->setPort(9998);
     126
     127        $this->assertStringEndsWith(self::$version, $client->getVersion());
     128    }
    98129}
  • redisearch/tags/0.3.3/wp-redisearch.php

    r2455651 r2525480  
    22/*
    33Plugin Name: RediSearch
    4 Version: 0.3.2
     4Version: 0.3.3
    55Description: Replace Wordpress search by RediSearch.
    66Author: Foad Yousefi
     
    2929// Plugin version .
    3030if ( ! defined( 'WPRS_VERSION' ) ) {
    31     define( 'WPRS_VERSION', '0.3.2' );
     31    define( 'WPRS_VERSION', '0.3.3' );
    3232}
    3333
  • redisearch/trunk/README.md

    r2455651 r2525480  
    5353
    5454### Changelog
     55
     56##### 0.3.3
     57* **Added**: Ability to connect via UNIX sockets
     58
    5559
    5660##### 0.3.2
  • redisearch/trunk/readme.txt

    r2455651 r2525480  
    66Tags: search, redisearch, redis, fuzzy, aggregation, searching, autosuggest, suggest, advanced search, woocommerce
    77Requires at least: 5.0
    8 Tested up to: 5.6
    9 Stable tag: 0.3.2
     8Tested up to: 5.7
     9Stable tag: 0.3.3
    1010Requires PHP: 7.2
    1111License: GPLv2 or later
     
    6666
    6767== Changelog ==
     68
     69= 0.3.3 =
     70* Added: Ability to connect via UNIX sockets
    6871
    6972= 0.3.2 =
  • redisearch/trunk/src/Admin/Admin.php

    r2455651 r2525480  
    151151  public static function RedisServerConf() {
    152152    Fields::add( 'header', null, __( 'Redis server configurations', 'wp-redisearch' ) );
    153     Fields::add( 'text', 'wp_redisearch_server', __( 'Redis server', 'wp-redisearch' ), __( 'Redis server url, usually it is 127.0.0.1', 'wp-redisearch' ) );
     153    Fields::add( 'select', 'wp_redisearch_connection_scheme', __( 'Server communication scheme', 'wp-redisearch' ), __( 'Choose Redis server communication scheme.', 'wp-redisearch'), array('tcp' => 'TCP', 'unix' => 'UNIX') );
     154    Fields::add( 'text', 'wp_redisearch_server', __( 'Redis server/path', 'wp-redisearch' ), __( 'Redis server url, usually it is 127.0.0.1 and in case of using UNIX socket, the socket path.', 'wp-redisearch' ) );
    154155    Fields::add( 'text', 'wp_redisearch_port', __( 'Redis port', 'wp-redisearch' ), __( 'Redis port number, by default it is 6379', 'wp-redisearch' ) );
    155156    Fields::add( 'password', 'wp_redisearch_password', __( 'Redis server password', 'wp-redisearch' ), __( 'If your redis server is not password protected, leave this field blank', 'wp-redisearch' ) );
  • redisearch/trunk/src/RediSearch/Client.php

    r2452159 r2525480  
    1919      Settings::RedisPort(),
    2020      Settings::RedisPassword(),
    21       0
     21      0,
     22      Settings::RedisScheme()
    2223    );
    2324  }
  • redisearch/trunk/src/RedisRaw/PredisAdapter.php

    r2452159 r2525480  
    1717  public $redis;
    1818 
    19   public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null): RedisRawClientInterface {
    20     $this->redis = new Client([
    21       'scheme' => 'tcp',
    22       'host' => $hostname,
    23       'port' => $port,
    24       'database' => $db,
    25       'password' => $password,
    26     ]);
     19  public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null, $scheme = 'tcp'): RedisRawClientInterface {
     20    $clientArgs = array();
     21    if ( $scheme === 'tcp' ) {
     22      $clientArgs = array(
     23      'scheme'    => 'tcp',
     24      'host'      => $hostname,
     25      'port'      => $port,
     26      'database'  => $db,
     27      'password'  => $password,
     28      );
     29    } else {
     30      $clientArgs = array(
     31        'scheme'    => 'unix',
     32        'path'      => $host
     33      );
     34    }
     35    $this->redis = new Client( $clientArgs );
    2736    $this->redis->connect();
    2837    return $this;
  • redisearch/trunk/src/Settings.php

    r2452159 r2525480  
    4545   
    4646    return isset( $redis_server ) && !empty( $redis_server ) ? $redis_server : '127.0.0.1';
     47  }
     48
     49  /**
     50  * Using UNIX socket for communicating with Redis server or not.
     51  * @since    0.1.0
     52  * @param
     53  * @return bool    $unixServer
     54  */
     55  public static function RedisScheme() {
     56    /**
     57     * First we try to get the WP_REDIS_SCHEME option from wp-config.php
     58     * @since 0.3.3
     59     */
     60    $redis_server = ( defined( 'WP_REDIS_SCHEME' ) ) ? WP_REDIS_SCHEME : get_option( 'wp_redisearch_connection_scheme' );
     61
     62    return get_option( 'wp_redisearch_connection_scheme' );
    4763  }
    4864
  • redisearch/trunk/vendor/foadyousefi/seven-fields/src/Fields/Fields.php

    r2013762 r2525480  
    7777          update_option( $name, $value );
    7878          break;
     79        case 'select':
     80          update_option( $name, $value );
     81          break;
    7982        case 'checkbox':
    8083          $value = isset( $value ) ? true : false;
  • redisearch/trunk/vendor/foadyousefi/seven-fields/src/Fields/Select.php

    r1949960 r2525480  
    3939    self::$description = $description;
    4040    self::$options = $options;
     41    // Save value in as wp_option in database
     42    parent::save( self::$name, 'select' );
    4143    return $this->field_html();
    4244  }
  • redisearch/trunk/vendor/front/redisearch/src/RedisRaw/PredisAdapter.php

    r2452159 r2525480  
    1717  public $redis;
    1818 
    19   public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null): RedisRawClientInterface {
    20     $this->redis = new Client([
    21       'scheme' => 'tcp',
    22       'host' => $hostname,
    23       'port' => $port,
    24       'database' => $db,
    25       'password' => $password,
    26     ]);
     19  public function connect($hostname = '127.0.0.1', $port = 6379, $db = 0, $password = null, $scheme = 'tcp'): RedisRawClientInterface {
     20    $clientArgs = array(
     21      'database'  => $db,
     22      'password'  => $password
     23    );
     24    if ( $scheme === 'tcp' ) {
     25      $clientArgs = array(
     26        'scheme'    => 'tcp',
     27        'port'      => $port,
     28        'host'      => $hostname
     29      );
     30    } else if ( $scheme === 'unix' ) {
     31      $clientArgs = array(
     32        'scheme'    => 'unix',
     33        'path'      => $host
     34      );
     35    }
     36    $this->redis = new Client( $clientArgs );
    2737    $this->redis->connect();
    2838    return $this;
  • redisearch/trunk/vendor/front/redisearch/src/Setup.php

    r2452159 r2525480  
    1717   * @return PredisAdapter
    1818   */
    19   public static function connect( $server = '127.0.0.1', $port = 6379, $password = null, $database = 0 ) {
     19  public static function connect( $server = '127.0.0.1', $port = 6379, $password = null, $database = 0, $scheme = 'tcp' ) {
    2020    // Connect to server
    21     $client = ( new PredisAdapter() )->connect( $server, $port, $database, $password );
     21    $client = ( new PredisAdapter() )->connect( $server, $port, $database, $password, $database, $scheme );
    2222    return $client;
    2323  }
  • redisearch/trunk/vendor/vaites/php-apache-tika/src/Client.php

    r2002534 r2525480  
    55use Closure;
    66use Exception;
     7use stdClass;
    78
    89use Vaites\ApacheTika\Clients\CLIClient;
    910use Vaites\ApacheTika\Clients\WebClient;
     11use Vaites\ApacheTika\Metadata\Metadata;
     12use Vaites\ApacheTika\Metadata\MetadataInterface;
    1013
    1114/**
     
    1316 *
    1417 * @author  David Martínez <contacto@davidmartinez.net>
    15  * @link    http://wiki.apache.org/tika/TikaJAXRS
    16  * @link    https://tika.apache.org/1.10/formats.html
     18 * @link    https://tika.apache.org/1.24/formats.html
    1719 */
    1820abstract class Client
    1921{
    20     /**
    21      * List of supported Apache Tika versions
     22    protected const MODE = null;
     23
     24    /**
     25     * Checked flag
     26     *
     27     * @var bool
     28     */
     29    protected $checked = false;
     30
     31    /**
     32     * Response using callbacks
     33     *
     34     * @var string
     35     */
     36    protected $response = null;
     37
     38    /**
     39     * Platform (unix or win)
     40     *
     41     * @var string
     42     */
     43    protected $platform = null;
     44
     45    /**
     46     * Cached responses to avoid multiple request for the same file.
    2247     *
    2348     * @var array
    2449     */
    25     protected static $supportedVersions =
    26     [
    27         '1.7', '1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.19.1'
    28     ];
    29 
    30     /**
    31      * Response using callbacks
    32      *
    33      * @var string
    34      */
    35     protected $response = null;
    36 
    37     /**
    38      * Cached responses to avoid multiple request for the same file.
    39      *
    40      * @var array
    41      */
    4250    protected $cache = [];
    4351
    4452    /**
     53     * Text encoding
     54     *
     55     * @var string|null
     56     */
     57    protected $encoding = null;
     58
     59    /**
    4560     * Callback called on secuential read
    4661     *
    47      * @var \Closure
     62     * @var callable|null
    4863     */
    4964    protected $callback = null;
    5065
    5166    /**
     67     * Enable or disable appending when using callback
     68     *
     69     * @var bool
     70     */
     71    protected $callbackAppend = true;
     72
     73    /**
    5274     * Size of chunks for callback
    5375     *
     
    6486
    6587    /**
    66      * Get a class instance
    67      *
    68      * @param   string  $param1     path or host
    69      * @param   int     $param2     Java binary path or port for web client
    70      * @param   array   $options    options for cURL request
    71      * @return  \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
    72      * @throws  \Exception
    73      */
    74     public static function make($param1 = null, $param2 = null, $options = [])
    75     {
    76         if (preg_match('/\.jar$/', func_get_arg(0)))
    77         {
    78             return new CLIClient($param1, $param2);
     88     * Configure client
     89     */
     90    public function __construct()
     91    {
     92        $this->platform = defined('PHP_WINDOWS_VERSION_MAJOR') ? 'win' : 'unix';
     93    }
     94
     95    /**
     96     * Get a class instance throwing an exception if check fails
     97     *
     98     * @param string     $param1 path or host
     99     * @param string|int $param2 Java binary path or port for web client
     100     * @param array      $options options for cURL request
     101     * @param bool       $check check JAR file or server connection
     102     * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
     103     * @throws \Exception
     104     */
     105    public static function make(string $param1 = null, $param2 = null, array $options = [], bool $check = true): Client
     106    {
     107        if(preg_match('/\.jar$/', func_get_arg(0)))
     108        {
     109            return new CLIClient($param1, $param2, $check);
    79110        }
    80111        else
    81112        {
    82             return new WebClient($param1, $param2, $options);
    83         }
     113            return new WebClient($param1, $param2, $options, $check);
     114        }
     115    }
     116
     117    /**
     118     * Get a class instance delaying the check
     119     *
     120     * @param string $param1 path or host
     121     * @param int    $param2 Java binary path or port for web client
     122     * @param array  $options options for cURL request
     123     * @return \Vaites\ApacheTika\Clients\CLIClient|\Vaites\ApacheTika\Clients\WebClient
     124     * @throws \Exception
     125     */
     126    public static function prepare($param1 = null, $param2 = null, $options = []): Client
     127    {
     128        return self::make($param1, $param2, $options, false);
     129    }
     130
     131    /**
     132     * Get the encoding
     133     */
     134    public function getEncoding(): ?string
     135    {
     136        return $this->encoding;
     137    }
     138
     139    /**
     140     * Set the encoding
     141     *
     142     * @throws \Exception
     143     */
     144    public function setEncoding(string $encoding): self
     145    {
     146        if(!empty($encoding))
     147        {
     148            $this->encoding = $encoding;
     149        }
     150        else
     151        {
     152            throw new Exception('Invalid encoding');
     153        }
     154
     155        return $this;
    84156    }
    85157
    86158    /**
    87159     * Get the callback
    88      *
    89      * @return  \Closure|null
    90      */
    91     public function getCallback()
     160     */
     161    public function getCallback(): ?Closure
    92162    {
    93163        return $this->callback;
     
    97167     * Set the callback (callable or closure) for call on secuential read
    98168     *
    99      * @param   mixed   $callback
    100      * @return  $this
    101      * @throws  \Exception
    102      */
    103     public function setCallback($callback)
    104     {
    105         if($callback instanceof Closure)
    106         {
     169     * @throws \Exception
     170     */
     171    public function setCallback(callable $callback, bool $append = true): self
     172    {
     173        if($callback instanceof Closure || is_array($callback))
     174        {
     175            $this->callbackAppend = (bool) $append;
    107176            $this->callback = $callback;
    108177        }
    109         elseif(is_callable($callback))
    110         {
    111             $this->callback = function($chunk) use($callback)
     178        elseif(is_string($callback))
     179        {
     180            $this->callbackAppend = (bool) $append;
     181            $this->callback = function($chunk) use ($callback)
    112182            {
    113183                return call_user_func_array($callback, [$chunk]);
     
    124194    /**
    125195     * Get the chunk size
    126      *
    127      * @return  int
    128      */
    129     public function getChunkSize()
     196     */
     197    public function getChunkSize(): int
    130198    {
    131199        return $this->chunkSize;
     
    135203     * Set the chunk size for secuential read
    136204     *
    137      * @param   int     $size
    138      * @return  $this
    139      * @throws  \Exception
    140      */
    141     public function setChunkSize($size)
    142     {
    143         if(static::MODE == 'cli' && is_numeric($size))
    144         {
    145             $this->chunkSize = (int)$size;
    146         }
    147         elseif(static::MODE == 'web')
     205     * @throws \Exception
     206     */
     207    public function setChunkSize(int $size): self
     208    {
     209        if(static::MODE == 'cli')
     210        {
     211            $this->chunkSize = $size;
     212        }
     213        else
    148214        {
    149215            throw new Exception('Chunk size is not supported on web mode');
    150216        }
    151         else
    152         {
    153             throw new Exception("$size is not a valid chunk size");
    154         }
    155217
    156218        return $this;
     
    159221    /**
    160222     * Get the remote download flag
    161      *
    162      * @return  bool
    163      */
    164     public function getDownloadRemote()
     223     */
     224    public function getDownloadRemote(): bool
    165225    {
    166226        return $this->downloadRemote;
     
    169229    /**
    170230     * Set the remote download flag
    171      *
    172      * @param   bool    $download
    173      * @return  $this
    174      */
    175     public function setDownloadRemote($download)
     231     */
     232    public function setDownloadRemote(bool $download): self
    176233    {
    177234        $this->downloadRemote = (bool) $download;
     
    183240     * Gets file metadata
    184241     *
    185      * @param   string  $file
    186      * @return  \Vaites\ApacheTika\Metadata\Metadata
    187      * @throws  \Exception
    188      */
    189     public function getMetadata($file)
    190     {
    191         return $this->request('meta', $file);
     242     * @throws \Exception
     243     */
     244    public function getMetadata(string $file): MetadataInterface
     245    {
     246        $response = $this->parseJsonResponse($this->request('meta', $file));
     247
     248        if($response instanceof stdClass == false)
     249        {
     250            throw new Exception("Unexpected metadata response for $file");
     251        }
     252
     253        return Metadata::make($response, $file);
     254    }
     255
     256    /**
     257     * Gets recursive file metadata where the returned array indexes are the file name.
     258     *
     259     * Example: for a sample.zip with an example.doc file, the return array looks like if be defined as:
     260     *
     261     *  [
     262     *      'sample.zip' => new Metadata()
     263     *      'sample.zip/example.doc' => new DocumentMetadata()
     264     *  ]
     265     *
     266     * @link https://cwiki.apache.org/confluence/display/TIKA/TikaServer#TikaServer-RecursiveMetadataandContent
     267     * @throws \Exception
     268     */
     269    public function getRecursiveMetadata(string $file, ?string $format = 'ignore'): array
     270    {
     271        if(in_array($format, ['text', 'html', 'ignore']) == false)
     272        {
     273            throw new Exception("Unknown recursive type (must be text, html, ignore or null)");
     274        }
     275
     276        $response = $this->parseJsonResponse($this->request("rmeta/$format", $file));
     277
     278        if(is_array($response) == false)
     279        {
     280            throw new Exception("Unexpected metadata response for $file");
     281        }
     282
     283        $metadata = [];
     284
     285        foreach($response as $item)
     286        {
     287            $name = basename($file);
     288            if(isset($item->{'X-TIKA:embedded_resource_path'}))
     289            {
     290                $name .= $item->{'X-TIKA:embedded_resource_path'};
     291            }
     292
     293            $metadata[$name] = Metadata::make($item, $file);
     294        }
     295
     296        return $metadata;
    192297    }
    193298
     
    195300     * Detect language
    196301     *
    197      * @param   string  $file
    198      * @return  string
    199      * @throws  \Exception
    200      */
    201     public function getLanguage($file)
     302     * @throws \Exception
     303     */
     304    public function getLanguage(string $file): string
    202305    {
    203306        return $this->request('lang', $file);
     
    207310     * Detect MIME type
    208311     *
    209      * @param   string  $file
    210      * @return  string
    211      * @throws \Exception
    212      */
    213     public function getMIME($file)
     312     * @throws \Exception
     313     */
     314    public function getMIME(string $file): string
    214315    {
    215316        return $this->request('mime', $file);
     
    219320     * Extracts HTML
    220321     *
    221      * @param   string  $file
    222      * @param   mixed   $callback
    223      * @return  string
    224      * @throws  \Exception
    225      */
    226     public function getHTML($file, $callback = null)
     322     * @throws \Exception
     323     */
     324    public function getHTML(string $file, callable $callback = null, bool $append = true): string
    227325    {
    228326        if(!is_null($callback))
    229327        {
    230             $this->setCallback($callback);
     328            $this->setCallback($callback, $append);
    231329        }
    232330
     
    235333
    236334    /**
     335     * Extracts XHTML
     336     *
     337     * @throws \Exception
     338     */
     339    public function getXHTML(string $file, callable $callback = null, bool $append = true): string
     340    {
     341        if(!is_null($callback))
     342        {
     343            $this->setCallback($callback, $append);
     344        }
     345
     346        return $this->request('xhtml', $file);
     347    }
     348
     349    /**
    237350     * Extracts text
    238351     *
    239      * @param   string  $file
    240      * @param   mixed   $callback
    241      * @return  string
    242      * @throws  \Exception
    243      */
    244     public function getText($file, $callback = null)
     352     * @throws \Exception
     353     */
     354    public function getText(string $file, callable $callback = null, bool $append = true): string
    245355    {
    246356        if(!is_null($callback))
    247357        {
    248             $this->setCallback($callback);
     358            $this->setCallback($callback, $append);
    249359        }
    250360
     
    255365     * Extracts main text
    256366     *
    257      * @param   string  $file
    258      * @param   mixed   $callback
    259      * @return  string
    260      * @throws  \Exception
    261      */
    262     public function getMainText($file, $callback = null)
     367     * @throws \Exception
     368     */
     369    public function getMainText(string $file, callable $callback = null, bool $append = true): string
    263370    {
    264371        if(!is_null($callback))
    265372        {
    266             $this->setCallback($callback);
     373            $this->setCallback($callback, $append);
    267374        }
    268375
     
    271378
    272379    /**
    273      * Returns the supported MIME types
    274      *
    275      * @return  string
    276      * @throws  \Exception
    277      */
    278     public function getSupportedMIMETypes()
    279     {
    280         return $this->request('mime-types');
    281     }
    282 
    283     /**
    284      * Returns the available detectors
    285      *
    286      * @return  string
    287      * @throws  \Exception
    288      */
    289     public function getAvailableDetectors()
    290     {
    291         return $this->request('detectors');
    292     }
    293 
    294     /**
    295      * Returns the available parsers
    296      *
    297      * @return  string
    298      * @throws  \Exception
    299      */
    300     public function getAvailableParsers()
    301     {
    302         return $this->request('parsers');
    303     }
    304 
    305     /**
    306380     * Returns current Tika version
    307381     *
    308      * @return  string
    309      * @throws  \Exception
    310      */
    311     public function getVersion()
     382     * @throws \Exception
     383     */
     384    public function getVersion(): string
    312385    {
    313386        return $this->request('version');
     
    317390     * Return the list of Apache Tika supported versions
    318391     *
    319      * @return array
    320      */
    321     public static function getSupportedVersions()
    322     {
    323         return self::$supportedVersions;
     392     * @throws \Exception
     393     */
     394    public function getSupportedVersions(): array
     395    {
     396        static $versions = null;
     397
     398        if(is_null($versions))
     399        {
     400            $composer = json_decode(file_get_contents(dirname(__DIR__) . '/composer.json'), true);
     401            $versions = $composer['extra']['supported-versions'] ?? null;
     402
     403            if(empty($versions))
     404            {
     405                throw new Exception("An error ocurred trying to read package's composer.json file");
     406            }
     407        }
     408
     409        return $versions;
     410    }
     411
     412    /**
     413     * Sets the checked flag
     414     */
     415    public function setChecked(bool $checked): self
     416    {
     417        $this->checked = (bool) $checked;
     418
     419        return $this;
     420    }
     421
     422    /**
     423     * Checks if instance is checked
     424     */
     425    public function isChecked(): bool
     426    {
     427        return $this->checked;
     428    }
     429
     430    /**
     431     * Check if a response is cached
     432     */
     433    protected function isCached(string $type, string $file): bool
     434    {
     435        return isset($this->cache[sha1($file)][$type]);
     436    }
     437
     438    /**
     439     * Get a cached response
     440     */
     441    protected function getCachedResponse(string $type, string $file)
     442    {
     443        return $this->cache[sha1($file)][$type] ?? null;
     444    }
     445
     446    /**
     447     * Check if a request type must be cached
     448     */
     449    protected function isCacheable(string $type): bool
     450    {
     451        return in_array($type, ['lang', 'meta']);
     452    }
     453
     454    /**
     455     * Caches a response
     456     */
     457    protected function cacheResponse(string $type, $response, string $file): bool
     458    {
     459        $this->cache[sha1($file)][$type] = $response;
     460
     461        return true;
    324462    }
    325463
    326464    /**
    327465     * Checks if a specific version is supported
    328      *
    329      * @param   string  $version
    330      * @return  bool
    331      */
    332     public static function isVersionSupported($version)
    333     {
    334         return in_array($version, self::getSupportedVersions());
     466     */
     467    public function isVersionSupported(string $version): bool
     468    {
     469        return in_array($version, $this->getSupportedVersions());
     470    }
     471
     472    /**
     473     * Check if a mime type is supported
     474     *
     475     * @param string $mime
     476     * @return bool
     477     * @throws \Exception
     478     */
     479    public function isMIMETypeSupported(string $mime): bool
     480    {
     481        return array_key_exists($mime, $this->getSupportedMIMETypes());
    335482    }
    336483
     
    338485     * Check the request before executing
    339486     *
    340      * @param   string  $type
    341      * @param   string  $file
    342      * @return  string
    343      * @throws  \Exception
    344      */
    345     public function checkRequest($type, $file)
     487     * @throws \Exception
     488     */
     489    public function checkRequest(string $type, string $file = null): ?string
    346490    {
    347491        // no checks for getters
     
    349493        {
    350494            //
    351         }
    352         // invalid local file
     495        } // invalid local file
    353496        elseif(!preg_match('/^http/', $file) && !file_exists($file))
    354497        {
    355498            throw new Exception("File $file can't be opened");
    356         }
    357         // invalid remote file
     499        } // invalid remote file
    358500        elseif(preg_match('/^http/', $file) && !preg_match('/200/', get_headers($file)[0]))
    359501        {
    360502            throw new Exception("File $file can't be opened", 2);
    361         }
    362         // download remote file if required only for integrated downloader
     503        } // download remote file if required only for integrated downloader
    363504        elseif(preg_match('/^http/', $file) && $this->downloadRemote)
    364505        {
     
    370511
    371512    /**
     513     * Parse the response returned by Apache Tika
     514     *
     515     * @return mixed
     516     * @throws \Exception
     517     */
     518    protected function parseJsonResponse(string $response)
     519    {
     520        // an empty response throws an error
     521        if(empty($response) || trim($response) == '')
     522        {
     523            throw new Exception('Empty response');
     524        }
     525
     526        // decode the JSON response
     527        $json = json_decode($response);
     528
     529        // exceptions if metadata is not valid
     530        if(json_last_error())
     531        {
     532            $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Error parsing JSON response';
     533
     534            throw new Exception($message, json_last_error());
     535        }
     536
     537        return $json;
     538    }
     539
     540    /**
    372541     * Download file to a temporary folder
    373542     *
    374      * @link    https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
    375      * @param   string  $file
    376      * @return  string
    377      * @throws  \Exception
    378      */
    379     protected function downloadFile($file)
     543     * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
     544     * @throws \Exception
     545     */
     546    protected function downloadFile(string $file): string
    380547    {
    381548        $dest = tempnam(sys_get_temp_dir(), 'TIKA');
     
    411578
    412579    /**
     580     * Must return the supported MIME types
     581     *
     582     * @throws \Exception
     583     */
     584    abstract public function getSupportedMIMETypes(): array;
     585
     586    /**
     587     * Must return the available detectors
     588     *
     589     * @throws \Exception
     590     */
     591    abstract public function getAvailableDetectors(): array;
     592
     593    /**
     594     * Must return the available parsers
     595     *
     596     * @throws \Exception
     597     */
     598    abstract public function getAvailableParsers(): array;
     599
     600    /**
     601     * Check Java binary, JAR path or server connection
     602     */
     603    abstract public function check(): void;
     604
     605    /**
    413606     * Configure and make a request and return its results.
    414607     *
    415      * @param   string  $type
    416      * @param   string  $file
    417      * @return  string
    418      * @throws  \Exception
    419      */
    420     abstract public function request($type, $file);
     608     * @throws \Exception
     609     */
     610    abstract public function request(string $type, string $file = null): string;
    421611}
  • redisearch/trunk/vendor/vaites/php-apache-tika/src/Clients/CLIClient.php

    r2002534 r2525480  
    66
    77use Vaites\ApacheTika\Client;
    8 use Vaites\ApacheTika\Metadata\Metadata;
    98
    109/**
     
    1211 *
    1312 * @author  David Martínez <contacto@davidmartinez.net>
    14  * @link    http://wiki.apache.org/tika/TikaJAXRS
    15  * @link    https://tika.apache.org/1.12/formats.html
     13 * @link    https://tika.apache.org/1.23/gettingstarted.html#Using_Tika_as_a_command_line_utility
    1614 */
    1715class CLIClient extends Client
    1816{
    19     const MODE = 'cli';
     17    protected const MODE = 'cli';
    2018
    2119    /**
     
    3432
    3533    /**
     34     * Java arguments
     35     *
     36     * @var string
     37     */
     38    protected $javaArgs = null;
     39
     40    /**
    3641     * Configure client
    3742     *
    38      * @param   string  $path
    39      * @param   string  $java
    40      *
    41      * @throws Exception
    42      */
    43     public function __construct($path = null, $java = null)
    44     {
     43     * @throws \Exception
     44     */
     45    public function __construct(string $path = null, string $java = null, bool $check = true)
     46    {
     47        parent::__construct();
     48
    4549        if($path)
    4650        {
     
    5256            $this->setJava($java);
    5357        }
     58
     59        if($check === true)
     60        {
     61            $this->check();
     62        }
    5463    }
    5564
    5665    /**
    5766     * Get the path
    58      *
    59      * @return  null|string
    60      */
    61     public function getPath()
     67     */
     68    public function getPath(): ?string
    6269    {
    6370        return $this->path;
     
    6673    /**
    6774     * Set the path
    68      *
    69      * @param   string  $path
    70      * @return  $this
    71      */
    72     public function setPath($path)
     75     */
     76    public function setPath($path): self
    7377    {
    7478        $this->path = $path;
     
    7983    /**
    8084     * Get the Java path
    81      *
    82      * @return  null|int
    83      */
    84     public function getJava()
     85     */
     86    public function getJava(): ?string
    8587    {
    8688        return $this->java;
     
    8991    /**
    9092     * Set the Java path
    91      *
    92      * @param   string    $java
    93      * @return  $this
    94      */
    95     public function setJava($java)
     93     */
     94    public function setJava(string $java): self
    9695    {
    9796        $this->java = $java;
     
    101100
    102101    /**
     102     * Get the Java arguments
     103     */
     104    public function getJavaArgs(): ?string
     105    {
     106        return $this->javaArgs;
     107    }
     108
     109    /**
     110     * Set the Java arguments
     111     *
     112     * NOTE: to modify child process jvm args, prepend "J" to each argument (-JXmx4g)
     113     */
     114    public function setJavaArgs(string $args): self
     115    {
     116        $this->javaArgs = $args;
     117
     118        return $this;
     119    }
     120
     121    /**
     122     * Returns the supported MIME types
     123     *
     124     * NOTE: the data provided by the CLI must be parsed: mime type has no spaces, aliases go next prefixed with spaces
     125     *
     126     * @throws \Exception
     127     */
     128    public function getSupportedMIMETypes(): array
     129    {
     130        $mime = null;
     131        $mimeTypes = [];
     132
     133        $response = preg_split("/\n/", $this->request('mime-types'));
     134
     135        foreach($response as $line)
     136        {
     137            if(preg_match('/^\w+/', $line))
     138            {
     139                $mime = trim($line);
     140                $mimeTypes[$mime] = ['alias' => []];
     141            }
     142            else
     143            {
     144                [$key, $value] = preg_split('/:\s+/', trim($line));
     145
     146                if($key == 'alias')
     147                {
     148                    $mimeTypes[$mime]['alias'][] = $value;
     149                }
     150                else
     151                {
     152                    $mimeTypes[$mime][$key] = $value;
     153                }
     154            }
     155        }
     156
     157
     158        return $mimeTypes;
     159    }
     160
     161    /**
     162     * Returns the available detectors
     163     *
     164     * @throws \Exception
     165     */
     166    public function getAvailableDetectors(): array
     167    {
     168        $detectors = [];
     169
     170        $split = preg_split("/\n/", $this->request('detectors'));
     171
     172        $parent = null;
     173        foreach($split as $line)
     174        {
     175            if(preg_match('/composite/i', $line))
     176            {
     177                $parent = trim(preg_replace('/\(.+\):/', '', $line));
     178                $detectors[$parent] = ['children' => [], 'composite' => true, 'name' => $parent];
     179            }
     180            else
     181            {
     182                $child = trim($line);
     183                $detectors[$parent]['children'][$child] = ['composite' => false, 'name' => $child];
     184            }
     185        }
     186
     187        return $detectors;
     188    }
     189
     190    /**
     191     * Returns the available parsers
     192     *
     193     * @throws \Exception
     194     */
     195    public function getAvailableParsers(): array
     196    {
     197        $parsers = [];
     198
     199        $split = preg_split("/\n/", $this->request('parsers'));
     200        array_shift($split);
     201
     202        $parent = null;
     203        foreach($split as $line)
     204        {
     205            if(preg_match('/composite/i', $line))
     206            {
     207                $parent = trim(preg_replace('/\(.+\):/', '', $line));
     208
     209                $parsers[$parent] = ['children' => [], 'composite' => true, 'name' => $parent, 'decorated' => false];
     210            }
     211            else
     212            {
     213                $child = trim($line);
     214
     215                $parsers[$parent]['children'][$child] = ['composite' => false, 'name' => $child, 'decorated' => false];
     216            }
     217        }
     218
     219        return $parsers;
     220    }
     221
     222    /**
     223     * Check Java binary, JAR path or server connection
     224     *
     225     * @throws \Exception
     226     */
     227    public function check(): void
     228    {
     229        if($this->isChecked() === false)
     230        {
     231            // Java command must not return an error
     232            try
     233            {
     234                $this->exec(($this->java ?: 'java') . ' -version');
     235            }
     236            catch(Exception $exception)
     237            {
     238                throw new Exception('Java command not found');
     239            }
     240
     241            // JAR path must exists
     242            if(file_exists($this->path) === false)
     243            {
     244                throw new Exception('Apache Tika app JAR not found');
     245            }
     246
     247            $this->setChecked(true);
     248        }
     249    }
     250
     251    /**
    103252     * Configure and make a request and return its results
    104253     *
    105      * @param   string  $type
    106      * @param   string  $file
    107      * @return string
    108      * @throws  \Exception
    109      */
    110     public function request($type, $file = null)
    111     {
     254     * @throws \Exception
     255     */
     256    public function request(string $type, string $file = null): string
     257    {
     258        // check if not checked
     259        $this->check();
     260
    112261        // check if is cached
    113         if(isset($this->cache[sha1($file)][$type]))
    114         {
    115             return $this->cache[sha1($file)][$type];
     262        if($file !== null && $this->isCached($type, $file))
     263        {
     264            return $this->getCachedResponse($type, $file);
    116265        }
    117266
     
    120269
    121270        // check the request
    122         $file = parent::checkRequest($type, $file);
     271        $file = $this->checkRequest($type, $file);
    123272
    124273        // add last argument
     
    130279        // build command
    131280        $jar = escapeshellarg($this->path);
    132         $command = ($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments);
     281        $command = trim(($this->java ?: 'java') . " -jar $jar " . implode(' ', $arguments) . " {$this->javaArgs}");
    133282
    134283        // run command
     
    136285
    137286        // metadata response
    138         if($type == 'meta')
     287        if(in_array(preg_replace('/\/.+/', '', $type), ['meta', 'rmeta']))
    139288        {
    140289            // fix for invalid? json returned only with images
    141290            $response = str_replace(basename($file) . '"}{', '", ', $response);
    142291
    143             // on Windows, response comes in another charset
    144             if(defined('PHP_WINDOWS_VERSION_MAJOR'))
    145             {
    146                 $response = utf8_encode($response);
    147             }
    148 
    149             $response = Metadata::make($response, $file);
     292            // on Windows, response must be encoded to UTF8
     293            $response = $this->platform == 'win' ? utf8_encode($response) : $response;
    150294        }
    151295
    152296        // cache certain responses
    153         if(in_array($type, ['lang', 'meta']))
    154         {
    155             $this->cache[sha1($file)][$type] = $response;
     297        if($this->isCacheable($type))
     298        {
     299            $this->cacheResponse($type, $response, $file);
    156300        }
    157301
     
    162306     * Run the command and return its results
    163307     *
    164      * @param   string  $command
    165      * @return  null|string
    166      * @throws  \Exception
    167      */
    168     public function exec($command)
     308     * @throws \Exception
     309     */
     310    public function exec(string $command): ?string
    169311    {
    170312        // run command
     
    187329                }
    188330
    189                 $this->response .= $chunk;
     331                if($this->callbackAppend === true)
     332                {
     333                    $this->response .= $chunk;
     334                }
    190335            }
    191336            fclose($pipes[1]);
     
    205350     * Get the arguments to run the command
    206351     *
    207      * @param   string  $type
    208      * @param   string  $file
    209      * @return  array
    210352     * @throws  Exception
    211353     */
    212     protected function getArguments($type, $file = null)
    213     {
    214         // parameters for command
    215         $arguments = [];
     354    protected function getArguments(string $type, string $file = null): array
     355    {
     356        $arguments = $this->encoding ? ["--encoding={$this->encoding}"] : [];
     357
    216358        switch($type)
    217359        {
     
    256398                break;
    257399
     400            case 'rmeta/ignore':
     401                $arguments[] = '--metadata --jsonRecursive';
     402                break;
     403
     404            case 'rmeta/html':
     405                $arguments[] = '--html --jsonRecursive';
     406                break;
     407
     408            case 'rmeta/text':
     409                $arguments[] = '--text --jsonRecursive';
     410                break;
     411
     412            case 'xhtml':
     413                $arguments[] = '--xml';
     414                break;
     415
    258416            default:
    259                 throw new Exception("Unknown type $type");
     417                throw new Exception($file ? "Unknown type $type for $file" : "Unknown type $type");
    260418        }
    261419
  • redisearch/trunk/vendor/vaites/php-apache-tika/src/Clients/WebClient.php

    r2002534 r2525480  
    66
    77use Vaites\ApacheTika\Client;
    8 use Vaites\ApacheTika\Metadata\Metadata;
    98
    109/**
     
    1211 *
    1312 * @author  David Martínez <contacto@davidmartinez.net>
    14  * @link    http://wiki.apache.org/tika/TikaJAXRS
    15  * @link    https://tika.apache.org/1.12/formats.html
     13 * @link    https://cwiki.apache.org/confluence/display/TIKA/TikaServer
    1614 */
    1715class WebClient extends Client
    1816{
    19     const MODE = 'web';
     17    protected const MODE = 'web';
    2018
    2119    /**
     
    3129     * @var string
    3230     */
    33     protected $host = '127.0.0.1';
     31    protected $host = null;
    3432
    3533    /**
     
    3836     * @var int
    3937     */
    40     protected $port = 9998;
     38    protected $port = null;
    4139
    4240    /**
     
    5452    protected $options =
    5553    [
    56         CURLINFO_HEADER_OUT    => true,
    57         CURLOPT_HTTPHEADER     => [],
    58         CURLOPT_PUT            => true,
    59         CURLOPT_RETURNTRANSFER => true,
    60         CURLOPT_TIMEOUT        => 5,
     54        CURLINFO_HEADER_OUT     => true,
     55        CURLOPT_HTTPHEADER      => [],
     56        CURLOPT_PUT             => true,
     57        CURLOPT_RETURNTRANSFER  => true,
     58        CURLOPT_TIMEOUT         => 5
    6159    ];
    6260
     
    6462     * Configure class and test if server is running
    6563     *
    66      * @param   string  $host
    67      * @param   int     $port
    68      * @param   array   $options
    69      * @throws  \Exception
    70      */
    71     public function __construct($host = null, $port = null, $options = [])
    72     {
    73         if($host)
     64     * @throws \Exception
     65     */
     66    public function __construct(string $host = null, int $port = null, array $options = [], bool $check = true)
     67    {
     68        parent::__construct();
     69
     70        if(is_string($host) && filter_var($host, FILTER_VALIDATE_URL))
     71        {
     72            $this->setUrl($host);
     73        }
     74        elseif($host)
    7475        {
    7576            $this->setHost($host);
    7677        }
    7778
    78         if($port)
     79        if(is_numeric($port))
    7980        {
    8081            $this->setPort($port);
     
    8889        $this->setDownloadRemote(true);
    8990
    90         $this->getVersion(); // exception if not running
     91        if($check === true)
     92        {
     93            $this->check();
     94        }
     95    }
     96
     97    /**
     98     * Get the base URL
     99     */
     100    public function getUrl(): string
     101    {
     102        return sprintf('http://%s:%d', $this->host, $this->port ?: 9998);
     103    }
     104
     105    /**
     106     * Set the host and port using an URL
     107     */
     108    public function setUrl(string $url): self
     109    {
     110        $url = parse_url($url);
     111
     112        $this->setHost($url['host']);
     113
     114        if(isset($url['port']))
     115        {
     116            $this->setPort($url['port']);
     117        }
     118
     119        return $this;
    91120    }
    92121
    93122    /**
    94123     * Get the host
    95      *
    96      * @return  null|string
    97      */
    98     public function getHost()
     124     */
     125    public function getHost(): ?string
    99126    {
    100127        return $this->host;
     
    103130    /**
    104131     * Set the host
    105      *
    106      * @param   string  $host
    107      * @return  $this
    108      */
    109     public function setHost($host)
     132     */
     133    public function setHost(string $host): self
    110134    {
    111135        $this->host = $host;
     
    116140    /**
    117141     * Get the port
    118      *
    119      * @return  null|int
    120      */
    121     public function getPort()
     142     */
     143    public function getPort(): ?int
    122144    {
    123145        return $this->port;
     
    126148    /**
    127149     * Set the port
    128      *
    129      * @param   int     $port
    130      * @return  $this
    131      */
    132     public function setPort($port)
     150     */
     151    public function setPort(int $port): self
    133152    {
    134153        $this->port = $port;
     
    139158    /**
    140159     * Get the number of retries
    141      *
    142      * @return  int
    143      */
    144     public function getRetries()
     160     */
     161    public function getRetries(): int
    145162    {
    146163        return $this->retries;
     
    149166    /**
    150167     * Set the number of retries
    151      *
    152      * @param   int     $retries
    153      * @return  $this
    154      */
    155     public function setRetries($retries)
     168     */
     169    public function setRetries(int $retries): self
    156170    {
    157171        $this->retries = $retries;
     
    162176    /**
    163177     * Get all the options
    164      *
    165      * @return  null|array
    166      */
    167     public function getOptions()
     178     */
     179    public function getOptions(): array
    168180    {
    169181        return $this->options;
     
    173185     * Get an specified option
    174186     *
    175      * @param   string  $key
    176187     * @return  mixed
    177188     */
    178     public function getOption($key)
    179     {
    180         return isset($this->options[$key]) ? $this->options[$key] : null;
     189    public function getOption(int $key)
     190    {
     191        return $this->options[$key] ?? null;
    181192    }
    182193
     
    184195     * Set a cURL option to be set with curl_setopt()
    185196     *
    186      * @link    http://php.net/manual/en/curl.constants.php
    187      * @link    http://php.net/manual/en/function.curl-setopt.php
    188      * @param   string  $key
    189      * @param   mixed   $value
    190      * @return  $this
    191      * @throws  \Exception
    192      */
    193     public function setOption($key, $value)
     197     * @link http://php.net/manual/en/curl.constants.php
     198     * @link http://php.net/manual/en/function.curl-setopt.php
     199     * @throws \Exception
     200     */
     201    public function setOption(int $key, $value): self
    194202    {
    195203        if(in_array($key, [CURLINFO_HEADER_OUT, CURLOPT_PUT, CURLOPT_RETURNTRANSFER]))
     
    206214     * Set the cURL options
    207215     *
    208      * @param   array   $options
    209      * @return  $this
    210      * @throws  \Exception
    211      */
    212     public function setOptions($options)
     216     * @throws \Exception
     217     */
     218    public function setOptions(array $options): self
    213219    {
    214220        foreach($options as $key => $value)
     
    222228    /**
    223229     * Get the timeout value for cURL
    224      *
    225      * @return  int
    226      */
    227     public function getTimeout()
     230     */
     231    public function getTimeout(): int
    228232    {
    229233        return $this->getOption(CURLOPT_TIMEOUT);
     
    233237     * Set the timeout value for cURL
    234238     *
    235      * @param   int     $value
    236      * @return  $this
    237      * @throws  \Exception
    238      */
    239     public function setTimeout($value)
     239     * @throws \Exception
     240     */
     241    public function setTimeout(int $value): self
    240242    {
    241243        $this->setOption(CURLOPT_TIMEOUT, (int) $value);
     
    245247
    246248    /**
     249     * Returns the supported MIME types
     250     *
     251     * @throws \Exception
     252     */
     253    public function getSupportedMIMETypes(): array
     254    {
     255        $mimeTypes = json_decode($this->request('mime-types'), true);
     256
     257        ksort($mimeTypes);
     258
     259        return $mimeTypes;
     260    }
     261
     262    /**
     263     * Returns the available detectors
     264     *
     265     * @throws \Exception
     266     */
     267    public function getAvailableDetectors(): array
     268    {
     269        $detectors = [json_decode($this->request('detectors'), true)];
     270
     271        foreach($detectors as $index => $parent)
     272        {
     273            $detectors[$parent['name']] = $parent;
     274
     275            if(isset($parent['children']))
     276            {
     277                foreach($parent['children'] as $subindex => $child)
     278                {
     279                    $detectors[$parent['name']]['children'][$child['name']] = $child;
     280
     281                    unset($detectors[$parent['name']]['children'][$subindex]);
     282                }
     283            }
     284
     285            unset($detectors[$index]);
     286        }
     287
     288        return $detectors;
     289    }
     290
     291    /**
     292     * Returns the available parsers
     293     *
     294     * @throws \Exception
     295     */
     296    public function getAvailableParsers(): array
     297    {
     298        $parsers = [json_decode($this->request('parsers'), true)];
     299
     300        foreach($parsers as $index => $parent)
     301        {
     302            $parsers[$parent['name']] = $parent;
     303
     304            if(isset($parent['children']))
     305            {
     306                foreach($parent['children'] as $subindex => $child)
     307                {
     308                    $parsers[$parent['name']]['children'][$child['name']] = $child;
     309
     310                    unset($parsers[$parent['name']]['children'][$subindex]);
     311                }
     312            }
     313
     314            unset($parsers[$index]);
     315        }
     316
     317        return $parsers;
     318    }
     319
     320    /**
     321     * Check if server is running
     322     *
     323     * @throws \Exception
     324     */
     325    public function check(): void
     326    {
     327        if($this->isChecked() === false)
     328        {
     329            $this->setChecked(true);
     330
     331            // throws an exception if server is unreachable or can't connect
     332            $this->request('version');
     333        }
     334    }
     335
     336    /**
    247337     * Configure, make a request and return its results
    248338     *
    249      * @param   string  $type
    250      * @param   string  $file
    251      * @return  string
    252      * @throws  \Exception
    253      */
    254     public function request($type, $file = null)
     339     * @throws \Exception
     340     */
     341    public function request(string $type, string $file = null): string
    255342    {
    256343        static $retries = [];
    257344
     345        // check if not checked
     346        $this->check();
     347
    258348        // check if is cached
    259         if(isset($this->cache[sha1($file)][$type]))
    260         {
    261             return $this->cache[sha1($file)][$type];
     349        if($file !== null && $this->isCached($type, $file))
     350        {
     351            return $this->getCachedResponse($type, $file);
    262352        }
    263353        elseif(!isset($retries[sha1($file)]))
     
    267357
    268358        // parameters for cURL request
    269         list($resource, $headers) = $this->getParameters($type, $file);
     359        [$resource, $headers] = $this->getParameters($type, $file);
    270360
    271361        // check the request
    272         $file = parent::checkRequest($type, $file);
     362        $file = $this->checkRequest($type, $file);
    273363
    274364        // cURL options
     
    282372
    283373        // cURL init and options
    284         $options[CURLOPT_URL] = "http://{$this->host}:{$this->port}" . "/$resource";
     374        $options[CURLOPT_URL] = $this->getUrl() . "/$resource";
    285375
    286376        // get the response and the HTTP status code
    287         list($response, $status) = $this->exec($options);
     377        [$response, $status] = $this->exec($options);
     378
     379        // reduce memory usage closing cURL resource
     380        if(isset($options[CURLOPT_INFILE]) && is_resource($options[CURLOPT_INFILE]))
     381        {
     382            fclose($options[CURLOPT_INFILE]);
     383        }
    288384
    289385        // request completed successfully
    290386        if($status == 200)
    291387        {
    292             if($type == 'meta')
     388            // cache certain responses
     389            if($this->isCacheable($type))
    293390            {
    294                 $response = Metadata::make($response, $file);
     391                $this->cacheResponse($type, $response, $file);
    295392            }
    296 
    297             // cache certain responses
    298             if(in_array($type, ['lang', 'meta']))
    299             {
    300                 $this->cache[sha1($file)][$type] = $response;
    301             }
    302         }
    303         // request completed successfully but result is empty
     393        } // request completed successfully but result is empty
    304394        elseif($status == 204)
    305395        {
    306396            $response = null;
    307         }
    308         // retry on request failed with error 500
     397        } // retry on request failed with error 500
    309398        elseif($status == 500 && $retries[sha1($file)]--)
    310399        {
    311400            $response = $this->request($type, $file);
    312         }
    313         // other status code is an error
     401        } // other status code is an error
    314402        else
    315403        {
    316             $this->error($status, $resource);
     404            $this->error($status, $resource, $file);
    317405        }
    318406
     
    323411     * Make a request to Apache Tika Server
    324412     *
    325      * @param   array   $options
    326      * @return  array
    327      * @throws  \Exception
    328      */
    329     protected function exec(array $options = [])
     413     * @throws \Exception
     414     */
     415    protected function exec(array $options = []): array
    330416    {
    331417        // cURL init and options
    332418        $curl = curl_init();
    333419
    334         // we avoid curl_setopt_array($curl, $options) because extrange Windows behaviour (issue #8)
     420        // we avoid curl_setopt_array($curl, $options) because strange Windows behaviour (issue #8)
    335421        foreach($options as $option => $value)
    336422        {
     
    338424        }
    339425
    340         // make the request
     426        // make the request directly
    341427        if(is_null($this->callback))
    342428        {
    343             $this->response = curl_exec($curl);
    344         }
     429            $this->response = curl_exec($curl) ?: '';
     430        } // with a callback, the response is appended on each block inside the callback
    345431        else
    346432        {
     
    362448     * Throws an exception for an error status code
    363449     *
    364      * @codeCoverageIgnore
    365      *
    366      * @param   int       $status
    367      * @param   string    $resource
    368      * @throws  \Exception
    369      */
    370     protected function error($status, $resource)
     450     * @throws \Exception
     451     */
     452    protected function error(int $status, string $resource, string $file = null): void
    371453    {
    372454        switch($status)
     
    374456            //  method not allowed
    375457            case 405:
    376                 throw new Exception('Method not allowed', 405);
     458                $message = 'Method not allowed';
    377459                break;
    378460
    379461            //  unsupported media type
    380462            case 415:
    381                 throw new Exception('Unsupported media type', 415);
     463                $message = 'Unsupported media type';
    382464                break;
    383465
    384466            //  unprocessable entity
    385467            case 422:
    386                 throw new Exception('Unprocessable document', 422);
     468                $message = 'Unprocessable document';
     469
     470                // using remote files require Tika server to be launched with specific options
     471                if($this->downloadRemote == false && preg_match('/^http/', $file))
     472                {
     473                    $message .= ' (is server launched using "-enableUnsecureFeatures -enableFileUrl" arguments?)';
     474                }
     475
    387476                break;
    388477
    389478            // server error
    390479            case 500:
    391                 throw new Exception('Error while processing document', 500);
     480                $message = 'Error while processing document';
    392481                break;
    393482
    394483            // unexpected
    395484            default:
    396                 throw new Exception("Unexpected response for /$resource ($status)", 501);
    397         }
     485                $message = "Unexpected response for /$resource ($status)";
     486                $status = 501;
     487        }
     488
     489        throw new Exception($message, $status);
    398490    }
    399491
     
    401493     * Get the parameters to make the request
    402494     *
    403      * @link    https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
    404      * @param   string  $type
    405      * @param   string  file
    406      * @return  array
    407      * @throws  \Exception
    408      */
    409     protected function getParameters($type, $file = null)
     495     * @link https://wiki.apache.org/tika/TikaJAXRS#Specifying_a_URL_Instead_of_Putting_Bytes
     496     * @throws \Exception
     497     */
     498    protected function getParameters(string $type, string $file = null): array
    410499    {
    411500        $headers = [];
     501        $callback = null;
    412502
    413503        if(!empty($file) && preg_match('/^http/', $file))
     
    433523                break;
    434524
     525            case 'detectors':
     526            case 'parsers':
    435527            case 'meta':
    436                 $resource = 'meta';
     528            case 'mime-types':
     529            case 'rmeta/html':
     530            case 'rmeta/ignore':
     531            case 'rmeta/text':
     532                $resource = $type;
    437533                $headers[] = 'Accept: application/json';
     534                $callback = function($response)
     535                {
     536                    return json_decode($response, true);
     537                };
    438538                break;
    439539
     
    448548                break;
    449549
    450             case 'detectors':
    451             case 'parsers':
    452             case 'mime-types':
    453550            case 'version':
    454551                $resource = $type;
    455552                break;
    456553
     554            case 'xhtml':
     555                throw new Exception("Tika Server does not support XHTML output");
     556                break;
     557
    457558            default:
    458559                throw new Exception("Unknown type $type");
    459560        }
    460561
    461         return [$resource, $headers];
     562        return [$resource, $headers, $callback];
    462563    }
    463564
     
    465566     * Get the cURL options
    466567     *
    467      * @param   string  $type
    468      * @param   string  file
    469      * @return  array
    470      * @throws  \Exception
    471      */
    472     protected function getCurlOptions($type, $file = null)
     568     * @throws \Exception
     569     */
     570    protected function getCurlOptions(string $type, string $file = null): array
    473571    {
    474572        // base options
     
    480578            $callback = $this->callback;
    481579
    482             $options[CURLOPT_WRITEFUNCTION] = function($handler, $data) use($callback)
     580            $options[CURLOPT_WRITEFUNCTION] = function($handler, $data) use ($callback)
    483581            {
    484                 $this->response .= $data;
     582                if($this->callbackAppend === true)
     583                {
     584                    $this->response .= $data;
     585                }
    485586
    486587                $callback($data);
     
    495596        {
    496597            //
    497         }
    498         // local file options
     598        } // local file options
    499599        elseif($file && file_exists($file) && is_readable($file))
    500600        {
    501601            $options[CURLOPT_INFILE] = fopen($file, 'r');
    502602            $options[CURLOPT_INFILESIZE] = filesize($file);
    503         }
    504         // other options for specific requests
    505         elseif(in_array($type,  ['detectors', 'mime-types', 'parsers', 'version']))
     603        } // other options for specific requests
     604        elseif(in_array($type, ['detectors', 'mime-types', 'parsers', 'version']))
    506605        {
    507606            $options[CURLOPT_PUT] = false;
    508         }
    509         // file not accesible
     607        } // file not accesible
    510608        else
    511609        {
  • redisearch/trunk/vendor/vaites/php-apache-tika/tests/BaseTest.php

    r2002534 r2525480  
    33namespace Vaites\ApacheTika\Tests;
    44
    5 use PHPUnit_Framework_TestCase;
     5use Exception;
     6
     7use PHPUnit\Framework\TestCase;
    68
    79/**
    810 * Common test functionality
    911 */
    10 abstract class BaseTest extends PHPUnit_Framework_TestCase
     12abstract class BaseTest extends TestCase
    1113{
    1214    /**
     
    4042    /**
    4143     * Get env variables
    42      *
    43      * @param null      $name
    44      * @param array     $data
    45      * @param string    $dataName
    46      */
    47     public function __construct($name = null, array $data = array(), $dataName = '')
     44     *
     45     * @throws \Exception
     46     */
     47    public function __construct(string $name = null, array $data = array(), $dataName = '')
    4848    {
    4949        self::$version = getenv('APACHE_TIKA_VERSION');
    5050        self::$binaries = getenv('APACHE_TIKA_BINARIES');
    5151
     52        if(empty(self::$version))
     53        {
     54            throw new Exception('APACHE_TIKA_VERSION environment variable not defined');
     55        }
     56
    5257        parent::__construct($name, $data, $dataName);
    5358    }
     
    5661     * Version test
    5762     */
    58     public function testVersion()
     63    public function testVersion(): void
    5964    {
    6065        $this->assertEquals('Apache Tika ' . self::$version, self::$client->getVersion());
     
    6469     * Metadata test
    6570     *
    66      * @dataProvider    documentProvider
    67      *
    68      * @param   string $file
    69      * @param   string $class
    70      * @throws  \Exception
    71      */
    72     public function testMetadata($file, $class = 'Metadata')
     71     * @dataProvider documentProvider
     72     */
     73    public function testMetadata(string $file, string $class = 'Metadata'): void
     74    {
     75        $this->assertInstanceOf("\\Vaites\\ApacheTika\\Metadata\\$class", self::$client->getMetadata($file));
     76    }
     77
     78    /**
     79     * Metadata test
     80     *
     81     * @dataProvider documentProvider
     82     */
     83    public function testDocumentMetadata(string $file, string $class = 'DocumentMetadata'): void
     84    {
     85        $this->testMetadata($file, $class);
     86    }
     87
     88    /**
     89     * Metadata title test
     90     *
     91     * @dataProvider documentProvider
     92     */
     93    public function testDocumentMetadataTitle(string $file): void
     94    {
     95        $this->assertEquals('Lorem ipsum dolor sit amet', self::$client->getMetadata($file)->title);
     96    }
     97
     98    /**
     99     * Metadata author test
     100     *
     101     * @dataProvider documentProvider
     102     */
     103    public function testDocumentMetadataAuthor(string $file): void
     104    {
     105        $this->assertEquals('David Martínez', self::$client->getMetadata($file)->author);
     106    }
     107
     108    /**
     109     * Metadata dates test
     110     *
     111     * @dataProvider documentProvider
     112     */
     113    public function testDocumentMetadataCreated(string $file): void
     114    {
     115        $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->created);
     116    }
     117
     118    /**
     119     * Metadata dates test
     120     *
     121     * @dataProvider documentProvider
     122     */
     123    public function testDocumentMetadataUpdated(string $file): void
     124    {
     125        $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->updated);
     126    }
     127
     128    /**
     129     * Metadata keywords test
     130     *
     131     * @dataProvider documentProvider
     132     */
     133    public function testDocumentMetadataKeywords(string $file): void
     134    {
     135        $this->assertContains('ipsum', self::$client->getMetadata($file)->keywords);
     136    }
     137
     138    /**
     139     * Recursive text metadata test
     140     *
     141     * @dataProvider recursiveProvider
     142     */
     143    public function testTextRecursiveMetadata(string $file): void
     144    {
     145        $nested = 'sample8.zip/sample1.doc';
     146
     147        $metadata = self::$client->getRecursiveMetadata($file, 'text');
     148
     149        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', $metadata[$nested]->content ?? 'ERROR');
     150    }
     151
     152    /**
     153     * Recursive HTML metadata test
     154     *
     155     * @dataProvider recursiveProvider
     156     */
     157    public function testHtmlRecursiveMetadata(string $file): void
     158    {
     159        $nested = 'sample8.zip/sample1.doc';
     160
     161        $metadata = self::$client->getRecursiveMetadata($file, 'html');
     162
     163        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', $metadata[$nested]->content ?? 'ERROR');
     164    }
     165
     166    /**
     167     * Recursive ignore metadata test
     168     *
     169     * @dataProvider ocrProvider
     170     */
     171    public function testIgnoreRecursiveMetadata(string $file): void
     172    {
     173        $metadata = self::$client->getRecursiveMetadata($file, 'ignore');
     174
     175        $this->assertNull(array_shift($metadata)->content);
     176    }
     177
     178    /**
     179     * Language test
     180     *
     181     * @dataProvider documentProvider
     182     */
     183    public function testDocumentLanguage(string $file): void
     184    {
     185        $this->assertRegExp('/^[a-z]{2}$/', self::$client->getLanguage($file));
     186    }
     187
     188    /**
     189     * MIME test
     190     *
     191     * @dataProvider documentProvider
     192     */
     193    public function testDocumentMIME(string $file): void
     194    {
     195        $this->assertNotEmpty(self::$client->getMIME($file));
     196    }
     197
     198    /**
     199     * HTML test
     200     *
     201     * @dataProvider documentProvider
     202     */
     203    public function testDocumentHTML(string $file): void
     204    {
     205        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', self::$client->getHTML($file));
     206    }
     207
     208    /**
     209     * Text test
     210     *
     211     * @dataProvider documentProvider
     212     */
     213    public function testDocumentText(string $file): void
     214    {
     215        $this->assertStringContainsString('Zenonis est, inquam, hoc Stoici', self::$client->getText($file));
     216    }
     217
     218    /**
     219     * Main text test
     220     *
     221     * @dataProvider documentProvider
     222     */
     223    public function testDocumentMainText(string $file): void
     224    {
     225        $this->assertStringContainsString('Lorem ipsum dolor sit amet', self::$client->getMainText($file));
     226    }
     227
     228    /**
     229     * Metadata test
     230     *
     231     * @dataProvider imageProvider
     232     */
     233    public function testImageMetadata(string $file, string $class = 'ImageMetadata'): void
     234    {
     235        $this->testMetadata($file, $class);
     236    }
     237
     238    /**
     239     * Metadata width test
     240     *
     241     * @dataProvider imageProvider
     242     */
     243    public function testImageMetadataWidth(string $file): void
     244    {
     245        $meta = self::$client->getMetadata($file);
     246
     247        $this->assertEquals(1600, $meta->width, basename($file));
     248    }
     249
     250    /**
     251     * Metadata height test
     252     *
     253     * @dataProvider imageProvider
     254     */
     255    public function testImageMetadataHeight(string $file): void
     256    {
     257        $meta = self::$client->getMetadata($file);
     258
     259        $this->assertEquals(900, $meta->height, basename($file));
     260    }
     261
     262    /**
     263     * OCR test
     264     *
     265     * @dataProvider ocrProvider
     266     */
     267    public function testImageOCR(string $file): void
     268    {
     269        $text = self::$client->getText($file);
     270
     271        $this->assertRegExp('/voluptate/i', $text);
     272    }
     273
     274    /**
     275     * Text callback test
     276     *
     277     * @dataProvider callbackProvider
     278     */
     279    public function testTextCallback(string $file): void
     280    {
     281        BaseTest::$shared = 0;
     282
     283        self::$client->getText($file, [$this, 'callableCallback']);
     284
     285        $this->assertGreaterThan(1, BaseTest::$shared);
     286    }
     287
     288    /**
     289     * Text callback test
     290     *
     291     * @dataProvider callbackProvider
     292     */
     293    public function testTextCallbackWithoutAppend(string $file): void
     294    {
     295        BaseTest::$shared = 0;
     296
     297        $response = self::$client->getText($file, [$this, 'callableCallback'], false);
     298
     299        $this->assertEmpty($response);
     300    }
     301
     302    /**
     303     * Main text callback test
     304     *
     305     * @dataProvider callbackProvider
     306     */
     307    public function testMainTextCallback(string $file): void
     308    {
     309        BaseTest::$shared = 0;
     310
     311        self::$client->getMainText($file, function()
     312        {
     313            BaseTest::$shared++;
     314        });
     315
     316        $this->assertGreaterThan(1, BaseTest::$shared);
     317    }
     318
     319    /**
     320     * Main text callback test
     321     *
     322     * @dataProvider callbackProvider
     323     */
     324    public function testHtmlCallback(string $file): void
     325    {
     326        BaseTest::$shared = 0;
     327
     328        self::$client->getHtml($file, function()
     329        {
     330            BaseTest::$shared++;
     331        });
     332
     333        $this->assertGreaterThan(1, BaseTest::$shared);
     334    }
     335
     336    /**
     337     * Remote file test with integrated download
     338     *
     339     * @dataProvider remoteProvider
     340     */
     341    public function testRemoteDocumentText(string $file): void
     342    {
     343        $this->assertStringContainsString('Rationis enim perfectio est virtus', self::$client->getText($file));
     344    }
     345
     346    /**
     347     * Remote file test with internal downloader
     348     *
     349     * @dataProvider remoteProvider
     350     */
     351    public function testDirectRemoteDocumentText(string $file): void
    73352    {
    74353        $client =& self::$client;
    75354
    76         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    77         {
    78             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    79         }
    80         else
    81         {
    82             $this->assertInstanceOf("\\Vaites\\ApacheTika\\Metadata\\$class", self::$client->getMetadata($file));
    83         }
    84     }
    85 
    86     /**
    87      * Metadata test
    88      *
    89      * @dataProvider    documentProvider
    90      *
    91      * @param   string $file
    92      * @param   string $class
    93      * @throws  \Exception
    94      */
    95     public function testDocumentMetadata($file, $class = 'DocumentMetadata')
     355        $client->setDownloadRemote(false);
     356
     357        $this->assertStringContainsString('Rationis enim perfectio est virtus', $client->getText($file));
     358    }
     359
     360    /**
     361     * Encoding tests
     362     *
     363     * @dataProvider encodingProvider
     364     */
     365    public function testEncodingDocumentText(string $file): void
    96366    {
    97367        $client =& self::$client;
    98368
    99         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    100         {
    101             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    102         }
    103         else
    104         {
    105             $this->testMetadata($file, $class);
    106         }
    107     }
    108 
    109     /**
    110      * Metadata title test
    111      *
    112      * @dataProvider    documentProvider
    113      *
    114      * @param   string $file
    115      * @throws  \Exception
    116      */
    117     public function testDocumentMetadataTitle($file)
    118     {
    119         $client =& self::$client;
    120 
    121         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    122         {
    123             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    124         }
    125         else
    126         {
    127             $this->assertEquals('Lorem ipsum dolor sit amet', self::$client->getMetadata($file)->title);
    128         }
    129     }
    130 
    131     /**
    132      * Metadata author test
    133      *
    134      * @dataProvider    documentProvider
    135      *
    136      * @param   string $file
    137      * @throws  \Exception
    138      */
    139     public function testDocumentMetadataAuthor($file)
    140     {
    141         $client =& self::$client;
    142 
    143         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    144         {
    145             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    146         }
    147         else
    148         {
    149             $this->assertEquals('David Martínez', self::$client->getMetadata($file)->author);
    150         }
    151     }
    152 
    153     /**
    154      * Metadata dates test
    155      *
    156      * @dataProvider    documentProvider
    157      *
    158      * @param   string $file
    159      * @throws  \Exception
    160      */
    161     public function testDocumentMetadataCreated($file)
    162     {
    163         $client =& self::$client;
    164 
    165         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    166         {
    167             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    168         }
    169         else
    170         {
    171             $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->created);
    172         }
    173     }
    174 
    175     /**
    176      * Metadata dates test
    177      *
    178      * @dataProvider    documentProvider
    179      *
    180      * @param   string $file
    181      * @throws  \Exception
    182      */
    183     public function testDocumentMetadataUpdated($file)
    184     {
    185         $client =& self::$client;
    186 
    187         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    188         {
    189             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    190         }
    191         else
    192         {
    193             $this->assertInstanceOf('DateTime', self::$client->getMetadata($file)->updated);
    194         }
    195     }
    196 
    197     /**
    198      * Metadata keywords test
    199      *
    200      * @dataProvider    documentProvider
    201      *
    202      * @param   string $file
    203      * @throws  \Exception
    204      */
    205     public function testDocumentMetadataKeywords($file)
    206     {
    207         $client =& self::$client;
    208 
    209         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    210         {
    211             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    212         }
    213         else
    214         {
    215             $this->assertContains('ipsum', self::$client->getMetadata($file)->keywords);
    216         }
    217     }
    218 
    219     /**
    220      * Language test
    221      *
    222      * @dataProvider    documentProvider
    223      *
    224      * @param   string $file
    225      * @throws  \Exception
    226      */
    227     public function testDocumentLanguage($file)
    228     {
    229         $client =& self::$client;
    230 
    231         if($client::MODE == 'web' && version_compare(self::$version, '1.9') < 0)
    232         {
    233             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks REST language identification');
    234         }
    235         else
    236         {
    237             $this->assertRegExp('/^[a-z]{2}$/', self::$client->getLanguage($file));
    238         }
    239     }
    240 
    241     /**
    242      * MIME test
    243      *
    244      * @dataProvider    documentProvider
    245      *
    246      * @param   string $file
    247      * @throws  \Exception
    248      */
    249     public function testDocumentMIME($file)
    250     {
    251         $client =& self::$client;
    252 
    253         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    254         {
    255             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    256         }
    257         else
    258         {
    259             $this->assertNotEmpty(self::$client->getMIME($file));
    260         }
    261     }
    262 
    263     /**
    264      * HTML test
    265      *
    266      * @dataProvider    documentProvider
    267      *
    268      * @param   string $file
    269      * @throws  \Exception
    270      */
    271     public function testDocumentHTML($file)
    272     {
    273         $client =& self::$client;
    274 
    275         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    276         {
    277             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    278         }
    279         else
    280         {
    281             $this->assertContains('Zenonis est, inquam, hoc Stoici', self::$client->getHTML($file));
    282         }
    283     }
    284 
    285     /**
    286      * Text test
    287      *
    288      * @dataProvider    documentProvider
    289      *
    290      * @param   string $file
    291      * @throws  \Exception
    292      */
    293     public function testDocumentText($file)
    294     {
    295         $client =& self::$client;
    296 
    297         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    298         {
    299             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    300         }
    301         else
    302         {
    303             $this->assertContains('Zenonis est, inquam, hoc Stoici', self::$client->getText($file));
    304         }
    305     }
    306 
    307     /**
    308      * Main text test
    309      *
    310      * @dataProvider    documentProvider
    311      *
    312      * @param   string $file
    313      * @throws  \Exception
    314      */
    315     public function testDocumentMainText($file)
    316     {
    317         $client =& self::$client;
    318 
    319         if($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    320         {
    321             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks main content extraction');
    322         }
    323         else
    324         {
    325             $this->assertContains('Lorem ipsum dolor sit amet', self::$client->getMainText($file));
    326         }
    327     }
    328 
    329     /**
    330      * Metadata test
    331      *
    332      * @dataProvider    imageProvider
    333      *
    334      * @param   string $file
    335      * @param   string $class
    336      * @throws  \Exception
    337      */
    338     public function testImageMetadata($file, $class = 'ImageMetadata')
    339     {
    340         $client =& self::$client;
    341 
    342         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    343         {
    344             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    345         }
    346         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    347         {
    348             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    349         }
    350         else
    351         {
    352             $this->testMetadata($file, $class);
    353         }
    354     }
    355 
    356     /**
    357      * Metadata width test
    358      *
    359      * @dataProvider    imageProvider
    360      *
    361      * @param   string $file
    362      * @throws  \Exception
    363      */
    364     public function testImageMetadataWidth($file)
    365     {
    366         $client =& self::$client;
    367 
    368         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    369         {
    370             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    371         }
    372         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    373         {
    374             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    375         }
    376         else
    377         {
    378             $meta = self::$client->getMetadata($file);
    379 
    380             $this->assertEquals(1600, $meta->width, basename($file));
    381         }
    382     }
    383 
    384     /**
    385      * Metadata height test
    386      *
    387      * @dataProvider    imageProvider
    388      *
    389      * @param   string $file
    390      * @throws  \Exception
    391      */
    392     public function testImageMetadataHeight($file)
    393     {
    394         $client =& self::$client;
    395 
    396         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    397         {
    398             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    399         }
    400         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    401         {
    402             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    403         }
    404         else
    405         {
    406             $meta = self::$client->getMetadata($file);
    407 
    408             $this->assertEquals(900, $meta->height, basename($file));
    409         }
    410     }
    411 
    412     /**
    413      * OCR test
    414      *
    415      * @dataProvider    ocrProvider
    416      *
    417      * @param   string $file
    418      * @throws  \Exception
    419      */
    420     public function testImageOCR($file)
    421     {
    422         $client =& self::$client;
    423 
    424         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    425         {
    426             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    427         }
    428         elseif($client::MODE == 'web' && version_compare(self::$version, '1.14') == 0)
    429         {
    430             $this->markTestSkipped('Apache Tika 1.14 throws "Expected \';\', got \',\'> when parsing some images');
    431         }
    432         else
    433         {
    434             $text = self::$client->getText($file);
    435 
    436             $this->assertRegExp('/voluptate/i', $text);
    437         }
    438     }
    439 
    440     /**
    441      * Text callback test
    442      *
    443      * @dataProvider    callbackProvider
    444      *
    445      * @param   string $file
    446      * @throws  \Exception
    447      */
    448     public function testTextCallback($file)
    449     {
    450         $client =& self::$client;
    451        
    452     if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    453         {
    454             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    455         }
    456         else
    457         {
    458             BaseTest::$shared = 0;
    459 
    460             self::$client->getText($file, [$this, 'callableCallback']);
    461 
    462             $this->assertGreaterThan(1, BaseTest::$shared);
    463         }
    464     }
    465 
    466     /**
    467      * Main text callback test
    468      *
    469      * @dataProvider    callbackProvider
    470      *
    471      * @param   string $file
    472      * @throws  \Exception
    473      */
    474     public function testMainTextCallback($file)
    475     {
    476         $client =& self::$client;
    477 
    478         if($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    479         {
    480             $this->markTestSkipped('Apache Tika ' . self::$version . ' lacks main content extraction');
    481         }
    482         else
    483         {
    484             BaseTest::$shared = 0;
    485 
    486             self::$client->getMainText($file, function ($chunk) {
    487                 BaseTest::$shared++;
    488             });
    489 
    490             $this->assertGreaterThan(1, BaseTest::$shared);
    491         }
    492     }
    493 
    494     /**
    495      * Main text callback test
    496      *
    497      * @dataProvider    callbackProvider
    498      *
    499      * @param   string $file
    500      * @throws  \Exception
    501      */
    502     public function testHtmlCallback($file)
    503     {
    504         $client =& self::$client;
    505 
    506         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    507         {
    508             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    509         }
    510         else
    511         {
    512             BaseTest::$shared = 0;
    513 
    514             self::$client->getHtml($file, function($chunk)
    515             {
    516                 BaseTest::$shared++;
    517             });
    518 
    519             $this->assertGreaterThan(1, BaseTest::$shared);
    520         }
    521     }
    522 
    523     /**
    524      * Remote file test with integrated download
    525      *
    526      * @dataProvider    remoteProvider
    527      *
    528      * @param   string $file
    529      * @throws  \Exception
    530      */
    531     public function testRemoteDocumentText($file)
    532     {
    533         $client =& self::$client;
    534 
    535         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    536         {
    537             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    538         }
    539         else
    540         {
    541             $this->assertContains('This is a small demonstration .pdf file', $client->getText($file));
    542         }
    543     }
    544 
    545     /**
    546      * Remote file test with internal downloader
    547      *
    548      * @dataProvider    remoteProvider
    549      *
    550      * @param   string $file
    551      * @throws  \Exception
    552      */
    553     public function testDirectRemoteDocumentText($file)
    554     {
    555         $client =& self::$client;
    556 
    557         if($client::MODE == 'web' && version_compare(self::$version, '1.9') == 0)
    558         {
    559             $this->markTestSkipped('Apache Tika 1.9 throws random "Error while processing document" errors');
    560         }
    561         elseif($client::MODE == 'web' && version_compare(self::$version, '1.15') < 0)
    562         {
    563             $this->markTestSkipped('Apache Tika between 1.10 and 1.14 doesn\'t supports unsecure features');
    564         }
    565         else
    566         {
    567             $client->setDownloadRemote(false);
    568 
    569             $this->assertContains('This is a small demonstration .pdf file', $client->getText($file));
    570         }
     369        $client->setEncoding('UTF-8');
     370
     371        $this->assertThat($client->getText($file), $this->logicalAnd
     372        (
     373            $this->stringContains('L’espéranto'),
     374            $this->stringContains('世界語'),
     375            $this->stringContains('Эспера́нто')
     376        ));
    571377    }
    572378
    573379    /**
    574380     * Test available detectors
    575      *
    576      * @throws  \Exception
    577      */
    578     public function testAvailableDetectors()
    579     {
    580         $this->assertContains('org.apache.tika.mime.MimeTypes', self::$client->getAvailableDetectors());
     381     */
     382    public function testAvailableDetectors(): void
     383    {
     384        $detectors = self::$client->getAvailableDetectors();
     385
     386        $this->assertArrayHasKey('org.apache.tika.detect.DefaultDetector', $detectors);
    581387    }
    582388
    583389    /**
    584390     * Test available parsers
    585      *
    586      * @throws  \Exception
    587      */
    588     public function testAvailableParsers()
    589     {
    590         $this->assertContains('org.apache.tika.parser.DefaultParser', self::$client->getAvailableParsers());
     391     */
     392    public function testAvailableParsers(): void
     393    {
     394        $parsers = self::$client->getAvailableParsers();
     395
     396        $this->assertArrayHasKey('org.apache.tika.parser.DefaultParser', $parsers);
    591397    }
    592398
    593399    /**
    594400     * Test supported MIME types
    595      *
    596      * @throws  \Exception
    597      */
    598     public function testSupportedMIMETypes()
    599     {
    600         $this->assertContains('application/x-pdf', self::$client->getSupportedMIMETypes());
     401     */
     402    public function testSupportedMIMETypes(): void
     403    {
     404        $this->assertArrayHasKey('application/pdf', self::$client->getSupportedMIMETypes());
     405    }
     406
     407
     408    /**
     409     * Test supported MIME types
     410     */
     411    public function testIsMIMETypeSupported(): void
     412    {
     413        $this->assertTrue(self::$client->isMIMETypeSupported('application/pdf'));
    601414    }
    602415
    603416    /**
    604417     * Static method to test callback
    605      *
    606      * @param   string  $chunk
    607      */
    608     public static function callableCallback($chunk)
     418     */
     419    public static function callableCallback(): void
    609420    {
    610421        BaseTest::$shared++;
     
    613424    /**
    614425     * Document file provider
    615      *
    616      * @return  array
    617      */
    618     public function documentProvider()
     426     */
     427    public function documentProvider(): array
    619428    {
    620429        return $this->samples('sample1');
     
    623432    /**
    624433     * Image file provider
    625      *
    626      * @return array
    627      */
    628     public function imageProvider()
     434     */
     435    public function imageProvider(): array
    629436    {
    630437        return $this->samples('sample2');
     
    633440    /**
    634441     * File provider for OCR testing
    635      *
    636      * @return array
    637      */
    638     public function ocrProvider()
     442     */
     443    public function ocrProvider(): array
    639444    {
    640445        return $this->samples('sample3');
     
    643448    /**
    644449     * File provider for callback testing
    645      *
    646      * @return array
    647      */
    648     public function callbackProvider()
     450     */
     451    public function callbackProvider(): array
    649452    {
    650453        return $this->samples('sample5');
     
    653456    /**
    654457     * File provider for remote testing
    655      *
    656      * @return array
    657      */
    658     public function remoteProvider()
     458     */
     459    public function remoteProvider(): array
    659460    {
    660461        return
    661462        [
    662463            [
    663                 'http://www.africau.edu/images/default/sample.pdf'
     464                'https://raw.githubusercontent.com/vaites/php-apache-tika/master/samples/sample6.pdf'
    664465            ]
    665466        ];
     
    667468
    668469    /**
     470     * File provider for encoding testing
     471     */
     472    public function encodingProvider(): array
     473    {
     474        return $this->samples('sample7');
     475    }
     476
     477    /**
     478     * File provider for recursive testing
     479     */
     480    public function recursiveProvider(): array
     481    {
     482        return $this->samples('sample8');
     483    }
     484
     485    /**
    669486     * File provider using "samples" folder
    670      *
    671      * @param   string $sample
    672      * @return  array
    673      */
    674     protected function samples($sample)
     487     */
     488    protected function samples(string $sample): array
    675489    {
    676490        $samples = [];
  • redisearch/trunk/vendor/vaites/php-apache-tika/tests/CLITest.php

    r2002534 r2525480  
    1313     * Create shared instances of clients
    1414     */
    15     public static function setUpBeforeClass()
     15    public static function setUpBeforeClass(): void
    1616    {
    1717        self::$client = Client::make(self::getPathForVersion(self::$version));
     
    1919
    2020    /**
     21     * XHTML test
     22     *
     23     * @dataProvider encodingProvider
     24     */
     25    public function testDocumentHTML(string $file): void
     26    {
     27        $this->assertStringStartsWith('<?xml version="1.0"', self::$client->getXHTML($file));
     28    }
     29
     30    /**
    2131     * Set path test
    2232     */
    23     public function testSetPath()
     33    public function testSetPath(): void
    2434    {
    25         $client = Client::make(self::getPathForVersion('1.0'));
     35        $path = self::getPathForVersion(self::$version);
    2636
    27         $this->assertEquals(self::getPathForVersion('1.0'), $client->getPath());
     37        $client = Client::make($path);
     38
     39        $this->assertEquals($path, $client->getPath());
    2840    }
    2941
     
    3143     * Set Java test
    3244     */
    33     public function testSetPort()
     45    public function testSetBinary(): void
    3446    {
    35         $client = Client::make(self::getPathForVersion('1.0'), '/opt/jdk/bin/java');
     47        $path = self::getPathForVersion(self::$version);
    3648
    37         $this->assertEquals('/opt/jdk/bin/java', $client->getJava());
     49        $client = Client::make($path, 'java');
     50
     51        $this->assertEquals('java', $client->getJava());
     52    }
     53
     54    /**
     55     * Set Java test
     56     */
     57    public function testSetArguments(): void
     58    {
     59        $path = self::getPathForVersion(self::$version);
     60
     61        $client = Client::make($path);
     62        $client->setJavaArgs('-JXmx4g');
     63
     64        $this->assertEquals('-JXmx4g', $client->getJavaArgs());
     65    }
     66
     67    /**
     68     * Test delayed check
     69     */
     70    public function testDelayedCheck(): void
     71    {
     72        $path = self::getPathForVersion(self::$version);
     73
     74        $client = Client::prepare('/nonexistent/path/to/apache-tika.jar');
     75        $client->setPath($path);
     76
     77        $this->assertStringEndsWith(self::$version, $client->getVersion());
    3878    }
    3979
    4080    /**
    4181     * Get the full path of Tika app for a specified version
    42      *
    43      * @param   string  $version
    44      * @return  string
    4582     */
    46     private static function getPathForVersion($version)
     83    private static function getPathForVersion(string $version): string
    4784    {
    4885        return self::$binaries . "/tika-app-{$version}.jar";
  • redisearch/trunk/vendor/vaites/php-apache-tika/tests/CommonTest.php

    r2002534 r2525480  
    11<?php namespace Vaites\ApacheTika\Tests;
    22
    3 use PHPUnit_Framework_TestCase;
     3use PHPUnit\Framework\TestCase;
    44
    55use Vaites\ApacheTika\Client;
     
    88 * Common test functionality
    99 */
    10 class CommonTest extends PHPUnit_Framework_TestCase
     10class CommonTest extends TestCase
    1111{
    1212    /**
     
    3333    /**
    3434     * Get env variables
    35      *
    36      * @param null      $name
    37      * @param array     $data
    38      * @param string    $dataName
    39      * @throws \Exception
    4035     */
    41     public function __construct($name = null, array $data = array(), $dataName = '')
     36    public function __construct(string $name = null, array $data = array(), $dataName = '')
    4237    {
    4338        self::$version = getenv('APACHE_TIKA_VERSION');
     
    5146     * Set chunk size test
    5247     */
    53     public function testSetChunkSize()
     48    public function testSetChunkSize(): void
    5449    {
    5550        self::$client->setChunkSize(42);
     
    5954
    6055    /**
     56     * Set download remote
     57     */
     58    public function testDownloadRemote(): void
     59    {
     60        self::$client->setDownloadRemote(true);
     61
     62        $this->assertTrue(self::$client->getDownloadRemote());
     63    }
     64
     65    /**
    6166     * Set callback (closure) test
    6267     */
    63     public function testSetClosureCallback()
     68    public function testSetClosureCallback(): void
    6469    {
    6570        self::$client->setCallback(function($chunk)
     
    7479     * Set callback (callable) test
    7580     */
    76     public function testSetCallableCallback()
     81    public function testSetCallableCallback(): void
    7782    {
    7883        self::$client->setCallback('trim');
     
    8489     * Get supported versions test
    8590     */
    86     public function testGetSupportedVersions()
     91    public function testGetSupportedVersions(): void
    8792    {
    88         $this->assertTrue(in_array('1.10', Client::getSupportedVersions()));
     93        $this->assertTrue(in_array(self::$version, self::$client->getSupportedVersions()));
    8994    }
    9095
     
    9297     * Is version supported vtest
    9398     */
    94     public function testIsVersionSupported()
     99    public function testIsVersionSupported(): void
    95100    {
    96         $this->assertTrue(Client::isVersionSupported('1.10'));
     101        $this->assertTrue(self::$client->isVersionSupported(self::$version));
    97102    }
    98103}
  • redisearch/trunk/vendor/vaites/php-apache-tika/tests/ErrorTest.php

    r2002534 r2525480  
    22
    33use Exception;
    4 use PHPUnit_Framework_TestCase;
     4
     5use PHPUnit\Framework\TestCase;
    56
    67use Vaites\ApacheTika\Client;
     
    1011 * Error tests
    1112 */
    12 class ErrorTest extends PHPUnit_Framework_TestCase
     13class ErrorTest extends TestCase
    1314{
    1415    /**
     
    2829    /**
    2930     * Get env variables
    30      *
    31      * @param null      $name
    32      * @param array     $data
    33      * @param string    $dataName
    34      */
    35     public function __construct($name = null, array $data = array(), $dataName = '')
     31     */
     32    public function __construct(string $name = null, array $data = array(), $dataName = '')
    3633    {
    3734        self::$version = getenv('APACHE_TIKA_VERSION');
     
    4441     * Test wrong command line mode path
    4542     */
    46     public function testAppPath()
    47     {
    48         try
    49         {
    50             $client = Client::make('/nonexistent/path/to/apache-tika.jar');
     43    public function testAppPath(): void
     44    {
     45        try
     46        {
     47            $client = Client::prepare('/nonexistent/path/to/apache-tika.jar');
    5148            $client->getVersion();
    5249        }
    5350        catch(Exception $exception)
    5451        {
    55             $this->assertContains('Unexpected exit value', $exception->getMessage());
     52            $this->assertStringContainsString('Apache Tika app JAR not found', $exception->getMessage());
    5653        }
    5754    }
     
    6057     * Test unexpected exit value for command line mode
    6158     */
    62     public function testAppExitValue()
     59    public function testAppExitValue(): void
    6360    {
    6461        $path = self::getPathForVersion(self::$version);
     
    7673            rename($path . '.bak', $path);
    7774
    78             $this->assertContains('Unexpected exit value', $exception->getMessage());
     75            $this->assertStringContainsString('Unexpected exit value', $exception->getMessage());
    7976        }
    8077    }
     
    8380     * Test invalid Java binary path for command line mode
    8481     */
    85     public function testAppJavaBinary()
     82    public function testJavaBinary(): void
    8683    {
    8784        $path = self::getPathForVersion(self::$version);
     
    9491        catch(Exception $exception)
    9592        {
    96             $this->assertContains('Unexpected exit value', $exception->getMessage());
     93            $this->assertStringContainsString('Java command not found', $exception->getMessage());
    9794        }
    9895    }
     
    10198     * Test wrong server
    10299     */
    103     public function testServerConnection()
    104     {
    105         try
    106         {
    107             Client::make('localhost', 9997);
    108 
    109             $this->fail();
    110         }
    111         catch(Exception $exception)
    112         {
    113             $this->assertEquals(7, $exception->getCode());
     100    public function testServerConnection(): void
     101    {
     102        try
     103        {
     104            $client = Client::prepare('localhost', 9997);
     105            $client->getVersion();
     106
     107            $this->fail();
     108        }
     109        catch(Exception $exception)
     110        {
     111            $this->assertThat($exception->getCode(), $this->logicalOr
     112            (
     113                $this->equalTo(CURLE_COULDNT_CONNECT),
     114                $this->equalTo(CURLE_OPERATION_TIMEDOUT)
     115            ));
    114116        }
    115117    }
     
    118120     * Test wrong request options
    119121     */
    120     public function testRequestOptions()
    121     {
    122         try
    123         {
    124             $client = Client::make('localhost', 9998, [CURLOPT_PROXY => 'localhost']);
     122    public function testRequestOptions(): void
     123    {
     124        try
     125        {
     126            $client = Client::make('localhost', 9998);
    125127            $client->request('bad');
    126128
     
    129131        catch(Exception $exception)
    130132        {
    131             $this->assertEquals(7, $exception->getCode());
     133            $this->assertStringContainsString('Unknown type bad', $exception->getMessage());
    132134        }
    133135    }
     
    136138     * Test invalidrequest options
    137139     */
    138     public function testRequestRestrictedOptions()
    139     {
    140         try
    141         {
    142             $client = Client::make('localhost', 9998, [CURLOPT_PUT => false]);
     140    public function testRequestRestrictedOptions(): void
     141    {
     142        try
     143        {
     144            Client::make('localhost', 9998, [CURLOPT_PUT => false]);
    143145        }
    144146        catch(Exception $exception)
     
    149151
    150152    /**
     153     * Test wrong recursive metadata type
     154     */
     155    public function testRequestMetadataType(): void
     156    {
     157        try
     158        {
     159            $client = Client::make('localhost', 9998);
     160            $client->getRecursiveMetadata(dirname(__DIR__) . '/samples/sample3.png', 'bad');
     161
     162            $this->fail();
     163        }
     164        catch(Exception $exception)
     165        {
     166            $this->assertStringContainsString('Unknown recursive type', $exception->getMessage());
     167        }
     168    }
     169
     170    /**
    151171     * Test unsupported media type
    152      */
    153     public function testUnsupportedMedia()
     172     *
     173     * NOTE: return value was changed in version 1.23
     174     *
     175     * @link    https://github.com/apache/tika/blob/master/CHANGES.txt
     176     */
     177    public function testUnsupportedMedia(): void
    154178    {
    155179        try
     
    162186        catch(Exception $exception)
    163187        {
    164             $this->assertEquals(415, $exception->getCode());
    165         }
    166     }
    167 
    168     /**
    169      * Test invalid callback
    170      */
    171     public function testInvalidCallback()
    172     {
    173         try
    174         {
    175             $client = Client::make(self::$binaries . '/tika-app-' . self::$version . '.jar');
    176             $client->setCallback('unknown_function');
    177         }
    178         catch(Exception $exception)
    179         {
    180             $this->assertContains('Invalid callback', $exception->getMessage());
     188            if(version_compare(self::$version, '1.23') < 0)
     189            {
     190                $this->assertEquals(415, $exception->getCode());
     191            }
     192            else
     193            {
     194                $this->assertEquals(0, $exception->getCode());
     195            }
     196        }
     197    }
     198
     199    /**
     200     * Test unknown recursive metadata type
     201     */
     202    public function testUnknownRecursiveMetadataType(): void
     203    {
     204        try
     205        {
     206            $client = Client::make('localhost', 9998);
     207            $client->getRecursiveMetadata('example.doc', 'error');
     208
     209            $this->fail();
     210        }
     211        catch(Exception $exception)
     212        {
     213            $this->assertStringContainsString('Unknown recursive type', $exception->getMessage());
    181214        }
    182215    }
     
    185218     * Test invalid chunk size
    186219     */
    187     public function testInvalidChunkSize()
    188     {
    189         try
    190         {
    191             $client = Client::make(self::$binaries . '/tika-app-' . self::$version . '.jar');
    192             $client->setChunkSize('string');
    193         }
    194         catch(Exception $exception)
    195         {
    196             $this->assertContains('is not a valid chunk size', $exception->getMessage());
    197         }
    198     }
    199 
    200     /**
    201      * Test invalid chunk size
    202      */
    203     public function testUnsupportedChunkSize()
     220    public function testUnsupportedChunkSize(): void
    204221    {
    205222        try
     
    210227        catch(Exception $exception)
    211228        {
    212             $this->assertContains('Chunk size is not supported', $exception->getMessage());
    213         }
    214     }
    215 
    216     /**
    217      * Test invalid metadata
    218      */
    219     public function testInvalidMetadata()
    220     {
    221         try
    222         {
    223             $metadata = Metadata::make('InvalidJsonString', './samples/sample1.doc');
    224         }
    225         catch(Exception $exception)
    226         {
    227             $this->assertEquals(JSON_ERROR_SYNTAX, $exception->getCode());
    228         }
    229     }
    230 
    231     /**
    232      * Test empty metadata
    233      */
    234     public function testEmptyMetadata()
    235     {
    236         try
    237         {
    238             $metadata = Metadata::make('', './samples/sample1.doc');
    239         }
    240         catch(Exception $exception)
    241         {
    242             $this->assertContains('Empty response', $exception->getMessage());
     229            $this->assertStringContainsString('Chunk size is not supported', $exception->getMessage());
    243230        }
    244231    }
     
    248235     *
    249236     * @dataProvider    parameterProvider
    250      *
    251      * @param   array   $parameters
    252      */
    253     public function testRequestType($parameters)
     237     */
     238    public function testRequestType(array $parameters): void
    254239    {
    255240        try
     
    262247        catch(Exception $exception)
    263248        {
    264             $this->assertContains('Unknown type bad', $exception->getMessage());
     249            $this->assertStringContainsString('Unknown type bad', $exception->getMessage());
    265250        }
    266251    }
     
    270255     *
    271256     * @dataProvider    parameterProvider
    272      *
    273      * @param   array   $parameters
    274      */
    275     public function testLocalFile($parameters)
     257     */
     258    public function testLocalFile(array $parameters): void
    276259    {
    277260        try
     
    292275     *
    293276     * @dataProvider    parameterProvider
    294      *
    295      * @param   array   $parameters
    296      */
    297     public function testRemoteFile($parameters)
     277     */
     278    public function testRemoteFile(array $parameters): void
    298279    {
    299280        try
     
    312293    /**
    313294     * Client parameters provider
    314      *
    315      * @return array
    316      */
    317     public function parameterProvider()
     295     */
     296    public function parameterProvider(): array
    318297    {
    319298        return
     
    326305    /**
    327306     * Get the full path of Tika app for a specified version
    328      *
    329      * @param   string  $version
    330      * @return  string
    331      */
    332     private static function getPathForVersion($version)
     307     */
     308    private static function getPathForVersion(string $version): string
    333309    {
    334310        return self::$binaries . "/tika-app-{$version}.jar";
  • redisearch/trunk/vendor/vaites/php-apache-tika/tests/WebTest.php

    r2002534 r2525480  
    1515     * Start Tika server and create shared instance of clients
    1616     */
    17     public static function setUpBeforeClass()
     17    public static function setUpBeforeClass(): void
    1818    {
    1919        self::$client = Client::make('localhost', 9998, [CURLOPT_TIMEOUT => 30]);
     
    2323     * cURL multiple options test
    2424     */
    25     public function testCurlOptions()
     25    public function testCurlOptions(): void
    2626    {
    2727        $client = Client::make('localhost', 9998, [CURLOPT_TIMEOUT => 3]);
     
    3434     * cURL single option test
    3535     */
    36     public function testCurlSingleOption()
     36    public function testCurlSingleOption(): void
    3737    {
    3838        $client = Client::make('localhost', 9998)->setOption(CURLOPT_TIMEOUT, 3);
     
    4444     * cURL timeout option test
    4545     */
    46     public function testCurlTimeoutOption()
     46    public function testCurlTimeoutOption(): void
    4747    {
    4848        $client = Client::make('localhost', 9998)->setTimeout(3);
     
    5454     * cURL headers test
    5555     */
    56     public function testCurlHeaders()
     56    public function testCurlHeaders(): void
    5757    {
    5858        $header = 'Content-Type: image/jpeg';
     
    6767     * Set host test
    6868     */
    69     public function testSetHost()
     69    public function testSetHost(): void
    7070    {
    7171        $client = Client::make('localhost', 9998);
     
    7878     * Set port test
    7979     */
    80     public function testSetPort()
     80    public function testSetPort(): void
    8181    {
    8282        $client = Client::make('localhost', 9998);
     
    8787
    8888    /**
     89     * Set url host test
     90     */
     91    public function testSetUrlHost(): void
     92    {
     93        $client = Client::make('http://localhost:9998');
     94
     95        $this->assertEquals('localhost', $client->getHost());
     96    }
     97
     98    /**
     99     * Set url port test
     100     */
     101    public function testSetUrlPort(): void
     102    {
     103        $client = Client::make('http://localhost:9998');
     104
     105        $this->assertEquals(9998, $client->getPort());
     106    }
     107
     108    /**
    89109     * Set retries test
    90110     */
    91     public function testRetriesPort()
     111    public function testSetRetries(): void
    92112    {
    93113        $client = Client::make('localhost', 9998);
     
    96116        $this->assertEquals(5, $client->getRetries());
    97117    }
     118
     119    /**
     120     * Test delayed check
     121     */
     122    public function testDelayedCheck(): void
     123    {
     124        $client = Client::prepare('localhost', 9997);
     125        $client->setPort(9998);
     126
     127        $this->assertStringEndsWith(self::$version, $client->getVersion());
     128    }
    98129}
  • redisearch/trunk/wp-redisearch.php

    r2455651 r2525480  
    22/*
    33Plugin Name: RediSearch
    4 Version: 0.3.2
     4Version: 0.3.3
    55Description: Replace Wordpress search by RediSearch.
    66Author: Foad Yousefi
     
    2929// Plugin version .
    3030if ( ! defined( 'WPRS_VERSION' ) ) {
    31     define( 'WPRS_VERSION', '0.3.2' );
     31    define( 'WPRS_VERSION', '0.3.3' );
    3232}
    3333
Note: See TracChangeset for help on using the changeset viewer.