123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- class PhpGtkDoc_Search2_Index
- {
- protected static $arReserved = array(
- 'atk', 'gtk', 'gdk', 'scn', 'pango', 'method',
- 'property', 'prop', 'field', 'enum', 'signal', 'constructor'
- );
- protected static $arReservedMethods = array(
- 'get', 'set'
- );
-
- public static function createIndex($strDocumentationDirectory, $strIndexFile)
- {
- if (!file_exists($strDocumentationDirectory) || !is_dir($strDocumentationDirectory)) {
- throw new Exception('Documentation directory does not exist: ' . $strDocumentationDirectory);
- }
- if ((file_exists($strIndexFile) && !is_writable($strIndexFile))
- || (!file_exists($strIndexFile) && !is_writable(dirname($strIndexFile)))) {
- throw new Exception('Index file is not writable: ' . $strIndexFile);
- }
- file_put_contents($strIndexFile,
- serialize(
- self::buildIndexFromFiles(
- self::getFiles($strDocumentationDirectory),
- $strDocumentationDirectory
- )
- )
- );
- }
-
- protected static function buildIndexFromFiles($arFiles, $strDocumentationDirectory)
- {
- $arIndex = array();
- $arCamelCaseWords = self::getCamelCaseWords(
- self::getTitles(
- self::getTitleFiles(
- $arFiles
- ),
- $strDocumentationDirectory
- )
- );
- foreach ($arFiles as $strFile) {
- $strBaseFile = basename($strFile);
- $strBaseFile = substr($strBaseFile, 0, strrpos($strBaseFile, '.'));
- $arPieces = explode('.', $strBaseFile);
-
-
-
- $arNewPieces = array();
- $nCountWords = count($arPieces);
- $nWordPos = -1;
- foreach ($arPieces as $strWord) {
- $nWordPos++;
- if ($nWordPos == $nCountWords - 1 && $nCountWords > 2) {
-
- $nPriority = 2;
- } else {
-
- $nPriority = 1;
- }
- $arNewPieces[$nPriority][] = $strWord;
- if (isset($arCamelCaseWords[$strWord])) {
- $arNewPieces[$nPriority] = array_merge($arNewPieces[$nPriority], $arCamelCaseWords[$strWord]);
- }
- $arMethodPieces = explode( '_', $strWord);
- if (count($arMethodPieces) > 1) {
-
-
- $arNewPieces[2] = array_merge($arNewPieces[2], $arMethodPieces);
- if (count( $arMethodPieces) > 2) {
-
- foreach ($arMethodPieces as $nId => $strPiece) {
- if ($nId < count($arMethodPieces) - 1) {
- $arNewPieces[2][] = $strPiece . '_' . $arMethodPieces[$nId + 1];
- }
- }
- }
- }
- }
-
- foreach ($arNewPieces as $nPriority => $arPriorityPieces) {
- foreach ($arPriorityPieces as $strPiece) {
- $arIndex[$strPiece][$nPriority][] = $strFile;
- }
- }
- }
-
- ksort($arIndex);
- return $arIndex;
- }
-
- protected static function getFiles($strDocumentationDirectory)
- {
- $strDir = getcwd();
- chdir($strDocumentationDirectory);
-
-
- $arFiles = glob('*.php');
- chdir($strDir);
- if (count($arFiles) == 0) {
- throw new Exception('No files found in ' . $strDocumentationDirectory);
- }
- return $arFiles;
- }
-
- protected static function getTitleFiles($arFiles)
- {
- $nFiles = count($arFiles);
- for ($nA = 0; $nA < $nFiles; $nA++) {
-
- if (!preg_match('/^[a-z0-9]+\\.(enum\\.)?[a-z0-9]+\\.[a-z]+$/', basename($arFiles[$nA]))) {
- unset($arFiles[$nA]);
- }
- }
- return $arFiles;
- }
-
- protected static function getTitles($arFiles, $strDocumentationDirectory)
- {
- $arTitles = array();
- foreach ($arFiles as $strFile) {
- if (substr($strFile, -4) === '.php') {
-
- if (preg_match('/manualHeader\\(\"(.+)\"\\,/', file_get_contents($strDocumentationDirectory . '/' . $strFile), $arMatches)) {
- $arTitles[] = $arMatches[1];
- }
- } else {
- if (preg_match('/<title>(.+)<\\/title>/', file_get_contents($strDocumentationDirectory . '/' . $strFile), $arMatches)) {
- $arTitles[] = $arMatches[1];
- }
- }
- }
- return $arTitles;
- }
-
- protected static function getCamelCaseWords($arTitles)
- {
- $arSplit = array();
- foreach ($arTitles as $strTitle) {
- if (strpos($strTitle, ' ') !== false) {
-
-
- continue;
- }
- $arSplit[strtolower($strTitle)] = self::varyWords(self::splitCamelCaseWord($strTitle));
- }
- return $arSplit;
- }
-
- protected static function splitCamelCaseWord($strWord)
- {
- $strWords = preg_replace('/([A-Z])/', ' \\1', $strWord);
- return explode(' ', strtolower($strWords));
- }
-
- protected static function varyWords($arWords)
- {
- $arVariations = $arWords;
- for ($nA = 0; $nA < count($arWords); $nA++) {
- $strVariation = '';
- for ($nB = $nA; $nB < count($arWords); $nB++) {
- $strVariation .= $arWords[$nB];
- $arVariations[] = $strVariation;
- }
- }
- return array_unique($arVariations);
- }
- }
- ?>
|