42118b89beb740ba84ae6285c2f44dcaa80f3fe4
[lhc/web/wiklou.git] / includes / specials / SpecialGlobalTemplateUsage.php
1 <?php
2 /**
3 * This file has been copied from Extension:GlobalUsage and adapted
4 * to show the usage of a template instead of a file.
5 * Special page to show global template usage. Also contains hook functions for
6 * showing usage on an template page.
7 *
8 * @author Bryan Tong Minh <bryan.tongminh@gmail.com>
9 * @author Peter Potrowl <peter017@gmail.com>
10 */
11
12 class SpecialGlobalTemplateUsage extends SpecialPage {
13 public function __construct() {
14 parent::__construct( 'GlobalTemplateUsage' );
15 }
16
17 /**
18 * Entry point
19 */
20 public function execute( $par ) {
21 global $wgOut, $wgRequest;
22
23 $target = $par ? $par : $wgRequest->getVal( 'target' );
24 $this->target = Title::newFromText( $target );
25
26 $this->setHeaders();
27
28 $this->showForm();
29
30 if ( is_null( $this->target ) ) {
31 $wgOut->setPageTitle( wfMsg( 'globaltemplateusage' ) );
32 return;
33 }
34
35 $wgOut->setPageTitle( wfMsg( 'globaltemplateusage-for', $this->target->getPrefixedText() ) );
36
37 $this->showResult();
38 }
39
40 /**
41 * Shows the search form
42 */
43 private function showForm() {
44 global $wgScript, $wgOut, $wgRequest;
45
46 /* Build form */
47 $html = Xml::openElement( 'form', array( 'action' => $wgScript ) ) . "\n";
48 // Name of SpecialPage
49 $html .= Html::hidden( 'title', $this->getTitle( )->getPrefixedText( ) ) . "\n";
50 // Limit
51 $html .= Html::hidden( 'limit', $wgRequest->getInt( 'limit', 50 ) );
52 // Input box with target prefilled if available
53 $formContent = "\t" . Xml::input( 'target', 40, is_null( $this->target ) ? ''
54 : $this->target->getPrefixedText( ) )
55 // Submit button
56 . "\n\t" . Xml::element( 'input', array(
57 'type' => 'submit',
58 'value' => wfMsg( 'globaltemplateusage-ok' )
59 ) );
60
61 // Wrap the entire form in a nice fieldset
62 $html .= Xml::fieldSet( wfMsg( 'globaltemplateusage-text' ), $formContent ) . "\n</form>";
63
64 $wgOut->addHtml( $html );
65 }
66
67 /**
68 * Creates as queryer and executes it based on $wgRequest
69 */
70 private function showResult() {
71 global $wgRequest;
72
73 $query = new GlobalUsageQuery( $this->target );
74
75 // Extract params from $wgRequest
76 if ( $wgRequest->getText( 'from' ) ) {
77 $query->setOffset( $wgRequest->getText( 'from' ) );
78 } elseif ( $wgRequest->getText( 'to' ) ) {
79 $query->setOffset( $wgRequest->getText( 'to' ), true );
80 }
81 $query->setLimit( $wgRequest->getInt( 'limit', 50 ) );
82
83 // Perform query
84 $query->searchTemplate();
85
86 // Show result
87 global $wgOut;
88
89 // Don't show form element if there is no data
90 if ( $query->count() == 0 ) {
91 $wgOut->addWikiMsg( 'globaltemplateusage-no-results', $this->target->getPrefixedText( ) );
92 return;
93 }
94
95 $navbar = $this->getNavBar( $query );
96 $targetName = $this->target->getPrefixedText( );
97
98 // Top navbar
99 $wgOut->addHtml( $navbar );
100
101 $wgOut->addHtml( '<div id="mw-globaltemplateusage-result">' );
102 foreach ( $query->getSingleResult() as $wiki => $result ) {
103 $wgOut->addHtml(
104 '<h2>' . wfMsgExt(
105 'globaltemplateusage-on-wiki', 'parseinline',
106 $targetName, WikiMap::getWikiName( $wiki ) )
107 . "</h2><ul>\n" );
108 foreach ( $result as $item ) {
109 $wgOut->addHtml( "\t<li>" . self::formatItem( $item ) . "</li>\n" );
110 }
111 $wgOut->addHtml( "</ul>\n" );
112 }
113 $wgOut->addHtml( '</div>' );
114
115 // Bottom navbar
116 $wgOut->addHtml( $navbar );
117 }
118
119 /**
120 * Helper to format a specific item
121 */
122 public static function formatItem( $item ) {
123 if ( !$item['namespace'] ) {
124 $page = $item['title'];
125 } else {
126 $page = "{$item['namespace']}:{$item['title']}";
127 }
128
129 $link = WikiMap::makeForeignLink( $item['wiki'], $page,
130 str_replace( '_', ' ', $page ) );
131 // Return only the title if no link can be constructed
132 return $link === false ? $page : $link;
133 }
134
135 /**
136 * Helper function to create the navbar, stolen from wfViewPrevNext
137 *
138 * @param $query GlobalTemplateUsageQuery An executed GlobalTemplateUsageQuery object
139 * @return string Navbar HTML
140 */
141 protected function getNavBar( $query ) {
142 global $wgLang, $wgUser;
143
144 $skin = $wgUser->getSkin();
145
146 $target = $this->target->getPrefixedText();
147 $limit = $query->getLimit();
148 $fmtLimit = $wgLang->formatNum( $limit );
149
150 # Find out which strings are for the prev and which for the next links
151 $offset = $query->getOffsetString();
152 $continue = $query->getContinueTemplateString();
153 if ( $query->isReversed() ) {
154 $from = $offset;
155 $to = $continue;
156 } else {
157 $from = $continue;
158 $to = $offset;
159 }
160
161 # Get prev/next link display text
162 $prev = wfMsgExt( 'prevn', array( 'parsemag', 'escape' ), $fmtLimit );
163 $next = wfMsgExt( 'nextn', array( 'parsemag', 'escape' ), $fmtLimit );
164 # Get prev/next link title text
165 $pTitle = wfMsgExt( 'prevn-title', array( 'parsemag', 'escape' ), $fmtLimit );
166 $nTitle = wfMsgExt( 'nextn-title', array( 'parsemag', 'escape' ), $fmtLimit );
167
168 # Fetch the title object
169 $title = $this->getTitle();
170
171 # Make 'previous' link
172 if ( $to ) {
173 $attr = array( 'title' => $pTitle, 'class' => 'mw-prevlink' );
174 $q = array( 'limit' => $limit, 'to' => $to, 'target' => $target );
175 $plink = $skin->link( $title, $prev, $attr, $q );
176 } else {
177 $plink = $prev;
178 }
179
180 # Make 'next' link
181 if ( $from ) {
182 $attr = array( 'title' => $nTitle, 'class' => 'mw-nextlink' );
183 $q = array( 'limit' => $limit, 'from' => $from, 'target' => $target );
184 $nlink = $skin->link( $title, $next, $attr, $q );
185 } else {
186 $nlink = $next;
187 }
188
189 # Make links to set number of items per page
190 $numLinks = array();
191 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
192 $fmtLimit = $wgLang->formatNum( $num );
193
194 $q = array( 'offset' => $offset, 'limit' => $num, 'target' => $target );
195 $lTitle = wfMsgExt( 'shown-title', array( 'parsemag', 'escape' ), $num );
196 $attr = array( 'title' => $lTitle, 'class' => 'mw-numlink' );
197
198 $numLinks[] = $skin->link( $title, $fmtLimit, $attr, $q );
199 }
200 $nums = $wgLang->pipeList( $numLinks );
201
202 return wfMsgHtml( 'viewprevnext', $plink, $nlink, $nums );
203 }
204 }
205
206