Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F2757586
Convert hooks.txt to YAML format for T115338
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
Akangupt
Oct 22 2015, 5:59 PM
2015-10-22 17:59:38 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None
Convert hooks.txt to YAML format for T115338
View Options
<?php
$hookFile = 'hooks.txt'; # complete path for file hooks.txt
$data = file_get_contents( $hookFile );
# 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 );
# comment the explanatory text
$explanatoryText = preg_replace( '~^(?!$)|\G\R~m', '# $0', $text[0] );
file_put_contents( $hookFile, $explanatoryText );
wfHooksInYaml( $text[1], $hookFile );
function wfHooksInYaml( $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', '# $0', $hook );
file_put_contents( $hookFile, $explanatoryText, FILE_APPEND );
} 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";
# $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_contents( $hookFile, $string, FILE_APPEND );
# $matches[5] has all the arguments.
splitArguments( $matches[5], $hookFile );
file_put_contents( $hookFile, "\n\n", FILE_APPEND );
}
}
}
}
# 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( $string, $hookFile ) {
file_put_contents( $hookFile, "\n arguments:", FILE_APPEND );
$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_contents( $hookFile, $string, FILE_APPEND );
}
}
}
?>
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2769447
Default Alt Text
Convert hooks.txt to YAML format for T115338 (4 KB)
Attached To
Mode
P2218 Convert hooks.txt to YAML format for T115338
Attached
Detach File
Event Timeline
Log In to Comment