MediaWiki master
PageSelectQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Page;
4
5use Iterator;
6use Wikimedia\Assert\Assert;
11
16
18 private $pageStore;
19
21 private $linkCache;
22
30 public function __construct( IReadableDatabase $db, PageStore $pageStore, ?LinkCache $linkCache = null ) {
31 parent::__construct( $db );
32 $this->pageStore = $pageStore;
33 $this->table( 'page' );
34 $this->linkCache = $linkCache;
35 }
36
44 public function wherePageIds( $pageIds ): self {
45 Assert::parameterType( [ 'integer', 'array' ], $pageIds, '$pageIds' );
46
47 if ( $pageIds ) {
48 $this->conds( [ 'page_id' => $pageIds ] );
49 } else {
50 $this->conds( '0 = 1' ); // force empty result set
51 }
52
53 return $this;
54 }
55
63 public function whereNamespace( int $namespace ): self {
64 $this->conds( [ 'page_namespace' => $namespace ] );
65 return $this;
66 }
67
76 public function whereTitlePrefix( int $namespace, string $prefix ): self {
77 $this->whereNamespace( $namespace );
78 $this->conds(
79 $this->db->expr( 'page_title', IExpression::LIKE, new LikeValue( $prefix, $this->db->anyString() ) )
80 );
81 return $this;
82 }
83
92 public function whereTitles( int $namespace, $pageTitles ): self {
93 Assert::parameterType( [ 'string', 'array' ], $pageTitles, '$pageTitles' );
94 $this->conds( [ 'page_namespace' => $namespace ] );
95 $this->conds( [ 'page_title' => $pageTitles ] );
96 return $this;
97 }
98
106 public function orderByTitle( string $dir = self::SORT_ASC ): self {
107 $this->orderBy( [ 'page_namespace', 'page_title' ], $dir );
108 return $this;
109 }
110
118 public function orderByPageId( string $dir = self::SORT_ASC ): self {
119 $this->orderBy( 'page_id', $dir );
120 return $this;
121 }
122
128 public function fetchPageRecord(): ?PageRecord {
129 $this->fields( $this->pageStore->getSelectFields() );
130
131 $row = $this->fetchRow();
132 if ( !$row ) {
133 return null;
134 }
135
136 $rec = $this->pageStore->newPageRecordFromRow( $row );
137 if ( $this->linkCache ) {
138 $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
139 }
140 return $rec;
141 }
142
148 public function fetchPageRecords(): Iterator {
149 $this->fields( $this->pageStore->getSelectFields() );
150
151 $result = $this->fetchResultSet();
152 foreach ( $result as $row ) {
153 $rec = $this->pageStore->newPageRecordFromRow( $row );
154 if ( $this->linkCache ) {
155 $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
156 }
157 yield $rec;
158 }
159 $result->free();
160 }
161
168 public function fetchPageRecordArray(): array {
169 $recs = [];
170
171 foreach ( $this->fetchPageRecords() as $rec ) {
172 $recs[ $rec->getId() ] = $rec;
173 }
174
175 return $recs;
176 }
177
183 public function fetchPageIds(): array {
184 $this->field( 'page_id' );
185 return array_map( 'intval', $this->fetchFieldValues() );
186 }
187
188}
Page existence and metadata cache.
Definition LinkCache.php:54
whereTitlePrefix(int $namespace, string $prefix)
Find by provided prefix.
fetchPageRecords()
Fetch PageRecords for the specified query.
wherePageIds( $pageIds)
Find by provided page ids.
whereTitles(int $namespace, $pageTitles)
Find by provided page titles.
fetchPageRecord()
Fetch a single PageRecord that matches specified criteria.
orderByPageId(string $dir=self::SORT_ASC)
Order results by page id.
fetchPageIds()
Returns an array of page ids matching the query.
orderByTitle(string $dir=self::SORT_ASC)
Order results by namespace and title in $direction.
fetchPageRecordArray()
Fetch PageRecords for the specified query as an associative array, using page IDs as array keys.
__construct(IReadableDatabase $db, PageStore $pageStore, ?LinkCache $linkCache=null)
whereNamespace(int $namespace)
Find by provided namespace.
table( $table, $alias=null)
Add a single table or a single parenthesized group.
Content of like value.
Definition LikeValue.php:14
Build SELECT queries with a fluent interface.
conds( $conds)
Add conditions to the query.
Data record representing a page that is (or used to be, or could be) an editable page on a wiki.
A database connection without write operations.