3 * Special page to show global file usage. Also contains hook functions for
4 * showing usage on an image page.
7 class SpecialGlobalFileUsage
extends SpecialPage
{
8 public function __construct() {
9 parent
::__construct( 'GlobalFileUsage' );
15 public function execute( $par ) {
16 $request = $this->getRequest();
17 $target = $par ?
$par : $request->getVal( 'target' );
18 $this->target
= Title
::makeTitleSafe( NS_FILE
, $target );
20 $this->filterLocal
= $request->getCheck( 'filterlocal' );
26 if ( !is_null( $this->target
) ) {
27 $this->getOutput()->setPageTitle( wfMsg( 'globalfileusage-for', $this->target
->getPrefixedText() ) );
33 * Shows the search form
35 private function showForm() {
36 global $wgScript, $wgContLang;
39 $html = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) . "\n";
40 // Name of SpecialPage
41 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
43 $html .= Html
::hidden( 'limit', $this->getRequest()->getInt( 'limit', 50 ) );
44 // Input box with target prefilled if available
45 $formContent = "\t" . Xml
::input( 'target', 40, is_null( $this->target
) ?
''
46 : $this->target
->getText() )
48 . "\n\t" . Xml
::element( 'input', array(
50 'value' => wfMsg( 'globalfileusage-ok' )
52 // Filter local checkbox
53 . "\n\t<p>" . Xml
::checkLabel( wfMsg( 'globalfileusage-filterlocal' ),
54 'filterlocal', 'mw-filterlocal', $this->filterLocal
) . '</p>';
56 if ( !is_null( $this->target
) ) {
57 $file = wfFindFile( $this->target
);
58 if ( $file !== null ) {
59 // Show the image if it exists
60 $html .= Linker
::makeImageLink2( $this->target
, $file,
61 array( 'align' => $this->getLang()->alignEnd(), 'thumbnail' => true ) );
65 // Wrap the entire form in a nice fieldset
66 $html .= Xml
::fieldSet( wfMsg( 'globalfileusage-text' ), $formContent ) . "\n</form>";
68 $this->getOutput()->addHtml( $html );
72 * Creates as queryer and executes it based on the WebRequest object
74 private function showResult() {
75 $request = $this->getRequest();
76 $query = new GlobalUsageQuery( $this->target
);
78 // Extract params from the WebRequest object
79 if ( $request->getText( 'from' ) ) {
80 $query->setOffset( $request->getText( 'from' ) );
81 } elseif ( $request->getText( 'to' ) ) {
82 $query->setOffset( $request->getText( 'to' ), true );
84 $query->setLimit( $request->getInt( 'limit', 50 ) );
85 $query->filterLocal( $this->filterLocal
);
91 $out = $this->getOutput();
93 // Don't show form element if there is no data
94 if ( $query->count() == 0 ) {
95 $out->addWikiMsg( 'globalfileusage-no-results', $this->target
->getPrefixedText() );
99 $offset = $query->getOffsetString();
100 $navbar = $this->getNavBar( $query );
101 $targetName = $this->target
->getText();
104 $out->addHtml( $navbar );
106 $out->addHtml( '<div id="mw-globalfileusage-result">' );
107 foreach ( $query->getSingleResult() as $wiki => $result ) {
110 'globalfileusage-on-wiki', 'parseinline',
111 $targetName, WikiMap
::getWikiName( $wiki ) )
113 foreach ( $result as $item ) {
114 $out->addHtml( "\t<li>" . self
::formatItem( $item ) . "</li>\n" );
116 $out->addHtml( "</ul>\n" );
118 $out->addHtml( '</div>' );
121 $out->addHtml( $navbar );
125 * Helper to format a specific item
127 public static function formatItem( $item ) {
128 if ( !$item['namespace'] ) {
129 $page = $item['title'];
131 $page = "{$item['namespace']}:{$item['title']}";
134 $link = WikiMap
::makeForeignLink( $item['wiki'], $page,
135 str_replace( '_', ' ', $page ) );
136 // Return only the title if no link can be constructed
137 return $link === false ?
$page : $link;
141 * Helper function to create the navbar, stolen from wfViewPrevNext
143 * @param $query GlobalUsageQuery An executed GlobalUsageQuery object
144 * @return string Navbar HTML
146 protected function getNavBar( $query ) {
147 $lang = $this->getLang();
148 $target = $this->target
->getText();
149 $limit = $query->getLimit();
150 $fmtLimit = $lang->formatNum( $limit );
152 # Find out which strings are for the prev and which for the next links
153 $offset = $query->getOffsetString();
154 $continue = $query->getContinueFileString();
155 if ( $query->isReversed() ) {
163 # Get prev/next link display text
164 $prev = wfMsgExt( 'prevn', array( 'parsemag', 'escape' ), $fmtLimit );
165 $next = wfMsgExt( 'nextn', array( 'parsemag', 'escape' ), $fmtLimit );
166 # Get prev/next link title text
167 $pTitle = wfMsgExt( 'prevn-title', array( 'parsemag', 'escape' ), $fmtLimit );
168 $nTitle = wfMsgExt( 'nextn-title', array( 'parsemag', 'escape' ), $fmtLimit );
170 # Fetch the title object
171 $title = $this->getTitle();
173 # Make 'previous' link
175 $attr = array( 'title' => $pTitle, 'class' => 'mw-prevlink' );
176 $q = array( 'limit' => $limit, 'to' => $to, 'target' => $target );
177 if ( $this->filterLocal
)
178 $q['filterlocal'] = '1';
179 $plink = Linker
::link( $title, $prev, $attr, $q );
186 $attr = array( 'title' => $nTitle, 'class' => 'mw-nextlink' );
187 $q = array( 'limit' => $limit, 'from' => $from, 'target' => $target );
188 if ( $this->filterLocal
)
189 $q['filterlocal'] = '1';
190 $nlink = Linker
::link( $title, $next, $attr, $q );
195 # Make links to set number of items per page
197 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
198 $fmtLimit = $lang->formatNum( $num );
200 $q = array( 'offset' => $offset, 'limit' => $num, 'target' => $target );
201 if ( $this->filterLocal
)
202 $q['filterlocal'] = '1';
203 $lTitle = wfMsgExt( 'shown-title', array( 'parsemag', 'escape' ), $num );
204 $attr = array( 'title' => $lTitle, 'class' => 'mw-numlink' );
206 $numLinks[] = Linker
::link( $title, $fmtLimit, $attr, $q );
208 $nums = $lang->pipeList( $numLinks );
210 return wfMsgHtml( 'viewprevnext', $plink, $nlink, $nums );