Complex classes like Doku_Indexer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Doku_Indexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 109 | class Doku_Indexer { |
||
| 110 | /** |
||
| 111 | * @var array $pidCache Cache for getPID() |
||
| 112 | */ |
||
| 113 | protected $pidCache = array(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Adds the contents of a page to the fulltext index |
||
| 117 | * |
||
| 118 | * The added text replaces previous words for the same page. |
||
| 119 | * An empty value erases the page. |
||
| 120 | * |
||
| 121 | * @param string $page a page name |
||
| 122 | * @param string $text the body of the page |
||
| 123 | * @return string|boolean the function completed successfully |
||
| 124 | * |
||
| 125 | * @author Tom N Harris <[email protected]> |
||
| 126 | * @author Andreas Gohr <[email protected]> |
||
| 127 | */ |
||
| 128 | public function addPageWords($page, $text) { |
||
| 129 | if (!$this->lock()) |
||
| 130 | return "locked"; |
||
| 131 | |||
| 132 | // load known documents |
||
| 133 | $pid = $this->getPIDNoLock($page); |
||
| 134 | if ($pid === false) { |
||
| 135 | $this->unlock(); |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | $pagewords = array(); |
||
| 140 | // get word usage in page |
||
| 141 | $words = $this->getPageWords($text); |
||
| 142 | if ($words === false) { |
||
| 143 | $this->unlock(); |
||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!empty($words)) { |
||
| 148 | foreach (array_keys($words) as $wlen) { |
||
| 149 | $index = $this->getIndex('i', $wlen); |
||
| 150 | foreach ($words[$wlen] as $wid => $freq) { |
||
| 151 | $idx = ($wid<count($index)) ? $index[$wid] : ''; |
||
| 152 | $index[$wid] = $this->updateTuple($idx, $pid, $freq); |
||
|
|
|||
| 153 | $pagewords[] = "$wlen*$wid"; |
||
| 154 | } |
||
| 155 | if (!$this->saveIndex('i', $wlen, $index)) { |
||
| 156 | $this->unlock(); |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // Remove obsolete index entries |
||
| 163 | $pageword_idx = $this->getIndexKey('pageword', '', $pid); |
||
| 164 | if ($pageword_idx !== '') { |
||
| 165 | $oldwords = explode(':',$pageword_idx); |
||
| 166 | $delwords = array_diff($oldwords, $pagewords); |
||
| 167 | $upwords = array(); |
||
| 168 | foreach ($delwords as $word) { |
||
| 169 | if ($word != '') { |
||
| 170 | list($wlen,$wid) = explode('*', $word); |
||
| 171 | $wid = (int)$wid; |
||
| 172 | $upwords[$wlen][] = $wid; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | foreach ($upwords as $wlen => $widx) { |
||
| 176 | $index = $this->getIndex('i', $wlen); |
||
| 177 | foreach ($widx as $wid) { |
||
| 178 | $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); |
||
| 179 | } |
||
| 180 | $this->saveIndex('i', $wlen, $index); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | // Save the reverse index |
||
| 184 | $pageword_idx = join(':', $pagewords); |
||
| 185 | if (!$this->saveIndexKey('pageword', '', $pid, $pageword_idx)) { |
||
| 186 | $this->unlock(); |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->unlock(); |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Split the words in a page and add them to the index. |
||
| 196 | * |
||
| 197 | * @param string $text content of the page |
||
| 198 | * @return array list of word IDs and number of times used |
||
| 199 | * |
||
| 200 | * @author Andreas Gohr <[email protected]> |
||
| 201 | * @author Christopher Smith <[email protected]> |
||
| 202 | * @author Tom N Harris <[email protected]> |
||
| 203 | */ |
||
| 204 | protected function getPageWords($text) { |
||
| 205 | |||
| 206 | $tokens = $this->tokenizer($text); |
||
| 207 | $tokens = array_count_values($tokens); // count the frequency of each token |
||
| 208 | |||
| 209 | $words = array(); |
||
| 210 | foreach ($tokens as $w=>$c) { |
||
| 211 | $l = wordlen($w); |
||
| 212 | if (isset($words[$l])){ |
||
| 213 | $words[$l][$w] = $c + (isset($words[$l][$w]) ? $words[$l][$w] : 0); |
||
| 214 | }else{ |
||
| 215 | $words[$l] = array($w => $c); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | // arrive here with $words = array(wordlen => array(word => frequency)) |
||
| 220 | $word_idx_modified = false; |
||
| 221 | $index = array(); //resulting index |
||
| 222 | foreach (array_keys($words) as $wlen) { |
||
| 223 | $word_idx = $this->getIndex('w', $wlen); |
||
| 224 | foreach ($words[$wlen] as $word => $freq) { |
||
| 225 | $word = (string)$word; |
||
| 226 | $wid = array_search($word, $word_idx, true); |
||
| 227 | if ($wid === false) { |
||
| 228 | $wid = count($word_idx); |
||
| 229 | $word_idx[] = $word; |
||
| 230 | $word_idx_modified = true; |
||
| 231 | } |
||
| 232 | if (!isset($index[$wlen])) |
||
| 233 | $index[$wlen] = array(); |
||
| 234 | $index[$wlen][$wid] = $freq; |
||
| 235 | } |
||
| 236 | // save back the word index |
||
| 237 | if ($word_idx_modified && !$this->saveIndex('w', $wlen, $word_idx)) |
||
| 238 | return false; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $index; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Add/update keys to/of the metadata index. |
||
| 246 | * |
||
| 247 | * Adding new keys does not remove other keys for the page. |
||
| 248 | * An empty value will erase the key. |
||
| 249 | * The $key parameter can be an array to add multiple keys. $value will |
||
| 250 | * not be used if $key is an array. |
||
| 251 | * |
||
| 252 | * @param string $page a page name |
||
| 253 | * @param mixed $key a key string or array of key=>value pairs |
||
| 254 | * @param mixed $value the value or list of values |
||
| 255 | * @return boolean|string the function completed successfully |
||
| 256 | * |
||
| 257 | * @author Tom N Harris <[email protected]> |
||
| 258 | * @author Michael Hamann <[email protected]> |
||
| 259 | */ |
||
| 260 | public function addMetaKeys($page, $key, $value=null) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Rename a page in the search index without changing the indexed content. This function doesn't check if the |
||
| 357 | * old or new name exists in the filesystem. It returns an error if the old page isn't in the page list of the |
||
| 358 | * indexer and it deletes all previously indexed content of the new page. |
||
| 359 | * |
||
| 360 | * @param string $oldpage The old page name |
||
| 361 | * @param string $newpage The new page name |
||
| 362 | * @return string|bool If the page was successfully renamed, can be a message in the case of an error |
||
| 363 | */ |
||
| 364 | public function renamePage($oldpage, $newpage) { |
||
| 365 | if (!$this->lock()) return 'locked'; |
||
| 366 | |||
| 367 | $pages = $this->getPages(); |
||
| 368 | |||
| 369 | $id = array_search($oldpage, $pages, true); |
||
| 370 | if ($id === false) { |
||
| 371 | $this->unlock(); |
||
| 372 | return 'page is not in index'; |
||
| 373 | } |
||
| 374 | |||
| 375 | $new_id = array_search($newpage, $pages, true); |
||
| 376 | if ($new_id !== false) { |
||
| 377 | // make sure the page is not in the index anymore |
||
| 378 | if ($this->deletePageNoLock($newpage) !== true) { |
||
| 379 | return false; |
||
| 380 | } |
||
| 381 | |||
| 382 | $pages[$new_id] = 'deleted:'.time().rand(0, 9999); |
||
| 383 | } |
||
| 384 | |||
| 385 | $pages[$id] = $newpage; |
||
| 386 | |||
| 387 | // update index |
||
| 388 | if (!$this->saveIndex('page', '', $pages)) { |
||
| 389 | $this->unlock(); |
||
| 390 | return false; |
||
| 391 | } |
||
| 392 | |||
| 393 | // reset the pid cache |
||
| 394 | $this->pidCache = array(); |
||
| 395 | |||
| 396 | $this->unlock(); |
||
| 397 | return true; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Renames a meta value in the index. This doesn't change the meta value in the pages, it assumes that all pages |
||
| 402 | * will be updated. |
||
| 403 | * |
||
| 404 | * @param string $key The metadata key of which a value shall be changed |
||
| 405 | * @param string $oldvalue The old value that shall be renamed |
||
| 406 | * @param string $newvalue The new value to which the old value shall be renamed, can exist (then values will be merged) |
||
| 407 | * @return bool|string If renaming the value has been successful, false or error message on error. |
||
| 408 | */ |
||
| 409 | public function renameMetaValue($key, $oldvalue, $newvalue) { |
||
| 410 | if (!$this->lock()) return 'locked'; |
||
| 411 | |||
| 412 | // change the relation references index |
||
| 413 | $metavalues = $this->getIndex($key, '_w'); |
||
| 414 | $oldid = array_search($oldvalue, $metavalues, true); |
||
| 415 | if ($oldid !== false) { |
||
| 416 | $newid = array_search($newvalue, $metavalues, true); |
||
| 417 | if ($newid !== false) { |
||
| 418 | // free memory |
||
| 419 | unset ($metavalues); |
||
| 420 | |||
| 421 | // okay, now we have two entries for the same value. we need to merge them. |
||
| 422 | $indexline = $this->getIndexKey($key.'_i', '', $oldid); |
||
| 423 | if ($indexline != '') { |
||
| 424 | $newindexline = $this->getIndexKey($key.'_i', '', $newid); |
||
| 425 | $pagekeys = $this->getIndex($key.'_p', ''); |
||
| 426 | $parts = explode(':', $indexline); |
||
| 427 | foreach ($parts as $part) { |
||
| 428 | list($id, $count) = explode('*', $part); |
||
| 429 | $newindexline = $this->updateTuple($newindexline, $id, $count); |
||
| 430 | |||
| 431 | $keyline = explode(':', $pagekeys[$id]); |
||
| 432 | // remove old meta value |
||
| 433 | $keyline = array_diff($keyline, array($oldid)); |
||
| 434 | // add new meta value when not already present |
||
| 435 | if (!in_array($newid, $keyline)) { |
||
| 436 | array_push($keyline, $newid); |
||
| 437 | } |
||
| 438 | $pagekeys[$id] = implode(':', $keyline); |
||
| 439 | } |
||
| 440 | $this->saveIndex($key.'_p', '', $pagekeys); |
||
| 441 | unset($pagekeys); |
||
| 442 | $this->saveIndexKey($key.'_i', '', $oldid, ''); |
||
| 443 | $this->saveIndexKey($key.'_i', '', $newid, $newindexline); |
||
| 444 | } |
||
| 445 | } else { |
||
| 446 | $metavalues[$oldid] = $newvalue; |
||
| 447 | if (!$this->saveIndex($key.'_w', '', $metavalues)) { |
||
| 448 | $this->unlock(); |
||
| 449 | return false; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | $this->unlock(); |
||
| 455 | return true; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Remove a page from the index |
||
| 460 | * |
||
| 461 | * Erases entries in all known indexes. |
||
| 462 | * |
||
| 463 | * @param string $page a page name |
||
| 464 | * @return string|boolean the function completed successfully |
||
| 465 | * |
||
| 466 | * @author Tom N Harris <[email protected]> |
||
| 467 | */ |
||
| 468 | public function deletePage($page) { |
||
| 469 | if (!$this->lock()) |
||
| 470 | return "locked"; |
||
| 471 | |||
| 472 | $result = $this->deletePageNoLock($page); |
||
| 473 | |||
| 474 | $this->unlock(); |
||
| 475 | |||
| 476 | return $result; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Remove a page from the index without locking the index, only use this function if the index is already locked |
||
| 481 | * |
||
| 482 | * Erases entries in all known indexes. |
||
| 483 | * |
||
| 484 | * @param string $page a page name |
||
| 485 | * @return boolean the function completed successfully |
||
| 486 | * |
||
| 487 | * @author Tom N Harris <[email protected]> |
||
| 488 | */ |
||
| 489 | protected function deletePageNoLock($page) { |
||
| 490 | // load known documents |
||
| 491 | $pid = $this->getPIDNoLock($page); |
||
| 492 | if ($pid === false) { |
||
| 493 | return false; |
||
| 494 | } |
||
| 495 | |||
| 496 | // Remove obsolete index entries |
||
| 497 | $pageword_idx = $this->getIndexKey('pageword', '', $pid); |
||
| 498 | if ($pageword_idx !== '') { |
||
| 499 | $delwords = explode(':',$pageword_idx); |
||
| 500 | $upwords = array(); |
||
| 501 | foreach ($delwords as $word) { |
||
| 502 | if ($word != '') { |
||
| 503 | list($wlen,$wid) = explode('*', $word); |
||
| 504 | $wid = (int)$wid; |
||
| 505 | $upwords[$wlen][] = $wid; |
||
| 506 | } |
||
| 507 | } |
||
| 508 | foreach ($upwords as $wlen => $widx) { |
||
| 509 | $index = $this->getIndex('i', $wlen); |
||
| 510 | foreach ($widx as $wid) { |
||
| 511 | $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); |
||
| 512 | } |
||
| 513 | $this->saveIndex('i', $wlen, $index); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | // Save the reverse index |
||
| 517 | if (!$this->saveIndexKey('pageword', '', $pid, "")) { |
||
| 518 | return false; |
||
| 519 | } |
||
| 520 | |||
| 521 | $this->saveIndexKey('title', '', $pid, ""); |
||
| 522 | $keyidx = $this->getIndex('metadata', ''); |
||
| 523 | foreach ($keyidx as $metaname) { |
||
| 524 | $val_idx = explode(':', $this->getIndexKey($metaname.'_p', '', $pid)); |
||
| 525 | $meta_idx = $this->getIndex($metaname.'_i', ''); |
||
| 526 | foreach ($val_idx as $id) { |
||
| 527 | if ($id === '') continue; |
||
| 528 | $meta_idx[$id] = $this->updateTuple($meta_idx[$id], $pid, 0); |
||
| 529 | } |
||
| 530 | $this->saveIndex($metaname.'_i', '', $meta_idx); |
||
| 531 | $this->saveIndexKey($metaname.'_p', '', $pid, ''); |
||
| 532 | } |
||
| 533 | |||
| 534 | return true; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Clear the whole index |
||
| 539 | * |
||
| 540 | * @return bool If the index has been cleared successfully |
||
| 541 | */ |
||
| 542 | public function clear() { |
||
| 543 | global $conf; |
||
| 544 | |||
| 545 | if (!$this->lock()) return false; |
||
| 546 | |||
| 547 | @unlink($conf['indexdir'].'/page.idx'); |
||
|
1 ignored issue
–
show
|
|||
| 548 | @unlink($conf['indexdir'].'/title.idx'); |
||
|
1 ignored issue
–
show
|
|||
| 549 | @unlink($conf['indexdir'].'/pageword.idx'); |
||
|
1 ignored issue
–
show
|
|||
| 550 | @unlink($conf['indexdir'].'/metadata.idx'); |
||
|
1 ignored issue
–
show
|
|||
| 551 | $dir = @opendir($conf['indexdir']); |
||
| 552 | if($dir!==false){ |
||
| 553 | while(($f = readdir($dir)) !== false){ |
||
| 554 | if(substr($f,-4)=='.idx' && |
||
| 555 | (substr($f,0,1)=='i' || substr($f,0,1)=='w' |
||
| 556 | || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx')) |
||
| 557 | @unlink($conf['indexdir']."/$f"); |
||
|
1 ignored issue
–
show
|
|||
| 558 | } |
||
| 559 | } |
||
| 560 | @unlink($conf['indexdir'].'/lengths.idx'); |
||
|
1 ignored issue
–
show
|
|||
| 561 | |||
| 562 | // clear the pid cache |
||
| 563 | $this->pidCache = array(); |
||
| 564 | |||
| 565 | $this->unlock(); |
||
| 566 | return true; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Split the text into words for fulltext search |
||
| 571 | * |
||
| 572 | * TODO: does this also need &$stopwords ? |
||
| 573 | * |
||
| 574 | * @triggers INDEXER_TEXT_PREPARE |
||
| 575 | * This event allows plugins to modify the text before it gets tokenized. |
||
| 576 | * Plugins intercepting this event should also intercept INDEX_VERSION_GET |
||
| 577 | * |
||
| 578 | * @param string $text plain text |
||
| 579 | * @param boolean $wc are wildcards allowed? |
||
| 580 | * @return array list of words in the text |
||
| 581 | * |
||
| 582 | * @author Tom N Harris <[email protected]> |
||
| 583 | * @author Andreas Gohr <[email protected]> |
||
| 584 | */ |
||
| 585 | public function tokenizer($text, $wc=false) { |
||
| 586 | $wc = ($wc) ? '' : '\*'; |
||
| 587 | $stopwords =& idx_get_stopwords(); |
||
| 588 | |||
| 589 | // prepare the text to be tokenized |
||
| 590 | $evt = new Doku_Event('INDEXER_TEXT_PREPARE', $text); |
||
| 591 | if ($evt->advise_before(true)) { |
||
| 592 | if (preg_match('/[^0-9A-Za-z ]/u', $text)) { |
||
| 593 | // handle asian chars as single words (may fail on older PHP version) |
||
| 594 | $asia = @preg_replace('/('.IDX_ASIAN.')/u', ' \1 ', $text); |
||
| 595 | if (!is_null($asia)) $text = $asia; // recover from regexp falure |
||
| 596 | } |
||
| 597 | } |
||
| 598 | $evt->advise_after(); |
||
| 599 | unset($evt); |
||
| 600 | |||
| 601 | $text = strtr($text, |
||
| 602 | array( |
||
| 603 | "\r" => ' ', |
||
| 604 | "\n" => ' ', |
||
| 605 | "\t" => ' ', |
||
| 606 | "\xC2\xAD" => '', //soft-hyphen |
||
| 607 | ) |
||
| 608 | ); |
||
| 609 | if (preg_match('/[^0-9A-Za-z ]/u', $text)) |
||
| 610 | $text = utf8_stripspecials($text, ' ', '\._\-:'.$wc); |
||
| 611 | |||
| 612 | $wordlist = explode(' ', $text); |
||
| 613 | foreach ($wordlist as $i => $word) { |
||
| 614 | $wordlist[$i] = (preg_match('/[^0-9A-Za-z]/u', $word)) ? |
||
| 615 | utf8_strtolower($word) : strtolower($word); |
||
| 616 | } |
||
| 617 | |||
| 618 | foreach ($wordlist as $i => $word) { |
||
| 619 | if ((!is_numeric($word) && strlen($word) < IDX_MINWORDLENGTH) |
||
| 620 | || array_search($word, $stopwords, true) !== false) |
||
| 621 | unset($wordlist[$i]); |
||
| 622 | } |
||
| 623 | return array_values($wordlist); |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the numeric PID of a page |
||
| 628 | * |
||
| 629 | * @param string $page The page to get the PID for |
||
| 630 | * @return bool|int The page id on success, false on error |
||
| 631 | */ |
||
| 632 | public function getPID($page) { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get the numeric PID of a page without locking the index. |
||
| 652 | * Only use this function when the index is already locked. |
||
| 653 | * |
||
| 654 | * @param string $page The page to get the PID for |
||
| 655 | * @return bool|int The page id on success, false on error |
||
| 656 | */ |
||
| 657 | protected function getPIDNoLock($page) { |
||
| 658 | // avoid expensive addIndexKey operation for the most recently requested pages by using a cache |
||
| 659 | if (isset($this->pidCache[$page])) return $this->pidCache[$page]; |
||
| 660 | $pid = $this->addIndexKey('page', '', $page); |
||
| 661 | // limit cache to 10 entries by discarding the oldest element as in DokuWiki usually only the most recently |
||
| 662 | // added item will be requested again |
||
| 663 | if (count($this->pidCache) > 10) array_shift($this->pidCache); |
||
| 664 | $this->pidCache[$page] = $pid; |
||
| 665 | return $pid; |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Get the page id of a numeric PID |
||
| 670 | * |
||
| 671 | * @param int $pid The PID to get the page id for |
||
| 672 | * @return string The page id |
||
| 673 | */ |
||
| 674 | public function getPageFromPID($pid) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Find pages in the fulltext index containing the words, |
||
| 680 | * |
||
| 681 | * The search words must be pre-tokenized, meaning only letters and |
||
| 682 | * numbers with an optional wildcard |
||
| 683 | * |
||
| 684 | * The returned array will have the original tokens as key. The values |
||
| 685 | * in the returned list is an array with the page names as keys and the |
||
| 686 | * number of times that token appears on the page as value. |
||
| 687 | * |
||
| 688 | * @param array $tokens list of words to search for |
||
| 689 | * @return array list of page names with usage counts |
||
| 690 | * |
||
| 691 | * @author Tom N Harris <[email protected]> |
||
| 692 | * @author Andreas Gohr <[email protected]> |
||
| 693 | */ |
||
| 694 | public function lookup(&$tokens) { |
||
| 695 | $result = array(); |
||
| 696 | $wids = $this->getIndexWords($tokens, $result); |
||
| 697 | if (empty($wids)) return array(); |
||
| 698 | // load known words and documents |
||
| 699 | $page_idx = $this->getIndex('page', ''); |
||
| 700 | $docs = array(); |
||
| 701 | foreach (array_keys($wids) as $wlen) { |
||
| 702 | $wids[$wlen] = array_unique($wids[$wlen]); |
||
| 703 | $index = $this->getIndex('i', $wlen); |
||
| 704 | foreach($wids[$wlen] as $ixid) { |
||
| 705 | if ($ixid < count($index)) |
||
| 706 | $docs["$wlen*$ixid"] = $this->parseTuples($page_idx, $index[$ixid]); |
||
| 707 | } |
||
| 708 | } |
||
| 709 | // merge found pages into final result array |
||
| 710 | $final = array(); |
||
| 711 | foreach ($result as $word => $res) { |
||
| 712 | $final[$word] = array(); |
||
| 713 | foreach ($res as $wid) { |
||
| 714 | // handle the case when ($ixid < count($index)) has been false |
||
| 715 | // and thus $docs[$wid] hasn't been set. |
||
| 716 | if (!isset($docs[$wid])) continue; |
||
| 717 | $hits = &$docs[$wid]; |
||
| 718 | foreach ($hits as $hitkey => $hitcnt) { |
||
| 719 | // make sure the document still exists |
||
| 720 | if (!page_exists($hitkey, '', false)) continue; |
||
| 721 | if (!isset($final[$word][$hitkey])) |
||
| 722 | $final[$word][$hitkey] = $hitcnt; |
||
| 723 | else |
||
| 724 | $final[$word][$hitkey] += $hitcnt; |
||
| 725 | } |
||
| 726 | } |
||
| 727 | } |
||
| 728 | return $final; |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Find pages containing a metadata key. |
||
| 733 | * |
||
| 734 | * The metadata values are compared as case-sensitive strings. Pass a |
||
| 735 | * callback function that returns true or false to use a different |
||
| 736 | * comparison function. The function will be called with the $value being |
||
| 737 | * searched for as the first argument, and the word in the index as the |
||
| 738 | * second argument. The function preg_match can be used directly if the |
||
| 739 | * values are regexes. |
||
| 740 | * |
||
| 741 | * @param string $key name of the metadata key to look for |
||
| 742 | * @param string $value search term to look for, must be a string or array of strings |
||
| 743 | * @param callback $func comparison function |
||
| 744 | * @return array lists with page names, keys are query values if $value is array |
||
| 745 | * |
||
| 746 | * @author Tom N Harris <[email protected]> |
||
| 747 | * @author Michael Hamann <[email protected]> |
||
| 748 | */ |
||
| 749 | public function lookupKey($key, &$value, $func=null) { |
||
| 750 | if (!is_array($value)) |
||
| 751 | $value_array = array($value); |
||
| 752 | else |
||
| 753 | $value_array =& $value; |
||
| 754 | |||
| 755 | // the matching ids for the provided value(s) |
||
| 756 | $value_ids = array(); |
||
| 757 | |||
| 758 | $metaname = idx_cleanName($key); |
||
| 759 | |||
| 760 | // get all words in order to search the matching ids |
||
| 761 | if ($key == 'title') { |
||
| 762 | $words = $this->getIndex('title', ''); |
||
| 763 | } else { |
||
| 764 | $words = $this->getIndex($metaname.'_w', ''); |
||
| 765 | } |
||
| 766 | |||
| 767 | if (!is_null($func)) { |
||
| 768 | foreach ($value_array as $val) { |
||
| 769 | foreach ($words as $i => $word) { |
||
| 770 | if (call_user_func_array($func, array($val, $word))) |
||
| 771 | $value_ids[$i][] = $val; |
||
| 772 | } |
||
| 773 | } |
||
| 774 | } else { |
||
| 775 | foreach ($value_array as $val) { |
||
| 776 | $xval = $val; |
||
| 777 | $caret = '^'; |
||
| 778 | $dollar = '$'; |
||
| 779 | // check for wildcards |
||
| 780 | if (substr($xval, 0, 1) == '*') { |
||
| 781 | $xval = substr($xval, 1); |
||
| 782 | $caret = ''; |
||
| 783 | } |
||
| 784 | if (substr($xval, -1, 1) == '*') { |
||
| 785 | $xval = substr($xval, 0, -1); |
||
| 786 | $dollar = ''; |
||
| 787 | } |
||
| 788 | if (!$caret || !$dollar) { |
||
| 789 | $re = $caret.preg_quote($xval, '/').$dollar; |
||
| 790 | foreach(array_keys(preg_grep('/'.$re.'/', $words)) as $i) |
||
| 791 | $value_ids[$i][] = $val; |
||
| 792 | } else { |
||
| 793 | if (($i = array_search($val, $words, true)) !== false) |
||
| 794 | $value_ids[$i][] = $val; |
||
| 795 | } |
||
| 796 | } |
||
| 797 | } |
||
| 798 | |||
| 799 | unset($words); // free the used memory |
||
| 800 | |||
| 801 | // initialize the result so it won't be null |
||
| 802 | $result = array(); |
||
| 803 | foreach ($value_array as $val) { |
||
| 804 | $result[$val] = array(); |
||
| 805 | } |
||
| 806 | |||
| 807 | $page_idx = $this->getIndex('page', ''); |
||
| 808 | |||
| 809 | // Special handling for titles |
||
| 810 | if ($key == 'title') { |
||
| 811 | foreach ($value_ids as $pid => $val_list) { |
||
| 812 | $page = $page_idx[$pid]; |
||
| 813 | foreach ($val_list as $val) { |
||
| 814 | $result[$val][] = $page; |
||
| 815 | } |
||
| 816 | } |
||
| 817 | } else { |
||
| 818 | // load all lines and pages so the used lines can be taken and matched with the pages |
||
| 819 | $lines = $this->getIndex($metaname.'_i', ''); |
||
| 820 | |||
| 821 | foreach ($value_ids as $value_id => $val_list) { |
||
| 822 | // parse the tuples of the form page_id*1:page2_id*1 and so on, return value |
||
| 823 | // is an array with page_id => 1, page2_id => 1 etc. so take the keys only |
||
| 824 | $pages = array_keys($this->parseTuples($page_idx, $lines[$value_id])); |
||
| 825 | foreach ($val_list as $val) { |
||
| 826 | $result[$val] = array_merge($result[$val], $pages); |
||
| 827 | } |
||
| 828 | } |
||
| 829 | } |
||
| 830 | if (!is_array($value)) $result = $result[$value]; |
||
| 831 | return $result; |
||
| 832 | } |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Find the index ID of each search term. |
||
| 836 | * |
||
| 837 | * The query terms should only contain valid characters, with a '*' at |
||
| 838 | * either the beginning or end of the word (or both). |
||
| 839 | * The $result parameter can be used to merge the index locations with |
||
| 840 | * the appropriate query term. |
||
| 841 | * |
||
| 842 | * @param array $words The query terms. |
||
| 843 | * @param array $result Set to word => array("length*id" ...) |
||
| 844 | * @return array Set to length => array(id ...) |
||
| 845 | * |
||
| 846 | * @author Tom N Harris <[email protected]> |
||
| 847 | */ |
||
| 848 | protected function getIndexWords(&$words, &$result) { |
||
| 849 | $tokens = array(); |
||
| 850 | $tokenlength = array(); |
||
| 851 | $tokenwild = array(); |
||
| 852 | foreach ($words as $word) { |
||
| 853 | $result[$word] = array(); |
||
| 854 | $caret = '^'; |
||
| 855 | $dollar = '$'; |
||
| 856 | $xword = $word; |
||
| 857 | $wlen = wordlen($word); |
||
| 858 | |||
| 859 | // check for wildcards |
||
| 860 | if (substr($xword, 0, 1) == '*') { |
||
| 861 | $xword = substr($xword, 1); |
||
| 862 | $caret = ''; |
||
| 863 | $wlen -= 1; |
||
| 864 | } |
||
| 865 | if (substr($xword, -1, 1) == '*') { |
||
| 866 | $xword = substr($xword, 0, -1); |
||
| 867 | $dollar = ''; |
||
| 868 | $wlen -= 1; |
||
| 869 | } |
||
| 870 | if ($wlen < IDX_MINWORDLENGTH && $caret && $dollar && !is_numeric($xword)) |
||
| 871 | continue; |
||
| 872 | if (!isset($tokens[$xword])) |
||
| 873 | $tokenlength[$wlen][] = $xword; |
||
| 874 | if (!$caret || !$dollar) { |
||
| 875 | $re = $caret.preg_quote($xword, '/').$dollar; |
||
| 876 | $tokens[$xword][] = array($word, '/'.$re.'/'); |
||
| 877 | if (!isset($tokenwild[$xword])) |
||
| 878 | $tokenwild[$xword] = $wlen; |
||
| 879 | } else { |
||
| 880 | $tokens[$xword][] = array($word, null); |
||
| 881 | } |
||
| 882 | } |
||
| 883 | asort($tokenwild); |
||
| 884 | // $tokens = array( base word => array( [ query term , regexp ] ... ) ... ) |
||
| 885 | // $tokenlength = array( base word length => base word ... ) |
||
| 886 | // $tokenwild = array( base word => base word length ... ) |
||
| 887 | $length_filter = empty($tokenwild) ? $tokenlength : min(array_keys($tokenlength)); |
||
| 888 | $indexes_known = $this->indexLengths($length_filter); |
||
| 889 | if (!empty($tokenwild)) sort($indexes_known); |
||
| 890 | // get word IDs |
||
| 891 | $wids = array(); |
||
| 892 | foreach ($indexes_known as $ixlen) { |
||
| 893 | $word_idx = $this->getIndex('w', $ixlen); |
||
| 894 | // handle exact search |
||
| 895 | if (isset($tokenlength[$ixlen])) { |
||
| 896 | foreach ($tokenlength[$ixlen] as $xword) { |
||
| 897 | $wid = array_search($xword, $word_idx, true); |
||
| 898 | if ($wid !== false) { |
||
| 899 | $wids[$ixlen][] = $wid; |
||
| 900 | foreach ($tokens[$xword] as $w) |
||
| 901 | $result[$w[0]][] = "$ixlen*$wid"; |
||
| 902 | } |
||
| 903 | } |
||
| 904 | } |
||
| 905 | // handle wildcard search |
||
| 906 | foreach ($tokenwild as $xword => $wlen) { |
||
| 907 | if ($wlen >= $ixlen) break; |
||
| 908 | foreach ($tokens[$xword] as $w) { |
||
| 909 | if (is_null($w[1])) continue; |
||
| 910 | foreach(array_keys(preg_grep($w[1], $word_idx)) as $wid) { |
||
| 911 | $wids[$ixlen][] = $wid; |
||
| 912 | $result[$w[0]][] = "$ixlen*$wid"; |
||
| 913 | } |
||
| 914 | } |
||
| 915 | } |
||
| 916 | } |
||
| 917 | return $wids; |
||
| 918 | } |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Return a list of all pages |
||
| 922 | * Warning: pages may not exist! |
||
| 923 | * |
||
| 924 | * @param string $key list only pages containing the metadata key (optional) |
||
| 925 | * @return array list of page names |
||
| 926 | * |
||
| 927 | * @author Tom N Harris <[email protected]> |
||
| 928 | */ |
||
| 929 | public function getPages($key=null) { |
||
| 930 | $page_idx = $this->getIndex('page', ''); |
||
| 931 | if (is_null($key)) return $page_idx; |
||
| 932 | |||
| 933 | $metaname = idx_cleanName($key); |
||
| 934 | |||
| 935 | // Special handling for titles |
||
| 936 | if ($key == 'title') { |
||
| 937 | $title_idx = $this->getIndex('title', ''); |
||
| 938 | array_splice($page_idx, count($title_idx)); |
||
| 939 | foreach ($title_idx as $i => $title) |
||
| 940 | if ($title === "") unset($page_idx[$i]); |
||
| 941 | return array_values($page_idx); |
||
| 942 | } |
||
| 943 | |||
| 944 | $pages = array(); |
||
| 945 | $lines = $this->getIndex($metaname.'_i', ''); |
||
| 946 | foreach ($lines as $line) { |
||
| 947 | $pages = array_merge($pages, $this->parseTuples($page_idx, $line)); |
||
| 948 | } |
||
| 949 | return array_keys($pages); |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Return a list of words sorted by number of times used |
||
| 954 | * |
||
| 955 | * @param int $min bottom frequency threshold |
||
| 956 | * @param int $max upper frequency limit. No limit if $max<$min |
||
| 957 | * @param int $minlen minimum length of words to count |
||
| 958 | * @param string $key metadata key to list. Uses the fulltext index if not given |
||
| 959 | * @return array list of words as the keys and frequency as values |
||
| 960 | * |
||
| 961 | * @author Tom N Harris <[email protected]> |
||
| 962 | */ |
||
| 963 | public function histogram($min=1, $max=0, $minlen=3, $key=null) { |
||
| 964 | if ($min < 1) |
||
| 965 | $min = 1; |
||
| 966 | if ($max < $min) |
||
| 967 | $max = 0; |
||
| 968 | |||
| 969 | $result = array(); |
||
| 970 | |||
| 971 | if ($key == 'title') { |
||
| 972 | $index = $this->getIndex('title', ''); |
||
| 973 | $index = array_count_values($index); |
||
| 974 | foreach ($index as $val => $cnt) { |
||
| 975 | if ($cnt >= $min && (!$max || $cnt <= $max) && strlen($val) >= $minlen) |
||
| 976 | $result[$val] = $cnt; |
||
| 977 | } |
||
| 978 | } |
||
| 979 | elseif (!is_null($key)) { |
||
| 980 | $metaname = idx_cleanName($key); |
||
| 981 | $index = $this->getIndex($metaname.'_i', ''); |
||
| 982 | $val_idx = array(); |
||
| 983 | foreach ($index as $wid => $line) { |
||
| 984 | $freq = $this->countTuples($line); |
||
| 985 | if ($freq >= $min && (!$max || $freq <= $max)) |
||
| 986 | $val_idx[$wid] = $freq; |
||
| 987 | } |
||
| 988 | if (!empty($val_idx)) { |
||
| 989 | $words = $this->getIndex($metaname.'_w', ''); |
||
| 990 | foreach ($val_idx as $wid => $freq) { |
||
| 991 | if (strlen($words[$wid]) >= $minlen) |
||
| 992 | $result[$words[$wid]] = $freq; |
||
| 993 | } |
||
| 994 | } |
||
| 995 | } |
||
| 996 | else { |
||
| 997 | $lengths = idx_listIndexLengths(); |
||
| 998 | foreach ($lengths as $length) { |
||
| 999 | if ($length < $minlen) continue; |
||
| 1000 | $index = $this->getIndex('i', $length); |
||
| 1001 | $words = null; |
||
| 1002 | foreach ($index as $wid => $line) { |
||
| 1003 | $freq = $this->countTuples($line); |
||
| 1004 | if ($freq >= $min && (!$max || $freq <= $max)) { |
||
| 1005 | if ($words === null) |
||
| 1006 | $words = $this->getIndex('w', $length); |
||
| 1007 | $result[$words[$wid]] = $freq; |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | } |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | arsort($result); |
||
| 1014 | return $result; |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Lock the indexer. |
||
| 1019 | * |
||
| 1020 | * @author Tom N Harris <[email protected]> |
||
| 1021 | * |
||
| 1022 | * @return bool|string |
||
| 1023 | */ |
||
| 1024 | protected function lock() { |
||
| 1025 | global $conf; |
||
| 1026 | $status = true; |
||
| 1027 | $run = 0; |
||
| 1028 | $lock = $conf['lockdir'].'/_indexer.lock'; |
||
| 1029 | while (!@mkdir($lock, $conf['dmode'])) { |
||
| 1030 | usleep(50); |
||
| 1031 | if(is_dir($lock) && time()-@filemtime($lock) > 60*5){ |
||
| 1032 | // looks like a stale lock - remove it |
||
| 1033 | if (!@rmdir($lock)) { |
||
| 1034 | $status = "removing the stale lock failed"; |
||
| 1035 | return false; |
||
| 1036 | } else { |
||
| 1037 | $status = "stale lock removed"; |
||
| 1038 | } |
||
| 1039 | }elseif($run++ == 1000){ |
||
| 1040 | // we waited 5 seconds for that lock |
||
| 1041 | return false; |
||
| 1042 | } |
||
| 1043 | } |
||
| 1044 | if (!empty($conf['dperm'])) { |
||
| 1045 | chmod($lock, $conf['dperm']); |
||
| 1046 | } |
||
| 1047 | return $status; |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Release the indexer lock. |
||
| 1052 | * |
||
| 1053 | * @author Tom N Harris <[email protected]> |
||
| 1054 | * |
||
| 1055 | * @return bool |
||
| 1056 | */ |
||
| 1057 | protected function unlock() { |
||
| 1058 | global $conf; |
||
| 1059 | @rmdir($conf['lockdir'].'/_indexer.lock'); |
||
|
1 ignored issue
–
show
|
|||
| 1060 | return true; |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Retrieve the entire index. |
||
| 1065 | * |
||
| 1066 | * The $suffix argument is for an index that is split into |
||
| 1067 | * multiple parts. Different index files should use different |
||
| 1068 | * base names. |
||
| 1069 | * |
||
| 1070 | * @param string $idx name of the index |
||
| 1071 | * @param string $suffix subpart identifier |
||
| 1072 | * @return array list of lines without CR or LF |
||
| 1073 | * |
||
| 1074 | * @author Tom N Harris <[email protected]> |
||
| 1075 | */ |
||
| 1076 | protected function getIndex($idx, $suffix) { |
||
| 1077 | global $conf; |
||
| 1078 | $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; |
||
| 1079 | if (!file_exists($fn)) return array(); |
||
| 1080 | return file($fn, FILE_IGNORE_NEW_LINES); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Replace the contents of the index with an array. |
||
| 1085 | * |
||
| 1086 | * @param string $idx name of the index |
||
| 1087 | * @param string $suffix subpart identifier |
||
| 1088 | * @param array $lines list of lines without LF |
||
| 1089 | * @return bool If saving succeeded |
||
| 1090 | * |
||
| 1091 | * @author Tom N Harris <[email protected]> |
||
| 1092 | */ |
||
| 1093 | protected function saveIndex($idx, $suffix, &$lines) { |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Retrieve a line from the index. |
||
| 1110 | * |
||
| 1111 | * @param string $idx name of the index |
||
| 1112 | * @param string $suffix subpart identifier |
||
| 1113 | * @param int $id the line number |
||
| 1114 | * @return string a line with trailing whitespace removed |
||
| 1115 | * |
||
| 1116 | * @author Tom N Harris <[email protected]> |
||
| 1117 | */ |
||
| 1118 | protected function getIndexKey($idx, $suffix, $id) { |
||
| 1119 | global $conf; |
||
| 1120 | $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; |
||
| 1121 | if (!file_exists($fn)) return ''; |
||
| 1122 | $fh = @fopen($fn, 'r'); |
||
| 1123 | if (!$fh) return ''; |
||
| 1124 | $ln = -1; |
||
| 1125 | while (($line = fgets($fh)) !== false) { |
||
| 1126 | if (++$ln == $id) break; |
||
| 1127 | } |
||
| 1128 | fclose($fh); |
||
| 1129 | return rtrim((string)$line); |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Write a line into the index. |
||
| 1134 | * |
||
| 1135 | * @param string $idx name of the index |
||
| 1136 | * @param string $suffix subpart identifier |
||
| 1137 | * @param int $id the line number |
||
| 1138 | * @param string $line line to write |
||
| 1139 | * @return bool If saving succeeded |
||
| 1140 | * |
||
| 1141 | * @author Tom N Harris <[email protected]> |
||
| 1142 | */ |
||
| 1143 | protected function saveIndexKey($idx, $suffix, $id, $line) { |
||
| 1144 | global $conf; |
||
| 1145 | if (substr($line, -1) != "\n") |
||
| 1146 | $line .= "\n"; |
||
| 1147 | $fn = $conf['indexdir'].'/'.$idx.$suffix; |
||
| 1148 | $fh = @fopen($fn.'.tmp', 'w'); |
||
| 1149 | if (!$fh) return false; |
||
| 1150 | $ih = @fopen($fn.'.idx', 'r'); |
||
| 1151 | if ($ih) { |
||
| 1152 | $ln = -1; |
||
| 1153 | while (($curline = fgets($ih)) !== false) { |
||
| 1154 | fwrite($fh, (++$ln == $id) ? $line : $curline); |
||
| 1155 | } |
||
| 1156 | if ($id > $ln) { |
||
| 1157 | while ($id > ++$ln) |
||
| 1158 | fwrite($fh, "\n"); |
||
| 1159 | fwrite($fh, $line); |
||
| 1160 | } |
||
| 1161 | fclose($ih); |
||
| 1162 | } else { |
||
| 1163 | $ln = -1; |
||
| 1164 | while ($id > ++$ln) |
||
| 1165 | fwrite($fh, "\n"); |
||
| 1166 | fwrite($fh, $line); |
||
| 1167 | } |
||
| 1168 | fclose($fh); |
||
| 1169 | if (isset($conf['fperm'])) |
||
| 1170 | chmod($fn.'.tmp', $conf['fperm']); |
||
| 1171 | io_rename($fn.'.tmp', $fn.'.idx'); |
||
| 1172 | return true; |
||
| 1173 | } |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Retrieve or insert a value in the index. |
||
| 1177 | * |
||
| 1178 | * @param string $idx name of the index |
||
| 1179 | * @param string $suffix subpart identifier |
||
| 1180 | * @param string $value line to find in the index |
||
| 1181 | * @return int|bool line number of the value in the index or false if writing the index failed |
||
| 1182 | * |
||
| 1183 | * @author Tom N Harris <[email protected]> |
||
| 1184 | */ |
||
| 1185 | protected function addIndexKey($idx, $suffix, $value) { |
||
| 1186 | $index = $this->getIndex($idx, $suffix); |
||
| 1187 | $id = array_search($value, $index, true); |
||
| 1188 | if ($id === false) { |
||
| 1189 | $id = count($index); |
||
| 1190 | $index[$id] = $value; |
||
| 1191 | if (!$this->saveIndex($idx, $suffix, $index)) { |
||
| 1192 | trigger_error("Failed to write $idx index", E_USER_ERROR); |
||
| 1193 | return false; |
||
| 1194 | } |
||
| 1195 | } |
||
| 1196 | return $id; |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Get the list of lengths indexed in the wiki. |
||
| 1201 | * |
||
| 1202 | * Read the index directory or a cache file and returns |
||
| 1203 | * a sorted array of lengths of the words used in the wiki. |
||
| 1204 | * |
||
| 1205 | * @author YoBoY <[email protected]> |
||
| 1206 | * |
||
| 1207 | * @return array |
||
| 1208 | */ |
||
| 1209 | protected function listIndexLengths() { |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Get the word lengths that have been indexed. |
||
| 1215 | * |
||
| 1216 | * Reads the index directory and returns an array of lengths |
||
| 1217 | * that there are indices for. |
||
| 1218 | * |
||
| 1219 | * @author YoBoY <[email protected]> |
||
| 1220 | * |
||
| 1221 | * @param array|int $filter |
||
| 1222 | * @return array |
||
| 1223 | */ |
||
| 1224 | protected function indexLengths($filter) { |
||
| 1225 | global $conf; |
||
| 1226 | $idx = array(); |
||
| 1227 | if (is_array($filter)) { |
||
| 1228 | // testing if index files exist only |
||
| 1229 | $path = $conf['indexdir']."/i"; |
||
| 1230 | foreach ($filter as $key => $value) { |
||
| 1231 | if (file_exists($path.$key.'.idx')) |
||
| 1232 | $idx[] = $key; |
||
| 1233 | } |
||
| 1234 | } else { |
||
| 1235 | $lengths = idx_listIndexLengths(); |
||
| 1236 | foreach ($lengths as $key => $length) { |
||
| 1237 | // keep all the values equal or superior |
||
| 1238 | if ((int)$length >= (int)$filter) |
||
| 1239 | $idx[] = $length; |
||
| 1240 | } |
||
| 1241 | } |
||
| 1242 | return $idx; |
||
| 1243 | } |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Insert or replace a tuple in a line. |
||
| 1247 | * |
||
| 1248 | * @author Tom N Harris <[email protected]> |
||
| 1249 | * |
||
| 1250 | * @param string $line |
||
| 1251 | * @param string|int $id |
||
| 1252 | * @param int $count |
||
| 1253 | * @return string |
||
| 1254 | */ |
||
| 1255 | protected function updateTuple($line, $id, $count) { |
||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * Split a line into an array of tuples. |
||
| 1272 | * |
||
| 1273 | * @author Tom N Harris <[email protected]> |
||
| 1274 | * @author Andreas Gohr <[email protected]> |
||
| 1275 | * |
||
| 1276 | * @param array $keys |
||
| 1277 | * @param string $line |
||
| 1278 | * @return array |
||
| 1279 | */ |
||
| 1280 | protected function parseTuples(&$keys, $line) { |
||
| 1281 | $result = array(); |
||
| 1282 | if ($line == '') return $result; |
||
| 1283 | $parts = explode(':', $line); |
||
| 1284 | foreach ($parts as $tuple) { |
||
| 1285 | if ($tuple === '') continue; |
||
| 1286 | list($key, $cnt) = explode('*', $tuple); |
||
| 1287 | if (!$cnt) continue; |
||
| 1288 | $key = $keys[$key]; |
||
| 1289 | if (!$key) continue; |
||
| 1290 | $result[$key] = $cnt; |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Sum the counts in a list of tuples. |
||
| 1297 | * |
||
| 1298 | * @author Tom N Harris <[email protected]> |
||
| 1299 | * |
||
| 1300 | * @param string $line |
||
| 1301 | * @return int |
||
| 1302 | */ |
||
| 1303 | protected function countTuples($line) { |
||
| 1313 | } |
||
| 1314 | |||
| 1608 |
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.