Wednesday, 20 November 2013

Create tar archive and exclude files and directories

http://stackoverflow.com/questions/984204/shell-command-to-tar-directory-excluding-certain-files-folders

Linux: How to create a filesystem in a file

There are two steps. One should create a large empty file. Then that file should be partitioned and formatted.

To create the empty file on a Linux filesystem, use fallocate:


fallocate -l 10G path/to/file/image.img


This won't work in non-Linux file systems such as FAT32. For those you can used the dd command to create a file full of zeroes.

dd if=/dev/zero of=/path/to/file/image.img -bs=10M -count=20480


The above command will create a 200gb file. The calculation is 204800 / 10 = 20480. The block size should be the size of the storage device's buffer. The speed of creation of the file depends on the size of the block size.

After creating the file, the filesystem should be created. mke2fs will give a warning but you can proceed after entering 'y'.

mke2fs -t ext4 /path/to/file


To mount the filesystem of the file, create a directory in the /mnt directory, and then mount it using the mount command. The filesystem won't show up as a device in the file manager but will function as a separate filesystem when you navigate to the mount directory.

sudo mkdir /mnt/backup_vol
sudo mount -o loop /path/to/disk_image /mnt/backup_vol


The filesystem can be accessed from /mnt/backup_vol

To unmount the filesystem:

sudo umount /mnt/backup_vol


Reference: http://freecode.com/articles/virtual-filesystem-building-a-linux-filesystem-from-an-ordinary-file

Friday, 15 November 2013

Renaming a wordpress theme

http://www.davidjrush.com/blog/2012/09/renaming-your-wordpress-theme/

PHP: put printed data into a variable

http://stackoverflow.com/questions/4798142/php-capture-print-require-output-in-variable

PHP get the current URL

To get the actual URL (non necessarily the one shown in the address bar because of URL rewriting):

 
 
$actual_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
 
To get the URL shown in the address bar:

$displayed_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Manually creating a Buddypress menu

The simplest method:

bp_get_displayed_user_nav();

But this works only when the current page is a member page (url is http://host/members/username). Because the function generates a menu with relative links.

The below code will replace the relative links with absolute ones:


$bp_user_link = bp_loggedin_user_domain();
  ob_start();
 bp_get_displayed_user_nav();
 $the_user_nav = ob_get_clean();
 
 print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));

bp_loggedin_user_domain gives the url http://host/members/username/

Since bp_get_displayed_user_nav directly prints the code, to capture the code in a variable we have to use ob_start();

One odd thing is that this command will not output the Messages menu in non-member pages. Therefore this has to be obtained separately.

  $bp_user_link = bp_loggedin_user_domain();
  $messages_count = messages_get_unread_count();
  $messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages

<span>$messages_count</span>
</a></li>\n\n"


Putting it together:



$bp_user_link = bp_loggedin_user_domain();
$messages_count = messages_get_unread_count();
$messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages 

<span>$messages_count</span>
</a></li>\n\n";

      $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      ob_start();
      bp_get_displayed_user_nav();
      $the_user_nav = ob_get_clean();
     
      print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));


$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
ob_start();
bp_get_displayed_user_nav();
$the_user_nav = ob_get_clean();
print str_replace('Profile','Profile'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));
This code works on a stand-alone page. However, when the code should be modified when putting it in a template. Because the same template is used in member and non-member pages.

$current_url_part = "$_SERVER[REQUEST_URI]";
$current_url_part_9 = substr($current_url_part,0,9);

if(!($current_url_part_9 == "/members/" && $current_url_part != "/members/")) // the second check is because the members page is simply http://192.168.42.20/members/, it doesn't have the username after it
{
 /* put an if over here where you check wether the url is under members. if so, show the regular bp_get_displayed_user_nav */
$bp_user_link = bp_loggedin_user_domain();
$messages_count = messages_get_unread_count();
$messages_code = "<li><a id='user-messages' href='{$bp_user_link}messages/'>Messages<span> $messages_count</span>
</a></li>\n\n";
//print $messages_code;
ob_start();
bp_get_displayed_user_nav();
$the_user_nav = ob_get_clean();
 
print str_replace('Profile</a></li>','Profile</a></li>'.$messages_code,str_replace('href="','href="'.$bp_user_link,$the_user_nav));
}
else
{
bp_get_displayed_user_nav();
}     
For the message count to be dynamically updated, the following html structure should be used since there is jquery that keeps the count updated:

<a id="user-messages">

<span><?php echo messages_get_unread_count(); ?></span>
</a>

Tuesday, 12 November 2013

PHP if alternative syntax

http://php.net/manual/en/control-structures.alternative-syntax.php