MySQL command-line import big database

Assuming the SQL file is an export of one database :

cat MyFile.sql | mysql -u MyUser -p'MyPassword' MyDatabase

Note the absence of space between the -p option and the password.

Posted in SQL, Snippet | Leave a comment

CodeMirror 2 Smarty Syntax Highlighting Mode

Quick and dirty, but way better than nothing !

CodeMirror.defineMode("tpl", function() {
  return {
    token: function(stream) {

      var ch = stream.next();

      if (ch == '{') {

        if (stream.peek() == '$') {
          // Variable
          stream.skipTo('}');
          stream.next();

          return 'smarty_variable';
        } else if (stream.peek() == '*') {
          // Comment
          stream.skipTo('}');
          stream.next();

          return 'smarty_comment';
        } else {
          // Logic
          stream.skipTo('}');
          stream.next();

          return 'smarty_logic';
        }

      }

    }
  };
});

CodeMirror.defineMIME("text/tpl", "tpl");
Posted in Code, Javascript, Snippet | Leave a comment

Mootools Function Implement for limited usage times

Ever wished to define a function that will only execute once, twice, or just a dozen times, even if it’s called a thousand times ? – A disposable function, if you will.

It is not smart performance-wise to use with Events, always prefer removeEvent which is cleaner, but in a lot of other cases it can be useful.

Behold, the power of BIC ! Just use it like this :

var pen = function(foo){
  alert(foo);
}.bic(2);

pen('hello'); // alerts hello
pen('goodbye'); // alerts goodbye
pen('pwned'); // nothing happens

The parameter in bic() is the max number of usages you want.

Below is the Mootools voodoo that can make it happen (note that bic and maxUsages are aliases) :


/*
---
description: Makes sure this function will only be used at most maxUsages times.
             A disposable function, like a bic!

authors:
- Antoine Goutenoir <antoine@goutenoir.com>

license:
- lulz

requires:
- Core

provides:
- Function.maxUsages
- Function.bic
...
*/
Function.implement({
  maxUsages: function(maxUsages) {
    if (!maxUsages) maxUsages = 1;
    var nbUsages = 1;
    return function(){
      if (nbUsages++ <= maxUsages) return this.apply(this, arguments);
    }.bind(this);
  }
});

Function.alias('bic', 'maxUsages');

Source (up-to-date)

Posted in Code, Javascript, Snippet, Tips | Leave a comment

Configure Apache for server-like localhost

Say your project is named foo. You access it with an URL like http://localhost/foo/. But when in production, it is accessed directly on the /, like this for example : http://www.foo.com/.

If you do not want to change the .htaccess (RewriteBase…) each time you move your files from one configuration to another (dev, prod…) and Phing is not an option, this is for you.

Edit the hosts file (C:\windows\system32\drivers\etc\hosts on windows xp) and add the following, for example :
127.0.0.1       foo.local
Then, we must tell apache where to fetch the files for the project foo when you enter foo.local as domain, in the httpd.conf file for example :
<VirtualHost *:80>
  DocumentRoot   "C:/workspace/foo/trunk/www/"
  ServerName     foo.local
  DirectoryIndex index.html index.php

  <Directory "C:/workspace/foo/trunk/www/">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>
Obvioulsy, you will need to change the path and make it point to your own sources.
You might need to add, before the above snippet (but only once) :
NameVirtualHost *:80
Posted in Snippet | Leave a comment

Osmos ~ Get sound under Debian 64bits

Fresh install, no sound.

Launched /opt/Osmos/Osmos :

—————————————————————————————————-
Log opened on Sun May 15 01:47:26 2011
Commandline: ./Osmos.bin64
Preinitializing game: HEMI version 1.6.1 1568
Localization: using language “fr”
Localization: loaded Osmos-fr.loc
Arch: Intel(R) Core(TM) i7 CPU       Q 740  @ 1.73GHz
OS: Linux 2.6.38-2-amd64 (#1 SMP Thu Apr 7 04:28:07 UTC 2011)
Using sound
Showing splash
Using fullscreen mode: 1920 x 1080
Not using vsync
Initializing GLRenderDevice…
OpenGL version: 4.1.0 NVIDIA 270.41.06
Initializing game
Initializing GLRenderer…
Loading textures
Loading fonts
Backed up stats to Stats/Backup/Osmos_0018.sta
Initializing SoundSystem…
Initializing OpenAL
Getting OpenAL device list
Found 3 devices:
Device 0: PulseAudio Software (3: OPENAL DEFAULT)
Device 1: ALSA Software
Device 2: PortAudio Software
Pass 1: no valid device was specified (-1)
Pass 2: no valid device was specified (-1)
Pass 3: attempting device 0: “PulseAudio Software”…
Failed to open device
WARNING: Failed to initialize OpenAL
App exiting — bye!
Writing config to Config.cfg
Log closed on Sun May 15 01:47:33 2011
—————————————————————————————————-

Solution :

Install alsoft-conf.
Launch it, click Apply.  If it’s grey, fiddle with the configuration. This will generate a config file for OpenAL with better default values than the ones provided by the package libopenal1.
Posted in Debian, Linux | Tagged , | 1 Comment

Github repository : mootools-cloud

This blog was built as a pastebin for miscellaneous code snippets, therefore it has shriveled to an empty shell and yet another ghost blog since I opened a github repository, which is better suited for that purpose.

On mootools-cloud, you will find javascript snippets and mootools extensions, written in the mootools-more way as much as possible. Feel free to fork it !

Openness of knowledge is paramount for the very survival of our civilization. It’s a long-term point of view and an arguable one ; and it’s definitely not my code that’ll influence anything. Though, all in all, removing the hindrances to access to knowledge will spawn a smarter generation more likely to make smarter decisions. Hope.

Posted in Code, Javascript | Leave a comment

Use symfony2 console with LAMPP without installing the php package under Ubuntu or Debian

You do not want to install the php5-cli package, maybe there isn’t the 5.3.5 version in your repositories yet, maybe you think LAMPP (v1.7.4 and above have php-5.3.5) should be enough, whatever. No worries. You just need to know where you installed lamp (usually /opt/lampp/), a text file editor, and you’ll be playing with your brand-new symfony2 installation in no time !

Open the console (or console_dev) file, located in the symfony2 app/ folder, and replace the first line,

#!/usr/bin/env php

by that one :

#!/opt/lampp/bin/php

Of course, if you installed LAMPP somewhere else, replace /opt/lampp/ by your installation path.

Posted in Code, Debian, Linux, PHP, Tips, Ubuntu | 1 Comment

Useful PHP Paths in Constants

Assuming you’re on the page http://www.mydomain.com/foo/bar.php


// BASE PATHS //////////////////////////////////////////////////////////////////
// Local path : /home/username/www/mydomain/foo
define('BASE_PATH', realpath('.'));
// Base Url without domain name : /foo
define('BASE_URL',  dirname($_SERVER["SCRIPT_NAME"]));
// Absolute Base Url with trailing slash : www.mydomain.com/foo/
define('BASE_ABS',  $_SERVER['SERVER_NAME'].BASE_URL.'/');

These come in handy when you’re building up a quick php/html template for flash for example ; just pass the BASE_ABS as flashvar and you won’t have to worry anymore about migrating from one server to another!

Posted in Code, PHP, Snippet | Leave a comment

Browser-Independant CSS3 Transition Class

When you’re starting to build a full-featured website/webapp with HTML5 and CSS3, you start to see the problem with having so many different browsers with so many different property naming conventions. Factorization becomes mandatory, so you might should add somewhere in your main css file the following, or your custom implementation of it.

.smooth {
  -webkit-transition: all 0.5s ease-in-out;
  -khtml-transition:  all 0.5s ease-in-out;
  -moz-transition:    all 0.5s ease-in-out;
  -o-transition:      all 0.5s ease-in-out;
  transition:         all 0.5s ease-in-out;
}

Then, you’ll just have to add the smooth class to your element, and for example any property you put in the :hover CSS definition for this element will transition smoothly !

I use many pre-defined transition classes such as :

.slick {
  -webkit-transition: all 0.3s ease-out;
  -khtml-transition:  all 0.3s ease-out;
  -moz-transition:    all 0.3s ease-out;
  -o-transition:      all 0.3s ease-out;
  transition:         all 0.3s ease-out;
}

or

.slow {
  -webkit-transition: all 1.8s cubic-bezier(0.1,0.5,0,0.9);
  -khtml-transition:  all 1.8s cubic-bezier(0.1,0.5,0,0.9);
  -moz-transition:    all 1.8s cubic-bezier(0.1,0.5,0,0.9);
  -o-transition:      all 1.8s cubic-bezier(0.1,0.5,0,0.9);
  transition:         all 1.8s cubic-bezier(0.1,0.5,0,0.9);
}
Posted in CSS, Code | Tagged | Leave a comment

Flash / JavaScript interaction and NPObject

You get the following JavaScript error whilst interacting with flash ?

Uncaught Error: Error calling method on NPObject!

Try adding this line in your flash :

Security.allowDomain("*");

Posted in Code, Javascript | Leave a comment