diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3df1955ad..0ab1a6923 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -133,7 +133,7 @@ jobs: with: env_vars: PHP_VERSION,DB_TYPE token: ${{ secrets.CODECOV_TOKEN }} - fail_ci_if_error: true + fail_ci_if_error: false - name: Test app:clean-attachments run: php bin/console partdb:attachments:clean-unused -n diff --git a/src/DataTables/Filters/PartSearchFilter.php b/src/DataTables/Filters/PartSearchFilter.php index 9f6734e56..dfc0d9247 100644 --- a/src/DataTables/Filters/PartSearchFilter.php +++ b/src/DataTables/Filters/PartSearchFilter.php @@ -70,11 +70,16 @@ class PartSearchFilter implements FilterInterface /** @var bool Use Internal Part number for searching */ protected bool $ipn = true; + /** @var int Helper variable for hacky array_map variable injection */ + protected int $it = 0; + public function __construct( /** @var string The string to query for */ protected string $keyword ) { + // Transform keyword and trim excess spaces + $keyword = trim(str_replace('+', ' ', $keyword)); } protected function getFieldsToSearch(): array @@ -133,36 +138,56 @@ public function apply(QueryBuilder $queryBuilder): void if (($fields_to_search === [] && !$search_dbId) || $this->keyword === '') { return; } - + $expressions = []; + $params = []; - if($fields_to_search !== []) { - //Convert the fields to search to a list of expressions - $expressions = array_map(function (string $field): string { - if ($this->regex) { - return sprintf("REGEXP(%s, :search_query) = TRUE", $field); - } - - return sprintf("ILIKE(%s, :search_query) = TRUE", $field); - }, $fields_to_search); - - //For regex, we pass the query as is, for like we add % to the start and end as wildcards - if ($this->regex) { - $queryBuilder->setParameter('search_query', $this->keyword); - } else { - //Escape % and _ characters in the keyword - $this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword); - $queryBuilder->setParameter('search_query', '%' . $this->keyword . '%'); - } - } - //Use equal expression to just search for exact numeric matches if ($search_dbId) { $expressions[] = $queryBuilder->expr()->eq('part.id', ':id_exact'); - $queryBuilder->setParameter('id_exact', (int) $this->keyword, + $params[] = new \Doctrine\ORM\Query\Parameter('id_exact', (int) $this->keyword, ParameterType::INTEGER); } + if ($this->regex) { + //Convert the fields to search to a list of expressions + $expressions = array_map(function (string $field): string { + return sprintf("REGEXP(%s, :search_query) = TRUE", $field); + }, $fields_to_search); + + //For regex, we pass the query as is, save html special chars + $params[] = new \Doctrine\ORM\Query\Parameter('search_query', $this->keyword); + } else { + //Escape % and _ characters in the keyword + $this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword); + + //Split keyword on spaces, but limit token count + $tokens = explode(' ', $this->keyword, 5); + + //Perform search of every single token in every selected field + //AND-combine the results (all tokens must be present in any of the results) + for ($i = 0; $i < sizeof($tokens); $i++) { + $this->it = $i; + $tokens[$i] = trim($tokens[$i]); + + //Skip empty words (e.g. because of multiple spaces) + if ($tokens[$i] === '') { + continue; + } + //Convert the fields to search to a list of expressions + $expressions = array_map(function (string $field): string { + return sprintf("ILIKE(%s, :search_query%u) = TRUE", $field, $this->it); + }, $fields_to_search); + + //Aggregate the parameters for consolidated commission + $params[] = new \Doctrine\ORM\Query\Parameter('search_query' . $i, + '%' . $tokens[$i] . '%'); + } + } + + $queryBuilder->setParameters( + new \Doctrine\Common\Collections\ArrayCollection($params) + ); //Guard condition if (!empty($expressions)) { //Add Or concatenation of the expressions to our query @@ -337,5 +362,4 @@ public function setComment(bool $comment): PartSearchFilter return $this; } - } diff --git a/update.sh b/update.sh new file mode 100644 index 000000000..1e7e81f15 --- /dev/null +++ b/update.sh @@ -0,0 +1,164 @@ +##!/bin/bash +set -euo pipefail + +### constants ### +FILE="Part-DB-release-stage" +GITREPO="/opt/partdb/git-repo" +PRODENV="/var/www/Part-DB-server" +BACKUPDIR="/HDD5/backups/partdb" +WEB_AVAILABLE="/etc/apache2/sites-available" +WEB_ENABLED="/etc/apache2/sites-enabled" +WEBSERVICE="apache2" +LIVE_CONF="01_partdb.conf" +TEST_CONF="06_partdb_testing.conf" +MNT_CONF="07_partdb_maintenance.conf" + +# We should probably do a sanity check here? +DBUSER="$1" +DBPASSWD="$2" + +### variables ### +stage="1" + +echo "" +echo "*** Part-DB upgrade tool v0.0001 (WorksForMe* edition) ***" + + +if [ $(whoami) != 'root' ]; then + echo "" + echo "This script must be run as root!" + exit -1 +fi + +if [ -f $FILE ]; then + stage=$(<$FILE) +fi + +if [ "$stage" = "1" ]; then # no update currently in progress + cd $GITREPO + git fetch --tags + # get latest version + gitver=$(git describe --tags --abbrev=0) + currentver=$( $FILE + else + echo "* Invalid stage: $stage, expected 1" + exit 1 + fi + ;; + "2") + if [ "$stage" = "2" ]; then + echo "* Stage 2: Dump DB and update Part-DB via git" + # cd into working dir + cd $GITREPO + git fetch --tags + # get latest version + version=$(git describe --tags --abbrev=0) + # dump DB, preventing overwrite by re-execution if e.g. the migration broke the database structure + mysqldump -u$DBUSER -p$DBPASSWD partdb > $BACKUPDIR/partdb_before_update_$version_$(date -Iseconds).sql + # pull changes, checkout latest tag + git pull && git checkout $version + # copy config and media files and correct ownership + cp "$PRODENV/.env.local" $GITREPO + cp -rn $PRODENV/public/media/ $GITREPO/public/ + chown -R www-data:www-data $GITREPO + # merge .env with .env.local, config/services.yaml, config/parameters.yaml if changed + # TODO how to handle customizations ??? meld ??? + echo "* Files are in place, build step pending" + echo "3" > $FILE + else + echo "* Invalid stage: $stage, expected 2" + exit 2 + fi + ;; + "3") + if [ "$stage" = "3" ] ; then + echo "* Stage 3: Build process" + # build steps + cd $GITREPO + environment=$(sed -nr 's/APP_ENV=(.*)/\1/p' .env.local) + if [ environment != "dev" ]; then + environment="no-dev" + fi + sudo -u www-data composer install --$environment -o + yarn install + yarn build + # check if installation succeeded and migrate db + sudo -u www-data php bin/console partdb:check-requirements + sudo -u www-data php bin/console doctrine:migrations:migrate + sudo -u www-data php bin/console cache:clear + # we can mess with the production db because we have a very recent backup + rsync -av --exclude=$GITREPO/.git* $GITREPO/ $PRODENV-test + + echo "* The new Part-DB version can now be tested. You may need to merge .env.local with .env and check yaml files in config/." + echo "4" > $FILE + else + echo "* Invalid stage: $stage, expected 3" + exit 3 + fi + ;; + "4") + if [ "$stage" = "4" ]; then + echo "Stage 4: Put Part-DB back in production mode, retaining the old copy" + # copy all to prod environment + mv $PRODENV $PRODENV-old + mv $PRODENV-test $PRODENV + # remove link to maintenance PartDB VHost + rm $WEB_ENABLED/$LIVE_CONF $WEB_ENABLED/$TEST_CONF + # link the new partdb version + ln -sf $WEB_AVAILABLE/$LIVE_CONF $WEB_ENABLED/$LIVE_CONF + # reload apache + if ! [ `systemctl reload $WEBSERVICE && systemctl is-active --quiet $WEBSERVICE` ]; then + echo "* Webserver restart failed! Please check your $WEBSERVICE site configurations." + break + fi + echo "" + echo "*** Done. ***" + rm $FILE + else + echo "* Invalid stage: $stage, expected 4" + exit 4 + fi + ;; + esac + if [ -f $FILE ]; then + stage=$(<$FILE) + else + stage="1" + fi + else + echo "Update process aborted before stage $curstage." + break + fi +done +exit 0