Index: trunk/extensions/GlobalUsage/GlobalUsageImagePageHooks.php |
— | — | @@ -1,23 +1,110 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class GlobalUsageImagePageHooks { |
| 5 | + private static $queryCache = array(); |
5 | 6 | |
6 | 7 | /** |
7 | | - * Show a global usage link on the image page |
| 8 | + * Get an executed query for use on image pages |
| 9 | + * |
| 10 | + * @param Title $title File to query for |
| 11 | + * @return GlobalUsageQuery Query object, already executed |
| 12 | + */ |
| 13 | + private static function getImagePageQuery( $title ) { |
| 14 | + $name = $title->getDBkey(); |
| 15 | + if ( !isset( self::$queryCache[$name] ) ) { |
| 16 | + $query = new GlobalUsageQuery( $title ); |
| 17 | + $query->filterLocal(); |
| 18 | + $query->execute(); |
| 19 | + |
| 20 | + self::$queryCache[$name] = $query; |
| 21 | + |
| 22 | + // Limit cache size to 100 |
| 23 | + if ( count( self::$queryCache ) > 100 ) |
| 24 | + array_shift( self::$queryCache ); |
| 25 | + } |
| 26 | + |
| 27 | + return self::$queryCache[$name]; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Show a global usage section on the image page |
8 | 32 | * |
9 | 33 | * @param object $imagePage The ImagePage |
10 | | - * @param string $html HTML to add to the image page as the link in image links section |
| 34 | + * @param string $html HTML to add to the image page as global usage section |
11 | 35 | * @return bool |
12 | 36 | */ |
13 | 37 | public static function onImagePageAfterImageLinks( $imagePage, &$html ) { |
| 38 | + if ( !self::hasResults( $imagePage ) ) |
| 39 | + return true; |
| 40 | + |
| 41 | + $title = $imagePage->getFile()->getTitle(); |
| 42 | + $targetName = $title->getText(); |
| 43 | + |
| 44 | + $query = self::getImagePageQuery( $title ); |
| 45 | + |
| 46 | + $guHtml = ''; |
| 47 | + foreach ( $query->getSingleImageResult() as $wiki => $result ) { |
| 48 | + $wikiName = WikiMap::getWikiName( $wiki ); |
| 49 | + $escWikiName = Sanitizer::escapeClass( $wikiName ); |
| 50 | + $guHtml .= "<li class='mw-gu-onwiki-$escWikiName'>" . wfMsgExt( |
| 51 | + 'globalusage-on-wiki', 'parseinline', |
| 52 | + $targetName, $wikiName ) . "\n<ul>"; |
| 53 | + foreach ( $result as $item ) |
| 54 | + $guHtml .= "\t<li>" . SpecialGlobalUsage::formatItem( $item ) . "</li>\n"; |
| 55 | + $guHtml .= "</ul></li>\n"; |
| 56 | + } |
| 57 | + |
| 58 | + if ( $guHtml ) { |
| 59 | + $html .= '<h2 id="globalusage">' . wfMsgHtml( 'globalusage' ) . "</h2>\n" |
| 60 | + . '<div id="mw-imagepage-section-globalusage">' |
| 61 | + . wfMsgExt( 'globalusage-of-file', 'parse' ) |
| 62 | + . "<ul>\n" . $guHtml . "</ul>\n"; |
| 63 | + if ( $query->hasMore() ) |
| 64 | + $html .= wfMsgExt( 'globalusage-more', 'parse', $targetName ); |
| 65 | + $html .= '</div>'; |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Show a link to the global image links in the TOC if there are any results available. |
| 73 | + */ |
| 74 | + public static function onImagePageShowTOC( $imagePage, &$toc ) { |
| 75 | + if ( self::hasResults( $imagePage ) ) { |
| 76 | + # Insert a link after the 3rd entry in the TOC |
| 77 | + array_splice( $toc, 3, 0, '<li><a href="#globalusage">' |
| 78 | + . wfMsgHtml( 'globalusage' ) . '</a></li>'); |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Check whether there are results for an image page. Checks whether the |
| 85 | + * file exists and is not local. |
| 86 | + * |
| 87 | + * @param $imagePage ImagePage |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + protected static function hasResults( $imagePage ) { |
| 91 | + # Don't display links if the target file does not exist |
| 92 | + $file = $imagePage->getFile(); |
| 93 | + if ( !$file->exists() ) { |
| 94 | + return false; |
| 95 | + } |
14 | 96 | |
15 | | - $targetName = $imagePage->getFile()->getTitle()->getText(); |
| 97 | + # Don't show global usage if the file is local. |
| 98 | + # Do show it however if the current repo is the shared repo. The way |
| 99 | + # we detect this is a bit hacky and less than ideal. See bug 23136 for |
| 100 | + # a discussion. |
| 101 | + global $wgGlobalUsageDatabase; |
| 102 | + $dbr = wfGetDB( DB_SLAVE ); |
| 103 | + if ( $file->getRepoName() == 'local' |
| 104 | + && $dbr->getDBname() != $wgGlobalUsageDatabase ) { |
| 105 | + return false; |
| 106 | + } |
16 | 107 | |
17 | | - $html .= '<p id="mw-imagepage-section-globalusage">'; |
18 | | - $html .= wfMsgExt( 'globalusage-more', 'parse', $targetName ); |
19 | | - $html .= '</p>'; |
20 | | - |
21 | | - return true; |
| 108 | + $query = self::getImagePageQuery( $imagePage->getFile()->getTitle() ); |
| 109 | + return (bool)$query->getResult(); |
22 | 110 | } |
23 | | - |
24 | | -} |
| 111 | +} |
\ No newline at end of file |
Index: trunk/extensions/GlobalUsage/GlobalUsage.i18n.php |
— | — | @@ -20,7 +20,7 @@ |
21 | 21 | 'globalusage-no-results' => '[[:$1]] is not used on other wikis.', |
22 | 22 | 'globalusage-on-wiki' => 'Usage on $2', |
23 | 23 | 'globalusage-of-file' => 'The following other wikis use this file:', |
24 | | - 'globalusage-more' => 'View [[{{#Special:GlobalUsage}}/$1|global usage]] of this file.', |
| 24 | + 'globalusage-more' => 'View [[{{#Special:GlobalUsage}}/$1|more global usage]] of this file.', |
25 | 25 | 'globalusage-filterlocal' => 'Do not show local usage', |
26 | 26 | ); |
27 | 27 | |