<?php
/**
* Place this script in the doc/ directory of MediaWiki and run it.
*
* This script creats a new file hooks.yaml from hooks.txt to make it machine readable.
* @author akangupt < akanksha2879@gmail.com >
*/
$hookFile = 'hooks.txt'; # complete path for file hooks.txt
if ( !file_exists( "hooks.txt" ) ) {
die( "Error: hooks.txt not found\n" );
} else {
$data = file_get_contents( $hookFile );
}
$hookYamlFile = 'hooks.yaml';
# create hooks.yaml
$handle = fopen( $hookYamlFile, 'w' ) or die( 'Cannot open file: '.$hookYamlFile.' \n' );
# split all the text in two parts.
# Explanatory text(text before the hooks) and rest of the text
$text = preg_split( "/(?=\'\w+\:*\w*\:*\w*\'\:)/", $data, 2 );
if ( $text === "" ) {
echo "Warning: No explanatory text found in hooks.txt\n";
}
# comment the explanatory text
$explanatoryText = preg_replace( '~^(?!$)|\G\R~m'.*)$~m', '# $0', $text[0] );
file_put_contentsfwrite( $hookFileandle, $explanatoryText );
wfHooksInYaml( $handle, $text[1], $hookFile );
function wfHooksInYaml( $handle, $string, $hookFile ) {
# extract each hook from rest of the text
$hooks = preg_split( "/\r?\n\r?\n/", $string );
foreach ( $hooks as $hook ) {
# if extracted hook doesn't start with a hook name
# i.e. explanatory part so comment that
$isText = preg_match( "@^\s*\'(\w+\:*\w*\:*\w*)\'\s*\:@msi", $hook );
if ( $isText != 1 ) {
$explanatoryText = preg_replace( '~^(?!$)|\G\R~m'.*)$~m', '# $0', $hook );
file_put_contentsfwrite( $hookFile, $explanatoryTextandle, FILE_APPEND );$explanatoryText );
} else {
$pattern = "@\s*\'(\w+\:*\w*\:*\w*)\'\s*:\s*(DEPRECATED)*!*(\r?\n)*\s*(.*?)(?:(?=\n\&\\$\w+\s*\:|\n\\$\w+\s*\:|\Z))\s*(?:(.*?)\Z)@msi";
# $spaces - indentation which should be added to each line
$spaces = " ";
if ( preg_match( $pattern, $hook, $matches ) ) {
# $matches[1] has hook's name.
$string = $matches[1].":\n";
if ( $matches[1] === "" ) {
die( "Error: Couldn't recognize the hook\n" );
}
# $matches[2] has information if hook is deprecated or not.
if( $matches[2] != '' ) {
$string .= " DEPRECATED";
if( $matches[3] === '' ) {
# if $matches[3] is empty
# i.e. there is a suggestion to use some other hooks.
# $matches[4] contains all the text
# which is between 'DEPRECATED!' keyword and explanation of arguments.
# So, to get the suggestion of other hook,
# split $matches[4] in description of hook and suggestion of other hook.
$text = preg_split( "@\.\n@", $matches[4], 2 );
# text[0] contains the suggestion of other hook to be used.
$string .= multiline( $text[0].".", $spaces );
# text[1] contains description of hook.
$string .= "\n Description".multiline( $text[1], $spaces );
} else {
# if $matches[3] is not empty i.e. hook is deprecated
# but there is no suggestion of other hook to be used in place of this hook.
$string .= ":\n";
# $matches[4] contains description of hook
$string .= " Description".multiline( $matches[4], $spaces );
}
} else {
if ( $matches[4] != '' ) {
if (preg_match( "@^(\&\\$\w+\s*\:|\\$\w+\s*\:)@", $matches[4]) ) {
# This covers the case when a hook doesn't have a description.
# i.e. $matches[4] contains an argument.
$string .= " Description:";
$matches[5] = $matches[4]."\n".$matches[5];
} else {
$string .= " Description".multiline( $matches[4], $spaces );
}
}
}
file_put_contentsfwrite( $hookFile, $stringandle, FILE_APPEND );$string );
# $matches[5] has all the arguments.
splitArguments( $handle, $matches[5], $hookFile );
file_put_contentsfwrite( $hookFile, "\n\n"andle, FILE_APPEND );"\n\n" );
}
}
}
}
# returns a string with literal block if
# a string is multiline or has a character that
# doesn't belong to A-Z a-z 0-9 _ . space
function multiline( $finalString, $spaces ) {
$string = preg_match( "@\n(?!$)|[^\w\s\.]@", $finalString ) ? (": |\n") : (": ");
if ( $string === ": " ) {
# don't add indentation in the string if string is singal line.
return $string.$finalString;
}
$pattern = "/\n\s\s|\n(?!$)/";
$replacement = "\n$spaces";
$finalString = preg_replace( $pattern, $replacement, $finalString );
return $string.$spaces.$finalString;
}
# function to split the arguments
function splitArguments( $handle, $string, $hookFile ) {
file_put_contentsfwrite( $hookFile, "\n arguments:"andle, FILE_APPEND );"\n arguments:" );
$pattern = "@\s*(\&\\$\w+|\\$\w+)\s*\:\s*(.*?(?:(?=\n\&\\$\w+\s*\:|\n\\$\w+\s*\:)|\Z))@msi";
if ( preg_match_all( $pattern, $string, $matches, PREG_SET_ORDER )) {
$spaces = " ";
foreach ( $matches as $match ) {
$string = "\n - \"".$match[1]."\"";
$string .= multiline( $match[2], $spaces );
file_put_contentsfwrite( $hookFile, $stringandle, FILE_APPEND );$string );
}
}
}
?>