| } | } | ||||
| } | } | ||||
| send_response($code, $type, $data); | |||||
| } | |||||
| sub send_response { | |||||
| my ($code, $type, $data) = @_; | |||||
| printf("Status: %s\r\n", $code); | printf("Status: %s\r\n", $code); | ||||
| printf("Content-type: %s\r\n", $type) if defined $type; | printf("Content-type: %s\r\n", $type) if defined $type; | ||||
| printf("Content-length: %i\r\n", length($data)) if defined $data; | printf("Content-length: %i\r\n", length($data)) if defined $data; | ||||
| print $data if defined $data; | print $data if defined $data; | ||||
| } | } | ||||
| sub fatal_api_error { | |||||
| my($code,$type,$body)=api_error(@_); | |||||
| send_response($code, $type, $body); | |||||
| exit; | |||||
| } | |||||
| sub load_conf { | sub load_conf { | ||||
| my($file) = @_; | my($file) = @_; | ||||
| } | } | ||||
| } | } | ||||
| my $out = { | |||||
| id => $document->{id}, | |||||
| pageId => [ @pageids ], | |||||
| name => $document->{name}, | |||||
| created => $document->{created}, | |||||
| owner => $document->{owner}, | |||||
| status => $document->{status}, | |||||
| languages => [ keys %lang ], | |||||
| }; | |||||
| if ( defined $document ) { | |||||
| my $out = { | |||||
| id => $document->{id}, | |||||
| pageId => [ @pageids ], | |||||
| name => $document->{name}, | |||||
| created => $document->{created}, | |||||
| owner => $document->{owner}, | |||||
| status => $document->{status}, | |||||
| languages => [ keys %lang ], | |||||
| }; | |||||
| return (200, "application/json", $out); | |||||
| } | |||||
| return (404, "application/json", ""); | |||||
| } | |||||
| sub db_get_page_object { | |||||
| my($id) = @_; | |||||
| return (200, "application/json", $out); | |||||
| my $out; | |||||
| my %lang; | |||||
| my @tags; | |||||
| my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id); | |||||
| while(my $hash = $q->fetchrow_hashref()) { | |||||
| $lang{$hash->{language}}++; | |||||
| } | |||||
| $q = sqlquery($dbh, " | |||||
| SELECT tags.tag AS tag | |||||
| FROM pages_tags LEFT JOIN tags ON pages_tags.tagId = tags.id | |||||
| WHERE pageId = ?i SORT BY tag", $id); | |||||
| while(my ($tag) = $q->fetchrow_array()) { push @tags, $tag; } | |||||
| $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id); | |||||
| while(my $hash = $q->fetchrow_hashref()) { | |||||
| $out = { | |||||
| id => $id, | |||||
| documentId => $hash->{documentId}, | |||||
| name => $hash->{name}, | |||||
| created => $hash->{created}, | |||||
| owner => $hash->{owner}, | |||||
| status => $hash->{status}, | |||||
| language => [ keys %lang ], | |||||
| tags => [ @tags ], | |||||
| }; | |||||
| } | |||||
| if ( exists $out->{id} ) { | |||||
| return (200, "application/json", $out); | |||||
| } | |||||
| return (404, "application/json", ""); | |||||
| } | } | ||||
| # create an empty document object. | # create an empty document object. | ||||
| return db_get_document_object($id); | return db_get_document_object($id); | ||||
| } | } | ||||
| sub api_v1_GET_pages_id { return api_error(501,"Not yet implemented"); } | |||||
| # get a specific page | |||||
| sub api_v1_GET_pages_id { | |||||
| my($id, $qs, $post, $user) = @_; | |||||
| return db_get_page_object($id); | |||||
| } | |||||
| # change document properties | # change document properties | ||||
| sub api_v1_PATCH_documents_id { | sub api_v1_PATCH_documents_id { | ||||
| return \@arr; | return \@arr; | ||||
| } | } | ||||
| sub fatal_api_error { | |||||
| my($code,$type,$body)=api_error(@_); | |||||
| printf("Status: %s\r\n", $code); | |||||
| printf("Content-type: %s\r\n", $type); | |||||
| printf("Content-length: %i\r\n", length($body)); | |||||
| printf("\r\n"); | |||||
| print $body; | |||||
| exit; | |||||
| } | |||||
| sub api_error { | sub api_error { | ||||
| my($code, $text)=@_; | my($code, $text)=@_; | ||||