public static function execute_multiple($sql, $args = array(NULL))
 {
     global $query_count_on_page;
     $db = Dal::get_connection();
     $sql = Dal::validate_sql($sql);
     $prh = $db->prepare($sql);
     if (PEAR::isError($prh)) {
         Logger::log(" Throwing exception DB_QUERY_FAILED while preparing a query for multiple execution | Message: " . $prh->getMessage() . " | SQL that caused this exception: " . $sql, LOGGER_ERROR);
         throw new PAException(DB_QUERY_FAILED, $prh->getMessage());
     }
     foreach ($args as $params) {
         Dal::execute_pre_hooks($sql, $params);
         $sth = $db->execute($prh, $params);
         Dal::execute_post_hooks($sql, $params);
         if (PEAR::isError($sth)) {
             Logger::log(" Throwing exception DB_QUERY_FAILED while in multiple query execution | Message: " . $sth->getMessage() . " | SQL that caused this exception: " . $sql, LOGGER_ERROR);
             throw new PAException(DB_QUERY_FAILED, $sth->getMessage());
         }
         $query_count_on_page++;
     }
     $db->freePrepared($prh);
     return TRUE;
 }
Exemple #2
0
 public function migrateLegacyFiles($dry_run = TRUE)
 {
     $this->dry_run = $dry_run;
     require_once PA::$path . "/db/Dal/DbUpdate.php";
     echo "Migrating legacy files to new storage system\n";
     $this->all_files = array();
     if (!($h = opendir(PA::$path . '/web/files'))) {
         throw new PAException(GENERAL_SOME_ERROR, "Unable to open web/files directory");
     }
     while (false !== ($f = readdir($h))) {
         if ($f[0] == '.') {
             continue;
         }
         $this->all_files[$f] = TRUE;
     }
     closedir($h);
     $this->unmatched = count($this->all_files);
     $this->unmatchable = 0;
     $this->matched = 0;
     $this->dupes = 0;
     echo "{$this->unmatched} files found\n";
     echo "Matching with user images ...\n";
     $sql = Dal::validate_sql("SELECT user_id,picture FROM {users}", $network);
     $sth = Dal::query($sql);
     while ($r = Dal::row($sth)) {
         list($uid, $pic) = $r;
         // user avatar
         $this->_matchLegacyFile($pic, array("role" => "avatar", "user" => $uid));
         //TODO: user header image
     }
     $this->_dumpMatchResults();
     $networks = DbUpdate::get_valid_networks();
     echo "Processing " . count($networks) . " networks\n";
     foreach ($networks as $network) {
         echo " Network: {$network}\n";
         // network level stuff
         list($network_id, $act, $logo, $extra) = Dal::query_one("SELECT network_id, is_active, inner_logo_image, extra FROM networks WHERE address=?", array($network));
         assert($act);
         // sanity check
         $extra = unserialize($extra);
         // network avatar
         $this->_matchLegacyFile($logo, array("role" => "avatar", "network" => $network_id));
         // network header image
         $header_image = @$extra["basic"]["header_image"]["name"];
         if (!empty($header_image)) {
             $this->_matchLegacyFile($header_image, array("role" => "header", "network" => $network_id));
         }
         // emblems
         foreach (unserialize(Dal::query_first(Dal::validate_sql("SELECT data FROM {moduledata} WHERE modulename='LogoModule'"))) as $emblem) {
             $this->_matchLegacyFile($emblem["file_name"], array("role" => "emblem", "network" => $network_id));
         }
         // group pictures
         $sth = Dal::query(Dal::validate_sql("SELECT collection_id, picture FROM {contentcollections} WHERE type=1 AND is_active=1", $network));
         while ($r = Dal::row($sth)) {
             list($cid, $pic) = $r;
             $this->_matchLegacyFile($pic, array("role" => "avatar", "network" => $network_id, "group" => $cid));
             $header = Dal::query_first(Dal::validate_sql("SELECT header_image FROM groups WHERE group_id=?", $network), array($cid));
             $this->_matchLegacyFile($header, array("role" => "header", "network" => $network_id, "group" => $cid));
         }
         /* disabled until we update peopleaggregator.net
         	    $sth = Dal::query(Dal::validate_sql("SELECT group_id, header_image FROM {groups}", $network));
         	    while ($r = Dal::row($sth)) {
         		list ($gid, $pic) = $r;
         		$this->_matchLegacyFile($network, "group", $gid, $pic);
         	    }
         	    */
         //TODO: advertisements
         // images, audio, video
         foreach (array("image", "audio", "video") as $table) {
             $sth = Dal::query(Dal::validate_sql('SELECT mc.content_id, mc.' . $table . '_file, c.author_id, c.collection_id, c.is_active FROM {' . $table . 's} mc LEFT JOIN {contents} c ON mc.content_id=c.content_id HAVING c.is_active=1', $network));
             while ($r = Dal::row($sth)) {
                 list($cid, $fn, $uid, $ccid, $act) = $r;
                 $this->_matchLegacyFile($fn, array("role" => "media", "network" => $network_id, "content" => $cid));
             }
         }
     }
     $this->_dumpMatchResults();
     foreach ($this->all_files as $fn => $v) {
         if ($v === TRUE) {
             echo " * unmatchable: {$fn}\n";
         }
     }
     echo "Overall results from web/files: ";
     $this->_dumpMatchResults();
 }
  function qup_all_networks($k, $sql_stmts) {
    if (!is_array($sql_stmts)) $sql_stmts = array($sql_stmts);
    if (!$this->quiet) { $this->note("Applying '$k' update to all networks ..."); }
    foreach ($sql_stmts as $sql) {
      if (!is_callable($sql) && (strpos($sql, "{") === FALSE)) {
        die("ERROR: SQL '$sql' is to be applied to all networks, but contains no {bracketed} table names!");
      }
    }
    //    $sth = Dal::query("SELECT address FROM networks WHERE is_active=1");
    $seen_default = FALSE;
    //    while (list($network_address) = Dal::row($sth)) {
    //    var_dump($this->networks);
    global $network_prefix;
    $prev_network_prefix = $network_prefix;
    $nets_done = $nets_updated = 0; $nets_total = count($this->networks);
    $last_prefix = ""; // to work out spacing
    foreach ($this->networks as $network_prefix) {
      set_time_limit(30);
      ++ $nets_done;
      if ($network_prefix == 'default') {
	$network_prefix = ''; // default network has network=='' in mc_db_status table
	$seen_default = TRUE;
      }

      if ($this->is_applied($k, $network_prefix)) continue;
      if (!$this->quiet) {
	if ($this->running_on_cli) {
	  echo "\r";
	  $len_diff = strlen($last_prefix) - strlen($network_prefix);
	  $spacing = ($len_diff > 0) ? str_repeat(" ", $len_diff) : "";
	} else {
	  $spacing = "";
	}
	$this->write("* $nets_done/$nets_total [$network_prefix]$spacing", FALSE);
      }
      foreach ($sql_stmts as $sql) {
        if (is_callable($sql)) {
          $sql();
        } else {
          $new_sql = Dal::validate_sql($sql, $network_prefix);
          Dal::query($new_sql);
        }
      }
      Dal::query("INSERT INTO mc_db_status SET stmt_key=?, network=?", Array($k, $network_prefix));
      ++ $nets_updated;
      $last_prefix = $network_prefix;
    }
    $network_prefix = $prev_network_prefix;
    if (!$seen_default && $this->full_update) {
      $this->write("WARNING: applied change '$k' to all known networks, but the default network doesn't have an entry in the 'networks' table.  This means net_extra.php hasn't been run; something has gone wrong with the upgrade.");
    }
    if (!$this->quiet && $this->running_on_cli && $nets_updated) echo "\n";
  }