Difference between revisions of "MediaWiki code snippets"
m (→Domain-based default redirect) |
({{code}}) |
||
Line 3: | Line 3: | ||
=== Raw wikitext content === | === Raw wikitext content === | ||
This function returns the raw content specified in ''$text'', it can be callled any time from extension-setup or after. If the ''$save'' parameter is supplied it will bring up a download dialog with teh default name set to ''$save'', otherwise it will download and open unprompted. If ''$expand'' is set to true, then any templates, parser-functions or variables in the content will be expanded. | This function returns the raw content specified in ''$text'', it can be callled any time from extension-setup or after. If the ''$save'' parameter is supplied it will bring up a download dialog with teh default name set to ''$save'', otherwise it will download and open unprompted. If ''$expand'' is set to true, then any templates, parser-functions or variables in the content will be expanded. | ||
− | <php> | + | |
+ | {{code|<php> | ||
function raw($text,$expand = false,$save = false) { | function raw($text,$expand = false,$save = false) { | ||
global $wgParser; | global $wgParser; | ||
Line 14: | Line 15: | ||
die; | die; | ||
} | } | ||
− | </php> | + | </php>}} |
=== Return an HTTP error page === | === Return an HTTP error page === | ||
− | <php> | + | {{code|<php> |
while(@ob_end_clean()); | while(@ob_end_clean()); | ||
header('HTTP/1.0 404 Not Found'); | header('HTTP/1.0 404 Not Found'); | ||
Line 25: | Line 26: | ||
echo $err; | echo $err; | ||
die; | die; | ||
− | </php> | + | </php>}} |
=== Domain-based default redirect === | === Domain-based default redirect === | ||
If no page title is specified, redirect to a default depending on the domain name in the requested URL. In this example requests to ''abc.org'' or any of it's subdomains with no title specified will redirect to the ''Welcome to ABC'' article, and any requests to the exact domain of ''www.xyz.org'' without a title end up at the ''XYZ Home'' article. Titleless requests to any other domain which resolves to this example wiki will be unaffected and left to the rest of the configuration to deal with. This code should be executed early in the ''LocalSettings'' before any extensions are included, but after ''$wgServer'' is defined. | If no page title is specified, redirect to a default depending on the domain name in the requested URL. In this example requests to ''abc.org'' or any of it's subdomains with no title specified will redirect to the ''Welcome to ABC'' article, and any requests to the exact domain of ''www.xyz.org'' without a title end up at the ''XYZ Home'' article. Titleless requests to any other domain which resolves to this example wiki will be unaffected and left to the rest of the configuration to deal with. This code should be executed early in the ''LocalSettings'' before any extensions are included, but after ''$wgServer'' is defined. | ||
− | <php> | + | {{code|<php> |
$d = $_SERVER['SERVER_NAME']; | $d = $_SERVER['SERVER_NAME']; | ||
$t = $_REQUEST['title']; | $t = $_REQUEST['title']; | ||
Line 37: | Line 38: | ||
if ($d == 'www.xyz.com') header("Location: $wgServer/XYZ_Home") && die; | if ($d == 'www.xyz.com') header("Location: $wgServer/XYZ_Home") && die; | ||
} | } | ||
− | </php> | + | </php>}} |
== Articles == | == Articles == | ||
=== Get article content === | === Get article content === | ||
− | <php> | + | {{code|<php> |
$title = Title::newFromText($titleText); | $title = Title::newFromText($titleText); | ||
$article = new Article($title); | $article = new Article($title); | ||
$wikitext = $article->getContent(); | $wikitext = $article->getContent(); | ||
− | </php> | + | </php>}} |
=== Edit or create === | === Edit or create === | ||
Line 51: | Line 52: | ||
== Using the parser == | == Using the parser == | ||
=== Parse wikitext === | === Parse wikitext === | ||
− | <php> | + | {{code|<php> |
$html = $wgParser->parse($wikitext,$title,new ParserOptions(),true,true)->getText(); | $html = $wgParser->parse($wikitext,$title,new ParserOptions(),true,true)->getText(); | ||
− | </php> | + | </php>}} |
+ | |||
=== Expand templates only === | === Expand templates only === | ||
− | <php> | + | {{code|<php> |
$wikitext = $wgParser->preprocess($wikitext,$title,new ParserOptions()); | $wikitext = $wgParser->preprocess($wikitext,$title,new ParserOptions()); | ||
− | </php> | + | </php>}} |
== MediaWiki Environment == | == MediaWiki Environment == | ||
=== Article title === | === Article title === | ||
This should be called at an appropriate time such as from the ''OutputPageBeforeHTML'' hook. | This should be called at an appropriate time such as from the ''OutputPageBeforeHTML'' hook. | ||
− | <php> | + | {{code|<php> |
$wgOut->setPageTitle('foo'); | $wgOut->setPageTitle('foo'); | ||
− | </php> | + | </php>}} |
== Article queries == | == Article queries == | ||
=== List article titles from a category === | === List article titles from a category === | ||
− | <php> | + | {{code|<php> |
$db = &wfGetDB(DB_SLAVE); | $db = &wfGetDB(DB_SLAVE); | ||
$cl = $db->tableName('categorylinks'); | $cl = $db->tableName('categorylinks'); | ||
$result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$category' ORDER BY cl_sortkey"); | $result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$category' ORDER BY cl_sortkey"); | ||
while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText(); | while ($row = mysql_fetch_row($result)) $titleText = Title::newFromID($row[0])->getText(); | ||
− | </php> | + | </php>}} |
=== Check if a title is in a category === | === Check if a title is in a category === | ||
− | <php> | + | {{code|<php> |
function inCat($title,$cat) { | function inCat($title,$cat) { | ||
if (!is_object($title)) $title = Title::newFromText($title)); | if (!is_object($title)) $title = Title::newFromText($title)); | ||
Line 85: | Line 87: | ||
return is_array(mysql_fetch_row($result)); | return is_array(mysql_fetch_row($result)); | ||
} | } | ||
− | </php> | + | </php>}} |
== Misc == | == Misc == | ||
=== examineBraces === | === examineBraces === | ||
This function returns an array of the brace structure found in the passed wikitext parameter. | This function returns an array of the brace structure found in the passed wikitext parameter. | ||
− | <php> | + | {{code|<php> |
function examineBraces(&$content) { | function examineBraces(&$content) { | ||
$braces = array(); | $braces = array(); | ||
Line 112: | Line 114: | ||
} | } | ||
return $braces; | return $braces; | ||
− | }</php> | + | } |
+ | </php>}} | ||
+ | |||
The following input, | The following input, | ||
− | <pre | + | |
+ | {{code|<pre> | ||
foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz | foo{{#bar:baz|biz{{foo|shmoo}}}}{{moo}}baz | ||
− | </pre> | + | </pre>}} |
+ | |||
Gives the following array: | Gives the following array: | ||
− | <pre | + | {{code|<pre> |
Array( | Array( | ||
[0] => Array( | [0] => Array( | ||
Line 141: | Line 147: | ||
) | ) | ||
) | ) | ||
− | </pre> | + | </pre>}} |
[[Category:MediaWiki]][[Category:Examples]][[Category:Examples/PHP]] | [[Category:MediaWiki]][[Category:Examples]][[Category:Examples/PHP]] |
Revision as of 08:22, 26 June 2007
Contents
Returning content to the client
Raw wikitext content
This function returns the raw content specified in $text, it can be callled any time from extension-setup or after. If the $save parameter is supplied it will bring up a download dialog with teh default name set to $save, otherwise it will download and open unprompted. If $expand is set to true, then any templates, parser-functions or variables in the content will be expanded.
Return an HTTP error page
Domain-based default redirect
If no page title is specified, redirect to a default depending on the domain name in the requested URL. In this example requests to abc.org or any of it's subdomains with no title specified will redirect to the Welcome to ABC article, and any requests to the exact domain of www.xyz.org without a title end up at the XYZ Home article. Titleless requests to any other domain which resolves to this example wiki will be unaffected and left to the rest of the configuration to deal with. This code should be executed early in the LocalSettings before any extensions are included, but after $wgServer is defined.
Articles
Get article content
Edit or create
Using the parser
Parse wikitext
Expand templates only
MediaWiki Environment
Article title
This should be called at an appropriate time such as from the OutputPageBeforeHTML hook.
Article queries
List article titles from a category
Check if a title is in a category
Misc
examineBraces
This function returns an array of the brace structure found in the passed wikitext parameter.
') {
$brace =& $braces[$depths[$depth-1]]; $brace[LENGTH] = $match[0][1]-$brace[OFFSET]+2; $brace[DEPTH] = $depth--; } else { $depths[$depth++] = count($braces); $braces[] = array( NAME => $match[1][0], OFFSET => $match[0][1] ); } } return $braces; } </php>}}
The following input,
Gives the following array: