1<?php
2/**
3 * DokuWiki Information about a page in JSON format
4 *
5 * @license		GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author		Sascha Leib <sascha (dot) leib (at) kolmio (dot) com>
7 */
8
9header('Content-Type: application/json');
10header('Access-Control-Allow-Origin: *');
11
12// Turn off all error reporting
13//error_reporting(0);
14
15/* connect to DokuWiki: */
16if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
17if (!defined('DOKU_INC')) { define('DOKU_INC', __DIR__ . '/../../../../'); }
18require_once(DOKU_INC . 'inc/init.php');
19
20/* get the output style (can be 'preview' or 'all') */
21$style = ( array_key_exists('v', $_GET) ? strtolower($_GET['v']) : 'all' );
22if ($style !== 'preview') { $style = 'all'; }
23
24/* initialize the storage: */
25$result = [
26	'type'	=> 'error'
27];
28
29/* find the page ID */
30$id = $_GET['id'];
31
32if ($id !== null) {
33
34	/* get all metadata; */
35	$meta = p_get_metadata($id);
36
37	if (array_key_exists('title', $meta) && $meta['title'] !== null) {
38
39		if ($style == 'preview') {
40			$result['type'] = 'preview';
41		} else {
42			$result['type'] = 'standard';
43			$result['pageid'] = $id;
44			$result['lang'] = $conf['lang'];
45		}
46
47		$result['title'] = $meta['title'];
48
49		/* The page URL(s) */
50		$url = wl($id);
51
52		if ($style == 'preview') {
53			$result['content_urls'] = [
54				'desktop' => [
55					'page' => wl($id)
56				]
57			];
58		} else {
59			$url = $conf['baseurl'] . wl($id);
60			$set = [
61				'page' => $url,
62				'revisions' => $url . '?do=revisions',
63				'edit' => $url . '?do=edit'
64			];
65			$result['content_urls'] = [
66				'desktop' => $set,
67				'mobile' => $set
68			];
69		}
70
71		/* extract the first paragraph:*/
72		$parts = explode("\n", $meta['description']['abstract']);
73		$result['extract'] = ( count($parts) > 2 ? $parts[2] : '' );
74		$result['extract_html'] = '<p>'. ( count($parts) > 2 ? $parts[2] : '' ) .'</p>';
75
76	} else {
77		/* page does not exist */
78		$result['extract'] = 'Error: page does not exist.';
79		$result['extract_html'] = '<p><strong>' . $result['extract'] . '</strong></p>';
80	}
81}
82
83/* output the result: */
84echo json_encode($result);