[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorPasteQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $ids; 7 private $phids; 8 private $authorPHIDs; 9 private $parentPHIDs; 10 11 private $needContent; 12 private $needRawContent; 13 private $languages; 14 private $includeNoLanguage; 15 private $dateCreatedAfter; 16 private $dateCreatedBefore; 17 18 public function withIDs(array $ids) { 19 $this->ids = $ids; 20 return $this; 21 } 22 23 public function withPHIDs(array $phids) { 24 $this->phids = $phids; 25 return $this; 26 } 27 28 public function withAuthorPHIDs(array $phids) { 29 $this->authorPHIDs = $phids; 30 return $this; 31 } 32 33 public function withParentPHIDs(array $phids) { 34 $this->parentPHIDs = $phids; 35 return $this; 36 } 37 38 public function needContent($need_content) { 39 $this->needContent = $need_content; 40 return $this; 41 } 42 43 public function needRawContent($need_raw_content) { 44 $this->needRawContent = $need_raw_content; 45 return $this; 46 } 47 48 public function withLanguages(array $languages) { 49 $this->includeNoLanguage = false; 50 foreach ($languages as $key => $language) { 51 if ($language === null) { 52 $languages[$key] = ''; 53 continue; 54 } 55 } 56 $this->languages = $languages; 57 return $this; 58 } 59 60 public function withDateCreatedBefore($date_created_before) { 61 $this->dateCreatedBefore = $date_created_before; 62 return $this; 63 } 64 65 public function withDateCreatedAfter($date_created_after) { 66 $this->dateCreatedAfter = $date_created_after; 67 return $this; 68 } 69 70 protected function loadPage() { 71 $table = new PhabricatorPaste(); 72 $conn_r = $table->establishConnection('r'); 73 74 $data = queryfx_all( 75 $conn_r, 76 'SELECT paste.* FROM %T paste %Q %Q %Q', 77 $table->getTableName(), 78 $this->buildWhereClause($conn_r), 79 $this->buildOrderClause($conn_r), 80 $this->buildLimitClause($conn_r)); 81 82 $pastes = $table->loadAllFromArray($data); 83 84 return $pastes; 85 } 86 87 protected function didFilterPage(array $pastes) { 88 if ($this->needRawContent) { 89 $pastes = $this->loadRawContent($pastes); 90 } 91 92 if ($this->needContent) { 93 $pastes = $this->loadContent($pastes); 94 } 95 96 return $pastes; 97 } 98 99 protected function buildWhereClause($conn_r) { 100 $where = array(); 101 102 $where[] = $this->buildPagingClause($conn_r); 103 104 if ($this->ids) { 105 $where[] = qsprintf( 106 $conn_r, 107 'id IN (%Ld)', 108 $this->ids); 109 } 110 111 if ($this->phids) { 112 $where[] = qsprintf( 113 $conn_r, 114 'phid IN (%Ls)', 115 $this->phids); 116 } 117 118 if ($this->authorPHIDs) { 119 $where[] = qsprintf( 120 $conn_r, 121 'authorPHID IN (%Ls)', 122 $this->authorPHIDs); 123 } 124 125 if ($this->parentPHIDs) { 126 $where[] = qsprintf( 127 $conn_r, 128 'parentPHID IN (%Ls)', 129 $this->parentPHIDs); 130 } 131 132 if ($this->languages) { 133 $where[] = qsprintf( 134 $conn_r, 135 'language IN (%Ls)', 136 $this->languages); 137 } 138 139 if ($this->dateCreatedAfter) { 140 $where[] = qsprintf( 141 $conn_r, 142 'dateCreated >= %d', 143 $this->dateCreatedAfter); 144 } 145 146 if ($this->dateCreatedBefore) { 147 $where[] = qsprintf( 148 $conn_r, 149 'dateCreated <= %d', 150 $this->dateCreatedBefore); 151 } 152 153 return $this->formatWhereClause($where); 154 } 155 156 private function getContentCacheKey(PhabricatorPaste $paste) { 157 return implode( 158 ':', 159 array( 160 'P'.$paste->getID(), 161 $paste->getFilePHID(), 162 $paste->getLanguage(), 163 )); 164 } 165 166 private function loadRawContent(array $pastes) { 167 $file_phids = mpull($pastes, 'getFilePHID'); 168 $files = id(new PhabricatorFileQuery()) 169 ->setParentQuery($this) 170 ->setViewer($this->getViewer()) 171 ->withPHIDs($file_phids) 172 ->execute(); 173 $files = mpull($files, null, 'getPHID'); 174 175 foreach ($pastes as $key => $paste) { 176 $file = idx($files, $paste->getFilePHID()); 177 if (!$file) { 178 unset($pastes[$key]); 179 continue; 180 } 181 try { 182 $paste->attachRawContent($file->loadFileData()); 183 } catch (Exception $ex) { 184 // We can hit various sorts of file storage issues here. Just drop the 185 // paste if the file is dead. 186 unset($pastes[$key]); 187 continue; 188 } 189 } 190 191 return $pastes; 192 } 193 194 private function loadContent(array $pastes) { 195 $cache = new PhabricatorKeyValueDatabaseCache(); 196 197 $cache = new PhutilKeyValueCacheProfiler($cache); 198 $cache->setProfiler(PhutilServiceProfiler::getInstance()); 199 200 $keys = array(); 201 foreach ($pastes as $paste) { 202 $keys[] = $this->getContentCacheKey($paste); 203 } 204 205 $caches = $cache->getKeys($keys); 206 $results = array(); 207 208 $need_raw = array(); 209 foreach ($pastes as $key => $paste) { 210 $key = $this->getContentCacheKey($paste); 211 if (isset($caches[$key])) { 212 $paste->attachContent(phutil_safe_html($caches[$key])); 213 $results[$paste->getID()] = $paste; 214 } else { 215 $need_raw[$key] = $paste; 216 } 217 } 218 219 if (!$need_raw) { 220 return $results; 221 } 222 223 $write_data = array(); 224 225 $need_raw = $this->loadRawContent($need_raw); 226 foreach ($need_raw as $key => $paste) { 227 $content = $this->buildContent($paste); 228 $paste->attachContent($content); 229 230 $write_data[$this->getContentCacheKey($paste)] = (string)$content; 231 $results[$paste->getID()] = $paste; 232 } 233 234 $cache->setKeys($write_data); 235 236 return $results; 237 } 238 239 private function buildContent(PhabricatorPaste $paste) { 240 $language = $paste->getLanguage(); 241 $source = $paste->getRawContent(); 242 243 if (empty($language)) { 244 return PhabricatorSyntaxHighlighter::highlightWithFilename( 245 $paste->getTitle(), 246 $source); 247 } else { 248 return PhabricatorSyntaxHighlighter::highlightWithLanguage( 249 $language, 250 $source); 251 } 252 } 253 254 public function getQueryApplicationClass() { 255 return 'PhabricatorPasteApplication'; 256 } 257 258 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |