120 lines
3.6 KiB
PHP
120 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* MLP5 content relations writer.
|
|
*
|
|
* Creates relationship groups in MLP5 from MLP2 source data.
|
|
*
|
|
* MLP5 schema:
|
|
* mlp_relationships (id BIGINT PK AUTO_INCREMENT, type VARCHAR(36))
|
|
* mlp_content_relations (relationship_id BIGINT, site_id BIGINT, content_id BIGINT, PK(relationship_id, site_id))
|
|
*
|
|
* @package Robotstxt_Mlp2_Mlp5
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class Robotstxt_Mlp2_Mlp5_Writer_Content_Relations
|
|
*/
|
|
class Robotstxt_Mlp2_Mlp5_Writer_Content_Relations {
|
|
|
|
/**
|
|
* Check whether any MLP5 relationship already references a given (site_id, content_id) pair.
|
|
*
|
|
* Used for idempotency: if the pair is already in MLP5, the group is skipped.
|
|
*
|
|
* @param int $site_id Site ID.
|
|
* @param int $content_id Post or term ID.
|
|
* @return bool
|
|
*/
|
|
public static function pair_exists_in_mlp5( int $site_id, int $content_id ): bool {
|
|
global $wpdb;
|
|
|
|
$mlp5 = new Robotstxt_Mlp2_Mlp5_Detector_Mlp5();
|
|
$table = $mlp5->get_content_relations_table();
|
|
|
|
if ( '' === $table ) {
|
|
return false;
|
|
}
|
|
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$found = (int) $wpdb->get_var(
|
|
$wpdb->prepare(
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
|
"SELECT COUNT(*) FROM `{$table}` WHERE site_id = %d AND content_id = %d",
|
|
$site_id,
|
|
$content_id
|
|
)
|
|
);
|
|
|
|
return $found > 0;
|
|
}
|
|
|
|
/**
|
|
* Create a relationship group in MLP5 for the given members.
|
|
*
|
|
* Inserts one row into mlp_relationships and one row per member into
|
|
* mlp_content_relations. Wrapped in a transaction for atomicity.
|
|
*
|
|
* @param string $type WordPress content type: post type slug or taxonomy name.
|
|
* Pass 'post' for standard posts, 'term' for terms.
|
|
* @param array[] $members Array of ['site_id' => int, 'element_id' => int].
|
|
* @return bool True on success, false on any DB error.
|
|
*/
|
|
public static function create_group( string $type, array $members ): bool {
|
|
global $wpdb;
|
|
|
|
$mlp5 = new Robotstxt_Mlp2_Mlp5_Detector_Mlp5();
|
|
$rel_table = $mlp5->get_relationships_table();
|
|
$cont_table = $mlp5->get_content_relations_table();
|
|
|
|
if ( '' === $rel_table || '' === $cont_table ) {
|
|
return false;
|
|
}
|
|
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$wpdb->query( 'START TRANSACTION' );
|
|
|
|
// Insert relationship group.
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$inserted = $wpdb->insert(
|
|
$rel_table,
|
|
array( 'type' => $type ),
|
|
array( '%s' )
|
|
);
|
|
|
|
if ( false === $inserted ) {
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$wpdb->query( 'ROLLBACK' );
|
|
return false;
|
|
}
|
|
|
|
$relationship_id = (int) $wpdb->insert_id;
|
|
|
|
// Insert each member.
|
|
foreach ( $members as $member ) {
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$ok = $wpdb->insert(
|
|
$cont_table,
|
|
array(
|
|
'relationship_id' => $relationship_id,
|
|
'site_id' => (int) $member['site_id'],
|
|
'content_id' => (int) $member['element_id'],
|
|
),
|
|
array( '%d', '%d', '%d' )
|
|
);
|
|
|
|
if ( false === $ok ) {
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$wpdb->query( 'ROLLBACK' );
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
|
$wpdb->query( 'COMMIT' );
|
|
return true;
|
|
}
|
|
}
|