Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
10 / 30
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikiCategoryPage
34.48% covered (danger)
34.48%
10 / 29
50.00% covered (danger)
50.00%
2 / 4
31.78
0.00% covered (danger)
0.00%
0 / 1
 hasViewableContent
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 isHidden
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 isExpectedUnusedCategory
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 doPurge
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Page;
22
23use MediaWiki\Category\Category;
24use MediaWiki\Deferred\DeferredUpdates;
25use MediaWiki\MediaWikiServices;
26
27/**
28 * Special handling for representing category pages.
29 */
30class WikiCategoryPage extends WikiPage {
31
32    /**
33     * Don't return a 404 for categories in use.
34     * In use defined as: either the actual page exists
35     * or the category currently has members.
36     *
37     * @return bool
38     */
39    public function hasViewableContent() {
40        if ( parent::hasViewableContent() ) {
41            return true;
42        } else {
43            $cat = Category::newFromTitle( $this->mTitle );
44            // If any of these are not 0, then has members
45            if ( $cat->getMemberCount()
46                || $cat->getSubcatCount()
47                || $cat->getFileCount()
48            ) {
49                return true;
50            }
51        }
52        return false;
53    }
54
55    /**
56     * Checks if a category is hidden.
57     *
58     * @since 1.27
59     * @return bool
60     */
61    public function isHidden() {
62        $pageId = $this->getTitle()->getArticleID();
63        $pageProps = MediaWikiServices::getInstance()
64            ->getPageProps()
65            ->getProperties( $this->getTitle(), 'hiddencat' );
66
67        return isset( $pageProps[$pageId] );
68    }
69
70    /**
71     * Checks if a category is expected to be an unused category.
72     *
73     * @since 1.33
74     * @return bool
75     */
76    public function isExpectedUnusedCategory() {
77        $pageId = $this->getTitle()->getArticleID();
78        $pageProps = MediaWikiServices::getInstance()
79            ->getPageProps()
80            ->getProperties( $this->getTitle(), 'expectunusedcategory' );
81
82        return isset( $pageProps[$pageId] );
83    }
84
85    /**
86     * Update category counts on purge (T85696)
87     *
88     * @return bool
89     */
90    public function doPurge() {
91        if ( !parent::doPurge() ) {
92            // Aborted by hook most likely
93            return false;
94        }
95
96        $title = $this->mTitle;
97        DeferredUpdates::addCallableUpdate(
98            static function () use ( $title ) {
99                $cat = Category::newFromTitle( $title );
100                // If the category has less than 5000 pages, refresh the counts.
101                // 5000 was chosen based on the discussion at T85696.
102                $cat->refreshCountsIfSmall( 5000 );
103            },
104            // Explicitly PRESEND so that counts are correct before we try to
105            // re-render the page on the next load so {{PAGESINCAT:...}} will
106            // be using the correct new values, not the old ones.
107            DeferredUpdates::PRESEND
108        );
109
110        return true;
111    }
112}
113
114/** @deprecated class alias since 1.44 */
115class_alias( WikiCategoryPage::class, 'WikiCategoryPage' );