MediaWiki  master
WikiPageFactory.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Page;
4 
6 use InvalidArgumentException;
9 use Title;
10 use TitleFactory;
12 use WikiFilePage;
14 use WikiPage;
15 
20 
22  private $titleFactory;
23 
26 
28  private $loadBalancer;
29 
35  public function __construct(
37  WikiPageFactoryHook $wikiPageFactoryHookRunner,
38  ILoadBalancer $loadBalancer
39  ) {
40  $this->titleFactory = $titleFactory;
41  $this->wikiPageFactoryHookRunner = $wikiPageFactoryHookRunner;
42  $this->loadBalancer = $loadBalancer;
43  }
44 
52  public function newFromTitle( Title $title ) {
53  $ns = $title->getNamespace();
54 
55  if ( $ns == NS_MEDIA ) {
56  throw new InvalidArgumentException( "NS_MEDIA is a virtual namespace; use NS_FILE." );
57  } elseif ( $ns < 0 ) {
58  throw new InvalidArgumentException( "Invalid or virtual namespace $ns given." );
59  }
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 
87  public function newFromLinkTarget( LinkTarget $title ) {
88  return $this->newFromTitle( $this->titleFactory->newFromLinkTarget( $title ) );
89  }
90 
102  public function newFromRow( $row, $from = 'fromdb' ) {
103  $page = $this->newFromTitle( $this->titleFactory->newFromRow( $row ) );
104  $page->loadFromRow( $row, $from );
105  return $page;
106  }
107 
118  public function newFromID( $id, $from = 'fromdb' ) {
119  // page ids are never 0 or negative, see T63166
120  if ( $id < 1 ) {
121  return null;
122  }
123 
124  $from = WikiPage::convertSelectType( $from );
125  [ $index ] = DBAccessObjectUtils::getDBOptions( $from );
126  $db = $this->loadBalancer->getMaintenanceConnectionRef( $index );
127  $pageQuery = WikiPage::getQueryInfo();
128  $row = $db->selectRow(
129  $pageQuery['tables'], $pageQuery['fields'], [ 'page_id' => $id ], __METHOD__,
130  [], $pageQuery['joins']
131  );
132  if ( !$row ) {
133  return null;
134  }
135  return $this->newFromRow( $row, $from );
136  }
137 
138 }
Page\WikiPageFactory\$wikiPageFactoryHookRunner
WikiPageFactoryHook $wikiPageFactoryHookRunner
Definition: WikiPageFactory.php:25
Page\WikiPageFactory\newFromTitle
newFromTitle(Title $title)
Create a WikiPage object from a title.
Definition: WikiPageFactory.php:52
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:53
Page\WikiPageFactory\newFromID
newFromID( $id, $from='fromdb')
Create a WikiPage object from a page ID.
Definition: WikiPageFactory.php:118
NS_FILE
const NS_FILE
Definition: Defines.php:75
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:87
WikiCategoryPage
Special handling for category pages.
Definition: WikiCategoryPage.php:26
Page\WikiPageFactory
Definition: WikiPageFactory.php:19
$title
$title
Definition: testCompression.php:38
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:83
DBAccessObjectUtils
Helper class for DAO classes.
Definition: DBAccessObjectUtils.php:29
Page::Hook\WikiPageFactoryHook
Stable to implement.
Definition: WikiPageFactoryHook.php:12
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:57
Page\WikiPageFactory\$titleFactory
TitleFactory $titleFactory
Definition: WikiPageFactory.php:22
WikiPage\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new page object.
Definition: WikiPage.php:334
Title
Represents a title within MediaWiki.
Definition: Title.php:41
Page\WikiPageFactory\$loadBalancer
ILoadBalancer $loadBalancer
Definition: WikiPageFactory.php:28
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:33
Page\WikiPageFactory\__construct
__construct(TitleFactory $titleFactory, WikiPageFactoryHook $wikiPageFactoryHookRunner, ILoadBalancer $loadBalancer)
Definition: WikiPageFactory.php:35
WikiPage\convertSelectType
static convertSelectType( $type)
Convert 'fromdb', 'fromdbmaster' and 'forupdate' to READ_* constants.
Definition: WikiPage.php:198
MediaWiki\Page
Definition: ContentModelChangeFactory.php:23
WikiFilePage
Special handling for file pages.
Definition: WikiFilePage.php:31
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Page\WikiPageFactory\newFromRow
newFromRow( $row, $from='fromdb')
Create a WikiPage object from a database row.
Definition: WikiPageFactory.php:102
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81