<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59062] trunk: Code Modernization: Remove xml_set_object() in AtomParser::parse().</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59062">59062</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59062","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-09-18 21:18:54 +0000 (Wed, 18 Sep 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Code Modernization: Remove xml_set_object() in AtomParser::parse().
The XML Parser extension still supports a quite dated mechanism for method based callbacks, where the object is first set via `xml_set_object()` and the callbacks are then set by passing only the name of the method to the relevant parameters on any of the `xml_set_*_handler()` functions.
{{{
xml_set_object( $parser, $my_obj );
xml_set_character_data_handler( $parser, 'method_name_on_my_obj' );
}}}
Passing proper callables to the `xml_set_*_handler()` functions has been supported for the longest time and is cross-version compatible. So the above code is 100% equivalent to:
{{{
xml_set_character_data_handler( $parser, [$my_obj, 'method_name_on_my_obj'] );
}}}
The mechanism of setting the callbacks with `xml_set_object()` has now been deprecated as of PHP 8.4, in favour of passing proper callables to the `xml_set_*_handler()` functions. This is also means that calling the `xml_set_object()` function is deprecated as well.
This commit fixes this deprecation for the `AtomParser::parse()` method.
This change is safeguarded via the new `AtomParser_Parse_Test` class.
Notes:
* Though this is "officially" an external library, this package is no longer externally maintained. The code style of the fix in the source file is in line with the existing code style for the file.
* It appears that this class is not actually used by WP Core itself, so it could be considered to deprecate the class. However, as the class is not currently deprecated, safeguarding the change with a test seemed prudent.
* The fixture used for the test reuses a fixture from the original package: https://code.google.com/archive/p/phpatomlib/source/default/source
* The new test class follows the recommended test format (naming convention of the class, `@covers` tag at class level, only testing one method) as per Trac tickets 62004 / 53010.
Refs:
* https://wiki.php.net/rfc/deprecations_php_8_4#xml_set_object_and_xml_set_handler_with_string_method_names
* https://www.php.net/manual/en/function.xml-set-object.php
* https://www.php.net/manual/en/ref.xml.php
Follow-up to <a href="https://core.trac.wordpress.org/changeset/5951">[5951]</a>.
Props jrf.
See <a href="https://core.trac.wordpress.org/ticket/62061">#62061</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesatomlibphp">trunk/src/wp-includes/atomlib.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitdatafeedAtomParser_Parse_Testxml">trunk/tests/phpunit/data/feed/AtomParser_Parse_Test.xml</a></li>
<li>trunk/tests/phpunit/tests/atomlib/</li>
<li><a href="#trunktestsphpunittestsatomlibAtomParser_Parse_Testphp">trunk/tests/phpunit/tests/atomlib/AtomParser_Parse_Test.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesatomlibphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/atomlib.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/atomlib.php 2024-09-18 21:04:48 UTC (rev 59061)
+++ trunk/src/wp-includes/atomlib.php 2024-09-18 21:18:54 UTC (rev 59062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,14 +157,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $parser = xml_parser_create_ns();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- xml_set_object($parser, $this);
- xml_set_element_handler($parser, "start_element", "end_element");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ xml_set_element_handler($parser, array($this, "start_element"), array($this, "end_element"));
</ins><span class="cx" style="display: block; padding: 0 10px"> xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
</span><span class="cx" style="display: block; padding: 0 10px"> xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- xml_set_character_data_handler($parser, "cdata");
- xml_set_default_handler($parser, "_default");
- xml_set_start_namespace_decl_handler($parser, "start_ns");
- xml_set_end_namespace_decl_handler($parser, "end_ns");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ xml_set_character_data_handler($parser, array($this, "cdata"));
+ xml_set_default_handler($parser, array($this, "_default"));
+ xml_set_start_namespace_decl_handler($parser, array($this, "start_ns"));
+ xml_set_end_namespace_decl_handler($parser, array($this, "end_ns"));
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->content = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunitdatafeedAtomParser_Parse_Testxml"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/feed/AtomParser_Parse_Test.xml</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/feed/AtomParser_Parse_Test.xml (rev 0)
+++ trunk/tests/phpunit/data/feed/AtomParser_Parse_Test.xml 2024-09-18 21:18:54 UTC (rev 59062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,54 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Author: Sam Ruby <rubys@intertwingly.net>
+-->
+
+<!--
+ Description: No errors should be produced by the extensive feed
+ Expect: !Error
+-->
+
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <title type="text">dive into mark</title>
+ <subtitle type="html">
+ A <em>lot</em> of effort
+ went into making this effortless
+ </subtitle>
+ <updated>2005-07-11T12:29:29Z</updated>
+ <id>tag:example.org,2003:3</id>
+ <link rel="alternate" type="text/html"
+ hreflang="en" href="http://example.org/"/>
+ <link rel="self" type="application/atom+xml"
+ href="http://example.org/feed.atom"/>
+ <rights>Copyright (c) 2003, Mark Pilgrim</rights>
+ <generator uri="http://www.example.com/" version="1.0">
+ Example Toolkit
+ </generator>
+ <entry>
+ <title>Atom draft-07 snapshot</title>
+ <link rel="alternate" type="text/html"
+ href="http://example.org/2005/04/02/atom"/>
+ <link rel="enclosure" type="audio/mpeg" length="1337"
+ href="http://example.org/audio/ph34r_my_podcast.mp3"/>
+ <id>tag:example.org,2003:3.2397</id>
+ <updated>2005-07-11T12:29:29Z</updated>
+ <published>2003-12-13T08:29:29-04:00</published>
+ <author>
+ <name>Mark Pilgrim</name>
+ <uri>http://example.org/</uri>
+ <email>f8dy@example.com</email>
+ </author>
+ <contributor>
+ <name>Sam Ruby</name>
+ </contributor>
+ <contributor>
+ <name>Joe Gregorio</name>
+ </contributor>
+ <content type="xhtml" xml:lang="en"
+ xml:base="http://diveintomark.org/">
+ <div xmlns="http://www.w3.org/1999/xhtml">
+ <p><i>[Update: The Atom draft is finished.]</i></p>
+ </div>
+ </content>
+ </entry>
+</feed>
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/data/feed/AtomParser_Parse_Test.xml
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsatomlibAtomParser_Parse_Testphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/atomlib/AtomParser_Parse_Test.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/atomlib/AtomParser_Parse_Test.php (rev 0)
+++ trunk/tests/phpunit/tests/atomlib/AtomParser_Parse_Test.php 2024-09-18 21:18:54 UTC (rev 59062)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,75 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering AtomParser functionality.
+ *
+ * @package WordPress
+ * @subpackage AtomLib
+ */
+
+/**
+ * Test Atom Syndication Format.
+ *
+ * @requires extension xml
+ *
+ * @covers AtomParser::parse
+ */
+final class AtomParser_Parse_Test extends WP_UnitTestCase {
+
+ /**
+ * Ensure the class being tested is loaded.
+ */
+ public function set_up() {
+ require_once dirname( __DIR__, 4 ) . '/src/wp-includes/atomlib.php';
+ }
+
+ /**
+ * Test that the `AtomParser::parse()` method correctly sets callback functions to handle certain parts of the XML.
+ *
+ * Safeguards handling of the PHP 8.4 deprecation of `xml_set_object()`.
+ */
+ public function test_parse_sets_handlers() {
+ $atom = new class() extends AtomParser {
+ public $start_element_call_counter = 0;
+ public $end_element_call_counter = 0;
+ public $start_ns_call_counter = 0;
+ public $end_ns_call_counter = 0;
+ public $cdata_call_counter = 0;
+ public $default_call_counter = 0;
+
+ // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase -- Overloading property of upstream class.
+ public $FILE = __DIR__ . '/../../data/feed/AtomParser_Parse_Test.xml';
+
+ public function start_element( $parser, $name, $attrs ) {
+ ++$this->start_element_call_counter;
+ }
+ public function end_element( $parser, $name ) {
+ ++$this->end_element_call_counter;
+ }
+ public function start_ns( $parser, $prefix, $uri ) {
+ ++$this->start_ns_call_counter;
+ }
+ public function end_ns( $parser, $prefix ) {
+ ++$this->end_ns_call_counter;
+ }
+ public function cdata( $parser, $data ) {
+ ++$this->cdata_call_counter;
+ }
+ public function _default( $parser, $data ) {
+ ++$this->default_call_counter;
+ }
+ };
+
+ $this->assertTrue( $atom->parse(), 'Parsing of XML file failed' );
+
+ // Ensure no errors were logged.
+ $this->assertNull( $atom->error, 'Unexpected errors encountered' );
+
+ $msg = '%s() handler did not get called expected nr of times';
+ $this->assertSame( 28, $atom->start_element_call_counter, sprintf( $msg, 'start_element' ) );
+ $this->assertSame( 28, $atom->end_element_call_counter, sprintf( $msg, 'end_element' ) );
+ $this->assertSame( 2, $atom->start_ns_call_counter, sprintf( $msg, 'start_ns' ) );
+ $this->assertSame( 0, $atom->end_ns_call_counter, sprintf( $msg, 'end_ns' ) );
+ $this->assertSame( 57, $atom->cdata_call_counter, sprintf( $msg, 'cdata' ) );
+ $this->assertSame( 2, $atom->default_call_counter, sprintf( $msg, '_default' ) );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/atomlib/AtomParser_Parse_Test.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>