Do occlusion queries respect the stencil test at all?
The specification has been clarified on this point, as has the Wiki article. So let's explain that.

The new wording in the spec says:


  the samples-passed count is incremented for each fragment still being processed after the depth test


This is better wording, as it makes clear that the order in which things are processed matters. OpenGL defines the depth test as happening after the stencil test, scissor test, etc. So this wording clearly states that the depth test, and everything before it, matter.

So it does respect the stencil test. And fragment shader discard, since the FS is specified to come before all of these tests.

Well, unless you force these tests to happen before the fragment shader. If you do that, then discarding the fragment in the FS will not prevent that fragment from being counted by the occlusion query. So if you enforce early tests, you lose the right to use discard to affect the occlusion query count.