Remove and commit a composer dependency in vendor/

Reverting a composer dependency addition directly via ‘Revert Change’ from gerrit would break composer, as we experienced after merging https://gerrit.wikimedia.org/r/#/c/188547/. The patch was to revert Plancake dependency due to compatibility issues in HHVM. I was un-aware of the fact that removing a dependency from composer.json would actually remove the dependency references from the vendor/ directory. Summing up – the correct way of producing a removal of a composer dependency would be:

  1. Remove the dependency from composer.json
  2. run composer update
  3. git add – -all
  4. git commit

A backport to an earlier branch can be produced with the following steps:

git pull --rebase
git checkout -b 'backportTo15' orgin/wmf/1.25wmf15
vi composer.json //remove dependecny
composer update
git add --all
git commt

If you screw up your backport in between – this would be an easy fix :

cd vendor
git pull --rebase
git review -d <change_id>
git checkout origin/wmf/1.25wmf15 . // would reset all files to wmf/1.25wmf15
vi composer.json // and remove the un-wanted dependency
composer update
git add --all 
git commit --amend
git review -R

Some moments from wikimedia-dev 🙂
[17:09:38] tonythomas: You’re meant to get composer to update itself. Otherwise it makes a huge mess.
[17:11:28] tonythomas: Yeah, composer and git aren’t friends. 😦

Using Plancake::MailParse library to strip email out of its headers

Due to some issues with composer loading we had to shift our mail parse library from pear::mimeDecode to a more good looking email parse library by Plancake. Just quoting down how we will employ the library to extract headers from an email:
Add the dependancy to your composer.json

    "require":
    {
        "floriansemm/official-library-php-email-parser": "dev-master"
    }

Now in the mail decode class, add this to perform the extraction:

/**
  * Extract headers from the received bounce email using Plancake mail parser
  *
  * @param string $email
  * @return array $emailHeaders.
  */
public function extractHeaders( $email ) {
	$emailHeaders = array();
	$decoder = new PlancakeEmailParser( $email );

	$emailHeaders[ 'to' ] = $decoder->getHeader( 'To' );
	$emailHeaders[ 'subject' ] = $decoder->getSubject();
	$emailHeaders[ 'date' ] = $decoder->getHeader( 'Date' );
	$emailHeaders[ 'x-failed-recipients' ] = $decoder->getHeader( 'X-Failed-Recipients' );

	return $emailHeaders;
}

Now you can have the headers ready in $emailHeaders 🙂 yay!
* The library has no other dependencies and looks neat as a whole. The code can be found here :https://github.com/plancake/official-library-php-email-parser ! Enjoy ! Happy Hacking!

Using PEAR::mimeDecode to strip email bounce out of its headers

Writing indigenous email header stripping functions involve tedious work and a lot of regex, as the bounce headers email can be encoded. up/down cased, and saving it, we planned to include the mailMimeDecode class – a pretty straightforward approach. Header stripping is quite easy, and it can be installed via composer too.
* Prepare your composer.json

{
    "require":
    {
        "pear/mail_mime-decode": "1.5.5",
        "pear/pear_exception": "1.0.x-dev"
    }
}

* Now, in the mail decode class, add the following to initialize the mimeDecode object

<?php
	// mimeDecode configurations
	$params['include_bodies'] = true;
	$params['decode_bodies'] = true;
	$params['decode_headers'] = true;

	$decoder = new Mail_mimeDecode( $email );
	$structure = $decoder->decode( $params );

	$emailHeaders = $structure->headers;
	
	$to = $emailHeaders[ 'to' ];
	$subject = $emailHeaders[ 'subject' ];
	$emailDate = $emailHeaders[ 'date' ];
	$permanentFailure = $emailHeaders[ 'x-failed-recipients' ];
?>

Now, you have the To, Subject, Date and X-Failed Recipients headers ready. Please note that, you need to just put the lower-cased default email header-name as ‘key’ in the $emailHeaders array, to fetch it.
Yay! Happy Hacking

Composer : Loading specific ‘tags’ instead of ‘branches’

The beauty of composer comes to play when we want to have a custom fork of a software, say hosted in Github to be autoloaded. Again, introducing composer to people who are new:-
Quoting from https://getcomposer.org/doc/00-intro.md

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

The docs tells exactly how to load a developement branch, using the dev- prefix (https://getcomposer.org/doc/05-repositories.md#vcs)
Problem:
* You want to load a custom release tag version from Github
+ You want to load release tag – 5.0-patch from Github repo of monolg
Solution:
* Edit the composer.json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
],
"require": {
"monolog/monolog": "5.0-patch"
}
}

* save it, run $ composer update
There you go ! 🙂 Thanks