-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecialExternalLinkAnalysis.php
More file actions
88 lines (84 loc) · 2.53 KB
/
SpecialExternalLinkAnalysis.php
File metadata and controls
88 lines (84 loc) · 2.53 KB
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
<?php
class SpecialExternalLinkAnalysis extends SpecialPage {
function __construct() {
parent::__construct( 'ExternalLinkAnalysis' );
}
function execute( $par ) {
$request = $this->getRequest();
$output = $this->getOutput();
$this->setHeaders();
# Get request data from, e.g.
$param = $request->getText( 'param' );
# Do stuff
# ...
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
array( 'externallinks', 'link_desc', 'page' ),
array( 'el_to', 'ld_desc', 'page_namespace', 'page_title' ),
array( 'ld_desc IS NOT NULL', 'el_to=ld_url', 'el_from=page_id' )
);
$results = array();
foreach ( $res as $row ) {
$host = parse_url( $row->el_to, PHP_URL_HOST );
#echo $host;
#die();
if ( !isset( $results[$host] ) ) {
$results[$host] = array ();
}
if ( !isset( $results[$host][$row->el_to] ) ) {
$results[$host][$row->el_to] = array();
$results[$host][$row->el_to]['pages'] = array();
}
$results[$host][$row->el_to]['description'] = $row->ld_desc;
$pageName = '';
if ( $row->page_namespace ) {
$pageName = "{{ns:" . $row->page_namespace . "}}:";
}
$pageName .= $row->page_title;
$results[$host][$row->el_to]['pages'][] = $pageName;
}
$wikitext = '';
#ksort ( $results );
#uasort( $results, $this->cmp );
foreach ( $results as $resultKey => $resultValue ) {
uasort( $resultValue, 'compareDescriptions' );
$firstMainspaceOneTopLevel = true;
foreach ( $resultValue as $resultValueKey => $resultValueValue ) {
$firstOne = true;
$firstMainspaceOne = true;
ksort ( $resultValueValue['pages'] );
foreach ( $resultValueValue['pages'] as $page ) {
if ( strpos( $page, '{{ns:' ) !== false ) {
continue;
}
if ( $firstMainspaceOneTopLevel ) {
$wikitext .= "==" . $resultKey . "==\n";
}
if ( $firstMainspaceOne ) {
$wikitext .= "===[" . $resultValueKey . ' '
. $resultValueValue['description'];
if ( $resultValueKey != $resultValueValue['description'] ) {
$wikitext .= " <small>($resultValueKey)</small>";
}
$wikitext .= "]===\n";
}
if ( !$firstOne ) {
$wikitext .= ", ";
}
$wikitext .= '[[' . str_replace( '_', ' ', $page ) . ']]';
$firstOne = false;
$firstMainspaceOne = false;
$firstMainspaceOneTopLevel = false;
}
$wikitext .= "\n";
}
}
$output->addWikiText( $wikitext );
}
}
function compareDescriptions($a, $b) {
if ($a['description'] == $b['description'] ) {
return 0;
}
return ($a['description'] < $b['description'] ) ? -1 : 1;
}