Heuristics to discover spammers/bots (In forums, blogs etc)

Question: The ways I can think of are:

Measure the time between actions.
Compare the posts' content (if they're too similar to each other) or, better yet, only the posted links.
Checking the distribution over a period of time the user is active (if the user is   active, say posting once every hour, for a week, then either we have a superman or a bot here).
Some special activity expected: like in stackoverflow, I would expect users to press their user name link (top middle) to see their new answers, comments, questions etc.
(added by chakrit) Number of links in a post.
Not heuristic. Use some async JS for user login. (Just makes life a bit harder on the bot programmer).
(added by Alekc) Not heuristic. User-agent values.
And, How could I forget Google's approach (mentioned down by Will Hartung). Give users the ability to mark someone as Spam, enough Spam votes means this is a Spam user. (calculating what is enough users, is the work here).

Any more ideas?
Comment: not really an answer, but i've seen people cut down a significant portion of comment/forum spams by just blacklisting subnets in .ru and .cn  .. unless you have readers from those countries.
Answer: I might be over estimating the intelligence of bot creators, but number 6 is completely useless against any semi decent bot creator. Using the C# browser control to create your bot would pretty much render 6 useless. From what I've seen with that type of software that's a pretty common approach.
Validating on the useragent is pretty much useless too all of the blog spam I use to get was from bots appearing to be valid web browsers.
I use to get a lot of blog spam. I would literally be deleting hundreds of comments a day. I made use of reCaptcha and now I might get  1 a month.
If you really try to make something like this. I would attempt by doing the following:
User starts off with no ability to post a url.
After X number of posts have been analyzed in relation to the other posts in the  thread then give them access to post urls.
The users activity on the site, the post quality, and what ever other factors you deem necessary will be a reputation for that users IP.
Then based the reputation of the IP and the other IPs on the same subnet you can make other decisions on whatever you want.
That was just the first thing that came to mind. Hope it helps.
Comment: +1 The ideas to restrict new users from posting urls, is a very good one, for my personal case. Not sure as to the generality of it.
Answer:
The number of links in a post.

I believe I've read somewhere that Akismet use the number of links as one of its major heuristics.
And most of spam comments at my blog contains 10+ links in them.
Speaking of which... you just might want to check out the Akismet API itself .. they are extremely effective.
Comment: +1. Links in post is a pretty good one. Again, you could cross refernce those with an blacklist from say Spamhaus.org
Answer: How about a search for spam related keywords in the post body?
Not a heuristic but an effective approach: You can also keep up-to-date with the stats published by StopForumSpam using their APIs.
Answer: Time between page visits is common I believe.
I need to add a comment section to my personal site and am thinking of asking people to give me their email address; I'll email them a "publish comment" link.
You might want to check if they've come from a Spam blacklist IP address (See http://www.spamhaus.org/)
Comment: Spamhaus looks promising :-) ... but I think I've had quiet a bad experience with blacklisting and proxies... :-(
Answer: There is another answer that suggests using Akismet for detecting spam, which I completely endorse.
However, they are not the only player on the block.
There is TypePad AntiSpam which uses the same heuristics as Akismet, as well as the same API (just a different URL and api key, the structure of the calls is the same).  It can be safe to say they pretty much take the same approach as Akismet.
You might also want to check out Project Honeypot.  From what I can tell, it can do a lookup based on the IP address of the user, and if it is a known malicious IP, it will tell you (harvester or something like that).
Finally, you can check LinkSleeve which approaches comment spam with what it claims to be a different way.  Basically, it checks the links that are being linked to in comments, and based on where the links are going to, makes a determination.
Comment: Honeypot is rather filtering too aggressively. I've been blocked out of my own website once because of my ISP's proxy address was included in the list... totally a bummer for me :-( ... but +1 anyway :-)
Answer: Don't forget the ultimate heuristic: The "Report Spam" button that users can click. If nothing else, this gives you as administrator a chance to update your rule base for stuff that may be slipping through. Of course, you can simply delete the offending post and user right away as well.
Comment: Note that this can be an abuse vector, most obviously if you let a user vote many times or allow votes from anonymous users - malicious users can use it to silence people they disagree with. To prevent this, consider manual review (possibly by trusted users) or at least an appeals process.
Answer: I have some doubts about 4° point, anyway i would also add User-Agent. It's pretty easy to fake, but in my experience, about 90% of bots are using Perl as UA
Comment: 4..This is heuristic, and one that is the most hard to implement, but you can gain from such a system much more then just spam filter (A system that tracks users behavior on your site).
Comment: Yep, i meant that it's pretty hard to track down behavior patterns. I.e. here on stack overflow one can just navigate around the questions without clicking on their profile.
Answer: I am sure there is a webservice of some kind that you can get a list of top SEO keywords, check the content for those keywords. if the content is to rich in keywords suspect it as being spam.


Manipulate Html table with Angular

Question: very simple
what is the equivalent of this jQuery code
$('#message tr').eq(index).addClass('negative') in angular,
or how can i achieve the same result with angular
can't use [class.bind] the above job should be done in by clicking the button.
the button will pass some data to the function and based on some condition and checking the negative class will be added to the related row not to all of them.
Comment: Angular works using variables you define in .ts and use in your .html. So, in general you not select the html element using jQuery selector, else in your .html you binding the atributes to variabes and change this variables. e.g. declare a variable `indexSelected:number=-1` and use in .html `click` and `..`
Comment: the solution you provide will not work, because suppose my html table has 10 rows and by clicking the button which is `(click)="diifer(data)"` the data inside differ method is a collection and inside the component this will iterate and find the difference between values and it find then the negative class will be added to related row which each td has different values or at least on td has different value amont other, not to all 10 rows
Comment: I add an answer to clarify my comment.
Answer: You have not added any code, so I imagine you have something like:

    {{data?.prop1}}
    {{data?.prop2}}
    <button
        (click)="indexSelected=(indexSelected==i)?-1:i;
                      indexSelected!=-1 && diifer(data)">
        select
        </button>

(or instead use a button add the "click" to the
)
You can also pass the "index" to the function diifer like
(click)="diifer(data,i)"

And
diifer(data:any,index)
{
   this.indexSelected=this.indexSelected==index?-1:index
   if (this.indexSelected!=-1)
   {
       ...do something..
   }
}

NOTE: See that you check if indexSelected is the "row" you select. If true, you "unselect" making indexSelected=-1
NOTE2: remember that index goes from 0 to arrayData.length-1

Manipulate Html table with Angular


Can Poisson deviance be used to evaluate models that use loss functions other than Poisson? (Such as MSE)

Question: I am currently doing a a study on emergency department utilization rates at various geography levels. Especially of interest, are tree-based approaches to this analysis - namely random forest and GBMs.
I've built some models using Sklearn RandomForestRegressor and HistGradientBoostingRegressor, both of which I've run using Poisson loss. For evaluation metrics, mainly I have been concerned with proportion of deviance explained (D squared), and mean Poisson deviance.
However, to add some robustness I would also like to run some models that use MSE as loss and compare the performance to the Poisson-based models. I know this makes some false distributional assumptions - correct me if I am wrong - but given that the tree estimators are non-parametric, I don't believe this is entirely disingenuous.
I know that "raw" error metrics could be viable here (i.e., MAE, MSE, total error, etc.), but I want to maintain the Poisson-specific metrics if possible. So, could I still use Poisson deviance/deviance explained when MSE is the loss? Is there a statistical explanation for why or why not?
Comment: Poisson distr with lambda~7(10) is almost Normally distributed == I think, after optimization with MAE loss_fx, poisson_deviance can be used - more generally called [tweedie_deviance](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_tweedie_deviance.html) of [Tweedie distribution](https://en.wikipedia.org/wiki/Tweedie_distribution)
Comment: [HERE](https://stats.stackexchange.com/a/502836/347139) is a good answer & my understanding: loss_function, calculating residuals, can be used for minimization of metric of these residuals. And Score of this optimization (here, minimization of metric) process prefferably having MSE-nature (Brier_score e.g.) -- important, I think, if dataset is balanced! - in such a case MSE is a good measure of approximation result (as have wrote above - only if lambda of Poisson RV shows Normality - such loss_fx is ok to use, i thik). And, yes, you'd better clearly understand Loss_fx, Metric & Score meaning.
Answer: This is fine. You are just using a different estimator of your outcome (MSE-based instead of Poisson-based) that is evaluated on a particular loss function that happens to be related to the Poisson distribution.
It is routine to optimize loss functions other than the ultimate loss function of interest. For instance, ridge regression optimizes a function that somewhat differs from square loss, typically in an attempt to obtain a better out-of-sample square loss. In this answer I posted a few weeks ago, I give a situation where optimizing absolute loss results in better (out-of-sample) square loss, and the same idea applies to your situation.


Taxonomy exposed filter with parent hierarchy only

Question: I have a taxonomy vocabulary with terms in hierarchical order. If I use an exposed filter, with level depth, all terms are listed, which are > 6,000. I want only the parent terms, which are only around 30.
I found here the following code (I adapted it to my case). It is an answered question, but obviously it does not apply efficiently to my case, because though it does what is suppose to do, it goes through every item and as a consequence is extremely slow (several seconds). Is there a better way that does not directly make a db_query to the taxonomy database tables or use a hook (PHP) ?
function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] == 'views-exposed-form-search-page-page'{
    foreach ($form['term_node_tid_depth']['#options'] as $term_key => $term) {   // Check if this isn't the 'ANY' option
      if($term_key !== 'All') {
        // Check if this is a child by looking for '-' as first char in string
        $term_value = reset($term->option);
        if($term_value[0] == "-") {
          unset($form['term_node_tid_depth']['#options'][$term_key]);
        }
      }
    }
  } // if
} // function

Answer: I had a situation like this, i done it by giving a custom options array to the exposed filter in hook_form_alter().
 function mymodule_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "views_exposed_form" && $form['#id']=='your-form-id' ){
 $tid_options = get_tid_options();

 $form['your-field-name']['#type'] ='select';
 $form['your-field-name']['#size'] = null;
 $form['your-field-name']['#default_value'] = '';
 $form['your-field-name']['#options'] = $tid_options;
   }
}

function get_tid_options() {
  $cc=array();
  $tax=taxonomy_vocabulary_machine_name_load('machine-name');
  $vid= $tax->vid;
  //Code to find parent terms here according to your vocabulary structure.
  $child_terms = taxonomy_get_tree($vid);
  foreach($child_terms as $values)
  $tid=$values->tid;
  $name=$values->name;
  $cc[$tid] = $name;
 }
return $cc;

}
Use taxonomy_get_parents or taxonomy_get_parents_all to find parent terms.
May be this will help you..
Comment: Thanks for your reply. I have used  **taxonomy_get_parents** (_$value->tid_) before, I was out of my mind by saying to get the values at low-level database. In any case, to be honest, only a major change of Drupal version could make such a hard code fail. I really meant if there is another high-level solution, i.e., a configuration in **Views UI**, another module or submodule, etc. Thanks!!
Answer: For listing the zero-level (parent) (or others), it is best to use the Drupal function taxonomy_get_tree
function get_options_of_custom_form() {
    $terms = taxonomy_get_tree(2, 0, 1);
    $options = array();
foreach($terms as $key => $value){
    $options[$value->tid] = $value->name;
} // foreach
  return $options;
} // function


Apache problems in performance test with mod_reqtimeout

Question: We have 2 Apache webserver behind a load balancer wich are connected to 2 (JBoss) application servers via mod ajp.
To those webservers, mobile devices connect via a REST API.
In our performance test we rather quickly ran into a lot of NonHttpResponse: errors which we identified to be coming from mod_reqtimeout:
[Mon Mar 16 14:42:49.324705 2015] [reqtimeout:info] [pid 27914:tid 140628428449536] [client 188.8.131.52:48280] AH01382: Request header read timeout

... which is configured as follows:
<IfModule reqtimeout_module>
    RequestReadTimeout header=10-20,minrate=500
    RequestReadTimeout body=10,minrate=500
</IfModule>

I was able to get rid of those errors by increasing those values to
RequestReadTimeout header=20-60,minrate=100

But this can't be the solution, since with some more simultaneous users the problem occured again (There is a requirement to be able to serve 300 concurrent users - 100 worked quite ok, with 300 we had over 10,000 of those Request header read timeout errors). I suspect it's the interaction of apaches KeepAlive, our mod_ajp configuration and mod_reqtimeout that leads mod_reqtimeout to the conclusion that there is a slowloris attack ongoing (to many open connections that do nothing) and I kindly ask for your help in tweaking those parameters.
Additional problem is a firewall between webserver and application server, which I suspect to kill open idle connections. I read about deactivating KeepAlive completely to solve this, but as I said, all our clients are mobile devices, so that's probably not an option (?).
Here are the other configs (parts of):
workers.properties:
worker.list=server
worker.maintain=60

worker.server.type=ajp13
worker.server.host=server
worker.server.port=15869
worker.server.socket_keepalive=True
worker.server.connection_pool_timeout=600
worker.server.ping_mode=A
worker.server.connection_ping_interval=60

mod_prefork:
<IfModule prefork.c>
    StartServers         5
    MinSpareServers      5
    MaxSpareServers     10
    #MaxClients         256
    MaxClients         300
    MaxRequestsPerChild  0
</IfModule>

mainserver.conf:
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15

ssl.conf (mobile devices connect via ssl):
Timeout 1200
KeepAlive On
MaxKeepAliveRequests 0
KeepAliveTimeout 10

Answer: It is unusual that you are using "prefork" MPM in what seems to be essentially a reverse-proxy, the hybrid "worker" MPM has better scalability, that's not the problem though.
DoS mitigation is usually best handled (if not by your ISP) on the front-end device that terminates the client requests, from your description this is a load-balancer, not Apache. Any competent load balancer will be HTTP aware (i.e. "Layer 7"), this will likely result in buffering of HTTP requests. This also applies if the load-balancer is terminating HTTPS, but less so if the load-balancer is simply relaying the HTTPS connections (as it cannot see the HTTP requests to buffer them). Nor does it apply if your load-balancer is a simple "Layer 3/4" NAT type load-balancer.
As to the possible cause of the timeouts:

your SSL instance KeepAliveTimeout is the same as the RequestReadTimeout header read timeout, it's possible there's a race where keepalive timeout is reached mid-way through the incoming client request/headers. If I try to reproduce this, in addition to AH01382 errors I also get  AH01991 (SSL input filter read failed)and AH00567 (request failed: error reading the headers). This may not explain all the problems though.
malformed client requests, not uncommon in the past (e.g. extra CR/LF after POST, incomplete requests when retrying after error). I don't know of anything current though, it depends on your client base, and more importantly, their connectivity.
there could be a bug similar to this recent one which caused spurious timeouts with "event" MPM.

To reproduce timeouts:
 $ openssl s_client -connect myhost:443
 GET / HTTP/1.1
 Host: myhost.whatever.com

 [server reply goes here]
 GET / HTTP/1.1
 Host:

You can script this to make it easier, otherwise you must type/paste the first request and headers within your configured 10s, then type but not complete the second request within the next 10s, you must have at least one full line (the request) submitted for the second request, then just wait.
Decreasing the KeepAliveTimeout (default is 5 seconds) may help. Note that KeepAliveTimeout is the time to receive a  complete a request.
I think the next step may be mod_log_forensic.
Regarding the connections to the back-end via AJP, are you using "ping" on the Apache Balancer configuration? If I understand your system correctly, the Tomcat configuration you have given won't apply to connections from Apache httpd to Tomcat. See the options here.
                          /-> apache httpd + ajp -\            /-> tomcat/jboss
client -> load-balancer  <                         > firewall <
                          \-> apache httpd + ajp -/            \-> tomcat/jboss

Comment: Wow - thank you very much for your detailed answer! I haven't been able to implement your suggestions yet. I'll be back as soon as I do. Thanks again!!


Remove PDF page breaks

Question: How can I remove page breaks from a pdf, so the output would be a single 'page' PDF? So if a normal page is 400x900 and I have 4 pages, a resulting file would be 1600x900. I previously did this for Tif files (Remove page breaks in multi-page tif to make one long page), but would like to do it with PDF. Could I possibly convert to ps, remove whatever code means 'page break', then convert back to pdf?
Comment: Quite right. I have updated my example to deal with pages of varying dimensions (both in width and height). Added using statement for better memory usage.
Comment: Do you want annotations to also be present on the merged page? If not, @Colton's answer shows the way. Improvement is possible, though, e.g. joining pages with different dimensions is not done properly, and some optimizations concerning the required resources are possible, too.
Answer: This can be done in the iTextSharp library by using a single columned PdfTable and dynamically changing the size of the document dependent upon the number of pages.
You'll of course need a few references to the iTextSharp DLL found here
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

Here's a simple example:
public static void MergePages()
{
    using (PdfReader reader = new PdfReader(@"C:\Users\cmilne\Desktop\AA0081913.pdf"))//Original PDF containing page breaks.
    {
        int pages = reader.NumberOfPages;
        float postProcessPageHeight = 0;
        float postProcessPageWidth = 0;
        for (int p = 1; p <= bill.PageCount; p++)
        {
            var size = bill.PdfReader.GetPageSize(p);
            postProcessPageHeight += (size.Height);
            if (size.Width > postProcessPageWidth)
                postProcessPageWidth = (size.Width);
        }

        var rect = new Rectangle(postProcessPageWidth, postProcessPageHeight);

        using (Document document = new Document(rect, 0, 0, 0, 0))
        {
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Users\cmilne\Desktop\AA0081913_NEW.pdf", FileMode.Create)); //Declare location\name of new PDF not containing page breaks.

            document.Open();
            PdfImportedPage page;
            PdfPTable table = new PdfPTable(1);
            table.WidthPercentage = 100;

            for (int i = 1; i <= pages; i++)
            {
                page = writer.GetImportedPage(reader, i);
                table.AddCell(iTextSharp.text.Image.GetInstance(page));
            }
            document.Add(table);
            document.Close();
        }
    }
}

The ending page size must be smaller than 14400 by 14400. (This is all that iTextSharp allows) An 8 1/2 x 11 PDF at a common resolution would make the max about 18 pages.
Comment: Thanks for adding this answer. I missed OPs comment until today!
Comment: I have updated my example to deal with pages of varying dimensions (both in width and height). Added using statement for better memory usage.
Comment: The code works BUT it didn't solve the problem. It just made the 1st page bigger and there were still two pages. Ugh. Downvote
Answer: Use the iTextSharp C# library.  It gives you a lot of options to manipulate PDFs.  I've used it before when I had to write an import application for a closed-source document repository.  It worked like a charm.  The only downside is their documentation is kind of spotty because they want you to purchase their book.  You can browser their Java API though for free since its almost identical to the C#, and just play around with it to find the C# version.
iText: http://itextpdf.com/
Comment: iTextSharp certainly does have a lot of options for manipulating PDFs, but I'm not seeing the option to remove page breaks. Do you know if it has that option?

Remove PDF page breaks


How to parse JSON into different models based on key?

Question: I need to parse JSON into different models based on one particular key that comes from server for example- if key = "userCoupon", I need to Map this into model called UserCouponDTO and if key = "discountCoupon", I need to map the response into another DTO called DiscountCouponDTO. I am using Alamofire for network call and ObjectMapper for mapping.
Comment: Ok so are you facing any issue? What have you tried? Any answer or tutorial you followed?
Answer: In swift 4 it's very easy to map responses to swift structs using the new Decodable protocol
Have a look at this article: https://grokswift.com/json-swift-4/
An easy example where the server response matches the struct
struct User: Decodable {
  let name: String
  let email: String?
}

In this case the server might response with
{
  "name": "John"
}

let decoder = JSONDecoder()
let user = try decoder.decode(dataFromServer, User.self)

in this case the User object will have name defined and email = nil
if you need to map from snake_case to camelCase you can use coding keys
struct User: Decodable {
  let firstName: String
  let lastName: String
  let email: String

  enum CodingKeys: String, CodingKey {
    case email
    case firstName = "first_name"
    case lastName = "last_name"
  }

  init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    email = try values.decode(String.self, forKey: .email)
    firstName = try values.decode(String.self, forKey: .firstName)
    lastName = try values.decode(String.self, forKey: .lastName)
  }
}

Hope this helps


the kernel trick and linear regression

Question: This question is inspired by this and the answer by Hamed.
Let us say the underlying data generation process is:
 y = 3x + sin(x)

So pretty non linear. I understand that the linear regression only has to be linear in parameters. So by transforming the original IV x into 2 new IVs x1 and x2:
x1=x and x2=sin(x)

one can estimate the parameters using this linear regression formula:
y = alpha * x1 + beta * x2

or in R speak:
lm(formula = y ~ x1 + x2 - 1)

is this correct and is this the kernel trick?
Answer: Is this correct? Yes, except that it's not the kernel trick. There are two equivalent approaches to a regression problem. One is to write the response variable $y$ as a linear combination of basis functions $\phi(x)$ as $y(x) = \sum_i \omega_i \phi_i(x)$, $i=1,2$ (2 in this case, you could have infinite). This is what you are doing, and what you are calling new input variables are the basis functions evaluated at the old variables, $\phi_1(x) = x$ and $\phi_2(x) = \sin(x)$. The weights in that linear combination are what you look for.
The kernel trick is to write the regression problem as $y(x) = \sum_n y_n K(x,x_n)$, $n=1,2,...,N$, where  $N$ is the number of data points and where the kernel function $K$ can be written in terms of $\phi_1$ and $\phi_2$. An alternative is to directly propose a kernel with suitable properties and forget about basis functions $\phi$. One must take into account that training a model based on the kernel approach involves estimating $N$ parameters, probably much more than the 2 you have in the basis function approach (what's the benefit then? is that you don't need to assume any functional form such as $x$ and $\sin(x)$, so the approach is more flexible. Also, while slow for training, is fast for predicting sot that's no problem.)
This (free) book https://www.microsoft.com/en-us/research/uploads/prod/2006/01/Bishop-Pattern-Recognition-and-Machine-Learning-2006.pdf talks about this in great detail (but is not very straightforward to dive in).
Comment: "From a classification point of view, I always thought the kernel trick is to transform the data into a high dimensional space to make them linear separable (see radial basis function networks)", that's what is known kernel trick in that context, I agree
Comment: Thanks. From a classification point of view, I always thought the kernel trick is to transform the data into a high dimensional space to make them linear separable (see radial basis function networks). I thought I do something similar here as I transform x -> x1, x2
Comment: My point was that a kernel approach is something more general, and is at the core of any linear regression problem. When you assume a kernel you are implicitly assuming a set of basis functions (maybe an infinite number of them). When you assume basis functions, you are implicitly defining a kernel form (an this form can be easily obtained with simple matrix operations)
Comment: Thanks. I have Bishop's book as hardcopy. Need to read that part again (-:
Comment: You are welcome. It's a nice book but I find that it's not so easy to just read a single page on it, normally you need a lot of contexts to understand it. I recommend sections 6.1 and 3.3.3. If you found my answer useful, you can mark it with the check mark on the left :)
Comment: I will accept don't worry.
Comment: I think the coefficient for the kernel trick equation should be $a_n$ instead of $x_n$ (which is the training point). One thing about kernel trick is that you have to embed the training points into the model, whilst a simple linear regression model can independent.
Comment: @syockit, indeed there's an error, thank you for pointing that out. Instead of $x_n$ it should be the values $y_n$ (see e.g., equation 3.61 of the book I talked about in the answer).

the kernel trick and linear regression


Webkit marquee only scroll when necessary

Question: I've got a question concerning webkit marquee. I've got 2 elements of a variable width. (The 2 elements are the same width), and both elements need to be a marquee.
However when the content has overflow (is larger then the element) I need a marquee, if not the text should remain as it is (no scrolling).
I've created a JSFiddle as an example:
http://jsfiddle.net/Vxwed/:
The long and short both need to be marquee'd through CSS3, while the long should scroll and the short one doesn't.

Hi my name is John Doe

Yeah!

Remember, the contents of the elements are variable (filled with javascript), so I cant do actual hardcoding on the elements marquee-behaviour.
Any CSS experts here able to help me? I've been researching this a lot but there is little information about this subject, since it's relatively new.
The only solution that I'm able to think of right now is using jQuery to measure the width of the elements, and then calculate if they need extra spacing. If they need apply marquee, else don't. But that doesn't seem very clean to me, I'd rather do this in HTML/CSS only if possible.
Comment: It's admirable that you want to solve this with CSS only -- however sometimes you have to draw a line. In this case I highly recommend finding another solution because as you it is a new property but even more so, at the moment it is a [proprietary property](https://developer.mozilla.org/en-US/docs/Web/CSS/Webkit_Extensions#Proprietary_WebKit-prefixed_entities_(do_not_use_on_Web_sites)) and is only [in draft for a W3C standard](https://www.w3.org/TR/css3-marquee/). EDIT: I only now see that this question is almost 4 years old. Wow.
Comment: You can't handle it by CSS only. You have to use javascript or JQuery to resolve it.
Answer: This probably doesn't do exactly what you want but it was a good problem to look at: http://jsfiddle.net/4hgd8ac1/
It uses CSS animations to animate the transform: translateX percentage as this is based off the width of the element itself. This means we can scroll an element it's full width left. By then giving the marquee a minimum width we can standardise the shorter text lengths. Then we use calc(100% + 100px) move the item 100% left except the width of the carousel (100px).
It doesn't quite have the traditional marquee feel with the text scrolling fully but using the animation keyframes it is possible to pause at the end of the text to give the user time to read.
p {
  height: 30px;
  width: 100px;
  background-color: #CCC;
  white-space: nowrap;
}

.marquee {
  overflow: hidden;
  position: relative;
}

.marquee__content {
  padding: 5px 0;
  margin-right: 100px;
  position: absolute;
  height: 20px;
  animation: scroller 3s linear infinite;
  min-width: 100px; /* This needs to match the width of the parent */
}

@keyframes scroller {
 0% {
     transform: translateX(0%);
 }

 /* 'pauses' the scroller at the start for 20% of the time, adjust to edit timing */
 20% {
   transform: translateX(0%);
 }

  /* 'pauses' the scroller at the end for 20% of the time */
  80% {
    /* Translate will use the width of the element so 100% scrolls it's full length. add the width of the marquee to stop smaller items scrolling */
   transform: translateX(calc(-100% + 100px));
  }

  100% {
   transform: translateX(calc(-100% + 100px));
  }
}

Hi my name is John Doe

Yeah!


jQuery .each update automatically and create global variable?

Question: I'm very new to code. I'm trying to create a scrolling effect using greensock draggable. I'd like my div to drag only if my content of another div exceeds the height value of it's parent's height. Here is the codepen I'm working on - https://codepen.io/kbeats/pen/vVYGOX
So far, I have for my dragging and scrolling code:
Draggable.create("#scrollIcon", {
  type: "y",
  bounds: "#toc",
  onDrag: scroll
});

$(".tile").each(function(){
 var height = $(this).height();
 });

function scroll(){
    if(height > $(toc).height()){
    TweenMax.set(".tile", {y: this.y * -1})
    }
 }

The console.log is saying 'height' is undefined, so I'm guessing it's only being stored as a local variable? Is there a way to iterate through each .tile class and create a global variable of the sum total height that will update automatically when the height changes? (Or maybe just update each time a .tile element is clicked?)
Ultimately, I'm trying to have my 'scroll' div only scroll when the .tile class content exceeds the parent (#toc) height and then somehow create an equation where the scroll amount will adjust depending on the total height of the .tile class. (So it will always be able to scroll through all the content, but not overshoot).
Comment: if you want to make the 'height' in $(".title").each global remove the 'var' and just use: height = $(this).height();. it will not broke the scroll function, but the logic in scroll is not ok.
Answer: The problem is that in this section:
$(".tile").each(function(){
 var height = $(this).height();
 });

height is only available in that scope. Also, you want to sum up the heights, not just the height of one of the .tiles.  Instead, you should do the each inside of the scroll function, and sum up the heights.
Also, you will want to scale the tween by the height ratio between the tile total and the toc, because the amount you'll want to scroll is not only based on the absolute height of the scrollbar. Instead of this.y * -1, you probably want something more like this.y * height / $(toc).height() * -1.
function scroll(){
    var height= 0;
    $('.tile').each(function() {
      height += $(this).height();
    });
    if(height > $(toc).height()) {
      TweenMax.set(".tile", {
        y: this.y * height / $(toc).height() * -1
      });
    }

var base = document.getElementById("Base");
var tab = document.getElementById("Tab");
var arrows = document.getElementById("tocArrows");
var content = document.getElementsByClassName("tile");
var toc = document.getElementById("toc");

//

// variables for slides

var oneSlideOne = document.getElementById("oneSlideOne");
var oneSlideTwo = document.getElementById("oneSlideTwo");

var twoSlideOne = document.getElementById("twoSlideOne");
var twoSlideTwo = document.getElementById("twoSlideTwo");

// menu open and close timeline

var tl = new TimelineMax({
  paused: true,
  reversed: true
});
tl.to(base, .5, {
  x: 280,
  ease: Sine.easeInOut
});
tl.to("#start1", 0.8, {
  morphSVG: "#end1",
  ease: Back.easeInOut
}, 0);
tl.to("#start2", 0.8, {
  morphSVG: "#end2",
  ease: Back.easeInOut
}, 0);

arrows.addEventListener("mousedown", openMenu);

function openMenu() {
  tl.reversed() ? tl.play() : tl.reverse();
}

// making the accordion menu

var acc = document.getElementsByClassName("tile");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener('click', function() {
    this.classList.toggle("active");

    var $panel = $("ul, li", this);
    this.classList.contains("active") ? $panel.show() : $panel.hide();
  })
}

// gsap for custom scroll bar?

Draggable.create("#scrollIcon", {
  type: "y",
  bounds: "#toc",
  onDrag: scroll
});

function scroll() {
  var height = 0;
  $('.tile').each(function() {
    height += $(this).height();
  });
  if (height > $(toc).height()) {
    TweenMax.set(".tile", {
      y: this.y * height / $(toc).height() * -1
    })
  }
}
.base {
  height: 250px;
  width: 280px;
  background-color: #17307F;
  position: absolute;
  left: -280px;
  top: 0px;
}

.tab {
  background-color: #17307F;
  position: absolute;
  width: 80px;
  height: 80px;
  left: 280px;
  border-radius: 0px 0px 20px 0px;
}

#scrollIcon {
  background-color: white;
  width: 25px;
  height: 50px;
  position: relative;
  float: right;
  margin-top: 20px;
  margin-right: 15px;
  border-radius: 30px;
  cursor: pointer;
}

#tocArrows {
  width: 50px;
  height: 50px;
  display: inline;
  margin: 0 auto;
  padding-top: 15px;
  padding-left: 15px;
  cursor: pointer;
}

#tocReverse {
  width: 50px;
  height: 50px;
  display: inline;
  margin: 0 auto;
  cursor: pointer;
  position: relative;
  top: -52px;
  visibility: hidden;
}

ul#toc {
  list-style: none;
  display: block;
  height: 200px;
  overflow: hidden;
  /*overflow-y: scroll;
  overflow-x: hidden;*/
}

li {
  position: relative;
  left: -40px;
  text-decoration: none;
  display: block;
}

li .subTile {
  display: none;
}

.tile {
  background-color: #74A3EB;
  width: 220px;
  padding-top: 10px;
  padding-bottom: 10px;
  border-radius: 10px;
  color: white;
  font-family: lato;
  font-weight: 700;
  font-size: 24px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  margin: 10px 2px 0px 10px;
  transition: 0.4s ease-in-out;
  display: block;
  top: 0px;
  left: -40px;
}

.active,
.tile:hover {
  background-color: #3C72F0;
  /* change this color */
}

.subTile {
  display: none;
  position: relative;
  background-color: #99B4FF;
  width: 200px;
  height: 40px;
  border-radius: 10px;
  display: none;
  overflow: hidden;
  max-height: 100%;
  color: white;
  font-family: lato;
  text-align: center;
  line-height: 40px;
  font-size: 20px;
  margin: 0px 10px 6px 10px;
  cursor: default;
  transition: background-color 0.2s ease-in-out;
}

.subTile:hover {
  background-color: #F2BB22;
}

#twoSlideTwo {
  font-size: 16px;
  height: 60px;
  line-height: 30px;
}

#twoSlideThree {
  font-size: 16px;
}

    <svg id="tocArrows" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 47.95 63.28"><title>tocArrows</title><polyline id="start1" points="15.07 3 43.71 31.64 15.07 60.28" fill="none" stroke="#fff" stroke-linecap="round" stroke-miterlimit="10" stroke-width="6"/><polyline id="start2" points="3 9.96 24.68 31.64 3 53.32" fill="none" stroke="#fff" stroke-linecap="round" stroke-miterlimit="10" stroke-width="6"/></svg>
    <svg id="tocReverse" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 47.95 63.28"><title>tocReverse</title><polyline id="end1" points="32.88 60.28 4.24 31.64 32.88 3" fill="none" stroke="#fff" stroke-linecap="round" stroke-miterlimit="10" stroke-width="6"/><polyline id="end2" points="44.95 53.32 23.27 31.64 44.95 9.96" fill="none" stroke="#fff" stroke-linecap="round" stroke-miterlimit="10" stroke-width="6"/></svg>

 Module One

 Title Slide

 References

 Introduction

 Next Slide

 Module Two

 Title Slide

 Third Slide Long Name

 Fourth Slide Long

Comment: Nevermind, I think it was an issue of not writing my equation correctly. I changed the equation for the Y position to {(height - $(toc).height()) * -1} and it seems to be working.
Comment: Thank you! I'm running into a new issue now - I'm trying to have the Y position of the .tile class auto update when a new sub-menu is toggled open, so that the bottom of the .tile class will always be at the bottom of the base menu (height 200). I added a function that's called each time a .tile class is clicked, -
function setTiles(){
  var height = 0;
  $(".tile").each(function(){
    height += $(this).height();
  })
  console.log(height);
  if(height>=200){
    TweenMax.set(".tile", {y:height - $(toc).height() * -1});
  }
}
Here's an updated - https://codepen.io/kbeats/pen/vVYGOX


Python regex to find either one or the other

Question: I have the following regex that checks for the word "Read" but i'm looking for it to check for either "Read" or "Deleted"
len(re.findall("Read", phrase))

How can I make the regex so it's looking for either Read or Deleted?
Comment: Does this answer your question? [How to match exact "multiple" strings in Python](https://stackoverflow.com/questions/4953272/how-to-match-exact-multiple-strings-in-python)
Answer: You can use alternatives (separated by a pipe |) to search for either "Read" or "Deleted":
len(re.findall("Read|Deleted", phrase))

Comment: What is the benefit of a non-capturing group over a plain `r"Read|Deleted"` here?
Comment: None, wasn't sure you could use them outside capture groups. Will edit.
Comment: Not sure why you would want to use that... I feel like that is more intended for escaping an unknown/external string, rather than a string literal that can be escaped directly in the source code (and rather efficiently with r-strings).
Answer: the pattern match must be independent of search word order
 phrase = " hello world Deleted,Undeleted,Read in a sentence"
 results=re.search("(Read|Deleted|Undeleted).*(Read|Deleted|Undeleted).* (Read|Deleted|Undeleted).*", phrase).groups()
 for group in results:
     print(group)

output
Deleted
Undeleted
Read


Regarding div size

Question: Basically, on one of the pages of my website, I have a div with 60% width, in which all of my content is stored.
My problem is, when I try the site on different, lower resolution monitors, Some of the content in the div ends up being cut out.
I don't want to increase the width, but I have no ideas on how to fix the issue without doing so.
Comment: Use percentages on the content too..
Comment: can you provide any code?
Answer: You can use media queries to change the style rules based on factors like device-width and resolution:
https://developer.mozilla.org/en-US/docs/CSS/Media_queries
For example, you could adjust the font-size in the div based on the size of the viewport

Regarding div size


k out of n probability

Question: An engineering system consisting of n components is said to be a k-out-of-n system (k≤n) when the system functions if and only if at least k out of the n components function. Suppose that all components function independently of each other and are not identical.
If the ith component functions with probability pi, i=1,2,3,4, compute the probability that a 2-out-of-4 system functions.
Option 1:
$$ \Bbb ℙ(2,3,4)  $$
$$=P_1 [1−(1−P_2)(1−P_3)(1−P_4)]+ $$
$$P_2 [1−(1−P_1)(1−P_3)(1−P_4)]+ $$
$$P_3 [1−(1−P_1)(1−P_2)(1−P_4)]+ $$
$$P_4 [1−(1−P_1)(1−P_2)(1−P_3)] $$
Option 2:
Would it be the same as 4-choose-2?
$$ \Bbb ℙ(2,3,4)  $$
$$ \Bbb =P_1 P_2 P_3 P_4 $$
$$ - P_1P_2
 - P_1P_3
 - P_1P_4
 - P_2P_3
 - P_2P_4
 - P_3P_4
 - P_1P_2P_3P_4
$$
Comment: What's the question? Are you looking for confirmation of your answer, or for a simpler form, or to understand the given answer?
Comment: i dont think my answer is right.   looking for the answer for, 2-out-of-4 components functioning for the entire system to be functioning.
Answer: Almost ... no where near what you need..
$$\quad{p_1\big(1-(1-p_2)(1-p_3)(1-p_4)\big)}\\ + {p_2\big(1-(1-p_1)(1-p_3)(1-p_4)\big)}\\ + {p_3\big(1-(1-p_1)(1-p_2)(1-p_4)\big)}\\ + {p_4\big(1-(1-p_1)(1-p_2)(1-p_3)\big)}$$
What you have is the probability that the first component functions and not all three of the others fail, or the second does and not all three of the others do, or... so forth.   On closer inspection, those are not disjoint events.

You require the probability that two, three, or four components function.   I suggest using the principle of inclusion and exclusion.

 $${p_1p_2+p_1p_3+p_1p_4+p_2p_3+p_2p_4+p_3p_4}\\{-p_1p_2p_3-p_1p_2p_4-p_1p_3p_4-p_2p_3p_4}\\{+p_1p_2p_3p_4}$$
Comment: thx for the input Graham.  so what you added at the end is the part for adding in probabilities that the components fail?  confused.
Comment: the case where Prob (success) for each of the 4 components are  identical and independent is the binomial case, so that's easy enough but when its independent and NOT identical is more complicated and that's the case i am after here.
Comment: Are you saying that's the 4-choose-2 case?   i'll take a gander.
Comment: i dont understand your example Graham.
Comment: I remember using a graphical example with circles grayed out or leaved empty when I was in undergraduate, maybe that would help him understand ?
Comment: Right Marine, i think that graphical example depicts how one needs to remove the double counting.. am i right?
Answer: This is what I have come up with.  But I am unable to come up with the generalized equation, so there may be some errors in here.
$$ P_1P_2P_3P_4  $$
$$ +(1-P_1)P_2P_3P_4  $$
$$ +P_1(1-P_2)P_3P_4  $$
$$ +P_1P_2(1-P_3)P_4  $$
$$ +P_1P_2P_3(1-P_4)  $$
$$ +(1-P_1)(1-P_2)P_3P_4  $$
$$ +(1-P_1)(1-P_2)(1-P_3)P_4  $$
$$ +(1-P_1)(1-P_2)(1-P_3)(1-P_4)  $$
$$ +P_1(1-P_2)(1-P_3)P_4  $$
$$ +P_1(1-P_2)(1-P_3)(1-P_4)  $$
$$ +P_1P_2(1-P_3)(1-P_4)  $$
Comment: Yes? No?  Maybe?  heck No?
Answer: The system functions when 2 or more than 2 components work. Thus it should be $\binom{4}{2}+ \binom{4}{3}+ \binom{4}{4} $ combination of individual component probabilities. Thus  $E $ is the event when system functions,
$$
\begin {align*}
P(E) = & p_1 p_2(1-p_3)(1-p_4)+ p_1 p_3(1-p_2)(1-p_4)+ p_1 p_4(1-p_2)(1-p_3)\\
+& p_2 p_3(1-p_2)(1-p_1)+ p_2 p_4(1-p_1)(1-p_3)+ p_3 p_4(1-p_1)(1-p_2)\\
 +& p_1 p_2 p_3 (1-p_4)+ p_1 p_3 p_4(1-p_2)+ p_1 p_2 p_4(1-p_3)+ p_2 p_3 p_4(1-p_1)\\
+& p_1 p_2 p_3 p_4
\end {align*}
$$

k out of n probability


Expected number of turns for SPROUT

Question: As a mathematical father (and with apparently plenty of time on my hands) I long ago computed the expected number of turns for a number of children's games that are effectively Markov maps. (Chutes and Ladders, Hi Ho! Cherry-O, Candy Land, etc.) And I've seen this and similar posts. (The TLDR version for this technique is that you have to diagonalize a matrix.)
My, now older, son has introduced me to a new game and I was wondering about its expected number of turns. The game is SPROUT and it's rules are as follows:

$n$ players play a game similar to every-man-for-himself dodge ball.
Initally a ball is put into play and all players are free to run around the playing field.
Free players can pick up the ball, take up to three steps with the ball, and throw it at another free player--if they hit the target that player is frozen and must sit down. If the target instead catches the ball then the Thrower is frozen and must sit.
A frozen player is freed if the player that froze them becomes frozen. The freed player "sprouts" back up.
The game ends when all players but one are frozen, this can only happen if that single free player has managed to freeze every every player.

Apparently with 30 kids or so this game almost never ends. It's a great game to play I guess because the kids get exercise, but even if you get "out" or frozen, it's usually not long until you are back in the game. And you have a sweet sense of retribution at the moment of your freedom.
Assuming that a turn of this game consists of one random free player freezing another random free player (and simultaneously freeing all the players that the newly frozen player had previously frozen), how many turns do we expect a game of $n$ players to take?
This is harder to analyze because the state of the game is not just the number of free players and the number of frozen players, we must remember who froze whom so we can free the captives if their opressor ever becomes frozen. I can run numerical simulations for this but I'd be much more satisfied with an analytical understanding.

Here's what I've done so far:
We can keep track of the state of the game by an $n$ long vector that tracks the state of each player. Let a zero in position $i$ denote that player $i$ is free and a number from $1$ to $n$ represents the freezor of the frozen player $i$.
Initialize the state to be all zeros.
$$\{0,0,0,0,0,0,0,\ldots,0\}$$
At any later turn the state will be a combination of zeros and integers from $1$ to $n$ (e.g. $\{0,1,5,5,0\}$ ) but we also have the property that any non-zero integers appearing in the state vector imply that there are zeros in those positions of the state vector as well (due to property 4 above).
This means that there are $$\sum_{k=0}^{n-1}{n \choose k}(n-k)^k$$ total states, including the initial state (which is never returned to) and the $n$ final states (one for each possible winning player).
This number is crazy big but I imagine that these states are sparsely connected since I have seen a game of ten boys completed. (Maybe one boy was just really superior at the game??)

I'd appreciate any feedback on my analysis. Am I thinking about this correctly? Is this just gonna be a hard problem? Is it really an easy problem??
Thanks

Update:
Upon further inspection I realize that there are actually far fewer states. If I do not care about player identity, only the total number of turns, I can relable my players as needed so that they are ordered with the free player indices lower than the the frozen players and each group further ordered as follows. For the free players let player 1 be the player in the "lead" i.e. the one who has frozen the most, let player 2 be the next according to this order and so on for $k$ free players. For the frozen players (beginning at index $k+1$) let them be ordered by the index of their Freezor. e.g.
$$\{0,0,0,0,1,1,1,2,2,3\}$$
Then the states are just ordered lists of non-negative integers with the only properties that

The last element is not greater in value than the number (count) of zeros.
The non-zero integers are monotonically non-decreasing in count. (Never fewer ones than twos, etc.)

This drastically reduces the number of states but I do not yet have a good count of this.
Comment: It looks like a hard problem to me. I think you correctly evaluated the number of states, and even though the matrix connecting them is sparse, it will be very hard to analyze even for relatively small values of $n$...
Comment: Thanks @5xum, I realized that I didn't need to keep track of players' identities specifically for my question so the number of states is much smaller. I updated the post in light of this.
Answer: Note that unless you're distinguishing between the players in some way (e.g., modeling their different skill levels), then their identities don't matter, and you can work with a much smaller number of states.
To see how this works, imagine that each live player is "stuck" to all the players that he has frozen.  So if a live player has frozen $n$ other players, he looks like a blob of $n+1$ players running around.  A blob of size $m$ represents one unfrozen player and the $m-1$ players that he has frozen.  As a state of the game is just a partition of the players into blobs, the number of states with $n$ players is exactly the number of integer partitions of $n$.  When a live player is hit, he becomes frozen, and all the players that he previously froze become live players that never froze anyone.  In the blob language, this means that when (the single unfrozen member of) a blob of size $m$ freezes (the single unfrozen member of) a blob of size $n$, we get one blob of size $m+1$ and $n-1$ blobs of size one.
In short, the rule is the following:

Start with a partition of $n$ into $n$ $1$'s.  Each turn, choose two different components of the partition at random.  Add one to the first component; subtract one from the second and then, if its size is still greater than $1$, break it into $1$'s.  The game is over when the partition has a single component.

Because the number of states is reasonably small (when $n=10$ there are $p(10)=42$ states; when $n=30$ there are $p(30)=5604$), this game should be amenable to a Chutes-and-Ladders-style analysis.

Update:
The expected length of the game turns out to have a surprisingly simple form; presumably there is an elegant proof of this, but I haven't found it.  For $n=2$, the game is over in one turn.  For $n=3$, the game state is $(2,1)$ after one turn; it then stays $(2,1)$ with probability $1/2$ and ends with probability $1/2$ each subsequent turn, so the expected duration is $1+2=3$.  This pattern continues: the expected duration with $n$ players is exactly $2^{n-1}-1$.  Moreover, the expected time remaining from any state appears always to be an integer.
Comment: There is another question on this site with an equivalent game, and the answer includes an elegant proof: https://math.stackexchange.com/q/2455288/19328
Comment: Thank you! This is a great new way for me to think about this. Give me a moment to digest.
Comment: My kids, in Oregon, play this game too :).  They call it "Sproutball".

Expected number of turns for SPROUT


Problema com altura de linha em uma tabela no HTML

Question: Usei o seguinte código para mostrar alguns dados em uma tabela:

            Nr DBC
            Descrição DCB
            Medicamento / Apresentação
            Estoque &#013 Inicial
            Entrada&#013 (Aquisição)
            Saida &#013(Receitas)
            Perda
            Estoque &#013 Final

{{ item.dcb_substancia }}
</>

{{ item.nome_substancia }}

{{ item.nome_medicamento }}

{{ item.estoque_anterior }}

{{ item.qtde_entrada }}

{{ item.qtde_saida }}

{{ item.qtde_perda }}

{{ item.estoque }}

O resultado é apresentado na imagem:

Eu preciso diminuir a altura da linha, como podem ver na imagem, está muito alta, no td e tr, atribui heigth="5px", mas não surtiu efeito.
Como posso alterar a altura da linha? Estou usando esse template
Comment: Não sei se ainda é útil para você essa resposta, mas na declaração da tag  linha 3 e na linha 15, há um erro de grafia no parâmetro height. Você escreveu heigth.
Answer: Seria preciso um exemplo mínimo, completo e verificável.
Mas até da para entender a tag h1~h6 tem essa formatação espaçada mesmo.

                                Nr DBC
                                Descrição DCB
                                Medicamento / Apresentação
                                Estoque &#013 Inicial
                                Entrada&#013 (Aquisição)
                                Saida &#013(Receitas)
                                Perda
                                Estoque &#013 Final

                                {{ item.dcb_substancia }}</>
                                {{ item.nome_substancia }}
                                {{ item.nome_medicamento }}
                                {{ item.estoque_anterior }}
                                {{ item.qtde_entrada }}
                                {{ item.qtde_saida }}
                                {{ item.qtde_perda }}
                                {{ item.estoque }}


query to retrieve all translations and nodes of a language that has been removed from the system?

Question: I am experiencing an issue after a language has been removed from the system. Every time I go on the admin/content page on a drupal 8 site I get:
Error: Call to a member function getId() on null in Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple() (line 254 of core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php).

This is a known issue on drupal.org but it has not been addressed yet.
I am afraid this may be a recurring issue and I am trying to build something to clean up content causing the issue (i.e. the content language was removed from the system).
I am looking for a Drupal query that would return all the nodes(entities)/translations which has a language that has been deleted from the system. I can load all content and loop through them to check $entity->language() == FALSE or $entity->language() == NULL but that does not seem to be performant even with using the batch API. Ideally, db query that returns the IDs of the offending content would be much better.
Then I could update them to language neutral or delete the content.
I am wrote something that does something similar to the way node update translations after a language is deleted:
  /**
   * {@inheritdoc}
   */
  public function clearRevisionsLanguage(LanguageInterface $language) {
    return $this->database->update('node_revision')
      ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
      ->condition('langcode', $language->getId())
      ->execute();
  }

I repurposed the above to:
function some_helper_to_update_node_lang($lang_code) {
  \Drupal::database()->update('node_revision')
    ->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
    ->condition('langcode', $lang_code)
    ->execute();
}

But the above function requires the language code, which is reasonable but I am not sure how to make this a more flexible in as much to return all content that has a removed language.
Answer: I ended executing this function to clean up the content:
function delete_non_existing_language_content() {
  // 1. Get enabled languages
  $languages = \Drupal::languageManager()->getLanguages();
  $lang_codes = array();
  foreach ($languages as $language) {
    $lang_codes[] = $language->getId();
  }
  // 2. Get all the nodes that have langcode not in the language list.
  $result = \Drupal::database()->select('node_field_data')
    ->fields('node_field_data')
    ->condition('langcode', $lang_codes, 'NOT IN')
    ->execute();

  // @todo think I can use fetchAllKeyed instead?
  $nids = array_keys($result->fetchAllAssoc('nid'));

  // 3.Delete all of them:
  // @todo Or possibly update them to language neutral code if possible:
  $storage_handler = \Drupal::entityTypeManager()->getStorage('node');
  $entities = $storage_handler->loadMultiple($nids);
  $storage_handler->delete($entities);

}

This worked for my use case, but I don't think it will be performant for large databases.


Magento catalog disappear after reindex

Question: One of my clients has tried to add a product attribute called 'Shipping Date' to products earlier today. He has created the attributed, and then assigned into default attributes set. After the he re-indexed everything to realised that category navigation menu has disappeared. He then deleted the added attribute and re-indexed again without any luck. I am now looking into it and I can't find anything wrong. All products are there as I can find those if I search for products. But I can't get the category navigation menus or go to individual products (before you ask, all products have been set up to show both in catalog and search).
Any suggestions?
Comment: Take a look @ http://overlycaffeinated.com/2011/02/when-reindexing-in-magento-fails-use-the-command-line/
Comment: @R.S Thanks mate. Just tried it , but it didn't work... Any other suggestions?
Comment: Did you try clearing cache after?
Comment: Maybe you could try two things 1) backup and delete the indexes files located at magento/var/locks 2) deactivate the flat catalog if enabled and reindex your shop
Comment: @SylvainRayé I tried the first one but had no luck. I believe I had to re-index after I backed up deleted those indexes? And the flat catalog is not enabled. Here's the site if you would like to have a look: www.pooodle.co.uk
Comment: @R.S Yes I cleared cache after rebuilding indexes...
Comment: I have exactly the same problem now with a current project I work on. I will let you know I resolve it. I feel that it could be a module conflict (in my case) or some trouble with attributes. The best solution is to analyze the sql collection query which can be done thanks to `$collection->load(true);`
Comment: Did you check your error log or try coping the site to a dev environment to make sure nothing change on the server itself? After doing a reindex do you get a error, success or nothing happen?
Comment: @R.S I've looked into a few error files and I'm not sure which one I should be looking at. Reindexing worked successfully, every time I tried it.
Comment: It seems the problem is something to do with the template. I enabled the default theme earlier and I got the categories back. But when I enabled the custom theme again, I lost those again...
Comment: This is the error message I get when enable display errors in index.php: 'Notice: Undefined index: isAjax  in xxx/app/design/frontend/default/blanco/template/checkout/cart/sidebar/default.phtml on line 15
#0 xxx/app/design/frontend/default/blanco/template/checkout/cart/sidebar/default.phtml(15): mageCoreErrorHandler(8, 'Undefined index...', '/var/www/vhosts...', 15, Array)'
Comment: Your error notice doesn't seem to have any influence with your problem. This error shows cart problem but no catalog problem. But you probably right about your template. Did you change anything in the layout of your template (into xml code of CMS pages or in layout xml files)?
Comment: @SylvainRayé According to my client, all he has done was, added an attribute, assigned it to Default, activated a static block and reindexed everything. When he realised that he cannot see the catalog items any more and also category menu disappeared, he has removed that attribute, disabled the static block and reindexed again. But the problem remained and that's when he called me. I reset pretty much everything without any luck. When I disable Ajax related items from that template, I get a different error message. I will copy it here as the next thing.
Comment: Warning: simplexml_load_string(): Entity: line 316: parser error : Opening and ending tag mismatch: block line 237 and reference  in xxx/app/code/core/Mage/Core/Model/Layout/Update.php on line 444

#0 [internal function]: mageCoreErrorHandler(2, 'simplexml_load_...', '/var/www/vhosts...', 444, Array)
#1 xxx/app/code/core/Mage/Core/Model/Layout/Update.php(444): simplexml_load_string('getFileLayoutUpdatesXml('frontend',
And few more lines...
Answer: OK this is what I did. After I took Sylvain's advice about enabling the default theme, it was apparent that this was something to do with a corrupted XML file. Luckily I had a back up from a week ago, so I replaced all theme xml files with that back up. Flushed cache and now we are back in business. Thank you so much for all the tips guys. You all are making this world a bearable place to live :-)

Magento catalog disappear after reindex


Compute expected standard deviation from proportion data

Question: This example is from the first chapter of Gelman & Hill 2007:

The following are the proportions of girl births in Vienna for each month in     1908 and 1909 (out of an average of 3900 births per month):
.4777 .4875 .4859 .4754 .4874 .4864 .4813 .4787 .4895 .4797 .4876 .4859 .4857 .4907 .5010 .4903 .4860 .4911 .4871 .4725 .4822 .4870 .4823 .4973
The data are in the folder girls. von Mises (1957) used these proportions to claim that the sex ratios were less variable than would be expected by chance.
Compute the standard deviation of these proportions and compare to the standard deviation that would be expected if the sexes of babies were inde- pendently decided with a constant probability over the 24-month period.

The standard deviation is: 0.064
I'm wondering how to calculate the expected standard deviation? Should I use a binomial model? but where do the probabilities come from then?
EDIT:
The next step would be to assess whether the observed (0.0064) and chance (0.0080) deviations significantly differ.
The exercise gives a hint: "under the randomness model, the actual variance should have a distribution with expected value equal to the theoretical variance, and proportional to a chi^2 with 23 degrees of freedom"
I'm wondering how I could assess whether the two deviations are significantly different. The only solution I can come up with is
(observed-expected^2)/expected = (0.0064-0.0080)^2/0.0080 = 0.00032 and then compare this to a chi^2 distribution with 23 degrees of freedom. Is this the correct way to do it?
Comment: In general, the variance of the sample variance can be quite complicated (and involves higher moments). It seems unlikely you are expected to use it here.
Answer: I think your calculated standard deviation for the proportions is $0.0064$ (note extra 0).
Yes,  to calculate the expected standard deviation, you are probably expected to use a binomial model and the formula $\sqrt{\dfrac{p(1-p)}{n}}.$ You can take the mean of your sample data to give you an estimate of $p$, though taking $0.5$ will give much the same result in practice. $n$ comes from "out of an average of 3900 births per month".
Comment: thanks! so the expected would be 0.0080 and the observed is 0.0064. Now to check whether this difference is significant I could run a X^2 test. Can I do it the following way: (observed - expected)ˆ2/expected and compare it to the Xˆ2 distribution?
Comment: I suppose you could, but for a $\chi^2$ test you want frequencies rather than proportions.  You would not be using the standard deviations in the test.
Comment: That chi-squared test would have zero degrees of freedom :-).
Answer: To assess whether difference between the observed (0.0064) and chance (0.0080) deviations is statistically significant, you can also compute the 95% confidence interval of the chance standard deviation. This is where the chi^2 distribution with 23 degrees of freedom comes to the picture; you need the quantiles of this distribution, among others, to form this confidence interval. Once you have the confidence interval, check if the observed deviation is inside the interval; if it is, then the difference between the observed and chance deviations is not statistically significant.
Answer: Very late response, but regarding the goodness-of-fit test to the binomial distribution, it turns out that this
$$
s^2(n-1)/\sigma^2
$$
has a $\chi^2$ distribution with $n-1$ degrees of freedom, where $s$ is the sample standard deviation and $\sigma$ is the expected standard deviation.
The formula above will produce a test statistic whose p-value you can compute against that distribution. Because the observed variance is "too small", you want the left-tailed probability (ie, the probability of getting a statistic smaller than the calculated value).


Drop shadow for a line in react native

Question: I want to implement a drop shadow for a line something like this:

What I did so far:
<View style=
  {{
    elevation: 5,
    borderBottomWidth: 2,
    bottom: 1,
  }}
>
</View>

Answer: import Svg, { LinearGradient, Defs, Stop, Rect } from 'react-native-svg'

<Svg height="100" width="100%" style={{ zIndex: 1, marginTop: -50 }}>
    <Defs>
        <LinearGradient id="grad" x1="0" y1="0" x2="0" y2="1">
            <Stop offset="0" stopColor="#e6e6e6" stopOpacity="1" />
            <Stop offset="1" stopColor="#e6e6e6" stopOpacity="0.01" />
        </LinearGradient>
    </Defs>
    <Rect height="100" width="100%" fill="url(#grad)" />
</Svg>

Answer: Use this code this might help you, Your can change the color as your theme.
cardContainer: {
 borderBottomWidth: 1,
 borderColor: #4d4d4d,
 shadowColor: #4d4d4d,
 shadowOffset: {
  width: 0,
  height: 8,
 },
 shadowOpacity: 0.8,
 shadowRadius: 13.51,
 elevation: 5,
}

Comment: Didn't helped! this is creating shadow all around. I just want shadow beneath an invisible line.
Comment: I have change `borderBottomWidth` just `borderBottomWidth` you can test now this will work for you.
Comment: Which component should I apply these style to? A button or a view or anything else?
Comment: You can copy these apply and apply to `` `` or any component.
Comment: Didn't worked! Did you check this code on your machine?  You can share the snack expo link as well.

Drop shadow for a line in react native


button alignment left start and right end android kotlin

Question: I want to design a layout whose expected output should be like this:

but I get this

this is my sample code. any idea how can i fixed it
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello"
                android:gravity="left"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="How"
                android:gravity="end"/>
        </LinearLayout>

Comment: Its `layout_gravity` not `gravity` . Use `ConstrainsLayout` or `RelativeLayout` instead. Even a `FrameLayout` will also work. Also [Check this](https://stackoverflow.com/questions/33076051/layout-gravity-not-working-in-horizontal-linear-layout).
Answer: Try the below code with two different Android Layouts
With RelativeLayout
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal">

      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignParentStart="true"
        android:text="hello"
       />

    <Button
        android:layout_width="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_height="wrap_content"
        android:text="How"
        android:gravity="center"/>
</RelativeLayout>

With ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:gravity="center"
        android:text="hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:gravity="center"
        android:text="How"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Answer: Change your parent layout to Relative layout
    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:layout_alignParentStart="true"
        android:layout_gravity="center"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

Answer: For the correct implementation of ConstraintLayout, you may follow below approach:
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        app:layout_constraintEnd_toStartOf="@id/how"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/how"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/hello"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Answer: if you want to insist on using the LinearLayout, just a place a view above of the how button like this :
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="hello"
            />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="How"
            android:layout_gravity="center"/>


Looking for a Module to allow presentation of data like a Microsoft Access Table

Question: A client that is currently using Microsoft Access to manage some reporting tasks would like us to extend a custom module to allow them the same (or similar) functionality on their website.
I was looking at Data Tables which has a Drupal module and that would get us most of the way there however I wanted to reach out to the experts out there to see if there are any better solutions.
Their website is currently Drupal 6 but we may be rebuilding it in Drupal 7 so I will need solutions for both versions.
EDIT I'm interested in displaying data tabularly, but also need to have the filtering and sorting options on the columns. Data Tables has the sorting down but I don't see how it can filter.
Thanks in advance!
Comment: There is another module that may be helpful to you worth a shot https://drupal.org/project/tablefield
Comment: I clarified a bit on what I'm looking for in my edit.
Answer: Take a look at Node Table module.

Generates sticky headers automatically
Supports flexible column sorting on none, one, or multiple columns
Supports a default sort column with asc / desc option
Supports add, delete, edit of rows and columns
Supports drag and drop reordering of rows and columns (row reordering works only when no column sorts are enabled)

NodeTable module is a versatile module for handling HTML tables on
  Drupal sites. The module provides functionality for creating,
  uploading, editing, and presenting HTML tables within nodes or as
  nodes.
Comment: This is only for Drupal 6, and doesn't give me all of the features I'm looking for. Thanks though
Answer: Not sure if this is going to be of any help, but did you consider the possibility of using:
Views: format as table and Better exposed filters.
Regards
Comment: That is definitely a possibility
Answer: Another great module for filtering and doing things to nodes (or comments or users) is Views Bulk Operations. You can do all the exposed filtering you want and then it lets you apply a wide variety of commands to the selected rows.
Answer: So I think Data Tables are the answer in this case. I looked at the site more and they do have everything I need on the extras page
I'm not sure if the module supports add-ons so maybe I will have to update it a little.
Answer: Attempt to answer this part of the question:

I'm interested in displaying data tabularly, but also need to have the
  filtering and sorting options on the columns. Data Tables has the
  sorting down but I don't see how it can filter.

If it's only a matter of displaying the tabular data, I usually just use the datatables module for that. It seems to work like a charm to me. Here are a few online samples to see that module at work (a rather basic views display, using the dataTables views style):

AbitMORE-scm.com: experiment with the search box in the upper right, eg first type "without" in it, see what happens, and then continue typing so that it becomes "without packages". Doing so you'll only get the rows displayed the contain both keywords somewhere in any of the columns (= 'and' operation), in this sample "without" in 1st column and "packages" in 2nd column.
Dr.Chgman.com: similar suggestion for the search box, like "zmf z- 1.2" (pause a few secs before typing the spaces in between).

The sorting of the tables for both samples is obvious of course (I think / hope). Just using the tiny triangles of any column you want, either ascending, or descending. Obvious, these samples are views displays. From the OP I am not sure if it is about a views display or not however. Maybe enhance the question to clarify that a bit?
If the question is also to retrieve the data from an external (to Drupal) database, and/or if we're not talking about some view here, then I use the Forena module (and not the datatables module). In that case I can still use the datatables plugin (not the module), because for that I just need to specify so in the "skin" that I want Forena to use for my report. Skins in Forena are used for theming reports created by forena, to add just any CSS and/or JS. If relevant for this question (the MS Access makes me doubt if yes or no), I'd be happy to enhance this answer with a few more details on forena and/or its skins.


Why does passing a tuple cause a key error in pandas?

Question: I know that I can select multiple columns if I pass a list of the column names. I was surprised to find that I can't pass a tuple of column names.
import pandas as pd

df = pd.DataFrame([[2,3,4],[3,4,5]],columns=['a','b','c'])

print df[['a','b']]
print df[('a','b')] # Key error

Why is this? Is there something important I'm missing? As far as I can tell, the only difference is that the second case, the multi-key is immutable.
Answer: You can have column name as tuple, so then use for selecting tuple:
import pandas as pd

df = pd.DataFrame([[2,3,4],[3,4,5]],columns=[('a', 'b'),'b','c'])
print (df)
   (a, b)  b  c
0       2  3  4
1       3  4  5

print (df[('a','b')])
0    2
1    3
Name: (a, b), dtype: int64

Answer: df = pd.DataFrame([[2,3,4],[3,4,5]],columns=[('a','b','c'])
df.columns

#Index([u'a', u'b', u'c'], dtype='object')

You do not have key ('a','b'), only 'a', 'b', 'c' keys exits..
Answer: Tuples are also used to select a column from a DataFrame with a MultiIndex:
import pandas as pd
columns = pd.MultiIndex.from_arrays([['a','b','c'], ['X','Y','Z']])
df = pd.DataFrame([[2,3,4],[3,4,5]], columns=columns)
#    a  b  c
#    X  Y  Z
# 0  2  3  4
# 1  3  4  5

then
In [203]: df[('a','X')]
Out[203]:
0    2
1    3
Name: (a, X), dtype: int64

A list of tuples selects multiple columns, with each tuple specifying one column:
In [204]: df[[('a','X'), ('b','Y')]]
Out[204]:
   a  b
   X  Y
0  2  3
1  3  4

DataFrame.__getitem__ uses type checking to produce this behavior:
    # lists are handled here ----------------------vvvv
    if isinstance(key, (Series, np.ndarray, Index, list)):
        # either boolean or fancy integer index
        return self._getitem_array(key)
    elif isinstance(key, DataFrame):
        return self._getitem_frame(key)
    # tuples are handled here when self has a MultiIndex
    elif is_mi_columns:
        return self._getitem_multilevel(key)
    # or else here
    else:
        return self._getitem_column(key)


Can $\Pr(B|-A)$ be written as $1-\Pr(-B|-A)$?

Question: Can $\Pr(B|-A)$ be written as $1-\Pr(-B|-A)$?
Would be helpful if someone could confirm this.
Comment: Have you tried a simple example?
Comment: Whatever you're given only changes the sample space. All the rules you learned still apply. In this case, if $A$ does not happen, it's still true that B either happens or does not, and those are still mutually exclusive.
Answer: Conditional Probability is still a probability measure.
All the usual rules apply.
Such as "the sum of the probabilities of complementary events equals one".
$$\mathsf P(A\mid \neg B) + \mathsf P(\neg A\mid\neg B)=1$$

If you want to be sure, you can just use the definition of conditioning.
When $\mathsf P(\lnot B)>0$ then:
$$\begin{align}
\mathsf P(A\mid \neg B) + \mathsf P(\neg A\mid\neg B) &= \dfrac{~\mathsf P(A\cap\neg B)~}{\mathsf P(\lnot B)}+\dfrac{~\mathsf P(\neg A\cap\neg B)~}{\mathsf P(\neg B)}&&\text{by definition}\\[1ex]&= \dfrac{~\mathsf P\big((A\cap\neg B)\cup(\neg A\cap\neg B)\big)~}{\mathsf P(\neg B)}&&\text{the events are disjoint}\\[1ex]&=\dfrac{~\mathsf P\big((A\cup\neg A)\cap\neg B\big)~}{\mathsf P(\neg B)}&&\text{distribution}\\[1ex]&=\dfrac{~\mathsf P(\neg B)~}{\mathsf P(\neg B)}\\[1ex]&=1
\end{align}$$
As was to be demonstrated.
$\blacksquare$
Answer: Remember that $B | -A$ is the event that $B$ occurs given that $A$ does not. Call this event $E$. Then we have $P(-E) = 1 - P(E).$ Now back substitute.
Comment: So it is correct right ?


How to make git output usage for any command?

Question: For certain subcommands in git, if I do not specify any parameters I get a convenient "usage" output for that command. Example:
$ git blame
usage: git blame [options] [rev-opts] [rev] [--] file

    [rev-opts] are documented in git-rev-list(1)

    --incremental         Show blame entries as we find them, incrementally
    -b                    Show blank SHA-1 for boundary commits (Default: off)
    --root                Do not treat root commits as boundaries (Default: off)
    --show-stats          Show work cost statistics
    --score-debug         Show output score for blame entries
    -f, --show-name       Show original filename (Default: auto)
    -n, --show-number     Show original linenumber (Default: off)
    -p, --porcelain       Show in a format designed for machine consumption
    --line-porcelain      Show porcelain format with per-line commit information
    -c                    Use the same output mode as git-annotate (Default: off)
    -t                    Show raw timestamp (Default: off)
    -l                    Show long commit SHA1 (Default: off)
    -s                    Suppress author name and timestamp (Default: off)
    -e, --show-email      Show author email instead of name (Default: off)
    -w                    Ignore whitespace differences
    --minimal             Spend extra cycles to find better match
    -S <file>             Use revisions from <file> instead of calling git-rev-list
    --contents <file>     Use <file>'s contents as the final image
    -C[<score>]           Find line copies within and across files
    -M[<score>]           Find line movements within and across files
    -L <n,m>              Process only line range n,m, counting from 1
    --abbrev[=<n>]        use <n> digits to display SHA-1s

However, some subcommands actually do things when no parameters are passed, such as git log for example. I've reviewed the documentation for git help but I don't see a way to simply display 'usage' for a command instead of pulling up my web browser. Does anyone know how I can force a subcommand to display its usage information to the command line like git blame did?
I'm using msysgit 1.9.2 on Windows. Thanks in advance.
Answer: Use git add -h instead of git add --help.
[:~] $ git add -h
usage: git add [options] [--] <pathspec>...

    -n, --dry-run         dry run
    -v, --verbose         be verbose

    -i, --interactive     interactive picking
    -p, --patch           select hunks interactively
    -e, --edit            edit current diff and apply
    -f, --force           allow adding otherwise ignored files
    -u, --update          update tracked files
    -N, --intent-to-add   record only the fact that the path will be added later
    -A, --all             add changes from all tracked and untracked files
    --ignore-removal      ignore paths removed in the working tree (same as --no-all)
    --refresh             don't add, only refresh the index
    --ignore-errors       just skip files which cannot be added because of errors
    --ignore-missing      check if - even missing - files are ignored in dry run

Comment: Normally I do `git help add`, but are `--help` and `-h` fundamentally different? That's a bit strange :)


Como definir uma página padrão?

Question: Baixei um projeto, que quando vou rodar da isso:
O servidor Web está configurado para não listar o conteúdo deste diretório.
sou novo nesse ambiente, o que tenho que configurar?
Obs: Visual Studio 2013 Ultimate
Answer: Não consegue aceder a uma página .aspx específica? A página chamada automaticamente deverá chamar-se default.aspx.
Comment: o prblema era exatamente esse, obrigado

Como definir uma página padrão?


skip processing html and page

Question: From a module, I want to output "hello world" simply as a string as efficiently as possible when i call /mymodule/helloworld.
ofcourse, using hook_preprocess_html and hook_preprocess_page i can assign nearly empty templates. the html template should contain <?php print $page; ?> and the page template <?php print render($page['content']); ?>, and the content could be the string 'hello world'.
however, drupal will still go through all the processes of generating $css in html or $page['sidebar_first'] in the page. Even if I override template_process_html it seems $variables['page']['page_top'] has already been populated.
How can I skip all the content processing and go straight for the output ?
[EDIT] had some usefull comments here, but still not what I want. I forgot to mention, I do want to use a template to print 'hello world'. So as I can override it in a theme, for example.

Yes, I can change the delivery callback (either in hook_menu or in
hook_page_delivery_callback_alter), but what the callback method
receives is a render array. I do want to use a node template to render
the content as html. I'd have to write my own delivery callback, I guess
Yes, I can print out the html and call drupal_exit(), but then I wouldnt have
the luxury of using a node template either

So, still looking.
*-pike
Comment: Print it out and call drupal_exit
Comment: You can add your own 'delivery callback' in hook_menu.
Comment: @mikeytown is that a proper way of doing things ?
Comment: @JReynolds - sounds good. could i give 'trim' as a delivery callback, for example ?
Comment: It's what you do if you want to control all the output. See https://api.drupal.org/api/drupal/modules%21overlay%21overlay.module/function/overlay_deliver_empty_page/7 https://api.drupal.org/api/drupal/modules%21image%21image.module/function/image_style_deliver/7
Answer: based on comments above and my own edits, i came up with this
function my_page_callback() {
    $output =  array(
        'template' => array(
            'data'      => array('dummy'=>true),
            '#theme'    => 'my_theme'
        )
    );
    print drupal_render($output);
    drupal_exit();
}

that goes straight for the beef: print the output. it does however use the templating system to create that output. it does not, i think, do all the preprocessing that would otherwise happen.

skip processing html and page


How can a form remember the selected tab after submit?

Question: I'm using vertical tabs in a form, and I keep all the values with <code>$form_state->setRebuild(true);</code>. In this way, the user can change some of the submitted values.
I would like to keep the selected tab active as well. How can I achieve this?
If there aren't other solutions, I will use JavaScript to manually select the tab after the form is reloaded.
Answer: You should be able to see which vertical tab was selected at the time your form was submitted. If not, you can certainly use js and a hidden field to keep track of it. Once you have it submitted your form then you can easily set the #default_tab of your vertical tabs based on the value in your the recently selected tab. Something like this:
<code>$form['information'] = array(
  '#type' => 'vertical_tabs',
  '#default_tab' => isset($form_state['values']['selected_tab'][...) ? $form_state['values']['selected_tab'][... : "some_tab",
);
</code>
Another option would be to use a pure js solution. By storing the currently selected tab in local storage and then selecting that tab when the form loads should work as well. In this case you may see your form "flash" as the selected tab is selected but that may or may not be a problem depending on your form and desired UX


Protecting an older Exchange server

Question: I have a server on one of my networks that is running Exchange 2003.  Yes, we will be upgrading to 2010 in about 6 months with new hardware and all, but for now I need to make sure this is stable and safe.  So for now I wanted to ask advice for backing up and disaster recovery of this server.  Here are the specifics:
This server is running Windows 2003 Server and Exchange 2003 with all updates and patches.  The hardware is a Dell PowerEdge 1850 with no RAID card. The OS and data partitions are both on a single SCSI drive in the first slot 0.  Right now the second drive is blank.
We are backing up the server using online NTBACKUP, and a hard disk Barracuda solution that uses a backup client to copy up the mail store to a local backup server and keep copies off site as well.  We currently have no brick-level or offline backups in place.
In protecting this server my first thought is the drive.  I can create a software mirror to the second drive, but I will need to convert the disk from basic to dynamic to do this.  I am a little scared to do this, since I seem to remember doing this many years ago and having a problem (like the disk didn't boot after the conversion or something).
We just purchased an identical PowerEdge from overstock.com (believe it or not), so I have that to work with.  And I also have several blank SCSI drives I can use for this.
So my question is what would you do to protect this server, knowing how it is set up and what I have available?
Answer: Mirrors are are for high-availability, not backup.  NTBACKUP is the easiest bang for the buck for a single isolated WS03/Exch03 server.
Comment: +1 raid is not backup. Raid is for increasing availability. However, ntbackup can be a bear to restore from if the file size grows too large. Make sure you test your backup's ability to restore! If you can, then bravo. If not, it's time to scramble. I've had luck with http://www.fpns.net/willy/msbackup.htm working on a 250 GB file...
Comment: +1 - RAID isn't backup. Having said that, I'd go ahead and mirror the disks using Windows software RAID and keep right on running NTBACKUP-based backups. Between that and the spare server you ought to be in good shape. As long as you're getting good, regular backups you're protected from corruption. You're limited to two disks in the PE1850 so there's no way you're going to be able to put the ESE transaction logs onto another physical spindle and still have RAID-1. I'd rather have RAID-1 than log/database separation anyway, personally.
Comment: Thanks for the comments.  I know RAID is not backup - I was more asking what to do to protect in general.  And specifically the question of converting disk 0 from basic to dynamic.  Is there any realistic risk when I pull the trigger on that convert?  If I can get that to work, then I will have a software mirror + 2 different backups to go to in case of corruption.
Comment: Excellent point about testing the backup. Especially since you have the spare server: I'd say test the restore on that, do the dynamic conversion on it, and see how it goes. Maybe even switch over to that if you can afford some downtime. In *theory* converting to dynamic should have no visible effect (except for the features) but we all know how useless theory is. This KB article has all the steps for converting to a mirrored drive as well as links to other ones for managing/reparing etc: http://support.microsoft.com/kb/323432
Comment: Just to let everyone know the conversion from basic to dynamic hosed the disk.  I had to use a low-level hex editor to modify a sector to get the disk back to basic.  Moving forward I will be using a third-party software solution to clone the drive for disaster recovery.  Beware of converting from basic to dynamic!

Protecting an older Exchange server


Can I find the topology according to certain definition of convergence?

Question: For instance, consider the space $C[0,1]$, I hope $f_n \rightarrow f$ means that $\sup_{[0,1]} |f_n - f| \rightarrow 0$.
I know the convergence is meaningful only by specifying the topology, and I am wondering whether I induce one topology by the specified convergence.
Comment: Hint: think about metric spaces first (and maybe the real analysis course)
Comment: If I know it's a metric space, I'v know the open set.
Comment: This is possible in general, but the axioms are quite involved and you need to use nets rather than sequences, see [here](https://mathoverflow.net/questions/19285/how-do-you-axiomatize-topology-via-nets).
Comment: @Thorgott, actually it seems that you can just use [sequences](https://math.stackexchange.com/questions/19681/when-is-a-notion-of-convergence-induced-by-a-topology?rq=1), but the axioms are still pretty involved.
Comment: @Alessandro I've not checked the details of that, but it seems to describe only sequential spaces (in the first place, it is not possible to completely describe non-sequential spaces only using sequences, pretty much by definition). But I guess this is more appropriate for OP who seems to start with a notion of convergence and wants a topology realizing this notion. All my remark adds is that not all topological spaces can be described that way.
Answer: First, you don't really need the notion of topology to make sense of the notion of convergence. In fact, there are notions of convergence that are provably not-topological (like convergence almost everywhere). Non-topological notions of convergence arise naturally, so one may be lead to consider new notions of space, like convergence spaces, where convergence is primitive (as opposed to being induced by something else, like a topology). You might wanna take a look at the article An initiation into convergence theory. In the article the author also describes how a convergence space induces a topology on the underlying set, the problem is that then this topology will introduce a (possibly strictly) weaker notion of convergence than the one you started with (for example, read this question).


Make all user profile pictures round

Question: I am trying to figure out how to apply a class to user profile pictures in Drupal 8 to make the pictures round.
Since I am using Bootstrap 3 I can just apply image-round class in this case, but I am not sure how to add the class to the profile image.
I tried adding a class with the Field Class module, but it didn't work.
Comment: Just add the css yourself.. It seems like a lot of effort to go into PHP to add a class that you can duplicate.
Answer: In your file of your theme: "MYTHEME.theme"
Create hook_preprocess_image
<code>/**
 * Implements hook_preprocess_image() to add classes to the search form.
 */
function MYTHEME_preprocess_image(&$variables) {
  ksm ($variables);
    if ($variables['style_name'] == 'thumbnail') {
        $variables['attributes']['class'][] = 'image-rounded';
    }
}
</code>
Comment: Thank you for the reply. I tried this like this:

function isabel_preprocess_image(&$variables) {
  ksm ($variables);
    if ($variables['imagecircle'] == 'thumbnail') {
        $variables['attributes']['class'][] = 'img-circle';
    }
}

But for some reason it threw an error on the line :   ksm ($variables);
Comment: that's right. the KSM function is used to show the contents of a variable. To use KSM you must have the DEVEL module installed. Please comment on that line and it will work correctly.

Make all user profile pictures round


Print preview does not render images

Question: I am trying to print a page from my Universal Windows Store app.
I currently have it set up that I get a list of tuples, (contains the data to be put on the printed pages: string and ImageSource), and when the user wants to print, a new PrintPage (custom subclass of Page, styled using XAML) is instantiated, and I set the elements on my PrintPage using this code:
public PrintPage(Tuple<string, ImageSource> tuple) {
    Title.Text = tuple.Item1;
    Image.Source = tuple.Item2;
}

where my XAML is this:
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="10*"/>
    </Grid.RowDefinitions>
    <TextBlock Name="Title"/>
    <Image Grid.Row="1" Name="Image" Stretch="Uniform"/>
</Grid>

The ImageSource is rendered using the following code (and displayed on a page to have the user choose what to print):
RenderTargetBitmap Bitmap = new RenderTargetBitmap();
await Bitmap.RenderAsync(DrawingArea); // DrawingArea is a `Grid` containing my elements

Now when I print my page, on the preview and on the printed page, I cannot see my image. What am I doing wrong?
Comment: just a guess, but don't you need to await RenderAsync?
Comment: Yes, that is a typo, edited my question
Answer: It wont show because it is a RenderTargetBitmap, you need to convert it to a BitmapImage like this:

private async void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(MyControl);

            var bitmapImage = await ConvertToBitmapImage(renderTargetBitmap);

            var grid = new Grid
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch
            };

            var image = new Image
            {
                Source = bitmapImage,
                Stretch = Windows.UI.Xaml.Media.Stretch.Uniform,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch
            };
            grid.Children.Add(image);

            _printHelper.AddFrameworkElementToPrint(grid);

            await _printHelper.ShowPrintUIAsync("", new PrintHelperOptions
            {
                Orientation = Windows.Graphics.Printing.PrintOrientation.Landscape
            });
        }

        private async Task<BitmapImage> ConvertToBitmapImage(RenderTargetBitmap renderTargetBitmap)
        {
            var pixels = await renderTargetBitmap.GetPixelsAsync();

            var stream = pixels.AsStream();

            var outStream = new InMemoryRandomAccessStream();

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream);

            var displayInformation = DisplayInformation.GetForCurrentView();

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, pixels.ToArray());

            await encoder.FlushAsync();
            outStream.Seek(0);

            var bitmap = new BitmapImage();
            await bitmap.SetSourceAsync(outStream);

            return bitmap;
        }

Print preview does not render images


How to format a view to show content but without a teaser?

Question: I want to format a View to show content but without a teaser.
I can choose fields instead content but then I don't get all the CSS sytling I get with content.
I can hide the teaser in the View page with CSS as follows but I don't think that's good SEOwise.
.node--view-mode-teaser .node__content {
    display: none;
}

How to format a view to show content but without a teaser?
Update after Patrick's comment
Comment: Is your question: How do I show the full text of the node body instead of the summary?
Comment: @PatrickKenny no, I don't want to show the full text. I do wish to show the "content" yes, but just without the teaser or summary.
Comment: I'm not sure what you mean by "content".  If you are using Fields, you should be able to delete the body field and then no teaser or summary will be displayed.
Comment: @PatrickKenny I believe that the screenshot I've attached would clarify my intent.
Answer: I advise you to use content rather than fields.
In view content, you can choose a view mode. Choose the view mode that suits you (full, teaser) or you can create one for your needs. In this example, the view mode is called "accroche" :

Go to to your content type / manage display. In this view mode you put the fields that you want to display in your view :

This way, you keep your css and it's easier to manage if you modify your templates.