Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / includes / page / WikiPageFactory.php
blobc04a835c12371820167a8c7061b4886c1a8f32ac
1 <?php
3 namespace MediaWiki\Page;
5 use MediaWiki\Linker\LinkTarget;
6 use MediaWiki\Page\Hook\WikiPageFactoryHook;
7 use MediaWiki\Title\Title;
8 use MediaWiki\Title\TitleFactory;
9 use stdClass;
10 use WikiCategoryPage;
11 use WikiFilePage;
12 use Wikimedia\Rdbms\DBAccessObjectUtils;
13 use Wikimedia\Rdbms\IConnectionProvider;
14 use WikiPage;
16 /**
17 * Service for creating WikiPage objects.
19 * @since 1.36
21 class WikiPageFactory {
22 private TitleFactory $titleFactory;
23 private WikiPageFactoryHook $wikiPageFactoryHookRunner;
24 private IConnectionProvider $dbProvider;
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;
36 /**
37 * Create a WikiPage object from a title.
39 * @param PageIdentity $pageIdentity
40 * @return WikiPage
42 public function newFromTitle( PageIdentity $pageIdentity ): WikiPage {
43 if ( $pageIdentity instanceof WikiPage ) {
44 return $pageIdentity;
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 ]
55 $ns = $pageIdentity->getNamespace();
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 );
61 $page = null;
62 if ( !$this->wikiPageFactoryHookRunner->onWikiPageFactory( $title, $page ) ) {
63 return $page;
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 );
77 return $page;
80 /**
81 * Create a WikiPage object from a link target.
83 * @param LinkTarget $title
84 * @return WikiPage
86 public function newFromLinkTarget( LinkTarget $title ): WikiPage {
87 return $this->newFromTitle( $this->titleFactory->newFromLinkTarget( $title ) );
90 /**
91 * Create a WikiPage object from a database row
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
99 * @return WikiPage
101 public function newFromRow( $row, $from = 'fromdb' ) {
102 $page = $this->newFromTitle( $this->titleFactory->newFromRow( $row ) );
103 $page->loadFromRow( $row, $from );
104 return $page;
108 * Create a WikiPage object from a page ID
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
115 * @return WikiPage|null Null when no page exists with that ID
117 public function newFromID( $id, $from = 'fromdb' ) {
118 // page ids are never 0 or negative, see T63166
119 if ( $id < 1 ) {
120 return null;
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;
132 return $this->newFromRow( $row, $from );