ED Plantillas
De FSF
Instala y sincroniza plantiallas
Ejecuta así
- cd /d C:\xampp\htdocs\scripts && C:\xampp\php\php.exe sync_templates.php
<?php
if (php_sapi_name() !== 'cli') {
die("Solo CLI\n");
}
$fsfApi = "http://localhost/00_FSF/api.php";
$edApi = "http://localhost/00_ED/api.php";
$user = "TU_USUARIO_ED";
$pass = "TU_PASSWORD_ED";
// =========================
// REQUEST BASE (con cookies)
// =========================
function request($url, $params, $cookie = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
}
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res, true);
}
// =========================
// LOGIN
// =========================
function login($api, $user, $pass, $cookie) {
$token = request($api, [
"action" => "query",
"meta" => "tokens",
"type" => "login",
"format" => "json"
], $cookie);
$loginToken = $token["query"]["tokens"]["logintoken"];
request($api, [
"action" => "login",
"lgname" => $user,
"lgpassword" => $pass,
"lgtoken" => $loginToken,
"format" => "json"
], $cookie);
}
// =========================
// TOKEN CSRF
// =========================
function getCsrf($api, $cookie) {
$res = request($api, [
"action" => "query",
"meta" => "tokens",
"format" => "json"
], $cookie);
return $res["query"]["tokens"]["csrftoken"];
}
// =========================
// TODAS LAS PLANTILLAS FSF
// =========================
function getTemplates($api) {
$list = [];
$cont = null;
do {
$params = [
"action" => "query",
"list" => "allpages",
"apnamespace" => 10,
"aplimit" => "max",
"format" => "json"
];
if ($cont) $params["apcontinue"] = $cont;
$res = file_get_contents($api . "?" . http_build_query($params));
$res = json_decode($res, true);
foreach ($res["query"]["allpages"] as $p) {
$list[] = $p["title"];
}
$cont = $res["continue"]["apcontinue"] ?? null;
} while ($cont);
return $list;
}
// =========================
// CONTENIDO (FSF o ED)
// =========================
function getContent($api, $title) {
$res = file_get_contents($api . "?" . http_build_query([
"action" => "query",
"prop" => "revisions",
"rvprop" => "content",
"format" => "json",
"titles" => $title
]));
$res = json_decode($res, true);
foreach ($res["query"]["pages"] as $p) {
if (isset($p["revisions"][0]["*"])) {
return $p["revisions"][0]["*"];
}
}
return null;
}
// =========================
// GUARDAR EN ED
// =========================
function save($api, $title, $content, $token, $cookie) {
request($api, [
"action" => "edit",
"title" => $title,
"text" => $content,
"token" => $token,
"format" => "json"
], $cookie);
}
// =========================
// EJECUCIÓN
// =========================
$cookie = __DIR__ . "/cookie.txt";
echo "Login...\n";
login($edApi, $user, $pass, $cookie);
$csrf = getCsrf($edApi, $cookie);
echo "Obteniendo plantillas...\n";
$templates = getTemplates($fsfApi);
echo "Total: " . count($templates) . "\n\n";
foreach ($templates as $tpl) {
echo "Sync $tpl ... ";
$fsfContent = getContent($fsfApi, $tpl);
$edContent = getContent($edApi, $tpl);
if (!$fsfContent) {
echo "ERROR (FSF vacío)\n";
continue;
}
if ($fsfContent !== $edContent) {
save($edApi, $tpl, $fsfContent, $csrf, $cookie);
echo "ACTUALIZADO\n";
} else {
echo "SIN CAMBIOS\n";
}
}
echo "\n✔ SINCRONIZACIÓN COMPLETA\n";