182 lines
5.4 KiB
Python
182 lines
5.4 KiB
Python
import json
|
|
import stat
|
|
from zipfile import BadZipFile, ZipFile
|
|
|
|
from .account_backup import MANIFEST_NAME, read_backup_manifest
|
|
from .account_backup_request import backup_archive
|
|
from .database_node import DEFAULT_SOCKET
|
|
from .node_files import check_root_file
|
|
from .wordpress_updates import DATABASE_NAME, WordPressUpdate
|
|
|
|
DATABASE_TARGET = (
|
|
"echo wp_json_encode( [ 'name' => DB_NAME, 'host' => DB_HOST, "
|
|
"'multisite' => is_multisite() ] );"
|
|
)
|
|
OFFICIAL_DOWNLOADS = "https://downloads.wordpress.org/"
|
|
|
|
|
|
def database_name(cli, root):
|
|
try:
|
|
regular_config = stat.S_ISREG((root / "wp-config.php").lstat().st_mode)
|
|
except FileNotFoundError:
|
|
regular_config = False
|
|
if not regular_config:
|
|
return ""
|
|
data = cli.require(
|
|
root,
|
|
"identify the WordPress database",
|
|
"eval",
|
|
DATABASE_TARGET,
|
|
read=True,
|
|
).stdout
|
|
try:
|
|
target = json.loads(data)
|
|
name = target["name"]
|
|
if (
|
|
target["host"] in ("localhost", f"localhost:{DEFAULT_SOCKET}")
|
|
and target["multisite"] is False
|
|
and isinstance(name, str)
|
|
and DATABASE_NAME.fullmatch(name)
|
|
):
|
|
return name
|
|
except (ValueError, KeyError, TypeError):
|
|
raise ValueError("WP-CLI returned invalid database details.") from None
|
|
return ""
|
|
|
|
|
|
def wp_json(cli, root, *arguments):
|
|
result = cli.require(
|
|
root,
|
|
"read the update information",
|
|
*arguments,
|
|
"--format=json",
|
|
read=True,
|
|
)
|
|
try:
|
|
return json.loads(result.stdout)
|
|
except ValueError:
|
|
raise ValueError(
|
|
"WP-CLI returned invalid update information."
|
|
) from None
|
|
|
|
|
|
def core_updates(cli, root, installed):
|
|
entries = wp_json(
|
|
cli, root, "core", "check-update", "--force-check", "--fields=version"
|
|
)
|
|
return [
|
|
WordPressUpdate("core", "wordpress", installed, entry["version"])
|
|
for entry in entries
|
|
]
|
|
|
|
|
|
def extension_updates(cli, root, kind):
|
|
entries = wp_json(
|
|
cli,
|
|
root,
|
|
kind,
|
|
"list",
|
|
"--fields=name,version,update_version,update_package",
|
|
"--update=available",
|
|
)
|
|
updates = []
|
|
for entry in entries:
|
|
slug = entry["name"]
|
|
target = entry["update_version"]
|
|
package = entry["update_package"]
|
|
directory = "plugin" if kind == "plugin" else "theme"
|
|
if package != f"{OFFICIAL_DOWNLOADS}{directory}/{slug}.{target}.zip":
|
|
continue
|
|
try:
|
|
updates.append(
|
|
WordPressUpdate(kind, slug, entry["version"], target)
|
|
)
|
|
except ValueError:
|
|
continue
|
|
return updates
|
|
|
|
|
|
def update_inventory(cli, root, installed, kind=None):
|
|
if not installed:
|
|
return []
|
|
try:
|
|
updates = (
|
|
core_updates(cli, root, installed)
|
|
if kind in (None, "core")
|
|
else []
|
|
)
|
|
for extension in ("plugin", "theme"):
|
|
if kind in (None, extension):
|
|
updates.extend(extension_updates(cli, root, extension))
|
|
except (KeyError, TypeError):
|
|
raise ValueError(
|
|
"WP-CLI returned invalid update information."
|
|
) from None
|
|
return updates
|
|
|
|
|
|
def verify_recovery_archive(request):
|
|
archive = backup_archive(request.site.account_id, request.backup_id)
|
|
try:
|
|
check_root_file(archive, mode=0o600)
|
|
with ZipFile(archive) as bundle:
|
|
with bundle.open(MANIFEST_NAME) as source:
|
|
backup = read_backup_manifest(source)
|
|
resources = [
|
|
resource
|
|
for resource, manifest, archive_name in backup.components()
|
|
if bundle.getinfo(manifest).file_size
|
|
and bundle.getinfo(archive_name).file_size
|
|
]
|
|
except (FileNotFoundError, BadZipFile, KeyError, ValueError):
|
|
raise ValueError(
|
|
"The WordPress recovery archive is unavailable."
|
|
) from None
|
|
if set(resources) != {request.site, request.database}:
|
|
raise ValueError(
|
|
"The recovery archive must include this site and database."
|
|
)
|
|
|
|
|
|
def apply_update(request, cli, root, installed):
|
|
update = WordPressUpdate.from_manifest(request.update)
|
|
verify_recovery_archive(request)
|
|
if database_name(cli, root) != request.database.name:
|
|
raise ValueError(
|
|
"The WordPress database changed or is no longer local."
|
|
)
|
|
if update not in update_inventory(cli, root, installed, update.kind):
|
|
raise ValueError(
|
|
"The selected update changed. Check for updates again."
|
|
)
|
|
|
|
version = f"--version={update.version}"
|
|
if update.kind == "core":
|
|
cli.require(root, "update WordPress core", "core", "update", version)
|
|
cli.require(root, "update the WordPress database", "core", "update-db")
|
|
return
|
|
cli.require(
|
|
root,
|
|
f"update the {update.kind}",
|
|
update.kind,
|
|
"update",
|
|
update.slug,
|
|
version,
|
|
)
|
|
result = cli.require(
|
|
root,
|
|
f"verify the {update.kind} version",
|
|
update.kind,
|
|
"get",
|
|
update.slug,
|
|
"--field=version",
|
|
read=True,
|
|
)
|
|
if (
|
|
result.stdout.decode("ascii", errors="replace").strip()
|
|
!= update.version
|
|
):
|
|
raise ValueError(
|
|
"The updated component version could not be verified."
|
|
)
|