1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-03 17:50:11 +02:00

The very simplest repository browser you could imagine, as a WordPress plugin

This commit is contained in:
Ciaran Gultnieks 2010-12-04 23:59:29 +00:00
parent 9f8e950374
commit 0dd8f9bb70
2 changed files with 195 additions and 0 deletions

34
wp-fdroid/readme.txt Normal file
View File

@ -0,0 +1,34 @@
=== WP FDroid ===
Contributors: CiaranG
Tags: android, repository
Requires at least: 2.9
Tested up to: 2.9
Stable tag: 0.1
Provides the ability to browse the contents of an FDroid repository.
== Description ==
This plugin provides the ability to browse the contents of an FDroid
repository. The repository browser can be inserted into the site by
creating a page containing the 'fdroidrepo' shortcode.
License: GPL v3
== Installation ==
1. Download and extract the zip file.
2. Upload the plugin directory to /wp-content/plugins on your server.
3. Go to the Plugins screen in your Wordpress Admin and activate it.
== Frequently Asked Questions ==
= Is this finished? =
No it isn't. I'm working on it.
== Screenshots ==
1. There isn't one yet actually.

161
wp-fdroid/wp-fdroid.php Normal file
View File

@ -0,0 +1,161 @@
<?php
/*
Plugin Name: WP FDroid
Plugin URI: http://f-droid.org/repository
Description: An FDroid repository browser
Author: Ciaran Gultnieks
Version: 0.01
Author URI: http://ciarang.com
Revision history
0.01 - 2010-12-04: Initial development version
*/
class FDroid
{
// Our text domain, for internationalisation
var $textdom='wp-fdroid';
// Constructor
function FDroid() {
// Add filters etc here!
add_shortcode('fdroidrepo',array($this, 'do_shortcode'));
add_filter('query_vars',array($this, 'queryvars'));
$this->inited=false;
}
// Register additional query variables. (Handler for the 'query_vars' filter)
function queryvars($qvars) {
$qvars[]='fdfilter';
$qvars[]='fdid';
$qvars[]='fdpage';
return $qvars;
}
// Lazy initialise. All non-trivial members should call this before doing anything else.
function lazyinit() {
if(!$this->inited) {
load_plugin_textdomain($this->textdom, PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));
$this->inited=true;
}
}
// Gets a required query parameter by name.
function getrequiredparam($name) {
global $wp_query;
if(!isset($wp_query->query_vars[$name]))
wp_die("Missing parameter ".$name,"Error");
return $wp_query->query_vars[$name];
}
// Make a link to this page, with the given query parameter string added
function makelink($params) {
$link=get_permalink();
if(strlen($params)==0)
return $link;
if(strpos($link,'?')===false)
$link.='?';
else
$link.='&';
$link.=$params;
return $link;
}
// Handler for the 'fdroidrepo' shortcode.
// $attribs - shortcode attributes
// $content - optional content enclosed between the starting and ending shortcode
// Returns the generated content.
function do_shortcode($attribs,$content=null) {
global $wp_query,$wp_rewrite;
$this->lazyinit();
$page=1;
if(isset($wp_query->query_vars['fdpage'])) {
$page=(int)$wp_query->query_vars['fdpage'];
if($page==0)
$page=1;
}
$filter=null;
if(isset($wp_query->query_vars['fdfilter']))
$filter=$wp_query->query_vars['fdfilter'];
$out=$this->get_apps($page,$filter);
return $out;
}
function get_apps($page,$filter=null) {
if($filter===null)
$out="<p>All applications";
else
$out="<p>Applications matching ".$filter;
$out.="</p>";
$perpage=10;
$skipped=0;
$got=0;
$total=0;
$xml = simplexml_load_file("/home/fdroid/public_html/repo/index.xml");
foreach($xml->children() as $app) {
foreach($app->children() as $el) {
switch($el->getName()) {
case "name":
$name=$el;
break;
}
}
if($filter===null || stristr($name,$filter)) {
if($skipped<($page-1)*$perpage) {
$skipped++;
} else if($got<$perpage) {
$out.="<p>".$name."</p>";
$got++;
}
$total++;
}
}
$numpages=ceil((float)$total/$perpage);
$out.='<p>';
if($page==1) {
$out.="&lt;&lt;first ";
$out.="&lt;prev ";
} else {
$out.='<a href="'.$this->makelink("fdpage=1").'">&lt;&lt;first</a> ';
$out.='<a href="'.$this->makelink("fdpage=".($page-1)).'">&lt;&lt;prev</a> ';
}
$out.=" Page $page of $numpages ";
if($page==$numpages) {
$out.="next&gt; ";
$out.="last&gt;&gt; ";
} else {
$out.='<a href="'.$this->makelink("fdpage=".($page+1)).'">next&gt;</a> ';
$out.='<a href="'.$this->makelink("fdpage=".$numpages).'">last&gt;&gt;</a> ';
}
$out.='</p>';
return $out;
}
}
$wp_fdroid = new FDroid();
?>