-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_author.php
114 lines (82 loc) · 2.27 KB
/
api_author.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
// author names
require_once (dirname(__FILE__) . '/couchsimple.php');
require_once (dirname(__FILE__) . '/lib.php');
require_once (dirname(__FILE__) . '/api_utils.php');
//--------------------------------------------------------------------------------------------------
function default_display()
{
echo "hi";
}
//--------------------------------------------------------------------------------------------------
// Authors with same last name and similar first names
function display_author_lastname_prefix($lastname, $firstname, $callback = '')
{
global $config;
global $couch;
$first_letter = mb_substr($firstname, 0, 1);
$startkey = array($lastname, $first_letter);
$endkey = array($lastname, $first_letter . mb_convert_encoding('￰', 'UTF-8', 'HTML-ENTITIES'));
//$endkey = array($lastname, $first_letter . 'zzz');
$url = '_design/author/_view/lastname_firstname?startkey=' . json_encode($startkey) . '&endkey=' . json_encode($endkey) . '&group_level=2';
if ($config['stale'])
{
$url .= '&stale=ok';
}
$resp = $couch->send("GET", "/" . $config['couchdb_options']['database'] . "/" . $url);
$response_obj = json_decode($resp);
$obj = new stdclass;
$obj->status = 404;
$obj->url = $url;
if (isset($response_obj->error))
{
$obj->error = $response_obj->error;
}
else
{
if (count($response_obj->rows) == 0)
{
$obj->error = 'Not found';
}
else
{
$obj->status = 200;
$obj->results = array();
foreach ($response_obj->rows as $row)
{
$author = new stdclass;
$author->firstname = $row->key[1];
$author->lastname = $row->key[0];
$author->name = $author->firstname . ' ' . $author->lastname;
$obj->results[] = $author;
}
}
}
api_output($obj, $callback);
}
//--------------------------------------------------------------------------------------------------
function main()
{
$callback = '';
$handled = false;
// If no query parameters
if (count($_GET) == 0)
{
default_display();
exit(0);
}
if (isset($_GET['callback']))
{
$callback = $_GET['callback'];
}
if (!$handled)
{
if (isset($_GET['lastname']) && isset($_GET['firstname']))
{
display_author_lastname_prefix($_GET['lastname'], $_GET['firstname'], $callback);
$handled = true;
}
}
}
main();
?>