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 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImportSource
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 11
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getScriptUser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTopics
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTopic
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 getPost
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getThreadData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getPageData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getFromPage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getApiKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getObjectKey
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Import\LiquidThreadsApi;
4
5use Flow\Import\IImportSource;
6use MediaWiki\User\User;
7
8class ImportSource implements IImportSource {
9    // Thread types defined by LQT which are returned via api
10    private const THREAD_TYPE_NORMAL = 0;
11    private const THREAD_TYPE_MOVED = 1;
12    private const THREAD_TYPE_DELETED = 2;
13    private const THREAD_TYPE_HIDDEN = 4;
14
15    /**
16     * @var ApiBackend
17     */
18    protected $api;
19
20    /**
21     * @var string
22     */
23    protected $pageName;
24
25    /**
26     * @var CachedThreadData
27     */
28    protected $threadData;
29
30    /**
31     * @var CachedPageData
32     */
33    protected $pageData;
34
35    /**
36     * @var int
37     */
38    protected $cachedTopics = 0;
39
40    /**
41     * @var User Used for scripted actions and occurances (such as suppression)
42     *  where the original user is not available.
43     */
44    protected $scriptUser;
45
46    /**
47     * @param ApiBackend $apiBackend
48     * @param string $pageName
49     * @param User $scriptUser
50     */
51    public function __construct( ApiBackend $apiBackend, $pageName, User $scriptUser ) {
52        $this->api = $apiBackend;
53        $this->pageName = $pageName;
54        $this->scriptUser = $scriptUser;
55
56        $this->threadData = new CachedThreadData( $this->api );
57        $this->pageData = new CachedPageData( $this->api );
58    }
59
60    /**
61     * Returns a system user suitable for assigning programatic actions to.
62     *
63     * @return User
64     */
65    public function getScriptUser() {
66        return $this->scriptUser;
67    }
68
69    /**
70     * @inheritDoc
71     */
72    public function getHeader() {
73        return new ImportHeader( $this->api, $this, $this->pageName );
74    }
75
76    /**
77     * @inheritDoc
78     */
79    public function getTopics() {
80        return new TopicIterator( $this, $this->threadData, $this->pageName );
81    }
82
83    /**
84     * @param int $id
85     * @return ImportTopic|null
86     */
87    public function getTopic( $id ) {
88        // reset our internal cached data every 100 topics. Otherwise imports
89        // of any considerable size will take up large amounts of memory for
90        // no reason, running into swap on smaller machines.
91        $this->cachedTopics++;
92        if ( $this->cachedTopics > 100 ) {
93            $this->threadData->reset();
94            $this->pageData->reset();
95            $this->cachedTopics = 0;
96        }
97
98        $data = $this->threadData->get( $id );
99        switch ( $data['type'] ) {
100            // Standard thread
101            case self::THREAD_TYPE_NORMAL:
102                return new ImportTopic( $this, $data );
103
104            // The topic no longer exists at the queried location, but
105            // a stub was left behind pointing to it. This modified
106            // version of ImportTopic gracefully adjusts the #REDIRECT
107            // into a template to keep a similar output to lqt.
108            case self::THREAD_TYPE_MOVED:
109                return new MovedImportTopic( $this, $data );
110
111            // To get these back from the api we would have to send the `showdeleted`
112            // query param.  As we are not requesting them, just ignore for now.
113            case self::THREAD_TYPE_DELETED:
114                return null;
115
116            // Was assigned but never used by LQT.
117            case self::THREAD_TYPE_HIDDEN:
118                return null;
119        }
120    }
121
122    /**
123     * @param int $id
124     * @return ImportPost
125     */
126    public function getPost( $id ) {
127        return new ImportPost( $this, $this->threadData->get( $id ) );
128    }
129
130    /**
131     * @param int $id
132     * @return array
133     */
134    public function getThreadData( $id ) {
135        if ( is_array( $id ) ) {
136            return $this->threadData->getMulti( $id );
137        } else {
138            return $this->threadData->get( $id );
139        }
140    }
141
142    /**
143     * @param int[]|int $pageIds
144     * @return array
145     */
146    public function getPageData( $pageIds ) {
147        if ( is_array( $pageIds ) ) {
148            return $this->pageData->getMulti( $pageIds );
149        } else {
150            return $this->pageData->get( $pageIds );
151        }
152    }
153
154    /**
155     * @param string $pageName
156     * @param int $startId
157     * @return array
158     */
159    public function getFromPage( $pageName, $startId = 0 ) {
160        return $this->threadData->getFromPage( $pageName, $startId );
161    }
162
163    /**
164     * Gets a unique identifier for the wiki being imported
165     * @return string Usually either a string 'local' or an API URL
166     */
167    public function getApiKey() {
168        return $this->api->getKey();
169    }
170
171    /**
172     * Returns a key uniquely representing an object determined by arguments.
173     * Parameters: Zero or more strings that uniquely represent the object
174     * for this ImportSource
175     *
176     * @return string Unique key
177     */
178    public function getObjectKey( /* $args */ ) {
179        $components = array_merge(
180            [ 'lqt-api', $this->getApiKey() ],
181            func_get_args()
182        );
183
184        return implode( ':', $components );
185    }
186}