1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 08:12:40 +01:00

Image macros for Phabricator!

Summary:
Added long waited image macro support for differential and others.

Test Plan:
Tried a couple of different macros and made sure they appear nicely
in the comment preview. Made sure that the normal comments are shown
correctly.

Reviewed By: epriestley
Reviewers: epriestley
CC: jungejason, tuomaspelkonen, epriestley
Differential Revision: 129
This commit is contained in:
tuomaspelkonen 2011-04-13 15:15:48 -07:00
parent d4fb7f578e
commit f7fe75f756
10 changed files with 191 additions and 0 deletions

View file

@ -0,0 +1,5 @@
CREATE TABLE phabricator_file.`file_imagemacro` (
`id` int unsigned NOT NULL auto_increment PRIMARY KEY,
`filePHID` varchar(64) NOT NULL,
`name` varchar(255) NOT NULL
);

View file

@ -287,6 +287,7 @@ phutil_register_library_map(array(
'PhabricatorFile' => 'applications/files/storage/file',
'PhabricatorFileController' => 'applications/files/controller/base',
'PhabricatorFileDAO' => 'applications/files/storage/base',
'PhabricatorFileImageMacro' => 'applications/files/storage/imagemacro',
'PhabricatorFileListController' => 'applications/files/controller/list',
'PhabricatorFileStorageBlob' => 'applications/files/storage/storageblob',
'PhabricatorFileURI' => 'applications/files/uri',
@ -356,6 +357,7 @@ phutil_register_library_map(array(
'PhabricatorRedirectController' => 'applications/base/controller/redirect',
'PhabricatorRemarkupRuleDifferential' => 'infrastructure/markup/remarkup/markuprule/differential',
'PhabricatorRemarkupRuleDiffusion' => 'infrastructure/markup/remarkup/markuprule/diffusion',
'PhabricatorRemarkupRuleImageMacro' => 'infrastructure/markup/remarkup/markuprule/imagemacro',
'PhabricatorRemarkupRuleManiphest' => 'infrastructure/markup/remarkup/markuprule/maniphest',
'PhabricatorRepository' => 'applications/repository/storage/repository',
'PhabricatorRepositoryArcanistProject' => 'applications/repository/storage/arcanistproject',
@ -672,6 +674,7 @@ phutil_register_library_map(array(
'PhabricatorFile' => 'PhabricatorFileDAO',
'PhabricatorFileController' => 'PhabricatorController',
'PhabricatorFileDAO' => 'PhabricatorLiskDAO',
'PhabricatorFileImageMacro' => 'PhabricatorFileDAO',
'PhabricatorFileListController' => 'PhabricatorFileController',
'PhabricatorFileStorageBlob' => 'PhabricatorFileDAO',
'PhabricatorFileUploadController' => 'PhabricatorFileController',
@ -733,6 +736,7 @@ phutil_register_library_map(array(
'PhabricatorRedirectController' => 'PhabricatorController',
'PhabricatorRemarkupRuleDifferential' => 'PhutilRemarkupRule',
'PhabricatorRemarkupRuleDiffusion' => 'PhutilRemarkupRule',
'PhabricatorRemarkupRuleImageMacro' => 'PhutilRemarkupRule',
'PhabricatorRemarkupRuleManiphest' => 'PhutilRemarkupRule',
'PhabricatorRepository' => 'PhabricatorRepositoryDAO',
'PhabricatorRepositoryArcanistProject' => 'PhabricatorRepositoryDAO',

View file

@ -30,6 +30,7 @@ class DifferentialMarkupEngineFactory {
$rules[] = new PhabricatorRemarkupRuleDifferential();
$rules[] = new PhabricatorRemarkupRuleDiffusion();
$rules[] = new PhabricatorRemarkupRuleManiphest();
$rules[] = new PhabricatorRemarkupRuleImageMacro();
$rules[] = new PhutilRemarkupRuleEscapeHTML();
$rules[] = new PhutilRemarkupRuleMonospace();

View file

@ -8,6 +8,7 @@
phutil_require_module('phabricator', 'infrastructure/markup/remarkup/markuprule/differential');
phutil_require_module('phabricator', 'infrastructure/markup/remarkup/markuprule/diffusion');
phutil_require_module('phabricator', 'infrastructure/markup/remarkup/markuprule/imagemacro');
phutil_require_module('phabricator', 'infrastructure/markup/remarkup/markuprule/maniphest');
phutil_require_module('phutil', 'markup/engine/remarkup');

View file

@ -121,6 +121,23 @@ class PhabricatorFile extends PhabricatorFileDAO {
return $file;
}
public static function newFromFileDownload($uri, $name) {
$uri = new PhutilURI($uri);
$timeout = stream_context_create(
array(
'http' => array(
'timeout' => 5,
),
));
$file_data = @file_get_contents($uri, false, $timeout);
if ($file_data === false) {
return null;
}
return self::newFromFileData($file_data, array('name' => $name));
}
public static function normalizeFileName($file_name) {
return preg_replace('/[^a-zA-Z0-9.~_-]/', '_', $file_name);
}

View file

@ -16,6 +16,7 @@ phutil_require_module('phabricator', 'infrastructure/env');
phutil_require_module('phutil', 'filesystem');
phutil_require_module('phutil', 'filesystem/tempfile');
phutil_require_module('phutil', 'future/exec');
phutil_require_module('phutil', 'parser/uri');
phutil_require_module('phutil', 'utils');

View file

@ -0,0 +1,45 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PhabricatorFileImageMacro extends PhabricatorFileDAO {
protected $filePHID;
protected $name;
public function getConfiguration() {
return array(
self::CONFIG_TIMESTAMPS => false,
) + parent::getConfiguration();
}
static public function newFromImageURI($uri, $file_name, $image_macro_name) {
$file = PhabricatorFile::newFromFileDownload($uri, $file_name);
if (!$file) {
return null;
}
$image_macro = new PhabricatorFileImageMacro();
$image_macro->setName($image_macro_name);
$image_macro->setFilePHID($file->getPHID());
$image_macro->save();
return $image_macro;
}
}

View file

@ -0,0 +1,13 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'applications/files/storage/base');
phutil_require_module('phabricator', 'applications/files/storage/file');
phutil_require_source('PhabricatorFileImageMacro.php');

View file

@ -0,0 +1,87 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @group markup
*/
class PhabricatorRemarkupRuleImageMacro
extends PhutilRemarkupRule {
const RANDOM_IMAGE_NAME = 'randomon';
private $images = array();
private $hash = 0;
public function __construct() {
$rows = id(new PhabricatorFileImageMacro())->loadAll();
foreach ($rows as $row) {
$this->images[$row->getName()] = $row->getFilePHID();
}
$this->images[self::RANDOM_IMAGE_NAME] = '';
}
public function apply($text) {
$this->hash = 0;
return preg_replace_callback(
'@\b([a-zA-Z0-9_\-]+)\b@U',
array($this, 'markupImageMacro'),
$text);
}
/**
* Silly function for generating some 'randomness' based on the
* words in the text
*/
private function updateHash($word) {
// Simple Jenkins hash
for ($ii = 0; $ii < strlen($word); $ii++) {
$this->hash += ord($word[$ii]);
$this->hash += ($this->hash << 10);
$this->hash ^= ($this->hash >> 6);
}
}
public function markupImageMacro($matches) {
// Update the hash that is used for defining each 'randomon' image. This way
// each 'randomon' image will be different, but they won't change when the
// text is updated.
$this->updateHash($matches[1]);
if (array_key_exists($matches[1], $this->images)) {
if ($matches[1] === self::RANDOM_IMAGE_NAME) {
$keys = array_keys($this->images);
$phid = $this->images[$keys[$this->hash % count($this->images)]];
} else {
$phid = $this->images[$matches[1]];
}
$img = phutil_render_tag(
'img',
array(
'src' => PhabricatorFileURI::getViewURIForPHID($phid),
'alt' => $matches[1],
'title' => $matches[1]),
null);
return $this->getEngine()->storeText($img);
} else {
return $matches[1];
}
}
}

View file

@ -0,0 +1,17 @@
<?php
/**
* This file is automatically generated. Lint this module to rebuild it.
* @generated
*/
phutil_require_module('phabricator', 'applications/files/storage/imagemacro');
phutil_require_module('phabricator', 'applications/files/uri');
phutil_require_module('phutil', 'markup');
phutil_require_module('phutil', 'markup/engine/remarkup/markuprule/base');
phutil_require_module('phutil', 'utils');
phutil_require_source('PhabricatorRemarkupRuleImageMacro.php');