Friday, May 13, 2016

Creating simple xml file using Drupal

Here is the snippet to create an xml file using our custom module. We can customise tags, attributes and values for the xml elements.

    $dom = new DomDocument('1.0');

    //add root - <parent>
    $parent = $dom->appendChild($dom->createElement('parent'));

    //set attributes to <parent> tag
    $parent->setAttribute("name", "parent1");

    //add <child1> element to <parent>
    $child1 = $parent->appendChild($dom->createElement('child1'));

    //add elements to <child1>
    $schema = $child1->appendChild($dom->createElement('schema'));
    $schema->appendChild($dom->createTextNode('Schema Name')); //add <title> text node element to <schema>
    $schemaversion = $child1->appendChild($dom->createElement('schemaversion'));

    //add <child2> element to <parent>              
    $child2 = $parent->appendChild($dom->createElement('child2'));
    //add elements to <child2>
    $listr = array('value1','value2');

    $i = 1;
    foreach ($listr as $parent => $href) {
                $baby = $child2->appendChild($dom->createElement('baby'));
                $baby->setAttribute("href", $href);
                $baby->setAttribute("type", "webcontent");
                $baby->setAttribute("intendeduse", "unspecified");
                $babyfile = $baby->appendChild($dom->createElement('file'));
                $babyfile->setAttribute("href", $href);

    // generate xml
    $dom->formatOutput = true; // set the formatOutput attribute of dom Document to true
    $test1 = $dom->saveXML(); // save XML as string or file
    $dom->save('simple.xml'); // save as file 

Simple Drupal CRON

Here is the snippet for simple cron using custom module. We are just calling a function while executing the cron (menucallback).

 * Implements hook_cronapi().
function modulename_cronapi($op, $function=NULL) {
    switch ($op) {
        case 'list':
            return array('modulename_cronname' => t('CRON Human Readable Name'));
        case 'rule':
            return '*/20 * * * *';
        case 'execute':
            return 'menucallback';

After this refresh the module cache and go to admin/config/system/cron to view this newly created cron.

Wget to download your site HTML pages

Here is the snipped to download your site as html with some restriction,

- This will not download jpeg,jpg,png,gif,mpeg,mp4 files.
- This will not download files under 'anyfolder' (sites/anyfolder)
- This will download your files inside 'files' (sites/files)

wget -p -nH --restrict-file-names=nocontrol -R jpeg,jpg,png,gif,mpeg,mp4 -X sites/anyfolder --html-extension -e robots=off --base=./ -k -P sites/files

Delete a directory recursively

Here is the snippet to delete the folder and its children folder, files recursively.

function delete_directory($dir) {
    if ($handle = opendir($dir)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir($dir.$file)) {
                    if(!@rmdir($dir.$file)) { // Empty directory? Remove it
                        delete_directory($dir.$file.'/'); // Not empty? Delete the files inside it
                else {

db_merge combination of db_insert and db_update

In drupal 7, there is a something new for table insertion and update. That is called db_merge which combines db_insert and db_update in a single query for single or multiple row insert and update.

$query = db_merge('table_name')
                    'field1' => value1,
                    'field2' => value2,
                    'field3' => value3,
                    'field3' => value3,
                ->key(array('field2' => value2))