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.
8 * @author Bryan Tong Minh <bryan.tongminh@gmail.com>
9 * @author Peter Potrowl <peter017@gmail.com>
12 class SpecialGlobalTemplateUsage
extends SpecialPage
{
13 public function __construct() {
14 parent
::__construct( 'GlobalTemplateUsage' );
20 public function execute( $par ) {
21 global $wgOut, $wgRequest;
23 $target = $par ?
$par : $wgRequest->getVal( 'target' );
24 $this->target
= Title
::newFromText( $target );
30 if ( is_null( $this->target
) ) {
31 $wgOut->setPageTitle( wfMsg( 'globaltemplateusage' ) );
35 $wgOut->setPageTitle( wfMsg( 'globaltemplateusage-for', $this->target
->getPrefixedText() ) );
41 * Shows the search form
43 private function showForm() {
44 global $wgScript, $wgOut, $wgRequest;
47 $html = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) . "\n";
48 // Name of SpecialPage
49 $html .= Html
::hidden( 'title', $this->getTitle( )->getPrefixedText( ) ) . "\n";
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( ) )
56 . "\n\t" . Xml
::element( 'input', array(
58 'value' => wfMsg( 'globaltemplateusage-ok' )
61 // Wrap the entire form in a nice fieldset
62 $html .= Xml
::fieldSet( wfMsg( 'globaltemplateusage-text' ), $formContent ) . "\n</form>";
64 $wgOut->addHtml( $html );
68 * Creates as queryer and executes it based on $wgRequest
70 private function showResult() {
73 $query = new GlobalUsageQuery( $this->target
);
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 );
81 $query->setLimit( $wgRequest->getInt( 'limit', 50 ) );
84 $query->searchTemplate();
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( ) );
95 $navbar = $this->getNavBar( $query );
96 $targetName = $this->target
->getPrefixedText( );
99 $wgOut->addHtml( $navbar );
101 $wgOut->addHtml( '<div id="mw-globaltemplateusage-result">' );
102 foreach ( $query->getSingleResult() as $wiki => $result ) {
105 'globaltemplateusage-on-wiki', 'parseinline',
106 $targetName, WikiMap
::getWikiName( $wiki ) )
108 foreach ( $result as $item ) {
109 $wgOut->addHtml( "\t<li>" . self
::formatItem( $item ) . "</li>\n" );
111 $wgOut->addHtml( "</ul>\n" );
113 $wgOut->addHtml( '</div>' );
116 $wgOut->addHtml( $navbar );
120 * Helper to format a specific item
122 public static function formatItem( $item ) {
123 if ( !$item['namespace'] ) {
124 $page = $item['title'];
126 $page = "{$item['namespace']}:{$item['title']}";
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;
136 * Helper function to create the navbar, stolen from wfViewPrevNext
138 * @param $query GlobalTemplateUsageQuery An executed GlobalTemplateUsageQuery object
139 * @return string Navbar HTML
141 protected function getNavBar( $query ) {
142 global $wgLang, $wgUser;
144 $skin = $wgUser->getSkin();
146 $target = $this->target
->getPrefixedText();
147 $limit = $query->getLimit();
148 $fmtLimit = $wgLang->formatNum( $limit );
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() ) {
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 );
168 # Fetch the title object
169 $title = $this->getTitle();
171 # Make 'previous' link
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 );
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 );
189 # Make links to set number of items per page
191 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
192 $fmtLimit = $wgLang->formatNum( $num );
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' );
198 $numLinks[] = $skin->link( $title, $fmtLimit, $attr, $q );
200 $nums = $wgLang->pipeList( $numLinks );
202 return wfMsgHtml( 'viewprevnext', $plink, $nlink, $nums );