MediaWiki  master
WikiPageFactory.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Page;
4 
6 use InvalidArgumentException;
9 use stdClass;
10 use Title;
11 use TitleFactory;
13 use WikiFilePage;
15 use WikiPage;
16 
21 
23  private $titleFactory;
24 
27 
29  private $loadBalancer;
30 
36  public function __construct(
38  WikiPageFactoryHook $wikiPageFactoryHookRunner,
39  ILoadBalancer $loadBalancer
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  throw new InvalidArgumentException(
60  "The given PageIdentity does not represent a proper page"
61  );
62  }
63 
64  $ns = $pageIdentity->getNamespace();
65 
66  // TODO: remove the need for casting to Title. We'll have to create a new hook to
67  // replace the WikiPageFactory hook.
68  $title = Title::castFromPageIdentity( $pageIdentity );
69 
70  $page = null;
71  if ( !$this->wikiPageFactoryHookRunner->onWikiPageFactory( $title, $page ) ) {
72  return $page;
73  }
74 
75  switch ( $ns ) {
76  case NS_FILE:
77  $page = new WikiFilePage( $title );
78  break;
79  case NS_CATEGORY:
80  $page = new WikiCategoryPage( $title );
81  break;
82  default:
83  $page = new WikiPage( $title );
84  }
85 
86  return $page;
87  }
88 
96  public function newFromLinkTarget( LinkTarget $title ) {
97  return $this->newFromTitle( $this->titleFactory->newFromLinkTarget( $title ) );
98  }
99 
111  public function newFromRow( $row, $from = 'fromdb' ) {
112  $page = $this->newFromTitle( $this->titleFactory->newFromRow( $row ) );
113  $page->loadFromRow( $row, $from );
114  return $page;
115  }
116 
127  public function newFromID( $id, $from = 'fromdb' ) {
128  // page ids are never 0 or negative, see T63166
129  if ( $id < 1 ) {
130  return null;
131  }
132 
133  $from = WikiPage::convertSelectType( $from );
134  [ $index ] = DBAccessObjectUtils::getDBOptions( $from );
135  $db = $this->loadBalancer->getMaintenanceConnectionRef( $index );
136  $pageQuery = WikiPage::getQueryInfo();
137  $row = $db->selectRow(
138  $pageQuery['tables'], $pageQuery['fields'], [ 'page_id' => $id ], __METHOD__,
139  [], $pageQuery['joins']
140  );
141  if ( !$row ) {
142  return null;
143  }
144  return $this->newFromRow( $row, $from );
145  }
146 
147 }
Page\WikiPageFactory\$wikiPageFactoryHookRunner
WikiPageFactoryHook $wikiPageFactoryHookRunner
Definition: WikiPageFactory.php:26
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:65
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:61
Page\WikiPageFactory\newFromID
newFromID( $id, $from='fromdb')
Create a WikiPage object from a page ID.
Definition: WikiPageFactory.php:127
DBAccessObjectUtils\getDBOptions
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
Definition: DBAccessObjectUtils.php:52
Page\WikiPageFactory\newFromLinkTarget
newFromLinkTarget(LinkTarget $title)
Create a WikiPage object from a link target.
Definition: WikiPageFactory.php:96
Title\castFromPageIdentity
static castFromPageIdentity(?PageIdentity $pageIdentity)
Return a Title for a given PageIdentity.
Definition: Title.php:328
WikiCategoryPage
Special handling for category pages.
Definition: WikiCategoryPage.php:26
Page\WikiPageFactory
Definition: WikiPageFactory.php:20
$title
$title
Definition: testCompression.php:38
DBAccessObjectUtils
Helper class for DAO classes.
Definition: DBAccessObjectUtils.php:29
Page::Hook\WikiPageFactoryHook
This is a hook handler interface, see docs/Hooks.md.
Definition: WikiPageFactoryHook.php:15
Page\WikiPageFactory\$titleFactory
TitleFactory $titleFactory
Definition: WikiPageFactory.php:23
WikiPage\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new page object.
Definition: WikiPage.php:370
Title
Represents a title within MediaWiki.
Definition: Title.php:46
Page\WikiPageFactory\newFromTitle
newFromTitle(PageIdentity $pageIdentity)
Create a WikiPage object from a title.
Definition: WikiPageFactory.php:53
Page\WikiPageFactory\$loadBalancer
ILoadBalancer $loadBalancer
Definition: WikiPageFactory.php:29
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:34
Page\WikiPageFactory\__construct
__construct(TitleFactory $titleFactory, WikiPageFactoryHook $wikiPageFactoryHookRunner, ILoadBalancer $loadBalancer)
Definition: WikiPageFactory.php:36
WikiPage\convertSelectType
static convertSelectType( $type)
Convert 'fromdb', 'fromdbmaster' and 'forupdate' to READ_* constants.
Definition: WikiPage.php:233
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
MediaWiki\Page
Definition: ContentModelChangeFactory.php:23
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:31
NS_FILE
const NS_FILE
Definition: Defines.php:70
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Page\PageIdentity\canExist
canExist()
Checks whether this PageIdentity represents a "proper" page, meaning that it could exist as an editab...
Page\PageIdentity\getNamespace
getNamespace()
Returns the page's namespace number.
Page\WikiPageFactory\newFromRow
newFromRow( $row, $from='fromdb')
Create a WikiPage object from a database row.
Definition: WikiPageFactory.php:111
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81