MidCOM exec Scripts

From OpenPSA Wiki

Jump to: navigation, search

These PHP scripts can be executed with the MidCOM URL Method midcom-exec. The can be used to execute all kinds of operations. For example, org.routamc.photostream contains conversion scripts for net.siriux.photos galleries. The main midcom exec directory contains the following:

about.php
config-test.php - tests config settings and dependencies
convert_legacy_metadata.php
credits.php
cron.php
dump_manifests.php
index-legacy-attachments.php
reindex.php


[edit] Sample Script

This updates net.nehmer.branchenbuch entries to use net.nehmer.account avatar images as a special image defined in the Datamanager2 Schema. There is one site-specific check for a schema which can be ignored.

<?php
$_MIDCOM->cache->content->enable_live_mode();
while(@ob_end_flush());
flush();
// Disable limits
ini_set('memory_limit', -1);
ini_set('max_execution_time', 0);
//die("XXX");
 
$_MIDCOM->componentloader->load('net.nehmer.branchenbuch');
$_MIDCOM->componentloader->load('net.nehmer.account');
 
$qb = midcom_db_person::new_query_builder();
//$qb->set_limit(100);
 
$entries = $qb->execute();
 
$topic = new midcom_db_topic('baf8210a488f3b8d4dbcabaafba8ba8e');
 
foreach ($entries as $i => $entry)
{
  echo "Scanning Result #{$i} (ID {$entry->id}, name {$entry->username})...\n";
 
    $qb2 = net_nehmer_branchenbuch_entry::new_query_builder();
    $qb2->add_constraint('account', '=', $entry->guid);
    //$qb2->set_offset(1);
    $bb_entries = $qb2->execute();
 
    foreach($bb_entries as $key => $bb_entry)
    {
        $logo_name = "image_logo";
        $type = $bb_entry->type;
        echo " -- found bb entry. \n";
        $avatar = $entry->get_attachment('avatar');
 
        if (!$avatar)
        {
            continue;
        }
        echo " -- found avatar\n";
        if ($type == 'schauspieler')
        {
            $logo_name = "image_portrait";
        }
        $schemamgr = new net_nehmer_branchenbuch_schemamgr($topic);
        $schemadb = Array($type => $schemamgr->get_account_schema($type));
        $entry_dm = new midcom_helper_datamanager2_datamanager($schemadb);
        $entry_dm->set_schema($type);
        $entry_dm->set_storage($bb_entry);
 
        $tmpfile = tempnam('/tmp', 'ac_bb_convert_');
        unlink($tmpfile);
        $dst = fopen($tmpfile, 'w');
        if (!$dst)
        {
            echo "ERROR: Could not open filehandle for '{$dst}', skipping<br/>\n";
            continue;
        }
        $src = mgd_open_attachment($avatar->id, 'r');
        if (!$src)
        {
            echo "ERROR: Could not open filehandle to attachent #{$att->id}, errstr: " . mgd_errstr() . "<br/>\n";
            continue;
        }
        while (!feof($src))
        {
            $buffer = fread($src, 131072); // 128 kB 
            fwrite($dst, $buffer, 131072);
        }
        fclose($src);
        fclose($dst);
        //$GLOBALS['debugger']->print_debug(10,get_class($avatar),$avatar);   
        if ($entry_dm->types[$logo_name]->set_image($logo_name, $tmpfile, "") )
            echo "image updated \n";
        flush();
    }
 
}
?>
Personal tools