Skip to content
wiki.fftac.org

wiki.fftac.org Core - Source Excerpt 03

Back to wiki.fftac.org Core

Summary

This source excerpt preserves a bounded section of Wiki.FFTAC.org/wp-content/plugins/wiki-fftac-core/wiki-fftac-core.php so readers can inspect the evidence without opening the full source file.

**Source path:** Wiki.FFTAC.org/wp-content/plugins/wiki-fftac-core/wiki-fftac-core.php

/**
 * Build a lightweight page record from the packaged index.
 *
 * @return array<string,mixed>
 */
function aiwikis_core_get_content_page_record_from_index( array $record, bool $include_body = false ): array {
	if ( $include_body ) {
		$file = aiwikis_core_get_content_page_file_from_index_record( $record );
		if ( '' !== $file ) {
			return aiwikis_core_get_content_page_record( $file );
		}
	}

	$frontmatter = isset( $record['frontmatter'] ) && is_array( $record['frontmatter'] ) ? $record['frontmatter'] : array();

	return array(
		'slug'          => isset( $record['slug'] ) ? aiwikis_core_normalize_content_slug( (string) $record['slug'] ) : '',
		'title'         => isset( $record['title'] ) ? (string) $record['title'] : '',
		'route'         => isset( $record['route'] ) ? (string) $record['route'] : '',
		'route_aliases' => isset( $record['route_aliases'] ) && is_array( $record['route_aliases'] ) ? $record['route_aliases'] : array(),
		'file'          => isset( $record['file'] ) ? (string) $record['file'] : '',
		'hash'          => isset( $record['hash'] ) ? (string) $record['hash'] : '',
		'frontmatter'   => $frontmatter,
		'body'          => (string) ( $record['body_preview'] ?? '' ),
		'description'   => isset( $record['description'] ) ? (string) $record['description'] : aiwikis_core_content_page_description( $frontmatter, (string) ( $record['body_preview'] ?? '' ) ),
	);
}

/**
 * Normalize a content filename into a public slug.
 */
function aiwikis_core_slug_from_content_filename( string $path ): string {
	$name = pathinfo( $path, PATHINFO_FILENAME );
	$name = preg_replace( '/^\d+-/', '', $name );
	return aiwikis_core_normalize_content_slug( (string) $name );
}

/**
 * Normalize a content slug while preserving route path segments.
 */
function aiwikis_core_normalize_content_slug( string $slug ): string {
	$slug = trim( str_replace( '\\', '/', $slug ) );
	$slug = trim( $slug, '/' );

	if ( '' === $slug ) {
		return 'home';
	}

	if ( 'home' === strtolower( $slug ) ) {
		return 'home';
	}

	$parts      = preg_split( '#/+#', $slug );
	$sanitized  = array();
	foreach ( is_array( $parts ) ? $parts : array( $slug ) as $part ) {
		$part = sanitize_title( (string) $part );
		if ( '' !== $part ) {
			$sanitized[] = $part;
		}
	}

	return empty( $sanitized ) ? 'home' : implode( '/', $sanitized );
}

/**
 * Parse a frontmatter value.
 *
 * @return string|array<int,string>
 */
function aiwikis_core_parse_frontmatter_value( string $value ) {
	$value = trim( $value );
	$value = trim( $value, '"' );
	$value = trim( $value, "'" );
	return $value;
}

/**
 * Parse a content markdown file into metadata and body.
 *
 * @return array{frontmatter:array<string,mixed>,body:string,hash:string,file:string}
 */
function aiwikis_core_parse_content_page_file( string $path ): array {
	$raw = file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	if ( false === $raw ) {
		$raw = '';
	}
	if ( str_starts_with( $raw, "\xEF\xBB\xBF" ) ) {
		$raw = substr( $raw, 3 );
	}

	$frontmatter = array();
	$body        = $raw;
	$lines       = preg_split( '/\R/', $raw );

	if ( is_array( $lines ) && isset( $lines[0] ) && '---' === trim( $lines[0] ) ) {
		$current_key = '';
		for ( $i = 1; $i < count( $lines ); $i++ ) {
			$line = $lines[ $i ];
			if ( '---' === trim( $line ) ) {
				$body = implode( "\n", array_slice( $lines, $i + 1 ) );
				break;
			}

			if ( preg_match( '/^([A-Za-z0-9_]+):\s*(.*)$/', $line, $matches ) ) {
				$current_key = $matches[1];
				$value       = trim( $matches[2] );
				if ( '' === $value ) {
					$frontmatter[ $current_key ] = array();
				} else {
					$frontmatter[ $current_key ] = aiwikis_core_parse_frontmatter_value( $value );
				}
				continue;
			}

			if ( '' !== $current_key && preg_match( '/^\s*-\s*(.+)$/', $line, $matches ) ) {
				if ( ! isset( $frontmatter[ $current_key ] ) || ! is_array( $frontmatter[ $current_key ] ) ) {
					$frontmatter[ $current_key ] = array();
				}
				$frontmatter[ $current_key ][] = aiwikis_core_parse_frontmatter_value( $matches[1] );
			}
		}
	}

	return array(
		'frontmatter' => $frontmatter,
		'body'        => trim( $body ),
		'hash'        => hash( 'sha256', $raw ),
		'file'        => basename( $path ),
	);
}

/**
 * Return legacy route aliases for a Markdown page.
 *
 * @return array<int,string>
 */
function aiwikis_core_get_content_page_route_aliases( array $frontmatter ): array {
	$aliases = array();
	$raw     = isset( $frontmatter['route_aliases'] ) ? $frontmatter['route_aliases'] : array();

	if ( is_string( $raw ) ) {
		$raw = array( $raw );
	}

	foreach ( is_array( $raw ) ? $raw : array() as $alias ) {
		$alias = aiwikis_core_normalize_content_slug( (string) $alias );
		if ( '' !== $alias && 'home' !== $alias ) {
			$aliases[] = $alias;
		}
	}

	return array_values( array_unique( $aliases ) );
}

/**
 * Resolve a parsed page record from a Markdown file.
 *
 * @return array{slug:string,title:string,route:string,route_aliases:array<int,string>,file:string,hash:string,frontmatter:array<string,mixed>,body:string}
 */
function aiwikis_core_get_content_page_record( string $file ): array {
	$parsed      = aiwikis_core_parse_content_page_file( $file );
	$frontmatter = $parsed['frontmatter'];
	$title       = isset( $frontmatter['title'] ) ? (string) $frontmatter['title'] : aiwikis_core_get_first_markdown_heading( $parsed['body'], pathinfo( $file, PATHINFO_FILENAME ) );
	$slug        = isset( $frontmatter['slug'] ) ? aiwikis_core_normalize_content_slug( (string) $frontmatter['slug'] ) : aiwikis_core_slug_from_content_filename( $file );
	$route       = 'home' === $slug ? '/' : '/' . $slug . '/';

	return array(
		'slug'          => $slug,
		'title'         => $title,
		'route'         => $route,
		'route_aliases' => aiwikis_core_get_content_page_route_aliases( $frontmatter ),
		'file'          => $parsed['file'],
		'hash'          => $parsed['hash'],
		'frontmatter'   => $frontmatter,
		'body'          => $parsed['body'],
	);
}

/**
 * Return public content page records.
 *
 * @return array<int,array<string,mixed>>
 */
function aiwikis_core_get_content_page_records(): array {
	$records = array();

	foreach ( aiwikis_core_get_content_page_files() as $file ) {
		$records[] = aiwikis_core_get_content_page_record( $file );
	}

	return $records;
}

/**
 * Return public content routes from the current Markdown page index.
 *
 * @return array<int,string>
 */
function aiwikis_core_get_public_content_routes(): array {
	$index = aiwikis_core_read_content_pages_index();
	if ( isset( $index['routes'] ) && is_array( $index['routes'] ) ) {
		return array_values( array_unique( array_map( 'strval', $index['routes'] ) ) );
	}

	$routes = array();

	foreach ( aiwikis_core_get_content_page_records() as $record ) {
		$route = isset( $record['route'] ) ? (string) $record['route'] : '';
		if ( '' !== $route ) {
			$routes[] = $route;
		}
	}

	return array_values( array_unique( $routes ) );
}

/**
 * Return a scalar frontmatter value.
 *
 * @param array<string,mixed> $frontmatter Parsed frontmatter.
 */
function aiwikis_core_frontmatter_string( array $frontmatter, string $key ): string {
	if ( ! isset( $frontmatter[ $key ] ) || is_array( $frontmatter[ $key ] ) ) {
		return '';
	}

	return trim( wp_strip_all_tags( (string) $frontmatter[ $key ] ) );
}

/**
 * Return whether a frontmatter flag is truthy.
 *
 * @param array<string,mixed> $frontmatter Parsed frontmatter.
 */
function aiwikis_core_frontmatter_bool( array $frontmatter, string $key ): bool {
	$value = isset( $frontmatter[ $key ] ) ? $frontmatter[ $key ] : false;

	if ( is_bool( $value ) ) {
		return $value;
	}

	if ( is_numeric( $value ) ) {
		return 0 !== (int) $value;
	}

	$normalized = strtolower( trim( (string) $value ) );
	return in_array( $normalized, array( '1', 'true', 'yes', 'y', 'on', 'noindex' ), true );
}

/**
 * Build a short public-safe search description for a Markdown-backed page.
 *
 * @param array<string,mixed> $frontmatter Parsed frontmatter.
 */
function aiwikis_core_content_page_description( array $frontmatter, string $body = '' ): string {
	foreach ( array( 'summary', 'meta_description', 'description' ) as $field ) {
		$description = aiwikis_core_frontmatter_string( $frontmatter, $field );
		if ( '' !== $description ) {
			return $description;
		}
	}

	foreach ( preg_split( '/\R/', $body ) as $line ) {
		$line = trim( wp_strip_all_tags( (string) $line ) );
		if ( '' === $line || preg_match( '/^(#|[-*]|\||```|~~~|>)/', $line ) ) {
			continue;
		}

		return wp_trim_words( $line, 32, '...' );
	}

	return 'Reviewed wiki.fftac.org memory page for WP-FFTAC navigation, source evidence, and AI-readable retrieval.';
}

/**
 * Return public route/search records for humans and machines.
 *
 * @return array<int,array<string,mixed>>
 */
function aiwikis_core_get_public_search_records(): array {
	static $memoized_records = null;