Adding support for domains in SpecialPasswordReset.php.
[lhc/web/wiklou.git] / includes / specials / SpecialGlobalFileUsage.php
1 <?php
2 /**
3 * Special page to show global file usage. Also contains hook functions for
4 * showing usage on an image page.
5 */
6
7 class SpecialGlobalFileUsage extends SpecialPage {
8 public function __construct() {
9 parent::__construct( 'GlobalFileUsage' );
10 }
11
12 /**
13 * Entry point
14 */
15 public function execute( $par ) {
16 $request = $this->getRequest();
17 $target = $par ? $par : $request->getVal( 'target' );
18 $this->target = Title::makeTitleSafe( NS_FILE, $target );
19
20 $this->filterLocal = $request->getCheck( 'filterlocal' );
21
22 $this->setHeaders();
23
24 $this->showForm();
25
26 if ( !is_null( $this->target ) ) {
27 $this->getOutput()->setPageTitle( wfMsg( 'globalfileusage-for', $this->target->getPrefixedText() ) );
28 $this->showResult();
29 }
30 }
31
32 /**
33 * Shows the search form
34 */
35 private function showForm() {
36 global $wgScript, $wgContLang;
37
38 /* Build form */
39 $html = Xml::openElement( 'form', array( 'action' => $wgScript ) ) . "\n";
40 // Name of SpecialPage
41 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
42 // Limit
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() )
47 // Submit button
48 . "\n\t" . Xml::element( 'input', array(
49 'type' => 'submit',
50 'value' => wfMsg( 'globalfileusage-ok' )
51 ) )
52 // Filter local checkbox
53 . "\n\t<p>" . Xml::checkLabel( wfMsg( 'globalfileusage-filterlocal' ),
54 'filterlocal', 'mw-filterlocal', $this->filterLocal ) . '</p>';
55
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 ) );
62 }
63 }
64
65 // Wrap the entire form in a nice fieldset
66 $html .= Xml::fieldSet( wfMsg( 'globalfileusage-text' ), $formContent ) . "\n</form>";
67
68 $this->getOutput()->addHtml( $html );
69 }
70
71 /**
72 * Creates as queryer and executes it based on the WebRequest object
73 */
74 private function showResult() {
75 $request = $this->getRequest();
76 $query = new GlobalUsageQuery( $this->target );
77
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 );
83 }
84 $query->setLimit( $request->getInt( 'limit', 50 ) );
85 $query->filterLocal( $this->filterLocal );
86
87 // Perform query
88 $query->searchFile();
89
90 // Show result
91 $out = $this->getOutput();
92
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() );
96 return;
97 }
98
99 $offset = $query->getOffsetString();
100 $navbar = $this->getNavBar( $query );
101 $targetName = $this->target->getText();
102
103 // Top navbar
104 $out->addHtml( $navbar );
105
106 $out->addHtml( '<div id="mw-globalfileusage-result">' );
107 foreach ( $query->getSingleResult() as $wiki => $result ) {
108 $out->addHtml(
109 '<h2>' . wfMsgExt(
110 'globalfileusage-on-wiki', 'parseinline',
111 $targetName, WikiMap::getWikiName( $wiki ) )
112 . "</h2><ul>\n" );
113 foreach ( $result as $item ) {
114 $out->addHtml( "\t<li>" . self::formatItem( $item ) . "</li>\n" );
115 }
116 $out->addHtml( "</ul>\n" );
117 }
118 $out->addHtml( '</div>' );
119
120 // Bottom navbar
121 $out->addHtml( $navbar );
122 }
123
124 /**
125 * Helper to format a specific item
126 */
127 public static function formatItem( $item ) {
128 if ( !$item['namespace'] ) {
129 $page = $item['title'];
130 } else {
131 $page = "{$item['namespace']}:{$item['title']}";
132 }
133
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;
138 }
139
140 /**
141 * Helper function to create the navbar, stolen from wfViewPrevNext
142 *
143 * @param $query GlobalUsageQuery An executed GlobalUsageQuery object
144 * @return string Navbar HTML
145 */
146 protected function getNavBar( $query ) {
147 $lang = $this->getLang();
148 $target = $this->target->getText();
149 $limit = $query->getLimit();
150 $fmtLimit = $lang->formatNum( $limit );
151
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() ) {
156 $from = $offset;
157 $to = $continue;
158 } else {
159 $from = $continue;
160 $to = $offset;
161 }
162
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 );
169
170 # Fetch the title object
171 $title = $this->getTitle();
172
173 # Make 'previous' link
174 if ( $to ) {
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 );
180 } else {
181 $plink = $prev;
182 }
183
184 # Make 'next' link
185 if ( $from ) {
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 );
191 } else {
192 $nlink = $next;
193 }
194
195 # Make links to set number of items per page
196 $numLinks = array();
197 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
198 $fmtLimit = $lang->formatNum( $num );
199
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' );
205
206 $numLinks[] = Linker::link( $title, $fmtLimit, $attr, $q );
207 }
208 $nums = $lang->pipeList( $numLinks );
209
210 return wfMsgHtml( 'viewprevnext', $plink, $nlink, $nums );
211 }
212 }
213