add paging support
authorChristian Weiske <cweiske@cweiske.de>
Mon, 4 Jan 2016 20:42:58 +0000 (21:42 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 4 Jan 2016 20:42:58 +0000 (21:42 +0100)
README.rst
src/mediatomb.php
src/podcasts.php
www/index.php

index 11bac20fe271c040f9fa9d571e91771a70c4964f..1d3d3a307a586205982ff37470a006f56f067dc9 100644 (file)
@@ -27,6 +27,7 @@ Features
 - Transcoding of non-mp3 file types to ``.mp3`` that iRadios can play
 - Single file mode for children that want to listen to a single story
   before sleeping
+- Paging results with ``?startitems=1&enditems=10`` GET parameters
 
 
 RSS feed support
index bdfa4ebae10d290b64af01944b4e9deb964ffc82..d80e20ee8681f85fc41bc931d9e81fe05cc92fcb 100644 (file)
@@ -26,7 +26,6 @@ function mediatombBrowse(Services_MediaTomb $smt, $fullPath, $prefix)
     $path = substr($fullPath, strlen($prefix));
     $container = $smt->getContainerByPath($path);
     $listItems = array();
-    addPreviousItem($listItems, $fullPath);
 
     $it = $container->getItemIterator(false);
     $it->rewind();
@@ -57,7 +56,7 @@ function mediatombBrowse(Services_MediaTomb $smt, $fullPath, $prefix)
         mediatombAddFile($listItems, $item);
     }
 
-    sendListItems($listItems);
+    sendListItems($listItems, buildPreviousItem($fullPath));
 }
 
 function mediatombAddFile(&$listItems, $item)
@@ -104,13 +103,14 @@ function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix)
     $container = $smt->getContainerByPath($path);
     $listItems = array();
 
+    $previous = null;
     if ($fileMode) {
         //show single file to play
-        addPreviousItem($listItems, pathEncode($fullPath));
+        $previous = buildPreviousItem(pathEncode($fullPath));
         $item = $smt->getSingleItem($container, $fileTitle, false);
         mediatombAddFile($listItems, $item);
     } else {
-        addPreviousItem($listItems, pathEncode('internetradio/' . $path . '/dummy'));
+        $previous = buildPreviousItem(pathEncode('internetradio/' . $path . '/dummy'));
 
         //browse directory
         foreach ($container->getItemIterator(false) as $item) {
@@ -121,6 +121,6 @@ function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix)
         }
     }
 
-    sendListItems($listItems);
+    sendListItems($listItems, $previous);
 }
 ?>
index 3acccbbb2a1713136be5febc605b60f57746a753..bb8f5ce7d675988482cbfe2a4620b216701f3f5e 100644 (file)
@@ -21,7 +21,6 @@ function sendPodcast($path)
     
     $sx = simplexml_load_file($cacheFile);
     $listItems = array();
-    addPreviousItem($listItems, $path);
 
     foreach ($sx->channel->item as $item) {
         $title = (string) $item->title;
@@ -35,7 +34,7 @@ function sendPodcast($path)
             'MP3'
         );
     }
-    sendListItems($listItems);
+    sendListItems($listItems, buildPreviousItem($path));
 }
 
 
index a81f774bddb46d6ae2530597001daad873385c4e..b3b2a7b4b59d821ac1f80f39fc024b5ce5036c4c 100644 (file)
@@ -2,7 +2,7 @@
 require_once __DIR__ . '/../src/header.php';
 
 if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0'
-    || $fullUri == '/RadioNativeLogin.php'
+    || $fullUri == '/RadioNativeLogin.phpb'
 ) {
     //initial login for "internet radio", podcasts and "my noxon"
     header('Content-type: text/html');
@@ -77,7 +77,7 @@ function sendDir($path)
     global $varDir;
 
     $listItems = array();
-    addPreviousItem($listItems, $path);
+    $enablePaging = true;
 
     $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
     $count = 0;
@@ -99,6 +99,7 @@ function sendDir($path)
         ) {
             //automatically execute script while listing this directory
             addScriptOutput($listItems, $entry);
+            $enablePaging = false;
         } else if ($ext == 'txt' || is_executable($entry)) {
             //plain text file
             ++$count;
@@ -108,7 +109,7 @@ function sendDir($path)
     if (!$count) {
         $listItems[] = getMessageItem('No files or folders');
     }
-    sendListItems($listItems);
+    sendListItems($listItems, buildPreviousItem($path), $enablePaging);
 }
 
 function sendScript($path)
@@ -116,11 +117,10 @@ function sendScript($path)
     global $varDir;
 
     $listItems = array();
-    addPreviousItem($listItems, $path);
 
     $fullPath = $varDir . $path;
     addScriptOutput($listItems, $fullPath);
-    sendListItems($listItems);
+    sendListItems($listItems, buildPreviousItem($path), false);
 }
 
 function addScriptOutput(&$listItems, $fullPath)
@@ -139,11 +139,10 @@ function sendTextFile($path)
 {
     global $varDir;
     $listItems = array();
-    addPreviousItem($listItems, $path);
 
     $lines = file($varDir . $path);
     addTextLines($listItems, $lines);
-    sendListItems($listItems);
+    sendListItems($listItems, buildPreviousItem($path));
 }
 
 function addTextLines(&$listItems, $lines)
@@ -215,13 +214,13 @@ function getPreviousItem($urlPath)
         . '</Item>';
 }
 
-function addPreviousItem(&$listItems, $urlPath)
+function buildPreviousItem($urlPath)
 {
     $parentDir = dirname($urlPath) . '/';
     if ($parentDir == '/') {
-        return;
+        return null;
     }
-    $listItems[] = getPreviousItem($parentDir);
+    return getPreviousItem($parentDir);
 }
 
 function nox_esc($string)
@@ -234,23 +233,38 @@ function sendMessage($msg)
     sendListItems(array(getMessageItem($msg)));
 }
 
-function sendListItems($listItems)
+function sendListItems($listItems, $previous = null, $enablePaging = true)
 {
     $startitems = 1;
-    $enditems = 10;
+    $enditems   = 100000;
     if (isset($_GET['startitems'])) {
         $startitems = (int) $_GET['startitems'];
     }
     if (isset($_GET['enditems'])) {
         $enditems = (int) $_GET['enditems'];
     }
-    //TODO: limit list
+
+    if ($enablePaging) {
+        $itemCount = count($listItems);
+    } else {
+        $itemCount = -1;
+    }
+    if ($previous !== null) {
+        $previous .= "\n";
+    }
 
     $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
     $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
     $xml .= '<ListOfItems>' . "\n";
+    $xml .= '<ItemCount>' . $itemCount . '</ItemCount>' . "\n";
+    $xml .= $previous;
+
+    $num = 0;
     foreach ($listItems as $item) {
-        $xml .= $item . "\n";
+        ++$num;
+        if (!$enablePaging || ($num >= $startitems && $num <= $enditems)) {
+            $xml .= $item . "\n";
+        }
     }
     $xml .= "</ListOfItems>\n";