-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrsspotd.php
251 lines (228 loc) · 9.68 KB
/
rsspotd.php
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
require_once './bin/globals.php';
require_once 'WikiAphpi/main.php';
header('Content-type: application/xml');
/**
* A classe PotdRss é responsável por buscar a imagem do dia (POTD) mais recentes que foi
* publicada na Wikipédia em português, extrair as informações de texto da imagem da página
* buscar informações adicionais sobre a imagem, como o nome do arquivo, o tamanho e o tipo
* do arquivo, e buscar metadados adicionais da imagem através da API do MediaWiki. As
* informações são devolvidas em um RSS ATOM.
*/
class PotdRss extends WikiAphpiUnlogged
{
/**
* Recupera o título da imagem do dia (POTD) mais recente já publicada
* @return array Título da imagem (ex: 5 de janeiro de 2023)
*/
private function fetchPotdData()
{
$atual_params = [
'action' => 'expandtemplates',
'format' => 'php',
'prop' => 'wikitext',
'text' => "{{CURRENTDAY}} de {{CURRENTMONTHNAME}} de {{CURRENTYEAR}}"
];
return array($this->see($atual_params)['expandtemplates']['wikitext']);
}
/**
* Busca o conteúdo textual da página de "Imagem em destaque" da Wikipédia.
* @param string $dayTitle Título da imagem do dia (Ex: 1 de janeiro de 2020).
* @return string O conteúdo textual da imagem.
*/
private function fetchTextData($dayTitle)
{
$text_params = [
'action' => 'parse',
'format' => 'php',
'page' => 'Wikipédia:Imagem_em_destaque/' . $dayTitle
];
$content = $this->see($text_params);
$text = $content["parse"]["text"]["*"] ?? false;
if (!$text) {
throw new Exception(print_r($content, true));
}
return $text;
}
/**
* Extrai o conteúdo do atributo "alt" de uma tag de imagem no texto fornecido.
* @param string $text O texto contendo a tag de imagem da qual deseja-se extrair o conteúdo.
* @return string O conteúdo do atributo "alt" da tag de imagem.
*/
private function extractContent($text)
{
preg_match_all('/(?<=<img alt=")[^"]*/', $text, $content);
$alt = $content["0"]["0"] ?? false;
if (!$alt) {
throw new Exception(print_r($content, true));
}
return $alt;
}
/**
* Extrai o nome do arquivo de imagem a partir do texto.
* @param string $text O texto que contém a imagem.
* @return string O nome do arquivo de imagem.
*/
private function extractFilename($text)
{
preg_match_all('/(?<=<a href="\/wiki\/Ficheiro:)[^"]*/', $text, $content);
$filename = urldecode($content["0"]["0"]);
if (!$filename) {
throw new Exception(print_r($content, true));
}
return $filename;
}
/**
* Busca os dados de uma imagem a partir de seu endereço.
* @param type string $filename O nome do arquivo de imagem.
* @return array Retorna um array com os seguintes elementos:
** string $location: O endereço final da imagem após redirecionamentos.
** int $size: O tamanho do arquivo em bytes.
** string $type: O tipo do conteúdo da imagem (MIME type).
*/
private function fetchFileInfo($filename)
{
$ch = curl_init("https://pt.wikipedia.org/wiki/Especial:Redirecionar/file/$filename?width=1000");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
$location = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE );
curl_close($ch);
return array($location, $size, $type);
}
/**
* Busca os metadados de uma imagem através da API do MediaWiki.
* @param string $filename O nome do arquivo de imagem.
* @return array Uma matriz contendo os metadados da imagem.
*/
private function fetchImageMeta($filename)
{
$api_params = [
'action' => 'query',
'format' => 'php',
'prop' => 'imageinfo',
'iiprop' => 'extmetadata',
'titles' => 'Ficheiro:' . $filename
];
$api = $this->see($api_params);
$meta = $api["query"]["pages"]["-1"]["imageinfo"]["0"]["extmetadata"] ?? false;
if (!$meta) {
throw new Exception(print_r($filename, true));
}
return $meta;
}
/**
* Constrói a descrição de um item RSS a partir do título da imagem.
* @param string $dayTitle Título da imagem.
* @param array $imageInfo Contendo as informações da imagem, incluindo o texto, nome do arquivo e metadados.
* @return string Descrição da imagem no formato de texto
*/
private function buildDescription($dayTitle, $imageInfo)
{
return "Imagem do dia em {$dayTitle}: {$this->extractContent($imageInfo['text'])}\nAutor: "
. trim(strip_tags($imageInfo['meta']["Artist"]["value"]))
. " (Licença: "
. strip_tags($imageInfo['meta']["LicenseShortName"]["value"])
. " - "
. strip_tags($imageInfo['meta']["LicenseUrl"]["value"] ?? '')
. ")\nVeja mais informações no link.\n\n#wikipedia #ptwikipedia #ptwiki #conhecimentolivre #fotododia #imagemdodia #wikicommons";
}
/**
* Cria um item do RSS a partir das informações da Imagem do Dia (POTD).
* @param array $image Array com informações do log da Imagem do Dia
* @return array Array com as informações formatadas para o RSS.
*/
private function buildRssItem($dayTitle)
{
$imageInfo = $this->fetchImageInfo($dayTitle);
$description = $this->buildDescription($dayTitle, $imageInfo);
$instagram = str_replace(
'Veja mais informações no link.',
"\nPara mais informações sobre essa imagem, entre no endereço da bio e pesquise por Imagem:{$imageInfo['filename']}",
$description
);
$link = "https://pt.wikipedia.org/wiki/WP:Imagem_em_destaque/" . rawurlencode($dayTitle);
$guid = crc32($dayTitle);
$timestamp = date('D, d M Y H:i:s O', strtotime("today"));
$fileInfo = $this->fetchFileInfo($imageInfo['filename']);
return [
'title' => $dayTitle,
'description' => $description,
'instagram' => $instagram,
'link' => $link,
'guid' => $guid,
'timestamp' => $timestamp,
'image_url' => $fileInfo['0'],
'image_length' => $fileInfo['1'],
'image_type' => $fileInfo['2']
];
}
/**
* Retorna informações sobre a imagem a partir do título.
* @param string $dayTitle Título da imagem do dia (Ex: 1 de janeiro de 2020).
* @return array Contendo as informações da imagem, incluindo o texto, nome do arquivo e metadados.
*/
private function fetchImageInfo($dayTitle)
{
$text = $this->fetchTextData($dayTitle);
$filename = $this->extractFilename($text);
$meta = $this->fetchImageMeta($filename);
return [
'text' => $text,
'filename' => $filename,
'meta' => $meta
];
}
/**
* Constrói um feed RSS a partir dos dados fornecidos.
* @param array $potd Dados da imagem do dia.
* @return string O feed RSS gerado.
*/
private function buildRss($potd)
{
$rss = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
$rss->addChild('channel');
$rss->channel->addChild('title', 'WikiPT - POTD');
$rss->channel->addChild('link', 'https://pt.wikipedia.org/');
$rss->channel->addChild('description', 'Wikipédia em português');
$atom_link = $rss->channel->addChild('atom:atom:link');
$atom_link->addAttribute('href', 'https://alberobot.toolforge.org/rsspotd.php');
$atom_link->addAttribute('rel', 'self');
$atom_link->addAttribute('type', 'application/rss+xml');
foreach ($potd as $potd_item) {
$item = $rss->channel->addChild('item');
$item->addChild('title', htmlspecialchars($potd_item["title"]));
$item->addChild('link', htmlspecialchars($potd_item["link"]));
$item->addChild('pubDate', htmlspecialchars($potd_item["timestamp"]));
$item->addChild('guid', 'https://pt.wikipedia.org/w/index.php?diff=' . $potd_item["guid"]);
$item->addChild('description', htmlspecialchars($potd_item["description"]));
$item->addChild('instagram', htmlspecialchars($potd_item["instagram"]));
$enclosure = $item->addChild('enclosure');
$enclosure->addAttribute('url', htmlspecialchars($potd_item["image_url"]));
$enclosure->addAttribute('length', htmlspecialchars($potd_item["image_length"]));
$enclosure->addAttribute('type', htmlspecialchars($potd_item["image_type"]));
}
return $rss->asXML();
}
/**
* Executa a geração do feed RSS com as informações do "Imagem do Dia" da Wikipédia em português.
* @return string Retorna o conteúdo do feed RSS gerado.
*/
public function run()
{
$potd = $this->fetchPotdData();
$items = [];
foreach ($potd as $thisDay) {
$item = $this->buildRssItem($thisDay);
$items[] = $item;
}
return $this->buildRss($items);
}
}
//Executa script
$potdRss = new PotdRss('https://pt.wikipedia.org/w/api.php');
echo $potdRss->run();