Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikiPageFactory
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 newFromTitle
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 newFromLinkTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newFromRow
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 newFromID
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Page;
4
5use MediaWiki\Linker\LinkTarget;
6use MediaWiki\Page\Hook\WikiPageFactoryHook;
7use MediaWiki\Title\Title;
8use MediaWiki\Title\TitleFactory;
9use stdClass;
10use WikiCategoryPage;
11use WikiFilePage;
12use Wikimedia\Rdbms\DBAccessObjectUtils;
13use Wikimedia\Rdbms\IConnectionProvider;
14use WikiPage;
15
16/**
17 * Service for creating WikiPage objects.
18 *
19 * @since 1.36
20 */
21class WikiPageFactory {
22    private TitleFactory $titleFactory;
23    private WikiPageFactoryHook $wikiPageFactoryHookRunner;
24    private IConnectionProvider $dbProvider;
25
26    public function __construct(
27        TitleFactory $titleFactory,
28        WikiPageFactoryHook $wikiPageFactoryHookRunner,
29        IConnectionProvider $dbProvider
30    ) {
31        $this->titleFactory = $titleFactory;
32        $this->wikiPageFactoryHookRunner = $wikiPageFactoryHookRunner;
33        $this->dbProvider = $dbProvider;
34    }
35
36    /**
37     * Create a WikiPage object from a title.
38     *
39     * @param PageIdentity $pageIdentity
40     * @return WikiPage
41     */
42    public function newFromTitle( PageIdentity $pageIdentity ): WikiPage {
43        if ( $pageIdentity instanceof WikiPage ) {
44            return $pageIdentity;
45        }
46
47        if ( !$pageIdentity->canExist() ) {
48            // BC with the Title class
49            throw new PageAssertionException(
50                'The given PageIdentity {pageIdentity} does not represent a proper page',
51                [ 'pageIdentity' => $pageIdentity ]
52            );
53        }
54
55        $ns = $pageIdentity->getNamespace();
56
57        // TODO: remove the need for casting to Title. We'll have to create a new hook to
58        //       replace the WikiPageFactory hook.
59        $title = Title::newFromPageIdentity( $pageIdentity );
60
61        $page = null;
62        if ( !$this->wikiPageFactoryHookRunner->onWikiPageFactory( $title, $page ) ) {
63            return $page;
64        }
65
66        switch ( $ns ) {
67            case NS_FILE:
68                $page = new WikiFilePage( $title );
69                break;
70            case NS_CATEGORY:
71                $page = new WikiCategoryPage( $title );
72                break;
73            default:
74                $page = new WikiPage( $title );
75        }
76
77        return $page;
78    }
79
80    /**
81     * Create a WikiPage object from a link target.
82     *
83     * @param LinkTarget $title
84     * @return WikiPage
85     */
86    public function newFromLinkTarget( LinkTarget $title ): WikiPage {
87        return $this->newFromTitle( $this->titleFactory->newFromLinkTarget( $title ) );
88    }
89
90    /**
91     * Create a WikiPage object from a database row
92     *
93     * @param stdClass $row Database row containing at least fields returned by getQueryInfo().
94     * @param string|int $from Source of $data:
95     *        - "fromdb" or IDBAccessObject::READ_NORMAL: from a replica DB
96     *        - "fromdbmaster" or IDBAccessObject::READ_LATEST: from the primary DB
97     *        - "forupdate" or IDBAccessObject::READ_LOCKING: from the primary DB using SELECT FOR UPDATE
98     *
99     * @return WikiPage
100     */
101    public function newFromRow( $row, $from = 'fromdb' ) {
102        $page = $this->newFromTitle( $this->titleFactory->newFromRow( $row ) );
103        $page->loadFromRow( $row, $from );
104        return $page;
105    }
106
107    /**
108     * Create a WikiPage object from a page ID
109     *
110     * @param int $id Article ID to load
111     * @param string|int $from One of the following values:
112     *        - "fromdb" or IDBAccessObject::READ_NORMAL to select from a replica DB
113     *        - "fromdbmaster" or IDBAccessObject::READ_LATEST to select from the primary database
114     *
115     * @return WikiPage|null Null when no page exists with that ID
116     */
117    public function newFromID( $id, $from = 'fromdb' ) {
118        // page ids are never 0 or negative, see T63166
119        if ( $id < 1 ) {
120            return null;
121        }
122        $db = DBAccessObjectUtils::getDBFromRecency( $this->dbProvider, WikiPage::convertSelectType( $from ) );
123        $pageQuery = WikiPage::getQueryInfo();
124        $row = $db->newSelectQueryBuilder()
125            ->queryInfo( $pageQuery )
126            ->where( [ 'page_id' => $id ] )
127            ->caller( __METHOD__ )
128            ->fetchRow();
129        if ( !$row ) {
130            return null;
131        }
132        return $this->newFromRow( $row, $from );
133    }
134
135}