Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 84
CollectionSession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 13
1332
0.00% covered (danger)
0.00%
0 / 84
 hasSession
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 startSession
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 touchSession
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 clearCollection
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 enable
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 disable
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 7
 isEnabled
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 4
 hasItems
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 countArticles
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 8
 findArticle
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 11
 purge
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 22
 getCollection
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 setCollection
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\Session\SessionManager;
/**
 * Collection Extension for MediaWiki
 *
 * Copyright (C) PediaPress GmbH
 *
 * 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
 */
class CollectionSession {
    /**
     * @return bool
     */
    public static function hasSession() {
        $session = SessionManager::getGlobalSession();
        return isset( $session['wsCollection'] );
    }
    public static function startSession() {
        $session = SessionManager::getGlobalSession();
        $session->persist();
        self::clearCollection();
    }
    public static function touchSession() {
        $session = SessionManager::getGlobalSession();
        $collection = $session['wsCollection'];
        $collection['timestamp'] = wfTimestampNow();
        $session['wsCollection'] = $collection;
    }
    public static function clearCollection() {
        $session = SessionManager::getGlobalSession();
        $session['wsCollection'] = [
            'enabled' => true,
            'title' => '',
            'subtitle' => '',
            'settings' => [],
            'items' => [],
        ];
        CollectionSuggest::clear();
        self::touchSession();
    }
    public static function enable() {
        $session = SessionManager::getGlobalSession();
        $session->persist();
        $session['wsCollection']['enabled'] = true;
        self::touchSession();
    }
    public static function disable() {
        $session = SessionManager::getGlobalSession();
        if ( !isset( $session['wsCollection'] ) ) {
            return;
        }
        self::clearCollection();
        $session['wsCollection']['enabled'] = false;
        self::touchSession();
    }
    /**
     * @return bool
     */
    public static function isEnabled() {
        $session = SessionManager::getGlobalSession();
        return isset( $session['wsCollection'] ) &&
            isset( $session['wsCollection']['enabled'] ) &&
            $session['wsCollection']['enabled'];
    }
    /**
     * @return bool
     */
    public static function hasItems() {
        $session = SessionManager::getGlobalSession();
        return isset( $session['wsCollection'] ) &&
            isset( $session['wsCollection']['items'] );
    }
    /**
     * @return int
     */
    public static function countArticles() {
        $session = SessionManager::getGlobalSession();
        if ( !self::hasItems() ) {
            return 0;
        }
        $count = 0;
        foreach ( $session['wsCollection']['items'] as $item ) {
            if ( $item['type'] == 'article' ) {
                $count++;
            }
        }
        return $count;
    }
    /**
     * @param string $title
     * @param int $oldid
     * @return int
     */
    public static function findArticle( $title, $oldid = 0 ) {
        if ( !self::hasItems() ) {
            return -1;
        }
        $session = SessionManager::getGlobalSession();
        foreach ( $session['wsCollection']['items'] as $index => $item ) {
            if ( $item['type'] == 'article' && $item['title'] == $title ) {
                if ( $oldid ) {
                    if ( $item['revision'] == strval( $oldid ) ) {
                        return $index;
                    }
                } else {
                    if ( $item['revision'] == $item['latest'] ) {
                        return $index;
                    }
                }
            }
        }
        return -1;
    }
    /**
     * @return bool
     */
    public static function purge() {
        $session = SessionManager::getGlobalSession();
        if ( !isset( $session['wsCollection'] ) ) {
            return false;
        }
        $coll = $session['wsCollection'];
        $newitems = [];
        if ( isset( $coll['items'] ) ) {
            $batch = new LinkBatch;
            $lc = MediaWikiServices::getInstance()->getLinkCache();
            foreach ( $coll['items'] as $item ) {
                if ( $item['type'] == 'article' ) {
                    $t = Title::newFromText( $item['title'] );
                    $batch->addObj( $t );
                }
            }
            $batch->execute();
            foreach ( $coll['items'] as $item ) {
                if ( $item['type'] == 'article' ) {
                    $t = Title::newFromText( $item['title'] );
                    if ( $t && !$lc->isBadLink( $t->getPrefixedDBkey() ) ) {
                        $newitems[] = $item;
                    }
                } else {
                    $newitems[] = $item;
                }
            }
        }
        $coll['items'] = $newitems;
        $session['wsCollection'] = $coll;
        return true;
    }
    /**
     * @return array
     */
    public static function getCollection() {
        $session = SessionManager::getGlobalSession();
        $collection = self::purge() ? $session['wsCollection'] : [];
        return array_merge( [
            /* Make sure required properties are present.  */
            'title' => '',
            'subtitle' => '',
            'settings' => [],
            'items' => [],
        ], $collection );
    }
    /**
     * @param array $collection
     */
    public static function setCollection( array $collection ) {
        $session = SessionManager::getGlobalSession();
        $session['wsCollection'] = $collection;
        self::touchSession();
    }
}