Computation of polynomial contrast variables

Question: Please give me idea how to efficiently recode a categorical variable (factor) into the set of orthogonal polynomial contrast variables.
For many types of contrast variables (e.g. deviation, simple, Helmert, etc.) the pass is:

Compose the contrast coefficients matrix correspondig to the type.
Inverse or generalize-inverse it to obtain the matrix of codes.

For example:
Suppose there is 3-group factor and we want to recode it into a set of deviation  contrast variables.
The last group is treated as reference. Then the contrast coefficients matrix L is

         Group1 Group2 Group3
   var1   2/3   -1/3   -1/3
   var2  -1/3    2/3   -1/3

and ginv(L) is then the sought-for coding matrix

         var1 var2
  Group1   1    0
  Group2   0    1
  Group3  -1   -1

(We might also use inv(L) instead if we add a row for constant, equal to 1/3, at the head of L.)

Is there the same or similar way to get polynomial contrast variables? If yes what matrix C would look like and how to compose it? If no what still is the way to compute the polynomial contrast variables efficiently (e.g. by matrix algebra).
Comment: I looked at your question after verifying (incidentally) that the results of `qr.qy()` agree with the manual calculations of `qr.Q(qr(X))` followed by `Q%*%z` on my post. I really wonder if I can say anything different to answer your question without duplication. I really don't want to do a bad job... I have read enough of your posts to have a lot of respect for you... If I find a way to express the concept without code, just conceptually through linear algebra, I'll come back to it. I'm happy, though, that you found my exploration of the issue of some value.  Best wishes, Toni.
Comment: @Antoni, thank you. My aim is to be able to code it (in SPSS, by its syntax). Is it possible to figure out how the functions you mention work?
Answer: As a segue to my prior post on this topic I want to share some tentative (albeit incomplete) exploration of the functions behind the linear algebra and related R functions. This is supposed to be a work in progress.
Part of the opaqueness of the functions has to do with the "compact" form of the Householder $\mathbf {QR}$ decomposition. The idea behind the Householder decomposition is to reflect vectors across a hyperplane determined by a unit-vector $\mathbf u$ as in the diagram below, but picking this plane in a purposeful way so as to project every column vector of the original matrix $\bf A$ onto the $\bf e_1$ standard unit vector.  The normalized norm-2 $1$ vector $\bf u$ can be used to compute the different Householder transformations $\mathbf{ I - 2\,  uu^T x}$.

The resultant projection can be expressed as
$\text{sign}(x_i=x_1)\times \lVert x \rVert \begin{bmatrix}1\\0\\0\\\vdots\\0\end{bmatrix}+\begin{bmatrix}x_1\\x_2\\x_3\\\vdots\\x_m\end{bmatrix}$
The vector $\bf v$ represents the difference between the column vectors $\bf x$ in the matrix $\bf A$ that we want to decompose and the vectors $\bf y$ corresponding to the reflection across the subspace or "mirror" determined by $\bf u$.
The method used by LAPACK liberates the need for storage of the first entry in the Householder reflectors by turning them into $1$'s. Instead of normalizing the vector $\bf v$ to $\bf u$ with $\lVert u\rVert= 1$, it is just the fist entry that is converted to a $1$; yet, these new vectors - call them $\bf w$ can still be used as a directional vectors.
The beauty of the method is that given that $\bf R$  in a $\bf QR$ decomposition is upper triangular, we can actually take advantage of the $0$ elements in $\bf R$ below the diagonal to fill them in with these $\bf w$ reflectors. Thankfully, the leading entries in these vectors all equal $1$, preventing a problem in the "disputed" diagonal of the matrix: knowing that they are all $1$ they don't need to be included, and can yield the diagonal to the entries of $\bf R$.
The "compact QR" matrix in the function qr()$qr can be understood as roughly the addition of the $\mathbf R$ matrix and the lower triangular "storage" matrix for the "modified" reflectors.
The Householder projection will still have the form $\mathbf{ I - 2\,  uu^T x}$, but we won't be working with $\bf u$ ($\lVert \bf x \rVert=1$), but rather with a vector $\bf w$, of which only the first entry is guanteed to be $1$, and
$\mathbf{ I - 2\,  uu^T x}=\mathbf{ I - 2\,  \frac{w}{\lVert w \rVert} \frac{w^T}{\lVert w \rVert} x}=\mathbf{ I - 2\,  \frac{w\,w^T}{\lVert w \rVert^2}\,x}\tag{1}$.
One would assume that it would be just fine to store these $\bf w$ reflectors below the diagonal or $\bf R$ excluding the first entry of $1$, and call it a day. However, things are never so easy. Instead what is stored below the diagonal in qr()$qr is a combination of $\bf w$ and the coefficients in the Householder transformation expressed as (1), such that, defining $\text{tau}$
as:
$\Large \tau = \frac{w^T\,w}{2}= \frac{\lVert \bf w \rVert}{2}$, the reflectors can be expressed as $\text{reflectors}=\large \bf w/\tau$. These "reflector" vectors are the ones stored right under $\bf R$ in the so-called "compact $\bf QR$".
Now we are one degree away from the $\bf w$ vectors, and the first entry is no longer $1$, Hence the output of qr() will need to include the key to restore them since we insist on excluding the first entry of the "reflector" vectors to fit everything in qr()$qr. So are we seeing the $\tau$ values in the output? Well, no that would be predictable. Instead in the output of qr()$qraux(where this key is stored) we find $\rho=\frac{\sum \text{reflectors}^2}{2}= \frac{\bf w^Tw}{\tau^2} / 2$.
So framed in red below, we see the "reflectors" ($\bf w/\tau$), excluding their first entry.

All the code is here, but since this answer is about the intersection of coding and linear algebra, I will paste the output for ease:

options(scipen=999)
set.seed(13)
(X = matrix(c(rnorm(16)), nrow=4, byrow=F))
           [,1]      [,2]       [,3]       [,4]
[1,]  0.5543269 1.1425261 -0.3653828 -1.3609845
[2,] -0.2802719 0.4155261  1.1051443 -1.8560272
[3,]  1.7751634 1.2295066 -1.0935940 -0.4398554
[4,]  0.1873201 0.2366797  0.4618709 -0.1939469

Now I wrote the function House() as follows:
   House = function(A){
    Q = diag(nrow(A))
    reflectors = matrix(0,nrow=nrow(A),ncol=ncol(A))
    for(r in 1:(nrow(A) - 1)){
        # We will apply Householder to progressively the columns in A, decreasing 1 element at a time.
        x = A[r:nrow(A), r]
        # We now get the vector v, starting with first entry = norm-2 of x[i] times 1
        # The sign is to avoid computational issues
        first = (sign(x[1]) * sqrt(sum(x^2))) +  x[1]
        # We get the rest of v, which is x unchanged, since e1 = [1, 0, 0, ..., 0]
        # We go the the last column / row, hence the if statement:
        v = if(length(x) > 1){c(first, x[2:length(x)])}else{v = c(first)}
        # Now we make the first entry unitary:
        w = v/first
        # Tau will be used in the Householder transform, so here it goes:
        t = as.numeric(t(w)%*%w) / 2
        # And the "reflectors" are stored as in the R qr()$qr function:
        reflectors[r: nrow(A), r] = w/t
        # The Householder tranformation is:
        I = diag(length(r:nrow(A)))
        H.transf = I - 1/t * (w %*% t(w))
        H_i  = diag(nrow(A))
        H_i[r:nrow(A),r:ncol(A)] = H.transf
        # And we apply the Householder reflection - we left multiply the entire A or Q
        A = H_i %*% A
        Q = H_i %*% Q
    }
    DECOMPOSITION = list("Q"= t(Q), "R"= round(A,7),
            "compact Q as in qr()$qr"=
            ((A*upper.tri(A,diag=T))+(reflectors*lower.tri(reflectors,diag=F))),
            "reflectors" = reflectors,
            "rho"=c(apply(reflectors[,1:(ncol(reflectors)- 1)], 2,
                function(x) sum(x^2) / 2), A[nrow(A),ncol(A)]))
    return(DECOMPOSITION)
}

Let's compare the ouput to the R built-in functions. First the home-made function:
(H = House(X))
$Q
            [,1]        [,2]       [,3]       [,4]
[1,] -0.29329367 -0.73996967  0.5382474  0.2769719
[2,]  0.14829152 -0.65124800 -0.5656093 -0.4837063
[3,] -0.93923665  0.13835611 -0.1947321 -0.2465187
[4,] -0.09911084 -0.09580458 -0.5936794  0.7928072

$R
          [,1]       [,2]       [,3]      [,4]
[1,] -1.890006 -1.4517318  1.2524151 0.5562856
[2,]  0.000000 -0.9686105 -0.6449056 2.1735456
[3,]  0.000000  0.0000000 -0.8829916 0.5180361
[4,]  0.000000  0.0000000  0.0000000 0.4754876

$`compact Q as in qr()$qr`
            [,1]        [,2]       [,3]      [,4]
[1,] -1.89000649 -1.45173183  1.2524151 0.5562856
[2,] -0.14829152 -0.96861050 -0.6449056 2.1735456
[3,]  0.93923665 -0.67574886 -0.8829916 0.5180361
[4,]  0.09911084  0.03909742  0.6235799 0.4754876

$reflectors
            [,1]        [,2]      [,3] [,4]
[1,]  1.29329367  0.00000000 0.0000000    0
[2,] -0.14829152  1.73609434 0.0000000    0
[3,]  0.93923665 -0.67574886 1.7817597    0
[4,]  0.09911084  0.03909742 0.6235799    0

$rho
[1] 1.2932937 1.7360943 1.7817597 0.4754876

to the R functions:
qr.Q(qr(X))
            [,1]        [,2]       [,3]       [,4]
[1,] -0.29329367 -0.73996967  0.5382474  0.2769719
[2,]  0.14829152 -0.65124800 -0.5656093 -0.4837063
[3,] -0.93923665  0.13835611 -0.1947321 -0.2465187
[4,] -0.09911084 -0.09580458 -0.5936794  0.7928072

qr.R(qr(X))
          [,1]       [,2]       [,3]      [,4]
[1,] -1.890006 -1.4517318  1.2524151 0.5562856
[2,]  0.000000 -0.9686105 -0.6449056 2.1735456
[3,]  0.000000  0.0000000 -0.8829916 0.5180361
[4,]  0.000000  0.0000000  0.0000000 0.4754876

$qr
            [,1]        [,2]       [,3]      [,4]
[1,] -1.89000649 -1.45173183  1.2524151 0.5562856
[2,] -0.14829152 -0.96861050 -0.6449056 2.1735456
[3,]  0.93923665 -0.67574886 -0.8829916 0.5180361
[4,]  0.09911084  0.03909742  0.6235799 0.4754876

$qraux
[1] 1.2932937 1.7360943 1.7817597 0.4754876

Comment: @amoeba Thank you. Since we are alone, let me confide in you: The author of the OP does not need any help from me, but I took his comment (above) about the inner workings of (specifically) the R functions that I used in the companion post as a personal fun challenge, because it made me realize how little I understood these implementations of QR factorization built into the R functions. Since it was incredibly difficult for me to find any good references, or get any answers that really made a difference by asking online, I am posting what I got after more effort than I care to admit.
Comment: +1 but I guess that SPSS has built in functions for QR decomposition (or am I wrong?), so if the aim is to code something in SPSS that involves QR there is hardly a need to manually implement QR algorithm.


Can't attach Visual Studio for Mac debugger to Unity 2020

Question: The option "Attach to Unity" does not appear on Visual Studio for Mac:

Also, on Unity's Preferences -> External Tools, I don't see a "Editor Attaching" option:

Everything was working fine a couple of days ago, and I didn't update Unity, Visual Studio, or macOS.
Any ideas?
Environment:

macOS Catalina 10.15.7
Unity 2020.1.4f1 Personal
Visual Studio for Mac Community v8.7.8 (build 4)
Answer: For anyone going through the same problem, I just had to go to Unity -> Preferences -> External Tools -> and click on "Regenerate project files". This thread helped me.


For $u \in C^\infty(\Omega \backslash \{0\})$ harmonic with $\lim_{x\to0}\frac{u(x)}{|x|^{2-N}} = 0$, there is $v\in C^2(\Omega)$ with $u=v$ a.e.

Question: This was an exercice in my functional analysis course: Let $\Omega = B(0,1) \subset \mathbb R^N$, $N \ge 3$, and let $u \in C^\infty(\Omega \backslash \{0\})$ be a harmonic function such that
$$\lim_{x \to 0} \frac{u(x)}{|x|^{2 - N}} = 0.$$
Under these hypothesis, I must prove that there exists a harmonic function $v \in C^\infty(\Omega)$ such that $v = u$ on $\Omega\backslash \{0\}$, but I really do not see how I should do that. At some point I thought that I could try with Weyl Lemma that states the following:

Let $u \in L^1_{loc}(\mathbb R^n)$ with $\Delta u = 0$ in the sens of distributions. Then there exists $v \in C^2(\mathbb R^n)$ with $u = v$ a.e. and $\Delta v = 0$.

But as nothing ensure me that $u \in L^1_{loc}$ so... Moreover I do not use the fact that $u(x)/|x|^{2 - N}$ goes to zero.
Maybe do I have to construct explicitly the function $v$ ? I am really struggling with that, any help is welcome!
Answer: Let $v$ solve the Dirichlet problem in $\Omega$ with $v=u$ on its boundary.
Let $g(x)=\lvert x \rvert^{2-N}$.
Then $g$ is positive and harmonic on $\Omega\setminus\{0\}$.
If $w=\left(u-v\right)/g$ we have
$w=0$ on $\partial \Omega$ and $\lim_{x \to 0}w(x)=0$. Also
$ 0=\Delta\!\left(u-v\right)/g=\Delta w+\left(2/g\right)\sum_{i}g_{x_{i}}w_{x{i}}$
so the maximum principle applies to $w$.
For any $y\in\Omega\setminus\{0\}$ and $\epsilon >0$ there exists $r\le \lvert y\rvert$
such that $\lvert w\rvert \le \epsilon$ on $\partial B(0,r)$ and therefore by
the maximum principle applied to $\Omega\setminus B(0,r)$ we have $\lvert w(y)\rvert \le \epsilon$.
Therefore $w=0$ throughout its domain.
Comment: Thanks a lot! Just how do you deduce that $\Delta [(u - v)/g] = 0$ ?
Comment: I think my claim was rather that $\left(\Delta(u-v)\right)/g=0$. Because $u$ and $v$ are both harmonic.
Comment: And the following equality is a consequence of $\Delta (gw)=g\Delta w + 2\sum_{i}g_{x_{i}}w_{x_{i}}+w\Delta g$.


Título ficando por fora da div

Question: Tenho uma div pai que contém duas divs filho e uma dessa div filho contém outras duas divs filho. O título de uma dessas está ficando por fora da mesma. Modifiquei os tamanhos da mesma devido ao tamanho disponível que o SO-pt permite, para evitar criar barras de rolagem na horizontal pelo menos. valores reais estarão ao final da exibição do exemplo da div.
Código

.div-pai-banner {
    width: 480px;
    height: 300px;
    background-color: #CCC;
    margin: auto;
    margin-top: 20px;
    border: 3px solid #666;
    font-family: tahoma, Arial;
    font-size: 1.5em;
    font-weight: bold;
}

.div-filho-time {
    width: 480px;
    height: 220px;
    background-color: #FFF;
    text-align: center;
}

.div-filho-footer {
    width: 480px;
    height: 80px;
    background-color: #00F;
}

.div-filho-patrocinio {
    width: 300px;
    height: 80px;
    float: left;
    background-color: #F00;
    text-align: center;
}

.div-filho-realizacao {
    width: 180px;
    height: 80px;
    background-color: #F00;
    text-align: center;
}
<!DOCTYPE html>

        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="estilo-banner.css"/>
        <title>Banner</title>

imagem do time...

Patrocínio:

Realização:

Valores reais:

.div-pai-banner  width: 800px; height: 500px;
.div-filho-time width: 800px; height: 400px;
.div-filho-footer width: 800px; height: 100px;
.div-filho-patrocinio width: 500px; height: 100px;
.div-filho-realizacao width: 300px; height: 100px;

P.S.: Nesse exemplo mais acima o título realização nem sequer apareceu!
Answer: Não é o título quem está fora, é a própria div que desceu pois é a propriedade padrão de exibição do elemento é display: block.
Basta adicionar display: inline-block no estilo da div-filho-realizacao.
.div-filho-realizacao {
  width: 300px;
  height: 100px;
  background-color: #f00;
  text-align: center;
  display: inline-block;
}

Explicação de alguns valores da propriedade display:
Block
O elemento se comporta como um bloco. Ocupando praticamente toda a largura disponível na página. Elementos de parágrafo (p) e título(h1, h2, ...) possuem esse comportamento por padrão.
Inline
O elemento se comporta como um elemento em linha. Exemplos de elemento que se comportam assim são por exemplo as tags span e a.
Inline-block
Semelhante ao inline, no entanto, ao definirmos inline-block em um elemento, conseguimos definir as propriedades de largura e altura para ele. Coisa que não conseguimos em um elemento com display: inline.
Fonte: http://tableless.github.io/iniciantes/manual/css/display.html
Comment: deu certo aqui, coloquei coloquei a div patrocinio para flutuar a esquerda, pensei que assim a outra div iria flutuar a sua direita, já que, a soma da largura das divs filha equivale a largura da div no qual elas estão contidas.
Comment: Sim, poderia utilizar a propriedade `float` também, mas na `div-filho-realizacao` com o valor `right`.

Título ficando por fora da div


What is the difference between $\propto$ and $\sim$

Question: Suppose I have two physical quantities, lets name them $a$ and $b$.
I wonder what the difference exactly is between
$$a\propto b,\tag{1}$$
and
$$a \sim b.\tag{2}$$
I know for eq. 1, that it means $a=kb$. (See e.g. What does the sign $\propto$ mean?)
For eq. 2 I found Meaning of $\sim$? , but it is applied to functions, e.g.$ \lim_{x \rightarrow x_0} \frac{a(x)}{b(x)} = 1 $, but I am a bit confused on what it means in the context of plain physical quantities. Same order of magnitude? Does it imply a linear scaling as $\propto$?
Comment: Can you say more about the context? Most symbols do not have a single meaning. For example, your second symbol could mean "equivalent to", "asymptotic to", or something situationally defined.
Comment: The first usually means "[proportional to](https://en.wikipedia.org/wiki/Proportionality_%28mathematics%29#Symbols)".
Comment: @Marc I have a balance equation (momentum). Basically, I keep the largest terms on both sides. They are not equal anymore, and I think $\approx$ does not feel right, but I want to indicate that they more or less scale in the same way. I am wondering if $\sim$ would be appropriate. Someone told me $\propto$ and $\sim$ mean the same thing. But I am not convinced.
Comment: @Jean-ClaudeArbaut Thanks, I have the wiki-page open already :). For the first one I am quite certain, my doubt is mainly in the second one.
Comment: If you are just keeping the largest terms, it sounds like you are in an asymptotic situation. Is there a limit being taken?
Comment: I have seen sometimes $\sim$ used as "proportional to", but actually, $\sim$ may mean many things depending on context. In *maths* it's mainly what you have written, but it's also used in probability to say "variable X follow the distribution D", or in group theory to mean two groups are isomorphic (not sure this one is standard, I have also seen $\simeq$).
Comment: @Marc Formally that is what I am doing, although the limit is implicitly taken (i.e. based on physical grounds).
Comment: I think the answer is that $a \sim b$ means the same thing as $a \propto b,$ except when it doesn't.
Comment: "More or less scale in the same way" - with respect to changing what variable?  Then the two sides are being treated as functions of that variable so the "asymptotic" interpretation would make sense.
Answer:
Like you said,  $a \propto b$ means: a is proportional to b and so:

$a \propto b\Rightarrow a=k\cdot b$ for some $k \in \mathbb{R}$

In contrast: $a \sim b$ means: a is distributed according to b and so:

$a \sim b \Rightarrow \lim_{x\rightarrow \infty} \dfrac{a(x)}{b(x)} = 1 $
So, $\sim$ indicates identical asymptotic behavior for arbitrary functions $a,b$. Maybe functions over time for your usecase?
Note that although $a \propto b$ is a more general statement than $a \sim b$ since it holds for all values of $a$ and $b$, rather than only for the limit, it does not imply $a \sim b$.


Every short exact sequence with a simple module splits

Question: More concretely, if $R$ is a ring, and $M$ is a simple $R$-module, I want to show that any short exact sequence $0 \rightarrow L \rightarrow M \rightarrow N \rightarrow0$ splits. To this end, I have already proved the fact that for any simple $R$-module $M$, there exists a maximal left ideal $I \triangleleft R$ such that $M \cong_R R/I$. Now my question is this: could I in some way identify $L \oplus N$ with $R/I$? In that way, I could compose the isomorphism $\phi : M \rightarrow R/I$ with some isomorphism $h : L \oplus N \rightarrow R/I$ to split the sequence. I'm a bit worried that the short-exactness doesn't seem te get into play here, though.
Comment: It splits because one of the other terms is $0$.
Comment: Hint kernel of a map from a simple module is either the module itself or trivial
Answer: Let $0 \rightarrow L \rightarrow M \rightarrow N \rightarrow0$ be any short exact sequence. Note that im$f$ is a submodule of $M$. As $f$ is injective, we have $L \cong \text{im}f \in \{\{0_M\}, M\}$ (since these are the only submodules of $M$). Consider the map $r_1 : M \rightarrow L$, $m \mapsto 0_L$. If $L \cong \{0_M\}$ (i.e. $L = \{0_L\}$), the for all $l \in L$ we have $r_1 \circ f(l) = r_1(f(l)) = 0_L = l$, so $r_1 \circ f$ = id$_L$ and $r_1$ is a retraction of $f$. Hence the sequence splits. If, on the other hand, im $f = M$, $f$ is bijective, so $r_2 := f^{-1}$ exists. Then $r_2 \circ f =$ id$_L$, so the sequence splits again.


Is logistic regression a "semi-parametric" model?

Question: I recently see a term "semi-parametric" in the answers of my question but not really understand what is this term means.
Wikipedia says

In statistics, a semiparametric model is a statistical model that has parametric and nonparametric components.

And gives Cox proportional hazards model as an example.
I see Cox proportional hazards model and logistic regression are very similar, why we say one is semi-parametric but not say another?

BTW I found this answer, says GLM is not a semi-parametric model.
Answer: The logistic regression is not "semi-parametric". It has only parametric component. For parametric model, the number of parameters is fixed and does not depend on the number of training data, but only depends on the model itself. This is true for logistic regression since if you have $n$ variables $X_1,\ldots,X_n$ you have $n+1$ parameters $w_0,\ldots,w_n$ to define the logistic regression model, and the number of these parameters does not increase or decrease based on the number of training data. Note that for non-parametric models you also have parameters, but the number of parameters is not fixed and depends on the number of training examples.
Comment: Yes. Read this paragraph of the *Kevin Murphy's Machine Learning book* for more clarification: "does the model have a fixed number of parameters, or does the number of parameters grow with the amount of training data? The former is called a parametric model, and the latter is called a non- parametric model."
Comment: Thanks can i use "if number of parameters are depending on number of row in data matrix" to see if a model is non parametric?
Comment: Why do you think neural networks are non-parametric? A neural net with a fixed structure is parametric.
Comment: Then what would a "semi-parametric" model be? Having both a fixed number and a data-dependent number of parameters?
Comment: Yes! Partially linear regression and the proportional hazards model are two examples.
Comment: @Hossein thanks for your help. But I am still not convinced we can use "if number of parameters are depending on number of row in data matrix" to check if it is a parametric method (although it is written in Murphy's book). For example, neural network, if network structure is fixed, then the number parameters will not depend on number of data. But it should be a non-parametric right?


Override taxonomy term path with Views

Question: I need to override by views my hierarchical taxonomy term paths:
www.example.com/[vocabulary name]
www.example.com/[vocabulary name]/term1
www.example.com/[vocabulary name]/term1/term2
www.example.com/[vocabulary name]/term1/term2/term3

Where
term1 has depth = 0
term2 has depth = 1
term3 has depth = 2

Vocabulary terms:
+ term1
-- + term2
---- + term3
---- + term3.a
---- + term3.b
-- + term2.a
-- + term2.b

+ term1.a
-- + term2.c
-- + term2.d
-- + term2.e

Paths have been configured by Pathauto
If the path leads to term with depth = 0 I'm using view display "Page".
If the path leads to term with depth = 1 I'm using view display "Page - category".
If the path leads to term with depth = 2 I'm using view display "Page - subcategory".
I tried to use Taxonomy Views Integrator to override the paths by Views, and it's working, but there is one disadvantage in this solution:
If I add a new content and create a new term (with the help of the Simple hierarchical select module ) then I'll be forced to edit this term and select the appropriate view  to override this term path. It might be very time consuming with many terms, so I'm looking for another solution.
How to set a view display for a specific term depth?
Comment: Can't you just clone the builtin Views taxonomy override view? Do you need different Views per taxonomy? Or by hierarchical depth?

http://example.com/admin/structure/views/view/taxonomy_term/edit
Comment: The question is how to build different views per vocabulary with hierarchical terms. Not sure what do you mean by `builtin Views taxonomy override view`
Comment: You must decide with argument from the path will do the filtering. So, if that path is "www.example.com/[vocabulary name]/term1/term2/term3" you will show results from which term (or vocabulary)? If you decide that an override of the views taxonomy term page is fine as mentioned before.
Comment: Can't decide which argument do the filtering, because for each view there is only one argument. Have a look at my answer [here](http://drupal.stackexchange.com/a/82157/16739) this question is based on it.
Comment: Similar question here, http://drupal.stackexchange.com/questions/56963/different-view-page-based-on-taxonomy-depth
Answer: I think you are making a little mistake that confuses you.
TVI does not allow to define a view for the vocabulary itself so the path www.example.com/[vocabulary name] cannot be handled by the module. The vocabulary settings are the default ones for all terms within the vocabulary. These settings can be overriden for each term and can be inherited with the latest version. If you want a specific view for your entire vocabulary, you will have to create it by yourself because no vocabulary page exists by default in Drupal.
If I get your problem described in this post and on drupal.org (https://drupal.org/node/2066015) I can suggest you the following :

Define view1 as a page display and give it the path related to your vocabulary's name
Define view2, view3 dans view4 using any path of your choice, it does not matter as TVI will call them directly
Define view2 as the vocabulary's default view
Define view3 and view4 as term's specific settings if needed

When trying to show a term page, TVI will do the following :

If the term has TVI settings, use it
Else find its first parent having a TVI setting with the "override" option and use it
If no parent is found, use the vocabulary's TVI settings

If I take your drupal.org sample you will have :
Terms
Vocabulary (view2)
T 1 (no settings)
    T 1.1 (view 3, not inheritable)
        T 1.1.1 (no settings)
    T 1.2 (view 4, inheritable)
        T 1.2.1 (no settings)
    T 1.3 (no settings)
T 2 (no settings)

Results
T 1 (view 2 from vocabulary)
    T 1.1 (view 3 from itself)
        T 1.1.1 (view 2 from vocabulary)
    T 1.2 (view 4 from itself)
        T 1.2.1 (view 4 from T 1.2)
    T 1.3 (view 2 from vocabulary)
T 2 (view 2 from vocabulary)

I hope this will help you to achieve what you want to do.
Comment: In your results `T1`, `T2` have the same default view - `View 2` - it's ok, this is what I need, but if I would like to set another default view, let's say `view 3` for `T 1.1, T 1.2 and T 1.3`, then I won't be able do that without changeing their parent default view (if I do that, I'll be forced to define each next parent separately, which might be very time consuming). Thank you anyway for your answer, because now, sadly, I'm sure it can't be done with TVI module.
Answer: I've found the solution by myself. It works without the TVI module.
Useful, but not required modules: Taxonomy Menu Block
This module allows to create a menu out of your taxonomies and comes with its own theme function
and a few hooks (see the .api.php file) that allows you to alter the generated data to your needs.
One of these hooks will be used by this solution.
In genereal there are four steps of this solution:

Install the Taxonomy Menu block module if needed.
Change the taxonomy term path of specific terms depending on their depth.
Create appropriate aliases or update them if they are already exist.
Set the new taxonomy term path for a specific view display.

Step by step
Install the Taxonomy Menu Block and go to: admin/structure/block/add-taxonomy-menu-block, do some settings and save it.
Change the taxonomy term path of specific terms depending on their depth.
Here I'm looking for parents only. If they're exist, that means the term is a child.
Otherwise, it is a parent.
function YOUR_MODULE_entity_info_alter(&$entity_info) {
  $entity_info['taxonomy_term']['uri callback'] = 'YOUR_MODULE_taxonomy_term_uri';
}

function YOUR_MODULE_taxonomy_term_uri($term) {
  switch ($term->vocabulary_machine_name) {
    case 'YOUR_VOCABULARY_MASHINE_NAME':
      $parents = taxonomy_get_parents($term->tid);
      if (empty($parents)) {
        return array(
          'path' => 'taxonomy/category/' . $term->tid,
        );
      }
      else {
        return array(
          'path' => 'taxonomy/subcategory/' . $term->tid,
        );
      }
    break;
  }
  // default taxonomy term uri
  return taxonomy_term_uri($term);
}

Create appropriate aliases or update them if they are already exist.
Go to: admin/config/search/path/patterns and set the pattern for all your vocabulary paths: [term:vocabulary]/[term:parents:join-path]/[term:name]
Important: If you already had pathauto running before having this issue you must rebuild the aliases by going to the delete aliases tab and deleting the old aliases for "Taxonomy term paths", then go to the bulk update tab and updating the "Taxonomy term paths".
Alter taxonomy menu block links
Unfortunatelly, if we change the default taxonomy term path, then neither menu created by the Taxonomy menu nor the Taxonomy Menu Block (TMB) module won't be able to replace system paths by their aliases, but luckily, the TMB allows us to alter the links to our needs:
function YOUR_MODULE_taxonomy_menu_block_tree_alter(&$tree, $vid) {

  $vocabulary = taxonomy_vocabulary_load($vid);
  if ($vocabulary->name == 'YOUR_VOCABULARY_MASHINE_NAME') {
    foreach ($tree as $tid => $term) {
      switch ($term['depth']) {
        case 0 :
          $tree[$tid]['path'] = drupal_get_path_alias('taxonomy/category/' . $tid);
          break;
        case 1 :
          $tree[$tid]['path'] = drupal_get_path_alias('taxonomy/subcategory/' . $tid);
          break;
      }
    }
  }
}

View page settings
In order to use different view display for a specific term depth you need to set the appropriate path under "Page Settings" for each display. For example:
View displays:
Main category page -> path: taxonomy/category/%
Subcategory page -> path: taxonomy/subcategory/%
Notice: you can't use taxonomy/term/% path any more, because now these specific terms have their own paths.
That's it!
In this solution both parent and child terms have their own view displays, so now you can add some new content and associate new terms to it using the simple hierarchical select only.
Answer: Try these short workaround called Taxonomy Views
Create a module with the following files.
taxonomy_views.info:
name = Taxonomy Views
description = It gives links like taxonomy/[vocabulary machine name]/[term id] to taxonomy term pages, so you can set up many Views for taxonomy pages. You can create two Views with paths "taxonomy/products/%" and "taxonomy/news/%" and enable vocabulary verification for each View, for example.
core = 7.x

dependencies[] = taxonomy
dependencies[] = views

taxonomy_views.module:
<?php

/**
 * @file
 * Views for taxonomy pages.
 */

/**
 * Implements hook_menu().
 */
function taxonomy_views_menu() {
  $items['taxonomy/%taxonomy_vocabulary_machine_name/term/%taxonomy_term'] = array(
    'title' => 'Taxonomy term',
    'title callback' => 'taxonomy_term_title',
    'title arguments' => array(2),
    'page callback' => 'taxonomy_term_page',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'file' => 'taxonomy.pages.inc',
    'file path' => drupal_get_path('module', 'taxonomy'),
  );
  return $items;
}

/**
 * Alter the entity info.
 *
 * Modules may implement this hook to alter the information that defines an
 * entity. All properties that are available in hook_entity_info() can be
 * altered here.
 *
 * @param array $entity_info
 *   The entity info array, keyed by entity name.
 *
 * @see hook_entity_info()
 */
function taxonomy_views_entity_info_alter(&$entity_info) {
  if (isset($entity_info['taxonomy_term'])) {
    $entity_info['taxonomy_term']['uri callback'] = 'taxonomy_views_term_uri';
  }
}

/**
 * Entity URI callback.
 */
function taxonomy_views_term_uri($term) {
  return array(
    'path' => 'taxonomy/' . $term->vocabulary_machine_name . '/term/' . $term->tid,
  );
}

Now you can create Views for paths like "taxonomy/[vocabulary-machine-name]/term/%" and set pathauto alias to [term:vocabulary]/[term:parents:join-path]/[term:name].
It's good, because you can set a View for a path taxonomy/products/term/% and enable vocabulary verification (check vocabulary is "products").
In addition, the old taxonomy term url is still available (taxonomy/term/%).
Comment: TVI module do the same, it solves the issue with the `taxonomy/[vocabulary]` path, but the problem is with the hierarchical terms (those with depth).
Comment: What does you mean by issue with hierarchical terms? I use the code above with paths (have just hierarchical terms) I wrote and it works good.
Comment: Each term has the same path. `taxonomy/term/tid` Your solution only change this path to another and has nothing to do with the depth of taxonomy terms. `www.example.com/[vocabulary name]/[term name no matter what depth it has]` it's NOT what I'm asking for.
Comment: Why can't you use pathauto aliases for that?
Comment: "Paths have been configured by Pathauto" read the question please.
Comment: What are patterns?
Comment: The patterns are provided at the very beginning of my question.
Comment: I really doesn't see any cons to use the pathauto pattern "[term:vocabulary]/[term:parents:join-path]/[term:name]" in your case.
Answer: Creata a general Views for a taxonomy term display (it has nothing to do with the path, since the internal path is always "taxonomy/term/%tid". Pathauto paths have nothing to do with thhis) and use a views Filter to use it only if the term belongs to a Vocabulary or anything you prefer.
Then clone this Views and change the filter for another views display.
Comment: You have to set up one views for each path depth. So [voc]/%term1, [voc]/%1/%term2 will be 2 different views. Also, do not use the Contextual filters of taxonomy term depth, make it as a simple views.
Answer: my solution is redirect it to our own views path in hook_init
if my views path is category/% then something like below code help
function hook_init(){
    /// redirect to category
  if(preg_match('/taxonomy\/term\/([0-9]+)$/',current_path(),$matches)){
       drupal_goto('category/'.$matches[1]);
  }
}

Override taxonomy term path with Views


Relationship between Kronecker's Approximation Thm and Weyl's Equidistribution Thm?

Question: According to Prof. Wikipedia, the Equidistribution Theorem was proved by Weyl in 1910 and independently by two others around the same time.
The theorem states that for $\alpha$ irrational, the sequence $\alpha, 2\alpha, 3\alpha,..., etc.  \pmod 1$ is uniformly distributed on the unit interval.
At Wolfram's MathWorld, a similar theorem is attributed to Kronecker, namely that for any irrational number $\alpha$, the sequence $\{n \alpha \}$ is dense in the unit interval, i.e., one can find a $\{k \alpha\}$ arbitrarily close to any real number b with $0 \leq b \leq 1 $. This is styled the Kronecker Approximation Theorem. Kronecker died in 1891. Note: $\{n\alpha\}$ is the fractional part of $n\alpha $.
I haven't seen a proof of Kronecker's theorem, but I have seen a proof of Weyl's Theorem. Naively (as usual), it appears the two ideas can be used interchangeably in some contexts. Even more naively, it seems there is a strong theoretical relationship between the two.
Can someone offer a thumbnail sketch of the salient differences/similarities of the two ideas (or a good reference for same)? Knowing no better, I would say Weyl owed a considerable debt to Kronecker.
Thank you.
Comment: Weyl's theorem is much stronger and implies Kronecker's theorem, which is much easier.
Answer: Kronecker is about sets, Weyl is about sequences.
If the sequence $u_1,u_2,\dots$ of elements of $[0,1)$ is uniformly distributed, then the underlying set $\lbrace\,u_1,u_2,\dots\,\rbrace$ is dense in $[0,1)$.
If a countable subset $S$ of $[0,1)$ is dense in $[0,1)$, then (by a theorem of Erdos) it can be ordered so as to be a uniformly distributed sequence - but it can also be ordered so as not to be uniformly distributed.
I have a feeling I've written this out on this site before.
EDIT: found it: When is a sequence $(x_n) \subset [0,1]$ dense in $[0,1]$? Not exactly the same as the current question, but there is some overlap.
Comment: Thanks. I will look at the other answer as well.


Changing order of execution of hook_preprocess_html

Question: I have 2 custom modules in Drupal 9, let's called them module A and module B.
Each module has a fully working hook_preprocess_html function defined in the .module file.  The hook_preprocess_html function in module A runs first, followed by the function in module B.
How can I update Module A so the hook_preprocess_html function in there runs after the one in module B?
I have tried to implement hook_module_implements_alter(&$implementations, $hook) but it is not even showing the hook_preprocess_html hook.  This preprocess_html seems not to apply to the module_implements_alter hook.
Many thanks for you help!
Comment: If this hook doesn't help I think there is still the module weight. See https://drupal.stackexchange.com/questions/186506/how-to-reduce-increase-modules-weight-on-the-module-install-process
Answer: Is there a hook_preprocess_hook in the list? As the doc says hook_module_implements_alter will only show the base hooks and hook_preprocess_html is a variant of hook_preprocess_hook
So you would need to change the weight for hook_preprocess_hook
Comment: Thanks for your reply.  No, hook_preprocess_hook is not in the list, which I guess is explained by apaderno.
Answer: hook_module_implements_alter() is invoked only for those hooks that are invoked via ModuleHandler::alter(), ModuleHandler::invokeAll(), and ModuleHandler::invokeAllWith(). All those methods call ModuleHandler::getImplementationInfo(), which calls ModuleHandler::buildImplementationInfo(), which contains the following code. (See the line after the Verify implementations that were added or modified. comment.)
  // Allow modules to change the weight of specific implementations, but avoid
  // an infinite loop.
  if ($hook != 'module_implements_alter') {
    // Remember the original implementations, before they are modified with
    // hook_module_implements_alter().
    $implementations_before = $implementations;

    // Verify implementations that were added or modified.
    $this->alter('module_implements', $implementations, $hook);

    // Verify new or modified implementations.
    foreach (array_diff_assoc($implementations, $implementations_before) as $module => $group) {
      // If an implementation of hook_module_implements_alter() changed or
      // added a group, the respective file needs to be included.
      if ($group) {
        $this->loadInclude($module, 'inc', "{$module}.{$group}");
      }

      // If a new implementation was added, verify that the function exists.
      if (!function_exists($module . '_' . $hook)) {
        throw new \RuntimeException("An invalid implementation {$module}_{$hook} was added by hook_module_implements_alter()");
      }
    }
  }

Registry::processExtension() creates the list of preprocess hooks implemented for a single theme hook with the following code.
foreach ($prefixes as $prefix) {
  if (isset($info['template']) && function_exists($prefix . '_preprocess')) {
    $info['preprocess functions'][] = $prefix . '_preprocess';
  }
  if (function_exists($prefix . '_preprocess_' . $hook)) {
    $info['preprocess functions'][] = $prefix . '_preprocess_' . $hook;
  }
}

$prefixes is initialized from the following code. In particular, see the if ($type == 'module') { /* … */ } part, which is the one that allows to modules to add their own variable preprocessors (a.k.a. preprocess hooks).
(I left only the relevant comment to make the code shorter. All the comments can be read in the linked documentation page, which shows also the method code.)
if ($type == 'module') {
  $prefixes[] = 'template';

  // Add all modules so they can intervene with their own variable
  // preprocessors. This allows them to provide variable preprocessors
  // even if they are not the owner of the current hook.
  $prefixes = array_merge($prefixes, $module_list);
}
elseif ($type == 'theme_engine' || $type == 'base_theme_engine') {
  $prefixes[] = $name . '_engine';
  $prefixes[] = $theme;
}
else {
  $prefixes[] = $name;
}

$module_list is initialized with $module_list = array_keys($this->moduleHandler->getModuleList());
The code that invokes the preprocess hooks does not use the methods from the ModuleHandler class I listed earlier, since it builds the list of preprocess hooks to invoke using Registry::processExtension(), which means hook_module_implements_alter() is not invoked for preprocess hooks.
The list of modules returned by ModuleHandler::getModuleList() is ordered by the modules weight, though. This means that changing a module weight will change the order preprocess hooks are invoked.
Comment: Thanks, that makes sense.  Does this mead that they only way to affect the order of preprocessors is to set a module weight at install?
Comment: If the list of modules is taken from *container.modules*, or from a function/method that returns the value of that container parameter, yes. That includes the code that calls `ModuleHandler::getModuleList()`, which is called from the method that uses the last code lines I show in the answer.
Comment: I am going to expand the answer to show that the method that creates the list of preprocess hooks uses `ModuleHandler::getModuleList()`, which would make more evident the code is not using `ModuleHandler::invokeAll()`, for example.
Comment: Done: Now the answer should be clearer about changing a module weight to change the order the preprocess hooks are invoked.
Comment: Excellent, thank you. I changed the module weight and now I'm getting things in teh right order for me.  I am surprised there's no other way, as it's not always possible to change a module weight by uninstalling/ re)installing once live.
Comment: @AlastairHoward You should not need to uninstall/reinstall a module to change its weight. It is enough you call [`module_set_weight()`](https://api.drupal.org/api/drupal/core%21includes%21module.inc/function/module_set_weight/9) inside a new `hook_update_N()` implementation.


How to programmatically create a View on a node that displays SQL Query content?

Question: How can I programmtically create a View on node(s) that displays the content from a specific query?
For example I would like to display the following:
<code>$args = array(':nid' => $node->nid);
$results = db_query('SELECT USER_ID FROM USER INNER JOIN node ON USER.OFFICE_ID =
  node.title AND node.nid = :nid', $args);
</code>
Essentially, I would like to grab the 'nid' from the current node and I would like the View to display all 'users' associated with the current node, as the query would return.
I have been able to achieve this through a custom module. But I have been requested to use Views instead.
EDIT:
I've done what Johnathan Elmore suggested and got the View to display the correct information if I manually enter the 'nid' in the 'Preview with contextual filters:' space. However, if I go to the node itself, it will not display the query content.
For example:
I have nid == 123 with url alias 'office-123', if I enter 123 in 'Preview with contextual filters:', it will display the correct query information. However, if I go to 'mysite/office-123' nothing is displayed.
How can I get this View to display when I go to the node url?
Comment: bump for edit...
Answer: You can create a View block to show on the nodes, and use a contextual filter (Content: Nid) in the View to figure out which node to reference.
Then you can display the related user (user reference field?) for that node in the block.
In case you are using pathauto to auto alias your node URL's, you can setup the Contextual Filter field (Content: Nid) to determine the Nid from the URL:
Comment: johnathan, thanks for the help, but would you know of a way to programmatically add my sql query to a view without using the views ui? I have 200+ nodes I need to do this for and doing all of them by hand is out of the question
Comment: You should be able to use this answer to create a block which will work automatically for each nodes.  It will derive the nid from the node's URL and generate a list of users that pertain to that node.  Are you trying to programmatically *change* the view as it's genererated?  My answer basically generates a View that is works equivalent to the SQL you gave.
Comment: If I may, you seem to have a very poor understanding of what Views does, and how it does it. I think you could save yourself A LOT of hassle reading the documentation or a good tutorial about Views. It would lead you do a greater level of abstraction, and factorize your code to make it more efficient and easy to maintain and extend. That's the whole point of the API. No offence meant : if you're able to make what you did with code, you'll find it easier with Views.
Comment: @JohnathanElmore, I tried your suggest and played around with the options quite a bit, but I am unable to get the View block to work correctly. More specifically, the query seems to be empty with your options. When you say contextual filters do you mean it under 'Advanced' or do you mean the line with 'Preview with contextual filters:'?
Comment: You configure the contextual filters first under the Advanced area.  Then to test the results you enter in something into the Preview contextual filters box, otherwise the query won't run. In your case you would enter a node id or URL path to the node
Comment: @JohnathanElmore. Thanks Johnathan, I've got my query tested and working. However, I don't quite understand the last step. I have an office named '83H' with nid '447' and it has url_alias 'office-83H'. If I go to 'mysite/office-83H', my view doesn't display. Would you know what could be wrong?
Comment: If office-83H is a node, then by choosing "Content ID from URL" Views will automatically know it needs derive the Node ID from the URL.  It grabs the source URL of 'mysite/office-83H', which is node/447, and then tells the View to only display users related to node 447.
Comment: @JohnathanElmore. Johnathan, that's the exact problem I am having. My query displays properly under Views UI, but it doesn't when I go to 'mysite/office-83H'. I can confirm that office-83H is a node as it appears under the node table in the DB.
Comment: Did you place the Views block to appear in the Content region, and did you set it to appear on the correct node type?  If you did and it's still not showing, what exactly did you enter into the Contextual filter textbox in Views UI to see that it works?
Comment: @JohnathanElmore. Thank you very much I see it now. I did not actually put the 'block' through structure/blocks in the content region.
Comment: Thanks for the question, this is a good way to keep refreshed


Geometry and tangent-chord theorem problem?

Question: According to the figure, CA is tangent to the circle, centre O, at A. ABT and POT are straight lines.
Question:
Given that BT is equal to the radius of the circle, prove that:

$\angle ABP = 3 \angle OBP$
$\angle POA = 3 \angle BOT$
$\angle OBT = 2 (\angle BTO + \angle CAB)$

My attempt:

$\angle AOP = 2 \angle ABP. $
$\angle OBP = \angle OPB.$
$\angle CAB = \angle APO.$
$\angle BOA = 2 \angle APB.$
Comment: What is the question?
Comment: prove that: 1. Angle ABP = 3 time of Angle OBP 2. Angle POA = 3 times of Angle BOT 3. Angle OBT = 2 times of (Angle BTO + Angle CAB)
Comment: Have you made any attempt? Please add an attempt so we can help you with where you might've got stuck
Comment: Yes, I have edited the original post to include my attempts.
Comment: Use the isosceles properties of BOT, AOB and AOP to relate the angles along with the AOP = 2* ABP. That should solve all of them
Comment: I still did not manage to get it. Can you give me a couple of initial steps so that I can follow through from there?
Answer:
$$\measuredangle ABP=\measuredangle OPB+\measuredangle OTB=\measuredangle OPB+\measuredangle TOB=$$
$$=\measuredangle OPB+\measuredangle OPB+\measuredangle OBP=3\measuredangle OBP.$$
$$\measuredangle POA=\measuredangle OTB+\measuredangle OAB=\measuredangle OTB+\measuredangle OBA=$$
$$=\measuredangle OTB+\measuredangle OTB+\measuredangle BOT=3\measuredangle BOT.$$
$$\measuredangle OBT=\measuredangle OAB+\measuredangle AOB=\measuredangle ABO+2\measuredangle CAB=$$
$$=\measuredangle BTO+\measuredangle BOT+2\measuredangle CAB=2(\measuredangle BTO+\measuredangle CAB).$$
Answer: a):
$BOT=2PBO$
$ABO=2BOT=4PBO$
$ABP=ABO-PBO=4PBO-PBI=3PBO$
b):
$ABP+\frac{1}{3}ABP=2BOT$
$\frac{4}{3}ABP=2BOT$
$ABP=\frac{1}{2}POA$
$\frac{4}{3}(\frac{1}{2}POA)=2BOT$
Therefore:
$POA=3BOT$
c):
$ OBT=AOB+OBA$
$AOB=2ABP=2CAB$
$OBA=2BTO$
Therefore:
$OBT 2(CAB+BTO)$


Programatically created node type does not display title field

Question: I have created a new node type within a module (for Drupal 7). I have created fields for it programatically as well and they all display. The one problem I am experiencing is that the title field does not display. I am not sure how or where to look to fix this. The title field does display in the node type's manage fields display.
Here is the code for creating the node type in hook_install():
<code>// define the node type
  $type = array(
    'type' => $machine_name,
    'name' => $t($parameters['name']),
    'base' => 'room_reservations',
    'module' => 'room_reservations',
    'description' => $t($parameters['description']),
    'has_title' => TRUE,
    'title_label' => $t($parameters['title_label']),
    'custom' => FALSE,
    'locked' => TRUE,
    'disabled' => FALSE,
  );

// set other node defaults not declared above
  $content_type = node_type_set_defaults($type);

// add the body field
  node_add_body_field($content_type, $t($parameters['body_label']));

// save the content type
  node_type_save($content_type);
</code>
Comment: You have dollar signs before t functions. Have you assigned a function name to variable t? As in $t = get_t(); Also have you done some theming work or are you using one of Drupal's core themes?
Comment: Yes I did use $t=get_t(). I am not doing any theming with this (yet), just using Drupal's Bartik theme.
Answer: Drupal does not display the title field in the manage display page by default.
You would have to use Display Suite Module and enable a layout for the page to get the title and other fields to display in the Manage Display page.
Comment: The question refers to the manage fields page, not the manage display page.
Comment: I guess that needs clarification "manage fields display." I thought this meant the display page.
Comment: Yeah I guess it could go either way.
Comment: I meant the page where you add or remove fields. It does display on that page but when I go and create a new page based on that content type the title textfield does not appear. Sorry about the confusion.
Answer: To automatically have the node title you need to define "node_content" as base for  the node type. Else I think you have to implement the title form field display/store for your node add/edit form


AngularJS reuse multiple controllers to build a webapp

Question: What I'm looking for is to learn a proper way to build a webapp. I already built some things, but I've got to a point where I need to reuse some data and with ng-controllers, it's getting harder.
I already know controllers are not the best option, so I tried to jump to directives, based on some articles I've read. But none of them has helped me.
So, let me try to describe it.
Description
I'm building a dashboard, where i have some clients (add/edit/del), projects (which invole one or more clients), agenda, configuration, financials, etc..
I've built a lot of things already, but for example:
On the home page I'm displaying a table with the last 5 clients added to the app, also a table with the last 5 projects added to the app. Then, on the clients.html page, I display the whole table, same goes for projects, on the project.html. Both with just 3-4 fields with name, tel and email. Then on the individual client page, i display the whole information about that client. Such as address, email, contacts, observations, etc. And also a list of projects he is involved.
So what I have is something like this:
app.js
myApp.controller('ClientesCtrl', function ($scope, $http, $routeParams) {
    $scope.pagename = "Clientes";

    $scope.get_cliente = function() { //Function - Get Cliente
        $http.get("scripts/data/clientes.json")
        .success( function(data) {
            $scope.pagedClientes = data;
        })
        .error(function(data) {});
    };

    $scope.add_cliente = function() { //Function - Add Cliente
        $scope.formprevent = true;
        $http.post('scripts/php/db.php?action=add_cliente',
            {
                'cod': $scope.cad.cod,
                [... more data ...]
            }
        )
        .success(function (data, status, headers, config) {})
        .error(function (data, status, headers, config) {});
    };
});

Same goes for projects, each one with it's controller.
My folder/File organizations is very very simple (since I don't know what I can put in different files, or even how to call it back). It's something like this:
-index.html
-scripts
--js
---angular.min.js
---angular-route.min.js
--app
---app.js
-content
--home.html
--clients.html
--projects.html
The Problem
The problem starts when i need to show those data in a lot of pages.
For example in the home page, i have a list with a resume of the main data, such as total clients, total projects, and it's like this:

{{pagedClientes.length || "00"}}

clientes totais

{{pagedProjects.length || "00"}}

Processos totais

00

Processos abertos

00

Compromissos abertos

                ID
                Name
                Options

 0">
                {{cliente.cod}}
                {{cliente.nm_client}}

No client

                ID
                Project
                Options

 0">
                {{project.cod}}
                {{project.nm_cliente}}

No project

So, basically, I need to add the controller in each block I need to display that data, also I have to 'init' the function to get the data again. This is a simple example, but I think it was enough to show what I need to solve.
It's worse when I need to show the individual client, with only their projects. So before I get crazy with all of those controllers, I want to know how is the best way to put up the logic, the folder/file organization, the code organization. Is it better to use a directive? to make the directive call a controller? Call multiple controllers? Or what?
Comment: What you need is a `service`. [`AngularJS services`](https://docs.angularjs.org/guide/services) are singletons, your fetched data will be accessible in the controllers if you inject the service.
Answer: You need to use services to fetch the data: https://docs.angularjs.org/guide/services. For your code it would look something like this:
    app.factory('clients', function ($http) {

        var getClients = function() { //Function - Get Cliente
            return $http.get("scripts/data/clientes.json")
            .success( function(data) {
                return data;
            })
            .error(function(data) {});
        };

        var add_cliente = function(cad) { //Function - Add Cliente
            $http.post('scripts/php/db.php?action=add_cliente',
                {
                    'cod': cad.cod,
                    [... more data ...]
                }
            )
            .success(function (data, status, headers, config) {})
            .error(function (data, status, headers, config) {});
        };

        return {
            getClients: getClients,
            add_cliente: add_cliente
        }

})

You can then inject the service in a controller and use the functions in your controller:
    app.controller('Ctrl', function (clients, $scope) {
        clients.getClients().then(function (data) {
        $scope.myClients = data;
        });
    });

Comment: hava a look here: http://plnkr.co/edit/WBJKwmAKKhAj30FQbfGj?p=preview . It doesn't use $http, but has a service that returns a promise with $timeout.   There should be no difference between calling $http in controller and in service
Comment: Could you provide an example with html to show the data from the get function? I'm getting only a blank space, not the actual data. Also, there is no errors in my console.
Answer: Read the client data on initialization of the controller - not need to use a ng-init.
myApp.controller('ClientesCtrl', function ($scope, $http, $routeParams) {
    $scope.pagename = "Clientes";

    $http.get("scripts/data/clientes.json")
        .success( function(data) {
            $scope.pagedClientes = data;
        })
        .error(function(data) {});

    $scope.add_cliente = function() { //Function - Add Cliente
        $scope.formprevent = true;
        $http.post('scripts/php/db.php?action=add_cliente',
            {
                'cod': $scope.cad.cod,
                [... more data ...]
            }
        )
        .success(function (data, status, headers, config) {})
        .error(function (data, status, headers, config) {});
    };
});

Controllers can be nested. Define the controller on a div which wraps the area where you need the client data. Same for the ProjectsController. You could even merge the 2 controllers in one.

{{pagedClientes.length || "00"}}

clientes totais

    ....

Comment: But with this method i'll still need to call the controller each place i want to show the data, right? Is there a way to make it more simple? Specially because angularjs 2.0 will have no `ng-controller`, so if there is already a work around, that would be better
Comment: You have to define the controller only once - not multiple times! Not sure about angular 2.0.
Comment: Ok. I'll try to work with what you said. Note: About the controller stuff, i saw it in here: https://www.youtube.com/watch?v=gNmWybAyBHI in case you want to check it.
Comment: Interesting, it seems in Angular 2.0 the conrollers will be replaced by directives, which makes sense as directives are more powerful than only controllers.


Failed to write session data (files)

Question: I'm trying to set up a fonctionnal Apache2/PHP web server on my debian Jessie machine.
It seems I cannot handle session variables. These lines keep repeating in /var/log/apache2/error.log:
[Wed Oct 12 19:11:24.828962 2016] [:error] [pid 28677] [client 127.0.0.1:50153] PHP Warning:  session_start(): open(/var/lib/php5/sessions/sess_7o0m589ns0hhmafaht6cqvml35, O_RDWR) failed: Permission denied (13) in /home/nicoco/Documents/coloc-web/phpstorm-project/controller/main.php on line 2
[Wed Oct 12 19:11:24.829464 2016] [:error] [pid 28677] [client 127.0.0.1:50153] PHP Warning:  Unknown: open(/var/lib/php5/sessions/sess_7o0m589ns0hhmafaht6cqvml35, O_RDWR) failed: Permission denied (13) in Unknown on line 0
[Wed Oct 12 19:11:24.829489 2016] [:error] [pid 28677] [client 127.0.0.1:50153] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5/sessions) in Unknown on line 0

which is weird because the file permissions looks OK:
# ls -la
total 12
drwx-wx-wt 2 root   root   4096 oct.  12 17:50 .
drwxr-xr-x 4 root   root   4096 sept. 10 19:47 ..
-rw------- 1 nicoco nicoco  202 oct.  12 19:05 sess_7o0m589ns0hhmafaht6cqvml35

As I'm completely new to both web server administration and PHP, it may be possible that the problem comes from my PHP code... Any hint?
Comment: I am running several Debian 8 apache+php systems.  drwx-wx-wt` is the distro default for `/var/lib/php5/sessions/` and works perfectly fine for our PHP sessions.  I am a bit confused why that session is owned by `nicoco` and not `www-data`, which is the default apache service account.
Comment: The `Permission denied (13)` in the error log is the telling message. Apache logs this message *only* when it has been denied by the OS from reading/writing to the file system. So either file/directory permissions or something like `SELinux`. It is *not* an Apache configuration problem, even if it looks like one. If everything *seems* right then one of your assumptions is wrong.
Answer: I finally figured this out.
As the comments pointed out, the file permissions weren't OK at all, I solved the problem by a neat rm /var/lib/php5/sessions/*.
I was using phpstorm's builtin web server before and switched to apache, I think that's why the permissions were messed up. Now I believe this is more normal:
# ls -l /var/lib/php5/sessions/
total 4
-rw------- 1 www-data www-data 115 oct.  14 20:18 sess_jo5m5mu4q8kehebcmumv9o8bs0

Failed to write session data (files)


How to make the tab in flutter?

Question: I want to make this design using tab in flutter. But when I'm trying to make it using tab controller but when we not select another one then it's background should be grey I'm trying it but it is not working. So, you can help me in this.
Comment: please post the code using which you have tried to implement this functionality so we can help you out.
Answer: For the best explanation and example take a look at the official Flutter cookbook on how to work with tabs and tab controllers.
https://docs.flutter.dev/cookbook/design/tabs
Answer: Use this Page
<code>class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin{

  late TabController controller;

  @override
  void initState() {
    controller = TabController(
      initialIndex: 0,
      length: 2,
      vsync: this,
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.teal,
        ),
        body: Column(
          children: [
            TabBar(
              controller: controller,
              isScrollable: true,
              labelColor: Colors.white,
              unselectedLabelColor: Colors.grey,
              splashBorderRadius: BorderRadius.circular(50),
              padding: const EdgeInsets.all(8),
              labelStyle: const TextStyle(fontSize: 16,fontWeight: FontWeight.bold),
              indicator: const BoxDecoration(
                color: Colors.teal,
                borderRadius: BorderRadius.all(Radius.circular(50)),
              ),
              tabs: const [
                SizedBox(
                  width: 100,
                  child: Tab(
                    text: "Gainer",
                  ),
                ),
                SizedBox(
                  width: 100,
                  child: Tab(
                    text: "Loser",
                  ),
                ),
              ],
            ),
            Expanded(
              child: TabBarView(
                controller: controller,
                children: const[
                  Center(
                    child: Text("Gainer"),
                  ),
                  Center(
                    child: Text("Loser"),
                  ),
                ],
              ),
            ),
          ],
        ));
  }
}
</code>

How to make the tab in flutter?


Is $ \frac{d}{dt} \Bigg|_{t=0} g e^{-t(X+Y)}e^{tY} = \frac{d}{dt} \Bigg|_{t=0} g e^{-tX} ?$

Question: Let $G$ be a compact Lie group with Lie algebra $\mathfrak{g}$. Consider the action of $G$ on itself by left multiplication.
Let $g \in G$ an let $X, Y \in \mathfrak{g}$.Then is
$$ \frac{d}{dt} \Bigg|_{t=0} g e^{-t(X+Y)}e^{tY} =  \frac{d}{dt} \Bigg|_{t=0} g e^{-tX} ,$$
even if $[X,Y] \neq 0 ?$
Comment: What do you think? Note that the $g$ in your question is superfluous, so you may as well omit. Then it comes down to the product rule and how to differentiate the exponential function.
Comment: Also, a good way to start thinking about those questions involving Lie algebras/group is to try if it works and how it works for matrix lie groups/algebras. That usually gives some pointers for the more general theory.
Comment: @Asma . Note that $-(X+Y)+Y=\color{red}{-X}.$ The $g\in G$ is just a constant in that computation as pointed out by the first comment.
Comment: Thanks a lot for the comments!  Omitting $g$ and applying the product rule I get $\frac{d}{dt} \Bigg |_0 e^{-t(X+Y)} e^{tY} = \frac{d}{dt} \Bigg |_0 e^{-t(X+Y)} + \frac{d}{dt} \Bigg |_0 e^{tY} = -(X+Y) +Y = Y$.
Comment: @Asma:  If you've figured it out, I encourage you to write up your own answer to the question.
Comment: But If I work with $g$, I'll get $\frac{d}{dt} \Bigg |_0 g e^{-t(X+Y)} e^{tY} =\frac{d}{dt} \Bigg |_0g e^{-t(X+Y)}  + \frac{d}{dt} \Bigg |_0 ge^{tY} $, but, from here I don't know how to continue the computation!
Comment: Now, I realize that my question was very trivial. Thanks guys for your help :)
Comment: @JasonDeVito, I've posted an answer as you suggested, please take a look !
Answer: Let $L_g(h):=gh$ and $R_g(h):=hg$ denote the left and right multiplication on $G$. Applying the following product rule:
$$
\frac{d}{dt} \Bigg|_{t=0}(a(t)b(t)) =dR_{b(0)}\left(a'(t)\right)+dL_{a(0)}\left(b'(t)\right)
$$
to the paths  $a,b:\mathbb{R}\to G$, defined by  $a(t) =L_g(e^{-t(X+Y)})= g.e^{-t(X+Y)}$
and $b(t)= e^{tY}$, we get
$$
\frac{d}{dt} \Bigg|_{t=0} ge^{-t(X+Y)} e^{tY}=dR_{e}\left(dL_g(-(X+Y))\right)+dL_{g}\left(Y\right)
= dL_g(-X) =\frac{d}{dt} \Bigg|_{t=0} ge^{-X},$$
since $dR_e= Id_\mathfrak{g}$ and $dL_g$ is a linear map on $\mathfrak{g}$.
Comment: For what it's worth, I think lcv's answer is incorrect (more specifically, I agree that it's correct for a matrix group, but I don't think it makes sense for a non-matrix group) and that this one is correct.
Comment: Not really (see the comment by @KurtG )
Comment: I don't see where is exactly the fault in my answer , is it in applying the product rule ?
Answer: Note that
\begin{align}
\frac{d}{dt}\bigg[e^{-t(X+Y)}e^{tY}\bigg] &=-(X+Y)e^{-t(X+Y)}e^{tY}+e^{-t(X+Y)}e^{tY}Y \\
 &=-e^{-t(X+Y)}(X+Y)e^{tY}+e^{-t(X+Y)}Ye^{tY} \\
 &=-e^{-t(X+Y)}Xe^{tY}.
\end{align}
Comment: I don't think this answer is correct for a non-matrix group.  What do you mean by multiplying a Lie algebra element by a Lie group element in general?
Comment: @JasonDeVito, Asma absolutely. This works for matrix groups as suggested by F.Conrad in the comments.
Comment: Thanks for your answer @Icv.I'm wondering why is  $e^{tY} Y$ equal to $  Ye^{tY}$ (I know that it holds if $G$ is matrix Lie group, but don't know why it is true for an abstract Lie group) , could you elaborate further please ?


Have over 200k rows in table, script takes over 20 seconds to load

Question: I've been editing a script for a company that had this application specifically developed for their business, now they have came to me and wanted some upgrades. The entire application is in PHP and MySQL, minus a couple Python scripts to import 200k records daily to the database. My problem is that I need to allow the ability to categorize and edit notes on each record depending on it's event type. The only way to do this is by the URL embedded within each record, for it's the only truly unique value. Successfully figured this out, but now the page script takes forever (24 secs) to load.
Could someone please assist me in optimizing this bit of code?
$notesq = mysql_query("SELECT * FROM `campaign_event_detail_v2` WHERE `call_recording_url`<>'' AND `event_type_name`='Call'") or die(mysql_error());
while($cnD = mysql_fetch_array($notesq)) {
$callid=$cnD[0];
$getD = mysql_query("SELECT campaign_notes.note, campaign_categories.category FROM campaign_notes LEFT JOIN campaign_categories ON campaign_notes.cid = campaign_categories.cid WHERE campaign_notes.cid='".$cnD['call_recording_url']."' OR campaign_categories.cid='".$cnD['call_recording_url']."'");
$getData = mysql_fetch_row($getD);
    mysql_query("UPDATE `campaign_event_detail_v2` SET `note`='".$getData[0]."',`category_id`='".$getData[1]."' WHERE `id`='".$callid."'");

}

Your help is greatly appreciated!
Thanks,
J
Comment: Loop of Death... replace `SELECT *` with `SELECT call_recording_url`, rename `mysql_fetch_array` to `mysql_fetch_assoc` (you are duplicating fetched data with array eg `$data[0]` and `$data[id]` contain same data in two variables).
Comment: Try to avoid `SELECT *`. Check your indexes too.
Comment: I'm a little novice when it comes to building advanced MySQL queries, care to elaborate?
Comment: @user1236482: Well, it seems you only need `id` and `call_recording_url` from `campaign_event_details_v2`. So, instead of doing `SELECT * FROM ...`, you should do `SELECT id,call_recording_url FROM ...`. Also, try to make `call_recording_url` NULL if there's no data, it'll speed up the table lookup. And `event_type_name` could probably use an index.
Comment: They mean to list the columns you need instead of using *. And setup indexes on your database to speed lookups up by A LOT.
Comment: Okay, gotcha. I edited the first bit, and still have speed issues... creating the index, will that alter the table in any way that would cause malfunction of the script? I'm worried because this is an extremely complex application...
Answer: Something like this:
SELECT campaign_notes.note,
   campaign_categories.category
FROM campaign_event_detail_v2 AS detail
LEFT JOIN campaign_notes ON campaign_notes.cid = detail.call_recording_url
LEFT JOIN campaign_categories ON campaign_notes.cid = campaign_categories.cid
          AND campaign_notes.cid = detail.call_recording_url
WHERE detail.call_recording_url<>''
AND detail.event_type_name='Call'
AND campaign_notes.cid IS NOT NULL -- filters any from campaign notes that are empty
AND campaign_categories.cid IS NOT NULL -- filters any from campaign categories that are empty;

This grabs the urls from notes and categories and filters that match the call_recording_url.  It then filters out any that didn't match in the left join (the is null check).  If that is still too slow, there are other ways of optimizing.  Then you should be able to loop through and do your update statement.
I would also try to index the ids you are joining on, especially the call_recording_url if that is a string, which takes longer to search on.
Answer: It seems like you could improve this query:
SELECT campaign_notes.note, campaign_categories.category
FROM campaign_notes
LEFT JOIN campaign_categories
  ON campaign_notes.cid = campaign_categories.cid
WHERE campaign_notes.cid = {$cnD['call_recording_url']}
  OR campaign_categories.cid = {$cnD['call_recording_url']}

To this:
SELECT campaign_notes.note, campaign_categories.category
FROM campaign_notes
LEFT JOIN campaign_categories
  ON campaign_categories.cid = campaign_notes.cid
WHERE campaign_notes.cid = {$cnD['call_recording_url']}

Answer: I think you can probably manage this in a single query:
UPDATE campaign_event_detail_v2 d
LEFT JOIN campaign_notes n ON n.cid = d.call_recording_url
LEFT JOIN campaign_categories c ON c.cid = n.cid
SET d.note = n.note, d.category_id = c.category
WHERE d.call_recording_url != '' AND d.event_type_name = 'Call'

I'm not 100% sure if this is the proper logic, from what I understood it is. I must apologize if it's not. But, my point being: you can probably make it all in a single query.
You probably should add indices on columns like event_type_name, category_id and cid, if they're not already there. It will not affect your scripts, but will take some time to execute depending on how many records you have in your table(s).
Also, it probably would be better to use triggers instead of executing this at each request.
Answer: I would create a stored procedure for this and i would create some new indexsed depend on the queryies (for example one index on: call_recording_url, event_type_name).
And -- of course -- you can create a nicer query from the two first by mixing them to only one.
Comment: What's the reasoning behind creating a stored procedure?
Comment: Peter, what would you recommend for mixing them to only one?
Comment: Stored procedures are compiled and the database server don't have to create execution plan on runtime and you don't have to access the database from PHP code 1 + 1 + many times just only once. @ITJoeNC: I don't know the exact structure of your tables.


502 Bad Gateway - Linux, php7, nginx. Como resolver esse erro?

Question: Estou tendo um erro 502 que representa alguma configuração no servidor, quando tento abrir um site usando linux, nginx e php7.
Pesquisando alguns outros POSTS, link sobre o mesmo erro vejo que o php precisa do fpm para ser reconhecido no nginx. Porém o fpm está instalado e atualizado. Veja foto abaixo:

Segundo o erro 505 tem algo acontecendo no servidor. Não deve ser o php-7 (eu acho).
Alguém tem alguma idéa do que poderia ser?
Atualizando
E finalmente parece que é um erro devido ao PHP. Testei com um arquivo html e nao tive problemas. Então parece que tem algo com o php mesmo. O que pode ser?

Últimos logs
2016/07/06 06:14:56 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:15:04 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:15:04 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:16:29 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

Comment: É, parece que ele está procurando um arquivo do php5-fpm e nao está achando. Mas eu estou usando com php-7 que já possui o fpm. Alguma idéia?
Comment: isso mesmo. Vc quase acertou. no lugar de mudar na pasta `default` eu mudei na pasta do projeto, no caso `ig.app.conf`. Uma nota: na instalação do php7 do linux 16.04 LTS através do  `apt install php-dev ` ou  `apt install php ` (que no linux 16.04 vai instalar php7.0 como default), notar que depois da pasta `/var/run/` ainda existe mais um diretorio `/php`. Ou seja, o caminho completo é `/var/run/php/php7-fpm.sock` e não  `/var/run/php7-fpm.sock`. POsta sua resposta para receber os créditos.
Comment: Pode colar o log do nginx?
Comment: No arquivo de configuração `/etc/nginx/sites-available/default` você atualiza  de `fastcgi_pass unix:/var/run/php5-fpm.sock;` para `fastcgi_pass unix:/var/run/php7-fpm.sock;`
Comment: Muito bom! :) Coloque isso como resposta porque alguém procurando por esse problema na página principal vai ver que não resposta e pode nem clicar
Answer: Nginx se comunica com PHP-FPM usando um soquete de domínio Unix. Sockets mapeiam para um caminho no sistema de ar.
Versão 5.x do PHP usava o caminho /var/run/php5-fpm.sock
Deve ficar assim:
fastcgi_pass unix:/var/run/php/php5-fpm.sock;

Já para PHP7.x o caminho é esse /var/run/php/php7.4-fpm.sock (exemplo, troque o .4 por outra se sua versão for diferente) e deve ficar assim:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

A configuração do site pode ser encontrada em diferentes pastas dependendo do sistema que usa. Então pesquise caso não encontre.
Nesta resposta usaremos um dos caminhos padrões.
Para editar o arquivo de configuração você pode usar nano (ou outro editor), é necessário usar sudo/su (dependendo se é baseado em Debian ou em Ubuntu), exemplo:
> sudo nano /etc/nginx/sites-available/default

server {
    listen 80;
    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    server_name example.com;

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

As configurações do SOCKET podem variar devido a versão do NGINX, como também a versão do sistema operacional. A versão do PHP pode também interferir.
Caso o seu servidor não tenha nenhuma referencia em arquivos padrão.
Pesquise sempre citando o sistema operacional.
Note que a "versão" do Socket é sempre alterada, juntamente com a versão do PHP-FPM. Mas não necessariamente o numero como no exemplo deve estar presente.


Multiple occurance issue in XML Parsing using hive

Question: I'm trying to create an Hive table out of XML file using com.ibm.spss.hive.serde2.xml.XmlSerDe. This works quite good for the tags with single occurrence. But I've an issue with multiple occurrence.
Below is my source XML.
<Item>
      <TimeStamp>2016-02-19T12:27:06.387Z</TimeStamp>
      <AlsoSeen End="2014-08-21T13:44:32.557Z" Start="2014-08-21T13:44:04.637Z" />
      <AlsoSeen End="2014-08-21T13:44:33.557Z" Start="2014-08-21T13:45:04.637Z" />
      <AlsoSeen End="2014-08-21T13:44:34.557Z" Start="2014-08-21T13:46:04.637Z" />
      <Title ID="112031424">FAULT IN OUR STARS, THE</Title>
      <FileName>The Fault in Our Stars (2014) EXTENDED HDRip x264 AAC-CPG</FileName>
</Item>

Below is my Hive Table DDL
add jar hivexmlserde-22.214.171.124.jar;

CREATE EXTERNAL TABLE xml_test
(
         Item_TimeStamp String
         ,Item_AS_Start  String
         ,Item_AS_End    String
         ,Item_Title     String
         ,Item_ID        String
         ,Item_Artist    String
         ,Item_Author    String
         ,Item_FileName  String
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES (
"column.xpath.Item_TimeStamp"="/Item/TimeStamp/text()",
"column.xpath.Item_AS_Start"="/Item/AlsoSeen/@Start",
"column.xpath.Item_AS_End"="/Item/AlsoSeen/@End",
"column.xpath.Item_Title"="/Item/Title/text()",
"column.xpath.Item_ID"="/Item/Title/@ID",
"column.xpath.Item_FileName"="/Item/FileName/text()"
)
STORED AS
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
LOCATION '/user/xxxxxx/XML_text'
TBLPROPERTIES (
"xmlinput.start"="<Item>",
"xmlinput.end"="</Item>"
);

When I query the table I get the below value
2016-02-19T12:27:06.387Z <string>2014-08-21T13:44:04.637Z2014-08-21T13:45:04.637Z2014-08-21T13:46:04.637Z</string> <string>2014-08-21T13:44:32.557Z2014-08-21T13:44:33.557Z2014-08-21T13:44:34.557Z</string> FAULT IN OUR STARS, THE 112031424 The Fault in Our Stars (2014) EXTENDED HDRip x264 AAC-CPG

But I expected output should be 3 different lines for 3 difference occurances of AlsoSeen tag as below
2016-02-19T12:27:06.387Z  2014-08-21T13:44:04.637Z 2014-08-21T13:44:32.557Z FAULT IN OUR STARS, THE 112031424 The Fault in Our Stars (2014) EXTENDED HDRip x264 AAC-CPG
2016-02-19T12:27:06.387Z  2014-08-21T13:45:04.637Z 2014-08-21T13:44:33.557Z FAULT IN OUR STARS, THE 112031424 The Fault in Our Stars (2014) EXTENDED HDRip x264 AAC-CPG
2016-02-19T12:27:06.387Z  2014-08-21T13:46:04.637Z 2014-08-21T13:44:34.557Z FAULT IN OUR STARS, THE 112031424 The Fault in Our Stars (2014) EXTENDED HDRip x264 AAC-CPG

Can anyone please help me?
Comment: I don't know anythig about that SerDe but the expressions sure look like XPath; so `/Item/AlsoSeen[2]/@Start` would get you the attrib of  2nd occurrence of element `` inside each element ``. But AFAIK you cannot iterate with XPath, you would need Java code -- or an XSLT stylesheet.
Comment: BTW, from the results, I guess the SerDe finds the "most granular common element" for all XPath expressions, assumes it means **one record**, and starts with spliiting the source XML in "records" before feeding each XPath expression into a column (i.e. a cell).
Comment: So I would advise you to **transform that XML before loading it into Hadoop**. With Linux utility `xsltproc` and a simple XSLT script, you could get a CSV with 1 column for all your "start" attributes separated by a space *(for a custom separator you would need an XSLT 2-compliant parser i.e. Saxon)* -- then you could read the CSV in Hive and "explode" the array into multiple rows. Trickier with multiple arrays but can be done with "indexed explode", and filtering out the cartesian mess by selecting only Start and End values with matching indexes.
Comment: @SamsonScharfrichter You're right, it's Xpath. Xpath supports wildcards like `*` , `@*` and `node()` which will match multiple values. SerDe is a Java library convention for HIVE that SERializes and DEserializes data, and there are SerDe for many file formats. Hive can use either 'delimited by' or SerDe to define file formats. In this case, the XML SerDe will do the iterating. The source code for this is here: https://github.com/dvasilen/Hive-XML-SerDe . I am suprised that this example worked though, see my answer below.
Answer: The documentation has some examples for this.
https://github.com/dvasilen/Hive-XML-SerDe/wiki/XML-data-sources
Using text() or @Elementname gives you single (primitive) values, and also, your table has been declared as all STRING columns, you may need complex types to hold multiple values, i.e. Map, Struct or Array.
The behaviour you are seeing relates to (5. Complex Types) in the documentation in the link above.

"Complex content being used as a primitive type will be converted to a
  valid XML string by adding a root element called <string>"

In your example, this result you are seeing is the 3 values for the @Start attribute concatenated and then wrapped with the <string> tag:
<string>2014-08-21T13:44:04.637Z2014-08-21T13:45:04.637Z2014-08-21T13:46:04.637Z</string>

Your XPath is using @Start and @End, not the /* pattern though, so I am surprised it worked at all.
I understand the results you want, but I am not sure that it is really matching the way the data is modeled. I think that <Item> is effectively a single row. Item has a <TimeStamp>, and a <Title> which has an @ID attribute, and it also has a property called <AlsoSeen> which can have multiple values for @End & @Start. A straight mapping from XML to Hive table would probably be this complex datatype:
 ARRAY<STRUCT<End: TIMESTAMP, Start: TIMESTAMP>> , an Array of Structs containing End and Start timestamps.
Unfortunately your data doesn't conform to the String format that Hive expects for timestamps, so you can just use STRING, as per https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types#LanguageManualTypes-timestamp
So you would end up with this table create statement (I've removed the Item_Artist and Item_Author in your original create table because you had no matching xpath for those columns in the SERDEPROPERTIES )
CREATE EXTERNAL TABLE xml_test
(
         Item_TimeStamp String
         ,Item_AlsoSeen ARRAY<STRUCT<End: STRING, Start: STRING>>
         ,Item_Title     String
         ,Item_ID        String
         ,Item_FileName  String
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES (
"column.xpath.Item_TimeStamp"="/Item/TimeStamp/text()",
"column.xpath.Item_AlsoSeen"="/Item/AlsoSeen",
"column.xpath.Item_Title"="/Item/Title/text()",
"column.xpath.Item_ID"="/Item/Title/@ID",
"column.xpath.Item_FileName"="/Item/FileName/text()"
)
STORED AS
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
LOCATION '/user/xxxxxx/XML_text'
TBLPROPERTIES (
"xmlinput.start"="<Item>",
"xmlinput.end"="</Item>"
);

Note the xpath I used for the AlsoSeen field: "/Item/AlsoSeen"
This is the pattern for Arrays and Structs as in the documentation linked above.
It will give you results like
+--------------------+--------------------+--------------------+---------+--------------------+
|      Item_TimeStamp|       Item_AlsoSeen|          Item_Title|  Item_ID|       Item_FileName|
+--------------------+--------------------+--------------------+---------+--------------------+
|2016-02-19T12:27:...|[[2014-08-21T13:4...|FAULT IN OUR STAR...|112031424|The Fault in Our ...|
+--------------------+--------------------+--------------------+---------+--------------------+

The Item_AlsoSeen column contains this, an Array () of Structs [] separated by commas, each struct has the end and the start.
WrappedArray([2014-08-21T13:44:32.557Z,2014-08-21T13:44:04.637Z], [2014-08-21T13:44:33.557Z,2014-08-21T13:45:04.637Z], [2014-08-21T13:44:34.557Z,2014-08-21T13:46:04.637Z])

From there, you can query the result set you wanted by using the  LATERAL VIEW explode() technique
e.g.
SELECT
Item_TimeStamp,
    --Item_AlsoSeen,
    ias.End,
    ias.Start,
    Item_Title,
    Item_ID,
    Item_FileName
FROM xml_test
    LATERAL VIEW explode(Item_AlsoSeen) ias as ias

Note that because I created AlsoSeen as an Array of Struct, the explode() function returns each struct as a row. LATERAL VIEW then effectively does a cross join or probably better called a CROSS APPLY to get a cartesian product.
+--------------------+--------------------+--------------------+--------------------+---------+--------------------+
|      Item_TimeStamp|                 End|               Start|          Item_Title|  Item_ID|       Item_FileName|
+--------------------+--------------------+--------------------+--------------------+---------+--------------------+
|2016-02-19T12:27:...|2014-08-21T13:44:...|2014-08-21T13:44:...|FAULT IN OUR STAR...|112031424|The Fault in Our ...|
|2016-02-19T12:27:...|2014-08-21T13:44:...|2014-08-21T13:45:...|FAULT IN OUR STAR...|112031424|The Fault in Our ...|
|2016-02-19T12:27:...|2014-08-21T13:44:...|2014-08-21T13:46:...|FAULT IN OUR STAR...|112031424|The Fault in Our ...|
+--------------------+--------------------+--------------------+--------------------+---------+--------------------+

Ref Hive Datatypes: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types
Answer: There are custom written UDF's which are array_index , numeric_range solves the problem easily. In order to use the functions the column type should be an Array. Please refer the post Hive Explode / Lateral View multiple arrays


Guzzle responses from POST on REST API are mixed

Question: I develop a custom module to store and retrieve users data from an API. I used the user entity hooks to call the API on registration, edition and consultation of the Drupal users.
It works well, and we use it on several website all using the same partner API.
To update or save the data, we use a POST method with the fields in the body and without URL parameters.
In some cases, when 2 users are registering or saving their account at the same time (max 3 seconds delay), then the information return in the POST response of the second call is the same as the first one.
*** Edit ***
I forget to say, after each POST the page is reloaded and their is a GET on the API using the id_contact that I store on the user data (states). I'm not sure if this is relevant.
*** /Edit ***
Here is the method I'm using to call the API:
/**
 * Make the Calls to the API.
 *
 * @param string $function
 *   The API endpoint to call.
 * @param string $method
 *   The method (GET / POST / ...).
 * @param array $data
 *   The data to POST to the API.
 *
 * @return array
 *   Array of data from the API, can be empty if error or no results.
 */
public static function callApi($function, $method = 'GET', array $data = []) {
  $srv_url = 'https://someapi.com/';

  if ($srv_url === '/') {
    return FALSE;
  }

  $headers = [
    'Content-Type' => 'application/json;charset=UTF-8',
  ];

  $token = self::getToken($srv_url);
  if ($token === FALSE) {
    return FALSE;
  }
  elseif ($token !== TRUE) {
    $headers['Authorization'] = 'Bearer ' . $token;
  }

  $options = [
    'headers' => $headers,
  ];
  $full_url = $srv_url . $function;

  if ($method === 'POST') {
    $options['body'] = Json::encode($data);
  }
  else {
    $full_url .= '?' . UrlHelper::buildQuery($data);
  }

  try {
    $client = \Drupal::httpClient();
    $mthd = strtolower($method);
    $response = $client->$mthd($full_url, $options);

    $statusCode = $response->getStatusCode();
    // Expected result.
    $jresp = Json::decode($response->getBody());

    // No result (GET).
    if ($statusCode == 204) {
      return [];
    }

    if (isset($jresp['code']) || !in_array($statusCode, [200, 201])) {
      // Something went wrong.
      $code = isset($jresp['code']) ? $jresp['code'] : $statusCode;
      if (isset($jresp['error'])) {
        $msg = $jresp['error'];
      }
      elseif (isset($jresp['message'])) {
        $msg = $jresp['message'];
      }
      else {
        $code = $statusCode;
        $msg = $response->getReasonPhrase() . '
' . $response->getBody()->getContents();
      }
      $message = "Response API {$method} - {$function} : {$code}
{$msg}
" . print_r($response, TRUE) . '
';
      \Drupal::logger('mymodule')->error($message);
      return [];
    }

    // Log posted result.
    if ($method === 'POST' && $function === 'contact') {
      AtixTools::debug('atix_crm_cible', ["Result API {$method} - {$function}" => ['options' => $options, 'resp' => $jresp]]);
    }
    if (isset($jresp['body'])) {
      return $jresp['body'];
    }
    return $jresp;
  }
  catch (RequestException $e) {
    watchdog_exception('mymodule', $e);
  }

  return [];
}

Here are an example of two calls where the data have been mixed. In the arrays are the options used for the POST request and the Body answered by the API.
Array
(
    [options] => Array
        (
            [headers] => Array
                (
                    [Content-Type] => application/json;charset=UTF-8
                    [Cache-Control] => no-cache
                    [Authorization] => Bearer XXXXXXX
                )

            [body] => @"email":firstname.lastname@example.com,"sms":"","nom":"ttf-test54","prenom":"ttf-test54","date_de_naissance":"","code_postal":"","ville":"","langue":"fr","id_filiale":9
        )

    [resp] => Array
        (
            [0] => Array
                (
                    [langue] => fr
                    [nom] => ttf-test54
                    [upd_date] => 2021-08-26 14:18:22
                    [id_contact] => 53550
                    [prenom] => ttf-test54
                    [email] => firstname.lastname@example.com
                )
        )
)

The second one has the same answer
Array
(
    [options] => Array
        (
            [headers] => Array
                (
                    [Content-Type] => application/json;charset=UTF-8
                    [Cache-Control] => no-cache
                    [Authorization] => Bearer XXXXXXX
                )

            [body] => @"email":firstname.lastname@example.com,"sms":"","nom":"ttf-test55","prenom":"ttf-test55","date_de_naissance":"","code_postal":"","ville":"","langue":"fr","id_filiale":9
        )

    [resp] => Array
        (
            [0] => Array
                (
                    [langue] => fr
                    [nom] => ttf-test54
                    [upd_date] => 2021-08-26 14:18:22
                    [id_contact] => 53550
                    [prenom] => ttf-test54
                    [email] => email@example.com
                )
        )
)

The people from the API assure me their is no cache on the server side, specially for a POST. The API is developed just for this purpose and is hosted on AWS (Amazon).
How is it possible that 2 calls to the API using Guzzle from two web pages are giving back the same set of data ?
Is their a way Drupal or Guzzle are caching the response from the server ?
I looked a lot to find a solution to resolve this trouble, try using no cache header (like in this example), try reproducing it with Postman or a scenario in integromat without any success.
Thanks for some help.
*** Edit2 ***
I had confirmation some data are mixed between 2 sites, which means it must come from the server side and the provider is working on a fix.
I choose to select the answer of @Yuseferi as good, even if it is not the case here, because it could have improve my code and I learn about ResourceResponse.
Comment: Sounds like Drupal is caching the Response.
Comment: It's not sure after all, I just cross some data of the API database, and some of the contact id we store in drupal are mixed also between sites. It's mean it must be the API server responding 2 times the same data using cache. I'm waiting for confirmation.
Answer: I think it's drupal cache,
instead of return $jresp;
try
$build = array(
  '#cache' => array(
    'max-age' => 0,
  ),
);

return (new ResourceResponse($jresp))->addCacheableDependency($build);

or
return new ModifiedResourceResponse($jresp, 200);

note: of course do not forget to add this classed in use section ;)
Comment: I don't use render on this method, it is used on a form submit call, and I was thinking caching apply to rendered elements, no ?
Comment: I didn't knew ResourceResponse, it's seems interesting and I'm going to try.
Comment: @TytooF did you try my solutions? I think the problem is caching.
Comment: No I didn't try the proposed solution, I had confirmation that the trouble was part of the server side API.


Detect vertical scroll has already ended and the user is pulling

Question: How to detect when user has already reached the end of NestedScrollView and in his current scroll attempt motion is pulling?
I've tried some patterns with detecting end scroll but that's far from what I am searching to achieve.
What I came to as well was that I can override public class  CoordinatorLayout.Behavior and play with scroll methods however I am happy with current @string/appbar_scrolling_view_behavior and I am not quite sure how to proceed.
view structure
<android.support.design.widget.CoordinatorLayout>

    <android.support.v4.widget.NestedScrollView
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

Anybody familiar with the issue?
Answer: You can detect the end of NestedScrollView like that
svProduct.getViewTreeObserver()
        .addOnScrollChangedListener(() -> {
          View view = svProduct.getChildAt(svProduct.getChildCount() - 1);
          int diff = (view.getBottom() - (svProduct.getHeight() + svProduct.getScrollY()));
          if (diff == 0) {
            // reached to bottom
          }
        });


How to tag content by users and filter the content in a view?

Question: I have a content type Meeting Minutes. I'd like to tag users in the minutes. There will be a view to the list of minutes only for the logged in user.
For example:

Meeting 1, tags Peter, Mary
Meeting 2, tags Paul, Mary
Meeting 3, tags Peter

When Mary logs in, the view shows:

Meeting 1
Meeting 2

When Peter logs in, the view shows:

Meeting 1
Meeting 3

Is there a user tags module for a content type and good for view-filter?
Answer: Add entity reference field in Meeting Minutes use entity type user and then tag users from there with you  content of Meeting
now in views
use Contextual filters
add Contextual filters select you user reference field like if your field name is
Content: user in meeting

then select
Provide default value

from check boxes a select list will be appear then select from them
User ID from logged in user

and done
Now when page load content should be filtered with current user id
i think this will solve your problem