Add "domain" and "branch" arguments

This commit is contained in:
Alexander Dietrich 2017-11-17 21:37:10 +01:00
parent ffc5ca3583
commit a66ba3a361
2 changed files with 112 additions and 67 deletions

View file

@ -7,7 +7,8 @@ Wordpress plugin to render latest (Hamburg-flavoured)
[gluon firmware](https://github.com/freifunkhamburg/gluon) version table. [gluon firmware](https://github.com/freifunkhamburg/gluon) version table.
Provides shortcode `[ff_hh_versions]` to display 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: Output looks like this:
@ -15,7 +16,26 @@ Output looks like this:
Arguments 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: Example:
``` ```
@ -23,4 +43,3 @@ Example:
Ubiquiti Firmware: [ff_hh_versions grep="ubiquiti"] Ubiquiti Firmware: [ff_hh_versions grep="ubiquiti"]
``` ```

View file

@ -3,23 +3,24 @@
Plugin Name: Freifunk Hamburg Firmware List Shortcode Plugin Name: Freifunk Hamburg Firmware List Shortcode
Plugin URI: http://mschuette.name/ Plugin URI: http://mschuette.name/
Description: Defines shortcodes to display Freifunk Hamburg Firmware versions Description: Defines shortcodes to display Freifunk Hamburg Firmware versions
Version: 0.4dev Version: 0.5dev
Author: Martin Schuette Author: Martin Schuette
Author URI: http://mschuette.name/ Author URI: http://mschuette.name/
Licence: 2-clause BSD Licence: 2-clause BSD
*/ */
define( 'FF_HH_STABLE_BASEDIR', 'https://updates.hamburg.freifunk.net/stable/' ); define( 'FF_HH_UPDATES_URL', 'https://updates.hamburg.freifunk.net/' );
define( 'FF_HH_CACHETIME', 1 ); define( 'FF_HH_CACHETIME', 1 );
/* gets metadata from URL, handles caching */ /* gets metadata from URL, handles caching */
function ff_hh_getmanifest( $basedir ) { function ff_hh_getmanifest( $branch_url, $domain, $branch ) {
// Caching // Caching
if ( WP_DEBUG || ( false === ( $manifest = get_transient( 'ff_hh_manifest' ) ) ) ) { $cache_key = 'ff_hh_manifest_' . $domain . '_' . $branch;
$manifest = array(); if ( WP_DEBUG || ( false === ( $manifest = get_transient( $cache_key ) ) ) ) {
$url = $basedir . 'sysupgrade/stable.manifest'; $manifest = array();
$url = $branch_url . '/sysupgrade/' . $branch . '.manifest';
$http_response = wp_remote_get( $url ); // TODO: error handling $http_response = wp_remote_get( $url ); // TODO: error handling
$input = wp_remote_retrieve_body( $http_response ); $input = wp_remote_retrieve_body( $http_response );
foreach ( explode( "\n", $input ) as $line ) { foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename ); $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ( $ret === 4 ) { if ( $ret === 4 ) {
@ -34,23 +35,25 @@ function ff_hh_getmanifest( $basedir ) {
} }
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
set_transient( 'ff_hh_manifest', $manifest, $cachetime ); set_transient( $cache_key, $manifest, $cachetime );
} }
return $manifest; return $manifest;
} }
/* gets latest version from first manifest line */ /* gets latest version from first manifest line */
function ff_hh_getlatest( $basedir ) { function ff_hh_getlatest( $branch_url, $domain, $branch ) {
// Caching // Caching
if ( false === ( $sw_ver = get_transient( 'ff_hh_latestversion' ) ) ) { $cache_key = 'ff_hh_latestversion_' . $domain . '_' . $branch;
if ( false === ( $sw_ver = get_transient( $cache_key ) ) ) {
$sw_ver = 'unknown'; $sw_ver = 'unknown';
$input = wp_remote_retrieve_body( wp_remote_get( $basedir . 'sysupgrade/stable.manifest' ) ); $url = $branch_url . '/sysupgrade/' . $branch . '.manifest';
$input = wp_remote_retrieve_body( wp_remote_get( $url ) );
foreach ( explode( "\n", $input ) as $line ) { foreach ( explode( "\n", $input ) as $line ) {
$ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename ); $ret = sscanf( $line, '%s %s %s %s', $hw, $sw_ver, $hash, $filename );
if ( $ret === 4 ) { if ( $ret === 4 ) {
// break processing on first matching line // break processing on first matching line
$cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS; $cachetime = FF_HH_CACHETIME * MINUTE_IN_SECONDS;
set_transient( 'ff_hh_latestversion', $sw_ver, $cachetime ); set_transient( $cache_key, $sw_ver, $cachetime );
break; break;
} }
} }
@ -63,32 +66,53 @@ if ( ! shortcode_exists( 'ff_hh_latestversion' ) ) {
} }
// Example: // Example:
// [ff_hh_latestversion] // [ff_hh_latestversion]
// [ff_hh_latestversion domain="ffhh-sued" branch="experimental"]
function ff_hh_shortcode_latestversion( $atts, $content, $name ) { function ff_hh_shortcode_latestversion( $atts, $content, $name ) {
$sw_ver = ff_hh_getlatest( FF_HH_STABLE_BASEDIR ); $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;
$sw_ver = ff_hh_getlatest( $branch_url, $domain, $branch );
$outstr = "<span class=\"ff $name\">$sw_ver</span>"; $outstr = "<span class=\"ff $name\">$sw_ver</span>";
return $outstr; return $outstr;
} }
if ( ! shortcode_exists( 'ff_hh_versions' ) ) { 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: // Example:
// [ff_hh_versions] // [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 ) { function ff_hh_shortcode_versions( $atts, $content, $name ) {
$manifest = ff_hh_getmanifest( FF_HH_STABLE_BASEDIR ); $domain = 'ffhh';
$branch = 'stable';
$grep = 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'];
}
}
$branch_url = FF_HH_UPDATES_URL . $domain . '/' . $branch;
$manifest = ff_hh_getmanifest( $branch_url, $domain, $branch );
$outstr = "<div class=\"ff $name\">"; $outstr = "<div class=\"ff $name\">";
$outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>'; $outstr .= '<table><tr><th>Modell</th><th>Erstinstallation</th><th>Aktualisierung</th></tr>';
# optionally filter output by given substring
if ( is_array( $atts )
&& array_key_exists( 'grep', $atts )
&& ! empty( $atts['grep'] ) ) {
$grep = $atts['grep'];
} else {
$grep = false;
}
foreach ( $manifest as $hw => $versions ) { foreach ( $manifest as $hw => $versions ) {
// filter // filter
if ( $grep && ( false === strpos( $hw, $grep ) ) ) { if ( $grep && ( false === strpos( $hw, $grep ) ) ) {
@ -107,12 +131,13 @@ function ff_hh_shortcode_versions( $atts, $content, $name ) {
$filename = str_replace( '-sysupgrade', '', $filename ); $filename = str_replace( '-sysupgrade', '', $filename );
if (strpos($filename,'netgear') !== false) { if (strpos($filename,'netgear') !== false) {
$filename = str_replace( '.bin', '.img', $filename ); $filename = str_replace( '.bin', '.img', $filename );
$filename = str_replace( '.tar', '.img', $filename ); $filename = str_replace( '.tar', '.img', $filename );
} }
$hw_ver_links[] = sprintf( $hw_ver_links[] = sprintf(
'<a href="%s%s">%s.x</a>', '<a href="%s%s">%s.x</a>',
FF_HH_STABLE_BASEDIR.'factory/', $branch_url . '/factory/',
$filename, $hw_ver $filename,
$hw_ver
); );
} }
if ( count($hw_ver_links) > 0) { if ( count($hw_ver_links) > 0) {
@ -126,8 +151,9 @@ function ff_hh_shortcode_versions( $atts, $content, $name ) {
foreach ( $versions as $hw_ver => $filename ) { foreach ( $versions as $hw_ver => $filename ) {
$hw_ver_links[] = sprintf( $hw_ver_links[] = sprintf(
'<a href="%s%s">%s.x</a>', '<a href="%s%s">%s.x</a>',
FF_HH_STABLE_BASEDIR.'sysupgrade/', $branch_url . '/sysupgrade/',
$filename, $hw_ver $filename,
$hw_ver
); );
} }
$outstr .= '<td>Hardware Ver. ' . join( ', ', $hw_ver_links ) . '</td>'; $outstr .= '<td>Hardware Ver. ' . join( ', ', $hw_ver_links ) . '</td>';
@ -171,43 +197,43 @@ function ff_hh_beautify_hw_name( $hw, $discard_vendor = '' ) {
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( 'HP-AG300H-WZR-600DHP', 'HP-AG300H & WZR-600DHP', $hw ); $hw = str_replace( 'HP-AG300H-WZR-600DHP', 'HP-AG300H & WZR-600DHP', $hw );
$hw = str_replace( '-WZR', 'WZR', $hw ); $hw = str_replace( '-WZR', 'WZR', $hw );
} elseif ( ! strncmp( $hw, 'netgear', 7 ) ) { } elseif ( ! strncmp( $hw, 'netgear', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'allnet', 6 ) ) { } elseif ( ! strncmp( $hw, 'allnet', 6 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'gl-inet', 7 ) ) { } elseif ( ! strncmp( $hw, 'gl-inet', 7 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'onion-omega', 11 ) ) { } elseif ( ! strncmp( $hw, 'onion-omega', 11 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
} elseif ( ! strncmp( $hw, 'alfa', 4 ) ) { } elseif ( ! strncmp( $hw, 'alfa', 4 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'wd', 2 ) ) { } elseif ( ! strncmp( $hw, 'wd', 2 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, '8devices', 8 ) ) { } elseif ( ! strncmp( $hw, '8devices', 8 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( 'CARAMBOLA2-BOARD', 'Carambola 2', $hw ); $hw = str_replace( 'CARAMBOLA2-BOARD', 'Carambola 2', $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'meraki', 6 ) ) { } elseif ( ! strncmp( $hw, 'meraki', 6 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( 'meraki', '', $hw ); $hw = str_replace( 'meraki', '', $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} elseif ( ! strncmp( $hw, 'openmesh', 8 ) ) { } elseif ( ! strncmp( $hw, 'openmesh', 8 ) ) {
if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw ); if ( $discard_vendor ) $hw = str_replace( $discard_vendor, '', $hw );
$hw = strtoupper( $hw ); $hw = strtoupper( $hw );
$hw = str_replace( 'openmesh', '', $hw ); $hw = str_replace( 'openmesh', '', $hw );
$hw = str_replace( '-', '', $hw ); $hw = str_replace( '-', '', $hw );
} }
return $hw; return $hw;
} }