1<?php
2/**
3 * Pagelist Plugin: lists pages
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <wikidesign@gmail.com>
7 */
8
9class syntax_plugin_pagelist extends DokuWiki_Syntax_Plugin {
10
11    function getType() { return 'substition';}
12    function getPType() { return 'block';}
13    function getSort() { return 168; }
14
15    /**
16     * Connect pattern to lexer
17     */
18    function connectTo($mode) {
19        $this->Lexer->addSpecialPattern('<pagelist.+?</pagelist>', $mode, 'plugin_pagelist');
20    }
21
22    /**
23     * Handle the match
24     */
25    function handle($match, $state, $pos, Doku_Handler $handler) {
26        global $ID;
27
28        $match = substr($match, 9, -11);  // strip markup
29        list($flags, $match) = explode('>', $match, 2);
30        $flags = explode('&', substr($flags, 1));
31        $items = explode('*', $match);
32
33        $pages = array();
34        $c = count($items);
35        for ($i = 0; $i < $c; $i++) {
36            if (!preg_match('/\[\[(.+?)\]\]/', $items[$i], $match)) continue;
37            list($id, $title, $description) = explode('|', $match[1], 3);
38            list($id, $section) = explode('#', $id, 2);
39            if (!$id) $id = $ID;
40            resolve_pageid(getNS($ID), $id, $exists);
41
42            // page has an image title
43            if (($title) && (preg_match('/\{\{(.+?)\}\}/', $title, $match))) {
44                list($image, $title) = explode('|', $match[1], 2);
45                list($ext, $mime) = mimetype($image);
46                if (!substr($mime, 0, 5) == 'image') $image = '';
47                $pages[] = array(
48                        'id'          => $id,
49                        'section'     => cleanID($section),
50                        'title'       => trim($title),
51                        'titleimage'  => trim($image),
52                        'description' => trim($description), // Holds the added parameter for own descriptions
53                        'exists'      => $exists,
54                        );
55
56            // text title (if any)
57            } else {
58                $pages[] = array(
59                        'id'          => $id,
60                        'section'     => cleanID($section),
61                        'title'       => trim($title),
62                        'description' => trim($description), // Holds the added parameter for own descriptions
63                        'exists'      => $exists,
64                        );
65            }
66        }
67        return array($flags, $pages);
68    }
69
70    /**
71     * Create output
72     */
73    function render($mode, Doku_Renderer $renderer, $data) {
74        list($flags, $pages) = $data;
75
76        // for XHTML output
77        if ($mode == 'xhtml') {
78            if (!$my =& plugin_load('helper', 'pagelist')) return false;
79            $my->setFlags($flags);
80            $my->startList();
81
82            if($my->sort || $my->rsort) {		// pages should be sorted by pagename
83            	$keys = array();
84            	$fnc = create_function('$a, $b', 'return strcmp(noNS($a["id"]), noNS($b["id"])); ');
85            	usort($pages, $fnc);
86            	// rsort is true - revserse sort the pages
87            	if($my->rsort) krsort($pages);
88            }
89
90            foreach($pages as $page) {
91                $my->addPage($page);
92            }
93            $renderer->doc .= $my->finishList();
94            return true;
95
96        // for metadata renderer
97        } elseif ($mode == 'metadata') {
98            foreach ($pages as $page) {
99                $renderer->meta['relation']['references'][$page['id']] = $page['exists'];
100            }
101            return true;
102        }
103        return false;
104    }
105}
106// vim:ts=4:sw=4:et:
107