http://www.mediawiki.org/wiki/Special:Code/MediaWiki/56228

Revision: 56228
Author:   btongminh
Date:     2009-09-12 12:21:04 +0000 (Sat, 12 Sep 2009)

Log Message:
-----------
* Did some work on the UI: should be more or less complete now.
* Changed the indices to what seems logical to me, but I think somebody who 
knows more about that should have a look over it.

Modified Paths:
--------------
    trunk/extensions/GlobalUsage/GlobalUsage.i18n.php
    trunk/extensions/GlobalUsage/GlobalUsage.pg.sql
    trunk/extensions/GlobalUsage/GlobalUsage.sql
    trunk/extensions/GlobalUsage/SpecialGlobalUsage.php

Modified: trunk/extensions/GlobalUsage/GlobalUsage.i18n.php
===================================================================
--- trunk/extensions/GlobalUsage/GlobalUsage.i18n.php   2009-09-12 11:36:19 UTC 
(rev 56227)
+++ trunk/extensions/GlobalUsage/GlobalUsage.i18n.php   2009-09-12 12:21:04 UTC 
(rev 56228)
@@ -15,7 +15,8 @@
        'globalusage-for'  => 'Global usage for "$1"',
        'globalusage-desc' => '[[Special:GlobalUsage|Special page]] to view 
global file usage',
        'globalusage-ok'   => 'Search',
-       'globalusage-text' => 'Search global file usage.'
+       'globalusage-text' => 'Search global file usage',
+       'globalusage-on-wiki' => 'Usage of [[:File:$1|$1]] on $2',
 );
 
 /** Message documentation (Message documentation)

Modified: trunk/extensions/GlobalUsage/GlobalUsage.pg.sql
===================================================================
--- trunk/extensions/GlobalUsage/GlobalUsage.pg.sql     2009-09-12 11:36:19 UTC 
(rev 56227)
+++ trunk/extensions/GlobalUsage/GlobalUsage.pg.sql     2009-09-12 12:21:04 UTC 
(rev 56228)
@@ -4,7 +4,7 @@
        gil_page_namespace TEXT     NOT NULL,
        gil_page_title     TEXT     NOT NULL,
        gil_to             TEXT     NOT NULL,
-       PRIMARY KEY (gil_wiki, gil_page)
+       PRIMARY KEY (gil_to, gil_wiki, gil_page)
 );
-CREATE INDEX globalimagelinks_wiki ON globalimagelinks(gil_to, gil_wiki);
+CREATE INDEX globalimagelinks_wiki ON globalimagelinks(gil_wiki, gil_page);
 

Modified: trunk/extensions/GlobalUsage/GlobalUsage.sql
===================================================================
--- trunk/extensions/GlobalUsage/GlobalUsage.sql        2009-09-12 11:36:19 UTC 
(rev 56227)
+++ trunk/extensions/GlobalUsage/GlobalUsage.sql        2009-09-12 12:21:04 UTC 
(rev 56228)
@@ -15,6 +15,6 @@
        -- If the domain format is used, only the "en.wikip" part is needed for 
an
        -- unique lookup
        
-       PRIMARY KEY (gil_wiki, gil_page, gil_to),
-       INDEX (gil_to, gil_wiki)
+       PRIMARY KEY (gil_to, gil_wiki, gil_page), 
+       INDEX (gil_wiki, gil_page)
 ) /*$wgDBTableOptions*/;

Modified: trunk/extensions/GlobalUsage/SpecialGlobalUsage.php
===================================================================
--- trunk/extensions/GlobalUsage/SpecialGlobalUsage.php 2009-09-12 11:36:19 UTC 
(rev 56227)
+++ trunk/extensions/GlobalUsage/SpecialGlobalUsage.php 2009-09-12 12:21:04 UTC 
(rev 56228)
@@ -10,6 +10,9 @@
                wfLoadExtensionMessages( 'globalusage' );
        } 
        
+       /**
+        * Entry point
+        */
        public function execute( $par ) {
                global $wgOut, $wgRequest;
                
@@ -18,6 +21,8 @@
                
                $this->setHeaders();
                
+               $this->showForm( $title );
+               
                if ( is_null( $title ) )
                {
                        $wgOut->setPageTitle( wfMsg( 'globalusage' ) );
@@ -26,76 +31,176 @@
                
                $wgOut->setPageTitle( wfMsg( 'globalusage-for', 
$title->getPrefixedText() ) );
                
-               $pager = new GlobalUsagePager( $title );
+               $this->showResult( $title );
+       }
+       
+       /**
+        * Shows the search form
+        */
+       private function showForm( $title ) {
+               global $wgScript, $wgOut;
+                               
+               $html = Xml::openElement( 'form', array( 'action' => $wgScript 
) ) . "\n";
+               $html .= Xml::hidden( 'title', 
$this->getTitle()->getPrefixedText() ) . "\n";
+               $formContent = "\t" . Xml::input( 'target', 40, is_null( $title 
) ? '' : $title->getPrefixedText() ) 
+                       . "\n\t" . Xml::element( 'input', array( 
+                                       'type' => 'submit', 
+                                       'value' => wfMsg( 'globalusage-ok' )
+                                       ) ); 
+               $html .= Xml::fieldSet( wfMsg( 'globalusage-text' ), 
$formContent ) . "\n</form>";
                
-               $wgOut->addHTML(
-                       '<p>' . $pager->getNavigationBar() . '</p>' .
-                       '<ul>' . $pager->getBody() . '</ul>' .
-                       '<p>' . $pager->getNavigationBar() . '</p>' );
+               $wgOut->addHtml( $html );
        }
+       
+       /**
+        * Creates as queryer and executes it based on $wgRequest
+        */
+       private function showResult( $target ) {
+               global $wgRequest;
+               
+               $query = new GlobalUsageQuery( $target );
+               
+               // Extract params from $wgRequest
+               $query->setContinue( $wgRequest->getInt( 'offset', 0 ) );
+               $query->setLimit( $wgRequest->getInt( 'limit', 50 ) );
+               
+               // Perform query
+               $query->execute();
+               
+               // Show result
+               global $wgOut;
+               
+               $navbar = wfViewPrevNext( $query->getOffset(), 
$query->getLimit(), $this->getTitle(), 
+                               'target=' . $target->getPrefixedText(), 
!$query->hasMore() );
+               $targetName = $target->getText();
+               
+               $wgOut->addHtml( $navbar );
+               
+               foreach ( $query->getResult() as $wiki => $result ) {
+                       $wgOut->addHtml( 
+                                       '<h2>' . wfMsgExt( 
+                                               'globalusage-on-wiki', 
'parseinline',
+                                               $targetName, $wiki )  
+                                       . "</h2><ul>\n" );
+                       foreach ( $result as $item )
+                               $wgOut->addHtml( "\t<li>" . $this->formatItem( 
$item ) . "</li>\n" );
+                       $wgOut->addHtml( "</ul>\n" );
+               }
+               
+               $wgOut->addHtml( $navbar );
+       }
+       /**
+        * Helper to format a specific item
+        * TODO: Make links
+        */
+       private function formatItem( $item ) {
+               if ( !$item['namespace'] )
+                       return htmlspecialchars( $item['title'] );
+               else
+                       return htmlspecialchars( 
"{$item['namespace']}:{$item['title']}" );
+       }
+
 }
 
+
+
 /**
- * Pager for globalimagelinks.
+ * A helper class to query the globalimagelinks table
+ * 
+ * Should maybe simply resort to offset/limit query rather 
  */
-class GlobalUsagePager extends IndexPager {
-       public function __construct( $title = null ) {
-               // Initialize parent first
-               parent::__construct();
-               
-               $this->title = $title;
-               
-               // Override the DB
+class GlobalUsageQuery {
+       private $limit = 50;
+       private $offset = 0;
+       private $hasMore = false;
+       private $result;
+       
+       
+       public function __construct( $target ) {
                global $wgGlobalUsageDatabase;
-               $this->mDb = wfGetDB( DB_SLAVE, array(), $wgGlobalUsageDatabase 
);
+               $this->db = wfGetDB( DB_SLAVE, array(), $wgGlobalUsageDatabase 
);
+               $this->target = $target;
+
        }
-       public function formatRow( $row ) {
-               return '<li>'
-                               . htmlspecialchars( $row->gil_wiki ) . ':'
-                               . htmlspecialchars( $row->gil_page_namespace ) 
. ':'
-                               . htmlspecialchars( $row->gil_page_title ) 
-                               . "</li>\n";
+       
+       /**
+        * Set the offset parameter
+        * 
+        * @param $offset int offset
+        */
+       public function setContinue( $offset ) {
+               $this->offset = $offset;
        }
-       public function getQueryInfo() {
-               $info = array(
-                               'tables' => array( 'globalimagelinks' ),
-                               'fields' => array( 
-                                               'gil_wiki', 
-                                               'gil_page_namespace', 
-                                               'gil_page_title', 
-                               ),      
+       /**
+        * Return the offset set by the user
+        * 
+        * @return int offset
+        */
+       public function getOffset() {
+               return $this->offset;
+       }
+       /**
+        * Set the maximum amount of items to return. Capped at 500.
+        * 
+        * @param $limit int The limit
+        */
+       public function setLimit( $limit ) {
+               $this->limit = min( $limit, 500 );
+       }
+       public function getLimit() {
+               return $this->limit;
+       }
+       
+       
+       /**
+        * Executes the query
+        */
+       public function execute() {
+               $res = $this->db->select( 'globalimagelinks',
+                               array( 
+                                       'gil_wiki', 
+                                       'gil_page', 
+                                       'gil_page_namespace', 
+                                       'gil_page_title' 
+                               ),
+                               array(
+                                       'gil_to' => $this->target->getDBkey()
+                               ),
+                               __METHOD__,
+                               array( 
+                                       'ORDER BY' => 'gil_wiki, gil_page',
+                                       'LIMIT' => $this->limit + 1,
+                                       'OFFSET' => $this->offset,
+                               )
                );
-               if ( !is_null( $this->title ) && $this->title->getNamespace() 
== NS_FILE ) {
-                       $info['conds'] = array( 'gil_to' => 
$this->title->getDBkey() );
+               
+               $count = 0;
+               $this->hasMore = false;
+               $this->result = array();
+               foreach ( $res as $row ) {
+                       $count++;
+                       if ( $count > $this->limit ) {
+                               $this->hasMore = true;
+                               break;
+                       }
+                       if ( !isset( $this->result[$row->gil_wiki] ) )
+                               $this->result[$row->gil_wiki] = array();
+                       $this->result[$row->gil_wiki][] = array( 
+                               'namespace' => $row->gil_page_namespace, 
+                               'title' => $row->gil_page_title 
+                       );      
                }
-               return $info;
        }
-       public function getIndexField() {
-               // FIXME: This is non-unique! Needs a hack in IndexPager to 
sort on (wiki, page)
-               return 'gil_wiki';
+       public function getResult() {
+               return $this->result;
        }
-
        
-       public function getNavigationBar() {
-               global $wgLang;
-
-               if ( isset( $this->mNavigationBar ) ) {
-                       return $this->mNavigationBar;
-               }
-               $fmtLimit = $wgLang->formatNum( $this->mLimit );
-               $linkTexts = array(
-                       'prev' => wfMsgExt( 'whatlinkshere-prev', array( 
'escape', 'parsemag' ), $fmtLimit ),
-                       'next' => wfMsgExt( 'whatlinkshere-next', array( 
'escape', 'parsemag' ), $fmtLimit ),
-                       'first' => wfMsgHtml( 'page_first' ),
-                       'last' => wfMsgHtml( 'page_last' )
-               );
-
-               $pagingLinks = $this->getPagingLinks( $linkTexts );
-               $limitLinks = $this->getLimitLinks();
-               $limits = $wgLang->pipeList( $limitLinks );
-
-               $this->mNavigationBar = "(" . $wgLang->pipeList( array( 
$pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
-                       wfMsgExt( 'viewprevnext', array( 'parsemag', 'escape', 
'replaceafter' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
-               return $this->mNavigationBar;
+       /**
+        * Returns whether there are more results
+        * 
+        * @return bool
+        */
+       public function hasMore() {
+               return $this->hasMore;
        }
-}
\ No newline at end of file
+}



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to