Merge "SkinTemplate: extract formatLanguageName() from outputPage()"
[lhc/web/wiklou.git] / includes / specials / SpecialMostlinkedtemplates.php
index b8d47e6..22932e5 100644 (file)
@@ -1,31 +1,43 @@
 <?php
 /**
+ * Implements Special:Mostlinkedtemplates
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup SpecialPage
+ * @author Rob Church <robchur@gmail.com>
  */
+
 /**
  * Special page lists templates with a large number of
  * transclusion links, i.e. "most used" templates
  *
  * @ingroup SpecialPage
- * @author Rob Church <robchur@gmail.com>
  */
-class SpecialMostlinkedtemplates extends QueryPage {
+class MostlinkedTemplatesPage extends QueryPage {
 
-       /**
-        * Name of the report
-        *
-        * @return string
-        */
-       public function getName() {
-               return 'Mostlinkedtemplates';
+       function __construct( $name = 'Mostlinkedtemplates' ) {
+               parent::__construct( $name );
        }
 
        /**
         * Is this report expensive, i.e should it be cached?
         *
-        * @return bool
+        * @return Boolean
         */
        public function isExpensive() {
                return true;
@@ -34,7 +46,7 @@ class SpecialMostlinkedtemplates extends QueryPage {
        /**
         * Is there a feed available?
         *
-        * @return bool
+        * @return Boolean
         */
        public function isSyndicated() {
                return false;
@@ -43,90 +55,74 @@ class SpecialMostlinkedtemplates extends QueryPage {
        /**
         * Sort the results in descending order?
         *
-        * @return bool
+        * @return Boolean
         */
        public function sortDescending() {
                return true;
        }
 
-       /**
-        * Generate SQL for the report
-        *
-        * @return string
-        */
-       public function getSql() {
-               $dbr = wfGetDB( DB_SLAVE );
-               $templatelinks = $dbr->tableName( 'templatelinks' );
-               $name = $dbr->addQuotes( $this->getName() );
-               return "SELECT {$name} AS type,
-                       " . NS_TEMPLATE . " AS namespace,
-                       tl_title AS title,
-                       COUNT(*) AS value
-                       FROM {$templatelinks}
-                       WHERE tl_namespace = " . NS_TEMPLATE . "
-                       GROUP BY 1, 2, 3";
+       public function getQueryInfo() {
+               return array (
+                       'tables' => array ( 'templatelinks' ),
+                       'fields' => array ( 'namespace' => 'tl_namespace',
+                                       'title' => 'tl_title',
+                                       'value' => 'COUNT(*)' ),
+                       'conds' => array ( 'tl_namespace' => NS_TEMPLATE ),
+                       'options' => array( 'GROUP BY' => array( 'tl_namespace', 'tl_title' ) )
+               );
        }
 
        /**
         * Pre-cache page existence to speed up link generation
         *
-        * @param Database $dbr Database connection
-        * @param int $res Result pointer
+        * @param $db DatabaseBase connection
+        * @param $res ResultWrapper
         */
        public function preprocessResults( $db, $res ) {
+               if ( !$res->numRows() ) {
+                       return;
+               }
+
                $batch = new LinkBatch();
-               while( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $batch->add( $row->namespace, $row->title );
                }
                $batch->execute();
-               if( $db->numRows( $res ) > 0 )
-                       $db->dataSeek( $res, 0 );
+
+               $res->seek( 0 );
        }
 
        /**
         * Format a result row
         *
-        * @param Skin $skin Skin to use for UI elements
-        * @param object $result Result row
-        * @return string
+        * @param $skin Skin to use for UI elements
+        * @param $result Result row
+        * @return String
         */
        public function formatResult( $skin, $result ) {
                $title = Title::makeTitleSafe( $result->namespace, $result->title );
-               if( $title instanceof Title ) {
-                       return wfSpecialList(
-                               $skin->makeLinkObj( $title ),
-                               $this->makeWlhLink( $title, $skin, $result )
-                       );
-               } else {
-                       $tsafe = htmlspecialchars( $result->title );
-                       return "Invalid title in result set; {$tsafe}";
+               if ( !$title ) {
+                       return Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
+                               Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
                }
+
+               return $this->getLanguage()->specialList(
+                       Linker::link( $title ),
+                       $this->makeWlhLink( $title, $result )
+               );
        }
 
        /**
         * Make a "what links here" link for a given title
         *
-        * @param Title $title Title to make the link for
-        * @param Skin $skin Skin to use
-        * @param object $result Result row
-        * @return string
+        * @param $title Title to make the link for
+        * @param $result Result row
+        * @return String
         */
-       private function makeWlhLink( $title, $skin, $result ) {
-               global $wgLang;
-               $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
-               $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
-                       $wgLang->formatNum( $result->value ) );
-               return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
+       private function makeWlhLink( $title, $result ) {
+               $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
+               $label = $this->msg( 'ntransclusions' )->numParams( $result->value )->escaped();
+               return Linker::link( $wlh, $label );
        }
 }
 
-/**
- * Execution function
- *
- * @param mixed $par Parameters passed to the page
- */
-function wfSpecialMostlinkedtemplates( $par = false ) {
-       list( $limit, $offset ) = wfCheckLimits();
-       $mlt = new SpecialMostlinkedtemplates();
-       $mlt->doQuery( $offset, $limit );
-}