[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/macro/markup/ -> PhabricatorImageMacroRemarkupRule.php (source)

   1  <?php
   2  
   3  final class PhabricatorImageMacroRemarkupRule extends PhutilRemarkupRule {
   4  
   5    private $macros;
   6  
   7    const KEY_RULE_MACRO = 'rule.macro';
   8  
   9    public function apply($text) {
  10      return preg_replace_callback(
  11        '@^\s*([a-zA-Z0-9:_\-]+)$@m',
  12        array($this, 'markupImageMacro'),
  13        $text);
  14    }
  15  
  16    public function markupImageMacro($matches) {
  17      if ($this->macros === null) {
  18        $this->macros = array();
  19  
  20        $viewer = $this->getEngine()->getConfig('viewer');
  21        $rows = id(new PhabricatorMacroQuery())
  22          ->setViewer($viewer)
  23          ->withStatus(PhabricatorMacroQuery::STATUS_ACTIVE)
  24          ->execute();
  25  
  26        $this->macros = mpull($rows, 'getPHID', 'getName');
  27      }
  28  
  29      $name = (string)$matches[1];
  30      if (empty($this->macros[$name])) {
  31        return $matches[1];
  32      }
  33  
  34      $engine = $this->getEngine();
  35  
  36  
  37      $metadata_key = self::KEY_RULE_MACRO;
  38      $metadata = $engine->getTextMetadata($metadata_key, array());
  39  
  40      $token = $engine->storeText('<macro>');
  41      $metadata[] = array(
  42        'token' => $token,
  43        'phid' => $this->macros[$name],
  44        'original' => $name,
  45      );
  46  
  47      $engine->setTextMetadata($metadata_key, $metadata);
  48  
  49      return $token;
  50    }
  51  
  52    public function didMarkupText() {
  53      $engine = $this->getEngine();
  54      $metadata_key = self::KEY_RULE_MACRO;
  55      $metadata = $engine->getTextMetadata($metadata_key, array());
  56  
  57      if (!$metadata) {
  58        return;
  59      }
  60  
  61      $phids = ipull($metadata, 'phid');
  62      $viewer = $this->getEngine()->getConfig('viewer');
  63  
  64      // Load all the macros.
  65      $macros = id(new PhabricatorMacroQuery())
  66        ->setViewer($viewer)
  67        ->withStatus(PhabricatorMacroQuery::STATUS_ACTIVE)
  68        ->withPHIDs($phids)
  69        ->execute();
  70      $macros = mpull($macros, null, 'getPHID');
  71  
  72      // Load all the images and audio.
  73      $file_phids = array_merge(
  74        array_values(mpull($macros, 'getFilePHID')),
  75        array_values(mpull($macros, 'getAudioPHID')));
  76  
  77      $file_phids = array_filter($file_phids);
  78  
  79      $files = array();
  80      if ($file_phids) {
  81        $files = id(new PhabricatorFileQuery())
  82          ->setViewer($viewer)
  83          ->withPHIDs($file_phids)
  84          ->execute();
  85        $files = mpull($files, null, 'getPHID');
  86      }
  87  
  88      // Replace any macros that we couldn't load the macro or image for with
  89      // the original text.
  90      foreach ($metadata as $key => $spec) {
  91        $macro = idx($macros, $spec['phid']);
  92        if ($macro) {
  93          $file = idx($files, $macro->getFilePHID());
  94          if ($file) {
  95            continue;
  96          }
  97        }
  98  
  99        $engine->overwriteStoredText($spec['token'], $spec['original']);
 100        unset($metadata[$key]);
 101      }
 102  
 103      foreach ($metadata as $spec) {
 104        $macro = $macros[$spec['phid']];
 105        $file = $files[$macro->getFilePHID()];
 106        $src_uri = $file->getBestURI();
 107  
 108        if ($this->getEngine()->isTextMode()) {
 109          $result = $spec['original'].' <'.$src_uri.'>';
 110          $engine->overwriteStoredText($spec['token'], $result);
 111          continue;
 112        } else if ($this->getEngine()->isHTMLMailMode()) {
 113          $src_uri = PhabricatorEnv::getProductionURI($src_uri);
 114        }
 115  
 116        $file_data = $file->getMetadata();
 117        $style = null;
 118        $height = idx($file_data, PhabricatorFile::METADATA_IMAGE_HEIGHT);
 119        $width = idx($file_data, PhabricatorFile::METADATA_IMAGE_WIDTH);
 120        if ($height && $width) {
 121          $style = sprintf(
 122            'height: %dpx; width: %dpx;',
 123            $height,
 124            $width);
 125        }
 126  
 127        $id = null;
 128        $audio = idx($files, $macro->getAudioPHID());
 129        $should_play = ($audio && $macro->getAudioBehavior() !=
 130          PhabricatorFileImageMacro::AUDIO_BEHAVIOR_NONE);
 131        if ($should_play) {
 132          $id = celerity_generate_unique_node_id();
 133  
 134          $loop = null;
 135          switch ($macro->getAudioBehavior()) {
 136            case PhabricatorFileImageMacro::AUDIO_BEHAVIOR_LOOP:
 137              $loop = true;
 138              break;
 139          }
 140  
 141          Javelin::initBehavior(
 142            'audio-source',
 143            array(
 144              'sourceID' => $id,
 145              'audioURI' => $audio->getBestURI(),
 146              'loop' => $loop,
 147            ));
 148        }
 149  
 150        $result = $this->newTag(
 151          'img',
 152          array(
 153            'id'    => $id,
 154            'src'   => $src_uri,
 155            'alt'   => $spec['original'],
 156            'title' => $spec['original'],
 157            'style' => $style,
 158          ));
 159  
 160        $engine->overwriteStoredText($spec['token'], $result);
 161      }
 162  
 163      $engine->setTextMetadata($metadata_key, array());
 164    }
 165  
 166  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1