summaryrefslogtreecommitdiff
path: root/bin/wiki/PropiedadActualizar.php
blob: 7ed1e5cb98992f98540565190284075095866d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
# ----
# Copyright (C) 2013-2020 - Reevo Project (http://reevo.org)
# License: Affero GPL version 3 - http://www.gnu.org/licenses/agpl.html
# ES: Este script recibe el nombre de una pagina e intenta agregar o actualizar las propiedades específicadas de las mismas con el valor dado.
# Algo como: php  PropiedadActualizar.php -t=Experiencia:Summerhill -f=true -p="experiencia:descripcion|Algo" -p="experiencia:lugar-pais|AR"
# ----


// error_reporting(0);
// Load all the stuff
require_once( __DIR__ . '/vendor/autoload.php' );
require_once( __DIR__ .'/../../etc/global_config.php' );

// Log in to a wiki
$api = new \Mediawiki\Api\MediawikiApi( $wgServer .'/api.php' );
$api->login( new \Mediawiki\Api\ApiUser( $REEVO_WIKI_API_USER, $REEVO_WIKI_API_PASS ) );
$services = new \Mediawiki\Api\MediawikiFactory( $api );

$opciones = getopt('t:p:f:');

if ($opciones['t']) {
  if ($opciones['p']) {
    $title = $opciones['t'];
    if (is_string($opciones['p'])) {
      $prop[] = $opciones['p'];
    } else {
      $prop = $opciones['p'];
    }
    foreach ($prop as $key => $value) {
      $p = explode('|', $value);
      $propiedades[$p[0]] = $p[1];
    }
    $result = ActualizarPropiedades($title, $propiedades, $opciones, $services);
    echo "\nSe actualizó correctamente la página $result ($wgServer/$result)\n\n\n";
  } else {
    echo "Es necesario que definas al menos una propiedad para agregar o modificar con -p='propiedad|valor'\n";
  }
} else {
  echo "Es necesario que definas el nombe de la pagina a editar con -t='Pagina'\n";
}

// Obtengo el nombre de la página como parámetro

function ActualizarPropiedades($title, $propiedades, $opciones, $services) {

  $page = $services->newPageGetter()->getFromTitle( $title );
  $pagecontent = $page->getRevisions()->getLatest();
  if (!$pagecontent) {
    echo "\n**** ERROR: No existe una página con el nombre '$title'\n\n\n";
    exit();
  } else {
    echo "\n**** PROCESANDO: '$title' ****\n";
  }

  $pagecontent = $page->getRevisions()->getLatest()->getContent()->getData();

  $template = preg_grep('/^\{{(.*(?<!}}))$/', explode("\n", $pagecontent));
  $template_pos = array_key_first($template);
  $templatefin = preg_grep('/^\}}(.*)$/', explode("\n", $pagecontent));
  $templatefin_pos = array_key_first($templatefin);


  $extracto = preg_grep('/^\|/', explode("\n", $pagecontent));
  foreach ($extracto as $key => $value) {
    $data = str_replace('|','',$value);
    $data_limpia = explode('=', $data);
    $propiedades_previas[$data_limpia[0]] = $data_limpia[1];
  }

  // Verifico que tengo que hacer
  if ($opciones['f'] == 'true') {
    echo "\nVoy a actulizar y/o agregar las siguientes propiedades: \n";
    $propiedades_actualizables = array_intersect_key($propiedades,$propiedades_previas);
    print_r($propiedades_actualizables);
    $propiedades_final = array_replace($propiedades_previas, $propiedades);
  } else {
    echo "\nNo se van a reemplazar los valores de propiedades existentes. Agrega '-f=true' para hacerlo. Propiedadedes existentes con sus valores actuales: \n";
    $propiedades_actualizables = array_intersect_key($propiedades, $propiedades_previas);
    print_r($propiedades_actualizables);

    echo "\nPropiedades antes no definidas que voy a agregar: \n";
    $propiedades_nuevas = array_diff_key($propiedades,$propiedades_previas);
    print_r($propiedades_nuevas);
    $propiedades_final = array_replace($propiedades_previas, $propiedades_nuevas);
  }

  // Construyo un array con el formato del template original
  $propiedades_final_array[] = $template[$template_pos];
  foreach ($propiedades_final as $key => $value) {
    $propiedades_final_array[] = '|'.$key.'='.$value;
  }
  $propiedades_final_array[] = $templatefin[$templatefin_pos];

  // Hago un array con el contenido de toda la pagina y reemplazo el template viejo por el nuevo
  $contenido_array = explode("\n", $pagecontent);
  $templatefin_pos = $templatefin_pos + 1;
  array_splice($contenido_array, $template_pos, $templatefin_pos,$propiedades_final_array);

  $contenido_final = implode("\n", $contenido_array);

  $newContent = new \Mediawiki\DataModel\Content( $contenido_final );
  $pagetitle = new \Mediawiki\DataModel\Title( $title );
  $identifier = new \Mediawiki\DataModel\PageIdentifier( $pagetitle );
  $revision = new \Mediawiki\DataModel\Revision( $newContent, $identifier );
  $services->newRevisionSaver()->save( $revision );

  return $title;

}

function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

?>