Index: trunk/extensions/GlobalUsage/GlobalUsageImagePageHooks.php |
— | — | @@ -0,0 +1,107 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class GlobalUsageImagePageHooks { |
| 5 | + private static $queryCache = array(); |
| 6 | + |
| 7 | + /** |
| 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 |
| 32 | + * |
| 33 | + * @param object $imagePage The ImagePage |
| 34 | + * @param string $html HTML to add to the image page as global usage section |
| 35 | + * @return bool |
| 36 | + */ |
| 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 | + $toc[] = '<li><a href="#globalusage">' . wfMsgHtml( 'globalusage' ) . '</a></li>'; |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Check whether there are results for an image page. Checks whether the |
| 82 | + * file exists and is not local. |
| 83 | + * |
| 84 | + * @param $imagePage ImagePage |
| 85 | + * @return bool |
| 86 | + */ |
| 87 | + protected static function hasResults( $imagePage ) { |
| 88 | + # Don't display links if the target file does not exist |
| 89 | + $file = $imagePage->getFile(); |
| 90 | + if ( !$file->exists() ) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + # Don't show global usage if the file is local. |
| 95 | + # Do show it however if the current repo is the shared repo. The way |
| 96 | + # we detect this is a bit hacky and less than ideal. See bug 23136 for |
| 97 | + # a discussion. |
| 98 | + global $wgGlobalUsageDatabase; |
| 99 | + $dbr = wfGetDB( DB_SLAVE ); |
| 100 | + if ( $file->getRepoName() == 'local' |
| 101 | + && $dbr->getDBname() != $wgGlobalUsageDatabase ) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + $query = self::getImagePageQuery( $imagePage->getFile()->getTitle() ); |
| 106 | + return (bool)$query->getResult(); |
| 107 | + } |
| 108 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/GlobalUsage/GlobalUsageImagePageHooks.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 109 | + native |
Index: trunk/extensions/GlobalUsage/GlobalUsage.php |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | <?php |
3 | 3 | /* |
4 | | - Copyright (c) 2008 - 2009 Bryan Tong Minh |
| 4 | + Copyright (c) 2008 - 2010 Bryan Tong Minh |
5 | 5 | |
6 | 6 | Permission is hereby granted, free of charge, to any person |
7 | 7 | obtaining a copy of this software and associated documentation |
— | — | @@ -48,6 +48,7 @@ |
49 | 49 | $wgExtensionMessagesFiles['GlobalUsage'] = $dir . 'GlobalUsage.i18n.php'; |
50 | 50 | $wgAutoloadClasses['GlobalUsage'] = $dir . 'GlobalUsage_body.php'; |
51 | 51 | $wgAutoloadClasses['GlobalUsageHooks'] = $dir . 'GlobalUsageHooks.php'; |
| 52 | +$wgAutoloadClasses['GlobalUsageImagePageHooks'] = $dir . 'GlobalUsageImagePageHooks.php'; |
52 | 53 | $wgAutoloadClasses['SpecialGlobalUsage'] = $dir . 'SpecialGlobalUsage.php'; |
53 | 54 | $wgAutoloadClasses['GlobalUsageQuery'] = $dir . 'GlobalUsageQuery.php'; |
54 | 55 | $wgAutoloadClasses['ApiQueryGlobalUsage'] = $dir . 'ApiQueryGlobalUsage.php'; |
— | — | @@ -60,7 +61,7 @@ |
61 | 62 | * - Local LinksUpdate |
62 | 63 | * - Local article deletion (remove from table) |
63 | 64 | * - Local article move (update page title) |
64 | | - * - Local file upload/deletion/move (toggle is_local flag) |
| 65 | + * - Local file upload/deletion/move |
65 | 66 | */ |
66 | 67 | $wgHooks['LinksUpdateComplete'][] = 'GlobalUsageHooks::onLinksUpdateComplete'; |
67 | 68 | $wgHooks['ArticleDeleteComplete'][] = 'GlobalUsageHooks::onArticleDeleteComplete'; |
— | — | @@ -68,8 +69,10 @@ |
69 | 70 | $wgHooks['FileUndeleteComplete'][] = 'GlobalUsageHooks::onFileUndeleteComplete'; |
70 | 71 | $wgHooks['UploadComplete'][] = 'GlobalUsageHooks::onUploadComplete'; |
71 | 72 | $wgHooks['TitleMoveComplete'][] = 'GlobalUsageHooks::onTitleMoveComplete'; |
72 | | -$wgHooks['ImagePageAfterImageLinks'][] = 'SpecialGlobalUsage::onImagePageAfterImageLinks'; |
73 | | -$wgHooks['ImagePageShowTOC'][] = 'SpecialGlobalUsage::onImagePageShowTOC'; |
| 73 | +/* Hooks for ImagePage */ |
| 74 | +$wgHooks['ImagePageAfterImageLinks'][] = 'GlobalUsageImagePageHooks::onImagePageAfterImageLinks'; |
| 75 | +$wgHooks['ImagePageShowTOC'][] = 'GlobalUsageImagePageHooks::onImagePageShowTOC'; |
| 76 | +/* Other hooks */ |
74 | 77 | $wgHooks['ParserTestTables'][] = 'GlobalUsageHooks::onParserTestTables'; |
75 | 78 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'GlobalUsageHooks::onLoadExtensionSchemaUpdates'; |
76 | 79 | |
Index: trunk/extensions/GlobalUsage/SpecialGlobalUsage.php |
— | — | @@ -133,7 +133,7 @@ |
134 | 134 | /** |
135 | 135 | * Helper to format a specific item |
136 | 136 | */ |
137 | | - private static function formatItem( $item ) { |
| 137 | + public static function formatItem( $item ) { |
138 | 138 | if ( !$item['namespace'] ) |
139 | 139 | $page = $item['title']; |
140 | 140 | else |
— | — | @@ -145,111 +145,7 @@ |
146 | 146 | return $link === false ? $page : $link; |
147 | 147 | } |
148 | 148 | |
149 | | - |
150 | | - private static $queryCache = array(); |
151 | 149 | /** |
152 | | - * Get an executed query for use on image pages |
153 | | - * |
154 | | - * @param Title $title File to query for |
155 | | - * @return GlobalUsageQuery Query object, already executed |
156 | | - */ |
157 | | - private static function getImagePageQuery( $title ) { |
158 | | - $name = $title->getDBkey(); |
159 | | - if ( !isset( self::$queryCache[$name] ) ) { |
160 | | - $query = new GlobalUsageQuery( $title ); |
161 | | - $query->filterLocal(); |
162 | | - $query->execute(); |
163 | | - |
164 | | - self::$queryCache[$name] = $query; |
165 | | - |
166 | | - // Limit cache size to 100 |
167 | | - if ( count( self::$queryCache ) > 100 ) |
168 | | - array_shift( self::$queryCache ); |
169 | | - } |
170 | | - |
171 | | - return self::$queryCache[$name]; |
172 | | - } |
173 | | - |
174 | | - /** |
175 | | - * Show a global usage section on the image page |
176 | | - * |
177 | | - * @param object $imagePage The ImagePage |
178 | | - * @param string $html HTML to add to the image page as global usage section |
179 | | - * @return bool |
180 | | - */ |
181 | | - public static function onImagePageAfterImageLinks( $imagePage, &$html ) { |
182 | | - if ( !self::hasResults( $imagePage ) ) |
183 | | - return true; |
184 | | - |
185 | | - $title = $imagePage->getFile()->getTitle(); |
186 | | - $targetName = $title->getText(); |
187 | | - |
188 | | - $query = self::getImagePageQuery( $title ); |
189 | | - |
190 | | - $guHtml = ''; |
191 | | - foreach ( $query->getSingleImageResult() as $wiki => $result ) { |
192 | | - $wikiName = WikiMap::getWikiName( $wiki ); |
193 | | - $escWikiName = Sanitizer::escapeClass( $wikiName ); |
194 | | - $guHtml .= "<li class='mw-gu-onwiki-$escWikiName'>" . wfMsgExt( |
195 | | - 'globalusage-on-wiki', 'parseinline', |
196 | | - $targetName, $wikiName ) . "\n<ul>"; |
197 | | - foreach ( $result as $item ) |
198 | | - $guHtml .= "\t<li>" . self::formatItem( $item ) . "</li>\n"; |
199 | | - $guHtml .= "</ul></li>\n"; |
200 | | - } |
201 | | - |
202 | | - if ( $guHtml ) { |
203 | | - $html .= '<h2 id="globalusage">' . wfMsgHtml( 'globalusage' ) . "</h2>\n" |
204 | | - . '<div id="mw-imagepage-section-globalusage">' |
205 | | - . wfMsgExt( 'globalusage-of-file', 'parse' ) |
206 | | - . "<ul>\n" . $guHtml . "</ul>\n"; |
207 | | - if ( $query->hasMore() ) |
208 | | - $html .= wfMsgExt( 'globalusage-more', 'parse', $targetName ); |
209 | | - $html .= '</div>'; |
210 | | - } |
211 | | - |
212 | | - return true; |
213 | | - } |
214 | | - |
215 | | - /** |
216 | | - * Show a link to the global image links in the TOC if there are any results available. |
217 | | - */ |
218 | | - public static function onImagePageShowTOC( $imagePage, &$toc ) { |
219 | | - if ( self::hasResults( $imagePage ) ) |
220 | | - $toc[] = '<li><a href="#globalusage">' . wfMsgHtml( 'globalusage' ) . '</a></li>'; |
221 | | - return true; |
222 | | - } |
223 | | - |
224 | | - /** |
225 | | - * Check whether there are results for an image page. Checks whether the |
226 | | - * file exists and is not local. |
227 | | - * |
228 | | - * @param $imagePage ImagePage |
229 | | - * @return bool |
230 | | - */ |
231 | | - protected static function hasResults( $imagePage ) { |
232 | | - # Don't display links if the target file does not exist |
233 | | - $file = $imagePage->getFile(); |
234 | | - if ( !$file->exists() ) { |
235 | | - return false; |
236 | | - } |
237 | | - |
238 | | - # Don't show global usage if the file is local. |
239 | | - # Do show it however if the current repo is the shared repo. The way |
240 | | - # we detect this is a bit hacky and less than ideal. See bug 23136 for |
241 | | - # a discussion. |
242 | | - global $wgGlobalUsageDatabase; |
243 | | - $dbr = wfGetDB( DB_SLAVE ); |
244 | | - if ( $file->getRepoName() == 'local' |
245 | | - && $dbr->getDBname() != $wgGlobalUsageDatabase ) { |
246 | | - return false; |
247 | | - } |
248 | | - |
249 | | - $query = self::getImagePageQuery( $imagePage->getFile()->getTitle() ); |
250 | | - return (bool)$query->getResult(); |
251 | | - } |
252 | | - |
253 | | - /** |
254 | 150 | * Helper function to create the navbar, stolen from wfViewPrevNext |
255 | 151 | * |
256 | 152 | * @param $query GlobalUsageQuery An executed GlobalUsageQuery object |