JeroenDeDauw /
EntityStore
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Queryr\EntityStore; |
||
| 4 | |||
| 5 | use Doctrine\DBAL\Connection; |
||
| 6 | use Doctrine\DBAL\DBALException; |
||
| 7 | use Queryr\EntityStore\Data\ItemInfo; |
||
| 8 | use Queryr\EntityStore\Data\ItemRow; |
||
| 9 | use Wikibase\DataModel\Entity\ItemId; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @licence GNU GPL v2+ |
||
| 13 | * @author Jeroen De Dauw < [email protected] > |
||
| 14 | */ |
||
| 15 | class ItemStore { |
||
| 16 | |||
| 17 | private $connection; |
||
| 18 | private $tableName; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * This constructor is package private. Construction is done via EntityStoreFactory. |
||
| 22 | * |
||
| 23 | * @param Connection $connection |
||
| 24 | * @param string $tableName |
||
| 25 | */ |
||
| 26 | 28 | public function __construct( Connection $connection, $tableName ) { |
|
| 27 | 28 | $this->connection = $connection; |
|
| 28 | 28 | $this->tableName = $tableName; |
|
| 29 | 28 | } |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @param ItemRow $itemRow |
||
| 33 | * |
||
| 34 | * @throws EntityStoreException |
||
| 35 | */ |
||
| 36 | 12 | public function storeItemRow( ItemRow $itemRow ) { |
|
| 37 | 12 | $this->deleteItemById( ItemId::newFromNumber( $itemRow->getNumericItemId() ) ); |
|
| 38 | |||
| 39 | try { |
||
| 40 | 10 | $this->connection->insert( |
|
| 41 | 10 | $this->tableName, |
|
| 42 | [ |
||
| 43 | 10 | 'item_id' => $itemRow->getNumericItemId(), |
|
| 44 | 10 | 'item_type' => $itemRow->getItemType(), |
|
| 45 | 10 | 'item_label_en' => $itemRow->getEnglishLabel(), |
|
| 46 | 10 | 'wp_title_en' => $itemRow->getEnglishWikipediaTitle(), |
|
| 47 | |||
| 48 | 10 | 'page_title' => $itemRow->getPageTitle(), |
|
| 49 | 10 | 'revision_id' => $itemRow->getRevisionId(), |
|
| 50 | 10 | 'revision_time' => $itemRow->getRevisionTime(), |
|
| 51 | |||
| 52 | 10 | 'item_json' => $itemRow->getItemJson(), |
|
| 53 | ] |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | catch ( DBALException $ex ) { |
||
| 57 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
||
| 58 | } |
||
| 59 | 10 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param ItemId $itemId |
||
| 63 | * |
||
| 64 | * @throws EntityStoreException |
||
| 65 | */ |
||
| 66 | 13 | public function deleteItemById( ItemId $itemId ) { |
|
| 67 | try { |
||
| 68 | 13 | $this->connection->delete( |
|
| 69 | 13 | $this->tableName, |
|
| 70 | [ |
||
| 71 | 13 | 'item_id' => $itemId->getNumericId() |
|
| 72 | ] |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | 3 | catch ( DBALException $ex ) { |
|
| 76 | 3 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
|
| 77 | } |
||
| 78 | 10 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param string|int $numericItemId |
||
| 82 | * @return ItemRow|null |
||
| 83 | * @throws EntityStoreException |
||
| 84 | */ |
||
| 85 | 6 | public function getItemRowByNumericItemId( $numericItemId ) { |
|
| 86 | try { |
||
| 87 | 6 | $rows = $this->selectItems() |
|
| 88 | 6 | ->where( 'item_id = ?' ) |
|
| 89 | 6 | ->setParameter( 0, (int)$numericItemId ) |
|
| 90 | 6 | ->execute(); |
|
| 91 | } |
||
| 92 | 2 | catch ( DBALException $ex ) { |
|
| 93 | 2 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
|
| 94 | } |
||
| 95 | |||
| 96 | 4 | return $this->newItemRowFromResult( $rows ); |
|
| 97 | } |
||
| 98 | |||
| 99 | 6 | private function selectItems() { |
|
| 100 | 6 | return $this->connection->createQueryBuilder()->select( |
|
| 101 | 6 | 'item_id', |
|
| 102 | 6 | 'item_json', |
|
| 103 | 6 | 'page_title', |
|
| 104 | 6 | 'revision_id', |
|
| 105 | 6 | 'revision_time', |
|
| 106 | 6 | 'item_type', |
|
| 107 | 6 | 'item_label_en', |
|
| 108 | 6 | 'wp_title_en' |
|
| 109 | 6 | )->from( $this->tableName ); |
|
| 110 | } |
||
| 111 | |||
| 112 | 4 | private function newItemRowFromResult( \Traversable $rows ) { |
|
| 113 | 4 | $rows = iterator_to_array( $rows ); |
|
| 114 | |||
| 115 | 4 | if ( count( $rows ) < 1 ) { |
|
| 116 | 2 | return null; |
|
| 117 | } |
||
| 118 | |||
| 119 | 2 | $row = reset( $rows ); |
|
| 120 | |||
| 121 | 2 | return ( new ItemRow() ) |
|
| 122 | 2 | ->setItemJson( $row['item_json'] ) |
|
| 123 | 2 | ->setNumericItemId( $row['item_id'] ) |
|
| 124 | 2 | ->setPageTitle( $row['page_title'] ) |
|
| 125 | 2 | ->setRevisionId( $row['revision_id'] ) |
|
| 126 | 2 | ->setRevisionTime( $row['revision_time'] ) |
|
| 127 | 2 | ->setItemType( $row['item_type'] ) |
|
| 128 | 2 | ->setEnglishWikipediaTitle( $row['wp_title_en'] ) |
|
| 129 | 2 | ->setEnglishLabel( $row['item_label_en'] ); |
|
| 130 | } |
||
| 131 | |||
| 132 | 4 | private function newItemInfoFromResultRow( array $row ) { |
|
| 133 | 4 | return ( new ItemInfo() ) |
|
| 134 | 4 | ->setNumericItemId( $row['item_id'] ) |
|
| 135 | 4 | ->setPageTitle( $row['page_title'] ) |
|
| 136 | 4 | ->setRevisionId( $row['revision_id'] ) |
|
| 137 | 4 | ->setRevisionTime( $row['revision_time'] ) |
|
| 138 | 4 | ->setItemType( $row['item_type'] ) |
|
| 139 | 4 | ->setEnglishWikipediaTitle( $row['wp_title_en'] ) |
|
| 140 | 4 | ->setEnglishLabel( $row['item_label_en'] ); |
|
| 141 | } |
||
| 142 | |||
| 143 | 8 | private function selectItemInfoSets() { |
|
| 144 | 8 | return $this->connection->createQueryBuilder()->select( |
|
| 145 | 8 | 'item_id', |
|
| 146 | 8 | 'page_title', |
|
| 147 | 8 | 'revision_id', |
|
| 148 | 8 | 'revision_time', |
|
| 149 | 8 | 'item_type', |
|
| 150 | 8 | 'item_label_en', |
|
| 151 | 8 | 'wp_title_en' |
|
| 152 | 8 | )->from( $this->tableName ); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param int $limit |
||
| 157 | * @param int $offset |
||
| 158 | * @param int|null $itemType |
||
| 159 | * |
||
| 160 | * @return ItemInfo[] |
||
| 161 | * @throws EntityStoreException |
||
| 162 | */ |
||
| 163 | 8 | public function getItemInfo( $limit, $offset, $itemType = null ) { |
|
| 164 | 8 | $query = $this->selectItemInfoSets() |
|
| 165 | 8 | ->orderBy( 'item_id', 'asc' ) |
|
| 166 | 8 | ->setMaxResults( $limit ) |
|
| 167 | 8 | ->setFirstResult( $offset ); |
|
| 168 | |||
| 169 | 8 | if ( is_int( $itemType ) ) { |
|
| 170 | 2 | $query->where( 'item_type = ?' )->setParameter( 0, $itemType ); |
|
| 171 | } |
||
| 172 | |||
| 173 | try { |
||
| 174 | 8 | $rows = $query->execute(); |
|
| 175 | } |
||
| 176 | 2 | catch ( DBALException $ex ) { |
|
| 177 | 2 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
|
| 178 | } |
||
| 179 | |||
| 180 | 6 | return $this->newItemInfoArrayFromResult( $rows ); |
|
|
0 ignored issues
–
show
|
|||
| 181 | } |
||
| 182 | |||
| 183 | 6 | private function newItemInfoArrayFromResult( \Traversable $rows ) { |
|
| 184 | 6 | $infoList = []; |
|
| 185 | |||
| 186 | 6 | foreach ( $rows as $resultRow ) { |
|
| 187 | 4 | $infoList[] = $this->newItemInfoFromResultRow( $resultRow ); |
|
| 188 | } |
||
| 189 | |||
| 190 | 6 | return $infoList; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int $limit |
||
| 195 | * @param int $offset |
||
| 196 | * |
||
| 197 | * @return int[] |
||
| 198 | * @throws EntityStoreException |
||
| 199 | */ |
||
| 200 | 3 | public function getItemTypes( $limit = 100, $offset = 0 ) { |
|
| 201 | try { |
||
| 202 | 3 | $rows = $this->connection->createQueryBuilder() |
|
| 203 | 3 | ->select( 'DISTINCT item_type' ) |
|
| 204 | 3 | ->from( $this->tableName ) |
|
| 205 | 3 | ->where( 'item_type IS NOT NULL' ) |
|
| 206 | 3 | ->orderBy( 'item_type', 'ASC' ) |
|
| 207 | 3 | ->setMaxResults( $limit ) |
|
| 208 | 3 | ->setFirstResult( $offset ) |
|
| 209 | 3 | ->execute(); |
|
| 210 | } |
||
| 211 | 1 | catch ( DBALException $ex ) { |
|
| 212 | 1 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
|
| 213 | } |
||
| 214 | |||
| 215 | 2 | return $this->getTypeArrayFromRows( $rows ); |
|
|
0 ignored issues
–
show
It seems like
$rows defined by $this->connection->creat...ult($offset)->execute() on line 202 can also be of type integer; however, Queryr\EntityStore\ItemS...:getTypeArrayFromRows() does only seem to accept object<Traversable>, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
|
|||
| 216 | } |
||
| 217 | |||
| 218 | 2 | private function getTypeArrayFromRows( \Traversable $rows ) { |
|
| 219 | 2 | $types = []; |
|
| 220 | |||
| 221 | 2 | foreach ( $rows as $row ) { |
|
| 222 | 1 | $types[] = (int)$row['item_type']; |
|
| 223 | } |
||
| 224 | |||
| 225 | 2 | return $types; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $pageName |
||
| 230 | * |
||
| 231 | * @return ItemId|null |
||
| 232 | * @throws EntityStoreException |
||
| 233 | */ |
||
| 234 | 3 | public function getIdForEnWikiPage( $pageName ) { |
|
| 235 | try { |
||
| 236 | 3 | $rows = $this->connection->createQueryBuilder() |
|
| 237 | 3 | ->select( 'item_id' ) |
|
| 238 | 3 | ->from( $this->tableName ) |
|
| 239 | 3 | ->where( 'wp_title_en = ?' ) |
|
| 240 | 3 | ->setParameter( 0, $pageName ) |
|
| 241 | 3 | ->setMaxResults( 1 ) |
|
| 242 | 3 | ->execute(); |
|
| 243 | } |
||
| 244 | 1 | catch ( DBALException $ex ) { |
|
| 245 | 1 | throw new EntityStoreException( $ex->getMessage(), $ex ); |
|
| 246 | } |
||
| 247 | |||
| 248 | 2 | foreach ( $rows as $row ) { |
|
|
0 ignored issues
–
show
The expression
$rows of type object<Doctrine\DBAL\Dri...esultStatement>|integer is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 249 | 1 | return ItemId::newFromNumber( $row['item_id'] ); |
|
| 250 | } |
||
| 251 | |||
| 252 | 1 | return null; |
|
| 253 | } |
||
| 254 | |||
| 255 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.