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 }
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:42
whereNamespace(int $namespace)
Find by provided namespace.
fetchPageRecord()
Fetch a single PageRecord that matches specified criteria.
fetchPageIds()
Returns an array of page ids matching the query.
wherePageIds( $pageIds)
Find by provided page ids.
__construct(IDatabase $db, PageStore $pageStore, ?LinkCache $linkCache=null)
fetchPageRecords()
Fetch PageRecords for the specified query.
whereTitles(int $namespace, $pageTitles)
Find by provided page titles.
orderByTitle(string $dir=self::SORT_ASC)
Order results by namespace and title in $direction.
whereTitlePrefix(int $namespace, string $prefix)
Find by provided prefix.
fetchPageRecordArray()
Fetch PageRecords for the specified query as an associative array, using page IDs as array keys.
orderByPageId(string $dir=self::SORT_ASC)
Order results by page id.
table( $table, $alias=null)
Add a single table or a single parenthesized group.
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.
Definition: PageRecord.php:23
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39