Skip to content

Instantly share code, notes, and snippets.

@markoheijnen
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markoheijnen/aeae3d938d3b88608291 to your computer and use it in GitHub Desktop.
Save markoheijnen/aeae3d938d3b88608291 to your computer and use it in GitHub Desktop.
Remove duplicated originals
<?php
require_once dirname( dirname( __FILE__ ) ) . '/gp-load.php';
class GP_Script_Remove_Duplicated_Originals extends GP_CLI {
var $usage = "<path>";
public function run() {
if ( empty( $this->args ) ) {
$this->usage();
}
$project_slug = array_shift( $this->args );
$project = GP::$project->by_path( $project_slug );
if ( ! $project ) {
$this->to_stderr( "Project '$project_slug' doesn't exist." );
exit( 1 );
}
$duplicated = $this->get_duplicated_originals( $project->id );
echo "Start removeing duplicated originals for " . $project->name . ".\n\n";
if ( $duplicated ) {
foreach ( $duplicated as $originals ) {
$oldest_original = array_shift( $originals );
foreach ( $originals as $original ) {
if ( strtotime( $original->date_added ) < strtotime('2014-11-20 00:00:00') ) {
continue;
}
GP::$original->delete_all( array( 'id' => $original->id ) );
echo "Deleted original " . $original->id . ".\n";
if ( in_array( 'move-translations', $this->args ) ) {
GP::$translation->update( array( 'original_id' => $oldest_original->id ), array( 'original_id' => $original->id ) );
}
else if ( in_array( 'delete-translations', $this->args ) ) {
GP::$translation->delete( array( 'original_id' => $original->id ) );
}
}
}
wp_cache_delete( $project->id, 'active_originals_count_by_project_id' );
}
else {
echo "No duplicated originals.\n";
}
}
public function get_duplicated_originals( $project_id ) {
// We could remove status here
$originals = GP::$original->many_no_map( "SELECT * FROM " . GP::$original->table . " WHERE project_id= %d AND status = '+active'", $project_id );
$hashmap = array();
foreach ( $originals as $original ) {
$hash = $original->context . '-' . $original->singular . '-' . $original->plural . '-' . $original->references . '-' . $original->comment;
if ( ! array_key_exists( $hash, $hashmap ) ) {
$hashmap[$hash] = array();
}
$hashmap[ $hash ][] = $original;
}
return array_filter( $hashmap, array( $this, 'more_then_one' ) );
}
public function more_then_one( $object ) {
if ( count( $object ) > 1 ) {
return true;
}
return false;
}
}
$gp_script = new GP_Script_Remove_Duplicated_Originals;
$gp_script->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment