Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onLinksUpdateComplete
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 onArticleDeleteComplete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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 * Hooks for PageAssessments extension
19 *
20 * @file
21 * @ingroup Extensions
22 */
23
24namespace MediaWiki\Extension\PageAssessments;
25
26use Content;
27use LogEntry;
28use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
29use MediaWiki\Hook\LinksUpdateCompleteHook;
30use MediaWiki\Hook\ParserFirstCallInitHook;
31use MediaWiki\Page\Hook\ArticleDeleteCompleteHook;
32use MediaWiki\User\User;
33use Parser;
34use RequestContext;
35use WikiPage;
36
37class Hooks implements
38    ParserFirstCallInitHook,
39    LinksUpdateCompleteHook,
40    ArticleDeleteCompleteHook
41{
42
43    /**
44     * Register the parser function hook
45     * @param Parser $parser
46     */
47    public function onParserFirstCallInit( $parser ) {
48        $parser->setFunctionHook( 'assessment', [ PageAssessmentsDAO::class, 'cacheAssessment' ] );
49    }
50
51    /**
52     * Update assessment records after talk page is saved
53     * @param LinksUpdate $linksUpdate
54     * @param mixed $ticket
55     */
56    public function onLinksUpdateComplete( $linksUpdate, $ticket ) {
57        $assessmentsOnTalkPages = RequestContext::getMain()->getConfig()->get(
58            'PageAssessmentsOnTalkPages'
59        );
60        $title = $linksUpdate->getTitle();
61        // Only check for assessment data where assessments are actually made.
62        if ( ( $assessmentsOnTalkPages && $title->isTalkPage() ) ||
63            ( !$assessmentsOnTalkPages && !$title->isTalkPage() )
64        ) {
65            $pOut = $linksUpdate->getParserOutput();
66            if ( $pOut->getExtensionData( 'ext-pageassessment-assessmentdata' ) !== null ) {
67                $assessmentData = $pOut->getExtensionData( 'ext-pageassessment-assessmentdata' );
68            } else {
69                // Even if there is no assessment data, we still need to run doUpdates
70                // in case any assessment data was deleted from the page.
71                $assessmentData = [];
72            }
73            // Assessment data should only be associated with subject pages regardless
74            // of whether it is recorded on talk pages or subject pages.
75            if ( $title->isTalkPage() ) {
76                $title = $title->getSubjectPage();
77            }
78            PageAssessmentsDAO::doUpdates( $title, $assessmentData, $ticket );
79        }
80    }
81
82    /**
83     * Delete assessment records when page is deleted
84     * @param WikiPage $article
85     * @param User|null $user
86     * @param string $reason
87     * @param int $id
88     * @param Content|null $content
89     * @param LogEntry|null $logEntry
90     * @param int|null $archivedRevisionCount
91     */
92    public function onArticleDeleteComplete(
93        $article,
94        $user,
95        $reason,
96        $id,
97        $content,
98        $logEntry,
99        $archivedRevisionCount
100    ) {
101        PageAssessmentsDAO::deleteRecordsForPage( $id );
102    }
103
104}