Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArticleChangedJob
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 newSpec
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 notify
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Wikistories\Jobs;
4
5use IJobSpecification;
6use Job;
7use JobSpecification;
8use MediaWiki\Config\Config;
9use MediaWiki\Extension\Notifications\Model\Event;
10use MediaWiki\Extension\Wikistories\Hooks\EchoNotificationsHandlers;
11use MediaWiki\Extension\Wikistories\PageLinksSearch;
12use MediaWiki\Extension\Wikistories\StoryContent;
13use MediaWiki\Extension\Wikistories\StoryContentAnalyzer;
14use MediaWiki\Page\WikiPageFactory;
15use MediaWiki\Registration\ExtensionRegistry;
16use MediaWiki\Revision\RevisionLookup;
17use MediaWiki\Title\Title;
18use MediaWiki\User\UserIdentity;
19
20class ArticleChangedJob extends Job {
21
22    private const COMMAND = 'ArticleChangedJob';
23
24    /** @var StoryContentAnalyzer */
25    private $analyzer;
26
27    /** @var RevisionLookup */
28    private $revisionLookup;
29
30    /** @var WikiPageFactory */
31    private $wikiPageFactory;
32
33    /** @var PageLinksSearch */
34    private $pageLinksSearch;
35
36    /** @var Config */
37    private $config;
38
39    /**
40     * @param string $command
41     * @param array $params
42     * @param RevisionLookup $revisionLookup
43     * @param StoryContentAnalyzer $analyzer
44     * @param WikiPageFactory $wikiPageFactory
45     * @param PageLinksSearch $pageLinksSearch
46     * @param Config $config
47     */
48    public function __construct(
49        $command,
50        $params,
51        RevisionLookup $revisionLookup,
52        StoryContentAnalyzer $analyzer,
53        WikiPageFactory $wikiPageFactory,
54        PageLinksSearch $pageLinksSearch,
55        Config $config
56    ) {
57        parent::__construct( self::COMMAND, $params );
58        // Delay to let multiple edits be deduplicated
59        $params[ 'jobReleaseTimestamp' ] = time() + 60;
60        $this->analyzer = $analyzer;
61        $this->revisionLookup = $revisionLookup;
62        $this->wikiPageFactory = $wikiPageFactory;
63        $this->pageLinksSearch = $pageLinksSearch;
64        $this->config = $config;
65    }
66
67    /**
68     * @param int $pageId
69     * @return IJobSpecification
70     */
71    public static function newSpec( int $pageId ): IJobSpecification {
72        return new JobSpecification(
73            self::COMMAND,
74            [ 'article_id' => $pageId ],
75            [ 'removeDuplicates' => true ]
76        );
77    }
78
79    /**
80     * Run the job
81     *
82     * @return bool Success
83     */
84    public function run() {
85        $notify = ExtensionRegistry::getInstance()->isLoaded( 'Echo' ) &&
86            $this->config->get( 'WikistoriesNotifyAboutStoryMaintenance' );
87        $articleId = $this->params[ 'article_id' ];
88        $rev = $this->revisionLookup->getRevisionByPageId( $articleId );
89        $agent = $rev->getUser();
90        $articleTitle = $rev->getPage()->getDBkey();
91        $pageIds = $this->pageLinksSearch->getPageLinks( $articleTitle, 99 );
92        foreach ( $pageIds as $pageId ) {
93            $page = $this->wikiPageFactory->newFromID( $pageId );
94            /** @var StoryContent $story */
95            $story = $page->getContent();
96            '@phan-var StoryContent $story';
97            if ( $notify && $this->analyzer->hasOutdatedText( $story ) ) {
98                $this->notify( $agent, $page->getTitle(), $articleTitle, $rev->getId() );
99            }
100            $page->doPurge();
101        }
102        return true;
103    }
104
105    /**
106     * @param UserIdentity $agent
107     * @param Title $storyTitle
108     * @param string $articleTitle
109     * @param int $revId
110     */
111    private function notify( UserIdentity $agent, Title $storyTitle, string $articleTitle, int $revId ) {
112        Event::create( [
113            'type' => EchoNotificationsHandlers::NOTIFICATION_TYPE,
114            'agent' => $agent,
115            'title' => $storyTitle,
116            'extra' => [
117                'articleTitle' => $articleTitle,
118                'articleRevId' => $revId,
119                'notifyAgent' => true,
120            ],
121        ] );
122    }
123}