forked from allu77/TVScraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MediaWikiAPI.php
83 lines (72 loc) · 2.29 KB
/
MediaWikiAPI.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
<?php
require_once('Logger.php');
require_once('SimpleBrowser.php');
class MediaWikiAPI {
protected $baseUrl;
protected $logger;
protected $browser;
public function setLogger($logger) {
$this->logger = $logger;
if ($this->browser != NULL) $this->browser->setLogger($logger);
}
public function setLogFile($logFile, $severity = LOGGER_DEBUG) {
$this->logger = new Logger($logFile, $severity);
if ($this->browser != NULL) $browser->setLogFile($logFile, $severity);
}
protected function log($msg, $severity = LOGGER_DEBUG) {
if ($this->logger) {
$this->logger->log($msg, $severity);
}
}
public function __construct($baseUrl) {
$this->baseUrl = $baseUrl;
$this->browser = new SimpleBrowser();
}
public function getPageIdByTitle($pageTitle) {
$url = "$this->baseUrl?action=query&format=xml&prop=info&titles=$pageTitle";
$this->log("Fetching URL $url");
$xml = DOMDocument::loadXML($this->browser->get($url));
$xPath = new DOMXPath($xml);
$p = $xPath->query("//page");
if ($p->length != 1) {
return -($p->length);
}
$pageId = $p->item(0)->getAttribute("pageid");
return $pageId ? $pageId : 0;
}
public function getLinksByPageId($pageId) {
$url = "$this->baseUrl?action=query&format=xml&prop=links&pageids=$pageId&pllimit=max";
$continue = "&continue=";
$links = Array();
while (strlen($continue) > 0) {
$this->log("Fetching URL $url$continue");
$xml = DOMDocument::loadXML($this->browser->get($url.$continue));
$xPath = new DOMXPath($xml);
$l = $xPath->query("//pl");
for ($i = 0; $i < $l->length; $i++) {
$links[] = $l->item($i)->getAttribute('title');
}
$c = $xPath->query('//continue');
$continue = '';
$this->log("Result has " . $c->length . " continue elements.");
if ($c->length == 1) {
foreach($c->item(0)->attributes as $name => $node) {
$continue .= "&$name=".$node->nodeValue;
}
}
}
return $links;
}
public function getContentByPageId($pageId) {
$url = "$this->baseUrl?action=query&format=xml&prop=revisions&rvprop=content&rvlimit=1&pageids=$pageId";
$this->log("Fetching URL $url");
$xml = DOMDocument::loadXML($this->browser->get($url));
$xPath = new DOMXPath($xml);
$r = $xPath->query('//rev');
if ($r->length == 1) {
return $r->item(0)->textContent;
} else {
return null;
}
}
}