Skip to content

Instantly share code, notes, and snippets.

@junnama
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junnama/11106932 to your computer and use it in GitHub Desktop.
Save junnama/11106932 to your computer and use it in GitHub Desktop.
EntriesFromRSS/php/config.php
<?php
require_once( 'MTUtil.php' );
class EntriesFromRSS extends MTPlugin {
var $registry = array(
'config_settings' => array(
'RewriteArchiverRewriteTo' => array( 'default' => '/archiver.html' ),
),
'callbacks' => array(
'post_init' => 'rewrite_archiver',
),
'tags' => array(
'block' => array(
'entriesfromrss' => '_hdlr_entriesfromrss',
),
),
);
function rewrite_archiver ( $mt, &$ctx, &$args ) {
$blog = $ctx->stash( 'blog' );
$app = $ctx->stash( 'bootstrapper' );
$file = $app->stash( 'file' );
$url = $app->stash( 'url' );
$request = $app->stash( 'request' );
$tmpl = $app->config( 'RewriteArchiverRewriteTo' );
$tmpl = preg_replace( '/^\//', '', $tmpl );
$tmpl = preg_replace( '/\//', DIRECTORY_SEPARATOR, $tmpl );
$site_url = $blog->site_url();
$root = preg_replace( '/^https{0,1}:\/\/.*?\//', '/', $site_url );
$search = preg_quote( $root, '/' );
$path = preg_replace( "/^$search/", '', $request );
if ( strpos( $path, '/' ) === FALSE ) {
return;
}
$category = preg_replace( "!(.*)/.*$!", '$1', $path );
$new_url = $site_url . $tmpl;
$new_request = $root . $tmpl;
$new_file = $blog->site_path() . DIRECTORY_SEPARATOR . $tmpl;
if (! file_exists( $new_file ) ) return;
$cat = cat_path_to_category( $category, NULL, 'category' );
if ( isset( $cat ) ) {
$app->stash( 'file', $new_file );
$app->stash( 'request', $new_request );
$app->stash( 'url', $new_url );
$app->stash( 'orig_url', $url );
$ctx->stash( 'category', $cat[ 0 ] );
$ctx->__stash[ 'vars' ][ 'category' ] = $category;
$ctx->__stash[ 'vars' ][ 'offset' ] = $app->param( 'offset' );
$ctx->__stash[ 'vars' ][ 'limit' ] = $app->param( 'limit' );
$cache = $app->cache_filename( $ctx->stash( 'blog_id' ), $new_file, $new_url );
$app->stash( 'cache', $cache );
}
}
function _hdlr_entriesfromrss ( $args, $content, &$ctx, &$repeat ) {
$localvars = common_loop_vars();
if (! isset( $content ) ) {
if ( isset( $args[ 'xml' ] ) ) {
$xml = $args[ 'xml' ];
} else {
$xml = 'atom.xml';
}
if ( isset( $args[ 'category' ] ) ) {
$current_cat = $args[ 'category' ];
}
if ( isset( $args[ 'limit' ] ) ) {
$limit = $args[ 'limit' ];
}
if ( isset( $args[ 'offset' ] ) ) {
$offset = $args[ 'offset' ];
} else {
$offset = 0;
}
$xml = file_get_contents( $xml );
$xml_obj = simplexml_load_string( $xml );
$xml_vars = get_object_vars( $xml_obj );
$entries = $xml_vars[ 'entry' ];
$entries_from_xml = array();
$_count = 0;
foreach ( $entries as $entry ) {
if ( $current_cat ) {
$categories = get_object_vars( $entry->category );
if ( isset( $categories ) ) {
foreach ( $categories as $category ) {
$cat_label = $category[ 'term' ];
if ( $cat_label == $current_cat ) {
if ( $offset <= $_count ) {
if (! $limit ) {
$entries_from_xml[] = $entry;
} else {
if ( count( $entries_from_xml ) < $limit ) {
$entries_from_xml[] = $entry;
} else {
$last = 1;
}
}
}
$_count++;
break;
}
}
}
if ( $last ) {
break;
}
} else {
if ( $limit || $offset ) {
if ( $offset <= $_count ) {
if (! $limit ) {
$entries_from_xml[] = $entry;
} else {
if ( count( $entries_from_xml ) < $limit ) {
$entries_from_xml[] = $entry;
} else {
$last = 1;
}
}
}
$_count++;
} else {
$entries_from_xml = $entries;
$last = 1;
}
if ( $last ) {
break;
}
}
}
if (! count( $entries_from_xml ) ) {
$repeat = FALSE;
return '';
}
$ctx->localize( $localvars );
$entries = $entries_from_xml;
$ctx->__stash[ 'vars' ][ '__counter__' ] = 0;
$ctx->stash( 'entries', $entries );
} else {
$entries = $ctx->stash( 'entries' );
}
$counter = $ctx->__stash[ 'vars' ][ '__counter__' ];
if ( $counter < count( $entries ) ) {
$entry = $entries[ $counter ];
$counter++;
$ctx->__stash[ 'vars' ][ '__counter__' ] = $counter;
$ctx->__stash[ 'vars' ][ '__odd__' ] = ( $counter % 2 ) == 1;
$ctx->__stash[ 'vars' ][ '__even__' ] = ( $counter % 2 ) == 0;
$ctx->__stash[ 'vars' ][ '__first__' ] = $counter == 1;
$ctx->__stash[ 'vars' ][ '__last__' ] = ( $counter == count( $entries ) );
$entry_vars = get_object_vars( $entry );
foreach ( $entry_vars as $entry_key => $entry_var ) {
if ( is_string( $entry_var ) ) {
$ctx->__stash[ 'vars' ][ $entry_key ] = $entry_var;
} else {
$entry_var = get_object_vars( $entry_var );
if ( $entry_key == 'link' ) {
$ctx->__stash[ 'vars' ][ 'permalink' ] = $entry_var[ '@attributes' ][ 'href' ];
} elseif ( $entry_key == 'author' ) {
$ctx->__stash[ 'vars' ][ 'author_name' ] = $entry_var[ 'name' ];
}
}
}
$repeat = TRUE;
} else {
$ctx->restore( $localvars );
$repeat = FALSE;
}
return $content;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment