/**
  * Search for a title in a certain PID
  *
  * @param int $searchPid Page id in which to search subpages matching title
  * @param string $title Title to search for
  * @return array First entry is uid, second entry is the row selected, including information about the page as a mount point.
  * @see findPageBySegment()
  */
 protected function findPageBySegmentAndPid($searchPid, $title)
 {
     // List of "pages" fields to traverse for a "directory title" in the speaking URL (only from RootLine!!)
     $segTitleFieldList = $this->conf['segTitleFieldList'] ? $this->conf['segTitleFieldList'] : TX_REALURL_SEGTITLEFIELDLIST_DEFAULT;
     $selList = t3lib_div::uniqueList('uid,pid,doktype,mount_pid,mount_pid_ol,tx_realurl_exclude,' . $segTitleFieldList);
     $segTitleFieldArray = t3lib_div::trimExplode(',', $segTitleFieldList, 1);
     // page select object - used to analyse mount points.
     $sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
     /** @var t3lib_pageSelect $sys_page */
     // Build an array with encoded values from the segTitleFieldArray of the subpages
     // First we find field values from the default language
     // Pages are selected in menu order and if duplicate titles are found the first takes precedence!
     $titles = array();
     // array(title => uid);
     $exclude = array();
     $uidTrack = array();
     /** @noinspection PhpUndefinedMethodInspection */
     $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selList, 'pages', 'pid=' . intval($searchPid) . ' AND deleted=0 AND doktype!=255', '', 'sorting');
     /** @noinspection PhpUndefinedMethodInspection */
     while (false != ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result))) {
         // Mount points
         $mount_info = $sys_page->getMountPointInfo($row['uid'], $row);
         if (is_array($mount_info)) {
             // There is a valid mount point.
             if ($mount_info['overlay']) {
                 // Overlay mode: Substitute WHOLE record
                 /** @noinspection PhpUndefinedMethodInspection */
                 $result2 = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selList, 'pages', 'uid=' . intval($mount_info['mount_pid']) . ' AND deleted=0 AND doktype!=255');
                 /** @noinspection PhpUndefinedMethodInspection */
                 $mp_row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result2);
                 if (is_array($mp_row)) {
                     $row = $mp_row;
                 } else {
                     unset($row);
                     // If the mount point could not be fetched, unset the row
                 }
             }
             $row['_IS_MOUNTPOINT'] = $mount_info;
         }
         // Collect titles from selected row
         if (is_array($row)) {
             if ($row['tx_realurl_exclude']) {
                 // segment is excluded
                 $exclude[] = $row;
             }
             // Process titles. Note that excluded segments are also searched
             // otherwise they will never be found
             $uidTrack[$row['uid']] = $row;
             foreach ($segTitleFieldArray as $fieldName) {
                 if ($row[$fieldName]) {
                     $encodedTitle = $this->encodeTitle($row[$fieldName]);
                     if (!isset($titles[$fieldName][$encodedTitle])) {
                         $titles[$fieldName][$encodedTitle] = $row['uid'];
                     }
                 }
             }
         }
     }
     /** @noinspection PhpUndefinedMethodInspection */
     $GLOBALS['TYPO3_DB']->sql_free_result($result);
     // We have to search the language overlay too, if: a) the language isn't the default (0), b) if it's not set (-1)
     $uidTrackKeys = array_keys($uidTrack);
     $language = $this->pObj->getDetectedLanguage();
     if ($language != 0) {
         foreach ($uidTrackKeys as $l_id) {
             /** @noinspection PhpUndefinedMethodInspection */
             $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(TX_REALURL_SEGTITLEFIELDLIST_PLO, 'pages_language_overlay', 'pid=' . intval($l_id) . ' AND deleted=0' . ($language > 0 ? ' AND sys_language_uid=' . $language : ''));
             /** @noinspection PhpUndefinedMethodInspection */
             while (false != ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result))) {
                 foreach ($segTitleFieldArray as $fieldName) {
                     if ($row[$fieldName]) {
                         $encodedTitle = $this->encodeTitle($row[$fieldName]);
                         if (!isset($titles[$fieldName][$encodedTitle])) {
                             $titles[$fieldName][$encodedTitle] = $l_id;
                         }
                     }
                 }
             }
             /** @noinspection PhpUndefinedMethodInspection */
             $GLOBALS['TYPO3_DB']->sql_free_result($result);
         }
     }
     // Merge titles
     $segTitleFieldArray = array_reverse($segTitleFieldArray);
     // To observe the priority order...
     $allTitles = array();
     foreach ($segTitleFieldArray as $fieldName) {
         if (is_array($titles[$fieldName])) {
             $allTitles = t3lib_div::array_merge($allTitles, $titles[$fieldName]);
         }
     }
     // Return
     $encodedTitle = $this->encodeTitle($title);
     $possibleMatch = array();
     if (isset($allTitles[$encodedTitle])) {
         if (!$uidTrack[$allTitles[$encodedTitle]]['tx_realurl_exclude']) {
             return array($allTitles[$encodedTitle], $uidTrack[$allTitles[$encodedTitle]], false, array());
         }
         $possibleMatch = $uidTrack[$allTitles[$encodedTitle]];
     }
     return array(false, false, $exclude, $possibleMatch);
 }