summaryrefslogtreecommitdiff
path: root/bin/wiki/GeneraSnapshot.php
blob: ab6cdd6f592c2cdfde31feb82dc0570112ec2219 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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 de objeto de Prensa, verifica que tenga la propiedad prensa:url definida y genera una subpagina '/Archivo' donde se almacena una copia del contenido en texto plano del original y se añade una captura de pantalla de la página fuente.
# ----


// 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 );

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

function GeneraSnapshot($pagetitle, $services) {
  $page = $services->newPageGetter()->getFromTitle( $pagetitle );
  $pagecontent = $page->getRevisions()->getLatest();
  if (!$pagecontent) {
    echo "No existe una página con el nombre '$pagetitle'\n";
    return;
  }

  $page_archivo = $services->newPageGetter()->getFromTitle( $pagetitle . '/Archivo');
  $pagecontent_archivo = $page_archivo->getRevisions()->getLatest();
  if ($pagecontent_archivo) {
    echo "La página '$pagetitle' ya tiene una subpágina /Archivo\n";
    return;
  }

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

  // armo un array con las propiedades de SMW
  $rows = explode("|", $pagecontent);
  foreach($rows as $row => $data)
  {
    $row_data = explode('=', $data);
    $page_properties[$row_data[0]] = $row_data[1];
  }

  if (array_key_exists('prensa:url', $page_properties)) {
    $urltmp = $page_properties['prensa:url'];
    $url = preg_replace( "/\r|\n/", "", $urltmp );
    // echo $url;
  } else {
    echo "La página '$pagetitle' no tiene definida la propiedad 'prensa:url'\n";
    return;
  }
  // Obtiene copia de texto del articulo
  $command = 'curl -Ls ' . $url . ' | unfluff | jq -r .text > /tmp/linkcontent';
  // echo "$command\n";
  exec($command);

  // Almacena la imagen
  exec('wget "https://manet.herokuapp.com/?width=1920&hesight=1080&quality=0.3&url=`echo ' . $url . ' | perl -p -e \'s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg\'`" -O /tmp/temp.jpg && convert -strip -interlace Plane -quality 25% /tmp/temp.jpg /tmp/final.jpg');

  // Definimos el nombre del archivo de la imagen
  $pagename = str_replace( ":", "-", $pagetitle);
  $snapshot_name = $pagename . '-snapshot.jpg';
  $snaphot_pagecontent = 'Snapshot generada para [['. $pagetitle .']]';
  $snaphot_desc = 'Snapshot generada para [['. $pagetitle .']]';
  $fileUploader = $services->newFileUploader();
  $fileUploader->setChunkSize( 1024 * 1024 * 10 );
  $fileUploader->upload( $snapshot_name, '/tmp/final.jpg', $snaphot_pagecontent, $snaphot_desc );

  // Crea nueva o actualiza existente
  $fileContent = file_get_contents('/tmp/linkcontent');
  $fileContent = iconv("UTF-8","ISO-8859-1//IGNORE",$fileContent);
  $fileContent = iconv("ISO-8859-1","UTF-8",$fileContent);
  $linkContent = '
  {{Prensa-Archivo
  |prensa:archivo:imagen='. $snapshot_name.'
  |prensa:archivo:texto=';
  $linkContent .= $fileContent;
  $linkContent .= '
  }}';

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

}


if ($argv[1]) {
  GeneraSnapshot($argv[1],$services);
} else {
  echo "Al no indicar una página, voy a generar los snapshots de todas las pagínas que usen Plantilla:Prensa y que no tengan snapshot \n";

  // Obtengo todas las paginas con Plantilla:Prensa
  $pageListGetter = $services->newPageListGetter();
  $examplePages =  $pageListGetter->getPageListFromPageTransclusions( 'Template:Prensa' );
  $array = accessProtected($examplePages,'pages');
  foreach ( $array as $exPage ) {
      $pagename = $exPage->getTitle()->getText();
      echo "$pagename \n";
      GeneraSnapshot($pagename, $services);
  }
}




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





// print_r($examplePages);

// $myArray = json_decode(json_encode($examplePages), true);
// print_r($examplePages->getPageList());






?>