r64906 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64905‎ | r64906 | r64907 >
Date:21:20, 10 April 2010
Author:btongminh
Status:ok (Comments)
Tags:
Comment:
Refactor: Move ImagePage hook callbacks from SpecialGlobalUsage to their own class
Modified paths:
  • /trunk/extensions/GlobalUsage/GlobalUsage.php (modified) (history)
  • /trunk/extensions/GlobalUsage/GlobalUsageImagePageHooks.php (added) (history)
  • /trunk/extensions/GlobalUsage/SpecialGlobalUsage.php (modified) (history)

Diff [purge]

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
1109 + native
Index: trunk/extensions/GlobalUsage/GlobalUsage.php
@@ -1,6 +1,6 @@
22 <?php
33 /*
4 - Copyright (c) 2008 - 2009 Bryan Tong Minh
 4+ Copyright (c) 2008 - 2010 Bryan Tong Minh
55
66 Permission is hereby granted, free of charge, to any person
77 obtaining a copy of this software and associated documentation
@@ -48,6 +48,7 @@
4949 $wgExtensionMessagesFiles['GlobalUsage'] = $dir . 'GlobalUsage.i18n.php';
5050 $wgAutoloadClasses['GlobalUsage'] = $dir . 'GlobalUsage_body.php';
5151 $wgAutoloadClasses['GlobalUsageHooks'] = $dir . 'GlobalUsageHooks.php';
 52+$wgAutoloadClasses['GlobalUsageImagePageHooks'] = $dir . 'GlobalUsageImagePageHooks.php';
5253 $wgAutoloadClasses['SpecialGlobalUsage'] = $dir . 'SpecialGlobalUsage.php';
5354 $wgAutoloadClasses['GlobalUsageQuery'] = $dir . 'GlobalUsageQuery.php';
5455 $wgAutoloadClasses['ApiQueryGlobalUsage'] = $dir . 'ApiQueryGlobalUsage.php';
@@ -60,7 +61,7 @@
6162 * - Local LinksUpdate
6263 * - Local article deletion (remove from table)
6364 * - Local article move (update page title)
64 - * - Local file upload/deletion/move (toggle is_local flag)
 65+ * - Local file upload/deletion/move
6566 */
6667 $wgHooks['LinksUpdateComplete'][] = 'GlobalUsageHooks::onLinksUpdateComplete';
6768 $wgHooks['ArticleDeleteComplete'][] = 'GlobalUsageHooks::onArticleDeleteComplete';
@@ -68,8 +69,10 @@
6970 $wgHooks['FileUndeleteComplete'][] = 'GlobalUsageHooks::onFileUndeleteComplete';
7071 $wgHooks['UploadComplete'][] = 'GlobalUsageHooks::onUploadComplete';
7172 $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 */
7477 $wgHooks['ParserTestTables'][] = 'GlobalUsageHooks::onParserTestTables';
7578 $wgHooks['LoadExtensionSchemaUpdates'][] = 'GlobalUsageHooks::onLoadExtensionSchemaUpdates';
7679
Index: trunk/extensions/GlobalUsage/SpecialGlobalUsage.php
@@ -133,7 +133,7 @@
134134 /**
135135 * Helper to format a specific item
136136 */
137 - private static function formatItem( $item ) {
 137+ public static function formatItem( $item ) {
138138 if ( !$item['namespace'] )
139139 $page = $item['title'];
140140 else
@@ -145,111 +145,7 @@
146146 return $link === false ? $page : $link;
147147 }
148148
149 -
150 - private static $queryCache = array();
151149 /**
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 - /**
254150 * Helper function to create the navbar, stolen from wfViewPrevNext
255151 *
256152 * @param $query GlobalUsageQuery An executed GlobalUsageQuery object

Comments

#Comment by Werdna (talk | contribs)   03:23, 9 December 2010

Looks OK

Status & tagging log