/** * \brief Combine copyright holders by name \n * Input records contain: content and type \n * Output records: copyright_count, content, type, hash \n * where content has been simplified from * the raw records and hash is the md5 of this * new content. * \return If $hash non zero, only rows with that hash will * be returned. */ function GroupHolders(&$rows, $hash) { /* Step 1: Clean up content, and add hash */ $NumRows = count($rows); for ($RowIdx = 0; $RowIdx < $NumRows; $RowIdx++) { if (MassageContent($rows[$RowIdx], $hash)) { unset($rows[$RowIdx]); } /* debug to compare original with new content else { echo "<br>row $RowIdx: ".htmlentities($rows[$RowIdx]['content']) . "<br>"; echo "row $RowIdx: ".htmlentities($rows[$RowIdx]['original']) . "<br>"; } */ } /* Step 2: sort the array by the new content */ usort($rows, 'hist_rowcmp'); /* Step 3: group content (remove dups, add counts) */ $NumRows = count($rows); for ($RowIdx = 1; $RowIdx < $NumRows; $RowIdx++) { if ($rows[$RowIdx]['content'] == $rows[$RowIdx - 1]['content']) { $rows[$RowIdx]['copyright_count'] = $rows[$RowIdx - 1]['copyright_count'] + 1; unset($rows[$RowIdx - 1]); } } /** sorting */ $ordercount = '-1'; $ordercopyright = '-1'; if (isset($_GET['orderc'])) { $ordercount = $_GET['orderc']; } if (isset($_GET['ordercp'])) { $ordercopyright = $_GET['ordercp']; } // sort by count if (1 == $ordercount) { usort($rows, 'hist_rowcmp_count_desc'); } else { if (0 == $ordercount) { usort($rows, 'hist_rowcmp_count_asc'); } else { if (1 == $ordercopyright) { usort($rows, 'hist_rowcmp_desc'); } else { if (0 == $ordercopyright) { usort($rows, 'hist_rowcmp'); } else { usort($rows, 'hist_rowcmp_count_desc'); } } } } // default as sorting by count desc /* note $rows indexes may not be contiguous due to unset in step 3 */ return $rows; }
/** * \brief Remove unwanted rows by hash and type and * exclusions and filter * \param $NumRows - the number of instances. * \return new array and $NumRows */ function GetRequestedRows($rows, $hash, $type, $excl, &$NumRows, $filter) { global $PG_CONN; $NumRows = count($rows); $prev = 0; $ExclArray = explode(":", $excl); /* filter will need to know the rf_pk of "No_license_found" or "Void" */ if (!empty($filter)) { $NoLicStr = "No_license_found"; $VoidLicStr = "Void"; $rf_clause = ""; $sql = "select rf_pk from license_ref where rf_shortname IN ('{$NoLicStr}', '{$VoidLicStr}')"; $result = pg_query($PG_CONN, $sql); DBCheckResult($result, $sql, __FILE__, __LINE__); if (pg_num_rows($result) > 0) { $rf_rows = pg_fetch_all($result); foreach ($rf_rows as $row) { if (!empty($rf_clause)) { $rf_clause .= " or "; } $rf_clause .= " rf_fk={$row['rf_pk']}"; } } pg_free_result($result); } for ($RowIdx = 0; $RowIdx < $NumRows; $RowIdx++) { $row = $rows[$RowIdx]; /* Remove undesired types */ if ($rows[$RowIdx]['type'] != $type) { unset($rows[$RowIdx]); continue; } /* remove excluded files */ if ($excl) { $FileExt = GetFileExt($rows[$RowIdx]['ufile_name']); if (in_array($FileExt, $ExclArray)) { unset($rows[$RowIdx]); continue; } } /* apply filters */ if ($filter == "nolics" and $rf_clause) { /* discard file unless it has no license */ $sql = "select rf_fk from license_file where ({$rf_clause}) and pfile_fk={$row['pf']}"; $result = pg_query($PG_CONN, $sql); DBCheckResult($result, $sql, __FILE__, __LINE__); $FoundRows = pg_num_rows($result); pg_free_result($result); if ($FoundRows == 0) { unset($rows[$RowIdx]); continue; } } /* rewrite content for easier human assimilation */ if (MassageContent($rows[$RowIdx], $hash)) { unset($rows[$RowIdx]); } } /* reset array keys, keep order (uploadtree_pk) */ $rows2 = array(); foreach ($rows as $row) { $rows2[] = $row; } unset($rows); /* remove duplicate files */ $NumRows = count($rows2); $prev = 0; for ($RowIdx = 0; $RowIdx < $NumRows; $RowIdx++) { if ($RowIdx > 0) { /* Since rows are ordered by uploadtree_pk, * remove duplicate uploadtree_pk's. This can happen if there * are multiple same copyrights in one file. */ if ($rows2[$RowIdx - 1]['uploadtree_pk'] == $rows2[$RowIdx]['uploadtree_pk']) { unset($rows2[$RowIdx - 1]); } } } /* sort by name so output has some order */ usort($rows2, 'copyright_namecmp'); return $rows2; }