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 $target = $par ?
$par : $this->getRequest()->getVal( 'target' );
22 $this->target
= Title
::newFromText( $target );
28 if ( $this->target
!== null ) {
29 $this->getOutput()->setPageTitle( wfMsg( 'globaltemplateusage-for', $this->target
->getPrefixedText() ) );
36 * Shows the search form
38 private function showForm() {
42 $html = Xml
::openElement( 'form', array( 'action' => $wgScript ) ) . "\n";
43 // Name of SpecialPage
44 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
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( ) )
51 . "\n\t" . Xml
::element( 'input', array(
53 'value' => wfMsg( 'globaltemplateusage-ok' )
56 // Wrap the entire form in a nice fieldset
57 $html .= Xml
::fieldSet( wfMsg( 'globaltemplateusage-text' ), $formContent ) . "\n</form>";
59 $this->getOutput()->addHtml( $html );
63 * Creates as queryer and executes it based on the WebRequest object
65 private function showResult() {
66 $request = $this->getRequest();
67 $query = new GlobalUsageQuery( $this->target
);
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 );
75 $query->setLimit( $request->getInt( 'limit', 50 ) );
78 $query->searchTemplate();
80 $out = $this->getOutput();
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( ) );
88 $navbar = $this->getNavBar( $query );
89 $targetName = $this->target
->getPrefixedText( );
92 $out->addHtml( $navbar );
94 $out->addHtml( '<div id="mw-globaltemplateusage-result">' );
95 foreach ( $query->getSingleResult() as $wiki => $result ) {
98 'globaltemplateusage-on-wiki', 'parseinline',
99 $targetName, WikiMap
::getWikiName( $wiki ) )
101 foreach ( $result as $item ) {
102 $out->addHtml( "\t<li>" . self
::formatItem( $item ) . "</li>\n" );
104 $out->addHtml( "</ul>\n" );
106 $out->addHtml( '</div>' );
109 $out->addHtml( $navbar );
113 * Helper to format a specific item
115 public static function formatItem( $item ) {
116 if ( !$item['namespace'] ) {
117 $page = $item['title'];
119 $page = "{$item['namespace']}:{$item['title']}";
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;
129 * Helper function to create the navbar, stolen from wfViewPrevNext
131 * @param $query GlobalTemplateUsageQuery An executed GlobalTemplateUsageQuery object
132 * @return string Navbar HTML
134 protected function getNavBar( $query ) {
135 $lang = $this->getLang();
136 $target = $this->target
->getPrefixedText();
137 $limit = $query->getLimit();
138 $fmtLimit = $lang->formatNum( $limit );
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() ) {
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 );
158 # Fetch the title object
159 $title = $this->getTitle();
161 # Make 'previous' link
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 );
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 );
179 # Make links to set number of items per page
181 foreach ( array( 20, 50, 100, 250, 500 ) as $num ) {
182 $fmtLimit = $lang->formatNum( $num );
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' );
188 $numLinks[] = Linker
::link( $title, $fmtLimit, $attr, $q );
190 $nums = $lang->pipeList( $numLinks );
192 return wfMsgHtml( 'viewprevnext', $plink, $nlink, $nums );