Skip directly to content

Tight coupling (no, not like that)

on August 11th, 2010 at 2:00:44 PM

Drupal has a function called file_save_upload()

The docs say that it "Saves a file upload to a new location." Let's see what it really does...

45698_OVERSTUFFED_SANDWICH02.JPG

 

Reads in from global arrays

// Make sure there's an upload to process.
  if (empty($_FILES['files']['name'][$source])) {
    return NULL;
  }

Accesses global variables

global $user;

Validates input

if (isset($validators['file_validate_extensions'][0])) {
      // Build the list of non-munged extensions if the caller provided them.
      $extensions = $validators['file_validate_extensions'][0];
    }

Saves to the DB

if ($file = file_save($file)) {

Prints messages to the screen

if ($file->destination === FALSE) {
    drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $source, '%directory' => $destination)), 'error');
    return FALSE;
  }

Writes to the disk

if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->uri)) {

Sets permissions on the disk

// Set the permissions on the new file.
  drupal_chmod($file->uri);

Could we shove any more stuff in here?

Some ideas:

  • Let's have it also send emails with the files as attachments
  • Image resizing
  • DropBox
  • Post to 4chan!
  • Archive all files in .lzh format
  • Submit mp3s to last.fm for scrobbling

Any more ideas? I want to make this function the bestest function ever. I want to make it bigger and better than Drupal.

This is obviously (mostly) a joke because the time to change this is probably long past.  Here's one issue though that takes out part of it (potentially): http://drupal.org/node/685818

My country

on August 4th, 2010 at 2:06:37 PM

P1020086.JPG

I wrote previously about the American visa application process and how my inlaws tried to get an appointment for an interview for 4 months.  Well, after writing to my really excellent congressman McGovern, I was able to secure them an interview which was this morning.

Sadly, their visas got denied today for the age old reason of America doesn't trust brown people to abide by the law and leave when their visa is up.  I live in a country which robs a 72yr old retired policeman and his family of the joy of watching his grand daughter's first year.  They have impeccable records, advanced degrees, speak English, plenty of money in the bank, and they own valuable property. Why?

Needless to say, we're thoroughly demoralized, angry and shocked.  Does anyone know if there is any recourse we can take?

The future of multimedia in D7 or "You don't want me to kill the monkey do you?"

on July 10th, 2010 at 12:59:28 PM

Hey folks,

Did you know that Drupal 7 will have a amazing multimedia capabilities due in large part to the Media module?

Hi, my name is Jacob Singh.

You may remember me from such kick-ass projects as Acquia Search (ApacheSolr), Drupal Gardens or GUI updates in core.  You may also just be dying to know what's going to happen to the monkey.  Either way, I'm here to tell you about what I've been up to along with many of my colleagues and Drupal buddies.

The Media module provides a rich interface for:

  • Managing files
  • Uploading new files or adding them from the internets
  • Browsing existing files
  • Embedding files in WYSIWYG editors
  • Adding fields and meta-data to files
  • Generally making Drupal suck less when it comes to files.

But don't take my word for it, watch me stumble through this demo (about 5 min of fast paced monkey-on-baby action)

and then vote for my media session at Drupalcon

Content | Site-Install.png

How to use theme_pager without a SQL query (and go to hell)

on June 29th, 2010 at 10:42:27 AM

pager.inc is... ugly.  Let's get that out of the way.  I'm sure there are valiant souls out there who are not bitching about it, but trying to fix it.  I'm not one of those people right now, I've got my own battles, but if you happen to go up against it and want to create a pager for a data source which isn't a SQL database, here's what you need to do (mostly copied from pager_query):

Global variables (yes and it gets worse)

pager.inc makes extensive use of global variables to pass around the state of the pager.  You can see how these are defined in pager_query():

global $pager_page_array, $pager_total, $pager_total_items;

Honestly, don't ask me what these are.  Roughly, $pager_page_array is an array of different pagers on the page and what page they are at (I think).  $pager_total is the total number of pages for each pager, $pager_total_items is the number of items in the set.

These are all set in pager_query if you care to find out more.

 

Getting the current page

You'll need this of course to split your set up so you can show just what you need to show on that page.

Fortunately, this is pretty straightforward in a really obtuse way:

$page = isset($_GET['page']) ? $_GET['page'] : '';
// The user will go to mypage?page=0,3,5,6.
// What does this mean?  There are 4 pagers on the page.
// They don't have names, just numbers.
// Sorta like in a prison, only it's a mental facility and the inmates are running it.
// The first one is on page 0, the 2nd one on page 3, etc. etc

$pager_page_array = explode(',', $page);
// So this builds an array of all the pagers on the page and what page they are on.
// One of these is the pager you care about. In most cases, it is the only one, 
// so you can just set $element = 0;
$yourpage = $pager_page_array[$element];

Okay, now you do whatever you need to with that in terms of running a for loop to build a table or whatever.

Setting up the pager

// Number to show per page.
$limit = 10;
// The bizarre "element number" as long as you are the only pager on the page
// this will work fine
$element = 0;
// $total_records is a variable you define that reflects the total number of
// records you plan on showing (not per page but overall).
$pager_total_items[$element] = $total_records
// This is the total number of pages.  If you can't figure this out,
// go back to grammar school.
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
// wtf...
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], 
  ((int)$pager_total[$element]) - 1));
// No seriously... wtf... this is basically saying, I'm on the first page,
// unless I'm not. I know, it's really clear.

Let it loose

return $my_paged_content . theme('pager');
// Go get a drink.

Okay, I hope I helped someone.  Happy hacking.

Oh and don't forget to vote for my DrupalCon Copenhagen sessions!! I really want to go and got to get 'em accepted. If you want me to remind (harass) you when voting opens, please follow me on twitter @JacobSingh.

Giving our customers the gift of Ham: Mollom (anti-spam service) provisioning API

on June 9th, 2010 at 10:28:17 AM

Well, Sprint 39 just finished up at Acquia Engineering and we've got a number of new exciting features and bug fixes releasing to Drupal Gardens in the next week.  One of these is Mollom - the best spam protection service on the internet - for all Drupal Gardens site owners.  For those not in the know, Mollom protects your site from spam by analyzing the contents of comments or other content that is contributed by users and presenting Captcha challenges if the content is suspect.

What this means for Drupal Gardens beta testers:

You'll get setup with the best spam protection available from Mollom.  No cost, nothing for you to do.  Just works.

What this means for Developers who want to provide Mollom to their customers:

Most people aren't aware, but Mollom offers an extensive provisioning API for Mollom partners to create accounts on behalf of their customers.  Gábor Hojtsy and I got to work on this feature and Gábor wrote the mollomapi module which does the provisioning. Here's a sample of how the API works:

$mollom_keys = mollomapi_site_add('whitehouse.gov', 'obama@whitehouse.gov');
if (!empty($mollom_keys) && is_array($mollom_keys)) {
  $public_key = $mollom_keys['public_key'];
  $private_key = $mollom_keys['private_key'];
}
Cool. right?  The full API docs are available from Mollom, so feel free to get in touch with mollom, request a reseller account and continue the quest to rid the 'net of spam!

Why Americans are seen as arrogant: The Visa.

on June 7th, 2010 at 5:24:30 AM

national_language.jpg

http://xkcd.com/84/

Okay, now let's consider for a moment what America is.  Is America a "white" county? Although many would like it to be, it is not.  Non-hispanic whites were only 68% of the population in 2008 and that number is likely inflated since the percentage is dropping rapidly.  Is it an "English" country?  Officially yes, although a huge number of Americans speak other native languages.  16% of the population is of Hispanic or S. American origin and most of them speak Spanish or Portugese at least as a 2nd language.  So what is it?  An "American" country?

At what point does one become an American and then have the right to delineate the "other" and then humiliate them?

Has any US and EU citizens reading this ever tried to get a visa for another country?  I've gotten several and it is usually as simple as "send in the form with $50 and your passport and get a visa in a week or so".

My in-laws are trying to get a visa to come to the US and visit their daughter, son-in-law and granddaughter.  My father in-law was an important figure in the police service for many years and they are 100% above board legally and financially secure.  Here's the process:

  1. Pay ~$150 per person in non-refundable application fees (note that a visa for India - and most other places is ~$50).
  2. Fill out long forms for all parties involved.
  3. Wait in line at a specific bank on the other side of town to deposit a demand draft.
  4. Make an appointment with the outsourced visa processing company (more on this in a moment)
  5. Go into the interview armed with bank statements, tax statements, personal recommendations, and a "yes suh" attitude.
  6. Get your visa issued (or not in the case of my brother in-law - twice in 3 years).
  7. Pay *another* $10 per person for a visa issuance fee.

We've just about got to #4 after giving the state department about $600 in fees.  So the only way to get an appointment now that they've outsourced the process is to go to the VFS website.  At the VFS website, you fill out a form, and a captcha and then...

There are NO appointments available.  No indication of when they will become available other than that we should "try later".  3 weeks of trying 10 times a day.  Waking up at 4:30AM to hope we catch the next batch of released appointments.

This is stupid, wasteful, arrogant and hateful behavior.  Especially for the amount of money they require (oh and they just raised it).  It is simply holding powerless brown people hostage to their desire to visit their loved ones.  There is no excuse for such an inefficient system.  Several attempts to contact people at the embassy and get a resolution has failed.  Apparently they think this makes sense and that their customers should be treated this way.

Well I don't think it makes any sense.  Sure there is large demand, but a system which takes your money and then makes you click on a web page hundreds of times instead of just giving you the next available time is absurd and those responsible for setting it up should be sacked.

 

Am I an asshole for walking out of movies?

on May 29th, 2010 at 6:30:00 AM

So I've been living with my in-laws in Noida (near Delhi) for the past 3 months.  They are wonderful people and I will miss them dearly after my triumphant return to the US in 30 days.  My wife's got two brothers, one is a twin and the other is a bit older.  They are unmarried and still living at home (as is often the custom here).  Sometimes on weekends they come down to our apartment to have a "movie night".  I've never met two single men in their 30s/40s who like RomComs until these two; but anyway with my snooty independent cinema and best picture nominee criteria we often have disputes over what to watch.

Last night it was "Twilight."  Obviously, not one of my picks.

How the hell anyone could make such a shitastic movie and not be in some kind of prison for defrauding a production company that they are a director is beyond me.   I mean, I even sat through about 3/4 of "All about Steve" which they subjected us to last month.  (notably, All About Steve won a razzie).  I made it until about the 4th awkward stuttering and staring match between the two brain-dead teenage protagonists before pouring myself a Talisker 18yo, grabbing a book and heading for the porch to try and undo the mental damage.

After the film got into a nice fight with the wife about it.  Apparently it is rude to walk out of a film, even one which I had protested against watching before starting.  Does anyone else have a problem with this?  I pretty frequently walk out of movies at home and at the theatre.

Let me know!  My pride is at stake here.

Alias a.ka. Alias a.k.a BashAliases

on May 21st, 2010 at 6:19:39 AM

I found a cool little tool today for building bash aliases.  

Being the lazy SOB that I am, I'm just going to quote the project page since it describes well why it exists:

This is all well and good but ... the time and forethought required to create these definitions [aliases] is often a significant barrier that is difficult to overcome. Even though the time it would take to create an alias would save much time in the long run (and much stress on hands and wrists and arms) the task at hand always seems to take priority. And so one rarely does.

- http://www.logicalpoetry.com/bashalias/

Here's a quite run down of a couple commands:

acd Alias Current Directory

Example:

$ cd ~/work/somewhere/deep/in/my/sourcecode/where/I/go/all/the/time
$ acd alias for /home/jacob/work/somewhere/deep/in/my/sourcecode/where/I/go/all/the/time?rsi
$ cd /
$ rsi
$ # Goes to the directory.

The alias is saved in ~/.bash/alias.

Also has alc

Alias Last Command

This one basically let's you choose from the last 9 entries in your history and make one an alias. This is not rocket surgery, or even brain science, or even basic linux administration, but it does encourage something we all should do more of anyway, and I like the simplicity. Check it out! http://www.logicalpoetry.com/bashalias/

Appcelerator's Titanium: A truncated review

on May 11th, 2010 at 5:25:58 AM

Like any self respecting entrepreneurial geek, I've got my eye on mobile applications.  Recently receiving my sweet sweet Nexus One (seriously, drool worthy phone), I wanted to see what I could break.

I used one of my very precious Sundays about a month ago to dig in.

What is Titanium 

As far as I can tell (this review is truncated) Titanium is basically an API and runtime which allows you to build a web application and deploy it to a mobile device, or run it on the desktop.  The differentiator is that while your app is just running in a webkit browser, you can add controls and utilize APIs on the host machine using Titanium's custom JavaScript APIs.  I guess they accomplish this via a plugin for webkit which renders them in the browser?  I don't know, but anyway, that's the gist.  It's supposed to be better than Phonegap (which is now my only other option for x-platform mobile development) because it uses these native controls instead of the ugly browser based ones.  So your app doesn't look like a web app (even though it kinda is).

The company's website has been nicely lacquered with Web 2.0 spray and they are venture backed.

Great idea, doesn't work.

Getting the desktop runtime working and building a sample app from the docs was pretty easy and straight forward.   But that's not what I came for.  The desktop is so 2009.  I wanted to get something working on Android.  Sadly, while the website claims everywhere that Titanium works for Android (and I'm sure it once did), I just couldn't get it to work.  No errors, just silent failure.  Then I found this thread:

 

http://developer.appcelerator.com/question/12991/create-new-mobile-project-in-ti-developer-121-missing-android-emulator-option

Android is completely borked.  It just doesn't work.

Amazing: 5 weeks later, still no resolution.  Barely a peep from customer service too over that time.  BTW, there are another dozen similar threads.  We're not talking about an edge case or training, we're talking about the product being fundamentally broken.  If I paid for it, I would certainly take legal action.  This company is venture backed and sells support services. 

Developers are willing to struggle, but only to a point

If you're a technology company making something for developers, or something developers will have to integrate, please pay attention.  Developers these days have a huge sway in I.T. decision making.  Sure, a great board and savvy marketing will help it slip past the VP or CEO, but developers will feel your product, touch it, they will test your support, they will try to hack it, and they will try to fix it.  And then, if you didn't waste too much of their time, and responded to them quickly and completely, they might make a recommendation.

So please, stop the product design meetings, skip that next conference, cancel the management offsite and have all hands support your product.  The idea is great, but unless you are fanatical about having me as a customer, I can't trust that you'll be fanatical about serving me later.

 

Create gorgeous rotating banners with 0% nerdliness

on May 6th, 2010 at 12:12:00 PM

Acquia's been working on ways to let people create really professional looking Drupal sites using only web-based tools.  One of those tools is the Rotating Banner module which just had its first release this morning.  What is a rotating banner?  I'm talking about those pretty slideshow type things at the top of most sites' homepages these days.  For a couple examples, check out http://acquia.comhttp://kofiannanfoundation.org/, http://amnesty.orgThis module was developed for Drupal Gardens by myself and James Elliot.

Core features:

  • Various transition effects (fade, wipe, etc)
  • A rich editor where you can pick a background image and add headlines and text areas with inline editing
  • Pre-defined layouts for your text or a custom mode where you can drag and drop right where you want.
  • Creates blocks, you can make a bunch of them.  Put them in sidebars, the header, etc.
  • Can be a static size, or will grow to the size of your largest / smallest image or the size of the region you put it in automatically!
  • Controls for switching between slides, or auto transition

Demo (click to view on YouTube):

Pages