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
;
12 use Wikimedia\Rdbms\DBAccessObjectUtils
;
13 use Wikimedia\Rdbms\IConnectionProvider
;
17 * Service for creating WikiPage objects.
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
31 $this->titleFactory
= $titleFactory;
32 $this->wikiPageFactoryHookRunner
= $wikiPageFactoryHookRunner;
33 $this->dbProvider
= $dbProvider;
37 * Create a WikiPage object from a title.
39 * @param PageIdentity $pageIdentity
42 public function newFromTitle( PageIdentity
$pageIdentity ): WikiPage
{
43 if ( $pageIdentity instanceof WikiPage
) {
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 );
62 if ( !$this->wikiPageFactoryHookRunner
->onWikiPageFactory( $title, $page ) ) {
68 $page = new WikiFilePage( $title );
71 $page = new WikiCategoryPage( $title );
74 $page = new WikiPage( $title );
81 * Create a WikiPage object from a link target.
83 * @param LinkTarget $title
86 public function newFromLinkTarget( LinkTarget
$title ): WikiPage
{
87 return $this->newFromTitle( $this->titleFactory
->newFromLinkTarget( $title ) );
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
101 public function newFromRow( $row, $from = 'fromdb' ) {
102 $page = $this->newFromTitle( $this->titleFactory
->newFromRow( $row ) );
103 $page->loadFromRow( $row, $from );
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
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__
)
132 return $this->newFromRow( $row, $from );