more code

This commit is contained in:
Ste Vaidis 2024-02-19 12:25:53 +02:00
parent 84ae2f07e2
commit e4a5ea0f99
10 changed files with 265 additions and 126 deletions

View File

@ -10,9 +10,35 @@ MODULES="modules/custom"
[ ! -d ${SITE} ] && echo "Directory ${SITE} does not exist" && exit
[ -d ${SITE}/web ] && SITE=${SITE}/web
echo -en " 🌈 Disable module ${B}bundle_${NODE}${E}"
cd /var/www/sites/${SITE}
../vendor/drush/drush/drush pmu "bundle_${NODE}"
echo
# if module does not exist
../vendor/drush/drush/drush pml | grep "(endpoint_get_${NODE})"
if [ $? -ne 0 ];then
echo -n " 🌈 Cannot found module ${B}endpoint_get_${NODE}${E}"
exit
fi
echo -e " 🍓 Found module ${B}endpoint_get_${NODE}${E}"
# if is enabled, disable it
../vendor/drush/drush/drush pml | grep -e "Enabled" | grep "(endpoint_get_${NODE})" &> /dev/null
if [ $? -eq 0 ];then
echo -n " 🍓 Disabling module ${B}endpoint_get_${NODE}${E}"
../vendor/drush/drush/drush pmu "endpoint_get_${NODE}"
../vendor/drush/drush/drush pml | grep "(endpoint_get_${NODE})"
fi
# if is not disabled, dont delete it
../vendor/drush/drush/drush pml | grep -e "Disabled" | grep "(endpoint_get_${NODE})" &> /dev/null
if [ $? -ne 0 ];then
echo -n " 🌈 Cannot deleting enabled module ${B}endpoint_get_${NODE}${E}"
exit
fi
# delete the damn thing
rm "/var/www/sites/${SITE}/modules/custom/endpoint_get_${NODE}" -rf
if [ $? -eq 0 ];then
echo -e " 🍓 Directory ${B}${SITE}/modules/custom/endpoint_get_${NODE}${E} deleted"
fi
echo -en " 📂 Delete directory ${B}${SITE}/modules/custom/bundle_${NODE}${E}"
rm "/var/www/sites/${SITE}/modules/custom/bundle_${NODE}" -rf

View File

@ -1,8 +1,5 @@
#!/bin/bash
SITE=${1}
NODE=${2}
G="\e[0;32m"
B="\e[0;34m"
W="\e[0;97m"
@ -12,10 +9,22 @@ SOURCE="/root/dev/endpoints"
BASE_DIR="/var/www/sites"
MODULES="modules/custom"
SITE=${1}
NODE=${2}
function quit() {
echo
echo -e "Syntax:"
echo -e "${W}./endpoint-get <drupal dir> <node type>${E}"
echo
echo -e "Example:"
echo -e "${W}./endpoint-get mysite4 events${E}"
echo
exit
}
[ "$#" -lt 2 ] && echo "Argument missing" && exit
[ ! -d ${SITE} ] && echo "Directory ${SITE} does not exist" && exit
[ "$#" -lt 2 ] && echo "Argument missing" && quit
[ ! -d ${SITE} ] && echo "Directory ${SITE} does not exist" && quit
[ -d ${SITE}/web ] && SITE=${SITE}/web
@ -32,9 +41,11 @@ echo
echo -en " 📂 Copy template module dir to ${B}${SITE}${E}"
cp -r "${SOURCE}/endpoint_get_AAAAA" "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
log $?
echo
cd "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
# Rename files
echo -en " 🍓 Rename ${B}endpoint_get_${NODE}.info.yml${E} file"
@ -46,7 +57,7 @@ mv "endpoint_get_AAAAA.routing.yml" "endpoint_get_${NODE}.routing.yml"
log $?
# Edit files
echo
echo -en " ⭐ Edit ${B}endpoint_get_${NODE}.info.yml${E} file"
sed -i "s/AAAAA/${NODE}/g" "endpoint_get_${NODE}.info.yml"
log $?
@ -66,29 +77,24 @@ log $?
echo -en " ⭐ Edit ${B}src/Controller/CategoryController.php${E} file"
sed -i "s/AAAAA/${NODE}/g" "src/Controller/CategoryController.php"
log $?
echo
# Filesystem
chown -R apache:apache "${BASE_DIR}/${SITE}/${MODULES}/endpoint_get_${NODE}"
exit
# Enable module
echo -e " 🌈 Enable module"
cd /var/www/sites/${SITE}
../vendor/drush/drush/drush en "endpoint_get_${NODE}"
# Test endpoint
BASE_URL="http://$(ls -l /var/www/ | grep ${SITE} | awk {'print $9'}).vm7"
API_URL="api/el"
NODE_URL="${BASE_URL}/${API_URL}/${NODE}"
NODES_URL="${BASE_URL}/${API_URL}/${NODE}s"
cat "endpoint-get-${NODE}.routing.yml" | grep path
# echo "BASE_URL :" ${BASE_URL}
# echo "API_URL :" ${API_URL}
# echo "NODE :" ${NODE}
# echo "NODE_URL :" ${NODE_URL}
# echo "NODEs_URL:" ${NODES_URL}
ENDPOINTS=(
api/en/${NODE}s
api/el/${NODE}s
@ -97,7 +103,18 @@ ENDPOINTS=(
for ENDPOINT in ${ENDPOINTS[@]}; do
ENDPOINT_STATUS=$(curl -sI --location --request GET $BASE_URL/$ENDPOINT?_format=json \
-b cookie.txt \
--header "Content-type: application/json" | grep HTTP | awk {'print $2 " " $3'})
echo -e " 🍒 GET $ENDPOINT: $ENDPOINT_STATUS"
done
--header "Content-type: application/json" | grep HTTP | awk {'print $2'})
RESPONSE=$(curl -s --location --request GET $BASE_URL/$ENDPOINT | head -c100)
echo -e "\n 🍒 GET $BASE_URL/$ENDPOINT $ENDPOINT_STATUS"
[ "${ENDPOINT_STATUS}" == "200" ] && curl -s --location --request GET $BASE_URL/$ENDPOINT | head -c75
echo
done
echo
echo
echo -e "Relmember to check the settings for:"
echo -e " - Permissions"
echo -e " - Translations"
echo -e " - Aliases"
echo

View File

@ -1,7 +1,8 @@
endpoint_get_AAAAA.node:
path: '/api/{lang}/AAAAAs/{alias}'
defaults:
_controller: 'Drupal\endpoint_get_AAAAA\Controller\NodeController::getNode'
_controller:
'Drupal\endpoint_get_AAAAA\Controller\NodeController::getNode'
methods: [GET]
requirements:
_access: 'TRUE'
@ -11,7 +12,8 @@ endpoint_get_AAAAA.node:
endpoint_get_AAAAA.nodes:
path: '/api/{lang}/AAAAAs'
defaults:
_controller: 'Drupal\endpoint_get_AAAAA\Controller\NodesController::getNodes'
_controller:
'Drupal\endpoint_get_AAAAA\Controller\NodesController::getNodes'
methods: [GET]
requirements:
_access: 'TRUE'
@ -21,7 +23,8 @@ endpoint_get_AAAAA.nodes:
# endpoint_get_AAAAA.categories:
# path: '/api/{lang}/AAAAAs/categories'
# defaults:
# _controller: 'Drupal\endpoint_get_AAAAA\Controller\CategoryController::getCategories'
# _controller:
# 'Drupal\endpoint_get_AAAAA\Controller\CategoryController::getCategories'
# methods: [GET]
# requirements:
# _access: 'TRUE'
@ -31,7 +34,8 @@ endpoint_get_AAAAA.nodes:
# endpoint_get_AAAAA.nodes_category:
# path: '/api/{lang}/AAAAAs/categories/{category}'
# defaults:
# _controller: 'Drupal\endpoint_get_AAAAA\Controller\NodesController::getNodes'
# _controller:
# 'Drupal\endpoint_get_AAAAA\Controller\NodesController::getNodes'
# methods: [GET]
# requirements:
# _access: 'TRUE'

View File

@ -1,6 +1,7 @@
<?php
namespace Drupal\bundle_article\Controller;
namespace Drupal\endpoint_get_AAAAA\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@ -14,19 +15,25 @@ class CategoryController extends ControllerBase {
private array $response = [];
private int $statusCode = self::HTTP_OK;
// MAIN
/*
*
* MAIN
*
*/
public function getCategories(Request $request, $lang) {
$this->response = [
'code' => $this->statusCode,
'lang' => $lang,
'categories' => $this->buildVocabulary($lang)];
'categories' => $this->buildVocabulary($lang)
];
return new JsonResponse($this->response, $this->statusCode);
}
// RESPONSE ARRAY
/*
*
* RESPONSE ARRAY
*
*/
private function buildVocabulary($lang): array {
$terms = $this->loadTerms($lang);
$terms_response = [];
@ -39,8 +46,11 @@ class CategoryController extends ControllerBase {
return $terms_response;
}
// RESPONSE ARRAY VALUES
/*
*
* RESPONSE ARRAY VALUES
*
*/
private function loadTerms($lang) {
// All terms from the vocabulary

View File

@ -1,15 +1,17 @@
<?php
namespace Drupal\bundle_article\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use Drupal\media\Plugin\media\Source;
use Drupal\media\Entity;
namespace Drupal\endpoint_get_AAAAA\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\TempStore;
use Drupal\Core\Url;
use Drupal\media\Plugin\media\Source;
use Drupal\media\Entity;
use Drupal\node\Entity\Node;
class NodeController extends ControllerBase {
@ -22,42 +24,55 @@ class NodeController extends ControllerBase {
private array $response = [];
private int $statusCode = self::HTTP_OK;
/* Main */
/*
*
* Main
*
*/
public function getNode(Request $request, $lang, $alias) {
/* check if alias exist in path */
/* Quit if alias not exist in path */
if (empty($alias)) {
$this->errorAliasMissing();
return new JsonResponse($this->response, $this->statusCode);
}
/* check if alias exist in drupal */
/* Get unaliased path */
$path_alias = '/'.$alias;
$path = \Drupal::service('path_alias.manager')->getPathByAlias($path_alias);
if ( $path_alias == $path) {
$this->errorNodeNotExist($alias, $path);
$path = \Drupal::service('path_alias.repository')->lookupByAlias($path_alias, $lang);
/* Quit if path does not exist in drupal */
if (is_null($path)) {
$this->errorNodeNotExist($alias);
return new JsonResponse($this->response, $this->statusCode);
}
/* check if node has translation */
$nid = Url::fromUri('internal:' . $path_alias)->getRouteParameters()['node'];
/* Get the nid from unaliased path */
$path = $path['path'];
$nid = explode('/',$path)[2];
/* Get the node */
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
/* Quit if node has no tranlation */
if (! $node->hasTranslation($lang)) {
$this->errorTranslationNotExist($lang, $alias);
return new JsonResponse($this->response, $this->statusCode);
}
/* build response */
$this->buildNodeResponse($lang, $alias);
/* Build the response */
$this->buildNodeResponse($lang, $alias, $nid)
return new JsonResponse($this->response, $this->statusCode);
}
/* Response */
/*
*
* Node Response
*
*/
private function buildNodeResponse($lang, $alias) {
$path = \Drupal::service('path_alias.manager')->getPathByAlias($alias);
$nid = Url::fromUri('internal:/' . $path)->getRouteParameters()['node'];
private function buildNodeResponse($lang, $alias, $nid) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid)->getTranslation($lang);
/* user name */
@ -66,26 +81,32 @@ class NodeController extends ControllerBase {
$name = $user->getDisplayName();
/* absolute urls for body inline images */
$base_url = Url::fromRoute('<current>')->setAbsolute()->toString();
$url_components = parse_url($base_url);
$domain = $url_components['scheme'].'://'.$url_components['host'].':'.$url_components['port'];
$body = str_replace(
'/sites/default/',
$domain.'/sites/default/',
$node->get('body')->value
);
// $base_url = Url::fromRoute('<current>')->setAbsolute()->toString();
// $url_components = parse_url($base_url);
// $domain = $url_components['scheme'].'://'.$url_components['host'].':'.$url_components['port'];
// $body = str_replace(
// '/sites/default/',
// $domain.'/sites/default/',
// $node->get('body')->value
// );
// $manager = '';
// if ($node->hasField('manager') && !$node->get('manager')->isEmpty()) {
// $manager = $node->get('manager')->value;
// }
$this->response = [
'code' => $this->statusCode,
'alias' => $alias,
'path' => $path,
// 'path' => $path,
'nid' => $nid,
'title' => $node->get('title')->value,
'body' => $body,
'lang' => $node->get('langcode')->value,
'alias' => $node->get('path')->alias,
'created' => $node->get('created')->value,
'author' => $name,
'title' => $node->get('title')->value,
// 'manager' => $manager,
// 'body' => $node->get('body')->value,
// 'body' => $body,
// 'lang' => $node->get('langcode')->value,
// 'alias' => $node->get('path')->alias,
// 'files' => $this->getFiles($node, 'field_file'),
// 'images' => $this->getImages($node, 'field_image'),
// 'category' => (
@ -97,11 +118,16 @@ class NodeController extends ControllerBase {
// $node->currentTranslation
// )->getName():'',
// 'subcategory' => $this->getTerms($node, self::FIELD_SUBCATEGORY)
'created' => $node->get('created')->value
];
}
/* Multiple value fields */
/*
*
* Multiple value fields
*
*/
// private function getTerms(Node $node, string $field): array {
// $terms = $node->get($field)->referencedEntities();
@ -143,13 +169,17 @@ class NodeController extends ControllerBase {
// return $uris;
// }
/* Error functions */
/*
*
* Error functions
*
*/
private function errorNodeNotExist($alias, $path) {
private function errorNodeNotExist($alias) {
$this->statusCode = self::HTTP_NOT_FOUND;
$this->response = [
'code' => $this->statusCode,
"message" => "Node with alias " . $alias . " does not exist." . $path
"message" => "Node with alias " . $alias . " does not exist."
];
}

View File

@ -22,24 +22,26 @@ class NodesController extends ControllerBase {
* Url : optional URL argument - single value
* Example : /api/en/articles/1234
*/
// private const FIELD_CATEGORY = 'field_AAAAA_category'; // Editable
private const FIELD_CATEGORY = 'field_AAAAA_category'; // Editable
/* Used for : Taxonomy reference field
* Value : multiple ID's of field_article_subcategory values
* Url : optional URL query params - accept multiple values
* Example : /api/en/articles/1234?subcategory=55,66,77
*/
// private const FIELD_SUBCATEGORY = 'field_AAAAA_subcategory'; // Editable
private const FIELD_SUBCATEGORY = 'field_AAAAA_subcategory'; // Editable
private const HTTP_BAD_REQUEST = 400;
private const HTTP_NOT_FOUND = 404;
private const HTTP_OK = 200;
private array $response = [];
private int $statusCode = self::HTTP_OK;
/* Main */
/*
*
* Main
*
*/
public function getNodes(Request $request, $lang, $category = 'all') {
$nodes = $this->loadNodes($request, $lang, $category);
$nodes_response = $this->buildNodes($nodes, $lang, $category);
@ -52,8 +54,11 @@ class NodesController extends ControllerBase {
return new JsonResponse($this->response, $this->statusCode);
}
/* Response List */
/*
*
* Response List
*
*/
private function buildNodes(array $nodes, $lang): array {
$nodes_response = [];
foreach ($nodes as $node) {
@ -107,10 +112,9 @@ class NodesController extends ControllerBase {
if ($published == 1) {
$nodes_response[] = [
'id' => $node->id(),
'id' => intval($node->id()),
'title' => $node->getTitle(),
'lang' => $node->get('langcode')->value,
'created' => $node->get('created')->value,
'alias' => $node->get('path')->alias,
'author' => $name,
// 'field_geolocation' => $field_geolocation,
@ -135,6 +139,7 @@ class NodesController extends ControllerBase {
// $node->currentTranslation
// )->getName():'',
// 'subcategory' => $this->getTerms($node, self::FIELD_SUBCATEGORY)
'created' => $node->get('created')->value
];
}
}
@ -142,8 +147,11 @@ class NodesController extends ControllerBase {
return $nodes_response;
}
/* Multivalue fields */
/*
*
* Multivalue fields
*
*/
// private function getTerms(Node $node, string $field): array {
// $terms = $node->get($field)->referencedEntities();
// $response = [];
@ -184,22 +192,25 @@ class NodesController extends ControllerBase {
// return $uris;
// }
/* get response data */
/*
*
* get response nodes
*
*/
private function loadNodes($request, $lang, $category) {
/* build query for nodes */
/* build the query for nodes */
$query = \Drupal::entityTypeManager()
->getStorage('node')
->getQuery()
->accessCheck(false);
/* category is a URL parameter, matched with a taxonomy field */
/* if category exist as URL parameter, create a query condition */
if ($category != 'all'){
$query->condition(self::FIELD_CATEGORY, $category);
}
/* add subcategory URL query filter, matched with a taxonomy field */
/* if subcategory exist as URL query, create the query condition */
if ($request->query->get('subcategory')){
$or_group = $query->orConditionGroup();
$terms = explode(',', $request->query->get('subcategory'));
@ -259,7 +270,6 @@ class NodesController extends ControllerBase {
$node->currentTranslation = $lang;
}
return $nodeList;
}
// \Drupal::logger('Bundle AAAAA')->notice(''.$some_var);
}

24
endpoints/info-fields.sh Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
IFS=$'\n'
DIR=$1
TYPE=$2
NAME=$3
cd $DIR
for FIELD in $(vendor/drush/drush/drush fi ${TYPE} ${NAME} | tail -n +3 | grep -v '\-\-\-'); do
NAME=$(echo $FIELD | awk '{print $1}')
SECOND=$(echo $FIELD | awk '{print $2}')
THIRD=$(echo $FIELD | awk '{print $3}')
COUNT=$(echo $SECOND | wc -c)
if [ "$COUNT" -gt 5 ]; then
TYPE=$SECOND
else
TYPE=$THIRD
fi
echo "$NAME $TYPE"
done

View File

@ -79,18 +79,24 @@ function check_path() {
# Create tarball
#
function create_tarball() {
log "Create backup-${DIR}.tar.gz"
tar -czpf "backup-${DIR}.tar.gz" "${DIR}" "${DIR}.sql"
log "Create ${DIR}-backup.tar.gz"
tar -czpf "${DIR}-backup.tar.gz" "${DIR}" "${DIR}.sql"
[ $? -ne 0 ] && return 1
return 0
}
#
# Create checksum
#
function create_checksum() {
log "Create backup-${DIR}-md5sum checksum"
CHECKSUM=$(md5sum backup-${DIR}.tar.gz | awk '{print $1}' )
echo ${CHECKSUM} > "backup-${DIR}.md5sum"
log "Create ${DIR}-backup.md5sum checksum"
CHECKSUM=$(md5sum ${DIR}-backup.tar.gz | awk '{print $1}' )
echo ${CHECKSUM} > "${DIR}-backup.md5sum"
}
#
# Start
#
function main () {
if check_path; then
if get_db_credentials; then
@ -110,5 +116,13 @@ function main () {
main
ls -lh backup-${DIR}.tar.gz --color=auto
ls -lh backup-${DIR}.md5sum --color=auto
#
# Show results
#
echo
ls -lh ${DIR}-backup.tar.gz --color=auto
ls -lh ${DIR}-backup.md5sum --color=auto
echo
echo -e " MD5 checksum: $(cat ${DIR}-backup.md5sum)"
echo

View File

@ -2,12 +2,13 @@
df -h | awk 'NR==1 || /\/$/'
rm -rf $1
rm -r $1.sql
rm -r $1.info
rm -r $1.copy.log
rm -r $1.composer.log
rm -r $1.settings.php
[ -d "${1}" ] && ls -1 | grep -E "^${1}$" | xargs rm -rf
[ -f $1.sql ] && rm -r $1.sql
[ -f $1.info ] && rm -r $1.info
[ -f $1.copy.log ] && rm -r $1.copy.log
[ -f $1.composer.log ] && rm -r $1.composer.log
[ -f $1.settings.log ] && rm -r $1.settings.log
./db -du $1
./db -dd $1

View File

@ -33,10 +33,13 @@ function fail() {
echo
echo -ne " 💧 Create new drupal in dir ${WHT}$NAME${END} ..."
composer --no-interaction create-project drupal/recommended-project $NAME
# CMD="composer --no-interaction create-project drupal/recommended-project $NAME"
# sudo su -l apache -s /bin/bash -c "$CMD"
# sudo su -l apache -s /bin/bash -c "cd $PWD; $CMD"
# composer --no-interaction create-project drupal/recommended-project:9.5.10 $NAME > /dev/null 2>&1
#CMD="composer --no-interaction create-project drupal/recommended-project:9 $NAME > /dev/null 2>&1"
#CMD="composer create-project drupal/recommended-project:9 $NAME"
#sudo su -l apache -s /bin/bash -c "cd $PWD; $CMD"
if [ $? = 0 ]; then ok; else fail; fi
echo -ne " 🐬 Create database ${WHT}$NAME${END} ..."
@ -55,8 +58,9 @@ echo -ne " 🐬 Flush privilages ..."
mysql -uroot -p1234 -e "FLUSH PRIVILEGES"
if [ $? = 0 ]; then ok; else fail; fi
# echo -ne " 🍒 Drupal settings.php"
# cd $NAME/web
echo -ne " 🍒 Drupal settings.php"
cd $NAME/web
# cp sites/default/default.settings.php sites/default/settings.php
# cat >> sites/default/settings.php <<EOL
# \$databases['default']['default'] = array (
@ -70,12 +74,10 @@ if [ $? = 0 ]; then ok; else fail; fi
# 'driver' => 'mysql',
# 'autoload' => 'core/modules/mysql/src/Driver/Database/mysql/',
# );
# EOL
# if [ $? = 0 ]; then ok; else fail; fi
echo -ne " 🐷 Change files owner to user ${WHT}apache${END}"
cd $NAME/web
mkdir sites/default/files
cd ../..
chown apache:apache $NAME -R
@ -83,13 +85,15 @@ find $NAME -type d -exec chmod 755 {} \;
find $NAME -type f -exec chmod 644 {} \;
if [ $? = 0 ]; then ok; else fail; fi
#echo -ne " 🍒 link html to ${WHT}$NAME${END}"
#unlink /var/www/html
#ln -s /var/www/sites/$NAME/web /var/www/html
#if [ $? = 0 ]; then ok; else fail; fi
echo
ls -l --color=auto /var/www/site[1234]
ls -l --color=auto /var/www/site[1234567890]
echo
# echo -ne " 🌈 Restart httpd "
@ -113,7 +117,6 @@ echo -e "Password: 1234"
echo
read -p "...and press [ENTER] to continue installing extra modules"
echo
exit
echo -e "🍓"
@ -152,9 +155,9 @@ echo
#composer require 'drupal/rest_entity_recursive:^2.0@RC'
# Devel
composer require 'drupal/devel'
rm -rf vendor/autoload.php vendor/autoload_runtime.php vendor/composer
composer install
drush cr
drush en devel_generate
# composer require 'drupal/devel'
# rm -rf vendor/autoload.php vendor/autoload_runtime.php vendor/composer
# composer install
# drush cr
# drush en devel_generate