Skip to content

Commit ccbbf3e

Browse files
committed
upgrade db class
1 parent 5803f1b commit ccbbf3e

File tree

3 files changed

+243
-15
lines changed

3 files changed

+243
-15
lines changed

includes/class-db.php

+72-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,84 @@
11
<?php
2-
2+
/**
3+
* Upgrade Class
4+
*
5+
* @package Webmention
6+
*/
37
namespace Webmention;
48

5-
class DB {
9+
/**
10+
* Upgrade Class
11+
*
12+
* @package Webmention
13+
*/
14+
class Upgrade {
615
/**
7-
* Which internal datastructure version we are running on.
16+
* Get the current version.
817
*
9-
* @var int
18+
* @return int
1019
*/
11-
private static $target_version = '1.0.1';
20+
public static function get_version() {
21+
return get_option( 'webmention_db_version', 0 );
22+
}
1223

13-
public static function get_target_version() {
14-
return self::$target_version;
24+
/**
25+
* Whether the database structure is up to date.
26+
*
27+
* @return bool True if the database structure is up to date, false otherwise.
28+
*/
29+
public static function is_latest_version() {
30+
return (bool) \version_compare(
31+
self::get_version(),
32+
WEBMENTION_VERSION,
33+
'=='
34+
);
1535
}
1636

17-
public static function get_version() {
18-
return get_option( 'webmention_db_version', 0 );
37+
/**
38+
* Locks the database migration process to prevent simultaneous migrations.
39+
*
40+
* @return bool|int True if the lock was successful, timestamp of existing lock otherwise.
41+
*/
42+
public static function lock() {
43+
global $wpdb;
44+
45+
// Try to lock.
46+
$lock_result = (bool) $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", 'activitypub_migration_lock', \time() ) ); // phpcs:ignore WordPress.DB
47+
48+
if ( ! $lock_result ) {
49+
$lock_result = \get_option( 'activitypub_migration_lock' );
50+
}
51+
52+
return $lock_result;
53+
}
54+
55+
/**
56+
* Unlocks the database migration process.
57+
*/
58+
public static function unlock() {
59+
\delete_option( 'activitypub_migration_lock' );
60+
}
61+
62+
/**
63+
* Whether the database migration process is locked.
64+
*
65+
* @return boolean
66+
*/
67+
public static function is_locked() {
68+
$lock = \get_option( 'activitypub_migration_lock' );
69+
70+
if ( ! $lock ) {
71+
return false;
72+
}
73+
74+
$lock = (int) $lock;
75+
76+
if ( $lock < \time() - 1800 ) {
77+
self::unlock();
78+
return false;
79+
}
80+
81+
return true;
1982
}
2083

2184
/**

includes/class-upgrade.php

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* Database Class
4+
*
5+
* @package Webmention
6+
*/
7+
namespace Webmention;
8+
9+
/**
10+
* Database Class
11+
*
12+
* @package Webmention
13+
*/
14+
class DB {
15+
/**
16+
* Which internal datastructure version we are running on.
17+
*
18+
* @var int
19+
*/
20+
private static $target_version = '1.0.1';
21+
22+
public static function get_target_version() {
23+
return self::$target_version;
24+
}
25+
26+
public static function get_version() {
27+
return get_option( 'webmention_db_version', 0 );
28+
}
29+
30+
/**
31+
* Whether the database structure is up to date.
32+
*
33+
* @return bool
34+
*/
35+
public static function is_latest_version() {
36+
return (bool) version_compare(
37+
self::get_version(),
38+
self::get_target_version(),
39+
'=='
40+
);
41+
}
42+
43+
/**
44+
* Updates the database structure if necessary.
45+
*/
46+
public static function update_database() {
47+
if ( self::is_latest_version() ) {
48+
return;
49+
}
50+
51+
$version_from_db = self::get_version();
52+
53+
if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
54+
self::migrate_to_1_0_0();
55+
}
56+
if ( version_compare( $version_from_db, '1.0.1', '<' ) ) {
57+
self::migrate_to_1_0_1();
58+
}
59+
60+
update_option( 'webmention_db_version', self::$target_version );
61+
}
62+
63+
/**
64+
* Rename meta keys.
65+
*
66+
* @param string $old The old commentmeta key
67+
* @param string $new The new commentmeta key
68+
*/
69+
public static function update_commentmeta_key( $old, $new ) {
70+
global $wpdb;
71+
72+
$wpdb->update(
73+
$wpdb->commentmeta,
74+
array( 'meta_key' => $new ),
75+
array( 'meta_key' => $old ),
76+
array( '%s' ),
77+
array( '%s' )
78+
);
79+
}
80+
81+
/**
82+
* Rename option keys.
83+
*
84+
* @param string $old The old option key
85+
* @param string $new The new option key
86+
*/
87+
public static function update_options_key( $old, $new ) {
88+
global $wpdb;
89+
90+
$wpdb->update(
91+
$wpdb->options,
92+
array( 'options_name' => $new ),
93+
array( 'options_name' => $old ),
94+
array( '%s' ),
95+
array( '%s' )
96+
);
97+
}
98+
99+
/**
100+
* The Migration for Plugin Version 5.0.0 and DB Version 1.0.0
101+
*
102+
* @since 5.0.0
103+
*
104+
* @return void
105+
*/
106+
public static function migrate_to_1_0_0() {
107+
wp_cache_flush();
108+
109+
// 1. rename comment meta
110+
self::update_commentmeta_key( 'semantic_linkbacks_source', 'webmention_source_url' );
111+
self::update_commentmeta_key( 'semantic_linkbacks_avatar', 'avatar' );
112+
self::update_commentmeta_key( 'semantic_linkbacks_canonical', 'url' );
113+
// 2. migrate comment type
114+
global $wpdb;
115+
116+
//Migrate Webmentions to comment types.
117+
$wpdb->query(
118+
"UPDATE {$wpdb->comments} comment SET comment_type = ( SELECT meta_value FROM {$wpdb->commentmeta} WHERE comment_id = comment.comment_ID AND meta_key = 'semantic_linkbacks_type' LIMIT 1 ) WHERE comment_type = 'webmention'"
119+
);
120+
121+
// Add protocol designation for Webmentions.
122+
$wpdb->query(
123+
"UPDATE {$wpdb->commentmeta} SET meta_key = 'protocol', meta_value = 'webmention' WHERE meta_key = 'semantic_linkbacks_type' OR meta_key = 'webmention_type'"
124+
);
125+
}
126+
127+
/**
128+
* Migrate to version 1.0.1
129+
*
130+
* @since 5.1.0
131+
*
132+
* @return void
133+
*/
134+
public static function migrate_to_1_0_1() {
135+
wp_cache_flush();
136+
137+
$comments = get_comments(
138+
array(
139+
'fields' => 'ids',
140+
'meta_query' => array(
141+
array(
142+
'key' => 'mf2_author',
143+
'compare' => 'EXISTS',
144+
),
145+
array(
146+
'key' => 'protocol',
147+
'value' => 'webmention',
148+
),
149+
),
150+
)
151+
);
152+
153+
foreach ( $comments as $comment_id ) {
154+
$author = get_comment_meta( $comment_id, 'mf2_author', true );
155+
$source = get_comment_meta( $comment_id, 'webmention_source_url', true );
156+
if ( is_array( $author ) ) {
157+
if ( array_key_exists( 'url', $author ) && ( $source !== $author['url'] ) ) {
158+
$comment = get_comment( $comment_id, ARRAY_A );
159+
$comment['comment_author_url'] = $author['url'];
160+
wp_update_comment( $comment );
161+
}
162+
}
163+
}
164+
}
165+
}

webmention.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
\define( 'WEBMENTION_PLUGIN_FILE', \plugin_dir_path( __FILE__ ) . '/' . \basename( __FILE__ ) );
2222
\define( 'WEBMENTION_PLUGIN_URL', \plugin_dir_url( __FILE__ ) );
2323

24-
require_once __DIR__ . '/includes/class-autoloader.php';
25-
require_once __DIR__ . '/includes/functions.php';
24+
require_once WEBMENTION_PLUGIN_DIR . '/includes/class-autoloader.php';
25+
require_once WEBMENTION_PLUGIN_DIR . '/includes/functions.php';
2626

2727
if ( \WP_DEBUG ) {
28-
require_once __DIR__ . '/includes/debug.php';
28+
require_once WEBMENTION_PLUGIN_DIR . '/includes/debug.php';
2929
}
3030

3131
// Register the autoloader.
32-
Autoloader::register_path( __NAMESPACE__, __DIR__ . '/includes' );
32+
Autoloader::register_path( __NAMESPACE__, WEBMENTION_PLUGIN_DIR . '/includes' );
3333

3434
// Initialize the plugin.
3535
$webmention = Webmention::get_instance();
@@ -39,7 +39,7 @@
3939
* Plugin Version Number used for caching.
4040
*/
4141
function version() {
42-
return $webmention->get_version();
42+
return Webmention::get_instance()->get_version();
4343
}
4444

4545
/**
@@ -80,5 +80,5 @@ function get_plugin_meta( $default_headers = array() ) {
8080

8181
// Check for CLI env, to add the CLI commands
8282
if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
83-
\WP_CLI::add_command( 'webmention', '\Webmention\Cli' );
83+
\WP_CLI::add_command( 'webmention', Cli::class );
8484
}

0 commit comments

Comments
 (0)