forked from wikimedia/mediawiki-extensions-GlobalUsage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalUsageImagePageHooks.php
120 lines (105 loc) · 3.55 KB
/
GlobalUsageImagePageHooks.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
<?php
class GlobalUsageImagePageHooks {
private static $queryCache = array();
/**
* Get an executed query for use on image pages
*
* @param Title $title File to query for
* @return GlobalUsageQuery Query object, already executed
*/
private static function getImagePageQuery( $title ) {
$name = $title->getDBkey();
if ( !isset( self::$queryCache[$name] ) ) {
$query = new GlobalUsageQuery( $title );
$query->filterLocal();
$query->execute();
self::$queryCache[$name] = $query;
// Limit cache size to 100
if ( count( self::$queryCache ) > 100 ) {
array_shift( self::$queryCache );
}
}
return self::$queryCache[$name];
}
/**
* Show a global usage section on the image page
*
* @param $imagePage ImagePage
* @param string $html HTML to add to the image page as global usage section
* @return bool
*/
public static function onImagePageAfterImageLinks( $imagePage, &$html ) {
if ( !self::hasResults( $imagePage ) ) {
return true;
}
$context = $imagePage->getContext();
$title = $imagePage->getFile()->getTitle();
$targetName = $title->getText();
foreach(Interwiki::getAllPrefixes(1) as $k) { $interWikis[$k[iw_wikiid]] = $k[iw_prefix];};
$query = self::getImagePageQuery( $title );
$guHtml = '';
foreach ( $query->getSingleImageResult() as $wiki => $result ) {
$interwiki = Interwiki::fetch($interWikis[$wiki]);
$wikiName = parse_url($interwiki->getURL(), PHP_URL_HOST);
$escWikiName = Sanitizer::escapeClass( $wikiName );
$guHtml .= "<li class='mw-gu-onwiki-$escWikiName'>" . $context->msg(
'globalusage-on-wiki',
$targetName, $wikiName )->parse() . "\n<ul>";
foreach ( $result as $item )
$guHtml .= "\t<li>" . SpecialGlobalUsage::formatItem( $item, $interwiki ) . "</li>\n";
$guHtml .= "</ul></li>\n";
}
if ( $guHtml ) {
$html .= '<h2 id="globalusage">' . $context->msg( 'globalusage' )->escaped() . "</h2>\n"
. '<div id="mw-imagepage-section-globalusage">'
. $context->msg( 'globalusage-of-file' )->parseAsBlock()
. "<ul>\n" . $guHtml . "</ul>\n";
if ( $query->hasMore() ) {
$html .= $context->msg( 'globalusage-more', $targetName )->parseAsBlock();
}
$html .= '</div>';
}
return true;
}
/**
* Show a link to the global image links in the TOC if there are any results available.
* @param $imagePage ImagePage
* @param $toc array
* @return bool
*/
public static function onImagePageShowTOC( $imagePage, &$toc ) {
if ( self::hasResults( $imagePage ) ) {
# Insert a link after the 3rd entry in the TOC
array_splice( $toc, 3, 0, '<li><a href="#globalusage">'
. $imagePage->getContext()->msg( 'globalusage' )->escaped() . '</a></li>' );
}
return true;
}
/**
* Check whether there are results for an image page. Checks whether the
* file exists and is not local.
*
* @param $imagePage ImagePage
* @return bool
*/
protected static function hasResults( $imagePage ) {
# Don't display links if the target file does not exist
$file = $imagePage->getFile();
if ( !$file->exists() ) {
return false;
}
# Don't show global usage if the file is local.
# Do show it however if the current repo is the shared repo. The way
# we detect this is a bit hacky and less than ideal. See bug 23136 for
# a discussion.
global $wgGlobalUsageDatabase;
$dbr = wfGetDB( DB_SLAVE );
if ( $file->getRepoName() == 'local'
&& $dbr->getDBname() != $wgGlobalUsageDatabase
) {
return false;
}
$query = self::getImagePageQuery( $imagePage->getFile()->getTitle() );
return (bool)$query->getResult();
}
}