PrefixSearch (ApiOpenSearch) now supports searching in multiple namespaces
[lhc/web/wiklou.git] / includes / CategoryViewer.php
index a9f5d23..49818e6 100644 (file)
  */
 
 class CategoryViewer extends ContextSource {
-       var $limit, $from, $until,
-               $articles, $articles_start_char,
-               $children, $children_start_char,
-               $showGallery, $imgsNoGalley,
-               $imgsNoGallery_start_char,
-               $imgsNoGallery;
+       /** @var int */
+       public $limit;
 
-       /**
-        * @var Array
-        */
-       var $nextPage;
+       /** @var array */
+       protected $from;
 
-       /**
-        * @var Array
-        */
-       var $flip;
+       /** @var array */
+       protected $until;
 
-       /**
-        * @var Title
-        */
-       var $title;
+       /** @var string[] */
+       public $articles;
 
-       /**
-        * @var Collation
-        */
-       var $collation;
+       /** @var array */
+       public $articles_start_char;
 
-       /**
-        * @var ImageGallery
-        */
-       var $gallery;
+       /** @var array */
+       protected $children;
 
-       /**
-        * Category object for this page
-        * @var Category
-        */
+       /** @var array */
+       protected $children_start_char;
+
+       /** @var bool */
+       protected $showGallery;
+
+       /** @var array */
+       protected $imgsNoGallery_start_char;
+
+       /** @var array */
+       protected $imgsNoGallery;
+
+       /** @var array */
+       protected $nextPage;
+
+       /** @var array */
+       protected $flip;
+
+       /** @var Title */
+       protected $title;
+
+       /** @var Collation */
+       protected $collation;
+
+       /** @var ImageGallery */
+       protected $gallery;
+
+       /** @var Category Category object for this page. */
        private $cat;
 
-       /**
-        * The original query array, to be used in generating paging links.
-        * @var array
-        */
+       /** @var array The original query array, to be used in generating paging links. */
        private $query;
 
        /**
-        * Constructor
-        *
         * @since 1.19 $context is a second, required parameter
-        * @param $title Title
-        * @param $context IContextSource
+        * @param Title $title
+        * @param IContextSource $context
         * @param array $from An array with keys page, subcat,
         *        and file for offset of results of each section (since 1.17)
         * @param array $until An array with 3 keys for until of each section (since 1.17)
-        * @param $query Array
+        * @param array $query
         */
-       function __construct( $title, IContextSource $context, $from = array(), $until = array(), $query = array() ) {
+       function __construct( $title, IContextSource $context, $from = array(),
+               $until = array(), $query = array()
+       ) {
                global $wgCategoryPagingLimit;
                $this->title = $title;
                $this->setContext( $context );
@@ -141,7 +148,15 @@ class CategoryViewer extends ContextSource {
                $this->children = array();
                $this->children_start_char = array();
                if ( $this->showGallery ) {
-                       $this->gallery = ImageGalleryBase::factory();
+                       // Note that null for mode is taken to mean use default.
+                       $mode = $this->getRequest()->getVal( 'gallerymode', null );
+                       try {
+                               $this->gallery = ImageGalleryBase::factory( $mode );
+                       } catch ( MWException $e ) {
+                               // User specified something invalid, fallback to default.
+                               $this->gallery = ImageGalleryBase::factory();
+                       }
+
                        $this->gallery->setHideBadImages();
                        $this->gallery->setContext( $this->getContext() );
                } else {
@@ -152,9 +167,9 @@ class CategoryViewer extends ContextSource {
 
        /**
         * Add a subcategory to the internal lists, using a Category object
-        * @param $cat Category
-        * @param $sortkey
-        * @param $pageLength
+        * @param Category $cat
+        * @param string $sortkey
+        * @param int $pageLength
         */
        function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) {
                // Subcategory; strip the 'Category' namespace from the link text.
@@ -173,15 +188,6 @@ class CategoryViewer extends ContextSource {
                        $this->getSubcategorySortChar( $cat->getTitle(), $sortkey );
        }
 
-       /**
-        * Add a subcategory to the internal lists, using a title object
-        * @deprecated since 1.17 kept for compatibility, use addSubcategoryObject instead
-        */
-       function addSubcategory( Title $title, $sortkey, $pageLength ) {
-               wfDeprecated( __METHOD__, '1.17' );
-               $this->addSubcategoryObject( Category::newFromTitle( $title ), $sortkey, $pageLength );
-       }
-
        /**
         * Get the character to be used for sorting subcategories.
         * If there's a link from Category:A to Category:B, the sortkey of the resulting
@@ -209,10 +215,10 @@ class CategoryViewer extends ContextSource {
 
        /**
         * Add a page in the image namespace
-        * @param $title Title
-        * @param $sortkey
-        * @param $pageLength
-        * @param $isRedirect bool
+        * @param Title $title
+        * @param string $sortkey
+        * @param int $pageLength
+        * @param bool $isRedirect
         */
        function addImage( Title $title, $sortkey, $pageLength, $isRedirect = false ) {
                global $wgContLang;
@@ -239,10 +245,10 @@ class CategoryViewer extends ContextSource {
 
        /**
         * Add a miscellaneous page
-        * @param $title
-        * @param $sortkey
-        * @param $pageLength
-        * @param $isRedirect bool
+        * @param Title $title
+        * @param string $sortkey
+        * @param int $pageLength
+        * @param bool $isRedirect
         */
        function addPage( $title, $sortkey, $pageLength, $isRedirect = false ) {
                global $wgContLang;
@@ -424,7 +430,12 @@ class CategoryViewer extends ContextSource {
                        $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
 
                        $r .= "<div id=\"mw-category-media\">\n";
-                       $r .= '<h2>' . $this->msg( 'category-media-header', wfEscapeWikiText( $this->title->getText() ) )->text() . "</h2>\n";
+                       $r .= '<h2>' .
+                               $this->msg(
+                                       'category-media-header',
+                                       wfEscapeWikiText( $this->title->getText() )
+                               )->text() .
+                               "</h2>\n";
                        $r .= $countmsg;
                        $r .= $this->getSectionPagingLinks( 'file' );
                        if ( $this->showGallery ) {
@@ -443,12 +454,14 @@ class CategoryViewer extends ContextSource {
         * of the output.
         *
         * @param string $type 'page', 'subcat', or 'file'
-        * @return String: HTML output, possibly empty if there are no other pages
+        * @return string HTML output, possibly empty if there are no other pages
         */
        private function getSectionPagingLinks( $type ) {
                if ( isset( $this->until[$type] ) && $this->until[$type] !== null ) {
                        return $this->pagingLinks( $this->nextPage[$type], $this->until[$type], $type );
-               } elseif ( $this->nextPage[$type] !== null || ( isset( $this->from[$type] ) && $this->from[$type] !== null ) ) {
+               } elseif ( $this->nextPage[$type] !== null
+                       || ( isset( $this->from[$type] ) && $this->from[$type] !== null )
+               ) {
                        return $this->pagingLinks( $this->from[$type], $this->nextPage[$type], $type );
                } else {
                        return '';
@@ -466,10 +479,10 @@ class CategoryViewer extends ContextSource {
         * Format a list of articles chunked by letter, either as a
         * bullet list or a columnar format, depending on the length.
         *
-        * @param $articles Array
-        * @param $articles_start_char Array
-        * @param $cutoff Int
-        * @return String
+        * @param array $articles
+        * @param array $articles_start_char
+        * @param int $cutoff
+        * @return string
         * @private
         */
        function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
@@ -499,9 +512,9 @@ class CategoryViewer extends ContextSource {
         * More distant TODO: Scrap this and use CSS columns, whenever IE finally
         * supports those.
         *
-        * @param $articles Array
-        * @param $articles_start_char Array
-        * @return String
+        * @param array $articles
+        * @param string[] $articles_start_char
+        * @return string
         * @private
         */
        static function columnList( $articles, $articles_start_char ) {
@@ -555,15 +568,16 @@ class CategoryViewer extends ContextSource {
 
        /**
         * Format a list of articles chunked by letter in a bullet list.
-        * @param $articles Array
-        * @param $articles_start_char Array
-        * @return String
+        * @param array $articles
+        * @param string[] $articles_start_char
+        * @return string
         * @private
         */
        static function shortList( $articles, $articles_start_char ) {
                $r = '<h3>' . htmlspecialchars( $articles_start_char[0] ) . "</h3>\n";
                $r .= '<ul><li>' . $articles[0] . '</li>';
-               for ( $index = 1; $index < count( $articles ); $index++ ) {
+               $articleCount = count( $articles );
+               for ( $index = 1; $index < $articleCount; $index++ ) {
                        if ( $articles_start_char[$index] != $articles_start_char[$index - 1] ) {
                                $r .= "</ul><h3>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
                        }
@@ -581,7 +595,7 @@ class CategoryViewer extends ContextSource {
         * @param string $last The 'from' parameter for the generated URL
         * @param string $type A prefix for parameters, 'page' or 'subcat' or
         *     'file'
-        * @return String HTML
+        * @return string HTML
         */
        private function pagingLinks( $first, $last, $type = '' ) {
                $prevLink = $this->msg( 'prevn' )->numParams( $this->limit )->escaped();
@@ -619,8 +633,8 @@ class CategoryViewer extends ContextSource {
         * Takes a title, and adds the fragment identifier that
         * corresponds to the correct segment of the category.
         *
-        * @param Title $title: The title (usually $this->title)
-        * @param string $section: Which section
+        * @param Title $title The title (usually $this->title)
+        * @param string $section Which section
         * @throws MWException
         * @return Title
         */
@@ -652,20 +666,20 @@ class CategoryViewer extends ContextSource {
         * @param int $rescnt The number of items returned by our database query.
         * @param int $dbcnt The number of items according to the category table.
         * @param string $type 'subcat', 'article', or 'file'
-        * @return String: A message giving the number of items, to output to HTML.
+        * @return string A message giving the number of items, to output to HTML.
         */
        private function getCountMessage( $rescnt, $dbcnt, $type ) {
-               # There are three cases:
-               #   1) The category table figure seems sane.  It might be wrong, but
-               #      we can't do anything about it if we don't recalculate it on ev-
-               #      ery category view.
-               #   2) The category table figure isn't sane, like it's smaller than the
-               #      number of actual results, *but* the number of results is less
-               #      than $this->limit and there's no offset.  In this case we still
-               #      know the right figure.
-               #   3) We have no idea.
-
-               # Check if there's a "from" or "until" for anything
+               // There are three cases:
+               //   1) The category table figure seems sane.  It might be wrong, but
+               //      we can't do anything about it if we don't recalculate it on ev-
+               //      ery category view.
+               //   2) The category table figure isn't sane, like it's smaller than the
+               //      number of actual results, *but* the number of results is less
+               //      than $this->limit and there's no offset.  In this case we still
+               //      know the right figure.
+               //   3) We have no idea.
+
+               // Check if there's a "from" or "until" for anything
 
                // This is a little ugly, but we seem to use different names
                // for the paging types then for the messages.
@@ -685,23 +699,22 @@ class CategoryViewer extends ContextSource {
                if ( $dbcnt == $rescnt ||
                        ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt )
                ) {
-                       # Case 1: seems sane.
+                       // Case 1: seems sane.
                        $totalcnt = $dbcnt;
                } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
-                       # Case 2: not sane, but salvageable.  Use the number of results.
-                       # Since there are fewer than 200, we can also take this opportunity
-                       # to refresh the incorrect category table entry -- which should be
-                       # quick due to the small number of entries.
+                       // Case 2: not sane, but salvageable.  Use the number of results.
+                       // Since there are fewer than 200, we can also take this opportunity
+                       // to refresh the incorrect category table entry -- which should be
+                       // quick due to the small number of entries.
                        $totalcnt = $rescnt;
                        $this->cat->refreshCounts();
                } else {
-                       # Case 3: hopeless.  Don't give a total count at all.
-                       # Give grep a chance to find the usages: category-subcat-count-limited,
-                       # category-article-count-limited, category-file-count-limited
+                       // Case 3: hopeless.  Don't give a total count at all.
+                       // Messages: category-subcat-count-limited, category-article-count-limited,
+                       // category-file-count-limited
                        return $this->msg( "category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock();
                }
-               # Give grep a chance to find the usages:
-               # category-subcat-count, category-article-count, category-file-count
+               // Messages: category-subcat-count, category-article-count, category-file-count
                return $this->msg( "category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock();
        }
 }