kollapsminoriteten/wp-admin/includes/class-wp-importer.php

334 lines
7.3 KiB
PHP
Raw Normal View History

2019-11-02 10:38:58 +01:00
<?php
/**
* WP_Importer base class
*/
2022-12-15 17:47:31 +01:00
#[AllowDynamicProperties]
2019-11-02 10:38:58 +01:00
class WP_Importer {
/**
* Class Constructor
*/
public function __construct() {}
/**
2023-05-23 23:18:12 +02:00
* Returns array with imported permalinks from WordPress database.
2019-11-02 10:38:58 +01:00
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $importer_name
2021-07-23 11:58:50 +02:00
* @param string $blog_id
2019-11-02 10:38:58 +01:00
* @return array
*/
2021-07-23 11:58:50 +02:00
public function get_imported_posts( $importer_name, $blog_id ) {
2019-11-02 10:38:58 +01:00
global $wpdb;
$hashtable = array();
$limit = 100;
$offset = 0;
2020-05-06 17:23:38 +02:00
// Grab all posts in chunks.
2019-11-02 10:38:58 +01:00
do {
2021-07-23 11:58:50 +02:00
$meta_key = $importer_name . '_' . $blog_id . '_permalink';
2019-11-02 10:38:58 +01:00
$sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit );
$results = $wpdb->get_results( $sql );
2020-05-06 17:23:38 +02:00
// Increment offset.
2019-11-02 10:38:58 +01:00
$offset = ( $limit + $offset );
if ( ! empty( $results ) ) {
foreach ( $results as $r ) {
2020-05-06 17:23:38 +02:00
// Set permalinks into array.
2020-12-10 14:06:04 +01:00
$hashtable[ $r->meta_value ] = (int) $r->post_id;
2019-11-02 10:38:58 +01:00
}
}
2023-09-26 10:33:34 +02:00
} while ( count( $results ) === $limit );
2019-11-02 10:38:58 +01:00
return $hashtable;
}
/**
2023-05-23 23:18:12 +02:00
* Returns count of imported permalinks from WordPress database.
2019-11-02 10:38:58 +01:00
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $importer_name
2021-07-23 11:58:50 +02:00
* @param string $blog_id
2019-11-02 10:38:58 +01:00
* @return int
*/
2021-07-23 11:58:50 +02:00
public function count_imported_posts( $importer_name, $blog_id ) {
2019-11-02 10:38:58 +01:00
global $wpdb;
$count = 0;
2020-05-06 17:23:38 +02:00
// Get count of permalinks.
2021-07-23 11:58:50 +02:00
$meta_key = $importer_name . '_' . $blog_id . '_permalink';
2019-11-15 22:59:44 +01:00
$sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key );
2019-11-02 10:38:58 +01:00
$result = $wpdb->get_results( $sql );
if ( ! empty( $result ) ) {
2020-12-10 14:06:04 +01:00
$count = (int) $result[0]->cnt;
2019-11-02 10:38:58 +01:00
}
return $count;
}
/**
2023-05-23 23:18:12 +02:00
* Sets array with imported comments from WordPress database.
2019-11-02 10:38:58 +01:00
*
* @global wpdb $wpdb WordPress database abstraction object.
*
2021-07-23 11:58:50 +02:00
* @param string $blog_id
2019-11-02 10:38:58 +01:00
* @return array
*/
2021-07-23 11:58:50 +02:00
public function get_imported_comments( $blog_id ) {
2019-11-02 10:38:58 +01:00
global $wpdb;
$hashtable = array();
$limit = 100;
$offset = 0;
2020-05-06 17:23:38 +02:00
// Grab all comments in chunks.
2019-11-02 10:38:58 +01:00
do {
$sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit );
$results = $wpdb->get_results( $sql );
2020-05-06 17:23:38 +02:00
// Increment offset.
2019-11-02 10:38:58 +01:00
$offset = ( $limit + $offset );
if ( ! empty( $results ) ) {
foreach ( $results as $r ) {
2020-05-06 17:23:38 +02:00
// Explode comment_agent key.
2021-07-23 11:58:50 +02:00
list ( $comment_agent_blog_id, $source_comment_id ) = explode( '-', $r->comment_agent );
$source_comment_id = (int) $source_comment_id;
2019-11-02 10:38:58 +01:00
2020-05-06 17:23:38 +02:00
// Check if this comment came from this blog.
2023-12-07 09:44:11 +01:00
if ( (int) $blog_id === (int) $comment_agent_blog_id ) {
2020-12-10 14:06:04 +01:00
$hashtable[ $source_comment_id ] = (int) $r->comment_ID;
2019-11-02 10:38:58 +01:00
}
}
}
2023-09-26 10:33:34 +02:00
} while ( count( $results ) === $limit );
2019-11-02 10:38:58 +01:00
return $hashtable;
}
/**
* @param int $blog_id
* @return int|void
*/
public function set_blog( $blog_id ) {
if ( is_numeric( $blog_id ) ) {
$blog_id = (int) $blog_id;
} else {
2019-11-15 22:59:44 +01:00
$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id );
$parsed = parse_url( $blog );
if ( ! $parsed || empty( $parsed['host'] ) ) {
2019-11-02 10:38:58 +01:00
fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" );
2020-09-15 14:29:22 +02:00
exit;
2019-11-02 10:38:58 +01:00
}
if ( empty( $parsed['path'] ) ) {
$parsed['path'] = '/';
}
$blogs = get_sites(
array(
'domain' => $parsed['host'],
'number' => 1,
'path' => $parsed['path'],
)
);
if ( ! $blogs ) {
fwrite( STDERR, "Error: Could not find blog\n" );
2020-09-15 14:29:22 +02:00
exit;
2019-11-02 10:38:58 +01:00
}
$blog = array_shift( $blogs );
$blog_id = (int) $blog->blog_id;
}
if ( function_exists( 'is_multisite' ) ) {
if ( is_multisite() ) {
switch_to_blog( $blog_id );
}
}
return $blog_id;
}
/**
* @param int $user_id
* @return int|void
*/
public function set_user( $user_id ) {
if ( is_numeric( $user_id ) ) {
$user_id = (int) $user_id;
} else {
$user_id = (int) username_exists( $user_id );
}
if ( ! $user_id || ! wp_set_current_user( $user_id ) ) {
fwrite( STDERR, "Error: can not find user\n" );
2020-09-15 14:29:22 +02:00
exit;
2019-11-02 10:38:58 +01:00
}
return $user_id;
}
/**
2023-05-23 23:18:12 +02:00
* Sorts by strlen, longest string first.
2019-11-02 10:38:58 +01:00
*
* @param string $a
* @param string $b
* @return int
*/
public function cmpr_strlen( $a, $b ) {
return strlen( $b ) - strlen( $a );
}
/**
2023-09-26 10:33:34 +02:00
* Gets URL.
2019-11-02 10:38:58 +01:00
*
* @param string $url
* @param string $username
* @param string $password
* @param bool $head
* @return array
*/
2025-04-25 12:30:07 +02:00
public function get_page(
$url,
$username = '',
#[\SensitiveParameter]
$password = '',
$head = false
) {
2020-05-06 17:23:38 +02:00
// Increase the timeout.
2019-11-02 10:38:58 +01:00
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
$headers = array();
$args = array();
if ( true === $head ) {
$args['method'] = 'HEAD';
}
if ( ! empty( $username ) && ! empty( $password ) ) {
$headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
}
$args['headers'] = $headers;
return wp_safe_remote_request( $url, $args );
}
/**
2023-05-23 23:18:12 +02:00
* Bumps up the request timeout for http requests.
2019-11-02 10:38:58 +01:00
*
* @param int $val
* @return int
*/
public function bump_request_timeout( $val ) {
return 60;
}
/**
2023-05-23 23:18:12 +02:00
* Checks if user has exceeded disk quota.
2019-11-02 10:38:58 +01:00
*
* @return bool
*/
public function is_user_over_quota() {
if ( function_exists( 'upload_is_user_over_quota' ) ) {
if ( upload_is_user_over_quota() ) {
return true;
}
}
return false;
}
/**
2023-05-23 23:18:12 +02:00
* Replaces newlines, tabs, and multiple spaces with a single space.
2019-11-02 10:38:58 +01:00
*
2022-06-16 14:03:35 +02:00
* @param string $text
2019-11-02 10:38:58 +01:00
* @return string
*/
2022-06-16 14:03:35 +02:00
public function min_whitespace( $text ) {
return preg_replace( '|[\r\n\t ]+|', ' ', $text );
2019-11-02 10:38:58 +01:00
}
/**
* Resets global variables that grow out of control during imports.
*
* @since 3.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
2020-12-10 14:06:04 +01:00
* @global int[] $wp_actions
2019-11-02 10:38:58 +01:00
*/
public function stop_the_insanity() {
global $wpdb, $wp_actions;
// Or define( 'WP_IMPORTING', true );
$wpdb->queries = array();
2020-05-06 17:23:38 +02:00
// Reset $wp_actions to keep it from growing out of control.
2019-11-02 10:38:58 +01:00
$wp_actions = array();
}
}
/**
* Returns value of command line params.
* Exits when a required param is not set.
*
* @param string $param
* @param bool $required
* @return mixed
*/
function get_cli_args( $param, $required = false ) {
$args = $_SERVER['argv'];
if ( ! is_array( $args ) ) {
$args = array();
}
$out = array();
$last_arg = null;
$return = null;
2020-12-10 14:06:04 +01:00
$il = count( $args );
2019-11-02 10:38:58 +01:00
for ( $i = 1, $il; $i < $il; $i++ ) {
if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) {
$parts = explode( '=', $match[1] );
$key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] );
if ( isset( $parts[1] ) ) {
$out[ $key ] = $parts[1];
} else {
$out[ $key ] = true;
}
$last_arg = $key;
} elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
2019-11-15 22:59:44 +01:00
$key = $match[1][ $j ];
2019-11-02 10:38:58 +01:00
$out[ $key ] = true;
}
$last_arg = $key;
2020-05-06 17:23:38 +02:00
} elseif ( null !== $last_arg ) {
2019-11-02 10:38:58 +01:00
$out[ $last_arg ] = $args[ $i ];
}
}
2020-05-06 17:23:38 +02:00
// Check array for specified param.
2019-11-02 10:38:58 +01:00
if ( isset( $out[ $param ] ) ) {
2020-05-06 17:23:38 +02:00
// Set return value.
2019-11-02 10:38:58 +01:00
$return = $out[ $param ];
}
2020-05-06 17:23:38 +02:00
// Check for missing required param.
2019-11-02 10:38:58 +01:00
if ( ! isset( $out[ $param ] ) && $required ) {
2020-05-06 17:23:38 +02:00
// Display message and exit.
2019-11-02 10:38:58 +01:00
echo "\"$param\" parameter is required but was not specified\n";
2020-09-15 14:29:22 +02:00
exit;
2019-11-02 10:38:58 +01:00
}
return $return;
}