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