MediaWiki  master
PageSelectQueryBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Page;
4 
5 use Iterator;
6 use LinkCache;
7 use Wikimedia\Assert\Assert;
10 
14 class PageSelectQueryBuilder extends SelectQueryBuilder {
15 
17  private $pageStore;
18 
20  private $linkCache;
21 
29  public function __construct( IDatabase $db, PageStore $pageStore, ?LinkCache $linkCache = null ) {
30  parent::__construct( $db );
31  $this->pageStore = $pageStore;
32  $this->table( 'page' );
33  $this->linkCache = $linkCache;
34  }
35 
43  public function wherePageIds( $pageIds ): self {
44  Assert::parameterType( 'integer|array', $pageIds, '$pageIds' );
45 
46  if ( $pageIds ) {
47  $this->conds( [ 'page_id' => $pageIds ] );
48  } else {
49  $this->conds( '0 = 1' ); // force empty result set
50  }
51 
52  return $this;
53  }
54 
62  public function whereNamespace( int $namespace ): self {
63  $this->conds( [ 'page_namespace' => $namespace ] );
64  return $this;
65  }
66 
75  public function whereTitlePrefix( int $namespace, string $prefix ): self {
76  $this->whereNamespace( $namespace );
77  $this->conds( 'page_title ' . $this->db->buildLike( $prefix, $this->db->anyString() ) );
78  return $this;
79  }
80 
89  public function whereTitles( int $namespace, $pageTitles ): self {
90  Assert::parameterType( 'string|array', $pageTitles, '$pageTitles' );
91  $this->conds( [ 'page_namespace' => $namespace ] );
92  $this->conds( [ 'page_title' => $pageTitles ] );
93  return $this;
94  }
95 
103  public function orderByTitle( string $dir = self::SORT_ASC ): self {
104  $this->orderBy( [ 'page_namespace', 'page_title' ], $dir );
105  return $this;
106  }
107 
115  public function orderByPageId( string $dir = self::SORT_ASC ): self {
116  $this->orderBy( 'page_id', $dir );
117  return $this;
118  }
119 
125  public function fetchPageRecord(): ?PageRecord {
126  $this->fields( $this->pageStore->getSelectFields() );
127 
128  $row = $this->fetchRow();
129  if ( !$row ) {
130  return null;
131  }
132 
133  $rec = $this->pageStore->newPageRecordFromRow( $row );
134  if ( $this->linkCache ) {
135  $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
136  }
137  return $rec;
138  }
139 
145  public function fetchPageRecords(): Iterator {
146  $this->fields( $this->pageStore->getSelectFields() );
147 
148  return call_user_func( function () {
149  $result = $this->fetchResultSet();
150  foreach ( $result as $row ) {
151  $rec = $this->pageStore->newPageRecordFromRow( $row );
152  if ( $this->linkCache ) {
153  $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
154  }
155  yield $rec;
156  }
157  $result->free();
158  } );
159  }
160 
167  public function fetchPageRecordArray(): array {
168  $recs = [];
169 
170  foreach ( $this->fetchPageRecords() as $rec ) {
171  $recs[ $rec->getId() ] = $rec;
172  }
173 
174  return $recs;
175  }
176 
182  public function fetchPageIds(): array {
183  $this->field( 'page_id' );
184  return array_map( 'intval', $this->fetchFieldValues() );
185  }
186 
187 }
Page\PageSelectQueryBuilder\fetchPageRecord
fetchPageRecord()
Fetch a single PageRecord that matches specified criteria.
Definition: PageSelectQueryBuilder.php:125
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:40
Page\PageRecord
Data record representing a page that is (or used to be, or could be) an editable page on a wiki.
Definition: PageRecord.php:25
Page\PageSelectQueryBuilder\wherePageIds
wherePageIds( $pageIds)
Find by provided page ids.
Definition: PageSelectQueryBuilder.php:43
Wikimedia\Rdbms\SelectQueryBuilder\fetchResultSet
fetchResultSet()
Run the constructed SELECT query and return all results.
Definition: SelectQueryBuilder.php:628
Page\PageSelectQueryBuilder\fetchPageRecords
fetchPageRecords()
Fetch PageRecords for the specified query.
Definition: PageSelectQueryBuilder.php:145
Page\PageSelectQueryBuilder\orderByPageId
orderByPageId(string $dir=self::SORT_ASC)
Order results by page id.
Definition: PageSelectQueryBuilder.php:115
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
Page\PageSelectQueryBuilder\fetchPageRecordArray
fetchPageRecordArray()
Fetch PageRecords for the specified query as an associative array, using page IDs as array keys.
Definition: PageSelectQueryBuilder.php:167
Page\PageSelectQueryBuilder\$pageStore
PageStore $pageStore
Definition: PageSelectQueryBuilder.php:17
Page\PageSelectQueryBuilder\fetchPageIds
fetchPageIds()
Returns an array of page ids matching the query.
Definition: PageSelectQueryBuilder.php:182
Wikimedia\Rdbms\SelectQueryBuilder
Definition: SelectQueryBuilder.php:11
Page\PageSelectQueryBuilder\$linkCache
LinkCache null $linkCache
Definition: PageSelectQueryBuilder.php:20
Wikimedia\Rdbms\SelectQueryBuilder\fields
fields( $fields)
Add a field or an array of fields to the query.
Definition: SelectQueryBuilder.php:184
Wikimedia\Rdbms\SelectQueryBuilder\fetchRow
fetchRow()
Run the constructed SELECT query, and return the first result row.
Definition: SelectQueryBuilder.php:673
Wikimedia\Rdbms\SelectQueryBuilder\fetchFieldValues
fetchFieldValues()
Run the constructed SELECT query, and extract a single field from each result row,...
Definition: SelectQueryBuilder.php:657
Page\PageSelectQueryBuilder
Definition: PageSelectQueryBuilder.php:14
Page\PageSelectQueryBuilder\whereTitles
whereTitles(int $namespace, $pageTitles)
Find by provided page titles.
Definition: PageSelectQueryBuilder.php:89
Wikimedia\Rdbms\SelectQueryBuilder\conds
conds( $conds)
Add conditions to the query.
Definition: SelectQueryBuilder.php:291
Page\PageSelectQueryBuilder\orderByTitle
orderByTitle(string $dir=self::SORT_ASC)
Order results by namespace and title in $direction.
Definition: PageSelectQueryBuilder.php:103
Page\PageSelectQueryBuilder\__construct
__construct(IDatabase $db, PageStore $pageStore, ?LinkCache $linkCache=null)
Definition: PageSelectQueryBuilder.php:29
Page\PageSelectQueryBuilder\whereTitlePrefix
whereTitlePrefix(int $namespace, string $prefix)
Find by provided prefix.
Definition: PageSelectQueryBuilder.php:75
MediaWiki\Page
Definition: ContentModelChangeFactory.php:23
Page\PageStore
Definition: PageStore.php:28
Wikimedia\Rdbms\SelectQueryBuilder\field
field( $field, $alias=null)
Add a single field to the query, optionally with an alias.
Definition: SelectQueryBuilder.php:211
Wikimedia\Rdbms\SelectQueryBuilder\orderBy
orderBy( $fields, $direction=null)
Set the ORDER BY clause.
Definition: SelectQueryBuilder.php:431
Wikimedia\Rdbms\SelectQueryBuilder\$db
IDatabase $db
Definition: SelectQueryBuilder.php:45
Page\PageSelectQueryBuilder\whereNamespace
whereNamespace(int $namespace)
Find by provided namespace.
Definition: PageSelectQueryBuilder.php:62
Wikimedia\Rdbms\JoinGroupBase\table
table( $table, $alias=null)
Add a single table or a single parenthesized group.
Definition: JoinGroupBase.php:28