Dreams of Thought

Are dreams thoughts… or are thoughts dreams..

RSS Feed

Archives for code

How to solve the “local edit, incoming delete upon update” issue in SVN

0 Comments

When I removed a file from svn it caused me lot of problems. This particular file was in CamelCase and I needed to replace it with a file in lower case. I svn deleted the CamelCase file and committed a copy of the lower case file. All this happened in a windows system. When I updated the checked out branch I got the error :

At revision 17582.
!  +  C FooBar.php
>   local edit, incoming delete upon update

Multiple attempts at svn cleanup, svn update etc. failed. I came across this stack overflow thread which recommended svn resolve. This did not work for me. Further down the thread I came across another solution which worked.

  1. create FooBar.php ( $touch FooBar.php )
  2. svn revert it ( $svn revert FooBar.php )
  3. Check status ( $svn status ) . It should say ?FooBar.php
  4. Now remove FooBar.php ( $rm FooBar.php )

That’s it, you’re done :)

Filed under code
Aug 11, 2011

SQLyog on Linux

0 Comments
Run SQLyog on Linux with WINE. You can download the sqlyog windows installer and just run it with WINE. SQLyog is my favourite GUI client. It is lightweight and powerful. I use the community edition which comes free. The paid versions of the app are awesome. I particularly miss the autocomplete feature. This feature autocompletes table names, field names, query commands etc. on tab press. I have used the MySQL query browser, but there's no comparison. The problem with SQLyog being that it runs only on windows :( . I tried alternatives - phpmyadmin, MySQL query browser - but none of them quite measured up against SQLyog. So I decided to try and get it working on my linux box ( Ubuntu Maverick - 10.10 ). I use Wine version wine-1.3.11 . Downloaded the latest installer from the community edition project page. Ran the installer with Wine and voila! That was it :) sqlyog running on wine in Ubuntu
Filed under code
Jan 16, 2011

How to update a field in a table with a field from another table

0 Comments

I needed a table which gave the map between the URLs and nodes in drupal. I ended up creating a table like this :

 

CREATE TABLE `nid_url_map` (
`nid` int(10) NOT NULL DEFAULT '0',
`url` varchar(255) DEFAULT NULL,
`type` varchar(32) DEFAULT NULL,
PRIMARY KEY (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

To populate this table I used the url_alias table :

CREATE TABLE `url_alias` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`src` varchar(128) NOT NULL DEFAULT '',
`dst` varchar(128) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`nid` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `dst_language` (`dst`,`language`),
KEY `src_language` (`src`,`language`),
KEY `nid` (`nid`)
) ENGINE=MyISAM AUTO_INCREMENT=267714 DEFAULT CHARSET=utf8

This is the query I ran at first to populate the table :

INSERT IGNORE INTO nid_url_map (nid,url)
SELECT SUBSTR(src,6) AS nid,dst AS url FROM url_alias
WHERE src LIKE 'node/%';

The url_alias table maps the internal url ( in the form of node/ID ) to the pretty URL of the page ( eg. comedy/monty-python-spam ) . Now what remained was the type. The node type is available in the node table :

CREATE TABLE `node` (
`nid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`vid` int(10) unsigned NOT NULL DEFAULT '0',
`type` varchar(32) NOT NULL DEFAULT '',
`language` varchar(12) NOT NULL DEFAULT '',
`title` varchar(255) NOT NULL DEFAULT '',
`uid` int(11) NOT NULL DEFAULT '0',
`status` int(11) NOT NULL DEFAULT '1',
`created` int(11) NOT NULL DEFAULT '0',
`changed` int(11) NOT NULL DEFAULT '0',
`comment` int(11) NOT NULL DEFAULT '0',
`promote` int(11) NOT NULL DEFAULT '0',
`moderate` int(11) NOT NULL DEFAULT '0',
`sticky` int(11) NOT NULL DEFAULT '0',
`tnid` int(10) unsigned NOT NULL DEFAULT '0',
`translate` int(11) NOT NULL DEFAULT '0',
`titlehash` char(32) DEFAULT '',
PRIMARY KEY (`nid`),
UNIQUE KEY `vid` (`vid`),
KEY `node_changed` (`changed`),
KEY `node_created` (`created`),
KEY `node_moderate` (`moderate`),
KEY `node_promote_status` (`promote`,`status`),
KEY `node_status_type` (`status`,`type`,`nid`),
KEY `node_title_type` (`title`,`type`(4)),
KEY `node_type` (`type`(4)),
KEY `uid` (`uid`),
KEY `tnid` (`tnid`),
KEY `translate` (`translate`),
KEY `title` (`title`),
KEY `titlehash` (`titlehash`)
) ENGINE=MyISAM AUTO_INCREMENT=197121 DEFAULT CHARSET=utf8

I referred the MySQL man page on the UPDATE query. Found a useful query in the comments there. In case I need to use this one again, here's where I'm gonna refer :)

UPDATE nid_url_map,node
SET nid_url_map.type = node.type
WHERE nid_url_map.nid = node.nid;

 

There you go, easy peasy :)

Filed under code
Dec 7, 2010

IRCTC disabled right click

6 Comments

Many readers of my post on booking tatkal tickets with Firefox's autofill forms extension have reported that IRCTC has now disabled right clicks. Not only does this hamper saving the form, it is also very annoying. I've found a workaround for this. After you fill in the form do not directly go and right click to save the form as a profile. Instead paste this code into the Firefox address bar -

javascript:void(document.oncontextmenu=null) .

This will re-enable the disabled right click. Now proceed as before.

This code can be used on any site that disables right click

Hat tip to this post on "Re-Enable Right-Click When Web Pages Turn It Off"

Filed under code, misc
Jun 9, 2010

How to programmatically create and log in drupal users

0 Comments

Creating a new user is very easy in Drupal 6. Here’s how.

<p>><p>$new_user = array(</p><p>'name'                   => $username,<br /> 'mail'                     => $mail,<br /> 'pass'                    => user_password(),<br /> 'status'                  => 1,<br /> 'auth_MODULENAME' => $username<br />)</p><p>$user = user_save(NULL,$new_user)</p><p>// log the user in</p><p>$user = user_authenticate($new_user)</p><p>

Now for the explanation. We create a $new_user array with values we want the newly created user to have. We pass this along to the user_save function and set the 1st parameter as NULL. From the code comments in the user module -

* @param $account
* The $user object for the user to modify or add. If $user->uid is
* omitted, a new user will be added.
*
* @param $array
* (optional) An array of fields and values to save. For example,
* array(‘name’ => ‘My name’); Setting a field to NULL deletes it from
* the data column.

So setting the 1st parameter as NULL creates a new user.

The parameters name, mail, pass and status are all self explanatory. The user_password function generates a random password (by default with a length of 10).

The (optional) ‘auth_MODULENAME’ element will record the user as being created externally. This will result in an entry in the authmap table like this -

“aid” “uid” “authname” “module”
“2″ “20″ “USERNAME” “MODULENAME”

The user_authenticate function logs the user in. This function expects an array as a parameter. It first loads the user in using the user_load function and in case of no errors logs the user in.

This approach of logging in a user is useful when we have the array with us which contains the raw values used to create the user. If all you have is the uid of the user, then logging the user in is very simple. Just use the global $user object.

</p><p>global $user</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>$user = $account</p><p>

Don’t use the user_authenticate function here as it expects the raw values(form values). This wil not work -

</p><p>$account = user_load( array('name' => 'USERNAME') ); // or user_load(UID)</p><p>user_authenticate((array)$account)</p><p>

It will take whatever password is stored in the user object(the raw password), md5 it and then run a query to load that user. Since the password in the query is not the raw password but the md5, the query will return nothing. This will cause an error in the user_authenticate function.

For a direct code example for programmatic log in, check the devel module’s devel_switch_user function.

Filed under drupal
Feb 22, 2010

How to dump or export all the table definitions in a MySQL database

0 Comments

So you have  a database with loads of tables. You want the table definitions of all of them. You don’t really need the data. mysqldump to the rescue!

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 > DUMPFILE

That’s it! Easy as a pie :) The key here is the –no-data option. It dumps all the table definitions, but not the table data.

Want to do this for multiple databases? No problem.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --databases DB1 DB2 > DUMPFILE

The –databases option allows you to add multiple databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt --all-databases > DUMPFILE

The –all-databases option allows you to dump all the databases.

mysqldump -u USERNAME --password=PASSWORD --no-data --opt DB1 --ignore-table DB1.TABLENAME1 --ignore-table DB1.TABLENAME2 > DUMPFILE

–ignore-table option allows you to skip dumping certain tables. Do not forget to specify the databasename when using this option.

 

Know any more tricks? Let us know in the comments below :)

Filed under code
Feb 5, 2010

Firebug opens automatically for all pages

0 Comments

Faced this bizzare issue today in Firefox. Firebug was opening automatically for all pages. Even on closing firebug, all I had to do was click on a new link in the same tab for it to appear again. This happened for all tabs.

Turns out I had accidently turned on “On for all Web Pages” option. Right clicked on the Firebug icon to turn it off. Problem solved :D

Right click Firebug

Right click Firebug to disable "On For All Web Pages"

Filed under code
Jan 20, 2010

Gravatar support in Drupal

0 Comments

If you’ve been using WordPress you probably know about Gravatar. Gravatar is an abbreviation for “Globally Recognized Avatar”. It’s a service which provides an avatar across sites. While Gravatar support is built-in in WordPress, it requires an external module to be installed in Drupal. Recently I had to use the module in a Drupal 5 instance.

The module provides a gravatar only for comments. You can have the module insert the gravatar into the comment body itself or have it provide a field in the comment object ($comment->gravatar) which can be used by the theme. It does this by using the hook_comment() .

In our case though, we needed it to show the gravatar instead of the user picture if the user had not uploaded any. This was very easy to do. The key function here is _gravatar_get_gravatar(). You can provide it any or all of these parameters – mail, default, size or rating. Mail is the email id of the user. Gravatar can supply a unique generated avatar in case the email id you send them is not that of a registered Gravatar user. This can be of many types – identicon, wavatar or monsterid. Which of these are what you want, is specified in the default parameter. The size is of course, the size. The gravatars uploaded come with a rating – G, PG, R or X. You can specify a maximum rating with the rating parameter. You can read more about the gravatar parameters.

You can pass whichever of these parameters you want to _gravatar_get_gravatar() and it will return to you the url of the gravatar. Now pass this to the theme_gravatar() function in the gravatar module.

$picture = theme('gravatar',$gravatar_url,$username,$url);

You have to do all of this in the theme_user_picture function of your theme. If your theme is something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could change it to something like -

function THEMENAME_user_picture($account) {
  if (variable_get('user_pictures', 0)) {
    if ($account->picture && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (user_access('use gravatar', $account) && (!user_access('disable own gravatar', $account) || !isset($account->gravatar) || $account->gravatar)) {
      $picture = _gravatar_get_gravatar(array('mail' => $account->mail));
      return theme('gravatar',$picture,$account->name,'');
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

You could even rewrite the default theme_gravatar provided by the gravatar module with your own THEMENAME_gravatar theme function.

Filed under drupal
Nov 4, 2009

Pirate tweet

0 Comments

September 19 is International Talk Like a Pirate Day.For this day, the Yahoo! Query Language (YQL) team announced on the YDN blog that they were bringing out a “pirate table”. This table would allow us to translate plain English to piratespeak using YQL. I had been meaning to play around with YQL a bit and took this opportunity to dive in. I couldn’t complete it in time for September 19 because of work, but managed to finally get it in some shape today. What I made is something I call “Pirate Tweet”.

Pirate tweet allows you to read your most recent tweets in piratespeak. Just enter a twitter username and you get the tweets in piratespeak. You can get the source at http://labs.gingerjoos.com/piratetweet/piratetweet.tar.gz . Please do link back to this post if you do use it. It is written in PHP and uses PHP’s builtin SimpleXML as well as libcurl. What is in there is just a rough basic barebones quick-and-dirty stuff. It includes an index.php file which takes care of spitting out the actual html. The common.php file does the actual processing. pirate-tweet.css file is some very basic css.

The twitter RSS feed for a user is something like http://twitter.com/statuses/user_timeline/USERNAME.rss . This is fed into the YQL query which does the translation, like this

SELECT * FROM piratespeak.translate
WHERE html IN
(SELECT description FROM rss
WHERE url = "http://twitter.com/statuses/user_timeline/USERNAME.rss")

.
So you send this query to http://query.yahooapis.com/v1/public/yql with the params

<code>$params = array (
'q'      => $query,
'format' => 'xml',
'env'    => 'store://kid666.com/piratespeak',
);

So that’s basically what it does. Hope someone finds this useful :)

I must say that YQL seems like a very interesting tool. Hopefully the MS – Yahoo! deal will have no negative impact on it. Without doubt, YQL is the star in Yahoo!’s products for developers. The ability to express data as queries is mind – blowing. Looking forward to doing more stuff with YQL.

Known issues :

Even links are passed as is to the YQL query. This results in, for example, http://is.gd/3oBWf to become http://be.gd/3oBWf .

Acknowledgment :
Much thanks to the YQL guys at Yahoo! and their wonderful documentation.

Also see :

Previously mentioned blog post has some more stuff using the same table.

Chris Heilmann made some stuff with the table as well. Do check it out.

Filed under code
Sep 28, 2009