Skip to content

Instantly share code, notes, and snippets.

View jonasraoni's full-sized avatar
☠️
Do what you want cause a pirate is free! You are a pirate!

Jonas Raoni Soares da Silva jonasraoni

☠️
Do what you want cause a pirate is free! You are a pirate!
View GitHub Profile
@jonasraoni
jonasraoni / ArticleSearchDAO.php
Last active April 26, 2024 08:17
Handicapped search code for OJS 3.4
<?php
/**
* @file classes/search/ArticleSearchDAO.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ArticleSearchDAO
@jonasraoni
jonasraoni / I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
Last active May 8, 2023 13:30
Fixed lib\pkp\classes\migration\upgrade\v3_4_0\I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
<?php
/**
* @file classes/migration/upgrade/v3_4_0/I8073_RemoveNotesWithoutQueriesAndRelatedObjects.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I8073_RemoveNotesWithoutQueriesAndRelatedObjects
@jonasraoni
jonasraoni / example.php
Last active March 3, 2023 12:05
Performs an optimization at a SELECT statement which is intended to be enclosed by a counter query (e.g. "SELECT COUNT(0) FROM (YOUR_GENERIC_QUERY)") for pagination purposes
<?php
print_r(optimizeCountQuery(
'SELECT (SELECT drop FROM expensive WHERE true = ?) AS subquery
FROM table
WHERE (SELECT ?) = param
ORDER BY not_needed_for_count, drop = ?',
[true,'param', true]
));
/*
@jonasraoni
jonasraoni / convert-latin1-utf8.sql
Created July 21, 2021 13:39
Detect and convert bad encoded UTF-8 data in MySQL
-- Convert
SELECT CONVERT(CAST(CONVERT('São Paulo' USING latin1) AS BINARY) USING utf8) -- São Paulo
-- Our lovely friends celebrate ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö \o/
-- Poor man's bad encoding detection (in my case there was LATIN1 data mixed with UTF8 in the database and I just wanted to find when things started to get mixed up)
-- When trying to convert, normally the amount of characters decrease...
SELECT field
FROM table
WHERE LENGTH(CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8)) < LENGTH(field)
@jonasraoni
jonasraoni / remove-trailing-zeros.cs
Created June 28, 2021 11:15
Remove trailing zeros from a decimal value (C#)
var value = 0.1230000000000000M;
Console.WriteLine(value);
Console.WriteLine(value / 1.0000000000000000000000000000M);
@jonasraoni
jonasraoni / localized-compare.js
Created March 15, 2021 12:13
Compare texts using another locale in JavaScript
const comparer = new Intl.Collator('ru', { numeric: true }).compare;
console.log(comparer('ф', 'в')); //returns -1, 0, 1
@jonasraoni
jonasraoni / tootip-on-overflow-directive.js
Last active October 1, 2021 09:20
Vue directive: Tooltip on overflow for hidden text
export default class TooltipOnOverflowDirective {
static install (Vue) {
Vue.directive('tooltip-on-overflow', this.directive);
}
static directive = {
bind (el) {
for (const event of ['mouseover', 'mouseout']) {
el.addEventListener(event, TooltipOnOverflowDirective[event]);
}
@jonasraoni
jonasraoni / format-number.js
Created January 12, 2021 09:37
Format number Vue directive
export default {
install: Vue => Vue.filter('format-number', (value, { thousand = ' ', decimal = '.', decimals = null, maxDecimals = decimals, minDecimals = decimals, normalize = true, roundToEven = true } = {}) => {
value = normalize ? normalizeNumber(value) : `${value != null ? value : ''}`;
let pieces = value.split('.');
if (!pieces[0].length) {
return;
}
if (minDecimals > 0) {
pieces[1] = (pieces[1] = pieces[1] || '').padEnd(minDecimals, '0');
@jonasraoni
jonasraoni / UrlBuilder.cs
Created December 23, 2020 08:44
Simple URL builder for C#
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace JonasRaoni
{
@jonasraoni
jonasraoni / valid-utf-8-regexp.txt
Created December 6, 2020 13:39
Regular expression to find valid UTF-8 characters (useful for filtering bad input which might break some applications)
[\x00-\x7F] # ASCII
|[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16