Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F2911921
Convert hooks.txt to YAML format for T115338
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Authored By
Akangupt
Nov 3 2015, 4:45 PM
2015-11-03 16:45:22 (UTC+0)
Size
5 KB
Referenced Files
None
Subscribers
None
Convert hooks.txt to YAML format for T115338
View Options
<?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 {
$hooksData = file_get_contents( $hookFile );
}
# convert all the tabs in four spaces because YAML forbids the tabs.
$data = preg_replace( "@\t@", " ", $hooksData );
$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( '~^(.*)$~m', '# $0', $text[0] );
fwrite( $handle, $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( '~^(.*)$~m', '# $0', $hook );
fwrite( $handle, $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 );
}
}
}
fwrite( $handle, $string );
# $matches[5] has all the arguments.
splitArguments( $handle, $matches[5], $hookFile );
fwrite( $handle, "\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 ) {
fwrite( $handle, "\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 );
fwrite( $handle, $string );
}
}
}
?>
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2923203
Default Alt Text
Convert hooks.txt to YAML format for T115338 (5 KB)
Attached To
Mode
P2218 Convert hooks.txt to YAML format for T115338
Attached
Detach File
Event Timeline
Log In to Comment