Skip to content

Commit

Permalink
Code refactor, add languages support, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
awaluk committed Aug 29, 2020
1 parent ed95d0e commit c9e4835
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Arkadiusz Waluk
Copyright (c) Arkadiusz Waluk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# API plugin to [Question2Answer](http://question2answer.org/)
# API plugin to [Question2Answer](https://question2answer.org/)

Simple API plugin to serving data from Q2A script in JSON.
Creates simple API and serve some data as JSON.

Tested on **Q2A version >= 1.7.0 and PHP >= 7.0**. Code style adjusted to Q2A style.

## Installation

Clone or download this repository to *qa-plugin* directory in your Q2A.
Clone or download this repository or selected [release](https://github.com/awaluk/q2a-api/releases) to *qa-plugin* directory in your Q2A.

## Available functions

- **/api/favorites** - getting favorites for logged user: users, questions, tags and categories


---
## Available endpoints

**Important notice about code!**
This code don't respect PHP PSR-2 standard, because Question2Answer unfortunately too don't respect this. Also, I couldn't design classes and functions in my own way, because most of them impose Question2Answer script.
- **/api/favorites** - get favorites data for logged user: users, questions, tags and categories
```json
{
"questions": ["2", "1"],
"users": ["Example username"],
"tags": ["Example tag 1", "Example tag 2"],
"categories": ["Example category"]
}
```
5 changes: 5 additions & 0 deletions lang/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'user_not_logged' => 'User not logged'
];
5 changes: 5 additions & 0 deletions lang/pl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'user_not_logged' => 'Użytkownik niezalogowany'
];
13 changes: 7 additions & 6 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "Q2A API",
"uri": "https://github.com/awaluk/q2a-api",
"description": "Simple API serving data from Q2A in JSON",
"version": "0.1",
"date": "2017-09-01",
"description": "Creates simple API and serve some data as JSON",
"version": "0.1.0",
"date": "2020-08-29",
"author": "Arkadiusz Waluk",
"author_uri": "http://waluk.pl",
"author_uri": "https://waluk.pl",
"license": "MIT",
"min_q2a": "1.5",
"update_uri": "https://raw.githubusercontent.com/awaluk/q2a-api/master/metadata.json"
"update_uri": "https://raw.githubusercontent.com/awaluk/q2a-api/master/metadata.json",
"min_q2a": "1.7",
"min_php": "7.0"
}
35 changes: 17 additions & 18 deletions qa-plugin.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
<?php
/**
* Q2A API - plugin to Question2Answer
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
*/

/*
Plugin Name: Q2A API
Plugin URI: https://github.com/awaluk/q2a-api
Plugin Description: Simple API serving data from Q2A in JSON
Plugin Version: 0.1
Plugin Date: 2017-09-01
Plugin Author: Arkadiusz Waluk
Plugin Author URI: https://waluk.pl
Plugin License: MIT
Plugin Minimum Question2Answer Version: 1.5
Plugin Update Check URI: https://raw.githubusercontent.com/awaluk/q2a-api/master/metadata.json
Plugin Name: Q2A API
Plugin URI: https://github.com/awaluk/q2a-api
Plugin Description: Creates simple API and serve some data as JSON
Plugin Version: 0.1.0
Plugin Date: 2020-08-29
Plugin Author: Arkadiusz Waluk
Plugin Author URI: https://waluk.pl
Plugin License: MIT
Plugin Update Check URI: https://raw.githubusercontent.com/awaluk/q2a-api/master/metadata.json
Plugin Minimum Question2Answer Version: 1.7
Plugin Minimum PHP Version: 7.0
*/

if (!defined('QA_VERSION')) {
header('Location: ../../');
exit;
header('Location: ../../');
exit();
}

qa_register_plugin_module('page', 'api-favorites.php', 'api_favorites', 'API user favorites');
define('API_URL', 'api/');
qa_register_plugin_module('page', 'src/api-favorites.php', 'api_favorites', 'API favorites');
qa_register_plugin_phrases('lang/*.php', 'q2a_api');

require_once 'api-functions.php';
require_once 'src/api-functions.php';
8 changes: 2 additions & 6 deletions api-favorites.php → src/api-favorites.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?php
/**
* Q2A API - plugin to Question2Answer
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
*/

class api_favorites
{
public function match_request($request)
{
return $request === API_URL.'favorites';
return $request === API_URL . 'favorites';
}

public function process_request()
{
$user_id = qa_get_logged_in_userid();
if (empty($user_id)) {
return_json_response(['error' => 'User not logged']);
return_json_response(['error' => qa_lang('q2a_api/user_not_logged')]);
}

$db_questions = qa_db_select_with_pending(qa_db_user_favorite_qs_selectspec($user_id));
Expand Down
8 changes: 1 addition & 7 deletions api-functions.php → src/api-functions.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Q2A API - plugin to Question2Answer
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
*/

define('API_URL', 'api/');

function return_json_response($data, $code = 200)
{
Expand All @@ -13,4 +7,4 @@ function return_json_response($data, $code = 200)

echo json_encode($data);
exit();
}
}

0 comments on commit c9e4835

Please sign in to comment.