Beispiel #1
0
 public function testEscapedStrings()
 {
     $params = new CommandParams('"foo bar" "baz fu" \'bar baz\'');
     $this->assertEquals(['foo bar', 'baz fu', "'bar", "baz'"], $params->getArguments());
     $this->assertSame('foo bar', $params->getFirstArgument());
     $this->assertSame('baz fu', $params->getSecondArgument());
     $this->assertSame("'bar", $params->getThirdArgument());
     $this->assertSame('foo bar', $params->getArgument(0));
     $this->assertSame('baz fu', $params->getArgument(1));
     $this->assertSame("'bar", $params->getArgument(2));
     $this->assertSame("baz'", $params->getArgument(3));
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function execute(CommandParams $params, CommandExecutionContext $executionContext)
 {
     $article = $params->getFirstArgument();
     $numSentences = (int) $params->getSecondArgument(0);
     $cacheFile = $this->getCacheFilePath($article);
     if (is_readable($cacheFile)) {
         $summary = file_get_contents($cacheFile);
         return $this->quote($summary, $numSentences);
     }
     $url = $this->buildURL($article);
     $response = file_get_contents($url);
     $data = json_decode($response);
     if (!$data) {
         throw new ExecutionFailedException('Wikipedia API response could not be decoded. Request URL: ' . $url . '. Response: ' . var_export($response, true));
     }
     foreach ($data->query->pages as $page) {
         if (isset($page->missing)) {
             continue;
         }
         file_put_contents($cacheFile, $page->extract);
         return $this->quote($page->extract, $numSentences);
     }
     throw new ExecutionFailedException("Failed to query for Wikipedia article '{$article}'. Request URL: {$url}");
 }
Beispiel #3
0
 /**
  * @param Image         $img
  * @param CommandParams $params
  *
  * @return string
  *
  * @throws ExecutionFailedException
  */
 private function returnImage(Image $img, CommandParams $params)
 {
     switch ($this->returnType) {
         case self::RETURN_TYPE_URL:
         case self::RETURN_TYPE_PATH:
             $tmpDir = $this->executionContext->ensureTempDirectory();
             $tmpFile = $tmpDir . '/' . md5($params->getParams()) . '.png';
             $img->encode('png')->save($tmpFile);
             if ($this->returnType === self::RETURN_TYPE_PATH) {
                 return $tmpFile;
             }
             return '<img src="/_tmp/' . basename($tmpFile) . '">';
         case self::RETURN_TYPE_DATA_URL:
             return '![Image](' . $img->encode('data-url')->getEncoded() . ')';
         case self::RETURN_TYPE_DATA:
             return $img->encode('png')->getEncoded();
         default:
             throw new ExecutionFailedException("Invalid return type '{$this->returnType}' detected.");
     }
 }
Beispiel #4
0
 /**
  * @param string        $contents
  * @param CommandParams $params
  *
  * @return string
  *
  * @throws ExecutionFailedException
  */
 private function findLines($contents, CommandParams $params)
 {
     $lines = explode("\n", $contents);
     $range = $params->getThirdArgument();
     if (is_numeric($range)) {
         return $this->wrapLines(array_slice($lines, $range - 1, 1));
     }
     $matches = [];
     if (!preg_match('|(\\d+)-(\\d+)|', $range, $matches)) {
         throw new ExecutionFailedException("Line definition '{$range}' is not valid. Must be in format N or N-N.");
     }
     $from = min([$matches[1], $matches[2]]);
     $to = max([$matches[1], $matches[2]]);
     return $this->wrapLines(array_slice($lines, $from - 1, $to - $from + 1));
 }