1<?php
2/**
3 * DokuWiki Syntax Plugin LastFm
4 *
5 * Shows various statistics from the last.fm service for a given user.
6 *
7 * Syntax:  {{lastfm>[username]?[keyword] [keyword] [keyword]}}
8 *
9 *   [username] - a valid last.fm username
10 *   [keyword]  - a space separated list of the following keywords:
11 *                 - topartists
12 *                 - topalbums
13 *                 - toptracks
14 *                 - tags
15 *                 - friends
16 *                 - neighbours
17 *                 - recenttracks
18 *                 - artistchart
19 *                 - albumchart
20 *                 - trackchart
21 *                 - L=n (limit number of records)
22 *                 - C=n (limit numer of columns)
23 *                 - IMGONLY (show only images in topalbums)
24 *
25 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
26 * @author  Michael Klier <chi@chimeric.de>
27 */
28// must be run within DokuWiki
29if(!defined('DOKU_INC')) die();
30
31if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
32if(!defined('DW_LF')) define('DW_LF',"\n");
33require_once(DOKU_PLUGIN.'syntax.php');
34
35/**
36 * All DokuWiki plugins to extend the parser/rendering mechanism
37 * need to inherit from this class
38 */
39class syntax_plugin_lastfm extends DokuWiki_Syntax_Plugin {
40
41    // UTC Offset
42    var $_utc_offset;
43
44    // Date format
45    var $_dformat;
46
47    /**
48     * General Info
49     */
50    function getInfo(){
51        return array(
52            'author' => 'Michael Klier (chi)',
53            'email'  => 'chi@chimeric.de',
54            'date'   => @file_get_contents(DOKU_PLUGIN.'lastfm/VERSION'),
55            'name'   => 'LastFM Plugin (syntax component)',
56            'desc'   => 'Displays lastfm statistics for a given user',
57            'url'    => 'http://dokuwiki.org/plugin:lastfm'
58        );
59    }
60
61    /**
62     * Syntax Type
63     *
64     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
65     */
66    function getType()  { return 'substition'; }
67    function getPType() { return 'block'; }
68    function getSort()  { return 312; }
69
70    /**
71     * Connect pattern to lexer
72     */
73    function connectTo($mode) {
74        $this->Lexer->addSpecialPattern('{{lastfm>[a-zA-Z0-9.\-_]*\?.*?}}',$mode,'plugin_lastfm');
75    }
76
77    /**
78     * Handler to prepare matched data for the rendering process
79     */
80    function handle($match, $state, $pos, &$handler){
81
82        $data   = array();
83        $charts = array('topartists', 'topalbums', 'toptracks', 'tags', 'friends',
84                        'neighbours', 'recenttracks', 'artistchart', 'albumchart',
85                        'trackchart', 'profile');
86
87        $match = substr($match,9,-2);
88
89        list($user,$params) = explode('?',$match);
90
91        $data['user'] = $user;
92        $params = explode(' ', $params);
93
94        $data['charts']  = array();
95        $data['limit']   = 10;
96        $data['cols']    = 5;
97        $data['imgonly'] = 0;
98
99        foreach($params as $param) {
100            if(in_array($param, $charts)) {
101                if($param == 'artistchart' || $param == 'albumchart' || $param == 'trackchart') {
102                    array_push($data['charts'], 'weekly'.$param);
103                } else {
104                    array_push($data['charts'], $param);
105                }
106            } else {
107                if(@preg_match('/\bL=([0-9]{1,2})\b/', $param, $match)) $data['limit'] = $match[1];
108                elseif(@preg_match('/\bC=([0-9]{1})\b/', $param, $match)) $data['cols'] = $match[1];
109                elseif(@preg_match('/\bIMGONLY\b/', $param, $match)) $data['imgonly'] = 1;
110            }
111        }
112
113        return ($data);
114    }
115
116    /**
117     * Handles the actual output creation.
118     */
119    function render($mode, &$renderer, $data) {
120        global $ID;
121        global $lang;
122
123        if($mode == 'xhtml'){
124            // disable caching
125            $renderer->info['cache'] = false;
126
127            $renderer->doc .= '<div class="plugin_lastfm">' . DW_LF;
128
129            $renderer->doc .= '  <ul class="plugin_lastfm_opts">' . DOKU_LF;
130            $renderer->doc .= '     <li class="plugin_lastfm_opt"><span class="plugin_lastfm_user">' . $data['user'] . '</span></li>' . DOKU_LF;
131            $renderer->doc .= '     <li class="plugin_lastfm_opt"><span class="plugin_lastfm_limit">' . $data['limit'] . '</span></li>' . DOKU_LF;
132            $renderer->doc .= '     <li class="plugin_lastfm_opt"><span class="plugin_lastfm_cols">' . $data['cols'] . '</span></li>' . DOKU_LF;
133            $renderer->doc .= '     <li class="plugin_lastfm_opt"><span class="plugin_lastfm_imgonly">' . $data['imgonly'] . '</span></li>' . DOKU_LF;
134            $renderer->doc .= '  </ul>'. DOKU_LF;
135
136            foreach($data['charts'] as $chart) {
137                $renderer->doc .= '  <span class="plugin_lastfm_charttype">last.fm ' . $this->getLang($chart) . '</span>' . DW_LF;
138                $renderer->doc .= '  <div class="plugin_lastfm_chart" id="plugin_lastfm_' . $chart . '"></div>' . DW_LF;
139            }
140
141            $renderer->doc .= '</div>' . DW_LF;
142
143            return true;
144        }
145        return false;
146    }
147}
148// vim:ts=4:sw=4:et:enc=utf-8:
149