MediaWiki REL1_37
WikiPageFactory.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Page;
4
6use InvalidArgumentException;
9use stdClass;
10use Title;
11use TitleFactory;
13use WikiFilePage;
15use WikiPage;
16
21
24
27
30
36 public function __construct(
40 ) {
41 $this->titleFactory = $titleFactory;
42 $this->wikiPageFactoryHookRunner = $wikiPageFactoryHookRunner;
43 $this->loadBalancer = $loadBalancer;
44 }
45
53 public function newFromTitle( PageIdentity $pageIdentity ) {
54 if ( $pageIdentity instanceof WikiPage ) {
55 return $pageIdentity;
56 }
57
58 if ( !$pageIdentity->canExist() ) {
59 // BC with the Title class
60 throw new InvalidArgumentException(
61 "The given PageIdentity does not represent a proper page"
62 );
63 }
64
65 $ns = $pageIdentity->getNamespace();
66
67 // TODO: remove the need for casting to Title. We'll have to create a new hook to
68 // replace the WikiPageFactory hook.
69 $title = Title::castFromPageIdentity( $pageIdentity );
70
71 $page = null;
72 if ( !$this->wikiPageFactoryHookRunner->onWikiPageFactory( $title, $page ) ) {
73 return $page;
74 }
75
76 switch ( $ns ) {
77 case NS_FILE:
78 $page = new WikiFilePage( $title );
79 break;
80 case NS_CATEGORY:
81 $page = new WikiCategoryPage( $title );
82 break;
83 default:
84 $page = new WikiPage( $title );
85 }
86
87 return $page;
88 }
89
97 public function newFromLinkTarget( LinkTarget $title ) {
98 return $this->newFromTitle( $this->titleFactory->newFromLinkTarget( $title ) );
99 }
100
112 public function newFromRow( $row, $from = 'fromdb' ) {
113 $page = $this->newFromTitle( $this->titleFactory->newFromRow( $row ) );
114 $page->loadFromRow( $row, $from );
115 return $page;
116 }
117
128 public function newFromID( $id, $from = 'fromdb' ) {
129 // page ids are never 0 or negative, see T63166
130 if ( $id < 1 ) {
131 return null;
132 }
133
134 $from = WikiPage::convertSelectType( $from );
135 [ $index ] = DBAccessObjectUtils::getDBOptions( $from );
136 $db = $this->loadBalancer->getMaintenanceConnectionRef( $index );
137 $pageQuery = WikiPage::getQueryInfo();
138 $row = $db->selectRow(
139 $pageQuery['tables'], $pageQuery['fields'], [ 'page_id' => $id ], __METHOD__,
140 [], $pageQuery['joins']
141 );
142 if ( !$row ) {
143 return null;
144 }
145 return $this->newFromRow( $row, $from );
146 }
147
148}
const NS_FILE
Definition Defines.php:70
const NS_CATEGORY
Definition Defines.php:78
Helper class for DAO classes.
__construct(TitleFactory $titleFactory, WikiPageFactoryHook $wikiPageFactoryHookRunner, ILoadBalancer $loadBalancer)
WikiPageFactoryHook $wikiPageFactoryHookRunner
newFromID( $id, $from='fromdb')
Create a WikiPage object from a page ID.
newFromTitle(PageIdentity $pageIdentity)
Create a WikiPage object from a title.
newFromRow( $row, $from='fromdb')
Create a WikiPage object from a database row.
newFromLinkTarget(LinkTarget $title)
Create a WikiPage object from a link target.
Creates Title objects.
Represents a title within MediaWiki.
Definition Title.php:48
Special handling for category pages.
Special handling for file pages.
Class representing a MediaWiki article and history.
Definition WikiPage.php:60
This is a hook handler interface, see docs/Hooks.md.
Interface for objects (potentially) representing an editable wiki page.
canExist()
Checks whether this PageIdentity represents a "proper" page, meaning that it could exist as an editab...
getNamespace()
Returns the page's namespace number.
Database cluster connection, tracking, load balancing, and transaction manager interface.