From 61921324d0dedd7e51847dd10e56897b51d9749e Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 16 May 2012 13:57:01 +0200 Subject: [PATCH] initial commit: mime detection for programming files --- data/programming.magic | 10 ++++ tests/DetectionTest.php | 56 +++++++++++++++++++ tests/files/css/file | 110 +++++++++++++++++++++++++++++++++++++ tests/files/css/mime-type | 1 + tests/files/diff/file | 24 ++++++++ tests/files/diff/mime-type | 1 + tests/files/htm/file | 8 +++ tests/files/htm/mime-type | 1 + tests/files/ini/file | 5 ++ tests/files/ini/mime-type | 1 + tests/files/js/file | 26 +++++++++ tests/files/js/mime-type | 1 + tests/files/php/file | 14 +++++ tests/files/php/mime-type | 1 + tests/files/rst/file | 15 +++++ tests/files/rst/mime-type | 1 + tests/files/sh/file | 44 +++++++++++++++ tests/files/sh/mime-type | 1 + tests/files/sql/file | 13 +++++ tests/files/sql/mime-type | 1 + tests/files/svg/file | 21 +++++++ tests/files/svg/mime-type | 1 + tests/files/ts/file | 34 ++++++++++++ tests/files/ts/mime-type | 1 + tests/files/txt/file | 3 + tests/files/txt/mime-type | 1 + tests/files/xml/file | 4 ++ tests/files/xml/mime-type | 1 + 28 files changed, 400 insertions(+) create mode 100644 data/programming.magic create mode 100644 tests/DetectionTest.php create mode 100644 tests/files/css/file create mode 100644 tests/files/css/mime-type create mode 100644 tests/files/diff/file create mode 100644 tests/files/diff/mime-type create mode 100644 tests/files/htm/file create mode 100644 tests/files/htm/mime-type create mode 100644 tests/files/ini/file create mode 100644 tests/files/ini/mime-type create mode 100644 tests/files/js/file create mode 100644 tests/files/js/mime-type create mode 100644 tests/files/php/file create mode 100644 tests/files/php/mime-type create mode 100644 tests/files/rst/file create mode 100644 tests/files/rst/mime-type create mode 100755 tests/files/sh/file create mode 100644 tests/files/sh/mime-type create mode 100644 tests/files/sql/file create mode 100644 tests/files/sql/mime-type create mode 100644 tests/files/svg/file create mode 100644 tests/files/svg/mime-type create mode 100644 tests/files/ts/file create mode 100644 tests/files/ts/mime-type create mode 100644 tests/files/txt/file create mode 100644 tests/files/txt/mime-type create mode 100644 tests/files/xml/file create mode 100644 tests/files/xml/mime-type diff --git a/data/programming.magic b/data/programming.magic new file mode 100644 index 0000000..ab492c5 --- /dev/null +++ b/data/programming.magic @@ -0,0 +1,10 @@ +0 search/2048/c =font-size: Cascading Style Sheet text +!:mime text/css +0 search/2048/c =color: Cascading Style Sheet text +!:mime text/css +0 search/2048/c =width: Cascading Style Sheet text +!:mime text/css +0 string =--- Difference file text +!:mime text/diff +0 string =diff Difference file text +!:mime text/diff diff --git a/tests/DetectionTest.php b/tests/DetectionTest.php new file mode 100644 index 0000000..1109cf5 --- /dev/null +++ b/tests/DetectionTest.php @@ -0,0 +1,56 @@ +assertNotEmpty($file, 'File is empty'); + $this->assertNotEmpty($expectedType, 'Expected type is empty'); + + $type = $this->detectType($file); + $this->assertEquals( + $expectedType, $type, + 'MIME type not detected correctly' + ); + } + + public function detectType($file) + { + $mt = new MIME_Type(); + $mt->magicFile = __DIR__ . '/../data/programming.magic'; + $mt->useMimeContentType = false; + $mt->useFileCmd = false; + $mt->useExtension = false; + $type = $mt->autoDetect($file); + + if ($type !== 'text/plain') { + return $type; + } + + + $type = MIME_Type::autoDetect($file); + return $type; + } +} + +?> diff --git a/tests/files/css/file b/tests/files/css/file new file mode 100644 index 0000000..c8d8dce --- /dev/null +++ b/tests/files/css/file @@ -0,0 +1,110 @@ +/* show IDs for anchors */ +a.anchorlink:before { + font-size: smaller; + content: '_'; + color: transparent; +} +h1[id]:hover a.anchorlink:before, +h2[id]:hover a.anchorlink:before, +h3[id]:hover a.anchorlink:before, +h4[id]:hover a.anchorlink:before, +h5[id]:hover a.anchorlink:before, +h6[id]:hover a.anchorlink:before { + content: "\00B6";/* pilcrow */ + color: #888; + font-size: smaller; +} +a.anchorlink { + text-decoration: none; + margin-left: 0.5em; + font-size: smaller; +} +.navbar .brand { + /*float: right;*/ + color: #DDA; + text-shadow: 0 0 30px rgba(255, 255, 255, .9); +} +.navbar .brand:hover { + color: #FFA; +} + +.navbar .container { + width: 940px; +} +.footer { + margin-top: 36px; + margin-bottom: 0px; + border-top: 1px solid #DDD; + color: #999; + text-align: center; +} + +h1 { + margin-bottom: 0.5ex; +} + +.repo-info { + margin-bottom: 2em; +} +.file .header { + padding: 1.0ex; + margin-bottom: 1em; + background-color: whiteSmoke; + border: 1px solid #EEE; + border: 1px solid rgba(0, 0, 0, 0.05); + -webkit-border-radius: 4px; + -moz-border-radius: 4px; + border-radius: 4px; +} +.file .header .btn-mini { + margin-left: 2px; +} +.file .code { + margin-left: 2em; +} +.file .image { + margin-bottom: 2ex; +} + +div.annotations div.alert { + margin-bottom: 1ex; +} + +ul.history li { + padding-left: 2px; + padding-bottom: 1px; +} +ul.history li.active { + background-color: #EEE; + border-radius: 3px; +} +ul.history a.hash { + font-family: monospace; +} + + +ul.pager { + margin-top: 2ex; +} + +form textarea.content { + width: 100%; + box-sizing: border-box; + font-family: monospace; +} +form .allwidth { + box-sizing: border-box; +} +form .allwidth label { + width: 20%; + float: left; +} +form input#description { + box-sizing: border-box; + height: 2em; + width: 100%; +} + +form label.inline { + display: inline; +} \ No newline at end of file diff --git a/tests/files/css/mime-type b/tests/files/css/mime-type new file mode 100644 index 0000000..cc02458 --- /dev/null +++ b/tests/files/css/mime-type @@ -0,0 +1 @@ +text/css diff --git a/tests/files/diff/file b/tests/files/diff/file new file mode 100644 index 0000000..e8c3f14 --- /dev/null +++ b/tests/files/diff/file @@ -0,0 +1,24 @@ +--- a/data/config.default.php ++++ b/data/config.default.php +@@ -45,16 +45,16 @@ $GLOBALS['phorkie']['languages'] = array( + 'mime' => 'image/jpeg', + 'show' => false + ), +- 'js' => array( +- 'title' => 'Javascript', +- 'mime' => 'application/javascript', +- 'geshi' => 'javascript' +- ), + 'ini' => array( + 'title' => 'Ini', + 'mime' => 'text/ini', + 'geshi' => 'ini' + ), ++ 'js' => array( ++ 'title' => 'Javascript', ++ 'mime' => 'application/javascript', ++ 'geshi' => 'javascript' ++ ), + 'php' => array( + 'title' => 'PHP', + 'mime' => 'text/x-php', diff --git a/tests/files/diff/mime-type b/tests/files/diff/mime-type new file mode 100644 index 0000000..8ed7978 --- /dev/null +++ b/tests/files/diff/mime-type @@ -0,0 +1 @@ +text/diff diff --git a/tests/files/htm/file b/tests/files/htm/file new file mode 100644 index 0000000..8dcbc5e --- /dev/null +++ b/tests/files/htm/file @@ -0,0 +1,8 @@ + + + foo + + +

heading

+ + \ No newline at end of file diff --git a/tests/files/htm/mime-type b/tests/files/htm/mime-type new file mode 100644 index 0000000..be49eeb --- /dev/null +++ b/tests/files/htm/mime-type @@ -0,0 +1 @@ +text/html diff --git a/tests/files/ini/file b/tests/files/ini/file new file mode 100644 index 0000000..758c821 --- /dev/null +++ b/tests/files/ini/file @@ -0,0 +1,5 @@ +foo=bar +baz=bat + +[section] +sectfoo = sectbar diff --git a/tests/files/ini/mime-type b/tests/files/ini/mime-type new file mode 100644 index 0000000..05a8515 --- /dev/null +++ b/tests/files/ini/mime-type @@ -0,0 +1 @@ +text/ini diff --git a/tests/files/js/file b/tests/files/js/file new file mode 100644 index 0000000..20066c4 --- /dev/null +++ b/tests/files/js/file @@ -0,0 +1,26 @@ +function filenameChange(elem, id) { + var filename = elem.value; + var hasExt = filename.indexOf(".") != -1; + if (hasExt) { + $('#typeselect_' + id).hide(); + $('#typetext_' + id).show(); + } else { + $('#typeselect_' + id).show(); + $('#typetext_' + id).hide(); + } +} +function initFilenames() +{ + $('input.filename').each( + function(num, elem) { + var id = elem.id; + var pos = id.indexOf('_'); + if (pos != -1) { + var elemNum = id.substr(pos + 1); + if (elemNum != 'new') { + filenameChange(elem, elemNum); + } + } + } + ); +} diff --git a/tests/files/js/mime-type b/tests/files/js/mime-type new file mode 100644 index 0000000..e0168c2 --- /dev/null +++ b/tests/files/js/mime-type @@ -0,0 +1 @@ +application/javascript diff --git a/tests/files/php/file b/tests/files/php/file new file mode 100644 index 0000000..1e1dba5 --- /dev/null +++ b/tests/files/php/file @@ -0,0 +1,14 @@ + diff --git a/tests/files/php/mime-type b/tests/files/php/mime-type new file mode 100644 index 0000000..c9d318d --- /dev/null +++ b/tests/files/php/mime-type @@ -0,0 +1 @@ +text/x-php diff --git a/tests/files/rst/file b/tests/files/rst/file new file mode 100644 index 0000000..5ece709 --- /dev/null +++ b/tests/files/rst/file @@ -0,0 +1,15 @@ +============= +Some rST test +============= + +This here is a restructuredtext test for phorkie. +Cool, isn't it? + +- Yes +- No +- Perhaps +- Awww + +Subheading +---------- +That's all folks diff --git a/tests/files/rst/mime-type b/tests/files/rst/mime-type new file mode 100644 index 0000000..96d9f57 --- /dev/null +++ b/tests/files/rst/mime-type @@ -0,0 +1 @@ +text/x-rst diff --git a/tests/files/sh/file b/tests/files/sh/file new file mode 100755 index 0000000..5ed6192 --- /dev/null +++ b/tests/files/sh/file @@ -0,0 +1,44 @@ +#!/bin/sh + +# Function to find the real directory a program resides in. +# Feb. 17, 2000 - Sam Lantinga, Loki Entertainment Software +FindPath() +{ + fullpath="`echo $1 | grep /`" + if [ "$fullpath" = "" ]; then + oIFS="$IFS" + IFS=: + for path in $PATH + do if [ -x "$path/$1" ]; then + if [ "$path" = "" ]; then + path="." + fi + fullpath="$path/$1" + break + fi + done + IFS="$oIFS" + fi + if [ "$fullpath" = "" ]; then + fullpath="$1" + fi + + # Is the sed/ls magic portable? + if [ -L "$fullpath" ]; then + #fullpath="`ls -l "$fullpath" | awk '{print $11}'`" + fullpath=`ls -l "$fullpath" |sed -e 's/.* -> //' |sed -e 's/\*//'` + fi + dirname $fullpath +} + + +here="`FindPath $0`" +echo $here +if [ ! -f "$here/lib/patch.dat" ] ; then + echo "$0: Fatal error: missing data files (*.dat) in $here/lib" + echo "If you have just installed the patch to the full version of the game" + echo "you will need to copy *.dat from your existing uplink installation" + exit 1 +fi +export LD_LIBRARY_PATH="$here/lib:$LD_LIBRARY_PATH" +$here/lib/uplink.bin.x86 "$@" diff --git a/tests/files/sh/mime-type b/tests/files/sh/mime-type new file mode 100644 index 0000000..4b02bf6 --- /dev/null +++ b/tests/files/sh/mime-type @@ -0,0 +1 @@ +image/x-shellscript diff --git a/tests/files/sql/file b/tests/files/sql/file new file mode 100644 index 0000000..8754120 --- /dev/null +++ b/tests/files/sql/file @@ -0,0 +1,13 @@ +CREATE TABLE vfs ( + vfs_id INT UNSIGNED NOT NULL, + vfs_type SMALLINT UNSIGNED NOT NULL, + vfs_path VARCHAR(255) NOT NULL, + vfs_name VARCHAR(255) NOT NULL, + vfs_modified BIGINT NOT NULL, + vfs_owner VARCHAR(255) NOT NULL, + vfs_data LONGBLOB, + PRIMARY KEY (vfs_id) +); + +CREATE INDEX vfs_path_idx ON vfs (vfs_path); +CREATE INDEX vfs_name_idx ON vfs (vfs_name); diff --git a/tests/files/sql/mime-type b/tests/files/sql/mime-type new file mode 100644 index 0000000..f5944a0 --- /dev/null +++ b/tests/files/sql/mime-type @@ -0,0 +1 @@ +text/x-sql diff --git a/tests/files/svg/file b/tests/files/svg/file new file mode 100644 index 0000000..296884c --- /dev/null +++ b/tests/files/svg/file @@ -0,0 +1,21 @@ + + + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/tests/files/svg/mime-type b/tests/files/svg/mime-type new file mode 100644 index 0000000..cfced64 --- /dev/null +++ b/tests/files/svg/mime-type @@ -0,0 +1 @@ +image/svg+xml diff --git a/tests/files/ts/file b/tests/files/ts/file new file mode 100644 index 0000000..d442667 --- /dev/null +++ b/tests/files/ts/file @@ -0,0 +1,34 @@ +config.doctype = xhtml_trans + + +page = PAGE +page.typeNum = 0 +page.10 = USER +page.10.userFunc = tx_templavoila_pi1->main_page +page.10.disableExplosivePreview = 1 + + +#special title +config.noPageTitle = 2 +page.headerData.5 = TEXT +page.headerData.5.field = title +page.headerData.5.wrap = |  - Neatful | Illustrator | Art Director + + +#no
|
around elements +# for our links +tt_content.stdWrap.innerWrap> + +tt_content.image.20.maxW = 610 + + +#remove blur link js and get rid of auto-generated javscript file +config.removeDefaultJS = 1 + +#remove default CSS styles and get rid of auto-generated css file +plugin.tx_felogin_pi1._CSS_DEFAULT_STYLE > +plugin.tx_indexedsearch._CSS_DEFAULT_STYLE > +plugin.tt_news._CSS_DEFAULT_STYLE > +plugin.tx_cssstyledcontent._CSS_DEFAULT_STYLE > +plugin.tx_cssfilelist._CSS_DEFAULT_STYLE > +xmlnews.10._CSS_DEFAULT_STYLE > diff --git a/tests/files/ts/mime-type b/tests/files/ts/mime-type new file mode 100644 index 0000000..4feac4e --- /dev/null +++ b/tests/files/ts/mime-type @@ -0,0 +1 @@ +image/x-typoscript diff --git a/tests/files/txt/file b/tests/files/txt/file new file mode 100644 index 0000000..67f9d33 --- /dev/null +++ b/tests/files/txt/file @@ -0,0 +1,3 @@ +This is a plain text file. + +Nothing more, just some simple text. diff --git a/tests/files/txt/mime-type b/tests/files/txt/mime-type new file mode 100644 index 0000000..e70108f --- /dev/null +++ b/tests/files/txt/mime-type @@ -0,0 +1 @@ +text/plain diff --git a/tests/files/xml/file b/tests/files/xml/file new file mode 100644 index 0000000..157e783 --- /dev/null +++ b/tests/files/xml/file @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tests/files/xml/mime-type b/tests/files/xml/mime-type new file mode 100644 index 0000000..93061af --- /dev/null +++ b/tests/files/xml/mime-type @@ -0,0 +1 @@ +text/xml -- 2.30.2