diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4721428 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: php +sudo: false + +php: + - 5.3 + - 5.4 + - 5.5 + - 5.6 + - hhvm + +env: + - WP_VERSION=latest + +before_script: + - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + - fgrep wp_version /tmp/wordpress/wp-includes/version.php + +script: phpunit diff --git a/README.md b/README.md index 04a97fc..b6e9fd0 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ freifunk-versions ================= +[![Build Status](https://travis-ci.org/freifunkhamburg/freifunk-versions.svg?branch=master)](https://travis-ci.org/freifunkhamburg/freifunk-versions) + Wordpress plugin to render latest (Hamburg-flavoured) [gluon firmware](https://github.com/freifunkhamburg/gluon) version table. Provides shortcode `[ff_hh_versions]` to display table. -Input data is read from http://updates.hamburg.freifunk.net/stable/sysupgrade/manifest +Input data is read from i.e. +https://updates.hamburg.freifunk.net/ffhh/stable/sysupgrade/stable.manifest Output looks like this: @@ -13,7 +16,26 @@ Output looks like this: Arguments --------- -An optional argument `grep` allows you show a subset of hardware versions, +The optional argument `domain` (default: `ffhh`) allows you to choose the +firmware's domain. + +Example: + +``` + Hamburg-Süd Firmware: [ff_hh_versions domain="ffhh-sued"] +``` + +The optional argument `branch` (default: `stable`) allows you to choose the +firmware's branch. + +Example: + +``` + Experimental Firmware: [ff_hh_versions branch="experimental"] +``` + +The optional argument `grep` allows you show a subset of hardware versions. + Example: ``` @@ -21,4 +43,3 @@ Example: Ubiquiti Firmware: [ff_hh_versions grep="ubiquiti"] ``` - diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100755 index 0000000..2ed09d9 --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} + +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} +WP_CORE_DIR=/tmp/wordpress/ + +set -ex + +install_wp() { + mkdir -p $WP_CORE_DIR + + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + + wget -nv -O /tmp/wordpress.tar.gz http://wordpress.org/${ARCHIVE_NAME}.tar.gz + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR + + wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # set up testing suite + mkdir -p $WP_TESTS_DIR + cd $WP_TESTS_DIR + svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ + + wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php +} + +install_db() { + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_wp +install_test_suite +install_db + diff --git a/freifunk-versions.php b/freifunk-versions.php index 7b5e567..b3190c0 100644 --- a/freifunk-versions.php +++ b/freifunk-versions.php @@ -3,138 +3,264 @@ Plugin Name: Freifunk Hamburg Firmware List Shortcode Plugin URI: http://mschuette.name/ Description: Defines shortcodes to display Freifunk Hamburg Firmware versions -Version: 0.4 +Version: 0.5dev Author: Martin Schuette Author URI: http://mschuette.name/ Licence: 2-clause BSD */ -define('FF_HH_STABLE_BASEDIR', 'http://updates.hamburg.freifunk.net/stable/'); -define('FF_HH_CACHETIME', 15); +define( 'FF_HH_UPDATES_URL', 'https://updates.hamburg.freifunk.net/' ); +define( 'FF_HH_CACHETIME', 1 ); /* gets metadata from URL, handles caching */ -function ff_hh_getmanifest ($basedir) { - // Caching - if ( WP_DEBUG || ( false === ( $manifest = get_transient( "ff_hh_manifest" ) ) ) ) { - $manifest = array(); - $url = $basedir . 'sysupgrade/manifest'; - $http_response = wp_remote_get( $url ); // TODO: error handling - $input = wp_remote_retrieve_body( $http_response ); - foreach ( explode("\n", $input) as $line ) { - $ret = sscanf($line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename); - if ($ret === 4) { - if (preg_match('/^(.*)-v(\d+)$/', $hw, $matches)) { - $hw = $matches[1]; - $hw_ver = $matches[2]; - } else { - $hw_ver = '1'; - } - $manifest[$hw][$hw_ver] = $filename; - } - } +function ff_hh_getmanifest( $branch_url, $domain, $branch ) { + // Caching + $cache_key = 'ff_hh_manifest_' . $domain . '_' . $branch; + if ( WP_DEBUG || ( false === ( $manifest = get_transient( $cache_key ) ) ) ) { + $manifest = array(); + $url = $branch_url . '/sysupgrade/' . $branch . '.manifest'; + $http_response = wp_remote_get( $url ); // TODO: error handling + $input = wp_remote_retrieve_body( $http_response ); + foreach ( explode( "\n", $input ) as $line ) { + $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename ); + if ( $ret === 4 ) { + if ( preg_match( '/^(.*)-v(\d+)$/', $hw, $matches ) ) { + $hw = $matches[1]; + $hw_ver = $matches[2]; + } else { + $hw_ver = '1'; + } + $manifest[$hw][$hw_ver] = $filename; + } + } - $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; - set_transient( "ff_hh_manifest", $manifest, $cachetime ); - } - return $manifest; + $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; + set_transient( $cache_key, $manifest, $cachetime ); + } + return $manifest; } /* gets latest version from first manifest line */ -function ff_hh_getlatest ($basedir) { - // Caching - if ( false === ( $sw_ver = get_transient( "ff_hh_latestversion" ) ) ) { - $sw_ver = 'unknown'; - $input = wp_remote_retrieve_body( wp_remote_get($basedir . 'sysupgrade/manifest') ); - foreach ( explode("\n", $input) as $line ) { - $ret = sscanf($line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename); - if ($ret === 4) { - // break processing on first matching line - $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; - set_transient( "ff_hh_latestversion", $sw_ver, $cachetime ); - break; - } - } - } - return $sw_ver; +function ff_hh_getlatest( $branch_url, $domain, $branch ) { + // Caching + $cache_key = 'ff_hh_latestversion_' . $domain . '_' . $branch; + if ( false === ( $sw_ver = get_transient( $cache_key ) ) ) { + $sw_ver = 'unknown'; + $url = $branch_url . '/sysupgrade/' . $branch . '.manifest'; + $input = wp_remote_retrieve_body( wp_remote_get( $url ) ); + foreach ( explode( "\n", $input ) as $line ) { + $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename ); + if ( $ret === 4 ) { + // break processing on first matching line + $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; + set_transient( $cache_key, $sw_ver, $cachetime ); + break; + } + } + } + return $sw_ver; } if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) { - add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion'); + add_shortcode( 'ff_hh_latestversion', 'ff_hh_shortcode_latestversion' ); } // Example: // [ff_hh_latestversion] +// [ff_hh_latestversion domain="ffhh-sued" branch="experimental"] function ff_hh_shortcode_latestversion( $atts, $content, $name ) { - $sw_ver = ff_hh_getlatest(FF_HH_STABLE_BASEDIR); - $outstr = "$sw_ver"; - return $outstr; + $domain = 'ffhh'; + $branch = 'stable'; + if ( is_array( $atts ) ) { + if ( array_key_exists( 'domain', $atts ) && ! empty( $atts['domain'] ) ) { + $domain = $atts['domain']; + } + if ( array_key_exists( 'branch', $atts ) && ! empty( $atts['branch'] ) ) { + $branch = $atts['branch']; + } + } + + $branch_url = FF_HH_UPDATES_URL . $domain . '/' . $branch; + if ( $domain === 'multi' ) { $branch_url = $branch_url . '/images'; } + $sw_ver = ff_hh_getlatest( $branch_url, $domain, $branch ); + $outstr = "$sw_ver"; + return $outstr; } + if ( ! shortcode_exists( 'ff_hh_versions' ) ) { - add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions'); + add_shortcode( 'ff_hh_versions', 'ff_hh_shortcode_versions' ); } // Example: // [ff_hh_versions] -// [ff_hh_versions grep="ubiquiti"] +// [ff_hh_versions domain="ffhh-sued" branch="experimental" grep="ubiquiti"] function ff_hh_shortcode_versions( $atts, $content, $name ) { - $manifest = ff_hh_getmanifest(FF_HH_STABLE_BASEDIR); + $domain = 'ffhh'; + $branch = 'stable'; + $grep = false; + $filter = false; + if ( is_array( $atts ) ) { + if ( array_key_exists( 'domain', $atts ) && ! empty( $atts['domain'] ) ) { + $domain = $atts['domain']; + } + if ( array_key_exists( 'branch', $atts ) && ! empty( $atts['branch'] ) ) { + $branch = $atts['branch']; + } + if ( array_key_exists( 'grep', $atts ) && ! empty( $atts['grep'] ) ) { + $grep = $atts['grep']; + } + if ( array_key_exists( 'filter', $atts ) && ! empty( $atts['filter'] ) ) { + $filter = explode ( ',', $atts['filter'] ); + } + } - $outstr = "
"; - $outstr .= ''; + $branch_url = FF_HH_UPDATES_URL . $domain . '/' . $branch; + if ( $domain === 'multi' ) { $branch_url = $branch_url . '/images'; } + $manifest = ff_hh_getmanifest( $branch_url, $domain, $branch ); - # optionally filter output by given substring - if (is_array($atts) - && array_key_exists('grep', $atts) - && !empty($atts['grep'])) { - $grep = $atts['grep']; - } else { - $grep = false; - } + $outstr = "
"; + $outstr .= '
ModellErstinstallationAktualisierung
'; - foreach ($manifest as $hw => $versions) { - // filter - if ($grep && (false === strpos($hw, $grep))) { - continue; - } - // beautify HW model names - if (!strncmp($hw, 'tp-link', 7)) { - if ($grep) $hw = str_replace($grep, '', $hw); - $hw = strtoupper($hw); - $hw = str_replace('-', ' ', $hw); - $hw = str_replace('TP LINK TL ', 'TP-Link TL-', $hw); - } elseif (!strncmp($hw, 'ubiquiti', 8)) { - if ($grep) $hw = str_replace($grep, '', $hw); - $hw = str_replace('bullet-m', 'bullet-m / nanostation-loco-m', $hw); - $hw = str_replace('-m', ' M2', $hw); - $hw = str_replace('-', ' ', $hw); - $hw = ucwords($hw); - } elseif (!strncmp($hw, 'd-link', 6)) { - if ($grep) $hw = str_replace($grep, '', $hw); - $hw = str_replace('-', ' ', $hw); - $hw = str_replace('d link dir ', 'D-Link DIR-', $hw); - } - $outstr .= sprintf("\n", $hw); + ksort($manifest); + foreach ( $manifest as $hw => $versions ) { + // select some models + if ( $grep && ( false === strpos( $hw, $grep ) ) ) { + continue; + } + // filter others + if ( $filter ) { + $filtered = false; + foreach ( $filter as $flt ) { + if ( strpos ( $hw, $flt ) !== false ) { + $filtered = true; + break; + } + } + if ( $filtered ) { + continue; + } + } - // factory versions - $hw_ver_links = array(); - foreach ($versions as $hw_ver => $filename) { - $filename = str_replace('-sysupgrade', '', $filename); - $hw_ver_links[] = sprintf('%s.x', - FF_HH_STABLE_BASEDIR.'factory/', $filename, $hw_ver); - } - $outstr .= ''; + $hw = ff_hh_beautify_hw_name( $hw, $grep ); + $outstr .= sprintf( "\n", $hw ); - // sysupgrade versions - $hw_ver_links = array(); - foreach ($versions as $hw_ver => $filename) { - $hw_ver_links[] = sprintf('%s.x', - FF_HH_STABLE_BASEDIR.'sysupgrade/', $filename, $hw_ver); - } - $outstr .= ''; + // factory versions + $hw_ver_links = array(); + foreach ( $versions as $hw_ver => $filename ) { + if ( strpos( $hw, 'Unifi Ac Pro' ) || strpos( $hw, 'Unifi Ac Lite' ) ) { + continue; + } - $outstr .= ''; - } + $filename = str_replace( '-sysupgrade', '', $filename ); + if (strpos($filename,'netgear') !== false) { + $filename = str_replace( '.bin', '.img', $filename ); + $filename = str_replace( '.tar', '.img', $filename ); + } + $hw_ver_links[] = sprintf( + '%s.x', + $branch_url . '/factory/', + $filename, + $hw_ver + ); + } + if ( count($hw_ver_links) > 0) { + $outstr .= ''; + } else { + $outstr .= ''; + } - $outstr .= '
ModellErstinstallationAktualisierung
%sHardware Version ' . join(', ', $hw_ver_links) . '
%sHardware Version ' . join(', ', $hw_ver_links) . '
Hardware Ver. ' . join( ', ', $hw_ver_links ) . 'Benutze das Image
zur Aktualisierung
'; - $outstr .= '
'; - // $outstr .= '
'.print_r($manifest, true).'
'; - return $outstr; + // sysupgrade versions + $hw_ver_links = array(); + foreach ( $versions as $hw_ver => $filename ) { + $hw_ver_links[] = sprintf( + '%s.x', + $branch_url . '/sysupgrade/', + $filename, + $hw_ver + ); + } + $outstr .= 'Hardware Ver. ' . join( ', ', $hw_ver_links ) . ''; + + $outstr .= ''; + } + + $outstr .= ''; + $outstr .= ''; + // $outstr .= '
'.print_r( $manifest, true ).'
'; + return $outstr; +} + +// some crude rules to add capitalization and whitespace to the +// hardware model name. +// set $discard_vendor to strip the vendor name +// (used for single-vendor lists, e.g. $discard_vendor = 'tp-link' ) +function ff_hh_beautify_hw_name( $hw, $discard_vendor = '' ) { + if ( ! strncmp( $hw, 'tp-link', 7 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', ' ', $hw ); + $hw = str_replace( ' TL ', ' TL-', $hw ); + } elseif ( ! strncmp( $hw, 'ubiquiti', 8 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = str_replace( 'bullet-m', 'bullet-m', $hw ); + $hw = str_replace( '-', ' ', $hw ); + $hw = ucwords( $hw ); + } elseif ( ! strncmp( $hw, 'ubnt', 4 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = str_replace( 'erx', 'ER-X', $hw ); + $hw = str_replace( 'sfp', 'SFP', $hw ); + $hw = trim( $hw, ' -' ); + $hw = ucwords( $hw ); + } elseif ( ! strncmp( $hw, 'd-link', 6 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', ' ', $hw ); + $hw = str_replace( ' DIR ', ' DIR-', $hw ); + } elseif ( ! strncmp( $hw, 'linksys', 7 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', ' ', $hw ); + $hw = str_replace( ' WRT', ' WRT-', $hw ); + } elseif ( ! strncmp( $hw, 'buffalo', 7 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( 'HP-AG300H-WZR-600DHP', 'HP-AG300H & WZR-600DHP', $hw ); + $hw = str_replace( '-WZR', 'WZR', $hw ); + } elseif ( ! strncmp( $hw, 'netgear', 7 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'allnet', 6 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'gl-', 3 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'onion-omega', 11 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + } elseif ( ! strncmp( $hw, 'alfa', 4 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'wd', 2 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, '8devices', 8 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( 'CARAMBOLA2-BOARD', 'Carambola 2', $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'meraki', 6 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( 'meraki', '', $hw ); + $hw = str_replace( '-', '', $hw ); + } elseif ( ! strncmp( $hw, 'openmesh', 8 ) ) { + if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); + $hw = strtoupper( $hw ); + $hw = str_replace( 'openmesh', '', $hw ); + $hw = str_replace( '-', '', $hw ); + } + return $hw; } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..aea0bcf --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,15 @@ + + + + ./tests/ + + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..e460912 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,15 @@ +assertEquals( $expect, trim( $result ) ); + } + } +}