MediaWiki master
PageSelectQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Page;
4
5use Iterator;
7use Wikimedia\Assert\Assert;
12
17
19 private $pageStore;
20
22 private $linkCache;
23
31 public function __construct( IReadableDatabase $db, PageStore $pageStore, ?LinkCache $linkCache = null ) {
32 parent::__construct( $db );
33 $this->pageStore = $pageStore;
34 $this->table( 'page' );
35 $this->linkCache = $linkCache;
36 }
37
45 public function wherePageIds( $pageIds ): self {
46 Assert::parameterType( [ 'integer', 'array' ], $pageIds, '$pageIds' );
47
48 if ( $pageIds ) {
49 $this->conds( [ 'page_id' => $pageIds ] );
50 } else {
51 $this->conds( '0 = 1' ); // force empty result set
52 }
53
54 return $this;
55 }
56
64 public function whereNamespace( int $namespace ): self {
65 $this->conds( [ 'page_namespace' => $namespace ] );
66 return $this;
67 }
68
77 public function whereTitlePrefix( int $namespace, string $prefix ): self {
78 $this->whereNamespace( $namespace );
79 $this->conds(
80 $this->db->expr( 'page_title', IExpression::LIKE, new LikeValue( $prefix, $this->db->anyString() ) )
81 );
82 return $this;
83 }
84
93 public function whereTitles( int $namespace, $pageTitles ): self {
94 Assert::parameterType( [ 'string', 'array' ], $pageTitles, '$pageTitles' );
95 $this->conds( [ 'page_namespace' => $namespace ] );
96 $this->conds( [ 'page_title' => $pageTitles ] );
97 return $this;
98 }
99
107 public function orderByTitle( string $dir = self::SORT_ASC ): self {
108 $this->orderBy( [ 'page_namespace', 'page_title' ], $dir );
109 return $this;
110 }
111
119 public function orderByPageId( string $dir = self::SORT_ASC ): self {
120 $this->orderBy( 'page_id', $dir );
121 return $this;
122 }
123
129 public function fetchPageRecord(): ?PageRecord {
130 $this->fields( $this->pageStore->getSelectFields() );
131
132 $row = $this->fetchRow();
133 if ( !$row ) {
134 return null;
135 }
136
137 $rec = $this->pageStore->newPageRecordFromRow( $row );
138 if ( $this->linkCache ) {
139 $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
140 }
141 return $rec;
142 }
143
149 public function fetchPageRecords(): Iterator {
150 $this->fields( $this->pageStore->getSelectFields() );
151
152 $result = $this->fetchResultSet();
153 foreach ( $result as $row ) {
154 $rec = $this->pageStore->newPageRecordFromRow( $row );
155 if ( $this->linkCache ) {
156 $this->linkCache->addGoodLinkObjFromRow( $rec, $row );
157 }
158 yield $rec;
159 }
160 $result->free();
161 }
162
169 public function fetchPageRecordArray(): array {
170 $recs = [];
171
172 foreach ( $this->fetchPageRecords() as $rec ) {
173 $recs[ $rec->getId() ] = $rec;
174 }
175
176 return $recs;
177 }
178
184 public function fetchPageIds(): array {
185 $this->field( 'page_id' );
186 return array_map( 'intval', $this->fetchFieldValues() );
187 }
188
189}
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:53
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.
IReadableDatabase IReadableDatabase $db
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.