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 
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  $result = $this->fetchResultSet();
149  foreach ( $result as $row ) {
150  $rec = $this->pageStore->newPageRecordFromRow( $row );
151  if ( $this->linkCache ) {
152  $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
153  }
154  yield $rec;
155  }
156  $result->free();
157  }
158 
165  public function fetchPageRecordArray(): array {
166  $recs = [];
167 
168  foreach ( $this->fetchPageRecords() as $rec ) {
169  $recs[ $rec->getId() ] = $rec;
170  }
171 
172  return $recs;
173  }
174 
180  public function fetchPageIds(): array {
181  $this->field( 'page_id' );
182  return array_map( 'intval', $this->fetchFieldValues() );
183  }
184 
185 }
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:42
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.
__construct(IDatabase $db, PageStore $pageStore, ?LinkCache $linkCache=null)
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.
whereNamespace(int $namespace)
Find by provided namespace.
table( $table, $alias=null)
Add a single table or a single parenthesized group.
A query builder for 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.
Definition: PageRecord.php:24
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40